api.c 2.3 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581445824458344584445854458644587445884458944590445914459244593445944459544596445974459844599446004460144602446034460444605446064460744608446094461044611446124461344614446154461644617446184461944620446214462244623446244462544626446274462844629446304463144632446334463444635446364463744638446394464044641446424464344644446454464644647446484464944650446514465244653446544465544656446574465844659446604466144662446634466444665446664466744668446694467044671446724467344674446754467644677446784467944680446814468244683446844468544686446874468844689446904469144692446934469444695446964469744698446994470044701447024470344704447054470644707447084470944710447114471244713447144471544716447174471844719447204472144722447234472444725447264472744728447294473044731447324473344734447354473644737447384473944740447414474244743447444474544746447474474844749447504475144752447534475444755447564475744758447594476044761447624476344764447654476644767447684476944770447714477244773447744477544776447774477844779447804478144782447834478444785447864478744788447894479044791447924479344794447954479644797447984479944800448014480244803448044480544806448074480844809448104481144812448134481444815448164481744818448194482044821448224482344824448254482644827448284482944830448314483244833448344483544836448374483844839448404484144842448434484444845448464484744848448494485044851448524485344854448554485644857448584485944860448614486244863448644486544866448674486844869448704487144872448734487444875448764487744878448794488044881448824488344884448854488644887448884488944890448914489244893448944489544896448974489844899449004490144902449034490444905449064490744908449094491044911449124491344914449154491644917449184491944920449214492244923449244492544926449274492844929449304493144932449334493444935449364493744938449394494044941449424494344944449454494644947449484494944950449514495244953449544495544956449574495844959449604496144962449634496444965449664496744968449694497044971449724497344974449754497644977449784497944980449814498244983449844498544986449874498844989449904499144992449934499444995449964499744998449994500045001450024500345004450054500645007450084500945010450114501245013450144501545016450174501845019450204502145022450234502445025450264502745028450294503045031450324503345034450354503645037450384503945040450414504245043450444504545046450474504845049450504505145052450534505445055450564505745058450594506045061450624506345064450654506645067450684506945070450714507245073450744507545076450774507845079450804508145082450834508445085450864508745088450894509045091450924509345094450954509645097450984509945100451014510245103451044510545106451074510845109451104511145112451134511445115451164511745118451194512045121451224512345124451254512645127451284512945130451314513245133451344513545136451374513845139451404514145142451434514445145451464514745148451494515045151451524515345154451554515645157451584515945160451614516245163451644516545166451674516845169451704517145172451734517445175451764517745178451794518045181451824518345184451854518645187451884518945190451914519245193451944519545196451974519845199452004520145202452034520445205452064520745208452094521045211452124521345214452154521645217452184521945220452214522245223452244522545226452274522845229452304523145232452334523445235452364523745238452394524045241452424524345244452454524645247452484524945250452514525245253452544525545256452574525845259452604526145262452634526445265452664526745268452694527045271452724527345274452754527645277452784527945280452814528245283452844528545286452874528845289452904529145292452934529445295452964529745298452994530045301453024530345304453054530645307453084530945310453114531245313453144531545316453174531845319453204532145322453234532445325453264532745328453294533045331453324533345334453354533645337453384533945340453414534245343453444534545346453474534845349453504535145352453534535445355453564535745358453594536045361453624536345364453654536645367453684536945370453714537245373453744537545376453774537845379453804538145382453834538445385453864538745388453894539045391453924539345394453954539645397453984539945400454014540245403454044540545406454074540845409454104541145412454134541445415454164541745418454194542045421454224542345424454254542645427454284542945430454314543245433454344543545436454374543845439454404544145442454434544445445454464544745448454494545045451454524545345454454554545645457454584545945460454614546245463454644546545466454674546845469454704547145472454734547445475454764547745478454794548045481454824548345484454854548645487454884548945490454914549245493454944549545496454974549845499455004550145502455034550445505455064550745508455094551045511455124551345514455154551645517455184551945520455214552245523455244552545526455274552845529455304553145532455334553445535455364553745538455394554045541455424554345544455454554645547455484554945550455514555245553455544555545556455574555845559455604556145562455634556445565455664556745568455694557045571455724557345574455754557645577455784557945580455814558245583455844558545586455874558845589455904559145592455934559445595455964559745598455994560045601456024560345604456054560645607456084560945610456114561245613456144561545616456174561845619456204562145622456234562445625456264562745628456294563045631456324563345634456354563645637456384563945640456414564245643456444564545646456474564845649456504565145652456534565445655456564565745658456594566045661456624566345664456654566645667456684566945670456714567245673456744567545676456774567845679456804568145682456834568445685456864568745688456894569045691456924569345694456954569645697456984569945700457014570245703457044570545706457074570845709457104571145712457134571445715457164571745718457194572045721457224572345724457254572645727457284572945730457314573245733457344573545736457374573845739457404574145742457434574445745457464574745748457494575045751457524575345754457554575645757457584575945760457614576245763457644576545766457674576845769457704577145772457734577445775457764577745778457794578045781457824578345784457854578645787457884578945790457914579245793457944579545796457974579845799458004580145802458034580445805458064580745808458094581045811458124581345814458154581645817458184581945820458214582245823458244582545826458274582845829458304583145832458334583445835458364583745838458394584045841458424584345844458454584645847458484584945850458514585245853458544585545856458574585845859458604586145862458634586445865458664586745868458694587045871458724587345874458754587645877458784587945880458814588245883458844588545886458874588845889458904589145892458934589445895458964589745898458994590045901459024590345904459054590645907459084590945910459114591245913459144591545916459174591845919459204592145922459234592445925459264592745928459294593045931459324593345934459354593645937459384593945940459414594245943459444594545946459474594845949459504595145952459534595445955459564595745958459594596045961459624596345964459654596645967459684596945970459714597245973459744597545976459774597845979459804598145982459834598445985459864598745988459894599045991459924599345994459954599645997459984599946000460014600246003460044600546006460074600846009460104601146012460134601446015460164601746018460194602046021460224602346024460254602646027460284602946030460314603246033460344603546036460374603846039460404604146042460434604446045460464604746048460494605046051460524605346054460554605646057460584605946060460614606246063460644606546066460674606846069460704607146072460734607446075460764607746078460794608046081460824608346084460854608646087460884608946090460914609246093460944609546096460974609846099461004610146102461034610446105461064610746108461094611046111461124611346114461154611646117461184611946120461214612246123461244612546126461274612846129461304613146132461334613446135461364613746138461394614046141461424614346144461454614646147461484614946150461514615246153461544615546156461574615846159461604616146162461634616446165461664616746168461694617046171461724617346174461754617646177461784617946180461814618246183461844618546186461874618846189461904619146192461934619446195461964619746198461994620046201462024620346204462054620646207462084620946210462114621246213462144621546216462174621846219462204622146222462234622446225462264622746228462294623046231462324623346234462354623646237462384623946240462414624246243462444624546246462474624846249462504625146252462534625446255462564625746258462594626046261462624626346264462654626646267462684626946270462714627246273462744627546276462774627846279462804628146282462834628446285462864628746288462894629046291462924629346294462954629646297462984629946300463014630246303463044630546306463074630846309463104631146312463134631446315463164631746318463194632046321463224632346324463254632646327463284632946330463314633246333463344633546336463374633846339463404634146342463434634446345463464634746348463494635046351463524635346354463554635646357463584635946360463614636246363463644636546366463674636846369463704637146372463734637446375463764637746378463794638046381463824638346384463854638646387463884638946390463914639246393463944639546396463974639846399464004640146402464034640446405464064640746408464094641046411464124641346414464154641646417464184641946420464214642246423464244642546426464274642846429464304643146432464334643446435464364643746438464394644046441464424644346444464454644646447464484644946450464514645246453464544645546456464574645846459464604646146462464634646446465464664646746468464694647046471464724647346474464754647646477464784647946480464814648246483464844648546486464874648846489464904649146492464934649446495464964649746498464994650046501465024650346504465054650646507465084650946510465114651246513465144651546516465174651846519465204652146522465234652446525465264652746528465294653046531465324653346534465354653646537465384653946540465414654246543465444654546546465474654846549465504655146552465534655446555465564655746558465594656046561465624656346564465654656646567465684656946570465714657246573465744657546576465774657846579465804658146582465834658446585465864658746588465894659046591465924659346594465954659646597465984659946600466014660246603466044660546606466074660846609466104661146612466134661446615466164661746618466194662046621466224662346624466254662646627466284662946630466314663246633466344663546636466374663846639466404664146642466434664446645466464664746648466494665046651466524665346654466554665646657466584665946660466614666246663466644666546666466674666846669466704667146672466734667446675466764667746678466794668046681466824668346684466854668646687466884668946690466914669246693466944669546696466974669846699467004670146702467034670446705467064670746708467094671046711467124671346714467154671646717467184671946720467214672246723467244672546726467274672846729467304673146732467334673446735467364673746738467394674046741467424674346744467454674646747467484674946750467514675246753467544675546756467574675846759467604676146762467634676446765467664676746768467694677046771467724677346774467754677646777467784677946780467814678246783467844678546786467874678846789467904679146792467934679446795467964679746798467994680046801468024680346804468054680646807468084680946810468114681246813468144681546816468174681846819468204682146822468234682446825468264682746828468294683046831468324683346834468354683646837468384683946840468414684246843468444684546846468474684846849468504685146852468534685446855468564685746858468594686046861468624686346864468654686646867468684686946870468714687246873468744687546876468774687846879468804688146882468834688446885468864688746888468894689046891468924689346894468954689646897468984689946900469014690246903469044690546906469074690846909469104691146912469134691446915469164691746918469194692046921469224692346924469254692646927469284692946930469314693246933469344693546936469374693846939469404694146942469434694446945469464694746948469494695046951469524695346954469554695646957469584695946960469614696246963469644696546966469674696846969469704697146972469734697446975469764697746978469794698046981469824698346984469854698646987469884698946990469914699246993469944699546996469974699846999470004700147002470034700447005470064700747008470094701047011470124701347014470154701647017470184701947020470214702247023470244702547026470274702847029470304703147032470334703447035470364703747038470394704047041470424704347044470454704647047470484704947050470514705247053470544705547056470574705847059470604706147062470634706447065470664706747068470694707047071470724707347074470754707647077470784707947080470814708247083470844708547086470874708847089470904709147092470934709447095470964709747098470994710047101471024710347104471054710647107471084710947110471114711247113471144711547116471174711847119471204712147122471234712447125471264712747128471294713047131471324713347134471354713647137471384713947140471414714247143471444714547146471474714847149471504715147152471534715447155471564715747158471594716047161471624716347164471654716647167471684716947170471714717247173471744717547176471774717847179471804718147182471834718447185471864718747188471894719047191471924719347194471954719647197471984719947200472014720247203472044720547206472074720847209472104721147212472134721447215472164721747218472194722047221472224722347224472254722647227472284722947230472314723247233472344723547236472374723847239472404724147242472434724447245472464724747248472494725047251472524725347254472554725647257472584725947260472614726247263472644726547266472674726847269472704727147272472734727447275472764727747278472794728047281472824728347284472854728647287472884728947290472914729247293472944729547296472974729847299473004730147302473034730447305473064730747308473094731047311473124731347314473154731647317473184731947320473214732247323473244732547326473274732847329473304733147332473334733447335473364733747338473394734047341473424734347344473454734647347473484734947350473514735247353473544735547356473574735847359473604736147362473634736447365473664736747368473694737047371473724737347374473754737647377473784737947380473814738247383473844738547386473874738847389473904739147392473934739447395473964739747398473994740047401474024740347404474054740647407474084740947410474114741247413474144741547416474174741847419474204742147422474234742447425474264742747428474294743047431474324743347434474354743647437474384743947440474414744247443474444744547446474474744847449474504745147452474534745447455474564745747458474594746047461474624746347464474654746647467474684746947470474714747247473474744747547476474774747847479474804748147482474834748447485474864748747488474894749047491474924749347494474954749647497474984749947500475014750247503475044750547506475074750847509475104751147512475134751447515475164751747518475194752047521475224752347524475254752647527475284752947530475314753247533475344753547536475374753847539475404754147542475434754447545475464754747548475494755047551475524755347554475554755647557475584755947560475614756247563475644756547566475674756847569475704757147572475734757447575475764757747578475794758047581475824758347584475854758647587475884758947590475914759247593475944759547596475974759847599476004760147602476034760447605476064760747608476094761047611476124761347614476154761647617476184761947620476214762247623476244762547626476274762847629476304763147632476334763447635476364763747638476394764047641476424764347644476454764647647476484764947650476514765247653476544765547656476574765847659476604766147662476634766447665476664766747668476694767047671476724767347674476754767647677476784767947680476814768247683476844768547686476874768847689476904769147692476934769447695476964769747698476994770047701477024770347704477054770647707477084770947710477114771247713477144771547716477174771847719477204772147722477234772447725477264772747728477294773047731477324773347734477354773647737477384773947740477414774247743477444774547746477474774847749477504775147752477534775447755477564775747758477594776047761477624776347764477654776647767477684776947770477714777247773477744777547776477774777847779477804778147782477834778447785477864778747788477894779047791477924779347794477954779647797477984779947800478014780247803478044780547806478074780847809478104781147812478134781447815478164781747818478194782047821478224782347824478254782647827478284782947830478314783247833478344783547836478374783847839478404784147842478434784447845478464784747848478494785047851478524785347854478554785647857478584785947860478614786247863478644786547866478674786847869478704787147872478734787447875478764787747878478794788047881478824788347884478854788647887478884788947890478914789247893478944789547896478974789847899479004790147902479034790447905479064790747908479094791047911479124791347914479154791647917479184791947920479214792247923479244792547926479274792847929479304793147932479334793447935479364793747938479394794047941479424794347944479454794647947479484794947950479514795247953479544795547956479574795847959479604796147962479634796447965479664796747968479694797047971479724797347974479754797647977479784797947980479814798247983479844798547986479874798847989479904799147992479934799447995479964799747998479994800048001480024800348004480054800648007480084800948010480114801248013480144801548016480174801848019480204802148022480234802448025480264802748028480294803048031480324803348034480354803648037480384803948040480414804248043480444804548046480474804848049480504805148052480534805448055480564805748058480594806048061480624806348064480654806648067480684806948070480714807248073480744807548076480774807848079480804808148082480834808448085480864808748088480894809048091480924809348094480954809648097480984809948100481014810248103481044810548106481074810848109481104811148112481134811448115481164811748118481194812048121481224812348124481254812648127481284812948130481314813248133481344813548136481374813848139481404814148142481434814448145481464814748148481494815048151481524815348154481554815648157481584815948160481614816248163481644816548166481674816848169481704817148172481734817448175481764817748178481794818048181481824818348184481854818648187481884818948190481914819248193481944819548196481974819848199482004820148202482034820448205482064820748208482094821048211482124821348214482154821648217482184821948220482214822248223482244822548226482274822848229482304823148232482334823448235482364823748238482394824048241482424824348244482454824648247482484824948250482514825248253482544825548256482574825848259482604826148262482634826448265482664826748268482694827048271482724827348274482754827648277482784827948280482814828248283482844828548286482874828848289482904829148292482934829448295482964829748298482994830048301483024830348304483054830648307483084830948310483114831248313483144831548316483174831848319483204832148322483234832448325483264832748328483294833048331483324833348334483354833648337483384833948340483414834248343483444834548346483474834848349483504835148352483534835448355483564835748358483594836048361483624836348364483654836648367483684836948370483714837248373483744837548376483774837848379483804838148382483834838448385483864838748388483894839048391483924839348394483954839648397483984839948400484014840248403484044840548406484074840848409484104841148412484134841448415484164841748418484194842048421484224842348424484254842648427484284842948430484314843248433484344843548436484374843848439484404844148442484434844448445484464844748448484494845048451484524845348454484554845648457484584845948460484614846248463484644846548466484674846848469484704847148472484734847448475484764847748478484794848048481484824848348484484854848648487484884848948490484914849248493484944849548496484974849848499485004850148502485034850448505485064850748508485094851048511485124851348514485154851648517485184851948520485214852248523485244852548526485274852848529485304853148532485334853448535485364853748538485394854048541485424854348544485454854648547485484854948550485514855248553485544855548556485574855848559485604856148562485634856448565485664856748568485694857048571485724857348574485754857648577485784857948580485814858248583485844858548586485874858848589485904859148592485934859448595485964859748598485994860048601486024860348604486054860648607486084860948610486114861248613486144861548616486174861848619486204862148622486234862448625486264862748628486294863048631486324863348634486354863648637486384863948640486414864248643486444864548646486474864848649486504865148652486534865448655486564865748658486594866048661486624866348664486654866648667486684866948670486714867248673486744867548676486774867848679486804868148682486834868448685486864868748688486894869048691486924869348694486954869648697486984869948700487014870248703487044870548706487074870848709487104871148712487134871448715487164871748718487194872048721487224872348724487254872648727487284872948730487314873248733487344873548736487374873848739487404874148742487434874448745487464874748748487494875048751487524875348754487554875648757487584875948760487614876248763487644876548766487674876848769487704877148772487734877448775487764877748778487794878048781487824878348784487854878648787487884878948790487914879248793487944879548796487974879848799488004880148802488034880448805488064880748808488094881048811488124881348814488154881648817488184881948820488214882248823488244882548826488274882848829488304883148832488334883448835488364883748838488394884048841488424884348844488454884648847488484884948850488514885248853488544885548856488574885848859488604886148862488634886448865488664886748868488694887048871488724887348874488754887648877488784887948880488814888248883488844888548886488874888848889488904889148892488934889448895488964889748898488994890048901489024890348904489054890648907489084890948910489114891248913489144891548916489174891848919489204892148922489234892448925489264892748928489294893048931489324893348934489354893648937489384893948940489414894248943489444894548946489474894848949489504895148952489534895448955489564895748958489594896048961489624896348964489654896648967489684896948970489714897248973489744897548976489774897848979489804898148982489834898448985489864898748988489894899048991489924899348994489954899648997489984899949000490014900249003490044900549006490074900849009490104901149012490134901449015490164901749018490194902049021490224902349024490254902649027490284902949030490314903249033490344903549036490374903849039490404904149042490434904449045490464904749048490494905049051490524905349054490554905649057490584905949060490614906249063490644906549066490674906849069490704907149072490734907449075490764907749078490794908049081490824908349084490854908649087490884908949090490914909249093490944909549096490974909849099491004910149102491034910449105491064910749108491094911049111491124911349114491154911649117491184911949120491214912249123491244912549126491274912849129491304913149132491334913449135491364913749138491394914049141491424914349144491454914649147491484914949150491514915249153491544915549156491574915849159491604916149162491634916449165491664916749168491694917049171491724917349174491754917649177491784917949180491814918249183491844918549186491874918849189491904919149192491934919449195491964919749198491994920049201492024920349204492054920649207492084920949210492114921249213492144921549216492174921849219492204922149222492234922449225492264922749228492294923049231492324923349234492354923649237492384923949240492414924249243492444924549246492474924849249492504925149252492534925449255492564925749258492594926049261492624926349264492654926649267492684926949270492714927249273492744927549276492774927849279492804928149282492834928449285492864928749288492894929049291492924929349294492954929649297492984929949300493014930249303493044930549306493074930849309493104931149312493134931449315493164931749318493194932049321493224932349324493254932649327493284932949330493314933249333493344933549336493374933849339493404934149342493434934449345493464934749348493494935049351493524935349354493554935649357493584935949360493614936249363493644936549366493674936849369493704937149372493734937449375493764937749378493794938049381493824938349384493854938649387493884938949390493914939249393493944939549396493974939849399494004940149402494034940449405494064940749408494094941049411494124941349414494154941649417494184941949420494214942249423494244942549426494274942849429494304943149432494334943449435494364943749438494394944049441494424944349444494454944649447494484944949450494514945249453494544945549456494574945849459494604946149462494634946449465494664946749468494694947049471494724947349474494754947649477494784947949480494814948249483494844948549486494874948849489494904949149492494934949449495494964949749498494994950049501495024950349504495054950649507495084950949510495114951249513495144951549516495174951849519495204952149522495234952449525495264952749528495294953049531495324953349534495354953649537495384953949540495414954249543495444954549546495474954849549495504955149552495534955449555495564955749558495594956049561495624956349564495654956649567495684956949570495714957249573495744957549576495774957849579495804958149582495834958449585495864958749588495894959049591495924959349594495954959649597495984959949600496014960249603496044960549606496074960849609496104961149612496134961449615496164961749618496194962049621496224962349624496254962649627496284962949630496314963249633496344963549636496374963849639496404964149642496434964449645496464964749648496494965049651496524965349654496554965649657496584965949660496614966249663496644966549666496674966849669496704967149672496734967449675496764967749678496794968049681496824968349684496854968649687496884968949690496914969249693496944969549696496974969849699497004970149702497034970449705497064970749708497094971049711497124971349714497154971649717497184971949720497214972249723497244972549726497274972849729497304973149732497334973449735497364973749738497394974049741497424974349744497454974649747497484974949750497514975249753497544975549756497574975849759497604976149762497634976449765497664976749768497694977049771497724977349774497754977649777497784977949780497814978249783497844978549786497874978849789497904979149792497934979449795497964979749798497994980049801498024980349804498054980649807498084980949810498114981249813498144981549816498174981849819498204982149822498234982449825498264982749828498294983049831498324983349834498354983649837498384983949840498414984249843498444984549846498474984849849498504985149852498534985449855498564985749858498594986049861498624986349864498654986649867498684986949870498714987249873498744987549876498774987849879498804988149882498834988449885498864988749888498894989049891498924989349894498954989649897498984989949900499014990249903499044990549906499074990849909499104991149912499134991449915499164991749918499194992049921499224992349924499254992649927499284992949930499314993249933499344993549936499374993849939499404994149942499434994449945499464994749948499494995049951499524995349954499554995649957499584995949960499614996249963499644996549966499674996849969499704997149972499734997449975499764997749978499794998049981499824998349984499854998649987499884998949990499914999249993499944999549996499974999849999500005000150002500035000450005500065000750008500095001050011500125001350014500155001650017500185001950020500215002250023500245002550026500275002850029500305003150032500335003450035500365003750038500395004050041500425004350044500455004650047500485004950050500515005250053500545005550056500575005850059500605006150062500635006450065500665006750068500695007050071500725007350074500755007650077500785007950080500815008250083500845008550086500875008850089500905009150092500935009450095500965009750098500995010050101501025010350104501055010650107501085010950110501115011250113501145011550116501175011850119501205012150122501235012450125501265012750128501295013050131501325013350134501355013650137501385013950140501415014250143501445014550146501475014850149501505015150152501535015450155501565015750158501595016050161501625016350164501655016650167501685016950170501715017250173501745017550176501775017850179501805018150182501835018450185501865018750188501895019050191501925019350194501955019650197501985019950200502015020250203502045020550206502075020850209502105021150212502135021450215502165021750218502195022050221502225022350224502255022650227502285022950230502315023250233502345023550236502375023850239502405024150242502435024450245502465024750248502495025050251502525025350254502555025650257502585025950260502615026250263502645026550266502675026850269502705027150272502735027450275502765027750278502795028050281502825028350284502855028650287502885028950290502915029250293502945029550296502975029850299503005030150302503035030450305503065030750308503095031050311503125031350314503155031650317503185031950320503215032250323503245032550326503275032850329503305033150332503335033450335503365033750338503395034050341503425034350344503455034650347503485034950350503515035250353503545035550356503575035850359503605036150362503635036450365503665036750368503695037050371503725037350374503755037650377503785037950380503815038250383503845038550386503875038850389503905039150392503935039450395503965039750398503995040050401504025040350404504055040650407504085040950410504115041250413504145041550416504175041850419504205042150422504235042450425504265042750428504295043050431504325043350434504355043650437504385043950440504415044250443504445044550446504475044850449504505045150452504535045450455504565045750458504595046050461504625046350464504655046650467504685046950470504715047250473504745047550476504775047850479504805048150482504835048450485504865048750488504895049050491504925049350494504955049650497504985049950500505015050250503505045050550506505075050850509505105051150512505135051450515505165051750518505195052050521505225052350524505255052650527505285052950530505315053250533505345053550536505375053850539505405054150542505435054450545505465054750548505495055050551505525055350554505555055650557505585055950560505615056250563505645056550566505675056850569505705057150572505735057450575505765057750578505795058050581505825058350584505855058650587505885058950590505915059250593505945059550596505975059850599506005060150602506035060450605506065060750608506095061050611506125061350614506155061650617506185061950620506215062250623506245062550626506275062850629506305063150632506335063450635506365063750638506395064050641506425064350644506455064650647506485064950650506515065250653506545065550656506575065850659506605066150662506635066450665506665066750668506695067050671506725067350674506755067650677506785067950680506815068250683506845068550686506875068850689506905069150692506935069450695506965069750698506995070050701507025070350704507055070650707507085070950710507115071250713507145071550716507175071850719507205072150722507235072450725507265072750728507295073050731507325073350734507355073650737507385073950740507415074250743507445074550746507475074850749507505075150752507535075450755507565075750758507595076050761507625076350764507655076650767507685076950770507715077250773507745077550776507775077850779507805078150782507835078450785507865078750788507895079050791507925079350794507955079650797507985079950800508015080250803508045080550806508075080850809508105081150812508135081450815508165081750818508195082050821508225082350824508255082650827508285082950830508315083250833508345083550836508375083850839508405084150842508435084450845508465084750848508495085050851508525085350854508555085650857508585085950860508615086250863508645086550866508675086850869508705087150872508735087450875508765087750878508795088050881508825088350884508855088650887508885088950890508915089250893508945089550896508975089850899509005090150902509035090450905509065090750908509095091050911509125091350914509155091650917509185091950920509215092250923509245092550926509275092850929509305093150932509335093450935509365093750938509395094050941509425094350944509455094650947509485094950950509515095250953509545095550956509575095850959509605096150962509635096450965509665096750968509695097050971509725097350974509755097650977509785097950980509815098250983509845098550986509875098850989509905099150992509935099450995509965099750998509995100051001510025100351004510055100651007510085100951010510115101251013510145101551016510175101851019510205102151022510235102451025510265102751028510295103051031510325103351034510355103651037510385103951040510415104251043510445104551046510475104851049510505105151052510535105451055510565105751058510595106051061510625106351064510655106651067510685106951070510715107251073510745107551076510775107851079510805108151082510835108451085510865108751088510895109051091510925109351094510955109651097510985109951100511015110251103511045110551106511075110851109511105111151112511135111451115511165111751118511195112051121511225112351124511255112651127511285112951130511315113251133511345113551136511375113851139511405114151142511435114451145511465114751148511495115051151511525115351154511555115651157511585115951160511615116251163511645116551166511675116851169511705117151172511735117451175511765117751178511795118051181511825118351184511855118651187511885118951190511915119251193511945119551196511975119851199512005120151202512035120451205512065120751208512095121051211512125121351214512155121651217512185121951220512215122251223512245122551226512275122851229512305123151232512335123451235512365123751238512395124051241512425124351244512455124651247512485124951250512515125251253512545125551256512575125851259512605126151262512635126451265512665126751268512695127051271512725127351274512755127651277512785127951280512815128251283512845128551286512875128851289512905129151292512935129451295512965129751298512995130051301513025130351304513055130651307513085130951310513115131251313513145131551316513175131851319513205132151322513235132451325513265132751328513295133051331513325133351334513355133651337513385133951340513415134251343513445134551346513475134851349513505135151352513535135451355513565135751358513595136051361513625136351364513655136651367513685136951370513715137251373513745137551376513775137851379513805138151382513835138451385513865138751388513895139051391513925139351394513955139651397513985139951400514015140251403514045140551406514075140851409514105141151412514135141451415514165141751418514195142051421514225142351424514255142651427514285142951430514315143251433514345143551436514375143851439514405144151442514435144451445514465144751448514495145051451514525145351454514555145651457514585145951460514615146251463514645146551466514675146851469514705147151472514735147451475514765147751478514795148051481514825148351484514855148651487514885148951490514915149251493514945149551496514975149851499515005150151502515035150451505515065150751508515095151051511515125151351514515155151651517515185151951520515215152251523515245152551526515275152851529515305153151532515335153451535515365153751538515395154051541515425154351544515455154651547515485154951550515515155251553515545155551556515575155851559515605156151562515635156451565515665156751568515695157051571515725157351574515755157651577515785157951580515815158251583515845158551586515875158851589515905159151592515935159451595515965159751598515995160051601516025160351604516055160651607516085160951610516115161251613516145161551616516175161851619516205162151622516235162451625516265162751628516295163051631516325163351634516355163651637516385163951640516415164251643516445164551646516475164851649516505165151652516535165451655516565165751658516595166051661516625166351664516655166651667516685166951670516715167251673516745167551676516775167851679516805168151682516835168451685516865168751688516895169051691516925169351694516955169651697516985169951700517015170251703517045170551706517075170851709517105171151712517135171451715517165171751718517195172051721517225172351724517255172651727517285172951730517315173251733517345173551736517375173851739517405174151742517435174451745517465174751748517495175051751517525175351754517555175651757517585175951760517615176251763517645176551766517675176851769517705177151772517735177451775517765177751778517795178051781517825178351784517855178651787517885178951790517915179251793517945179551796517975179851799518005180151802518035180451805518065180751808518095181051811518125181351814518155181651817518185181951820518215182251823518245182551826518275182851829518305183151832518335183451835518365183751838518395184051841518425184351844518455184651847518485184951850518515185251853518545185551856518575185851859518605186151862518635186451865518665186751868518695187051871518725187351874518755187651877518785187951880518815188251883518845188551886518875188851889518905189151892518935189451895518965189751898518995190051901519025190351904519055190651907519085190951910519115191251913519145191551916519175191851919519205192151922519235192451925519265192751928519295193051931519325193351934519355193651937519385193951940519415194251943519445194551946519475194851949519505195151952519535195451955519565195751958519595196051961519625196351964519655196651967519685196951970519715197251973519745197551976519775197851979519805198151982519835198451985519865198751988519895199051991519925199351994519955199651997519985199952000520015200252003520045200552006520075200852009520105201152012520135201452015520165201752018520195202052021520225202352024520255202652027520285202952030520315203252033520345203552036520375203852039520405204152042520435204452045520465204752048520495205052051520525205352054520555205652057520585205952060520615206252063520645206552066520675206852069520705207152072520735207452075520765207752078520795208052081520825208352084520855208652087520885208952090520915209252093520945209552096520975209852099521005210152102521035210452105521065210752108521095211052111521125211352114521155211652117521185211952120521215212252123521245212552126521275212852129521305213152132521335213452135521365213752138521395214052141521425214352144521455214652147521485214952150521515215252153521545215552156521575215852159521605216152162521635216452165521665216752168521695217052171521725217352174521755217652177521785217952180521815218252183521845218552186521875218852189521905219152192521935219452195521965219752198521995220052201522025220352204522055220652207522085220952210522115221252213522145221552216522175221852219522205222152222522235222452225522265222752228522295223052231522325223352234522355223652237522385223952240522415224252243522445224552246522475224852249522505225152252522535225452255522565225752258522595226052261522625226352264522655226652267522685226952270522715227252273522745227552276522775227852279522805228152282522835228452285522865228752288522895229052291522925229352294522955229652297522985229952300523015230252303523045230552306523075230852309523105231152312523135231452315523165231752318523195232052321523225232352324523255232652327523285232952330523315233252333523345233552336523375233852339523405234152342523435234452345523465234752348523495235052351523525235352354523555235652357523585235952360523615236252363523645236552366523675236852369523705237152372523735237452375523765237752378523795238052381523825238352384523855238652387523885238952390523915239252393523945239552396523975239852399524005240152402524035240452405524065240752408524095241052411524125241352414524155241652417524185241952420524215242252423524245242552426524275242852429524305243152432524335243452435524365243752438524395244052441524425244352444524455244652447524485244952450524515245252453524545245552456524575245852459524605246152462524635246452465524665246752468524695247052471524725247352474524755247652477524785247952480524815248252483524845248552486524875248852489524905249152492524935249452495524965249752498524995250052501525025250352504525055250652507525085250952510525115251252513525145251552516525175251852519525205252152522525235252452525525265252752528525295253052531525325253352534525355253652537525385253952540525415254252543525445254552546525475254852549525505255152552525535255452555525565255752558525595256052561525625256352564525655256652567525685256952570525715257252573525745257552576525775257852579525805258152582525835258452585525865258752588525895259052591525925259352594525955259652597525985259952600526015260252603526045260552606526075260852609526105261152612526135261452615526165261752618526195262052621526225262352624526255262652627526285262952630526315263252633526345263552636526375263852639526405264152642526435264452645526465264752648526495265052651526525265352654526555265652657526585265952660526615266252663526645266552666526675266852669526705267152672526735267452675526765267752678526795268052681526825268352684526855268652687526885268952690526915269252693526945269552696526975269852699527005270152702527035270452705527065270752708527095271052711527125271352714527155271652717527185271952720527215272252723527245272552726527275272852729527305273152732527335273452735527365273752738527395274052741527425274352744527455274652747527485274952750527515275252753527545275552756527575275852759527605276152762527635276452765527665276752768527695277052771527725277352774527755277652777527785277952780527815278252783527845278552786527875278852789527905279152792527935279452795527965279752798527995280052801528025280352804528055280652807528085280952810528115281252813528145281552816528175281852819528205282152822528235282452825528265282752828528295283052831528325283352834528355283652837528385283952840528415284252843528445284552846528475284852849528505285152852528535285452855528565285752858528595286052861528625286352864528655286652867528685286952870528715287252873528745287552876528775287852879528805288152882528835288452885528865288752888528895289052891528925289352894528955289652897528985289952900529015290252903529045290552906529075290852909529105291152912529135291452915529165291752918529195292052921529225292352924529255292652927529285292952930529315293252933529345293552936529375293852939529405294152942529435294452945529465294752948529495295052951529525295352954529555295652957529585295952960529615296252963529645296552966529675296852969529705297152972529735297452975529765297752978529795298052981529825298352984529855298652987529885298952990529915299252993529945299552996529975299852999530005300153002530035300453005530065300753008530095301053011530125301353014530155301653017530185301953020530215302253023530245302553026530275302853029530305303153032530335303453035530365303753038530395304053041530425304353044530455304653047530485304953050530515305253053530545305553056530575305853059530605306153062530635306453065530665306753068530695307053071530725307353074530755307653077530785307953080530815308253083530845308553086530875308853089530905309153092530935309453095530965309753098530995310053101531025310353104531055310653107531085310953110531115311253113531145311553116531175311853119531205312153122531235312453125531265312753128531295313053131531325313353134531355313653137531385313953140531415314253143531445314553146531475314853149531505315153152531535315453155531565315753158531595316053161531625316353164531655316653167531685316953170531715317253173531745317553176531775317853179531805318153182531835318453185531865318753188531895319053191531925319353194531955319653197531985319953200532015320253203532045320553206532075320853209532105321153212532135321453215532165321753218532195322053221532225322353224532255322653227532285322953230532315323253233532345323553236532375323853239532405324153242532435324453245532465324753248532495325053251532525325353254532555325653257532585325953260532615326253263532645326553266532675326853269532705327153272532735327453275532765327753278532795328053281532825328353284532855328653287532885328953290532915329253293532945329553296532975329853299533005330153302533035330453305533065330753308533095331053311533125331353314533155331653317533185331953320533215332253323533245332553326533275332853329533305333153332533335333453335533365333753338533395334053341533425334353344533455334653347533485334953350533515335253353533545335553356533575335853359533605336153362533635336453365533665336753368533695337053371533725337353374533755337653377533785337953380533815338253383533845338553386533875338853389533905339153392533935339453395533965339753398533995340053401534025340353404534055340653407534085340953410534115341253413534145341553416534175341853419534205342153422534235342453425534265342753428534295343053431534325343353434534355343653437534385343953440534415344253443534445344553446534475344853449534505345153452534535345453455534565345753458534595346053461534625346353464534655346653467534685346953470534715347253473534745347553476534775347853479534805348153482534835348453485534865348753488534895349053491534925349353494534955349653497534985349953500535015350253503535045350553506535075350853509535105351153512535135351453515535165351753518535195352053521535225352353524535255352653527535285352953530535315353253533535345353553536535375353853539535405354153542535435354453545535465354753548535495355053551535525355353554535555355653557535585355953560535615356253563535645356553566535675356853569535705357153572535735357453575535765357753578535795358053581535825358353584535855358653587535885358953590535915359253593535945359553596535975359853599536005360153602536035360453605536065360753608536095361053611536125361353614536155361653617536185361953620536215362253623536245362553626536275362853629536305363153632536335363453635536365363753638536395364053641536425364353644536455364653647536485364953650536515365253653536545365553656536575365853659536605366153662536635366453665536665366753668536695367053671536725367353674536755367653677536785367953680536815368253683536845368553686536875368853689536905369153692536935369453695536965369753698536995370053701537025370353704537055370653707537085370953710537115371253713537145371553716537175371853719537205372153722537235372453725537265372753728537295373053731537325373353734537355373653737537385373953740537415374253743537445374553746537475374853749537505375153752537535375453755537565375753758537595376053761537625376353764537655376653767537685376953770537715377253773537745377553776537775377853779537805378153782537835378453785537865378753788537895379053791537925379353794537955379653797537985379953800538015380253803538045380553806538075380853809538105381153812538135381453815538165381753818538195382053821538225382353824538255382653827538285382953830538315383253833538345383553836538375383853839538405384153842538435384453845538465384753848538495385053851538525385353854538555385653857538585385953860538615386253863538645386553866538675386853869538705387153872538735387453875538765387753878538795388053881538825388353884538855388653887538885388953890538915389253893538945389553896538975389853899539005390153902539035390453905539065390753908539095391053911539125391353914539155391653917539185391953920539215392253923539245392553926539275392853929539305393153932539335393453935539365393753938539395394053941539425394353944539455394653947539485394953950539515395253953539545395553956539575395853959539605396153962539635396453965539665396753968539695397053971539725397353974539755397653977539785397953980539815398253983539845398553986539875398853989539905399153992539935399453995539965399753998539995400054001540025400354004540055400654007540085400954010540115401254013540145401554016540175401854019540205402154022540235402454025540265402754028540295403054031540325403354034540355403654037540385403954040540415404254043540445404554046540475404854049540505405154052540535405454055540565405754058540595406054061540625406354064540655406654067540685406954070540715407254073540745407554076540775407854079540805408154082540835408454085540865408754088540895409054091540925409354094540955409654097540985409954100541015410254103541045410554106541075410854109541105411154112541135411454115541165411754118541195412054121541225412354124541255412654127541285412954130541315413254133541345413554136541375413854139541405414154142541435414454145541465414754148541495415054151541525415354154541555415654157541585415954160541615416254163541645416554166541675416854169541705417154172541735417454175541765417754178541795418054181541825418354184541855418654187541885418954190541915419254193541945419554196541975419854199542005420154202542035420454205542065420754208542095421054211542125421354214542155421654217542185421954220542215422254223542245422554226542275422854229542305423154232542335423454235542365423754238542395424054241542425424354244542455424654247542485424954250542515425254253542545425554256542575425854259542605426154262542635426454265542665426754268542695427054271542725427354274542755427654277542785427954280542815428254283542845428554286542875428854289542905429154292542935429454295542965429754298542995430054301543025430354304543055430654307543085430954310543115431254313543145431554316543175431854319543205432154322543235432454325543265432754328543295433054331543325433354334543355433654337543385433954340543415434254343543445434554346543475434854349543505435154352543535435454355543565435754358543595436054361543625436354364543655436654367543685436954370543715437254373543745437554376543775437854379543805438154382543835438454385543865438754388543895439054391543925439354394543955439654397543985439954400544015440254403544045440554406544075440854409544105441154412544135441454415544165441754418544195442054421544225442354424544255442654427544285442954430544315443254433544345443554436544375443854439544405444154442544435444454445544465444754448544495445054451544525445354454544555445654457544585445954460544615446254463544645446554466544675446854469544705447154472544735447454475544765447754478544795448054481544825448354484544855448654487544885448954490544915449254493544945449554496544975449854499545005450154502545035450454505545065450754508545095451054511545125451354514545155451654517545185451954520545215452254523545245452554526545275452854529545305453154532545335453454535545365453754538545395454054541545425454354544545455454654547545485454954550545515455254553545545455554556545575455854559545605456154562545635456454565545665456754568545695457054571545725457354574545755457654577545785457954580545815458254583545845458554586545875458854589545905459154592545935459454595545965459754598545995460054601546025460354604546055460654607546085460954610546115461254613546145461554616546175461854619546205462154622546235462454625546265462754628546295463054631546325463354634546355463654637546385463954640546415464254643546445464554646546475464854649546505465154652546535465454655546565465754658546595466054661546625466354664546655466654667546685466954670546715467254673546745467554676546775467854679546805468154682546835468454685546865468754688546895469054691546925469354694546955469654697546985469954700547015470254703547045470554706547075470854709547105471154712547135471454715547165471754718547195472054721547225472354724547255472654727547285472954730547315473254733547345473554736547375473854739547405474154742547435474454745547465474754748547495475054751547525475354754547555475654757547585475954760547615476254763547645476554766547675476854769547705477154772547735477454775547765477754778547795478054781547825478354784547855478654787547885478954790547915479254793547945479554796547975479854799548005480154802548035480454805548065480754808548095481054811548125481354814548155481654817548185481954820548215482254823548245482554826548275482854829548305483154832548335483454835548365483754838548395484054841548425484354844548455484654847548485484954850548515485254853548545485554856548575485854859548605486154862548635486454865548665486754868548695487054871548725487354874548755487654877548785487954880548815488254883548845488554886548875488854889548905489154892548935489454895548965489754898548995490054901549025490354904549055490654907549085490954910549115491254913549145491554916549175491854919549205492154922549235492454925549265492754928549295493054931549325493354934549355493654937549385493954940549415494254943549445494554946549475494854949549505495154952549535495454955549565495754958549595496054961549625496354964549655496654967549685496954970549715497254973549745497554976549775497854979549805498154982549835498454985549865498754988549895499054991549925499354994549955499654997549985499955000550015500255003550045500555006550075500855009550105501155012550135501455015550165501755018550195502055021550225502355024550255502655027550285502955030550315503255033550345503555036550375503855039550405504155042550435504455045550465504755048550495505055051550525505355054550555505655057550585505955060550615506255063550645506555066550675506855069550705507155072550735507455075550765507755078550795508055081550825508355084550855508655087550885508955090550915509255093550945509555096550975509855099551005510155102551035510455105551065510755108551095511055111551125511355114551155511655117551185511955120551215512255123551245512555126551275512855129551305513155132551335513455135551365513755138551395514055141551425514355144551455514655147551485514955150551515515255153551545515555156551575515855159551605516155162551635516455165551665516755168551695517055171551725517355174551755517655177551785517955180551815518255183551845518555186551875518855189551905519155192551935519455195551965519755198551995520055201552025520355204552055520655207552085520955210552115521255213552145521555216552175521855219552205522155222552235522455225552265522755228552295523055231552325523355234552355523655237552385523955240552415524255243552445524555246552475524855249552505525155252552535525455255552565525755258552595526055261552625526355264552655526655267552685526955270552715527255273552745527555276552775527855279552805528155282552835528455285552865528755288552895529055291552925529355294552955529655297552985529955300553015530255303553045530555306553075530855309553105531155312553135531455315553165531755318553195532055321553225532355324553255532655327553285532955330553315533255333553345533555336553375533855339553405534155342553435534455345553465534755348553495535055351553525535355354553555535655357553585535955360553615536255363553645536555366553675536855369553705537155372553735537455375553765537755378553795538055381553825538355384553855538655387553885538955390553915539255393553945539555396553975539855399554005540155402554035540455405554065540755408554095541055411554125541355414554155541655417554185541955420554215542255423554245542555426554275542855429554305543155432554335543455435554365543755438554395544055441554425544355444554455544655447554485544955450554515545255453554545545555456554575545855459554605546155462554635546455465554665546755468554695547055471554725547355474554755547655477554785547955480554815548255483554845548555486554875548855489554905549155492554935549455495554965549755498554995550055501555025550355504555055550655507555085550955510555115551255513555145551555516555175551855519555205552155522555235552455525555265552755528555295553055531555325553355534555355553655537555385553955540555415554255543555445554555546555475554855549555505555155552555535555455555555565555755558555595556055561555625556355564555655556655567555685556955570555715557255573555745557555576555775557855579555805558155582555835558455585555865558755588555895559055591555925559355594555955559655597555985559955600556015560255603556045560555606556075560855609556105561155612556135561455615556165561755618556195562055621556225562355624556255562655627556285562955630556315563255633556345563555636556375563855639556405564155642556435564455645556465564755648556495565055651556525565355654556555565655657556585565955660556615566255663556645566555666556675566855669556705567155672556735567455675556765567755678556795568055681556825568355684556855568655687556885568955690556915569255693556945569555696556975569855699557005570155702557035570455705557065570755708557095571055711557125571355714557155571655717557185571955720557215572255723557245572555726557275572855729557305573155732557335573455735557365573755738557395574055741557425574355744557455574655747557485574955750557515575255753557545575555756557575575855759557605576155762557635576455765557665576755768557695577055771557725577355774557755577655777557785577955780557815578255783557845578555786557875578855789557905579155792557935579455795557965579755798557995580055801558025580355804558055580655807558085580955810558115581255813558145581555816558175581855819558205582155822558235582455825558265582755828558295583055831558325583355834558355583655837558385583955840558415584255843558445584555846558475584855849558505585155852558535585455855558565585755858558595586055861558625586355864558655586655867558685586955870558715587255873558745587555876558775587855879558805588155882558835588455885558865588755888558895589055891558925589355894558955589655897558985589955900559015590255903559045590555906559075590855909559105591155912559135591455915559165591755918559195592055921559225592355924559255592655927559285592955930559315593255933559345593555936559375593855939559405594155942559435594455945559465594755948559495595055951559525595355954559555595655957559585595955960559615596255963559645596555966559675596855969559705597155972559735597455975559765597755978559795598055981559825598355984559855598655987559885598955990559915599255993559945599555996559975599855999560005600156002560035600456005560065600756008560095601056011560125601356014560155601656017560185601956020560215602256023560245602556026560275602856029560305603156032560335603456035560365603756038560395604056041560425604356044560455604656047560485604956050560515605256053560545605556056560575605856059560605606156062560635606456065560665606756068560695607056071560725607356074560755607656077560785607956080560815608256083560845608556086560875608856089560905609156092560935609456095560965609756098560995610056101561025610356104561055610656107561085610956110561115611256113561145611556116561175611856119561205612156122561235612456125561265612756128561295613056131561325613356134561355613656137561385613956140561415614256143561445614556146561475614856149561505615156152561535615456155561565615756158561595616056161561625616356164561655616656167561685616956170561715617256173561745617556176561775617856179561805618156182561835618456185561865618756188561895619056191561925619356194561955619656197561985619956200562015620256203562045620556206562075620856209562105621156212562135621456215562165621756218562195622056221562225622356224562255622656227562285622956230562315623256233562345623556236562375623856239562405624156242562435624456245562465624756248562495625056251562525625356254562555625656257562585625956260562615626256263562645626556266562675626856269562705627156272562735627456275562765627756278562795628056281562825628356284562855628656287562885628956290562915629256293562945629556296562975629856299563005630156302563035630456305563065630756308563095631056311563125631356314563155631656317563185631956320563215632256323563245632556326563275632856329563305633156332563335633456335563365633756338563395634056341563425634356344563455634656347563485634956350563515635256353563545635556356563575635856359563605636156362563635636456365563665636756368563695637056371563725637356374563755637656377563785637956380563815638256383563845638556386563875638856389563905639156392563935639456395563965639756398563995640056401564025640356404564055640656407564085640956410564115641256413564145641556416564175641856419564205642156422564235642456425564265642756428564295643056431564325643356434564355643656437564385643956440564415644256443564445644556446564475644856449564505645156452564535645456455564565645756458564595646056461564625646356464564655646656467564685646956470564715647256473564745647556476564775647856479564805648156482564835648456485564865648756488564895649056491564925649356494564955649656497564985649956500565015650256503565045650556506565075650856509565105651156512565135651456515565165651756518565195652056521565225652356524565255652656527565285652956530565315653256533565345653556536565375653856539565405654156542565435654456545565465654756548565495655056551565525655356554565555655656557565585655956560565615656256563565645656556566565675656856569565705657156572565735657456575565765657756578565795658056581565825658356584565855658656587565885658956590565915659256593565945659556596565975659856599566005660156602566035660456605566065660756608566095661056611566125661356614566155661656617566185661956620566215662256623566245662556626566275662856629566305663156632566335663456635566365663756638566395664056641566425664356644566455664656647566485664956650566515665256653566545665556656566575665856659566605666156662566635666456665566665666756668566695667056671566725667356674566755667656677566785667956680566815668256683566845668556686566875668856689566905669156692566935669456695566965669756698566995670056701567025670356704567055670656707567085670956710567115671256713567145671556716567175671856719567205672156722567235672456725567265672756728567295673056731567325673356734567355673656737567385673956740567415674256743567445674556746567475674856749567505675156752567535675456755567565675756758567595676056761567625676356764567655676656767567685676956770567715677256773567745677556776567775677856779567805678156782567835678456785567865678756788567895679056791567925679356794567955679656797567985679956800568015680256803568045680556806568075680856809568105681156812568135681456815568165681756818568195682056821568225682356824568255682656827568285682956830568315683256833568345683556836568375683856839568405684156842568435684456845568465684756848568495685056851568525685356854568555685656857568585685956860568615686256863568645686556866568675686856869568705687156872568735687456875568765687756878568795688056881568825688356884568855688656887568885688956890568915689256893568945689556896568975689856899569005690156902569035690456905569065690756908569095691056911569125691356914569155691656917569185691956920569215692256923569245692556926569275692856929569305693156932569335693456935569365693756938569395694056941569425694356944569455694656947569485694956950569515695256953569545695556956569575695856959569605696156962569635696456965569665696756968569695697056971569725697356974569755697656977569785697956980569815698256983569845698556986569875698856989569905699156992569935699456995569965699756998569995700057001570025700357004570055700657007570085700957010570115701257013570145701557016570175701857019570205702157022570235702457025570265702757028570295703057031570325703357034570355703657037570385703957040570415704257043570445704557046570475704857049570505705157052570535705457055570565705757058570595706057061570625706357064570655706657067570685706957070570715707257073570745707557076570775707857079570805708157082570835708457085570865708757088570895709057091570925709357094570955709657097570985709957100571015710257103571045710557106571075710857109571105711157112571135711457115571165711757118571195712057121571225712357124571255712657127571285712957130571315713257133571345713557136571375713857139571405714157142571435714457145571465714757148571495715057151571525715357154571555715657157571585715957160571615716257163571645716557166571675716857169571705717157172571735717457175571765717757178571795718057181571825718357184571855718657187571885718957190571915719257193571945719557196571975719857199572005720157202572035720457205572065720757208572095721057211572125721357214572155721657217572185721957220572215722257223572245722557226572275722857229572305723157232572335723457235572365723757238572395724057241572425724357244572455724657247572485724957250572515725257253572545725557256572575725857259572605726157262572635726457265572665726757268572695727057271572725727357274572755727657277572785727957280572815728257283572845728557286572875728857289572905729157292572935729457295572965729757298572995730057301573025730357304573055730657307573085730957310573115731257313573145731557316573175731857319573205732157322573235732457325573265732757328573295733057331573325733357334573355733657337573385733957340573415734257343573445734557346573475734857349573505735157352573535735457355573565735757358573595736057361573625736357364573655736657367573685736957370573715737257373573745737557376573775737857379573805738157382573835738457385573865738757388573895739057391573925739357394573955739657397573985739957400574015740257403574045740557406574075740857409574105741157412574135741457415574165741757418574195742057421574225742357424574255742657427574285742957430574315743257433574345743557436574375743857439574405744157442574435744457445574465744757448574495745057451574525745357454574555745657457574585745957460574615746257463574645746557466574675746857469574705747157472574735747457475574765747757478574795748057481574825748357484574855748657487574885748957490574915749257493574945749557496574975749857499575005750157502575035750457505575065750757508575095751057511575125751357514575155751657517575185751957520575215752257523575245752557526575275752857529575305753157532575335753457535575365753757538575395754057541575425754357544575455754657547575485754957550575515755257553575545755557556575575755857559575605756157562575635756457565575665756757568575695757057571575725757357574575755757657577575785757957580575815758257583575845758557586575875758857589575905759157592575935759457595575965759757598575995760057601576025760357604576055760657607576085760957610576115761257613576145761557616576175761857619576205762157622576235762457625576265762757628576295763057631576325763357634576355763657637576385763957640576415764257643576445764557646576475764857649576505765157652576535765457655576565765757658576595766057661576625766357664576655766657667576685766957670576715767257673576745767557676576775767857679576805768157682576835768457685576865768757688576895769057691576925769357694576955769657697576985769957700577015770257703577045770557706577075770857709577105771157712577135771457715577165771757718577195772057721577225772357724577255772657727577285772957730577315773257733577345773557736577375773857739577405774157742577435774457745577465774757748577495775057751577525775357754577555775657757577585775957760577615776257763577645776557766577675776857769577705777157772577735777457775577765777757778577795778057781577825778357784577855778657787577885778957790577915779257793577945779557796577975779857799578005780157802578035780457805578065780757808578095781057811578125781357814578155781657817578185781957820578215782257823578245782557826578275782857829578305783157832578335783457835578365783757838578395784057841578425784357844578455784657847578485784957850578515785257853578545785557856578575785857859578605786157862578635786457865578665786757868578695787057871578725787357874578755787657877578785787957880578815788257883578845788557886578875788857889578905789157892578935789457895578965789757898578995790057901579025790357904579055790657907579085790957910579115791257913579145791557916579175791857919579205792157922579235792457925579265792757928579295793057931579325793357934579355793657937579385793957940579415794257943579445794557946579475794857949579505795157952579535795457955579565795757958579595796057961579625796357964579655796657967579685796957970579715797257973579745797557976579775797857979579805798157982579835798457985579865798757988579895799057991579925799357994579955799657997579985799958000580015800258003580045800558006580075800858009580105801158012580135801458015580165801758018580195802058021580225802358024580255802658027580285802958030580315803258033580345803558036580375803858039580405804158042580435804458045580465804758048580495805058051580525805358054580555805658057580585805958060580615806258063580645806558066580675806858069580705807158072580735807458075580765807758078580795808058081580825808358084580855808658087580885808958090580915809258093580945809558096580975809858099581005810158102581035810458105581065810758108581095811058111581125811358114581155811658117581185811958120581215812258123581245812558126581275812858129581305813158132581335813458135581365813758138581395814058141581425814358144581455814658147581485814958150581515815258153581545815558156581575815858159581605816158162581635816458165581665816758168581695817058171581725817358174581755817658177581785817958180581815818258183581845818558186581875818858189581905819158192581935819458195581965819758198581995820058201582025820358204582055820658207582085820958210582115821258213582145821558216582175821858219582205822158222582235822458225582265822758228582295823058231582325823358234582355823658237582385823958240582415824258243582445824558246582475824858249582505825158252582535825458255582565825758258582595826058261582625826358264582655826658267582685826958270582715827258273582745827558276582775827858279582805828158282582835828458285582865828758288582895829058291582925829358294582955829658297582985829958300583015830258303583045830558306583075830858309583105831158312583135831458315583165831758318583195832058321583225832358324583255832658327583285832958330583315833258333583345833558336583375833858339583405834158342583435834458345583465834758348583495835058351583525835358354583555835658357583585835958360583615836258363583645836558366583675836858369583705837158372583735837458375583765837758378583795838058381583825838358384583855838658387583885838958390583915839258393583945839558396583975839858399584005840158402584035840458405584065840758408584095841058411584125841358414584155841658417584185841958420584215842258423584245842558426584275842858429584305843158432584335843458435584365843758438584395844058441584425844358444584455844658447584485844958450584515845258453584545845558456584575845858459584605846158462584635846458465584665846758468584695847058471584725847358474584755847658477584785847958480584815848258483584845848558486584875848858489584905849158492584935849458495584965849758498584995850058501585025850358504585055850658507585085850958510585115851258513585145851558516585175851858519585205852158522585235852458525585265852758528585295853058531585325853358534585355853658537585385853958540585415854258543585445854558546585475854858549585505855158552585535855458555585565855758558585595856058561585625856358564585655856658567585685856958570585715857258573585745857558576585775857858579585805858158582585835858458585585865858758588585895859058591585925859358594585955859658597585985859958600586015860258603586045860558606586075860858609586105861158612586135861458615586165861758618586195862058621586225862358624586255862658627586285862958630586315863258633586345863558636586375863858639586405864158642586435864458645586465864758648586495865058651586525865358654586555865658657586585865958660586615866258663586645866558666586675866858669586705867158672586735867458675586765867758678586795868058681586825868358684586855868658687586885868958690586915869258693586945869558696586975869858699587005870158702587035870458705587065870758708587095871058711587125871358714587155871658717587185871958720587215872258723587245872558726587275872858729587305873158732587335873458735587365873758738587395874058741587425874358744587455874658747587485874958750587515875258753587545875558756587575875858759587605876158762587635876458765587665876758768587695877058771587725877358774587755877658777587785877958780587815878258783587845878558786587875878858789587905879158792587935879458795587965879758798587995880058801588025880358804588055880658807588085880958810588115881258813588145881558816588175881858819588205882158822588235882458825588265882758828588295883058831588325883358834588355883658837588385883958840588415884258843588445884558846588475884858849588505885158852588535885458855588565885758858588595886058861588625886358864588655886658867588685886958870588715887258873588745887558876588775887858879588805888158882588835888458885588865888758888588895889058891588925889358894588955889658897588985889958900589015890258903589045890558906589075890858909589105891158912589135891458915589165891758918589195892058921589225892358924589255892658927589285892958930589315893258933589345893558936589375893858939589405894158942589435894458945589465894758948589495895058951589525895358954589555895658957589585895958960589615896258963589645896558966589675896858969589705897158972589735897458975589765897758978589795898058981589825898358984589855898658987589885898958990589915899258993589945899558996589975899858999590005900159002590035900459005590065900759008590095901059011590125901359014590155901659017590185901959020590215902259023590245902559026590275902859029590305903159032590335903459035590365903759038590395904059041590425904359044590455904659047590485904959050590515905259053590545905559056590575905859059590605906159062590635906459065590665906759068590695907059071590725907359074590755907659077590785907959080590815908259083590845908559086590875908859089590905909159092590935909459095590965909759098590995910059101591025910359104591055910659107591085910959110591115911259113591145911559116591175911859119591205912159122591235912459125591265912759128591295913059131591325913359134591355913659137591385913959140591415914259143591445914559146591475914859149591505915159152591535915459155591565915759158591595916059161591625916359164591655916659167591685916959170591715917259173591745917559176591775917859179591805918159182591835918459185591865918759188591895919059191591925919359194591955919659197591985919959200592015920259203592045920559206592075920859209592105921159212592135921459215592165921759218592195922059221592225922359224592255922659227592285922959230592315923259233592345923559236592375923859239592405924159242592435924459245592465924759248592495925059251592525925359254592555925659257592585925959260592615926259263592645926559266592675926859269592705927159272592735927459275592765927759278592795928059281592825928359284592855928659287592885928959290592915929259293592945929559296592975929859299593005930159302593035930459305593065930759308593095931059311593125931359314593155931659317593185931959320593215932259323593245932559326593275932859329593305933159332593335933459335593365933759338593395934059341593425934359344593455934659347593485934959350593515935259353593545935559356593575935859359593605936159362593635936459365593665936759368593695937059371593725937359374593755937659377593785937959380593815938259383593845938559386593875938859389593905939159392593935939459395593965939759398593995940059401594025940359404594055940659407594085940959410594115941259413594145941559416594175941859419594205942159422594235942459425594265942759428594295943059431594325943359434594355943659437594385943959440594415944259443594445944559446594475944859449594505945159452594535945459455594565945759458594595946059461594625946359464594655946659467594685946959470594715947259473594745947559476594775947859479594805948159482594835948459485594865948759488594895949059491594925949359494594955949659497594985949959500595015950259503595045950559506595075950859509595105951159512595135951459515595165951759518595195952059521595225952359524595255952659527595285952959530595315953259533595345953559536595375953859539595405954159542595435954459545595465954759548595495955059551595525955359554595555955659557595585955959560595615956259563595645956559566595675956859569595705957159572595735957459575595765957759578595795958059581595825958359584595855958659587595885958959590595915959259593595945959559596595975959859599596005960159602596035960459605596065960759608596095961059611596125961359614596155961659617596185961959620596215962259623596245962559626596275962859629596305963159632596335963459635596365963759638596395964059641596425964359644596455964659647596485964959650596515965259653596545965559656596575965859659596605966159662596635966459665596665966759668596695967059671596725967359674596755967659677596785967959680596815968259683596845968559686596875968859689596905969159692596935969459695596965969759698596995970059701597025970359704597055970659707597085970959710597115971259713597145971559716597175971859719597205972159722597235972459725597265972759728597295973059731597325973359734597355973659737597385973959740597415974259743597445974559746597475974859749597505975159752597535975459755597565975759758597595976059761597625976359764597655976659767597685976959770597715977259773597745977559776597775977859779597805978159782597835978459785597865978759788597895979059791597925979359794597955979659797597985979959800598015980259803598045980559806598075980859809598105981159812598135981459815598165981759818598195982059821598225982359824598255982659827598285982959830598315983259833598345983559836598375983859839598405984159842598435984459845598465984759848598495985059851598525985359854598555985659857598585985959860598615986259863598645986559866598675986859869598705987159872598735987459875598765987759878598795988059881598825988359884598855988659887598885988959890598915989259893598945989559896598975989859899599005990159902599035990459905599065990759908599095991059911599125991359914599155991659917599185991959920599215992259923599245992559926599275992859929599305993159932599335993459935599365993759938599395994059941599425994359944599455994659947599485994959950599515995259953599545995559956599575995859959599605996159962599635996459965599665996759968599695997059971599725997359974599755997659977599785997959980599815998259983599845998559986599875998859989599905999159992599935999459995599965999759998599996000060001600026000360004600056000660007600086000960010600116001260013600146001560016600176001860019600206002160022600236002460025600266002760028600296003060031600326003360034600356003660037600386003960040600416004260043600446004560046600476004860049600506005160052600536005460055600566005760058600596006060061600626006360064600656006660067600686006960070600716007260073600746007560076600776007860079600806008160082600836008460085600866008760088600896009060091600926009360094600956009660097600986009960100601016010260103601046010560106601076010860109601106011160112601136011460115601166011760118601196012060121601226012360124601256012660127601286012960130601316013260133601346013560136601376013860139601406014160142601436014460145601466014760148601496015060151601526015360154601556015660157601586015960160601616016260163601646016560166601676016860169601706017160172601736017460175601766017760178601796018060181601826018360184601856018660187601886018960190601916019260193601946019560196601976019860199602006020160202602036020460205602066020760208602096021060211602126021360214602156021660217602186021960220602216022260223602246022560226602276022860229602306023160232602336023460235602366023760238602396024060241602426024360244602456024660247602486024960250602516025260253602546025560256602576025860259602606026160262602636026460265602666026760268602696027060271602726027360274602756027660277602786027960280602816028260283602846028560286602876028860289602906029160292602936029460295602966029760298602996030060301603026030360304603056030660307603086030960310603116031260313603146031560316603176031860319603206032160322603236032460325603266032760328603296033060331603326033360334603356033660337603386033960340603416034260343603446034560346603476034860349603506035160352603536035460355603566035760358603596036060361603626036360364603656036660367603686036960370603716037260373603746037560376603776037860379603806038160382603836038460385603866038760388603896039060391603926039360394603956039660397603986039960400604016040260403604046040560406604076040860409604106041160412604136041460415604166041760418604196042060421604226042360424604256042660427604286042960430604316043260433604346043560436604376043860439604406044160442604436044460445604466044760448604496045060451604526045360454604556045660457604586045960460604616046260463604646046560466604676046860469604706047160472604736047460475604766047760478604796048060481604826048360484604856048660487604886048960490604916049260493604946049560496604976049860499605006050160502605036050460505605066050760508605096051060511605126051360514605156051660517605186051960520605216052260523605246052560526605276052860529605306053160532605336053460535605366053760538605396054060541605426054360544605456054660547605486054960550605516055260553605546055560556605576055860559605606056160562605636056460565605666056760568605696057060571605726057360574605756057660577605786057960580605816058260583605846058560586605876058860589605906059160592605936059460595605966059760598605996060060601606026060360604606056060660607606086060960610606116061260613606146061560616606176061860619606206062160622606236062460625606266062760628606296063060631606326063360634606356063660637606386063960640606416064260643606446064560646606476064860649606506065160652606536065460655606566065760658606596066060661606626066360664606656066660667606686066960670606716067260673606746067560676606776067860679606806068160682606836068460685606866068760688606896069060691606926069360694606956069660697606986069960700607016070260703607046070560706607076070860709607106071160712607136071460715607166071760718607196072060721607226072360724607256072660727607286072960730607316073260733607346073560736607376073860739607406074160742607436074460745607466074760748607496075060751607526075360754607556075660757607586075960760607616076260763607646076560766607676076860769607706077160772607736077460775607766077760778607796078060781607826078360784607856078660787607886078960790607916079260793607946079560796607976079860799608006080160802608036080460805608066080760808608096081060811608126081360814608156081660817608186081960820608216082260823608246082560826608276082860829608306083160832608336083460835608366083760838608396084060841608426084360844608456084660847608486084960850608516085260853608546085560856608576085860859608606086160862608636086460865608666086760868608696087060871608726087360874608756087660877608786087960880608816088260883608846088560886608876088860889608906089160892608936089460895608966089760898608996090060901609026090360904609056090660907609086090960910609116091260913609146091560916609176091860919609206092160922609236092460925609266092760928609296093060931609326093360934609356093660937609386093960940609416094260943609446094560946609476094860949609506095160952609536095460955609566095760958609596096060961609626096360964609656096660967609686096960970609716097260973609746097560976609776097860979609806098160982609836098460985609866098760988609896099060991609926099360994609956099660997609986099961000610016100261003610046100561006610076100861009610106101161012610136101461015610166101761018610196102061021610226102361024610256102661027610286102961030610316103261033610346103561036610376103861039610406104161042610436104461045610466104761048610496105061051610526105361054610556105661057610586105961060610616106261063610646106561066610676106861069610706107161072610736107461075610766107761078610796108061081610826108361084610856108661087610886108961090610916109261093610946109561096610976109861099611006110161102611036110461105611066110761108611096111061111611126111361114611156111661117611186111961120611216112261123611246112561126611276112861129611306113161132611336113461135611366113761138611396114061141611426114361144611456114661147611486114961150611516115261153611546115561156611576115861159611606116161162611636116461165611666116761168611696117061171611726117361174611756117661177611786117961180611816118261183611846118561186611876118861189611906119161192611936119461195611966119761198611996120061201612026120361204612056120661207612086120961210612116121261213612146121561216612176121861219612206122161222612236122461225612266122761228612296123061231612326123361234612356123661237612386123961240612416124261243612446124561246612476124861249612506125161252612536125461255612566125761258612596126061261612626126361264612656126661267612686126961270612716127261273612746127561276612776127861279612806128161282612836128461285612866128761288612896129061291612926129361294612956129661297612986129961300613016130261303613046130561306613076130861309613106131161312613136131461315613166131761318613196132061321613226132361324613256132661327613286132961330613316133261333613346133561336613376133861339613406134161342613436134461345613466134761348613496135061351613526135361354613556135661357613586135961360613616136261363613646136561366613676136861369613706137161372613736137461375613766137761378613796138061381613826138361384613856138661387613886138961390613916139261393613946139561396613976139861399614006140161402614036140461405614066140761408614096141061411614126141361414614156141661417614186141961420614216142261423614246142561426614276142861429614306143161432614336143461435614366143761438614396144061441614426144361444614456144661447614486144961450614516145261453614546145561456614576145861459614606146161462614636146461465614666146761468614696147061471614726147361474614756147661477614786147961480614816148261483614846148561486614876148861489614906149161492614936149461495614966149761498614996150061501615026150361504615056150661507615086150961510615116151261513615146151561516615176151861519615206152161522615236152461525615266152761528615296153061531615326153361534615356153661537615386153961540615416154261543615446154561546615476154861549615506155161552615536155461555615566155761558615596156061561615626156361564615656156661567615686156961570615716157261573615746157561576615776157861579615806158161582615836158461585615866158761588615896159061591615926159361594615956159661597615986159961600616016160261603616046160561606616076160861609616106161161612616136161461615616166161761618616196162061621616226162361624616256162661627616286162961630616316163261633616346163561636616376163861639616406164161642616436164461645616466164761648616496165061651616526165361654616556165661657616586165961660616616166261663616646166561666616676166861669616706167161672616736167461675616766167761678616796168061681616826168361684616856168661687616886168961690616916169261693616946169561696616976169861699617006170161702617036170461705617066170761708617096171061711617126171361714617156171661717617186171961720617216172261723617246172561726617276172861729617306173161732617336173461735617366173761738617396174061741617426174361744617456174661747617486174961750617516175261753617546175561756617576175861759617606176161762617636176461765617666176761768617696177061771617726177361774617756177661777617786177961780617816178261783617846178561786617876178861789617906179161792617936179461795617966179761798617996180061801618026180361804618056180661807618086180961810618116181261813618146181561816618176181861819618206182161822618236182461825618266182761828618296183061831618326183361834618356183661837618386183961840618416184261843618446184561846618476184861849618506185161852618536185461855618566185761858618596186061861618626186361864618656186661867618686186961870618716187261873618746187561876618776187861879618806188161882618836188461885618866188761888618896189061891618926189361894618956189661897618986189961900619016190261903619046190561906619076190861909619106191161912619136191461915619166191761918619196192061921619226192361924619256192661927619286192961930619316193261933619346193561936619376193861939619406194161942619436194461945619466194761948619496195061951619526195361954619556195661957619586195961960619616196261963619646196561966619676196861969619706197161972619736197461975619766197761978619796198061981619826198361984619856198661987619886198961990619916199261993619946199561996619976199861999620006200162002620036200462005620066200762008620096201062011620126201362014620156201662017620186201962020620216202262023620246202562026620276202862029620306203162032620336203462035620366203762038620396204062041620426204362044620456204662047620486204962050620516205262053620546205562056620576205862059620606206162062620636206462065620666206762068620696207062071620726207362074620756207662077620786207962080620816208262083620846208562086620876208862089620906209162092620936209462095620966209762098620996210062101621026210362104621056210662107621086210962110621116211262113621146211562116621176211862119621206212162122621236212462125621266212762128621296213062131621326213362134621356213662137621386213962140621416214262143621446214562146621476214862149621506215162152621536215462155621566215762158621596216062161621626216362164621656216662167621686216962170621716217262173621746217562176621776217862179621806218162182621836218462185621866218762188621896219062191621926219362194621956219662197621986219962200622016220262203622046220562206622076220862209622106221162212622136221462215622166221762218622196222062221622226222362224622256222662227622286222962230622316223262233622346223562236622376223862239622406224162242622436224462245622466224762248622496225062251622526225362254622556225662257622586225962260622616226262263622646226562266622676226862269622706227162272622736227462275622766227762278622796228062281622826228362284622856228662287622886228962290622916229262293622946229562296622976229862299623006230162302623036230462305623066230762308623096231062311623126231362314623156231662317623186231962320623216232262323623246232562326623276232862329623306233162332623336233462335623366233762338623396234062341623426234362344623456234662347623486234962350623516235262353623546235562356623576235862359623606236162362623636236462365623666236762368623696237062371623726237362374623756237662377623786237962380623816238262383623846238562386623876238862389623906239162392623936239462395623966239762398623996240062401624026240362404624056240662407624086240962410624116241262413624146241562416624176241862419624206242162422624236242462425624266242762428624296243062431624326243362434624356243662437624386243962440624416244262443624446244562446624476244862449624506245162452624536245462455624566245762458624596246062461624626246362464624656246662467624686246962470624716247262473624746247562476624776247862479624806248162482624836248462485624866248762488624896249062491624926249362494624956249662497624986249962500625016250262503625046250562506625076250862509625106251162512625136251462515625166251762518625196252062521625226252362524625256252662527625286252962530625316253262533625346253562536625376253862539625406254162542625436254462545625466254762548625496255062551625526255362554625556255662557625586255962560625616256262563625646256562566625676256862569625706257162572625736257462575625766257762578625796258062581625826258362584625856258662587625886258962590625916259262593625946259562596625976259862599626006260162602626036260462605626066260762608626096261062611626126261362614626156261662617626186261962620626216262262623626246262562626626276262862629626306263162632626336263462635626366263762638626396264062641626426264362644626456264662647626486264962650626516265262653626546265562656626576265862659626606266162662626636266462665626666266762668626696267062671626726267362674626756267662677626786267962680626816268262683626846268562686626876268862689626906269162692626936269462695626966269762698626996270062701627026270362704627056270662707627086270962710627116271262713627146271562716627176271862719627206272162722627236272462725627266272762728627296273062731627326273362734627356273662737627386273962740627416274262743627446274562746627476274862749627506275162752627536275462755627566275762758627596276062761627626276362764627656276662767627686276962770627716277262773627746277562776627776277862779627806278162782627836278462785627866278762788627896279062791627926279362794627956279662797627986279962800628016280262803628046280562806628076280862809628106281162812628136281462815628166281762818628196282062821628226282362824628256282662827628286282962830628316283262833628346283562836628376283862839628406284162842628436284462845628466284762848628496285062851628526285362854628556285662857628586285962860628616286262863628646286562866628676286862869628706287162872628736287462875628766287762878628796288062881628826288362884628856288662887628886288962890628916289262893628946289562896628976289862899629006290162902629036290462905629066290762908629096291062911629126291362914629156291662917629186291962920629216292262923629246292562926629276292862929629306293162932629336293462935629366293762938629396294062941629426294362944629456294662947629486294962950629516295262953629546295562956629576295862959629606296162962629636296462965629666296762968629696297062971629726297362974629756297662977629786297962980629816298262983629846298562986629876298862989629906299162992629936299462995629966299762998629996300063001630026300363004630056300663007630086300963010630116301263013630146301563016630176301863019630206302163022630236302463025630266302763028630296303063031630326303363034630356303663037630386303963040630416304263043630446304563046630476304863049630506305163052630536305463055630566305763058630596306063061630626306363064630656306663067630686306963070630716307263073630746307563076630776307863079630806308163082630836308463085630866308763088630896309063091630926309363094630956309663097630986309963100631016310263103631046310563106631076310863109631106311163112631136311463115631166311763118631196312063121631226312363124631256312663127631286312963130631316313263133631346313563136631376313863139631406314163142631436314463145631466314763148631496315063151631526315363154631556315663157631586315963160631616316263163631646316563166631676316863169631706317163172631736317463175631766317763178631796318063181631826318363184631856318663187631886318963190631916319263193631946319563196631976319863199632006320163202632036320463205632066320763208632096321063211632126321363214632156321663217632186321963220632216322263223632246322563226632276322863229632306323163232632336323463235632366323763238632396324063241632426324363244632456324663247632486324963250632516325263253632546325563256632576325863259632606326163262632636326463265632666326763268632696327063271632726327363274632756327663277632786327963280632816328263283632846328563286632876328863289632906329163292632936329463295632966329763298632996330063301633026330363304633056330663307633086330963310633116331263313633146331563316633176331863319633206332163322633236332463325633266332763328633296333063331633326333363334633356333663337633386333963340633416334263343633446334563346633476334863349633506335163352633536335463355633566335763358633596336063361633626336363364633656336663367633686336963370633716337263373633746337563376633776337863379633806338163382633836338463385633866338763388633896339063391633926339363394633956339663397633986339963400634016340263403634046340563406634076340863409634106341163412634136341463415634166341763418634196342063421634226342363424634256342663427634286342963430634316343263433634346343563436634376343863439634406344163442634436344463445634466344763448634496345063451634526345363454634556345663457634586345963460634616346263463634646346563466634676346863469634706347163472634736347463475634766347763478634796348063481634826348363484634856348663487634886348963490634916349263493634946349563496634976349863499635006350163502635036350463505635066350763508635096351063511635126351363514635156351663517635186351963520635216352263523635246352563526635276352863529635306353163532635336353463535635366353763538635396354063541635426354363544635456354663547635486354963550635516355263553635546355563556635576355863559635606356163562635636356463565635666356763568635696357063571635726357363574635756357663577635786357963580635816358263583635846358563586635876358863589635906359163592635936359463595635966359763598635996360063601636026360363604636056360663607636086360963610636116361263613636146361563616636176361863619636206362163622636236362463625636266362763628636296363063631636326363363634636356363663637636386363963640636416364263643636446364563646636476364863649636506365163652636536365463655636566365763658636596366063661636626366363664636656366663667636686366963670636716367263673636746367563676636776367863679636806368163682636836368463685636866368763688636896369063691636926369363694636956369663697636986369963700637016370263703637046370563706637076370863709637106371163712637136371463715637166371763718637196372063721637226372363724637256372663727637286372963730637316373263733637346373563736637376373863739637406374163742637436374463745637466374763748637496375063751637526375363754637556375663757637586375963760637616376263763637646376563766637676376863769637706377163772637736377463775637766377763778637796378063781637826378363784637856378663787637886378963790637916379263793637946379563796637976379863799638006380163802638036380463805638066380763808638096381063811638126381363814638156381663817638186381963820638216382263823638246382563826638276382863829638306383163832638336383463835638366383763838638396384063841638426384363844638456384663847638486384963850638516385263853638546385563856638576385863859638606386163862638636386463865638666386763868638696387063871638726387363874638756387663877638786387963880638816388263883638846388563886638876388863889638906389163892638936389463895638966389763898638996390063901639026390363904639056390663907639086390963910639116391263913639146391563916639176391863919639206392163922639236392463925639266392763928639296393063931639326393363934639356393663937639386393963940639416394263943639446394563946639476394863949639506395163952639536395463955639566395763958639596396063961639626396363964639656396663967639686396963970639716397263973639746397563976639776397863979639806398163982639836398463985639866398763988639896399063991639926399363994639956399663997639986399964000640016400264003640046400564006640076400864009640106401164012640136401464015640166401764018640196402064021640226402364024640256402664027640286402964030640316403264033640346403564036640376403864039640406404164042640436404464045640466404764048640496405064051640526405364054640556405664057640586405964060640616406264063640646406564066640676406864069640706407164072640736407464075640766407764078640796408064081640826408364084640856408664087640886408964090640916409264093640946409564096640976409864099641006410164102641036410464105641066410764108641096411064111641126411364114641156411664117641186411964120641216412264123641246412564126641276412864129641306413164132641336413464135641366413764138641396414064141641426414364144641456414664147641486414964150641516415264153641546415564156641576415864159641606416164162641636416464165641666416764168641696417064171641726417364174641756417664177641786417964180641816418264183641846418564186641876418864189641906419164192641936419464195641966419764198641996420064201642026420364204642056420664207642086420964210642116421264213642146421564216642176421864219642206422164222642236422464225642266422764228642296423064231642326423364234642356423664237642386423964240642416424264243642446424564246642476424864249642506425164252642536425464255642566425764258642596426064261642626426364264642656426664267642686426964270642716427264273642746427564276642776427864279642806428164282642836428464285642866428764288642896429064291642926429364294642956429664297642986429964300643016430264303643046430564306643076430864309643106431164312643136431464315643166431764318643196432064321643226432364324643256432664327643286432964330643316433264333643346433564336643376433864339643406434164342643436434464345643466434764348643496435064351643526435364354643556435664357643586435964360643616436264363643646436564366643676436864369643706437164372643736437464375643766437764378643796438064381643826438364384643856438664387643886438964390643916439264393643946439564396643976439864399644006440164402644036440464405644066440764408644096441064411644126441364414644156441664417644186441964420644216442264423644246442564426644276442864429644306443164432644336443464435644366443764438644396444064441644426444364444644456444664447644486444964450644516445264453644546445564456644576445864459644606446164462644636446464465644666446764468644696447064471644726447364474644756447664477644786447964480644816448264483644846448564486644876448864489644906449164492644936449464495644966449764498644996450064501645026450364504645056450664507645086450964510645116451264513645146451564516645176451864519645206452164522645236452464525645266452764528645296453064531645326453364534645356453664537645386453964540645416454264543645446454564546645476454864549645506455164552645536455464555645566455764558645596456064561645626456364564645656456664567645686456964570645716457264573645746457564576645776457864579645806458164582645836458464585645866458764588645896459064591645926459364594645956459664597645986459964600646016460264603646046460564606646076460864609646106461164612646136461464615646166461764618646196462064621646226462364624646256462664627646286462964630646316463264633646346463564636646376463864639646406464164642646436464464645646466464764648646496465064651646526465364654646556465664657646586465964660646616466264663646646466564666646676466864669646706467164672646736467464675646766467764678646796468064681646826468364684646856468664687646886468964690646916469264693646946469564696646976469864699647006470164702647036470464705647066470764708647096471064711647126471364714647156471664717647186471964720647216472264723647246472564726647276472864729647306473164732647336473464735647366473764738647396474064741647426474364744647456474664747647486474964750647516475264753647546475564756647576475864759647606476164762647636476464765647666476764768647696477064771647726477364774647756477664777647786477964780647816478264783647846478564786647876478864789647906479164792647936479464795647966479764798647996480064801648026480364804648056480664807648086480964810648116481264813648146481564816648176481864819648206482164822648236482464825648266482764828648296483064831648326483364834648356483664837648386483964840648416484264843648446484564846648476484864849648506485164852648536485464855648566485764858648596486064861648626486364864648656486664867648686486964870648716487264873648746487564876648776487864879648806488164882648836488464885648866488764888648896489064891648926489364894648956489664897648986489964900649016490264903649046490564906649076490864909649106491164912649136491464915649166491764918649196492064921649226492364924649256492664927649286492964930649316493264933649346493564936649376493864939649406494164942649436494464945649466494764948649496495064951649526495364954649556495664957649586495964960649616496264963649646496564966649676496864969649706497164972649736497464975649766497764978649796498064981649826498364984649856498664987649886498964990649916499264993649946499564996649976499864999650006500165002650036500465005650066500765008650096501065011650126501365014650156501665017650186501965020650216502265023650246502565026650276502865029650306503165032650336503465035650366503765038650396504065041650426504365044650456504665047650486504965050650516505265053650546505565056650576505865059650606506165062650636506465065650666506765068650696507065071650726507365074650756507665077650786507965080650816508265083650846508565086650876508865089650906509165092650936509465095650966509765098650996510065101651026510365104651056510665107651086510965110651116511265113651146511565116651176511865119651206512165122651236512465125651266512765128651296513065131651326513365134651356513665137651386513965140651416514265143651446514565146651476514865149651506515165152651536515465155651566515765158651596516065161651626516365164651656516665167651686516965170651716517265173651746517565176651776517865179651806518165182651836518465185651866518765188651896519065191651926519365194651956519665197651986519965200652016520265203652046520565206652076520865209652106521165212652136521465215652166521765218652196522065221652226522365224652256522665227652286522965230652316523265233652346523565236652376523865239652406524165242652436524465245652466524765248652496525065251652526525365254652556525665257652586525965260652616526265263652646526565266652676526865269652706527165272652736527465275652766527765278652796528065281652826528365284652856528665287652886528965290652916529265293652946529565296652976529865299653006530165302653036530465305653066530765308653096531065311653126531365314653156531665317653186531965320653216532265323653246532565326653276532865329653306533165332653336533465335653366533765338653396534065341653426534365344653456534665347653486534965350653516535265353653546535565356653576535865359653606536165362653636536465365653666536765368653696537065371653726537365374653756537665377653786537965380653816538265383653846538565386653876538865389653906539165392653936539465395653966539765398653996540065401654026540365404654056540665407654086540965410654116541265413654146541565416654176541865419654206542165422654236542465425654266542765428654296543065431654326543365434654356543665437654386543965440654416544265443654446544565446654476544865449654506545165452654536545465455654566545765458654596546065461654626546365464654656546665467654686546965470654716547265473654746547565476654776547865479654806548165482654836548465485654866548765488654896549065491654926549365494654956549665497654986549965500655016550265503655046550565506655076550865509655106551165512655136551465515655166551765518655196552065521655226552365524655256552665527655286552965530655316553265533655346553565536655376553865539655406554165542655436554465545655466554765548655496555065551655526555365554655556555665557655586555965560655616556265563655646556565566655676556865569655706557165572655736557465575655766557765578655796558065581655826558365584655856558665587655886558965590655916559265593655946559565596655976559865599656006560165602656036560465605656066560765608656096561065611656126561365614656156561665617656186561965620656216562265623656246562565626656276562865629656306563165632656336563465635656366563765638656396564065641656426564365644656456564665647656486564965650656516565265653656546565565656656576565865659656606566165662656636566465665656666566765668656696567065671656726567365674656756567665677656786567965680656816568265683656846568565686656876568865689656906569165692656936569465695656966569765698656996570065701657026570365704657056570665707657086570965710657116571265713657146571565716657176571865719657206572165722657236572465725657266572765728657296573065731657326573365734657356573665737657386573965740657416574265743657446574565746657476574865749657506575165752657536575465755657566575765758657596576065761657626576365764657656576665767657686576965770657716577265773657746577565776657776577865779657806578165782657836578465785657866578765788657896579065791657926579365794657956579665797657986579965800658016580265803658046580565806658076580865809658106581165812658136581465815658166581765818658196582065821658226582365824658256582665827658286582965830658316583265833658346583565836658376583865839658406584165842658436584465845658466584765848658496585065851658526585365854658556585665857658586585965860658616586265863658646586565866658676586865869658706587165872658736587465875658766587765878658796588065881658826588365884658856588665887658886588965890658916589265893658946589565896658976589865899659006590165902659036590465905659066590765908659096591065911659126591365914659156591665917659186591965920659216592265923659246592565926659276592865929659306593165932659336593465935659366593765938659396594065941659426594365944659456594665947659486594965950659516595265953659546595565956659576595865959659606596165962659636596465965659666596765968659696597065971659726597365974659756597665977659786597965980659816598265983659846598565986659876598865989659906599165992659936599465995659966599765998659996600066001660026600366004660056600666007660086600966010660116601266013660146601566016660176601866019660206602166022660236602466025660266602766028660296603066031660326603366034660356603666037660386603966040660416604266043660446604566046660476604866049660506605166052660536605466055660566605766058660596606066061660626606366064660656606666067660686606966070660716607266073660746607566076660776607866079660806608166082660836608466085660866608766088660896609066091660926609366094660956609666097660986609966100661016610266103661046610566106661076610866109661106611166112661136611466115661166611766118661196612066121661226612366124661256612666127661286612966130661316613266133661346613566136661376613866139661406614166142661436614466145661466614766148661496615066151661526615366154661556615666157661586615966160661616616266163661646616566166661676616866169661706617166172661736617466175661766617766178661796618066181661826618366184661856618666187661886618966190661916619266193661946619566196661976619866199662006620166202662036620466205662066620766208662096621066211662126621366214662156621666217662186621966220662216622266223662246622566226662276622866229662306623166232662336623466235662366623766238662396624066241662426624366244662456624666247662486624966250662516625266253662546625566256662576625866259662606626166262662636626466265662666626766268662696627066271662726627366274662756627666277662786627966280662816628266283662846628566286662876628866289662906629166292662936629466295662966629766298662996630066301663026630366304663056630666307663086630966310663116631266313663146631566316663176631866319663206632166322663236632466325663266632766328663296633066331663326633366334663356633666337663386633966340663416634266343663446634566346663476634866349663506635166352663536635466355663566635766358663596636066361663626636366364663656636666367663686636966370663716637266373663746637566376663776637866379663806638166382663836638466385663866638766388663896639066391663926639366394663956639666397663986639966400664016640266403664046640566406664076640866409664106641166412664136641466415664166641766418664196642066421664226642366424664256642666427664286642966430664316643266433664346643566436664376643866439664406644166442664436644466445664466644766448664496645066451664526645366454664556645666457664586645966460664616646266463664646646566466664676646866469664706647166472664736647466475664766647766478664796648066481664826648366484664856648666487664886648966490664916649266493664946649566496664976649866499665006650166502665036650466505665066650766508665096651066511665126651366514665156651666517665186651966520665216652266523665246652566526665276652866529665306653166532665336653466535665366653766538665396654066541665426654366544665456654666547665486654966550665516655266553665546655566556665576655866559665606656166562665636656466565665666656766568665696657066571665726657366574665756657666577665786657966580665816658266583665846658566586665876658866589665906659166592665936659466595665966659766598665996660066601666026660366604666056660666607666086660966610666116661266613666146661566616666176661866619666206662166622666236662466625666266662766628666296663066631666326663366634666356663666637666386663966640666416664266643666446664566646666476664866649666506665166652666536665466655666566665766658666596666066661666626666366664666656666666667666686666966670666716667266673666746667566676666776667866679666806668166682666836668466685666866668766688666896669066691666926669366694666956669666697666986669966700667016670266703667046670566706667076670866709667106671166712667136671466715667166671766718667196672066721667226672366724667256672666727667286672966730667316673266733667346673566736667376673866739667406674166742667436674466745667466674766748667496675066751667526675366754667556675666757667586675966760667616676266763667646676566766667676676866769667706677166772667736677466775667766677766778667796678066781667826678366784667856678666787667886678966790667916679266793667946679566796667976679866799668006680166802668036680466805668066680766808668096681066811668126681366814668156681666817668186681966820668216682266823668246682566826668276682866829668306683166832668336683466835668366683766838668396684066841668426684366844668456684666847668486684966850668516685266853668546685566856668576685866859668606686166862668636686466865668666686766868668696687066871668726687366874668756687666877668786687966880668816688266883668846688566886668876688866889668906689166892668936689466895668966689766898668996690066901669026690366904669056690666907669086690966910669116691266913669146691566916669176691866919669206692166922669236692466925669266692766928669296693066931669326693366934669356693666937669386693966940669416694266943669446694566946669476694866949669506695166952669536695466955669566695766958669596696066961669626696366964669656696666967669686696966970669716697266973669746697566976669776697866979669806698166982669836698466985669866698766988669896699066991669926699366994669956699666997669986699967000670016700267003670046700567006670076700867009670106701167012670136701467015670166701767018670196702067021670226702367024670256702667027670286702967030670316703267033670346703567036670376703867039670406704167042670436704467045670466704767048670496705067051670526705367054670556705667057670586705967060670616706267063670646706567066670676706867069670706707167072670736707467075670766707767078670796708067081670826708367084670856708667087670886708967090670916709267093670946709567096670976709867099671006710167102671036710467105671066710767108671096711067111671126711367114671156711667117671186711967120671216712267123671246712567126671276712867129671306713167132671336713467135671366713767138671396714067141671426714367144671456714667147671486714967150671516715267153671546715567156671576715867159671606716167162671636716467165671666716767168671696717067171671726717367174671756717667177671786717967180671816718267183671846718567186671876718867189671906719167192671936719467195671966719767198671996720067201672026720367204672056720667207672086720967210672116721267213672146721567216672176721867219672206722167222672236722467225672266722767228672296723067231672326723367234672356723667237672386723967240672416724267243672446724567246672476724867249672506725167252672536725467255672566725767258672596726067261672626726367264672656726667267672686726967270672716727267273672746727567276672776727867279672806728167282672836728467285672866728767288672896729067291672926729367294672956729667297672986729967300673016730267303673046730567306673076730867309673106731167312673136731467315673166731767318673196732067321673226732367324673256732667327673286732967330673316733267333673346733567336673376733867339673406734167342673436734467345673466734767348673496735067351673526735367354673556735667357673586735967360673616736267363673646736567366673676736867369673706737167372673736737467375673766737767378673796738067381673826738367384673856738667387673886738967390673916739267393673946739567396673976739867399674006740167402674036740467405674066740767408674096741067411674126741367414674156741667417674186741967420674216742267423674246742567426674276742867429674306743167432674336743467435674366743767438674396744067441674426744367444674456744667447674486744967450674516745267453674546745567456674576745867459674606746167462674636746467465674666746767468674696747067471674726747367474674756747667477674786747967480674816748267483674846748567486674876748867489674906749167492674936749467495674966749767498674996750067501675026750367504675056750667507675086750967510675116751267513675146751567516675176751867519675206752167522675236752467525675266752767528675296753067531675326753367534675356753667537675386753967540675416754267543675446754567546675476754867549675506755167552675536755467555675566755767558675596756067561675626756367564675656756667567675686756967570675716757267573675746757567576675776757867579675806758167582675836758467585675866758767588675896759067591675926759367594675956759667597675986759967600676016760267603676046760567606676076760867609676106761167612676136761467615676166761767618676196762067621676226762367624676256762667627676286762967630676316763267633676346763567636676376763867639676406764167642676436764467645676466764767648676496765067651676526765367654676556765667657676586765967660676616766267663676646766567666676676766867669676706767167672676736767467675676766767767678676796768067681676826768367684676856768667687676886768967690676916769267693676946769567696676976769867699677006770167702677036770467705677066770767708677096771067711677126771367714677156771667717677186771967720677216772267723677246772567726677276772867729677306773167732677336773467735677366773767738677396774067741677426774367744677456774667747677486774967750677516775267753677546775567756677576775867759677606776167762677636776467765677666776767768677696777067771677726777367774677756777667777677786777967780677816778267783677846778567786677876778867789677906779167792677936779467795677966779767798677996780067801678026780367804678056780667807678086780967810678116781267813678146781567816678176781867819678206782167822678236782467825678266782767828678296783067831678326783367834678356783667837678386783967840678416784267843678446784567846678476784867849678506785167852678536785467855678566785767858678596786067861678626786367864678656786667867678686786967870678716787267873678746787567876678776787867879678806788167882678836788467885678866788767888678896789067891678926789367894678956789667897678986789967900679016790267903679046790567906679076790867909679106791167912679136791467915679166791767918679196792067921679226792367924679256792667927679286792967930679316793267933679346793567936679376793867939679406794167942679436794467945679466794767948679496795067951679526795367954679556795667957679586795967960679616796267963679646796567966679676796867969679706797167972679736797467975679766797767978679796798067981679826798367984679856798667987679886798967990679916799267993679946799567996679976799867999680006800168002680036800468005680066800768008680096801068011680126801368014680156801668017680186801968020680216802268023680246802568026680276802868029680306803168032680336803468035680366803768038680396804068041680426804368044680456804668047680486804968050680516805268053680546805568056680576805868059680606806168062680636806468065680666806768068680696807068071680726807368074680756807668077680786807968080680816808268083680846808568086680876808868089680906809168092680936809468095680966809768098680996810068101681026810368104681056810668107681086810968110681116811268113681146811568116681176811868119681206812168122681236812468125681266812768128681296813068131681326813368134681356813668137681386813968140681416814268143681446814568146681476814868149681506815168152681536815468155681566815768158681596816068161681626816368164681656816668167681686816968170681716817268173681746817568176681776817868179681806818168182681836818468185681866818768188681896819068191681926819368194681956819668197681986819968200682016820268203682046820568206682076820868209682106821168212682136821468215682166821768218682196822068221682226822368224682256822668227682286822968230682316823268233682346823568236682376823868239682406824168242682436824468245682466824768248682496825068251682526825368254682556825668257682586825968260682616826268263682646826568266682676826868269682706827168272682736827468275682766827768278682796828068281682826828368284682856828668287682886828968290682916829268293682946829568296682976829868299683006830168302683036830468305683066830768308683096831068311683126831368314683156831668317683186831968320683216832268323683246832568326683276832868329683306833168332683336833468335683366833768338683396834068341683426834368344683456834668347683486834968350683516835268353683546835568356683576835868359683606836168362683636836468365683666836768368683696837068371683726837368374683756837668377683786837968380683816838268383683846838568386683876838868389683906839168392683936839468395683966839768398683996840068401684026840368404684056840668407684086840968410684116841268413684146841568416684176841868419684206842168422684236842468425684266842768428684296843068431684326843368434684356843668437684386843968440684416844268443684446844568446684476844868449684506845168452684536845468455684566845768458684596846068461684626846368464684656846668467684686846968470684716847268473684746847568476684776847868479684806848168482684836848468485684866848768488684896849068491684926849368494684956849668497684986849968500685016850268503685046850568506685076850868509685106851168512685136851468515685166851768518685196852068521685226852368524685256852668527685286852968530685316853268533685346853568536685376853868539685406854168542685436854468545685466854768548685496855068551685526855368554685556855668557685586855968560685616856268563685646856568566685676856868569685706857168572685736857468575685766857768578685796858068581685826858368584685856858668587685886858968590685916859268593685946859568596685976859868599686006860168602686036860468605686066860768608686096861068611686126861368614686156861668617686186861968620686216862268623686246862568626686276862868629686306863168632686336863468635686366863768638686396864068641686426864368644686456864668647686486864968650686516865268653686546865568656686576865868659686606866168662686636866468665686666866768668686696867068671686726867368674686756867668677686786867968680686816868268683686846868568686686876868868689686906869168692686936869468695686966869768698686996870068701687026870368704687056870668707687086870968710687116871268713687146871568716687176871868719687206872168722687236872468725687266872768728687296873068731687326873368734687356873668737687386873968740687416874268743687446874568746687476874868749687506875168752687536875468755687566875768758687596876068761687626876368764687656876668767687686876968770687716877268773687746877568776687776877868779687806878168782687836878468785687866878768788687896879068791687926879368794687956879668797687986879968800688016880268803688046880568806688076880868809688106881168812688136881468815688166881768818688196882068821688226882368824688256882668827688286882968830688316883268833688346883568836688376883868839688406884168842688436884468845688466884768848688496885068851688526885368854688556885668857688586885968860688616886268863688646886568866688676886868869688706887168872688736887468875688766887768878688796888068881688826888368884688856888668887688886888968890688916889268893688946889568896688976889868899689006890168902689036890468905689066890768908689096891068911689126891368914689156891668917689186891968920689216892268923689246892568926689276892868929689306893168932689336893468935689366893768938689396894068941689426894368944689456894668947689486894968950689516895268953689546895568956689576895868959689606896168962689636896468965689666896768968689696897068971689726897368974689756897668977689786897968980689816898268983689846898568986689876898868989689906899168992689936899468995689966899768998689996900069001690026900369004690056900669007690086900969010690116901269013690146901569016690176901869019690206902169022690236902469025690266902769028690296903069031690326903369034690356903669037690386903969040690416904269043690446904569046690476904869049690506905169052690536905469055690566905769058690596906069061690626906369064690656906669067690686906969070690716907269073690746907569076690776907869079690806908169082690836908469085690866908769088690896909069091690926909369094690956909669097690986909969100691016910269103691046910569106691076910869109691106911169112691136911469115691166911769118691196912069121691226912369124691256912669127691286912969130691316913269133691346913569136691376913869139691406914169142691436914469145691466914769148691496915069151691526915369154691556915669157691586915969160691616916269163691646916569166691676916869169691706917169172691736917469175691766917769178691796918069181691826918369184691856918669187691886918969190691916919269193691946919569196691976919869199692006920169202692036920469205692066920769208692096921069211692126921369214692156921669217692186921969220692216922269223692246922569226692276922869229692306923169232692336923469235692366923769238692396924069241692426924369244692456924669247692486924969250692516925269253692546925569256692576925869259692606926169262692636926469265692666926769268692696927069271692726927369274692756927669277692786927969280692816928269283692846928569286692876928869289692906929169292692936929469295692966929769298692996930069301693026930369304693056930669307693086930969310693116931269313693146931569316693176931869319693206932169322693236932469325693266932769328693296933069331693326933369334693356933669337693386933969340693416934269343693446934569346693476934869349693506935169352693536935469355693566935769358693596936069361693626936369364693656936669367693686936969370693716937269373693746937569376693776937869379693806938169382693836938469385693866938769388693896939069391693926939369394693956939669397693986939969400694016940269403694046940569406694076940869409694106941169412694136941469415694166941769418694196942069421694226942369424694256942669427694286942969430694316943269433694346943569436694376943869439694406944169442694436944469445694466944769448694496945069451694526945369454694556945669457694586945969460694616946269463694646946569466694676946869469694706947169472694736947469475694766947769478694796948069481694826948369484694856948669487694886948969490694916949269493694946949569496694976949869499695006950169502695036950469505695066950769508695096951069511695126951369514695156951669517695186951969520695216952269523695246952569526695276952869529695306953169532695336953469535695366953769538695396954069541695426954369544695456954669547695486954969550695516955269553695546955569556695576955869559695606956169562695636956469565695666956769568695696957069571695726957369574695756957669577695786957969580695816958269583695846958569586695876958869589695906959169592695936959469595695966959769598695996960069601696026960369604696056960669607696086960969610696116961269613696146961569616696176961869619696206962169622696236962469625696266962769628696296963069631696326963369634696356963669637696386963969640696416964269643696446964569646696476964869649696506965169652696536965469655696566965769658696596966069661696626966369664696656966669667696686966969670696716967269673696746967569676696776967869679696806968169682696836968469685696866968769688696896969069691696926969369694696956969669697696986969969700697016970269703697046970569706697076970869709697106971169712697136971469715697166971769718697196972069721697226972369724697256972669727697286972969730697316973269733697346973569736697376973869739697406974169742697436974469745697466974769748697496975069751697526975369754697556975669757697586975969760697616976269763697646976569766697676976869769697706977169772697736977469775
  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. #ifndef USER_CRYPTO_ERROR
  215. #define USER_CRYPTO_ERROR (-101) /* error returned by IPP lib. */
  216. #endif
  217. #endif
  218. #ifndef NO_SIG_WRAPPER
  219. #include <wolfssl/wolfcrypt/signature.h>
  220. #endif
  221. #ifdef HAVE_AESCCM
  222. #include <wolfssl/wolfcrypt/aes.h>
  223. #endif
  224. #ifdef HAVE_PKCS7
  225. #include <wolfssl/wolfcrypt/pkcs7.h>
  226. #include <wolfssl/wolfcrypt/asn.h>
  227. #ifdef HAVE_LIBZ
  228. #include <wolfssl/wolfcrypt/compress.h>
  229. #endif
  230. #endif
  231. #ifdef WOLFSSL_SMALL_CERT_VERIFY
  232. #include <wolfssl/wolfcrypt/asn.h>
  233. #endif
  234. #ifndef NO_DSA
  235. #include <wolfssl/wolfcrypt/dsa.h>
  236. #ifndef ONEK_BUF
  237. #define ONEK_BUF 1024
  238. #endif
  239. #ifndef TWOK_BUF
  240. #define TWOK_BUF 2048
  241. #endif
  242. #ifndef FOURK_BUF
  243. #define FOURK_BUF 4096
  244. #endif
  245. #ifndef DSA_SIG_SIZE
  246. #define DSA_SIG_SIZE 40
  247. #endif
  248. #ifndef MAX_DSA_PARAM_SIZE
  249. #define MAX_DSA_PARAM_SIZE 256
  250. #endif
  251. #endif
  252. #ifdef WOLFSSL_CMAC
  253. #include <wolfssl/wolfcrypt/cmac.h>
  254. #endif
  255. #ifdef HAVE_ED25519
  256. #include <wolfssl/wolfcrypt/ed25519.h>
  257. #endif
  258. #ifdef HAVE_CURVE25519
  259. #include <wolfssl/wolfcrypt/curve25519.h>
  260. #endif
  261. #ifdef HAVE_ED448
  262. #include <wolfssl/wolfcrypt/ed448.h>
  263. #endif
  264. #ifdef HAVE_CURVE448
  265. #include <wolfssl/wolfcrypt/curve448.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_ED25519
  321. #include <wolfssl/openssl/ed25519.h>
  322. #endif
  323. #ifdef HAVE_ED448
  324. #include <wolfssl/openssl/ed448.h>
  325. #endif
  326. #endif /* OPENSSL_EXTRA */
  327. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  328. && !defined(NO_SHA256) && !defined(RC_NO_RNG)
  329. #include <wolfssl/wolfcrypt/srp.h>
  330. #endif
  331. #if (defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)) || \
  332. defined(HAVE_SESSION_TICKET) || (defined(OPENSSL_EXTRA) && \
  333. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)) || \
  334. defined(WOLFSSL_TEST_STATIC_BUILD) || defined(WOLFSSL_DTLS) || \
  335. defined(HAVE_ECH) || defined(HAVE_EX_DATA) || !defined(NO_SESSION_CACHE) \
  336. || !defined(WOLFSSL_NO_TLS12)
  337. /* for testing SSL_get_peer_cert_chain, or SESSION_TICKET_HINT_DEFAULT,
  338. * for setting authKeyIdSrc in WOLFSSL_X509, or testing DTLS sequence
  339. * number tracking */
  340. #include "wolfssl/internal.h"
  341. #endif
  342. /* force enable test buffers */
  343. #ifndef USE_CERT_BUFFERS_2048
  344. #define USE_CERT_BUFFERS_2048
  345. #endif
  346. #ifndef USE_CERT_BUFFERS_256
  347. #define USE_CERT_BUFFERS_256
  348. #endif
  349. #include <wolfssl/certs_test.h>
  350. #include "tests/utils.h"
  351. /* include misc.c here regardless of NO_INLINE, because misc.c implementations
  352. * have default (hidden) visibility, and in the absence of visibility, it's
  353. * benign to mask out the library implementation.
  354. */
  355. #define WOLFSSL_MISC_INCLUDED
  356. #include <wolfcrypt/src/misc.c>
  357. #ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  358. /* FIPS build has replaced ecc.h. */
  359. #define wc_ecc_key_get_priv(key) (&((key)->k))
  360. #define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  361. #endif
  362. typedef struct testVector {
  363. const char* input;
  364. const char* output;
  365. size_t inLen;
  366. size_t outLen;
  367. } testVector;
  368. #if defined(HAVE_PKCS7)
  369. typedef struct {
  370. const byte* content;
  371. word32 contentSz;
  372. int contentOID;
  373. int encryptOID;
  374. int keyWrapOID;
  375. int keyAgreeOID;
  376. byte* cert;
  377. size_t certSz;
  378. byte* privateKey;
  379. word32 privateKeySz;
  380. } pkcs7EnvelopedVector;
  381. #ifndef NO_PKCS7_ENCRYPTED_DATA
  382. typedef struct {
  383. const byte* content;
  384. word32 contentSz;
  385. int contentOID;
  386. int encryptOID;
  387. byte* encryptionKey;
  388. word32 encryptionKeySz;
  389. } pkcs7EncryptedVector;
  390. #endif
  391. #endif /* HAVE_PKCS7 */
  392. typedef int (*ctx_cb)(WOLFSSL_CTX* ctx);
  393. typedef int (*ssl_cb)(WOLFSSL* ssl);
  394. typedef int (*test_cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  395. typedef struct test_ssl_cbf {
  396. method_provider method;
  397. ctx_cb ctx_ready;
  398. ssl_cb ssl_ready;
  399. ssl_cb on_result;
  400. ssl_cb on_cleanup;
  401. WOLFSSL_CTX* ctx;
  402. const char* caPemFile;
  403. const char* certPemFile;
  404. const char* keyPemFile;
  405. const char* crlPemFile;
  406. #ifdef WOLFSSL_STATIC_MEMORY
  407. byte* mem;
  408. word32 memSz;
  409. wolfSSL_method_func method_ex;
  410. #endif
  411. int devId;
  412. int return_code;
  413. int last_err;
  414. unsigned char isSharedCtx:1;
  415. unsigned char loadToSSL:1;
  416. unsigned char ticNoInit:1;
  417. unsigned char doUdp:1;
  418. } test_ssl_cbf;
  419. #define TEST_SSL_MEMIO_BUF_SZ (64 * 1024)
  420. typedef struct test_ssl_memio_ctx {
  421. WOLFSSL_CTX* s_ctx;
  422. WOLFSSL_CTX* c_ctx;
  423. WOLFSSL* s_ssl;
  424. WOLFSSL* c_ssl;
  425. const char* c_ciphers;
  426. const char* s_ciphers;
  427. char* c_msg;
  428. int c_msglen;
  429. char* s_msg;
  430. int s_msglen;
  431. test_ssl_cbf s_cb;
  432. test_ssl_cbf c_cb;
  433. byte c_buff[TEST_SSL_MEMIO_BUF_SZ];
  434. int c_len;
  435. byte s_buff[TEST_SSL_MEMIO_BUF_SZ];
  436. int s_len;
  437. } test_ssl_memio_ctx;
  438. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  439. test_ssl_cbf* server_cb, test_cbType client_on_handshake);
  440. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  441. const char* currentTestName;
  442. char tmpDirName[16];
  443. int tmpDirNameSet = 0;
  444. #endif
  445. /*----------------------------------------------------------------------------*
  446. | Constants
  447. *----------------------------------------------------------------------------*/
  448. /* Test result constants and macros. */
  449. /* Test succeeded. */
  450. #define TEST_SUCCESS (1)
  451. /* Test failed. */
  452. #define TEST_FAIL (0)
  453. /* Test skipped - not run. */
  454. #define TEST_SKIPPED (-7777)
  455. /* Returns the result based on whether check is true.
  456. *
  457. * @param [in] check Condition for success.
  458. * @return When condition is true: TEST_SUCCESS.
  459. * @return When condition is false: TEST_FAIL.
  460. */
  461. #ifdef DEBUG_WOLFSSL_VERBOSE
  462. #define XSTRINGIFY(s) STRINGIFY(s)
  463. #define STRINGIFY(s) #s
  464. #define TEST_RES_CHECK(check) ({ \
  465. int _ret = (check) ? TEST_SUCCESS : TEST_FAIL; \
  466. if (_ret == TEST_FAIL) { \
  467. fprintf(stderr, " check \"%s\" at %d ", \
  468. XSTRINGIFY(check), __LINE__); \
  469. } \
  470. _ret; })
  471. #else
  472. #define TEST_RES_CHECK(check) \
  473. ((check) ? TEST_SUCCESS : TEST_FAIL)
  474. #endif /* DEBUG_WOLFSSL_VERBOSE */
  475. #define TEST_STRING "Everyone gets Friday off."
  476. #define TEST_STRING_SZ 25
  477. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  478. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  479. #define TEST_RSA_BITS 1024
  480. #else
  481. #define TEST_RSA_BITS 2048
  482. #endif
  483. #define TEST_RSA_BYTES (TEST_RSA_BITS/8)
  484. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  485. (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
  486. static const char* bogusFile =
  487. #ifdef _WIN32
  488. "NUL"
  489. #else
  490. "/dev/null"
  491. #endif
  492. ;
  493. #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
  494. enum {
  495. TESTING_RSA = 1,
  496. TESTING_ECC = 2
  497. };
  498. #ifdef WOLFSSL_QNX_CAAM
  499. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  500. static int testDevId = WOLFSSL_CAAM_DEVID;
  501. #else
  502. static int testDevId = INVALID_DEVID;
  503. #endif
  504. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  505. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  506. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  507. #define HAVE_IO_TESTS_DEPENDENCIES
  508. #endif
  509. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  510. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  511. #define HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  512. #endif
  513. /*----------------------------------------------------------------------------*
  514. | BIO with fixed read/write size
  515. *----------------------------------------------------------------------------*/
  516. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  517. static int wolfssl_bio_s_fixed_mem_write(WOLFSSL_BIO* bio, const char* data,
  518. int len)
  519. {
  520. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  521. len = 0;
  522. }
  523. else {
  524. if (bio->wrSz - bio->wrIdx < len) {
  525. len = bio->wrSz - bio->wrIdx;
  526. }
  527. XMEMCPY((char*)bio->ptr + bio->wrIdx, data, len);
  528. bio->wrIdx += len;
  529. }
  530. return len;
  531. }
  532. static int wolfssl_bio_s_fixed_mem_read(WOLFSSL_BIO* bio, char* data, int len)
  533. {
  534. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  535. len = 0;
  536. }
  537. else {
  538. if (bio->wrSz - bio->rdIdx < len) {
  539. len = bio->wrSz - bio->rdIdx;
  540. }
  541. XMEMCPY(data, (char*)bio->ptr + bio->rdIdx, len);
  542. bio->rdIdx += len;
  543. }
  544. return len;
  545. }
  546. static WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_fixed_mem(void)
  547. {
  548. static WOLFSSL_BIO_METHOD meth;
  549. meth.type = WOLFSSL_BIO_BIO;
  550. XMEMCPY(meth.name, "Fixed Memory Size", 18);
  551. meth.writeCb = wolfssl_bio_s_fixed_mem_write;
  552. meth.readCb = wolfssl_bio_s_fixed_mem_read;
  553. return &meth;
  554. }
  555. #endif
  556. /*----------------------------------------------------------------------------*
  557. | Setup
  558. *----------------------------------------------------------------------------*/
  559. static int test_wolfSSL_Init(void)
  560. {
  561. EXPECT_DECLS;
  562. ExpectIntEQ(wolfSSL_Init(), WOLFSSL_SUCCESS);
  563. return EXPECT_RESULT();
  564. }
  565. static int test_wolfSSL_Cleanup(void)
  566. {
  567. EXPECT_DECLS;
  568. ExpectIntEQ(wolfSSL_Cleanup(), WOLFSSL_SUCCESS);
  569. return EXPECT_RESULT();
  570. }
  571. /* Initialize the wolfCrypt state.
  572. * POST: 0 success.
  573. */
  574. static int test_wolfCrypt_Init(void)
  575. {
  576. EXPECT_DECLS;
  577. ExpectIntEQ(wolfCrypt_Init(), 0);
  578. return EXPECT_RESULT();
  579. } /* END test_wolfCrypt_Init */
  580. static int test_wolfCrypt_Cleanup(void)
  581. {
  582. EXPECT_DECLS;
  583. ExpectIntEQ(wolfCrypt_Cleanup(), 0);
  584. return EXPECT_RESULT();
  585. }
  586. /*----------------------------------------------------------------------------*
  587. | Platform dependent function test
  588. *----------------------------------------------------------------------------*/
  589. static int test_fileAccess(void)
  590. {
  591. EXPECT_DECLS;
  592. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  593. const char *fname[] = {
  594. svrCertFile, svrKeyFile, caCertFile,
  595. eccCertFile, eccKeyFile, eccRsaCertFile,
  596. cliCertFile, cliCertDerFile, cliKeyFile,
  597. dhParamFile,
  598. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  599. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  600. NULL
  601. };
  602. const char derfile[] = "./certs/server-cert.der";
  603. XFILE f = XBADFILE;
  604. size_t sz;
  605. byte *buff = NULL;
  606. int i;
  607. ExpectTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  608. for (i=0; EXPECT_SUCCESS() && fname[i] != NULL ; i++) {
  609. ExpectTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  610. XFCLOSE(f);
  611. }
  612. ExpectTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  613. ExpectTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  614. ExpectIntGE(sz = (size_t) XFTELL(f), sizeof_server_cert_der_2048);
  615. ExpectTrue(XFSEEK(f, 0, XSEEK_SET) == 0);
  616. ExpectTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL);
  617. ExpectTrue(XFREAD(buff, 1, sz, f) == sz);
  618. ExpectIntEQ(XMEMCMP(server_cert_der_2048, buff, sz), 0);
  619. XFREE(buff, NULL, DYNAMIC_TYPE_FILE);
  620. XFCLOSE(f);
  621. #endif
  622. return EXPECT_RESULT();
  623. }
  624. /*----------------------------------------------------------------------------*
  625. | Method Allocators
  626. *----------------------------------------------------------------------------*/
  627. static int test_wolfSSL_Method_Allocators(void)
  628. {
  629. EXPECT_DECLS;
  630. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  631. do { \
  632. WOLFSSL_METHOD *method = NULL; \
  633. condition(method = allocator()); \
  634. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  635. } while (0)
  636. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  637. TEST_METHOD_ALLOCATOR(a, ExpectNotNull)
  638. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  639. TEST_METHOD_ALLOCATOR(a, ExpectNull)
  640. #ifndef NO_OLD_TLS
  641. #ifdef WOLFSSL_ALLOW_SSLV3
  642. #ifndef NO_WOLFSSL_SERVER
  643. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  644. #endif
  645. #ifndef NO_WOLFSSL_CLIENT
  646. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  647. #endif
  648. #endif
  649. #ifdef WOLFSSL_ALLOW_TLSV10
  650. #ifndef NO_WOLFSSL_SERVER
  651. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  652. #endif
  653. #ifndef NO_WOLFSSL_CLIENT
  654. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  655. #endif
  656. #endif
  657. #ifndef NO_WOLFSSL_SERVER
  658. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  659. #endif
  660. #ifndef NO_WOLFSSL_CLIENT
  661. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  662. #endif
  663. #endif /* !NO_OLD_TLS */
  664. #ifndef WOLFSSL_NO_TLS12
  665. #ifndef NO_WOLFSSL_SERVER
  666. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  667. #endif
  668. #ifndef NO_WOLFSSL_CLIENT
  669. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  670. #endif
  671. #endif /* !WOLFSSL_NO_TLS12 */
  672. #ifdef WOLFSSL_TLS13
  673. #ifndef NO_WOLFSSL_SERVER
  674. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  675. #endif
  676. #ifndef NO_WOLFSSL_CLIENT
  677. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  678. #endif
  679. #endif /* WOLFSSL_TLS13 */
  680. #ifndef NO_WOLFSSL_SERVER
  681. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  682. #endif
  683. #ifndef NO_WOLFSSL_CLIENT
  684. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  685. #endif
  686. #ifdef WOLFSSL_DTLS
  687. #ifndef NO_OLD_TLS
  688. #ifndef NO_WOLFSSL_SERVER
  689. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  690. #endif
  691. #ifndef NO_WOLFSSL_CLIENT
  692. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  693. #endif
  694. #endif
  695. #ifndef WOLFSSL_NO_TLS12
  696. #ifndef NO_WOLFSSL_SERVER
  697. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  698. #endif
  699. #ifndef NO_WOLFSSL_CLIENT
  700. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  701. #endif
  702. #endif
  703. #endif /* WOLFSSL_DTLS */
  704. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  705. /* Stubs */
  706. #ifndef NO_WOLFSSL_SERVER
  707. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  708. #endif
  709. #ifndef NO_WOLFSSL_CLIENT
  710. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  711. #endif
  712. #endif
  713. /* Test Either Method (client or server) */
  714. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  715. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  716. #ifndef NO_OLD_TLS
  717. #ifdef WOLFSSL_ALLOW_TLSV10
  718. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  719. #endif
  720. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  721. #endif /* !NO_OLD_TLS */
  722. #ifndef WOLFSSL_NO_TLS12
  723. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  724. #endif /* !WOLFSSL_NO_TLS12 */
  725. #ifdef WOLFSSL_TLS13
  726. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  727. #endif /* WOLFSSL_TLS13 */
  728. #ifdef WOLFSSL_DTLS
  729. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  730. #ifndef NO_OLD_TLS
  731. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  732. #endif /* !NO_OLD_TLS */
  733. #ifndef WOLFSSL_NO_TLS12
  734. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  735. #endif /* !WOLFSSL_NO_TLS12 */
  736. #endif /* WOLFSSL_DTLS */
  737. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  738. return EXPECT_RESULT();
  739. }
  740. /*----------------------------------------------------------------------------*
  741. | Context
  742. *----------------------------------------------------------------------------*/
  743. #ifndef NO_WOLFSSL_SERVER
  744. static int test_wolfSSL_CTX_new(void)
  745. {
  746. EXPECT_DECLS;
  747. WOLFSSL_CTX *ctx;
  748. WOLFSSL_METHOD* method;
  749. ExpectNull(ctx = wolfSSL_CTX_new(NULL));
  750. ExpectNotNull(method = wolfSSLv23_server_method());
  751. ExpectNotNull(ctx = wolfSSL_CTX_new(method));
  752. wolfSSL_CTX_free(ctx);
  753. return EXPECT_RESULT();
  754. }
  755. #endif
  756. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  757. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  758. static int test_for_double_Free(void)
  759. {
  760. EXPECT_DECLS;
  761. WOLFSSL_CTX* ctx = NULL;
  762. WOLFSSL* ssl = NULL;
  763. int skipTest = 0;
  764. const char* testCertFile;
  765. const char* testKeyFile;
  766. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  767. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  768. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  769. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  770. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  771. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  772. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  773. "NULL-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  774. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  775. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  776. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  777. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  778. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  779. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  780. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  781. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  782. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  783. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  784. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  785. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  786. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  787. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  788. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  789. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  790. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  791. "LY1305-OLD:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  792. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  793. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  794. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  795. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  796. /* OpenVPN uses a "blacklist" method to specify which ciphers NOT to use */
  797. #ifdef OPENSSL_EXTRA
  798. char openvpnCiphers[] = "DEFAULT:!EXP:!LOW:!MEDIUM:!kDH:!kECDH:!DSS:!PSK:"
  799. "!SRP:!kRSA:!aNULL:!eNULL";
  800. #endif
  801. #ifndef NO_RSA
  802. testCertFile = svrCertFile;
  803. testKeyFile = svrKeyFile;
  804. #elif defined(HAVE_ECC)
  805. testCertFile = eccCertFile;
  806. testKeyFile = eccKeyFile;
  807. #else
  808. skipTest = 1;
  809. #endif
  810. if (skipTest != 1) {
  811. #ifndef NO_WOLFSSL_SERVER
  812. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  813. #else
  814. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  815. #endif
  816. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  817. WOLFSSL_FILETYPE_PEM));
  818. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  819. WOLFSSL_FILETYPE_PEM));
  820. ExpectNotNull(ssl = wolfSSL_new(ctx));
  821. /* First test freeing SSL, then CTX */
  822. wolfSSL_free(ssl);
  823. ssl = NULL;
  824. wolfSSL_CTX_free(ctx);
  825. ctx = NULL;
  826. #ifndef NO_WOLFSSL_CLIENT
  827. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  828. #else
  829. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  830. #endif
  831. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  832. WOLFSSL_FILETYPE_PEM));
  833. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  834. WOLFSSL_FILETYPE_PEM));
  835. ExpectNotNull(ssl = wolfSSL_new(ctx));
  836. /* Next test freeing CTX then SSL */
  837. wolfSSL_CTX_free(ctx);
  838. ctx = NULL;
  839. wolfSSL_free(ssl);
  840. ssl = NULL;
  841. #ifndef NO_WOLFSSL_SERVER
  842. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  843. #else
  844. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  845. #endif
  846. /* Test setting ciphers at ctx level */
  847. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  848. WOLFSSL_FILETYPE_PEM));
  849. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  850. WOLFSSL_FILETYPE_PEM));
  851. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  852. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  853. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  854. /* only update TLSv13 suites */
  855. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  856. #endif
  857. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  858. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  859. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  860. /* only update pre-TLSv13 suites */
  861. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx,
  862. "ECDHE-RSA-AES128-GCM-SHA256"));
  863. #endif
  864. #ifdef OPENSSL_EXTRA
  865. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, openvpnCiphers));
  866. #endif
  867. ExpectNotNull(ssl = wolfSSL_new(ctx));
  868. wolfSSL_CTX_free(ctx);
  869. ctx = NULL;
  870. wolfSSL_free(ssl);
  871. ssl = NULL;
  872. #ifndef NO_WOLFSSL_CLIENT
  873. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  874. #else
  875. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  876. #endif
  877. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  878. WOLFSSL_FILETYPE_PEM));
  879. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  880. WOLFSSL_FILETYPE_PEM));
  881. ExpectNotNull(ssl = wolfSSL_new(ctx));
  882. /* test setting ciphers at SSL level */
  883. ExpectTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  884. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  885. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  886. /* only update TLSv13 suites */
  887. ExpectTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  888. #endif
  889. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  890. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  891. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  892. /* only update pre-TLSv13 suites */
  893. ExpectTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  894. #endif
  895. wolfSSL_CTX_free(ctx);
  896. ctx = NULL;
  897. wolfSSL_free(ssl);
  898. ssl = NULL;
  899. }
  900. return EXPECT_RESULT();
  901. }
  902. #endif
  903. static int test_wolfSSL_CTX_set_cipher_list_bytes(void)
  904. {
  905. EXPECT_DECLS;
  906. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)) && \
  907. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  908. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  909. const char* testCertFile;
  910. const char* testKeyFile;
  911. WOLFSSL_CTX* ctx = NULL;
  912. WOLFSSL* ssl = NULL;
  913. const byte cipherList[] =
  914. {
  915. /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x16,
  916. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x39,
  917. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x33,
  918. /* TLS_DH_anon_WITH_AES_128_CBC_SHA */ 0xC0, 0x34,
  919. /* TLS_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x35,
  920. /* TLS_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x2F,
  921. /* TLS_RSA_WITH_NULL_MD5 */ 0xC0, 0x01,
  922. /* TLS_RSA_WITH_NULL_SHA */ 0xC0, 0x02,
  923. /* TLS_PSK_WITH_AES_256_CBC_SHA */ 0xC0, 0x8d,
  924. /* TLS_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0xae,
  925. /* TLS_PSK_WITH_AES_256_CBC_SHA384 */ 0xC0, 0xaf,
  926. /* TLS_PSK_WITH_AES_128_CBC_SHA */ 0xC0, 0x8c,
  927. /* TLS_PSK_WITH_NULL_SHA256 */ 0xC0, 0xb0,
  928. /* TLS_PSK_WITH_NULL_SHA384 */ 0xC0, 0xb1,
  929. /* TLS_PSK_WITH_NULL_SHA */ 0xC0, 0x2c,
  930. /* SSL_RSA_WITH_RC4_128_SHA */ 0xC0, 0x05,
  931. /* SSL_RSA_WITH_RC4_128_MD5 */ 0xC0, 0x04,
  932. /* SSL_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0A,
  933. /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
  934. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x14,
  935. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x13,
  936. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0A,
  937. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x09,
  938. /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */ 0xC0, 0x11,
  939. /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x07,
  940. /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x12,
  941. /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x08,
  942. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x27,
  943. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256*/ 0xC0, 0x23,
  944. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x28,
  945. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384*/ 0xC0, 0x24,
  946. /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */ 0xC0, 0x06,
  947. /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */ 0xC0, 0x3a,
  948. /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x37,
  949. /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
  950. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0F,
  951. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x0E,
  952. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x05,
  953. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x04,
  954. /* TLS_ECDH_RSA_WITH_RC4_128_SHA */ 0xC0, 0x0C,
  955. /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x02,
  956. /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0D,
  957. /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x03,
  958. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x29,
  959. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x25,
  960. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x2A,
  961. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x26,
  962. /* WDM_WITH_NULL_SHA256 */ 0x00, 0xFE, /* wolfSSL DTLS Multicast */
  963. /* SHA256 */
  964. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x6b,
  965. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x67,
  966. /* TLS_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x3d,
  967. /* TLS_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x3c,
  968. /* TLS_RSA_WITH_NULL_SHA256 */ 0x00, 0x3b,
  969. /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */ 0x00, 0xb2,
  970. /* TLS_DHE_PSK_WITH_NULL_SHA256 */ 0x00, 0xb4,
  971. /* SHA384 */
  972. /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */ 0x00, 0xb3,
  973. /* TLS_DHE_PSK_WITH_NULL_SHA384 */ 0x00, 0xb5,
  974. /* AES-GCM */
  975. /* TLS_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9c,
  976. /* TLS_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9d,
  977. /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9e,
  978. /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9f,
  979. /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa7,
  980. /* TLS_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xa8,
  981. /* TLS_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa9,
  982. /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xaa,
  983. /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xab,
  984. /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
  985. /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2b,
  986. /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2c,
  987. /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2d,
  988. /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2e,
  989. /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2f,
  990. /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x30,
  991. /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x31,
  992. /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x32,
  993. /* AES-CCM, first byte is 0xC0 but isn't ECC,
  994. * also, in some of the other AES-CCM suites
  995. * there will be second byte number conflicts
  996. * with non-ECC AES-GCM */
  997. /* TLS_RSA_WITH_AES_128_CCM_8 */ 0xC0, 0xa0,
  998. /* TLS_RSA_WITH_AES_256_CCM_8 */ 0xC0, 0xa1,
  999. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */ 0xC0, 0xac,
  1000. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */ 0xC0, 0xae,
  1001. /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */ 0xC0, 0xaf,
  1002. /* TLS_PSK_WITH_AES_128_CCM */ 0xC0, 0xa4,
  1003. /* TLS_PSK_WITH_AES_256_CCM */ 0xC0, 0xa5,
  1004. /* TLS_PSK_WITH_AES_128_CCM_8 */ 0xC0, 0xa8,
  1005. /* TLS_PSK_WITH_AES_256_CCM_8 */ 0xC0, 0xa9,
  1006. /* TLS_DHE_PSK_WITH_AES_128_CCM */ 0xC0, 0xa6,
  1007. /* TLS_DHE_PSK_WITH_AES_256_CCM */ 0xC0, 0xa7,
  1008. /* Camellia */
  1009. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x41,
  1010. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x84,
  1011. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xba,
  1012. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc0,
  1013. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x45,
  1014. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x88,
  1015. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xbe,
  1016. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc4,
  1017. /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
  1018. /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa8,
  1019. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa9,
  1020. /* TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xaa,
  1021. /* TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xac,
  1022. /* TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xab,
  1023. /* TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xad,
  1024. /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
  1025. /* TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x13,
  1026. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x14,
  1027. /* TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x15,
  1028. /* ECDHE_PSK RFC8442, first byte is 0xD0 (ECDHE_PSK_BYTE) */
  1029. /* TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 */ 0xD0, 0x01,
  1030. /* TLS v1.3 cipher suites */
  1031. /* TLS_AES_128_GCM_SHA256 */ 0x13, 0x01,
  1032. /* TLS_AES_256_GCM_SHA384 */ 0x13, 0x02,
  1033. /* TLS_CHACHA20_POLY1305_SHA256 */ 0x13, 0x03,
  1034. /* TLS_AES_128_CCM_SHA256 */ 0x13, 0x04,
  1035. /* TLS_AES_128_CCM_8_SHA256 */ 0x13, 0x05,
  1036. /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
  1037. /* TLS_SHA256_SHA256 */ 0xC0, 0xB4,
  1038. /* TLS_SHA384_SHA384 */ 0xC0, 0xB5
  1039. };
  1040. #ifndef NO_RSA
  1041. testCertFile = svrCertFile;
  1042. testKeyFile = svrKeyFile;
  1043. #elif defined(HAVE_ECC)
  1044. testCertFile = eccCertFile;
  1045. testKeyFile = eccKeyFile;
  1046. #endif
  1047. #ifndef NO_WOLFSSL_SERVER
  1048. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1049. #else
  1050. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1051. #endif
  1052. ExpectTrue(wolfSSL_CTX_set_cipher_list_bytes(ctx, &cipherList[0U],
  1053. sizeof(cipherList)));
  1054. wolfSSL_CTX_free(ctx);
  1055. ctx = NULL;
  1056. #ifndef NO_WOLFSSL_SERVER
  1057. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1058. #else
  1059. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1060. #endif
  1061. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1062. WOLFSSL_FILETYPE_PEM));
  1063. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1064. WOLFSSL_FILETYPE_PEM));
  1065. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1066. ExpectTrue(wolfSSL_set_cipher_list_bytes(ssl, &cipherList[0U],
  1067. sizeof(cipherList)));
  1068. wolfSSL_free(ssl);
  1069. wolfSSL_CTX_free(ctx);
  1070. #endif /* (OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES) &&
  1071. (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && (!NO_RSA || HAVE_ECC) */
  1072. return EXPECT_RESULT();
  1073. }
  1074. static int test_wolfSSL_CTX_use_certificate_file(void)
  1075. {
  1076. EXPECT_DECLS;
  1077. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1078. WOLFSSL_CTX *ctx = NULL;
  1079. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1080. /* invalid context */
  1081. ExpectFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  1082. WOLFSSL_FILETYPE_PEM));
  1083. /* invalid cert file */
  1084. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  1085. WOLFSSL_FILETYPE_PEM));
  1086. /* invalid cert type */
  1087. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  1088. #ifdef NO_RSA
  1089. /* rsa needed */
  1090. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1091. WOLFSSL_FILETYPE_PEM));
  1092. #else
  1093. /* success */
  1094. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1095. WOLFSSL_FILETYPE_PEM));
  1096. #endif
  1097. wolfSSL_CTX_free(ctx);
  1098. #endif
  1099. return EXPECT_RESULT();
  1100. }
  1101. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  1102. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  1103. {
  1104. EXPECT_DECLS;
  1105. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  1106. WOLFSSL_CTX* ctx = NULL;
  1107. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1108. ExpectIntEQ(SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  1109. server_cert_der_2048), WOLFSSL_SUCCESS);
  1110. wolfSSL_CTX_free(ctx);
  1111. #endif
  1112. return EXPECT_RESULT();
  1113. }
  1114. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  1115. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  1116. * context using buffer.
  1117. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  1118. * --enable-testcert flag.
  1119. */
  1120. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  1121. {
  1122. EXPECT_DECLS;
  1123. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  1124. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  1125. WOLFSSL_CTX* ctx = NULL;
  1126. int ret;
  1127. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1128. ExpectIntEQ(ret = wolfSSL_CTX_use_certificate_buffer(ctx,
  1129. server_cert_der_2048, sizeof_server_cert_der_2048,
  1130. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1131. wolfSSL_CTX_free(ctx);
  1132. #endif
  1133. return EXPECT_RESULT();
  1134. } /* END test_wolfSSL_CTX_use_certificate_buffer */
  1135. static int test_wolfSSL_CTX_use_PrivateKey_file(void)
  1136. {
  1137. EXPECT_DECLS;
  1138. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1139. WOLFSSL_CTX *ctx = NULL;
  1140. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1141. /* invalid context */
  1142. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  1143. WOLFSSL_FILETYPE_PEM));
  1144. /* invalid key file */
  1145. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  1146. WOLFSSL_FILETYPE_PEM));
  1147. /* invalid key type */
  1148. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  1149. /* success */
  1150. #ifdef NO_RSA
  1151. /* rsa needed */
  1152. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1153. WOLFSSL_FILETYPE_PEM));
  1154. #else
  1155. /* success */
  1156. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1157. WOLFSSL_FILETYPE_PEM));
  1158. #endif
  1159. wolfSSL_CTX_free(ctx);
  1160. #endif
  1161. return EXPECT_RESULT();
  1162. }
  1163. /* test both file and buffer versions along with unloading trusted peer certs */
  1164. static int test_wolfSSL_CTX_trust_peer_cert(void)
  1165. {
  1166. EXPECT_DECLS;
  1167. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1168. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_RSA)
  1169. WOLFSSL_CTX *ctx = NULL;
  1170. WOLFSSL* ssl = NULL;
  1171. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1172. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1173. #if !defined(NO_FILESYSTEM)
  1174. /* invalid file */
  1175. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  1176. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1177. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  1178. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1179. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1180. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1181. /* success */
  1182. ExpectIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1183. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1184. /* unload cert */
  1185. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1186. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1187. /* invalid file */
  1188. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
  1189. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1190. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
  1191. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1192. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1193. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1194. /* success */
  1195. ExpectIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1196. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1197. #ifdef WOLFSSL_LOCAL_X509_STORE
  1198. /* unload cert */
  1199. ExpectIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1200. ExpectIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
  1201. #endif
  1202. #endif
  1203. /* Test of loading certs from buffers */
  1204. /* invalid buffer */
  1205. ExpectIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  1206. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1207. /* success */
  1208. #ifdef USE_CERT_BUFFERS_1024
  1209. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  1210. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1211. #endif
  1212. #ifdef USE_CERT_BUFFERS_2048
  1213. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  1214. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1215. #endif
  1216. /* unload cert */
  1217. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1218. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1219. wolfSSL_free(ssl);
  1220. wolfSSL_CTX_free(ctx);
  1221. #endif
  1222. return EXPECT_RESULT();
  1223. }
  1224. static int test_wolfSSL_CTX_load_verify_locations(void)
  1225. {
  1226. EXPECT_DECLS;
  1227. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  1228. WOLFSSL_CTX *ctx = NULL;
  1229. #ifndef NO_RSA
  1230. WOLFSSL_CERT_MANAGER* cm = NULL;
  1231. #ifdef PERSIST_CERT_CACHE
  1232. int cacheSz = 0;
  1233. unsigned char* cache = NULL;
  1234. int used = 0;
  1235. #ifndef NO_FILESYSTEM
  1236. const char* cacheFile = "./tests/cert_cache.tmp";
  1237. #endif
  1238. int i;
  1239. int t;
  1240. int* p;
  1241. #endif
  1242. #endif
  1243. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1244. const char* load_certs_path = "./certs/external";
  1245. const char* load_no_certs_path = "./examples";
  1246. const char* load_expired_path = "./certs/test/expired";
  1247. #endif
  1248. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1249. /* invalid arguments */
  1250. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL),
  1251. WOLFSSL_FAILURE);
  1252. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL),
  1253. WOLFSSL_FAILURE);
  1254. /* invalid ca file */
  1255. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  1256. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  1257. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  1258. ((defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)) && \
  1259. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  1260. /* invalid path */
  1261. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  1262. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  1263. #endif
  1264. #if defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)
  1265. /* test ignoring the invalid path */
  1266. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, bogusFile,
  1267. WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR), WOLFSSL_SUCCESS);
  1268. #endif
  1269. /* load ca cert */
  1270. #ifdef NO_RSA
  1271. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1272. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  1273. #else /* Skip the following test without RSA certs. */
  1274. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1275. WOLFSSL_SUCCESS);
  1276. #ifdef PERSIST_CERT_CACHE
  1277. /* Get cert cache size */
  1278. ExpectIntGT(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), 0);
  1279. ExpectNotNull(cache = (byte*)XMALLOC(cacheSz, NULL,
  1280. DYNAMIC_TYPE_TMP_BUFFER));
  1281. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, NULL),
  1282. BAD_FUNC_ARG);
  1283. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, -1, NULL),
  1284. BAD_FUNC_ARG);
  1285. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, -1, NULL),
  1286. BAD_FUNC_ARG);
  1287. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, cacheSz, NULL),
  1288. BAD_FUNC_ARG);
  1289. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, &used),
  1290. BAD_FUNC_ARG);
  1291. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, cacheSz, &used),
  1292. BAD_FUNC_ARG);
  1293. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, cacheSz, &used),
  1294. BAD_FUNC_ARG);
  1295. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, -1, &used),
  1296. BAD_FUNC_ARG);
  1297. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, NULL),
  1298. BAD_FUNC_ARG);
  1299. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz - 10, &used),
  1300. BUFFER_E);
  1301. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, &used), 1);
  1302. ExpectIntEQ(cacheSz, used);
  1303. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, -1),
  1304. BAD_FUNC_ARG);
  1305. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, -1),
  1306. BAD_FUNC_ARG);
  1307. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, -1),
  1308. BAD_FUNC_ARG);
  1309. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, cacheSz),
  1310. BAD_FUNC_ARG);
  1311. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, cacheSz),
  1312. BAD_FUNC_ARG);
  1313. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, cacheSz),
  1314. BAD_FUNC_ARG);
  1315. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, -1),
  1316. BAD_FUNC_ARG);
  1317. /* Smaller than header. */
  1318. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, 1), BUFFER_E);
  1319. for (i = 1; i < cacheSz; i++) {
  1320. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz - i),
  1321. BUFFER_E);
  1322. }
  1323. if (EXPECT_SUCCESS()) {
  1324. /* Modify header for bad results! */
  1325. p = (int*)cache;
  1326. /* version */
  1327. t = p[0]; p[0] = 0xff;
  1328. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1329. CACHE_MATCH_ERROR);
  1330. p[0] = t; p++;
  1331. /* rows */
  1332. t = p[0]; p[0] = 0xff;
  1333. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1334. CACHE_MATCH_ERROR);
  1335. p[0] = t; p++;
  1336. /* columns[0] */
  1337. t = p[0]; p[0] = -1;
  1338. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1339. PARSE_ERROR);
  1340. p[0] = t; p += CA_TABLE_SIZE;
  1341. /* signerSz*/
  1342. t = p[0]; p[0] = 0xff;
  1343. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1344. CACHE_MATCH_ERROR);
  1345. p[0] = t;
  1346. }
  1347. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz), 1);
  1348. ExpectIntEQ(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), used);
  1349. #ifndef NO_FILESYSTEM
  1350. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1351. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1352. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1353. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, cacheFile), 1);
  1354. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1355. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1356. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1357. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "no-file"),
  1358. WOLFSSL_BAD_FILE);
  1359. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, cacheFile), 1);
  1360. /* File contents is not a cache. */
  1361. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "./certs/ca-cert.pem"),
  1362. CACHE_MATCH_ERROR);
  1363. #endif
  1364. XFREE(cache, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1365. #endif
  1366. /* Test unloading CA's */
  1367. ExpectIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  1368. #ifdef PERSIST_CERT_CACHE
  1369. /* Verify no certs (result is less than cacheSz) */
  1370. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1371. #endif
  1372. /* load ca cert again */
  1373. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1374. WOLFSSL_SUCCESS);
  1375. /* Test getting CERT_MANAGER */
  1376. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  1377. /* Test unloading CA's using CM */
  1378. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  1379. #ifdef PERSIST_CERT_CACHE
  1380. /* Verify no certs (result is less than cacheSz) */
  1381. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1382. #endif
  1383. #endif
  1384. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1385. /* Test loading CA certificates using a path */
  1386. #ifdef NO_RSA
  1387. /* failure here okay since certs in external directory are RSA */
  1388. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1389. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1390. #else
  1391. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1392. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1393. #endif
  1394. /* Test loading path with no files */
  1395. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1396. load_no_certs_path, WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  1397. /* Test loading expired CA certificates */
  1398. #ifdef NO_RSA
  1399. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1400. load_expired_path,
  1401. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1402. WOLFSSL_SUCCESS);
  1403. #else
  1404. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1405. load_expired_path,
  1406. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1407. WOLFSSL_SUCCESS);
  1408. #endif
  1409. /* Test loading CA certificates and ignoring all errors */
  1410. #ifdef NO_RSA
  1411. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1412. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  1413. #else
  1414. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1415. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  1416. #endif
  1417. #endif
  1418. wolfSSL_CTX_free(ctx);
  1419. #endif
  1420. return EXPECT_RESULT();
  1421. }
  1422. static int test_wolfSSL_CTX_load_system_CA_certs(void)
  1423. {
  1424. int res = TEST_SKIPPED;
  1425. #if defined(WOLFSSL_SYS_CA_CERTS) && !defined(NO_WOLFSSL_CLIENT) && \
  1426. (!defined(NO_RSA) || defined(HAVE_ECC))
  1427. WOLFSSL_CTX* ctx;
  1428. byte dirValid = 0;
  1429. int ret = 0;
  1430. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1431. if (ctx == NULL) {
  1432. fprintf(stderr, "wolfSSL_CTX_new failed.\n");
  1433. ret = -1;
  1434. }
  1435. if (ret == 0) {
  1436. #if defined(USE_WINDOWS_API) || defined(__APPLE__)
  1437. dirValid = 1;
  1438. #else
  1439. word32 numDirs;
  1440. const char** caDirs = wolfSSL_get_system_CA_dirs(&numDirs);
  1441. if (caDirs == NULL || numDirs == 0) {
  1442. fprintf(stderr, "wolfSSL_get_system_CA_dirs failed.\n");
  1443. ret = -1;
  1444. }
  1445. else {
  1446. ReadDirCtx dirCtx;
  1447. word32 i;
  1448. for (i = 0; i < numDirs; ++i) {
  1449. if (wc_ReadDirFirst(&dirCtx, caDirs[i], NULL) == 0) {
  1450. /* Directory isn't empty. */
  1451. dirValid = 1;
  1452. wc_ReadDirClose(&dirCtx);
  1453. break;
  1454. }
  1455. }
  1456. }
  1457. #endif
  1458. }
  1459. /*
  1460. * If the directory isn't empty, we should be able to load CA
  1461. * certs from it. On Windows/Mac, we assume the CA cert stores are
  1462. * usable.
  1463. */
  1464. if (ret == 0 && dirValid && wolfSSL_CTX_load_system_CA_certs(ctx) !=
  1465. WOLFSSL_SUCCESS) {
  1466. fprintf(stderr, "wolfSSL_CTX_load_system_CA_certs failed.\n");
  1467. ret = -1;
  1468. }
  1469. #ifdef OPENSSL_EXTRA
  1470. if (ret == 0 &&
  1471. wolfSSL_CTX_set_default_verify_paths(ctx) != WOLFSSL_SUCCESS) {
  1472. fprintf(stderr, "wolfSSL_CTX_set_default_verify_paths failed.\n");
  1473. ret = -1;
  1474. }
  1475. #endif /* OPENSSL_EXTRA */
  1476. wolfSSL_CTX_free(ctx);
  1477. res = TEST_RES_CHECK(ret == 0);
  1478. #endif /* WOLFSSL_SYS_CA_CERTS && !NO_WOLFSSL_CLIENT */
  1479. return res;
  1480. }
  1481. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1482. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz,
  1483. int file_type)
  1484. {
  1485. int ret;
  1486. WOLFSSL_CERT_MANAGER* cm;
  1487. cm = wolfSSL_CertManagerNew();
  1488. if (cm == NULL) {
  1489. fprintf(stderr, "test_cm_load_ca failed\n");
  1490. return -1;
  1491. }
  1492. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  1493. wolfSSL_CertManagerFree(cm);
  1494. return ret;
  1495. }
  1496. static int test_cm_load_ca_file(const char* ca_cert_file)
  1497. {
  1498. int ret = 0;
  1499. byte* cert_buf = NULL;
  1500. size_t cert_sz = 0;
  1501. #if defined(WOLFSSL_PEM_TO_DER)
  1502. DerBuffer* pDer = NULL;
  1503. #endif
  1504. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1505. if (ret == 0) {
  1506. /* normal test */
  1507. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  1508. if (ret == WOLFSSL_SUCCESS) {
  1509. /* test including null terminator in length */
  1510. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1511. if (tmp == NULL) {
  1512. ret = MEMORY_E;
  1513. }
  1514. else {
  1515. cert_buf = tmp;
  1516. cert_buf[cert_sz] = '\0';
  1517. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  1518. WOLFSSL_FILETYPE_PEM);
  1519. }
  1520. }
  1521. #if defined(WOLFSSL_PEM_TO_DER)
  1522. if (ret == WOLFSSL_SUCCESS) {
  1523. /* test loading DER */
  1524. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1525. if (ret == 0 && pDer != NULL) {
  1526. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  1527. WOLFSSL_FILETYPE_ASN1);
  1528. wc_FreeDer(&pDer);
  1529. }
  1530. }
  1531. #endif
  1532. }
  1533. free(cert_buf);
  1534. return ret;
  1535. }
  1536. static int test_cm_load_ca_buffer_ex(const byte* cert_buf, size_t cert_sz,
  1537. int file_type, word32 flags)
  1538. {
  1539. int ret;
  1540. WOLFSSL_CERT_MANAGER* cm;
  1541. cm = wolfSSL_CertManagerNew();
  1542. if (cm == NULL) {
  1543. fprintf(stderr, "test_cm_load_ca failed\n");
  1544. return -1;
  1545. }
  1546. ret = wolfSSL_CertManagerLoadCABuffer_ex(cm, cert_buf, cert_sz, file_type,
  1547. 0, flags);
  1548. wolfSSL_CertManagerFree(cm);
  1549. return ret;
  1550. }
  1551. static int test_cm_load_ca_file_ex(const char* ca_cert_file, word32 flags)
  1552. {
  1553. int ret = 0;
  1554. byte* cert_buf = NULL;
  1555. size_t cert_sz = 0;
  1556. #if defined(WOLFSSL_PEM_TO_DER)
  1557. DerBuffer* pDer = NULL;
  1558. #endif
  1559. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1560. if (ret == 0) {
  1561. /* normal test */
  1562. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz,
  1563. WOLFSSL_FILETYPE_PEM, flags);
  1564. if (ret == WOLFSSL_SUCCESS) {
  1565. /* test including null terminator in length */
  1566. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1567. if (tmp == NULL) {
  1568. ret = MEMORY_E;
  1569. }
  1570. else {
  1571. cert_buf = tmp;
  1572. cert_buf[cert_sz] = '\0';
  1573. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz+1,
  1574. WOLFSSL_FILETYPE_PEM, flags);
  1575. }
  1576. }
  1577. #if defined(WOLFSSL_PEM_TO_DER)
  1578. if (ret == WOLFSSL_SUCCESS) {
  1579. /* test loading DER */
  1580. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1581. if (ret == 0 && pDer != NULL) {
  1582. ret = test_cm_load_ca_buffer_ex(pDer->buffer, pDer->length,
  1583. WOLFSSL_FILETYPE_ASN1, flags);
  1584. wc_FreeDer(&pDer);
  1585. }
  1586. }
  1587. #endif
  1588. }
  1589. free(cert_buf);
  1590. return ret;
  1591. }
  1592. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  1593. static int test_wolfSSL_CertManagerAPI(void)
  1594. {
  1595. EXPECT_DECLS;
  1596. #ifndef NO_CERTS
  1597. WOLFSSL_CERT_MANAGER* cm = NULL;
  1598. unsigned char c;
  1599. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1600. wolfSSL_CertManagerFree(NULL);
  1601. ExpectIntEQ(wolfSSL_CertManager_up_ref(NULL), 0);
  1602. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(NULL), BAD_FUNC_ARG);
  1603. #ifdef WOLFSSL_TRUST_PEER_CERT
  1604. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(NULL), BAD_FUNC_ARG);
  1605. #endif
  1606. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer_ex(NULL, &c, 1,
  1607. WOLFSSL_FILETYPE_ASN1, 0, 0), WOLFSSL_FATAL_ERROR);
  1608. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1609. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, -1,
  1610. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1611. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, -1,
  1612. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1613. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, -1,
  1614. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1615. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, 1,
  1616. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1617. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, 1,
  1618. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1619. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, 1,
  1620. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1621. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, -1,
  1622. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1623. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, 1, -1),
  1624. WOLFSSL_BAD_FILETYPE);
  1625. #endif
  1626. #if !defined(NO_FILESYSTEM)
  1627. {
  1628. const char* ca_cert = "./certs/ca-cert.pem";
  1629. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1630. const char* ca_cert_der = "./certs/ca-cert.der";
  1631. #endif
  1632. const char* ca_path = "./certs";
  1633. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1634. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, NULL, -1),
  1635. BAD_FUNC_ARG);
  1636. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  1637. BAD_FUNC_ARG);
  1638. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, ca_cert,
  1639. WOLFSSL_FILETYPE_PEM), BAD_FUNC_ARG);
  1640. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert, -1),
  1641. WOLFSSL_BAD_FILETYPE);
  1642. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, "no-file",
  1643. WOLFSSL_FILETYPE_ASN1), WOLFSSL_BAD_FILE);
  1644. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert_der,
  1645. WOLFSSL_FILETYPE_PEM), ASN_NO_PEM_HEADER);
  1646. #endif
  1647. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, NULL),
  1648. WOLFSSL_FATAL_ERROR);
  1649. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, NULL),
  1650. WOLFSSL_FATAL_ERROR);
  1651. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, ca_path),
  1652. WOLFSSL_FATAL_ERROR);
  1653. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, ca_path),
  1654. WOLFSSL_FATAL_ERROR);
  1655. }
  1656. #endif
  1657. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  1658. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), 1);
  1659. #elif !defined(HAVE_CRL)
  1660. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), NOT_COMPILED_IN);
  1661. #endif
  1662. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(NULL), BAD_FUNC_ARG);
  1663. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(cm), 1);
  1664. #ifdef HAVE_CRL
  1665. /* Test APIs when CRL is disabled. */
  1666. #ifdef HAVE_CRL_IO
  1667. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  1668. #endif
  1669. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  1670. sizeof_server_cert_der_2048), 1);
  1671. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  1672. #endif
  1673. /* OCSP */
  1674. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(NULL, 0), BAD_FUNC_ARG);
  1675. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(NULL), BAD_FUNC_ARG);
  1676. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(NULL), BAD_FUNC_ARG);
  1677. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(NULL), BAD_FUNC_ARG);
  1678. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  1679. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  1680. #if !defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  1681. !defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  1682. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), NOT_COMPILED_IN);
  1683. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), NOT_COMPILED_IN);
  1684. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), NOT_COMPILED_IN);
  1685. #endif
  1686. #ifdef HAVE_OCSP
  1687. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, -1), BAD_FUNC_ARG);
  1688. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, -1), BAD_FUNC_ARG);
  1689. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, -1), BAD_FUNC_ARG);
  1690. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, 1), BAD_FUNC_ARG);
  1691. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, 1), BAD_FUNC_ARG);
  1692. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, 1), BAD_FUNC_ARG);
  1693. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, -1), BAD_FUNC_ARG);
  1694. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, NULL, 0,
  1695. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  1696. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, NULL, 1,
  1697. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  1698. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, &c, 1,
  1699. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  1700. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, NULL),
  1701. BAD_FUNC_ARG);
  1702. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, ""),
  1703. BAD_FUNC_ARG);
  1704. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, NULL), 1);
  1705. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(NULL, NULL, NULL, NULL),
  1706. BAD_FUNC_ARG);
  1707. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(cm, NULL, NULL, NULL), 1);
  1708. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(cm), 1);
  1709. /* Test APIs when OCSP is disabled. */
  1710. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, &c, 1,
  1711. NULL, NULL, NULL, NULL), 1);
  1712. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, 1), 1);
  1713. #endif
  1714. ExpectIntEQ(wolfSSL_CertManager_up_ref(cm), 1);
  1715. wolfSSL_CertManagerFree(cm);
  1716. wolfSSL_CertManagerFree(cm);
  1717. cm = NULL;
  1718. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1719. #ifdef HAVE_OCSP
  1720. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, WOLFSSL_OCSP_URL_OVERRIDE |
  1721. WOLFSSL_OCSP_CHECKALL), 1);
  1722. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  1723. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  1724. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  1725. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  1726. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), 1);
  1727. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  1728. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), 1);
  1729. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), 1);
  1730. #endif
  1731. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  1732. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  1733. #endif
  1734. #ifdef WOLFSSL_TRUST_PEER_CERT
  1735. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(cm), 1);
  1736. #endif
  1737. wolfSSL_CertManagerFree(cm);
  1738. #endif
  1739. return EXPECT_RESULT();
  1740. }
  1741. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  1742. {
  1743. EXPECT_DECLS;
  1744. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1745. const char* ca_cert = "./certs/ca-cert.pem";
  1746. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1747. int ret;
  1748. ExpectIntLE(ret = test_cm_load_ca_file(ca_cert), 1);
  1749. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1750. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1751. #elif defined(NO_RSA)
  1752. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  1753. #else
  1754. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  1755. #endif
  1756. ExpectIntLE(ret = test_cm_load_ca_file(ca_expired_cert), 1);
  1757. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1758. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1759. #elif defined(NO_RSA)
  1760. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  1761. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  1762. !defined(NO_ASN_TIME)
  1763. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  1764. #else
  1765. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  1766. #endif
  1767. #endif
  1768. return EXPECT_RESULT();
  1769. }
  1770. static int test_wolfSSL_CertManagerLoadCABuffer_ex(void)
  1771. {
  1772. EXPECT_DECLS;
  1773. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1774. const char* ca_cert = "./certs/ca-cert.pem";
  1775. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1776. int ret;
  1777. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_cert, WOLFSSL_LOAD_FLAG_NONE),
  1778. 1);
  1779. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1780. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1781. #elif defined(NO_RSA)
  1782. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  1783. #else
  1784. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  1785. #endif
  1786. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_expired_cert,
  1787. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), 1);
  1788. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1789. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1790. #elif defined(NO_RSA)
  1791. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  1792. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  1793. !defined(NO_ASN_TIME) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1794. defined(OPENSSL_COMPATIBLE_DEFAULTS)
  1795. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  1796. #else
  1797. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  1798. #endif
  1799. #endif
  1800. return EXPECT_RESULT();
  1801. }
  1802. static int test_wolfSSL_CertManagerGetCerts(void)
  1803. {
  1804. EXPECT_DECLS;
  1805. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1806. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1807. defined(WOLFSSL_SIGNER_DER_CERT)
  1808. WOLFSSL_CERT_MANAGER* cm = NULL;
  1809. WOLFSSL_STACK* sk = NULL;
  1810. X509* x509 = NULL;
  1811. X509* cert1 = NULL;
  1812. FILE* file1 = NULL;
  1813. #ifdef DEBUG_WOLFSSL_VERBOSE
  1814. WOLFSSL_BIO* bio = NULL;
  1815. #endif
  1816. int i = 0;
  1817. int ret = 0;
  1818. const byte* der;
  1819. int derSz = 0;
  1820. ExpectNotNull(file1 = fopen("./certs/ca-cert.pem", "rb"));
  1821. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  1822. if (file1 != NULL) {
  1823. fclose(file1);
  1824. }
  1825. ExpectNull(sk = wolfSSL_CertManagerGetCerts(NULL));
  1826. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1827. ExpectNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1828. ExpectNotNull(der = wolfSSL_X509_get_der(cert1, &derSz));
  1829. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  1830. /* Check that ASN_SELF_SIGNED_E is returned for a self-signed cert for QT
  1831. * and full OpenSSL compatibility */
  1832. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1833. WOLFSSL_FILETYPE_ASN1), ASN_SELF_SIGNED_E);
  1834. #else
  1835. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1836. WOLFSSL_FILETYPE_ASN1), ASN_NO_SIGNER_E);
  1837. #endif
  1838. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  1839. "./certs/ca-cert.pem", NULL));
  1840. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1841. for (i = 0; EXPECT_SUCCESS() && i < sk_X509_num(sk); i++) {
  1842. ExpectNotNull(x509 = sk_X509_value(sk, i));
  1843. ExpectIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  1844. #ifdef DEBUG_WOLFSSL_VERBOSE
  1845. bio = BIO_new(wolfSSL_BIO_s_file());
  1846. if (bio != NULL) {
  1847. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  1848. X509_print(bio, x509);
  1849. BIO_free(bio);
  1850. }
  1851. #endif /* DEBUG_WOLFSSL_VERBOSE */
  1852. }
  1853. wolfSSL_X509_free(cert1);
  1854. sk_X509_pop_free(sk, NULL);
  1855. wolfSSL_CertManagerFree(cm);
  1856. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1857. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1858. defined(WOLFSSL_SIGNER_DER_CERT) */
  1859. return EXPECT_RESULT();
  1860. }
  1861. static int test_wolfSSL_CertManagerSetVerify(void)
  1862. {
  1863. EXPECT_DECLS;
  1864. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1865. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1866. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  1867. WOLFSSL_CERT_MANAGER* cm = NULL;
  1868. int tmp = myVerifyAction;
  1869. const char* ca_cert = "./certs/ca-cert.pem";
  1870. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  1871. wolfSSL_CertManagerSetVerify(NULL, NULL);
  1872. wolfSSL_CertManagerSetVerify(NULL, myVerify);
  1873. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1874. wolfSSL_CertManagerSetVerify(cm, myVerify);
  1875. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1876. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL), -1);
  1877. #else
  1878. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL),
  1879. WOLFSSL_SUCCESS);
  1880. #endif
  1881. /* Use the test CB that always accepts certs */
  1882. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  1883. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, expiredCert,
  1884. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1885. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  1886. {
  1887. const char* verifyCert = "./certs/server-cert.der";
  1888. /* Use the test CB that always fails certs */
  1889. myVerifyAction = VERIFY_FORCE_FAIL;
  1890. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, verifyCert,
  1891. WOLFSSL_FILETYPE_ASN1), VERIFY_CERT_ERROR);
  1892. }
  1893. #endif
  1894. wolfSSL_CertManagerFree(cm);
  1895. myVerifyAction = tmp;
  1896. #endif
  1897. return EXPECT_RESULT();
  1898. }
  1899. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  1900. defined(DEBUG_UNIT_TEST_CERTS)
  1901. /* Used when debugging name constraint tests. Not static to allow use in
  1902. * multiple locations with complex define guards. */
  1903. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  1904. {
  1905. BIO* out = BIO_new_file(fileName, "wb");
  1906. if (out != NULL) {
  1907. PEM_write_bio_X509(out, x509);
  1908. BIO_free(out);
  1909. }
  1910. }
  1911. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  1912. {
  1913. BIO* out = BIO_new_file(fileName, "wb");
  1914. if (out != NULL) {
  1915. BIO_write(out, der, derSz);
  1916. BIO_free(out);
  1917. }
  1918. }
  1919. #else
  1920. #define DEBUG_WRITE_CERT_X509(x509, fileName) WC_DO_NOTHING
  1921. #define DEBUG_WRITE_DER(der, derSz, fileName) WC_DO_NOTHING
  1922. #endif
  1923. static int test_wolfSSL_CertManagerNameConstraint(void)
  1924. {
  1925. EXPECT_DECLS;
  1926. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1927. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1928. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1929. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1930. !defined(NO_SHA256)
  1931. WOLFSSL_CERT_MANAGER* cm = NULL;
  1932. WOLFSSL_EVP_PKEY *priv = NULL;
  1933. WOLFSSL_X509_NAME* name = NULL;
  1934. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  1935. const char* server_cert = "./certs/test/server-goodcn.pem";
  1936. int i = 0;
  1937. static const byte extNameConsOid[] = {85, 29, 30};
  1938. RsaKey key;
  1939. WC_RNG rng;
  1940. byte *der = NULL;
  1941. int derSz = 0;
  1942. word32 idx = 0;
  1943. byte *pt;
  1944. WOLFSSL_X509 *x509 = NULL;
  1945. WOLFSSL_X509 *ca = NULL;
  1946. wc_InitRng(&rng);
  1947. /* load in CA private key for signing */
  1948. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
  1949. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  1950. sizeof_server_key_der_2048), 0);
  1951. /* get ca certificate then alter it */
  1952. ExpectNotNull(der =
  1953. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  1954. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  1955. WOLFSSL_FILETYPE_ASN1));
  1956. ExpectNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  1957. if (EXPECT_SUCCESS() && (der != NULL)) {
  1958. XMEMCPY(der, pt, derSz);
  1959. /* find the name constraint extension and alter it */
  1960. pt = der;
  1961. for (i = 0; i < derSz - 3; i++) {
  1962. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  1963. pt += 3;
  1964. break;
  1965. }
  1966. pt++;
  1967. }
  1968. ExpectIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  1969. /* go to the length value and set it to 0 */
  1970. while (i < derSz && *pt != 0x81) {
  1971. pt++;
  1972. i++;
  1973. }
  1974. ExpectIntNE(i, derSz); /* did not place to alter */
  1975. pt++;
  1976. *pt = 0x00;
  1977. }
  1978. /* resign the altered certificate */
  1979. ExpectIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  1980. FOURK_BUF, &key, NULL, &rng)), 0);
  1981. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1982. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1983. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  1984. wolfSSL_CertManagerFree(cm);
  1985. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1986. wolfSSL_X509_free(x509);
  1987. wc_FreeRsaKey(&key);
  1988. wc_FreeRng(&rng);
  1989. /* add email alt name to satisfy constraint */
  1990. pt = (byte*)server_key_der_2048;
  1991. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1992. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1993. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1994. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1995. WOLFSSL_FILETYPE_ASN1));
  1996. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1997. DEBUG_WRITE_DER(der, derSz, "ca.der");
  1998. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1999. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2000. /* Good cert test with proper alt email name */
  2001. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2002. WOLFSSL_FILETYPE_PEM));
  2003. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2004. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2005. name = NULL;
  2006. ExpectNotNull(name = X509_NAME_new());
  2007. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2008. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2009. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2010. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2011. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2012. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2013. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2014. X509_NAME_free(name);
  2015. name = NULL;
  2016. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2017. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2018. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2019. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2020. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2021. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2022. wolfSSL_X509_free(x509);
  2023. x509 = NULL;
  2024. /* Cert with bad alt name list */
  2025. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2026. WOLFSSL_FILETYPE_PEM));
  2027. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2028. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2029. name = NULL;
  2030. ExpectNotNull(name = X509_NAME_new());
  2031. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2032. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2033. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2034. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2035. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2036. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2037. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2038. X509_NAME_free(name);
  2039. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2040. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2041. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2042. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2043. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2044. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2045. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2046. wolfSSL_CertManagerFree(cm);
  2047. wolfSSL_X509_free(x509);
  2048. wolfSSL_X509_free(ca);
  2049. wolfSSL_EVP_PKEY_free(priv);
  2050. #endif
  2051. return EXPECT_RESULT();
  2052. }
  2053. static int test_wolfSSL_CertManagerNameConstraint2(void)
  2054. {
  2055. EXPECT_DECLS;
  2056. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2057. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2058. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2059. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  2060. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  2061. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  2062. const char* server_cert = "./certs/server-cert.pem";
  2063. WOLFSSL_CERT_MANAGER* cm = NULL;
  2064. WOLFSSL_X509 *x509 = NULL;
  2065. WOLFSSL_X509 *ca = NULL;
  2066. const unsigned char *der = NULL;
  2067. const unsigned char *pt;
  2068. WOLFSSL_EVP_PKEY *priv = NULL;
  2069. WOLFSSL_X509_NAME* name = NULL;
  2070. int derSz = 0;
  2071. /* C=US*/
  2072. char altName[] = {
  2073. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2074. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  2075. };
  2076. /* C=ID */
  2077. char altNameFail[] = {
  2078. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2079. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  2080. };
  2081. /* C=US ST=California*/
  2082. char altNameExc[] = {
  2083. 0x30, 0x22,
  2084. 0x31, 0x0B,
  2085. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  2086. 0x31, 0x13,
  2087. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  2088. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  2089. };
  2090. /* load in CA private key for signing */
  2091. pt = ca_key_der_2048;
  2092. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  2093. sizeof_ca_key_der_2048));
  2094. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2095. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2096. WOLFSSL_FILETYPE_ASN1));
  2097. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2098. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2099. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2100. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2101. WOLFSSL_FILETYPE_PEM));
  2102. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2103. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2104. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2105. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2106. #else
  2107. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2108. #endif
  2109. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2110. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2111. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2112. /* add in matching DIR alt name and resign */
  2113. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2114. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2115. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2116. #else
  2117. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2118. #endif
  2119. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2120. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2121. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2122. wolfSSL_X509_free(x509);
  2123. x509 = NULL;
  2124. /* check verify fail */
  2125. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2126. WOLFSSL_FILETYPE_PEM));
  2127. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2128. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2129. /* add in miss matching DIR alt name and resign */
  2130. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2131. ASN_DIR_TYPE);
  2132. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2133. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2134. #else
  2135. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2136. #endif
  2137. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2138. #ifndef WOLFSSL_NO_ASN_STRICT
  2139. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2140. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2141. #else
  2142. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2143. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2144. #endif
  2145. /* check that it still fails if one bad altname and one good altname is in
  2146. * the certificate */
  2147. wolfSSL_X509_free(x509);
  2148. x509 = NULL;
  2149. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2150. WOLFSSL_FILETYPE_PEM));
  2151. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2152. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2153. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2154. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2155. ASN_DIR_TYPE);
  2156. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2157. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2158. #else
  2159. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2160. #endif
  2161. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2162. #ifndef WOLFSSL_NO_ASN_STRICT
  2163. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2164. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2165. #else
  2166. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2167. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2168. #endif
  2169. /* check it fails with switching position of bad altname */
  2170. wolfSSL_X509_free(x509);
  2171. x509 = NULL;
  2172. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2173. WOLFSSL_FILETYPE_PEM));
  2174. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2175. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2176. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2177. ASN_DIR_TYPE);
  2178. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2179. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2180. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2181. #else
  2182. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2183. #endif
  2184. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2185. #ifndef WOLFSSL_NO_ASN_STRICT
  2186. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2187. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2188. #else
  2189. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2190. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2191. #endif
  2192. wolfSSL_CertManagerFree(cm);
  2193. wolfSSL_X509_free(x509);
  2194. x509 = NULL;
  2195. wolfSSL_X509_free(ca);
  2196. ca = NULL;
  2197. /* now test with excluded name constraint */
  2198. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2199. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  2200. WOLFSSL_FILETYPE_ASN1));
  2201. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2202. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2203. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2204. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2205. WOLFSSL_FILETYPE_PEM));
  2206. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  2207. ASN_DIR_TYPE);
  2208. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2209. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2210. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2211. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2212. #else
  2213. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2214. #endif
  2215. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2216. #ifndef WOLFSSL_NO_ASN_STRICT
  2217. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2218. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2219. #else
  2220. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2221. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2222. #endif
  2223. wolfSSL_CertManagerFree(cm);
  2224. wolfSSL_X509_free(x509);
  2225. wolfSSL_X509_free(ca);
  2226. wolfSSL_EVP_PKEY_free(priv);
  2227. #endif
  2228. return EXPECT_RESULT();
  2229. }
  2230. static int test_wolfSSL_CertManagerNameConstraint3(void)
  2231. {
  2232. EXPECT_DECLS;
  2233. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2234. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2235. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2236. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2237. !defined(NO_SHA256)
  2238. WOLFSSL_CERT_MANAGER* cm = NULL;
  2239. WOLFSSL_EVP_PKEY *priv = NULL;
  2240. WOLFSSL_X509_NAME* name = NULL;
  2241. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  2242. const char* server_cert = "./certs/test/server-goodcn.pem";
  2243. byte *der = NULL;
  2244. int derSz = 0;
  2245. byte *pt;
  2246. WOLFSSL_X509 *x509 = NULL;
  2247. WOLFSSL_X509 *ca = NULL;
  2248. pt = (byte*)server_key_der_2048;
  2249. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2250. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2251. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2252. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2253. WOLFSSL_FILETYPE_ASN1));
  2254. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2255. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2256. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2257. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2258. /* check satisfying .wolfssl.com constraint passes */
  2259. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2260. WOLFSSL_FILETYPE_PEM));
  2261. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2262. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2263. name = NULL;
  2264. ExpectNotNull(name = X509_NAME_new());
  2265. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2266. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2267. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2268. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2269. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2270. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2271. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2272. X509_NAME_free(name);
  2273. name = NULL;
  2274. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2275. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2276. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2277. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2278. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2279. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2280. wolfSSL_X509_free(x509);
  2281. x509 = NULL;
  2282. /* check satisfying .random.com constraint passes */
  2283. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2284. WOLFSSL_FILETYPE_PEM));
  2285. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2286. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2287. name = NULL;
  2288. ExpectNotNull(name = X509_NAME_new());
  2289. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2290. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2291. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2292. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2293. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2294. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  2295. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2296. X509_NAME_free(name);
  2297. name = NULL;
  2298. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  2299. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2300. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2301. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2302. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2303. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2304. wolfSSL_X509_free(x509);
  2305. x509 = NULL;
  2306. /* check fail case when neither constraint is matched */
  2307. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2308. WOLFSSL_FILETYPE_PEM));
  2309. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2310. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2311. name = NULL;
  2312. ExpectNotNull(name = X509_NAME_new());
  2313. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2314. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2315. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2316. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2317. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2318. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  2319. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2320. X509_NAME_free(name);
  2321. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2322. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2323. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2324. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2325. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2326. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2327. wolfSSL_CertManagerFree(cm);
  2328. wolfSSL_X509_free(x509);
  2329. wolfSSL_X509_free(ca);
  2330. wolfSSL_EVP_PKEY_free(priv);
  2331. #endif
  2332. return EXPECT_RESULT();
  2333. }
  2334. static int test_wolfSSL_CertManagerNameConstraint4(void)
  2335. {
  2336. EXPECT_DECLS;
  2337. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2338. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2339. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2340. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2341. !defined(NO_SHA256)
  2342. WOLFSSL_CERT_MANAGER* cm = NULL;
  2343. WOLFSSL_EVP_PKEY *priv = NULL;
  2344. WOLFSSL_X509_NAME* name = NULL;
  2345. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  2346. const char* server_cert = "./certs/test/server-goodcn.pem";
  2347. byte *der = NULL;
  2348. int derSz;
  2349. byte *pt;
  2350. WOLFSSL_X509 *x509 = NULL;
  2351. WOLFSSL_X509 *ca = NULL;
  2352. pt = (byte*)server_key_der_2048;
  2353. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2354. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2355. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2356. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2357. WOLFSSL_FILETYPE_ASN1));
  2358. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2359. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2360. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2361. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2362. /* check satisfying wolfssl.com constraint passes */
  2363. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2364. WOLFSSL_FILETYPE_PEM));
  2365. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2366. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2367. name = NULL;
  2368. ExpectNotNull(name = X509_NAME_new());
  2369. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2370. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2371. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2372. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2373. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2374. X509_NAME_free(name);
  2375. name = NULL;
  2376. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2377. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2378. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2379. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2380. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2381. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2382. wolfSSL_X509_free(x509);
  2383. x509 = NULL;
  2384. /* check satisfying example.com constraint passes */
  2385. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2386. WOLFSSL_FILETYPE_PEM));
  2387. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2388. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2389. name = NULL;
  2390. ExpectNotNull(name = X509_NAME_new());
  2391. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2392. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2393. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2394. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  2395. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2396. X509_NAME_free(name);
  2397. name = NULL;
  2398. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  2399. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2400. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2401. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2402. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2403. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2404. wolfSSL_X509_free(x509);
  2405. x509 = NULL;
  2406. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  2407. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2408. WOLFSSL_FILETYPE_PEM));
  2409. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2410. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2411. name = NULL;
  2412. ExpectNotNull(name = X509_NAME_new());
  2413. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2414. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2415. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2416. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2417. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2418. X509_NAME_free(name);
  2419. name = NULL;
  2420. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2421. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2422. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  2423. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2424. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
  2425. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2426. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2427. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2428. wolfSSL_X509_free(x509);
  2429. x509 = NULL;
  2430. /* check fail when one DNS in the list is bad */
  2431. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2432. WOLFSSL_FILETYPE_PEM));
  2433. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2434. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2435. name = NULL;
  2436. ExpectNotNull(name = X509_NAME_new());
  2437. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2438. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2439. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2440. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2441. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2442. X509_NAME_free(name);
  2443. name = NULL;
  2444. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2445. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  2446. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2447. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2448. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  2449. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2450. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2451. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2452. wolfSSL_X509_free(x509);
  2453. x509 = NULL;
  2454. /* check fail case when neither constraint is matched */
  2455. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2456. WOLFSSL_FILETYPE_PEM));
  2457. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2458. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2459. name = NULL;
  2460. ExpectNotNull(name = X509_NAME_new());
  2461. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2462. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2463. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2464. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  2465. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2466. X509_NAME_free(name);
  2467. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  2468. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2469. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2470. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2471. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2472. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2473. wolfSSL_CertManagerFree(cm);
  2474. wolfSSL_X509_free(x509);
  2475. wolfSSL_X509_free(ca);
  2476. wolfSSL_EVP_PKEY_free(priv);
  2477. #endif
  2478. return EXPECT_RESULT();
  2479. }
  2480. static int test_wolfSSL_CertManagerNameConstraint5(void)
  2481. {
  2482. EXPECT_DECLS;
  2483. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2484. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2485. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2486. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2487. !defined(NO_SHA256)
  2488. WOLFSSL_CERT_MANAGER* cm = NULL;
  2489. WOLFSSL_EVP_PKEY *priv = NULL;
  2490. WOLFSSL_X509_NAME* name = NULL;
  2491. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  2492. const char* server_cert = "./certs/test/server-goodcn.pem";
  2493. byte *der = NULL;
  2494. int derSz;
  2495. byte *pt;
  2496. WOLFSSL_X509 *x509 = NULL;
  2497. WOLFSSL_X509 *ca = NULL;
  2498. pt = (byte*)server_key_der_2048;
  2499. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2500. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2501. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2502. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2503. WOLFSSL_FILETYPE_ASN1));
  2504. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2505. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2506. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2507. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2508. /* check satisfying wolfssl.com constraint passes */
  2509. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2510. WOLFSSL_FILETYPE_PEM));
  2511. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2512. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2513. name = NULL;
  2514. ExpectNotNull(name = X509_NAME_new());
  2515. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2516. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2517. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2518. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  2519. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2520. X509_NAME_free(name);
  2521. name = NULL;
  2522. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  2523. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  2524. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2525. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2526. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2527. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2528. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2529. wolfSSL_X509_free(x509);
  2530. x509 = NULL;
  2531. /* fail with DNS check because of common name */
  2532. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2533. WOLFSSL_FILETYPE_PEM));
  2534. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2535. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2536. name = NULL;
  2537. ExpectNotNull(name = X509_NAME_new());
  2538. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2539. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2540. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2541. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2542. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2543. X509_NAME_free(name);
  2544. name = NULL;
  2545. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2546. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  2547. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2548. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  2549. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2550. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2551. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2552. wolfSSL_X509_free(x509);
  2553. x509 = NULL;
  2554. /* fail on permitted DNS name constraint */
  2555. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2556. WOLFSSL_FILETYPE_PEM));
  2557. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2558. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2559. name = NULL;
  2560. ExpectNotNull(name = X509_NAME_new());
  2561. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2562. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2563. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2564. X509_NAME_free(name);
  2565. name = NULL;
  2566. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  2567. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  2568. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2569. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2570. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  2571. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2572. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2573. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2574. wolfSSL_X509_free(x509);
  2575. x509 = NULL;
  2576. /* fail on permitted email name constraint */
  2577. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2578. WOLFSSL_FILETYPE_PEM));
  2579. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2580. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2581. name = NULL;
  2582. ExpectNotNull(name = X509_NAME_new());
  2583. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2584. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2585. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2586. X509_NAME_free(name);
  2587. name = NULL;
  2588. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2589. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2590. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  2591. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2592. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  2593. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2594. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2595. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2596. wolfSSL_X509_free(x509);
  2597. x509 = NULL;
  2598. /* success with empty email name */
  2599. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2600. WOLFSSL_FILETYPE_PEM));
  2601. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2602. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2603. name = NULL;
  2604. ExpectNotNull(name = X509_NAME_new());
  2605. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2606. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2607. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2608. X509_NAME_free(name);
  2609. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2610. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2611. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  2612. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2613. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2614. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2615. wolfSSL_X509_free(x509);
  2616. wolfSSL_CertManagerFree(cm);
  2617. wolfSSL_X509_free(ca);
  2618. wolfSSL_EVP_PKEY_free(priv);
  2619. #endif
  2620. return EXPECT_RESULT();
  2621. }
  2622. static int test_wolfSSL_CertManagerCRL(void)
  2623. {
  2624. EXPECT_DECLS;
  2625. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  2626. !defined(NO_RSA)
  2627. const char* ca_cert = "./certs/ca-cert.pem";
  2628. const char* crl1 = "./certs/crl/crl.pem";
  2629. const char* crl2 = "./certs/crl/crl2.pem";
  2630. const unsigned char crl_buff[] = {
  2631. 0x30, 0x82, 0x02, 0x04, 0x30, 0x81, 0xed, 0x02,
  2632. 0x01, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  2633. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  2634. 0x00, 0x30, 0x81, 0x94, 0x31, 0x0b, 0x30, 0x09,
  2635. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  2636. 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  2637. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74,
  2638. 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06,
  2639. 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  2640. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x11, 0x30,
  2641. 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x08,
  2642. 0x53, 0x61, 0x77, 0x74, 0x6f, 0x6f, 0x74, 0x68,
  2643. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04,
  2644. 0x0b, 0x0c, 0x0a, 0x43, 0x6f, 0x6e, 0x73, 0x75,
  2645. 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  2646. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  2647. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66,
  2648. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x31,
  2649. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  2650. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10,
  2651. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  2652. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  2653. 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, 0x36,
  2654. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x17,
  2655. 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, 0x32,
  2656. 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, 0x14,
  2657. 0x30, 0x12, 0x02, 0x01, 0x02, 0x17, 0x0d, 0x32,
  2658. 0x32, 0x31, 0x32, 0x31, 0x36, 0x32, 0x31, 0x31,
  2659. 0x37, 0x35, 0x30, 0x5a, 0xa0, 0x0e, 0x30, 0x0c,
  2660. 0x30, 0x0a, 0x06, 0x03, 0x55, 0x1d, 0x14, 0x04,
  2661. 0x03, 0x02, 0x01, 0x02, 0x30, 0x0d, 0x06, 0x09,
  2662. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  2663. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00,
  2664. 0x39, 0x44, 0xff, 0x39, 0xf4, 0x04, 0x45, 0x79,
  2665. 0x7e, 0x73, 0xe2, 0x42, 0x48, 0xdb, 0x85, 0x66,
  2666. 0xfd, 0x99, 0x76, 0x94, 0x7c, 0xb5, 0x79, 0x5d,
  2667. 0x15, 0x71, 0x36, 0xa9, 0x87, 0xf0, 0x73, 0x05,
  2668. 0x50, 0x08, 0x6b, 0x1c, 0x6e, 0xde, 0x96, 0x45,
  2669. 0x31, 0xc3, 0xc0, 0xba, 0xba, 0xf5, 0x08, 0x1d,
  2670. 0x05, 0x4a, 0x52, 0x39, 0xe9, 0x03, 0xef, 0x59,
  2671. 0xc8, 0x1d, 0x4a, 0xf2, 0x86, 0x05, 0x99, 0x7b,
  2672. 0x4b, 0x74, 0xf6, 0xd3, 0x75, 0x8d, 0xb2, 0x57,
  2673. 0xba, 0xac, 0xa7, 0x11, 0x14, 0xd6, 0x6c, 0x71,
  2674. 0xc4, 0x4c, 0x1c, 0x68, 0xbc, 0x49, 0x78, 0xf0,
  2675. 0xc9, 0x52, 0x8a, 0xe7, 0x8b, 0x54, 0xe6, 0x20,
  2676. 0x58, 0x20, 0x60, 0x66, 0xf5, 0x14, 0xd8, 0xcb,
  2677. 0xff, 0xe0, 0xa0, 0x45, 0xbc, 0xb4, 0x81, 0xad,
  2678. 0x1d, 0xbc, 0xcf, 0xf8, 0x8e, 0xa8, 0x87, 0x24,
  2679. 0x55, 0x99, 0xd9, 0xce, 0x47, 0xf7, 0x5b, 0x4a,
  2680. 0x33, 0x6d, 0xdb, 0xbf, 0x93, 0x64, 0x1a, 0xa6,
  2681. 0x46, 0x5f, 0x27, 0xdc, 0xd8, 0xd4, 0xf9, 0xc2,
  2682. 0x42, 0x2a, 0x7e, 0xb2, 0x7c, 0xdd, 0x98, 0x77,
  2683. 0xf5, 0x88, 0x7d, 0x15, 0x25, 0x08, 0xbc, 0xe0,
  2684. 0xd0, 0x8d, 0xf4, 0xc3, 0xc3, 0x04, 0x41, 0xa4,
  2685. 0xd1, 0xb1, 0x39, 0x4a, 0x6b, 0x2c, 0xb5, 0x2e,
  2686. 0x9a, 0x65, 0x43, 0x0d, 0x0e, 0x73, 0xf4, 0x06,
  2687. 0xe1, 0xb3, 0x49, 0x34, 0x94, 0xb0, 0xb7, 0xff,
  2688. 0xc0, 0x27, 0xc1, 0xb5, 0xea, 0x06, 0xf7, 0x71,
  2689. 0x71, 0x97, 0xbb, 0xbc, 0xc7, 0x1a, 0x9f, 0xeb,
  2690. 0xf6, 0x3d, 0xa5, 0x7b, 0x55, 0xa7, 0xbf, 0xdd,
  2691. 0xd7, 0xee, 0x97, 0xb8, 0x9d, 0xdc, 0xcd, 0xe3,
  2692. 0x06, 0xdb, 0x9a, 0x2c, 0x60, 0xbf, 0x70, 0x84,
  2693. 0xfa, 0x6b, 0x8d, 0x70, 0x7d, 0xde, 0xe8, 0xb7,
  2694. 0xab, 0xb0, 0x38, 0x68, 0x6c, 0xc0, 0xb1, 0xe1,
  2695. 0xba, 0x45, 0xe0, 0xd7, 0x12, 0x3d, 0x71, 0x5b
  2696. };
  2697. WOLFSSL_CERT_MANAGER* cm = NULL;
  2698. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2699. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(NULL, 0), BAD_FUNC_ARG);
  2700. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  2701. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECK), 1);
  2702. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm,
  2703. WOLFSSL_CRL_CHECK | WOLFSSL_CRL_CHECKALL), 1);
  2704. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 16), 1);
  2705. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  2706. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, -1), BAD_FUNC_ARG);
  2707. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, -1), BAD_FUNC_ARG);
  2708. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, -1),
  2709. BAD_FUNC_ARG);
  2710. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, 1), BAD_FUNC_ARG);
  2711. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, 1),
  2712. BAD_FUNC_ARG);
  2713. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, 1), BAD_FUNC_ARG);
  2714. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048, -1),
  2715. BAD_FUNC_ARG);
  2716. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  2717. sizeof_server_cert_der_2048), ASN_NO_SIGNER_E);
  2718. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(NULL, NULL), BAD_FUNC_ARG);
  2719. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(cm, NULL), 1);
  2720. #ifdef HAVE_CRL_IO
  2721. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(NULL, NULL), BAD_FUNC_ARG);
  2722. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  2723. #endif
  2724. #ifndef NO_FILESYSTEM
  2725. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(NULL, NULL, WOLFSSL_FILETYPE_ASN1,
  2726. 0), BAD_FUNC_ARG);
  2727. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, NULL, WOLFSSL_FILETYPE_ASN1,
  2728. 0), BAD_FUNC_ARG);
  2729. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  2730. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, "./certs/crl", -1, 0), 1);
  2731. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(NULL, NULL,
  2732. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2733. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  2734. BAD_FUNC_ARG);
  2735. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  2736. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, "./certs/crl/crl.pem", -1),
  2737. ASN_PARSE_E);
  2738. #endif
  2739. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, -1,
  2740. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2741. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, -1,
  2742. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2743. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, -1,
  2744. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2745. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, 1,
  2746. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2747. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, 1,
  2748. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2749. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, 1,
  2750. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2751. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, -1,
  2752. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2753. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(NULL), BAD_FUNC_ARG);
  2754. DoExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  2755. ExpectIntEQ(WOLFSSL_SUCCESS,
  2756. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2757. ExpectIntEQ(WOLFSSL_SUCCESS,
  2758. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2759. ExpectIntEQ(WOLFSSL_SUCCESS,
  2760. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  2761. wolfSSL_CertManagerFreeCRL(cm);
  2762. ExpectIntEQ(WOLFSSL_SUCCESS,
  2763. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2764. ExpectIntEQ(WOLFSSL_SUCCESS,
  2765. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2766. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  2767. sizeof_server_cert_der_2048), CRL_MISSING);
  2768. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server_cert_der_2048,
  2769. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1), CRL_MISSING);
  2770. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, sizeof(crl_buff),
  2771. WOLFSSL_FILETYPE_ASN1), 1);
  2772. wolfSSL_CertManagerFree(cm);
  2773. #endif
  2774. return EXPECT_RESULT();
  2775. }
  2776. static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
  2777. {
  2778. EXPECT_DECLS;
  2779. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA)
  2780. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  2781. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  2782. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  2783. defined(HAVE_LIGHTY)
  2784. WOLFSSL_CERT_MANAGER* cm = NULL;
  2785. /* Raw OCSP response bytes captured using the following setup:
  2786. * - Run responder with
  2787. * openssl ocsp -port 9999 -ndays 9999
  2788. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  2789. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  2790. * -rkey certs/ocsp/ocsp-responder-key.pem
  2791. * -CA certs/ocsp/intermediate1-ca-cert.pem
  2792. * - Run client with
  2793. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  2794. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  2795. * -cert certs/ocsp/server1-cert.pem
  2796. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  2797. * - Select the response packet in Wireshark, and export it using
  2798. * "File->Export Packet Dissection->As "C" Arrays". Select "Selected
  2799. * packets only". After importing into the editor, remove the initial
  2800. * ~148 bytes of header, ending with the Content-Length and the \r\n\r\n.
  2801. */
  2802. static const byte response[] = {
  2803. 0x30, 0x82, 0x07, 0x40, /* ....0..@ */
  2804. 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, /* ......90 */
  2805. 0x82, 0x07, 0x35, 0x06, 0x09, 0x2b, 0x06, 0x01, /* ..5..+.. */
  2806. 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, /* ...0.... */
  2807. 0x07, 0x26, 0x30, 0x82, 0x07, 0x22, 0x30, 0x82, /* .&0.."0. */
  2808. 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, /* .@...0.. */
  2809. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  2810. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  2811. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  2812. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  2813. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  2814. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  2815. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  2816. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  2817. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  2818. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  2819. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  2820. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, /* ring1.0. */
  2821. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, /* ..U....w */
  2822. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, /* olfSSL O */
  2823. 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, /* CSP Resp */
  2824. 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, /* onder1.0 */
  2825. 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, /* ...*.H.. */
  2826. 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, /* ......in */
  2827. 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, /* fo@wolfs */
  2828. 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, /* sl.com.. */
  2829. 0x32, 0x30, 0x32, 0x33, 0x31, 0x31, 0x30, 0x38, /* 20231108 */
  2830. 0x30, 0x30, 0x32, 0x36, 0x33, 0x37, 0x5a, 0x30, /* 002637Z0 */
  2831. 0x64, 0x30, 0x62, 0x30, 0x3a, 0x30, 0x09, 0x06, /* d0b0:0.. */
  2832. 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, /* .+...... */
  2833. 0x04, 0x14, 0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, /* ..qM.#@Y */
  2834. 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, /* ...7C.1. */
  2835. 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04, 0x04, 0x14, /* ..C..... */
  2836. 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, /* ..:.,... */
  2837. 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, /* ..L.*.q. */
  2838. 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, /* dD...... */
  2839. 0x00, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x33, 0x31, /* ...20231 */
  2840. 0x31, 0x30, 0x38, 0x30, 0x30, 0x32, 0x36, 0x33, /* 10800263 */
  2841. 0x37, 0x5a, 0xa0, 0x11, 0x18, 0x0f, 0x32, 0x30, /* 7Z....20 */
  2842. 0x35, 0x31, 0x30, 0x33, 0x32, 0x35, 0x30, 0x30, /* 51032500 */
  2843. 0x32, 0x36, 0x33, 0x37, 0x5a, 0xa1, 0x23, 0x30, /* 2637Z.#0 */
  2844. 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, /* !0...+.. */
  2845. 0x05, 0x05, 0x07, 0x30, 0x01, 0x02, 0x04, 0x12, /* ...0.... */
  2846. 0x04, 0x10, 0xdb, 0xbc, 0x2a, 0x76, 0xa0, 0xb4, /* ....*v.. */
  2847. 0x1e, 0x5d, 0xf6, 0x2b, 0x8e, 0x38, 0x62, 0xdb, /* .].+.8b. */
  2848. 0x90, 0xed, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, /* ..0...*. */
  2849. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, /* H....... */
  2850. 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x87, 0xde, /* ........ */
  2851. 0xfb, 0xf9, 0x3a, 0x90, 0x1f, 0x90, 0xde, 0xcf, /* ..:..... */
  2852. 0xfe, 0xad, 0x64, 0x19, 0x34, 0x17, 0xf8, 0x15, /* ..d.4... */
  2853. 0x01, 0x22, 0x5f, 0x67, 0x41, 0xa4, 0x18, 0xf7, /* ."_gA... */
  2854. 0x16, 0xb7, 0xc9, 0xf3, 0xe1, 0x9f, 0xcd, 0x40, /* .......@ */
  2855. 0x56, 0x77, 0x6e, 0x6a, 0xfb, 0x92, 0x6a, 0x6f, /* Vwnj..jo */
  2856. 0x28, 0x3e, 0x22, 0x48, 0xa1, 0xc2, 0xd8, 0x1d, /* (>"H.... */
  2857. 0xc7, 0xe6, 0x78, 0x7f, 0xb6, 0x09, 0xfe, 0x2c, /* ..x...., */
  2858. 0xb5, 0xef, 0x29, 0x7c, 0xc5, 0x51, 0x16, 0x7b, /* ..)|.Q.{ */
  2859. 0x8f, 0xfb, 0x44, 0xa8, 0xcd, 0xf5, 0x5c, 0x0f, /* ..D...\. */
  2860. 0x46, 0x0e, 0xb1, 0xa4, 0xeb, 0x5b, 0xf5, 0x86, /* F....[.. */
  2861. 0x11, 0x0f, 0xcd, 0xe2, 0xe5, 0x3c, 0x91, 0x72, /* .....<.r */
  2862. 0x0d, 0x6a, 0xcb, 0x95, 0x99, 0x39, 0x91, 0x48, /* .j...9.H */
  2863. 0x65, 0x97, 0xb9, 0x78, 0xb5, 0x88, 0x7f, 0x76, /* e..x...v */
  2864. 0xa1, 0x43, 0x2f, 0xf6, 0x1f, 0x49, 0xb7, 0x08, /* .C/..I.. */
  2865. 0x36, 0xe4, 0x2e, 0x34, 0x25, 0xda, 0x16, 0x74, /* 6..4%..t */
  2866. 0x47, 0x62, 0x56, 0xff, 0x2f, 0x02, 0x03, 0x44, /* GbV./..D */
  2867. 0x89, 0x04, 0xe7, 0xb8, 0xde, 0x0a, 0x35, 0x43, /* ......5C */
  2868. 0xae, 0xd7, 0x54, 0xbe, 0xc3, 0x7c, 0x95, 0xa5, /* ..T..|.. */
  2869. 0xc8, 0xe0, 0x2e, 0x52, 0xb6, 0xea, 0x99, 0x45, /* ...R...E */
  2870. 0xfd, 0xda, 0x4b, 0xd5, 0x79, 0x07, 0x64, 0xca, /* ..K.y.d. */
  2871. 0x64, 0xba, 0x52, 0x12, 0x62, 0x8c, 0x08, 0x9a, /* d.R.b... */
  2872. 0x32, 0xeb, 0x85, 0x65, 0x05, 0x39, 0x07, 0x5d, /* 2..e.9.] */
  2873. 0x39, 0x4a, 0xcf, 0xa5, 0x30, 0xf6, 0xd1, 0xf7, /* 9J..0... */
  2874. 0x29, 0xaa, 0x23, 0x42, 0xc6, 0x85, 0x16, 0x7f, /* ).#B.... */
  2875. 0x64, 0x16, 0xb1, 0xb0, 0x5d, 0xcd, 0x88, 0x2d, /* d...]..- */
  2876. 0x06, 0xb0, 0xa9, 0xdf, 0xa3, 0x9f, 0x25, 0x41, /* ......%A */
  2877. 0x89, 0x9a, 0x19, 0xe1, 0xaa, 0xcd, 0xdf, 0x51, /* .......Q */
  2878. 0xcb, 0xa9, 0xc3, 0x7e, 0x27, 0xbc, 0x7d, 0x9b, /* ...~'.}. */
  2879. 0x6f, 0x4d, 0x79, 0x87, 0x09, 0x3f, 0xac, 0xd2, /* oMy..?.. */
  2880. 0x4a, 0x3b, 0xbe, 0xf8, 0x7a, 0xa4, 0x93, 0x45, /* J;..z..E */
  2881. 0x11, 0x64, 0x40, 0xc5, 0x03, 0xc9, 0x24, 0x5b, /* .d@...$[ */
  2882. 0xe9, 0x6d, 0xfc, 0x94, 0x08, 0xbe, 0xa0, 0x82, /* .m...... */
  2883. 0x04, 0xc6, 0x30, 0x82, 0x04, 0xc2, 0x30, 0x82, /* ..0...0. */
  2884. 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, /* ..0..... */
  2885. 0x02, 0x01, 0x02, 0x02, 0x01, 0x04, 0x30, 0x0d, /* ......0. */
  2886. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, /* ..*.H... */
  2887. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x81, 0x97, /* .....0.. */
  2888. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  2889. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  2890. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  2891. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  2892. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  2893. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  2894. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  2895. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  2896. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  2897. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  2898. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  2899. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30, 0x16, /* ring1.0. */
  2900. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, /* ..U....w */
  2901. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x72, /* olfSSL r */
  2902. 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, /* oot CA1. */
  2903. 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, /* 0...*.H. */
  2904. 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, /* .......i */
  2905. 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, /* nfo@wolf */
  2906. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, /* ssl.com0 */
  2907. 0x1e, 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, /* ...22121 */
  2908. 0x36, 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, /* 6211750Z */
  2909. 0x17, 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, /* ..250911 */
  2910. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, /* 211750Z0 */
  2911. 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, /* ..1.0... */
  2912. 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, /* U....US1 */
  2913. 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, /* .0...U.. */
  2914. 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, /* ..Washin */
  2915. 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, /* gton1.0. */
  2916. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, /* ..U....S */
  2917. 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, /* eattle1. */
  2918. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, /* 0...U... */
  2919. 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  2920. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  2921. 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, /* ...Engin */
  2922. 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, /* eering1. */
  2923. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, /* 0...U... */
  2924. 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  2925. 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, /* OCSP Re */
  2926. 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, /* sponder1 */
  2927. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, /* .0...*.H */
  2928. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, /* ........ */
  2929. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, /* info@wol */
  2930. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, /* fssl.com */
  2931. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, /* 0.."0... */
  2932. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  2933. 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, /* ........ */
  2934. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, /* 0....... */
  2935. 0x00, 0xb8, 0xba, 0x23, 0xb4, 0xf6, 0xc3, 0x7b, /* ...#...{ */
  2936. 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, /* .....a.. */
  2937. 0x1e, 0x63, 0xb9, 0x85, 0x23, 0x34, 0x50, 0x6d, /* .c..#4Pm */
  2938. 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, /* .|.....u */
  2939. 0x5c, 0x2d, 0xf7, 0x63, 0x88, 0xd1, 0x07, 0x7a, /* \-.c...z */
  2940. 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, /* ..E5+... */
  2941. 0x22, 0xb4, 0x94, 0x41, 0x38, 0xe2, 0x9d, 0x74, /* "..A8..t */
  2942. 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, /* ..0".Q.. */
  2943. 0xca, 0x3f, 0x46, 0x2b, 0xfe, 0xe5, 0x5a, 0x3f, /* .?F+..Z? */
  2944. 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, /* Atgu.... */
  2945. 0xc3, 0xee, 0x42, 0xf8, 0x8d, 0xeb, 0x92, 0x95, /* ..B..... */
  2946. 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, /* ..e.C... */
  2947. 0x16, 0x80, 0x90, 0xce, 0x24, 0x35, 0x21, 0xc4, /* ....$5!. */
  2948. 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, /* U.ZQ..-. */
  2949. 0x0a, 0x5a, 0x4f, 0x4a, 0x73, 0x31, 0x50, 0xee, /* .ZOJs1P. */
  2950. 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, /* J..9...H */
  2951. 0x87, 0xb1, 0x99, 0xe2, 0x10, 0xa7, 0x06, 0x72, /* .......r */
  2952. 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, /* g.\..... */
  2953. 0x76, 0xf8, 0xe0, 0x4a, 0xec, 0xbc, 0x93, 0xf4, /* v..J.... */
  2954. 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, /* fL(q..f. */
  2955. 0xb4, 0x90, 0x30, 0xbb, 0x17, 0xb0, 0xfe, 0x97, /* ..0..... */
  2956. 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, /* ....]... */
  2957. 0x19, 0x12, 0x3c, 0xab, 0x82, 0x71, 0x78, 0xff, /* ..<..qx. */
  2958. 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, /* .?2..q.. */
  2959. 0x8c, 0x27, 0xac, 0x11, 0xb8, 0xd8, 0x43, 0x49, /* .'....CI */
  2960. 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, /* ..p..... */
  2961. 0x24, 0x87, 0x17, 0x3b, 0xd8, 0x04, 0x65, 0x6c, /* $..;..el */
  2962. 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, /* .vP..... */
  2963. 0x73, 0x68, 0x26, 0x14, 0x87, 0x95, 0xc3, 0x5f, /* sh&...._ */
  2964. 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, /* na...... */
  2965. 0x0a, 0x8b, 0x98, 0xf3, 0xe3, 0xff, 0x4e, 0x44, /* ......ND */
  2966. 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, /* .et|qTe. */
  2967. 0x39, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x82, /* 9....... */
  2968. 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, /* ..0...0. */
  2969. 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, /* ..U....0 */
  2970. 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, /* .0...U.. */
  2971. 0x04, 0x16, 0x04, 0x14, 0x32, 0x67, 0xe1, 0xb1, /* ....2g.. */
  2972. 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, /* y....#.p */
  2973. 0x40, 0x50, 0xb5, 0x46, 0x56, 0xb8, 0x30, 0x36, /* @P.FV.06 */
  2974. 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, /* 0....U.# */
  2975. 0x04, 0x81, 0xbc, 0x30, 0x81, 0xb9, 0x80, 0x14, /* ...0.... */
  2976. 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, /* s.../... */
  2977. 0x47, 0xa5, 0x38, 0xd7, 0xb0, 0x04, 0x82, 0x3a, /* G.8....: */
  2978. 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, /* ~r.!.... */
  2979. 0x81, 0x9a, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, /* ..0..1.0 */
  2980. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, /* ...U.... */
  2981. 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, /* US1.0... */
  2982. 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, /* U....Was */
  2983. 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, /* hington1 */
  2984. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, /* .0...U.. */
  2985. 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, /* ..Seattl */
  2986. 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, /* e1.0...U */
  2987. 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  2988. 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, /* SSL1.0.. */
  2989. 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, /* .U....En */
  2990. 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, /* gineerin */
  2991. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, /* g1.0...U */
  2992. 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  2993. 0x53, 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, /* SSL root */
  2994. 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, /* CA1.0.. */
  2995. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, /* .*.H.... */
  2996. 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, /* ....info */
  2997. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, /* @wolfssl */
  2998. 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, /* .com..c0 */
  2999. 0x13, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x0c, /* ...U.%.. */
  3000. 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, /* 0...+... */
  3001. 0x05, 0x07, 0x03, 0x09, 0x30, 0x0d, 0x06, 0x09, /* ....0... */
  3002. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  3003. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, /* ........ */
  3004. 0x2f, 0xb7, 0x6b, 0xec, 0xb7, 0x12, 0x63, 0xb9, /* /.k...c. */
  3005. 0x57, 0xdc, 0x04, 0x4d, 0x9c, 0x67, 0x74, 0x98, /* W..M.gt. */
  3006. 0x06, 0x28, 0x68, 0x37, 0x34, 0xc2, 0x50, 0xe9, /* .(h74.P. */
  3007. 0x2a, 0xd4, 0x1a, 0xb2, 0x32, 0x1a, 0x9d, 0x2b, /* *...2..+ */
  3008. 0x4f, 0x23, 0x50, 0xea, 0xb4, 0x95, 0x86, 0xc3, /* O#P..... */
  3009. 0xb9, 0x5f, 0x34, 0x3e, 0x99, 0x91, 0xa7, 0x80, /* ._4>.... */
  3010. 0x5f, 0x6e, 0x1b, 0x6e, 0xdb, 0xe9, 0x02, 0x38, /* _n.n...8 */
  3011. 0x6f, 0xdf, 0xc5, 0x9b, 0x0d, 0xa3, 0x1c, 0xa9, /* o....... */
  3012. 0x15, 0x76, 0x16, 0x66, 0xa8, 0x4e, 0xfb, 0xd3, /* .v.f.N.. */
  3013. 0x43, 0x76, 0xf1, 0x72, 0xb7, 0xd1, 0xfa, 0xee, /* Cv.r.... */
  3014. 0x39, 0xa6, 0x96, 0xc1, 0xa2, 0x93, 0xa4, 0x9b, /* 9....... */
  3015. 0x1e, 0x9f, 0xba, 0x71, 0x8f, 0xba, 0xbd, 0x67, /* ...q...g */
  3016. 0x6a, 0xf2, 0x15, 0x5f, 0xf1, 0x64, 0xe7, 0xcf, /* j.._.d.. */
  3017. 0x26, 0xb8, 0x4c, 0xc0, 0xeb, 0x85, 0x04, 0x58, /* &.L....X */
  3018. 0xd9, 0x4a, 0x6b, 0xd9, 0x86, 0xf5, 0x80, 0x21, /* .Jk....! */
  3019. 0xbf, 0x91, 0xc8, 0x4b, 0x9f, 0x04, 0xed, 0x57, /* ...K...W */
  3020. 0x7a, 0xd2, 0x58, 0xac, 0x5b, 0x47, 0xaf, 0x4d, /* z.X.[G.M */
  3021. 0x7f, 0x5b, 0x1d, 0x6d, 0x68, 0x9b, 0x84, 0x98, /* .[.mh... */
  3022. 0x2a, 0x31, 0x02, 0x2c, 0xe9, 0x1b, 0xaf, 0x11, /* *1.,.... */
  3023. 0x0b, 0x78, 0x49, 0xbe, 0x68, 0x68, 0xcb, 0x9c, /* .xI.hh.. */
  3024. 0x41, 0x56, 0xe8, 0xb5, 0x59, 0xda, 0xff, 0xca, /* AV..Y... */
  3025. 0x59, 0x99, 0x17, 0x3e, 0x11, 0x0a, 0x8f, 0x49, /* Y..>...I */
  3026. 0x24, 0x0b, 0x81, 0x42, 0x63, 0xcd, 0x4f, 0xf6, /* $..Bc.O. */
  3027. 0x2b, 0x9d, 0xd1, 0x79, 0x75, 0xd7, 0x4a, 0xcc, /* +..yu.J. */
  3028. 0x4c, 0xb7, 0x2b, 0xd7, 0xe8, 0xe7, 0xd4, 0x48, /* L.+....H */
  3029. 0x3c, 0x14, 0x3b, 0x1c, 0x28, 0xe8, 0x46, 0x7a, /* <.;.(.Fz */
  3030. 0xdc, 0x11, 0x9d, 0x7f, 0x1c, 0xab, 0x10, 0x95, /* ........ */
  3031. 0x17, 0xb2, 0xc7, 0x7a, 0xbb, 0x17, 0x44, 0x59, /* ...z..DY */
  3032. 0x69, 0x8e, 0x16, 0x05, 0x94, 0x8c, 0x88, 0xd9, /* i....... */
  3033. 0xdc, 0x9a, 0xfd, 0xf2, 0x93, 0xbe, 0x68, 0xba, /* ......h. */
  3034. 0x3c, 0xd6, 0x2b, 0x61, 0x3a, 0x8b, 0xf7, 0x66, /* <.+a:..f */
  3035. 0xcb, 0x54, 0xe8, 0xe4, 0xdb, 0x9f, 0xcc, 0x9e /* .T...... */
  3036. };
  3037. OcspEntry entry[1];
  3038. CertStatus status[1];
  3039. OcspRequest* request = NULL;
  3040. #ifndef NO_FILESYSTEM
  3041. const char* ca_cert = "./certs/ca-cert.pem";
  3042. #endif
  3043. byte serial[] = {0x05};
  3044. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  3045. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  3046. XMEMSET(entry, 0, sizeof(OcspEntry));
  3047. XMEMSET(status, 0, sizeof(CertStatus));
  3048. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3049. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3050. DYNAMIC_TYPE_OCSP_REQUEST));
  3051. if ((request != NULL) && (request->serial != NULL)) {
  3052. request->serialSz = sizeof(serial);
  3053. XMEMCPY(request->serial, serial, sizeof(serial));
  3054. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3055. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  3056. }
  3057. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3058. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3059. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm,
  3060. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  3061. /* Response should be valid. */
  3062. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3063. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3064. /* Flip a byte in the request serial number, response should be invalid
  3065. * now. */
  3066. if ((request != NULL) && (request->serial != NULL))
  3067. request->serial[0] ^= request->serial[0];
  3068. ExpectIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3069. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3070. #ifndef NO_FILESYSTEM
  3071. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3072. sizeof(server_cert_der_2048)), ASN_NO_SIGNER_E);
  3073. ExpectIntEQ(WOLFSSL_SUCCESS,
  3074. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3075. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3076. sizeof(server_cert_der_2048)), 1);
  3077. #endif
  3078. wolfSSL_OCSP_REQUEST_free(request);
  3079. wolfSSL_CertManagerFree(cm);
  3080. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  3081. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  3082. #endif /* HAVE_OCSP */
  3083. return EXPECT_RESULT();
  3084. }
  3085. static int test_wolfSSL_CheckOCSPResponse(void)
  3086. {
  3087. EXPECT_DECLS;
  3088. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA) && \
  3089. defined(OPENSSL_ALL)
  3090. const char* responseFile = "./certs/ocsp/test-response.der";
  3091. const char* responseMultiFile = "./certs/ocsp/test-multi-response.der";
  3092. const char* responseNoInternFile =
  3093. "./certs/ocsp/test-response-nointern.der";
  3094. const char* caFile = "./certs/ocsp/root-ca-cert.pem";
  3095. OcspResponse* res = NULL;
  3096. byte data[4096];
  3097. const unsigned char* pt;
  3098. int dataSz = 0; /* initialize to mitigate spurious maybe-uninitialized from
  3099. * gcc sanitizer with --enable-heapmath.
  3100. */
  3101. XFILE f = XBADFILE;
  3102. WOLFSSL_OCSP_BASICRESP* bs = NULL;
  3103. WOLFSSL_X509_STORE* st = NULL;
  3104. WOLFSSL_X509* issuer = NULL;
  3105. ExpectTrue((f = XFOPEN(responseFile, "rb")) != XBADFILE);
  3106. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3107. if (f != XBADFILE) {
  3108. XFCLOSE(f);
  3109. f = XBADFILE;
  3110. }
  3111. pt = data;
  3112. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3113. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3114. SSL_FILETYPE_PEM));
  3115. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3116. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3117. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3118. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  3119. wolfSSL_OCSP_BASICRESP_free(bs);
  3120. bs = NULL;
  3121. wolfSSL_OCSP_RESPONSE_free(res);
  3122. res = NULL;
  3123. wolfSSL_X509_STORE_free(st);
  3124. st = NULL;
  3125. wolfSSL_X509_free(issuer);
  3126. issuer = NULL;
  3127. /* check loading a response with optional certs */
  3128. ExpectTrue((f = XFOPEN(responseNoInternFile, "rb")) != XBADFILE);
  3129. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3130. if (f != XBADFILE)
  3131. XFCLOSE(f);
  3132. f = XBADFILE;
  3133. pt = data;
  3134. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3135. wolfSSL_OCSP_RESPONSE_free(res);
  3136. res = NULL;
  3137. /* check loading a response with multiple certs */
  3138. {
  3139. WOLFSSL_CERT_MANAGER* cm = NULL;
  3140. OcspEntry *entry = NULL;
  3141. CertStatus* status = NULL;
  3142. OcspRequest* request = NULL;
  3143. byte serial1[] = {0x01};
  3144. byte serial[] = {0x02};
  3145. byte issuerHash[] = {
  3146. 0x44, 0xA8, 0xDB, 0xD1, 0xBC, 0x97, 0x0A, 0x83,
  3147. 0x3B, 0x5B, 0x31, 0x9A, 0x4C, 0xB8, 0xD2, 0x52,
  3148. 0x37, 0x15, 0x8A, 0x88
  3149. };
  3150. byte issuerKeyHash[] = {
  3151. 0x73, 0xB0, 0x1C, 0xA4, 0x2F, 0x82, 0xCB, 0xCF,
  3152. 0x47, 0xA5, 0x38, 0xD7, 0xB0, 0x04, 0x82, 0x3A,
  3153. 0x7E, 0x72, 0x15, 0x21
  3154. };
  3155. ExpectNotNull(entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  3156. DYNAMIC_TYPE_OPENSSL));
  3157. ExpectNotNull(status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  3158. DYNAMIC_TYPE_OPENSSL));
  3159. if (entry != NULL)
  3160. XMEMSET(entry, 0, sizeof(OcspEntry));
  3161. if (status != NULL)
  3162. XMEMSET(status, 0, sizeof(CertStatus));
  3163. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3164. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3165. DYNAMIC_TYPE_OCSP_REQUEST));
  3166. if (request != NULL && request->serial != NULL) {
  3167. request->serialSz = sizeof(serial);
  3168. XMEMCPY(request->serial, serial, sizeof(serial));
  3169. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3170. XMEMCPY(request->issuerKeyHash, issuerKeyHash,
  3171. sizeof(issuerKeyHash));
  3172. }
  3173. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3174. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3175. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, caFile, NULL),
  3176. WOLFSSL_SUCCESS);
  3177. ExpectTrue((f = XFOPEN(responseMultiFile, "rb")) != XBADFILE);
  3178. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3179. if (f != XBADFILE)
  3180. XFCLOSE(f);
  3181. f = XBADFILE;
  3182. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3183. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3184. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3185. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3186. ExpectNotNull(entry->status);
  3187. if (request != NULL && request->serial != NULL)
  3188. XMEMCPY(request->serial, serial1, sizeof(serial1));
  3189. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3190. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3191. /* store both status's in the entry to check that "next" is not
  3192. * overwritten */
  3193. if (EXPECT_SUCCESS() && status != NULL && entry != NULL) {
  3194. status->next = entry->status;
  3195. entry->status = status;
  3196. }
  3197. if (request != NULL && request->serial != NULL)
  3198. XMEMCPY(request->serial, serial, sizeof(serial));
  3199. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3200. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3201. ExpectNotNull(entry->status->next);
  3202. /* compare the status found */
  3203. ExpectIntEQ(status->serialSz, entry->status->serialSz);
  3204. ExpectIntEQ(XMEMCMP(status->serial, entry->status->serial,
  3205. status->serialSz), 0);
  3206. if (status != NULL && entry != NULL && entry->status != status) {
  3207. XFREE(status, NULL, DYNAMIC_TYPE_OPENSSL);
  3208. }
  3209. wolfSSL_OCSP_CERTID_free(entry);
  3210. wolfSSL_OCSP_REQUEST_free(request);
  3211. wolfSSL_CertManagerFree(cm);
  3212. }
  3213. #if defined(WC_RSA_PSS)
  3214. {
  3215. const char* responsePssFile = "./certs/ocsp/test-response-rsapss.der";
  3216. /* check loading a response with RSA-PSS signature */
  3217. ExpectTrue((f = XFOPEN(responsePssFile, "rb")) != XBADFILE);
  3218. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3219. if (f != XBADFILE)
  3220. XFCLOSE(f);
  3221. pt = data;
  3222. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3223. /* try to verify the response */
  3224. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3225. SSL_FILETYPE_PEM));
  3226. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3227. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3228. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3229. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0),
  3230. WOLFSSL_SUCCESS);
  3231. wolfSSL_OCSP_BASICRESP_free(bs);
  3232. wolfSSL_OCSP_RESPONSE_free(res);
  3233. wolfSSL_X509_STORE_free(st);
  3234. wolfSSL_X509_free(issuer);
  3235. }
  3236. #endif
  3237. #endif /* HAVE_OCSP */
  3238. return EXPECT_RESULT();
  3239. }
  3240. static int test_wolfSSL_FPKI(void)
  3241. {
  3242. EXPECT_DECLS;
  3243. #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3244. XFILE f = XBADFILE;
  3245. const char* fpkiCert = "./certs/fpki-cert.der";
  3246. DecodedCert cert;
  3247. byte buf[4096];
  3248. byte* uuid = NULL;
  3249. byte* fascn = NULL;
  3250. word32 fascnSz;
  3251. word32 uuidSz;
  3252. int bytes = 0;
  3253. ExpectTrue((f = XFOPEN(fpkiCert, "rb")) != XBADFILE);
  3254. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3255. if (f != XBADFILE)
  3256. XFCLOSE(f);
  3257. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3258. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3259. ExpectIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
  3260. ExpectNotNull(fascn = (byte*)XMALLOC(fascnSz, NULL,
  3261. DYNAMIC_TYPE_TMP_BUFFER));
  3262. ExpectIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
  3263. XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3264. ExpectIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
  3265. ExpectNotNull(uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  3266. ExpectIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
  3267. XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3268. wc_FreeDecodedCert(&cert);
  3269. #endif
  3270. return EXPECT_RESULT();
  3271. }
  3272. /* use RID in confuncture with other names to test parsing of unknown other
  3273. * names */
  3274. static int test_wolfSSL_OtherName(void)
  3275. {
  3276. EXPECT_DECLS;
  3277. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3278. XFILE f = XBADFILE;
  3279. const char* ridCert = "./certs/rid-cert.der";
  3280. DecodedCert cert;
  3281. byte buf[4096];
  3282. int bytes = 0;
  3283. ExpectTrue((f = XFOPEN(ridCert, "rb")) != XBADFILE);
  3284. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3285. if (f != XBADFILE)
  3286. XFCLOSE(f);
  3287. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3288. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3289. wc_FreeDecodedCert(&cert);
  3290. #endif
  3291. return EXPECT_RESULT();
  3292. }
  3293. static int test_wolfSSL_CertRsaPss(void)
  3294. {
  3295. EXPECT_DECLS;
  3296. /* FIPS v2 and below don't support long salts. */
  3297. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
  3298. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  3299. (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
  3300. (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
  3301. XFILE f = XBADFILE;
  3302. const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
  3303. const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
  3304. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3305. RSA_MAX_SIZE >= 3072
  3306. const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
  3307. #endif
  3308. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3309. const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
  3310. #endif
  3311. DecodedCert cert;
  3312. byte buf[4096];
  3313. int bytes = 0;
  3314. WOLFSSL_CERT_MANAGER* cm = NULL;
  3315. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3316. ExpectIntEQ(WOLFSSL_SUCCESS,
  3317. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
  3318. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3319. ExpectIntEQ(WOLFSSL_SUCCESS,
  3320. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
  3321. #endif
  3322. ExpectTrue((f = XFOPEN(rsaPssSha256Cert, "rb")) != XBADFILE);
  3323. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3324. if (f != XBADFILE) {
  3325. XFCLOSE(f);
  3326. f = XBADFILE;
  3327. }
  3328. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3329. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3330. wc_FreeDecodedCert(&cert);
  3331. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3332. RSA_MAX_SIZE >= 3072
  3333. ExpectTrue((f = XFOPEN(rsaPssSha384Cert, "rb")) != XBADFILE);
  3334. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3335. if (f != XBADFILE)
  3336. XFCLOSE(f);
  3337. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3338. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3339. wc_FreeDecodedCert(&cert);
  3340. #endif
  3341. wolfSSL_CertManagerFree(cm);
  3342. #endif
  3343. return EXPECT_RESULT();
  3344. }
  3345. static int test_wolfSSL_CTX_load_verify_locations_ex(void)
  3346. {
  3347. EXPECT_DECLS;
  3348. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3349. !defined(NO_WOLFSSL_CLIENT)
  3350. WOLFSSL_CTX* ctx = NULL;
  3351. const char* ca_cert = "./certs/ca-cert.pem";
  3352. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  3353. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3354. /* test good CA */
  3355. ExpectTrue(WOLFSSL_SUCCESS ==
  3356. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  3357. WOLFSSL_LOAD_FLAG_NONE));
  3358. /* test expired CA */
  3359. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3360. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3361. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3362. #else
  3363. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3364. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3365. #endif
  3366. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3367. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3368. wolfSSL_CTX_free(ctx);
  3369. #endif
  3370. return EXPECT_RESULT();
  3371. }
  3372. static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
  3373. {
  3374. EXPECT_DECLS;
  3375. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3376. defined(USE_CERT_BUFFERS_2048)
  3377. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  3378. WOLFSSL_CTX* ctx;
  3379. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  3380. byte ca_expired_cert[TWOK_BUF];
  3381. word32 sizeof_ca_expired_cert = 0;
  3382. XFILE fp = XBADFILE;
  3383. #ifndef NO_WOLFSSL_CLIENT
  3384. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3385. #else
  3386. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3387. #endif
  3388. ExpectNotNull(ctx);
  3389. /* test good CA */
  3390. ExpectTrue(WOLFSSL_SUCCESS ==
  3391. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  3392. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  3393. WOLFSSL_LOAD_FLAG_NONE));
  3394. /* load expired CA */
  3395. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  3396. ExpectTrue((fp = XFOPEN(ca_expired_cert_file, "rb")) != XBADFILE);
  3397. ExpectIntGT(sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  3398. sizeof(ca_expired_cert), fp), 0);
  3399. if (fp != XBADFILE)
  3400. XFCLOSE(fp);
  3401. /* test expired CA failure */
  3402. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3403. ExpectIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3404. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3405. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3406. #else
  3407. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3408. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3409. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3410. #endif
  3411. /* test expired CA success */
  3412. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3413. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3414. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3415. wolfSSL_CTX_free(ctx);
  3416. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  3417. #endif
  3418. return EXPECT_RESULT();
  3419. }
  3420. static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  3421. {
  3422. EXPECT_DECLS;
  3423. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  3424. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048) && \
  3425. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3426. WOLFSSL_CTX* ctx = NULL;
  3427. #ifndef NO_WOLFSSL_CLIENT
  3428. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3429. #else
  3430. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3431. #endif
  3432. ExpectTrue(WOLFSSL_SUCCESS == wolfSSL_CTX_load_verify_chain_buffer_format(
  3433. ctx, ca_cert_chain_der, sizeof_ca_cert_chain_der,
  3434. WOLFSSL_FILETYPE_ASN1));
  3435. wolfSSL_CTX_free(ctx);
  3436. #endif
  3437. return EXPECT_RESULT();
  3438. }
  3439. static int test_wolfSSL_CTX_add1_chain_cert(void)
  3440. {
  3441. EXPECT_DECLS;
  3442. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  3443. defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  3444. WOLFSSL_CTX* ctx;
  3445. WOLFSSL* ssl = NULL;
  3446. const char *certChain[] = {
  3447. "./certs/intermediate/client-int-cert.pem",
  3448. "./certs/intermediate/ca-int2-cert.pem",
  3449. "./certs/intermediate/ca-int-cert.pem",
  3450. "./certs/ca-cert.pem",
  3451. NULL
  3452. };
  3453. const char** cert;
  3454. WOLFSSL_X509* x509 = NULL;
  3455. WOLF_STACK_OF(X509)* chain = NULL;
  3456. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3457. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3458. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3459. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3460. WOLFSSL_FILETYPE_PEM));
  3461. ExpectIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  3462. X509_free(x509);
  3463. x509 = NULL;
  3464. }
  3465. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3466. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3467. WOLFSSL_FILETYPE_PEM));
  3468. ExpectIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  3469. X509_free(x509);
  3470. x509 = NULL;
  3471. }
  3472. ExpectIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  3473. ExpectIntEQ(sk_X509_num(chain), 3);
  3474. ExpectIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  3475. ExpectIntEQ(sk_X509_num(chain), 3);
  3476. SSL_free(ssl);
  3477. SSL_CTX_free(ctx);
  3478. #endif
  3479. return EXPECT_RESULT();
  3480. }
  3481. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  3482. {
  3483. EXPECT_DECLS;
  3484. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3485. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3486. const char* server_chain_der = "./certs/server-cert-chain.der";
  3487. const char* client_single_pem = "./certs/client-cert.pem";
  3488. WOLFSSL_CTX* ctx;
  3489. (void)server_chain_der;
  3490. (void)client_single_pem;
  3491. (void)ctx;
  3492. #ifndef NO_WOLFSSL_CLIENT
  3493. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3494. #else
  3495. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3496. #endif
  3497. ExpectNotNull(ctx);
  3498. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3499. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3500. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3501. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3502. wolfSSL_CTX_free(ctx);
  3503. #endif
  3504. return EXPECT_RESULT();
  3505. }
  3506. static int test_wolfSSL_CTX_SetTmpDH_file(void)
  3507. {
  3508. EXPECT_DECLS;
  3509. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3510. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3511. WOLFSSL_CTX *ctx = NULL;
  3512. (void)ctx;
  3513. #ifndef NO_WOLFSSL_CLIENT
  3514. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3515. #else
  3516. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3517. #endif
  3518. /* invalid context */
  3519. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  3520. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3521. /* invalid dhParamFile file */
  3522. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3523. NULL, WOLFSSL_FILETYPE_PEM));
  3524. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3525. bogusFile, WOLFSSL_FILETYPE_PEM));
  3526. /* success */
  3527. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  3528. WOLFSSL_FILETYPE_PEM));
  3529. wolfSSL_CTX_free(ctx);
  3530. #endif
  3531. return EXPECT_RESULT();
  3532. }
  3533. static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
  3534. {
  3535. EXPECT_DECLS;
  3536. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  3537. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3538. WOLFSSL_CTX *ctx = NULL;
  3539. #ifndef NO_WOLFSSL_CLIENT
  3540. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3541. #else
  3542. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3543. #endif
  3544. /* invalid context */
  3545. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL,
  3546. dh_key_der_2048, sizeof_dh_key_der_2048,
  3547. WOLFSSL_FILETYPE_ASN1));
  3548. /* invalid dhParamFile file */
  3549. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  3550. 0, WOLFSSL_FILETYPE_ASN1));
  3551. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3552. dsa_key_der_2048, sizeof_dsa_key_der_2048,
  3553. WOLFSSL_FILETYPE_ASN1));
  3554. /* success */
  3555. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3556. dh_key_der_2048, sizeof_dh_key_der_2048,
  3557. WOLFSSL_FILETYPE_ASN1));
  3558. wolfSSL_CTX_free(ctx);
  3559. #endif
  3560. return EXPECT_RESULT();
  3561. }
  3562. static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  3563. {
  3564. EXPECT_DECLS;
  3565. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  3566. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3567. WOLFSSL_CTX *ctx;
  3568. (void)ctx;
  3569. #ifndef NO_WOLFSSL_CLIENT
  3570. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3571. #else
  3572. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3573. #endif
  3574. ExpectNotNull(ctx);
  3575. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  3576. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  3577. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3578. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  3579. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3580. dh_key_der_2048, sizeof_dh_key_der_2048,
  3581. WOLFSSL_FILETYPE_ASN1));
  3582. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  3583. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3584. dh_key_der_2048, sizeof_dh_key_der_2048,
  3585. WOLFSSL_FILETYPE_ASN1));
  3586. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  3587. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3588. dh_key_der_2048, sizeof_dh_key_der_2048,
  3589. WOLFSSL_FILETYPE_ASN1));
  3590. wolfSSL_CTX_free(ctx);
  3591. #endif
  3592. return EXPECT_RESULT();
  3593. }
  3594. static int test_wolfSSL_CTX_der_load_verify_locations(void)
  3595. {
  3596. EXPECT_DECLS;
  3597. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_DER_LOAD) && \
  3598. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3599. WOLFSSL_CTX* ctx = NULL;
  3600. const char* derCert = "./certs/server-cert.der";
  3601. const char* nullPath = NULL;
  3602. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  3603. const char* emptyPath = "";
  3604. /* der load Case 1 ctx NULL */
  3605. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3606. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3607. #ifndef NO_WOLFSSL_CLIENT
  3608. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3609. #else
  3610. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3611. #endif
  3612. /* Case 2 filePath NULL */
  3613. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  3614. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3615. /* Case 3 invalid format */
  3616. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3617. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  3618. /* Case 4 filePath not valid */
  3619. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  3620. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3621. /* Case 5 filePath empty */
  3622. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  3623. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3624. #ifndef NO_RSA
  3625. /* Case 6 success case */
  3626. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3627. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3628. #endif
  3629. wolfSSL_CTX_free(ctx);
  3630. #endif
  3631. return EXPECT_RESULT();
  3632. }
  3633. static int test_wolfSSL_CTX_enable_disable(void)
  3634. {
  3635. EXPECT_DECLS;
  3636. #ifndef NO_CERTS
  3637. WOLFSSL_CTX* ctx = NULL;
  3638. #ifdef HAVE_CRL
  3639. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  3640. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  3641. #endif
  3642. #ifdef HAVE_OCSP
  3643. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  3644. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  3645. #endif
  3646. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  3647. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  3648. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  3649. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  3650. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  3651. ExpectIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  3652. #endif
  3653. #ifndef NO_WOLFSSL_CLIENT
  3654. #ifdef HAVE_EXTENDED_MASTER
  3655. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  3656. #endif
  3657. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3658. #ifdef HAVE_EXTENDED_MASTER
  3659. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  3660. #endif
  3661. #elif !defined(NO_WOLFSSL_SERVER)
  3662. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3663. #endif
  3664. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  3665. #ifdef HAVE_CRL
  3666. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  3667. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  3668. #endif
  3669. #ifdef HAVE_OCSP
  3670. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  3671. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  3672. WOLFSSL_SUCCESS);
  3673. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  3674. WOLFSSL_SUCCESS);
  3675. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  3676. WOLFSSL_SUCCESS);
  3677. #endif
  3678. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  3679. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  3680. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  3681. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  3682. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  3683. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  3684. #endif
  3685. wolfSSL_CTX_free(ctx);
  3686. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  3687. #endif /* NO_CERTS */
  3688. return EXPECT_RESULT();
  3689. }
  3690. static int test_wolfSSL_CTX_ticket_API(void)
  3691. {
  3692. EXPECT_DECLS;
  3693. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  3694. WOLFSSL_CTX* ctx = NULL;
  3695. void *userCtx = (void*)"this is my ctx";
  3696. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3697. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  3698. ExpectTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  3699. wolfSSL_CTX_free(ctx);
  3700. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  3701. ExpectNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  3702. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  3703. return EXPECT_RESULT();
  3704. }
  3705. static int test_wolfSSL_set_minmax_proto_version(void)
  3706. {
  3707. EXPECT_DECLS;
  3708. #ifdef OPENSSL_EXTRA
  3709. WOLFSSL_CTX *ctx = NULL;
  3710. WOLFSSL *ssl = NULL;
  3711. (void)ssl;
  3712. #ifndef NO_WOLFSSL_CLIENT
  3713. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3714. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3715. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3716. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3717. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  3718. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  3719. ExpectIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3720. ExpectIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  3721. ExpectIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3722. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  3723. wolfSSL_free(ssl);
  3724. wolfSSL_CTX_free(ctx);
  3725. ctx = NULL;
  3726. #endif
  3727. #ifndef NO_WOLFSSL_SERVER
  3728. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3729. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3730. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3731. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  3732. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  3733. wolfSSL_CTX_free(ctx);
  3734. #endif
  3735. #endif
  3736. return EXPECT_RESULT();
  3737. }
  3738. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
  3739. defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  3740. static int test_wolfSSL_CTX_set_max_proto_version_on_result(WOLFSSL* ssl)
  3741. {
  3742. EXPECT_DECLS;
  3743. ExpectStrEQ(wolfSSL_get_version(ssl), "TLSv1.2");
  3744. return EXPECT_RESULT();
  3745. }
  3746. static int test_wolfSSL_CTX_set_max_proto_version_ctx_ready(WOLFSSL_CTX* ctx)
  3747. {
  3748. EXPECT_DECLS;
  3749. /* Set TLS 1.2 */
  3750. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  3751. WOLFSSL_SUCCESS);
  3752. return EXPECT_RESULT();
  3753. }
  3754. /* Test using wolfSSL_CTX_set_max_proto_version to limit the version below
  3755. * what was set at ctx creation. */
  3756. static int test_wolfSSL_CTX_set_max_proto_version(void)
  3757. {
  3758. EXPECT_DECLS;
  3759. test_ssl_cbf client_cbs;
  3760. test_ssl_cbf server_cbs;
  3761. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  3762. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  3763. client_cbs.method = wolfTLS_client_method;
  3764. server_cbs.method = wolfTLS_server_method;
  3765. server_cbs.ctx_ready = test_wolfSSL_CTX_set_max_proto_version_ctx_ready;
  3766. client_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  3767. server_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  3768. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  3769. &server_cbs, NULL), TEST_SUCCESS);
  3770. return EXPECT_RESULT();
  3771. }
  3772. #else
  3773. static int test_wolfSSL_CTX_set_max_proto_version(void)
  3774. {
  3775. return TEST_SKIPPED;
  3776. }
  3777. #endif
  3778. /*----------------------------------------------------------------------------*
  3779. | SSL
  3780. *----------------------------------------------------------------------------*/
  3781. static int test_server_wolfSSL_new(void)
  3782. {
  3783. EXPECT_DECLS;
  3784. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3785. !defined(NO_WOLFSSL_SERVER)
  3786. WOLFSSL_CTX *ctx = NULL;
  3787. WOLFSSL_CTX *ctx_nocert = NULL;
  3788. WOLFSSL *ssl = NULL;
  3789. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3790. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3791. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  3792. WOLFSSL_FILETYPE_PEM));
  3793. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  3794. WOLFSSL_FILETYPE_PEM));
  3795. /* invalid context */
  3796. ExpectNull(ssl = wolfSSL_new(NULL));
  3797. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && \
  3798. !defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_INIT_CTX_KEY)
  3799. ExpectNull(ssl = wolfSSL_new(ctx_nocert));
  3800. #endif
  3801. /* success */
  3802. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3803. wolfSSL_free(ssl);
  3804. wolfSSL_CTX_free(ctx);
  3805. wolfSSL_CTX_free(ctx_nocert);
  3806. #endif
  3807. return EXPECT_RESULT();
  3808. }
  3809. static int test_client_wolfSSL_new(void)
  3810. {
  3811. EXPECT_DECLS;
  3812. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3813. !defined(NO_WOLFSSL_CLIENT)
  3814. WOLFSSL_CTX *ctx = NULL;
  3815. WOLFSSL_CTX *ctx_nocert = NULL;
  3816. WOLFSSL *ssl = NULL;
  3817. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3818. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3819. ExpectTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  3820. /* invalid context */
  3821. ExpectNull(ssl = wolfSSL_new(NULL));
  3822. /* success */
  3823. ExpectNotNull(ssl = wolfSSL_new(ctx_nocert));
  3824. wolfSSL_free(ssl);
  3825. ssl = NULL;
  3826. /* success */
  3827. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3828. wolfSSL_free(ssl);
  3829. wolfSSL_CTX_free(ctx);
  3830. wolfSSL_CTX_free(ctx_nocert);
  3831. #endif
  3832. return EXPECT_RESULT();
  3833. }
  3834. static int test_wolfSSL_SetTmpDH_file(void)
  3835. {
  3836. EXPECT_DECLS;
  3837. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3838. !defined(NO_WOLFSSL_SERVER)
  3839. WOLFSSL_CTX *ctx = NULL;
  3840. WOLFSSL *ssl = NULL;
  3841. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3842. #ifndef NO_RSA
  3843. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  3844. WOLFSSL_FILETYPE_PEM));
  3845. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  3846. WOLFSSL_FILETYPE_PEM));
  3847. #elif defined(HAVE_ECC)
  3848. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  3849. WOLFSSL_FILETYPE_PEM));
  3850. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  3851. WOLFSSL_FILETYPE_PEM));
  3852. #elif defined(HAVE_ED25519)
  3853. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  3854. WOLFSSL_FILETYPE_PEM));
  3855. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  3856. WOLFSSL_FILETYPE_PEM));
  3857. #elif defined(HAVE_ED448)
  3858. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  3859. WOLFSSL_FILETYPE_PEM));
  3860. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  3861. WOLFSSL_FILETYPE_PEM));
  3862. #endif
  3863. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3864. /* invalid ssl */
  3865. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  3866. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3867. /* invalid dhParamFile file */
  3868. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  3869. NULL, WOLFSSL_FILETYPE_PEM));
  3870. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  3871. bogusFile, WOLFSSL_FILETYPE_PEM));
  3872. /* success */
  3873. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  3874. WOLFSSL_FILETYPE_PEM));
  3875. wolfSSL_free(ssl);
  3876. wolfSSL_CTX_free(ctx);
  3877. #endif
  3878. return EXPECT_RESULT();
  3879. }
  3880. static int test_wolfSSL_SetTmpDH_buffer(void)
  3881. {
  3882. EXPECT_DECLS;
  3883. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  3884. WOLFSSL_CTX *ctx = NULL;
  3885. WOLFSSL *ssl = NULL;
  3886. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3887. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  3888. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3889. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  3890. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3891. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3892. /* invalid ssl */
  3893. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  3894. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3895. /* invalid dhParamFile file */
  3896. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  3897. 0, WOLFSSL_FILETYPE_ASN1));
  3898. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  3899. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3900. /* success */
  3901. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3902. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3903. wolfSSL_free(ssl);
  3904. wolfSSL_CTX_free(ctx);
  3905. #endif
  3906. return EXPECT_RESULT();
  3907. }
  3908. static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
  3909. {
  3910. EXPECT_DECLS;
  3911. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  3912. WOLFSSL_CTX *ctx = NULL;
  3913. WOLFSSL_CTX *ctx2 = NULL;
  3914. WOLFSSL *ssl = NULL;
  3915. WOLFSSL *ssl2 = NULL;
  3916. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3917. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  3918. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3919. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  3920. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3921. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  3922. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3923. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3924. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  3925. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3926. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  3927. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3928. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  3929. ExpectNotNull(ssl2 = wolfSSL_new(ctx2));
  3930. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3931. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3932. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  3933. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3934. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3935. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  3936. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3937. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3938. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  3939. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3940. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  3941. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  3942. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3943. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  3944. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3945. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3946. wolfSSL_free(ssl2);
  3947. wolfSSL_CTX_free(ctx2);
  3948. wolfSSL_free(ssl);
  3949. wolfSSL_CTX_free(ctx);
  3950. #endif
  3951. return EXPECT_RESULT();
  3952. }
  3953. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  3954. * allowed.
  3955. * POST: return 1 on success.
  3956. */
  3957. static int test_wolfSSL_SetMinVersion(void)
  3958. {
  3959. int res = TEST_SKIPPED;
  3960. #ifndef NO_WOLFSSL_CLIENT
  3961. int failFlag = WOLFSSL_SUCCESS;
  3962. WOLFSSL_CTX* ctx = NULL;
  3963. WOLFSSL* ssl = NULL;
  3964. int itr;
  3965. #ifndef NO_OLD_TLS
  3966. const int versions[] = {
  3967. #ifdef WOLFSSL_ALLOW_TLSV10
  3968. WOLFSSL_TLSV1,
  3969. #endif
  3970. WOLFSSL_TLSV1_1,
  3971. WOLFSSL_TLSV1_2};
  3972. #elif !defined(WOLFSSL_NO_TLS12)
  3973. const int versions[] = { WOLFSSL_TLSV1_2 };
  3974. #else
  3975. const int versions[] = { WOLFSSL_TLSV1_3 };
  3976. #endif
  3977. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3978. ssl = wolfSSL_new(ctx);
  3979. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  3980. if (wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS) {
  3981. failFlag = WOLFSSL_FAILURE;
  3982. }
  3983. }
  3984. wolfSSL_free(ssl);
  3985. wolfSSL_CTX_free(ctx);
  3986. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  3987. #endif
  3988. return res;
  3989. } /* END test_wolfSSL_SetMinVersion */
  3990. #ifdef OPENSSL_EXTRA
  3991. static int test_ED25519(void)
  3992. {
  3993. EXPECT_DECLS;
  3994. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  3995. defined(WOLFSSL_KEY_GEN)
  3996. byte priv[ED25519_PRV_KEY_SIZE];
  3997. unsigned int privSz = (unsigned int)sizeof(priv);
  3998. byte pub[ED25519_PUB_KEY_SIZE];
  3999. unsigned int pubSz = (unsigned int)sizeof(pub);
  4000. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4001. const char* msg = TEST_STRING;
  4002. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4003. byte sig[ED25519_SIG_SIZE];
  4004. unsigned int sigSz = (unsigned int)sizeof(sig);
  4005. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4006. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  4007. WOLFSSL_SUCCESS);
  4008. ExpectIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  4009. ExpectIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  4010. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4011. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4012. &sigSz), WOLFSSL_SUCCESS);
  4013. ExpectIntEQ(sigSz, ED25519_SIG_SIZE);
  4014. #ifdef HAVE_ED25519_VERIFY
  4015. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4016. sigSz), WOLFSSL_SUCCESS);
  4017. #endif /* HAVE_ED25519_VERIFY */
  4018. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4019. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4020. return EXPECT_RESULT();
  4021. }
  4022. static int test_ED448(void)
  4023. {
  4024. EXPECT_DECLS;
  4025. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  4026. defined(WOLFSSL_KEY_GEN)
  4027. byte priv[ED448_PRV_KEY_SIZE];
  4028. unsigned int privSz = (unsigned int)sizeof(priv);
  4029. byte pub[ED448_PUB_KEY_SIZE];
  4030. unsigned int pubSz = (unsigned int)sizeof(pub);
  4031. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4032. const char* msg = TEST_STRING;
  4033. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4034. byte sig[ED448_SIG_SIZE];
  4035. unsigned int sigSz = (unsigned int)sizeof(sig);
  4036. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4037. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz),
  4038. WOLFSSL_SUCCESS);
  4039. ExpectIntEQ(privSz, ED448_PRV_KEY_SIZE);
  4040. ExpectIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  4041. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4042. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4043. &sigSz), WOLFSSL_SUCCESS);
  4044. ExpectIntEQ(sigSz, ED448_SIG_SIZE);
  4045. #ifdef HAVE_ED448_VERIFY
  4046. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4047. sigSz), WOLFSSL_SUCCESS);
  4048. #endif /* HAVE_ED448_VERIFY */
  4049. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4050. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4051. return EXPECT_RESULT();
  4052. }
  4053. #endif /* OPENSSL_EXTRA */
  4054. #include <wolfssl/openssl/pem.h>
  4055. /*----------------------------------------------------------------------------*
  4056. | EVP
  4057. *----------------------------------------------------------------------------*/
  4058. static int test_wolfSSL_EVP_PKEY_print_public(void)
  4059. {
  4060. EXPECT_DECLS;
  4061. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  4062. WOLFSSL_BIO* rbio = NULL;
  4063. WOLFSSL_BIO* wbio = NULL;
  4064. WOLFSSL_EVP_PKEY* pkey = NULL;
  4065. char line[256] = { 0 };
  4066. char line1[256] = { 0 };
  4067. int i;
  4068. /* test error cases */
  4069. ExpectIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  4070. /*
  4071. * test RSA public key print
  4072. * in this test, pass '3' for indent
  4073. */
  4074. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  4075. ExpectNotNull(rbio = BIO_new_mem_buf( client_keypub_der_1024,
  4076. sizeof_client_keypub_der_1024));
  4077. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4078. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4079. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  4080. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4081. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  4082. ExpectIntEQ(XSTRNCMP(line, line1, XSTRLEN(line1)), 0);
  4083. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4084. strcpy(line1, " Modulus:\n");
  4085. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4086. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4087. strcpy(line1, " 00:bc:73:0e:a8:49:f3:74:a2:a9:ef:18:a5:da:55:\n");
  4088. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4089. /* skip to the end of modulus element*/
  4090. for (i = 0; i < 8 ;i++) {
  4091. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4092. }
  4093. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4094. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  4095. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4096. /* should reach EOF */
  4097. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4098. EVP_PKEY_free(pkey);
  4099. pkey = NULL;
  4100. BIO_free(rbio);
  4101. BIO_free(wbio);
  4102. rbio = NULL;
  4103. wbio = NULL;
  4104. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  4105. /*
  4106. * test DSA public key print
  4107. */
  4108. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  4109. ExpectNotNull(rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  4110. sizeof_dsa_pub_key_der_2048));
  4111. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4112. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4113. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4114. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4115. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  4116. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4117. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4118. strcpy(line1, "pub:\n");
  4119. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4120. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4121. strcpy(line1,
  4122. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  4123. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4124. /* skip to the end of pub element*/
  4125. for (i = 0; i < 17 ;i++) {
  4126. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4127. }
  4128. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4129. strcpy(line1, "P:\n");
  4130. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4131. /* skip to the end of P element*/
  4132. for (i = 0; i < 18 ;i++) {
  4133. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4134. }
  4135. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4136. strcpy(line1, "Q:\n");
  4137. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4138. /* skip to the end of Q element*/
  4139. for (i = 0; i < 3 ;i++) {
  4140. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4141. }
  4142. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4143. strcpy(line1, "G:\n");
  4144. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4145. /* skip to the end of G element*/
  4146. for (i = 0; i < 18 ;i++) {
  4147. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4148. }
  4149. /* should reach EOF */
  4150. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4151. EVP_PKEY_free(pkey);
  4152. pkey = NULL;
  4153. BIO_free(rbio);
  4154. BIO_free(wbio);
  4155. rbio = NULL;
  4156. wbio = NULL;
  4157. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  4158. /*
  4159. * test ECC public key print
  4160. */
  4161. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  4162. ExpectNotNull(rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  4163. sizeof_ecc_clikeypub_der_256));
  4164. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4165. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4166. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4167. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4168. strcpy(line1, "Public-Key: (256 bit)\n");
  4169. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4170. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4171. strcpy(line1, "pub:\n");
  4172. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4173. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4174. strcpy(line1,
  4175. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  4176. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4177. /* skip to the end of pub element*/
  4178. for (i = 0; i < 4 ;i++) {
  4179. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4180. }
  4181. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4182. strcpy(line1, "ASN1 OID: prime256v1\n");
  4183. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4184. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4185. strcpy(line1, "NIST CURVE: P-256\n");
  4186. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4187. /* should reach EOF */
  4188. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4189. EVP_PKEY_free(pkey);
  4190. pkey = NULL;
  4191. BIO_free(rbio);
  4192. BIO_free(wbio);
  4193. rbio = NULL;
  4194. wbio = NULL;
  4195. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  4196. /*
  4197. * test DH public key print
  4198. */
  4199. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  4200. ExpectNotNull(rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  4201. sizeof_dh_pub_key_der_2048));
  4202. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4203. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4204. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL), 1);
  4205. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4206. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  4207. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4208. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4209. strcpy(line1, "public-key:\n");
  4210. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4211. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4212. strcpy(line1,
  4213. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  4214. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4215. /* skip to the end of public-key element*/
  4216. for (i = 0; i < 17 ;i++) {
  4217. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4218. }
  4219. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4220. strcpy(line1, "prime:\n");
  4221. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4222. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4223. strcpy(line1,
  4224. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  4225. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4226. /* skip to the end of prime element*/
  4227. for (i = 0; i < 17 ;i++) {
  4228. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4229. }
  4230. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4231. strcpy(line1, "generator: 2 (0x02)\n");
  4232. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4233. /* should reach EOF */
  4234. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4235. EVP_PKEY_free(pkey);
  4236. pkey = NULL;
  4237. BIO_free(rbio);
  4238. BIO_free(wbio);
  4239. rbio = NULL;
  4240. wbio = NULL;
  4241. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  4242. /* to prevent "unused variable" warning */
  4243. (void)pkey;
  4244. (void)wbio;
  4245. (void)rbio;
  4246. (void)line;
  4247. (void)line1;
  4248. (void)i;
  4249. #endif /* OPENSSL_EXTRA */
  4250. return EXPECT_RESULT();
  4251. }
  4252. /* Test functions for base64 encode/decode */
  4253. static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
  4254. {
  4255. EXPECT_DECLS;
  4256. #if defined(OPENSSL_EXTRA) && \
  4257. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  4258. EVP_ENCODE_CTX* ctx = NULL;
  4259. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4260. ExpectIntEQ(ctx->remaining,0);
  4261. ExpectIntEQ(ctx->data[0],0);
  4262. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1],0);
  4263. EVP_ENCODE_CTX_free(ctx);
  4264. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  4265. return EXPECT_RESULT();
  4266. }
  4267. static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
  4268. {
  4269. EXPECT_DECLS;
  4270. #if defined(OPENSSL_EXTRA) && \
  4271. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  4272. EVP_ENCODE_CTX* ctx = NULL;
  4273. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4274. EVP_ENCODE_CTX_free(ctx);
  4275. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  4276. return EXPECT_RESULT();
  4277. }
  4278. static int test_wolfSSL_EVP_EncodeInit(void)
  4279. {
  4280. EXPECT_DECLS;
  4281. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4282. EVP_ENCODE_CTX* ctx = NULL;
  4283. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4284. ExpectIntEQ(ctx->remaining, 0);
  4285. ExpectIntEQ(ctx->data[0], 0);
  4286. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  4287. if (ctx != NULL) {
  4288. /* make ctx dirty */
  4289. ctx->remaining = 10;
  4290. XMEMSET(ctx->data, 0x77, sizeof(ctx->data));
  4291. }
  4292. EVP_EncodeInit(ctx);
  4293. ExpectIntEQ(ctx->remaining, 0);
  4294. ExpectIntEQ(ctx->data[0], 0);
  4295. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  4296. EVP_ENCODE_CTX_free(ctx);
  4297. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  4298. return EXPECT_RESULT();
  4299. }
  4300. static int test_wolfSSL_EVP_EncodeUpdate(void)
  4301. {
  4302. EXPECT_DECLS;
  4303. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4304. int outl;
  4305. int total;
  4306. const unsigned char plain0[] = {"Th"};
  4307. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  4308. const unsigned char plain2[] = {"This is additional data."};
  4309. const unsigned char encBlock0[] = {"VGg="};
  4310. const unsigned char enc0[] = {"VGg=\n"};
  4311. /* expected encoded result for the first output 64 chars plus trailing LF*/
  4312. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  4313. const unsigned char enc2[] =
  4314. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  4315. unsigned char encOutBuff[300];
  4316. EVP_ENCODE_CTX* ctx = NULL;
  4317. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4318. EVP_EncodeInit(ctx);
  4319. /* illegal parameter test */
  4320. ExpectIntEQ(
  4321. EVP_EncodeUpdate(
  4322. NULL, /* pass NULL as ctx */
  4323. encOutBuff,
  4324. &outl,
  4325. plain1,
  4326. sizeof(plain1)-1),
  4327. 0 /* expected result code 0: fail */
  4328. );
  4329. ExpectIntEQ(
  4330. EVP_EncodeUpdate(
  4331. ctx,
  4332. NULL, /* pass NULL as out buff */
  4333. &outl,
  4334. plain1,
  4335. sizeof(plain1)-1),
  4336. 0 /* expected result code 0: fail */
  4337. );
  4338. ExpectIntEQ(
  4339. EVP_EncodeUpdate(
  4340. ctx,
  4341. encOutBuff,
  4342. NULL, /* pass NULL as outl */
  4343. plain1,
  4344. sizeof(plain1)-1),
  4345. 0 /* expected result code 0: fail */
  4346. );
  4347. ExpectIntEQ(
  4348. EVP_EncodeUpdate(
  4349. ctx,
  4350. encOutBuff,
  4351. &outl,
  4352. NULL, /* pass NULL as in */
  4353. sizeof(plain1)-1),
  4354. 0 /* expected result code 0: fail */
  4355. );
  4356. ExpectIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  4357. /* meaningless parameter test */
  4358. ExpectIntEQ(
  4359. EVP_EncodeUpdate(
  4360. ctx,
  4361. encOutBuff,
  4362. &outl,
  4363. plain1,
  4364. 0), /* pass zero input */
  4365. 1 /* expected result code 1: success */
  4366. );
  4367. /* very small data encoding test */
  4368. EVP_EncodeInit(ctx);
  4369. ExpectIntEQ(
  4370. EVP_EncodeUpdate(
  4371. ctx,
  4372. encOutBuff,
  4373. &outl,
  4374. plain0,
  4375. sizeof(plain0)-1),
  4376. 1 /* expected result code 1: success */
  4377. );
  4378. ExpectIntEQ(outl,0);
  4379. if (EXPECT_SUCCESS()) {
  4380. EVP_EncodeFinal(
  4381. ctx,
  4382. encOutBuff + outl,
  4383. &outl);
  4384. }
  4385. ExpectIntEQ( outl, sizeof(enc0)-1);
  4386. ExpectIntEQ(
  4387. XSTRNCMP(
  4388. (const char*)encOutBuff,
  4389. (const char*)enc0,sizeof(enc0) ),
  4390. 0);
  4391. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  4392. ExpectIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  4393. sizeof(encBlock0)-1);
  4394. ExpectStrEQ(encOutBuff, encBlock0);
  4395. /* pass small size( < 48bytes ) input, then make sure they are not
  4396. * encoded and just stored in ctx
  4397. */
  4398. EVP_EncodeInit(ctx);
  4399. total = 0;
  4400. outl = 0;
  4401. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  4402. ExpectIntEQ(
  4403. EVP_EncodeUpdate(
  4404. ctx,
  4405. encOutBuff, /* buffer for output */
  4406. &outl, /* size of output */
  4407. plain1, /* input */
  4408. sizeof(plain1)-1), /* size of input */
  4409. 1); /* expected result code 1:success */
  4410. total += outl;
  4411. ExpectIntEQ(outl, 0); /* no output expected */
  4412. ExpectIntEQ(ctx->remaining, sizeof(plain1) -1);
  4413. ExpectTrue(
  4414. XSTRNCMP((const char*)(ctx->data),
  4415. (const char*)plain1,
  4416. ctx->remaining) ==0 );
  4417. ExpectTrue(encOutBuff[0] == 0);
  4418. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  4419. * the stored data and the new input together
  4420. */
  4421. ExpectIntEQ(
  4422. EVP_EncodeUpdate(
  4423. ctx,
  4424. encOutBuff + outl, /* buffer for output */
  4425. &outl, /* size of output */
  4426. plain2, /* additional input */
  4427. sizeof(plain2) -1), /* size of additional input */
  4428. 1); /* expected result code 1:success */
  4429. total += outl;
  4430. ExpectIntNE(outl, 0); /* some output is expected this time*/
  4431. ExpectIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  4432. ExpectIntEQ(
  4433. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  4434. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  4435. EVP_EncodeFinal(
  4436. ctx,
  4437. encOutBuff + outl,
  4438. &outl);
  4439. total += outl;
  4440. ExpectIntNE(total,0);
  4441. ExpectIntNE(outl,0);
  4442. ExpectIntEQ(XSTRNCMP(
  4443. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  4444. /* test with illeagal parameters */
  4445. outl = 1;
  4446. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  4447. ExpectIntEQ(outl, 0);
  4448. outl = 1;
  4449. EVP_EncodeFinal(ctx, NULL, &outl);
  4450. ExpectIntEQ(outl, 0);
  4451. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  4452. EVP_EncodeFinal(NULL, NULL, NULL);
  4453. EVP_ENCODE_CTX_free(ctx);
  4454. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  4455. return EXPECT_RESULT();
  4456. }
  4457. static int test_wolfSSL_EVP_EncodeFinal(void)
  4458. {
  4459. int res = TEST_SKIPPED;
  4460. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4461. /* tests for wolfSSL_EVP_EncodeFinal are included in
  4462. * test_wolfSSL_EVP_EncodeUpdate
  4463. */
  4464. res = TEST_SUCCESS;
  4465. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  4466. return res;
  4467. }
  4468. static int test_wolfSSL_EVP_DecodeInit(void)
  4469. {
  4470. EXPECT_DECLS;
  4471. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4472. EVP_ENCODE_CTX* ctx = NULL;
  4473. ExpectNotNull( ctx = EVP_ENCODE_CTX_new());
  4474. ExpectIntEQ( ctx->remaining,0);
  4475. ExpectIntEQ( ctx->data[0],0);
  4476. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  4477. if (ctx != NULL) {
  4478. /* make ctx dirty */
  4479. ctx->remaining = 10;
  4480. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  4481. }
  4482. EVP_DecodeInit(ctx);
  4483. ExpectIntEQ( ctx->remaining,0);
  4484. ExpectIntEQ( ctx->data[0],0);
  4485. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  4486. EVP_ENCODE_CTX_free(ctx);
  4487. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4488. return EXPECT_RESULT();
  4489. }
  4490. static int test_wolfSSL_EVP_DecodeUpdate(void)
  4491. {
  4492. EXPECT_DECLS;
  4493. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4494. int outl;
  4495. unsigned char decOutBuff[300];
  4496. EVP_ENCODE_CTX* ctx = NULL;
  4497. static const unsigned char enc1[] =
  4498. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  4499. /* const unsigned char plain1[] =
  4500. {"This is a base64 decoding test."} */
  4501. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4502. EVP_DecodeInit(ctx);
  4503. /* illegal parameter tests */
  4504. /* pass NULL as ctx */
  4505. ExpectIntEQ(
  4506. EVP_DecodeUpdate(
  4507. NULL, /* pass NULL as ctx */
  4508. decOutBuff,
  4509. &outl,
  4510. enc1,
  4511. sizeof(enc1)-1),
  4512. -1 /* expected result code -1: fail */
  4513. );
  4514. ExpectIntEQ( outl, 0);
  4515. /* pass NULL as output */
  4516. ExpectIntEQ(
  4517. EVP_DecodeUpdate(
  4518. ctx,
  4519. NULL, /* pass NULL as out buff */
  4520. &outl,
  4521. enc1,
  4522. sizeof(enc1)-1),
  4523. -1 /* expected result code -1: fail */
  4524. );
  4525. ExpectIntEQ( outl, 0);
  4526. /* pass NULL as outl */
  4527. ExpectIntEQ(
  4528. EVP_DecodeUpdate(
  4529. ctx,
  4530. decOutBuff,
  4531. NULL, /* pass NULL as outl */
  4532. enc1,
  4533. sizeof(enc1)-1),
  4534. -1 /* expected result code -1: fail */
  4535. );
  4536. /* pass NULL as input */
  4537. ExpectIntEQ(
  4538. EVP_DecodeUpdate(
  4539. ctx,
  4540. decOutBuff,
  4541. &outl,
  4542. NULL, /* pass NULL as in */
  4543. sizeof(enc1)-1),
  4544. -1 /* expected result code -1: fail */
  4545. );
  4546. ExpectIntEQ( outl, 0);
  4547. ExpectIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  4548. /* pass zero length input */
  4549. ExpectIntEQ(
  4550. EVP_DecodeUpdate(
  4551. ctx,
  4552. decOutBuff,
  4553. &outl,
  4554. enc1,
  4555. 0), /* pass zero as input len */
  4556. 1 /* expected result code 1: success */
  4557. );
  4558. /* decode correct base64 string */
  4559. {
  4560. static const unsigned char enc2[] =
  4561. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  4562. static const unsigned char plain2[] =
  4563. {"This is a base64 decoding test."};
  4564. EVP_EncodeInit(ctx);
  4565. ExpectIntEQ(
  4566. EVP_DecodeUpdate(
  4567. ctx,
  4568. decOutBuff,
  4569. &outl,
  4570. enc2,
  4571. sizeof(enc2)-1),
  4572. 0 /* expected result code 0: success */
  4573. );
  4574. ExpectIntEQ(outl,sizeof(plain2) -1);
  4575. ExpectIntEQ(
  4576. EVP_DecodeFinal(
  4577. ctx,
  4578. decOutBuff + outl,
  4579. &outl),
  4580. 1 /* expected result code 1: success */
  4581. );
  4582. ExpectIntEQ(outl, 0); /* expected DecodeFinal output no data */
  4583. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  4584. sizeof(plain2) -1 ),0);
  4585. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  4586. sizeof(plain2)-1);
  4587. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  4588. sizeof(plain2) -1 ),0);
  4589. }
  4590. /* decode correct base64 string which does not have '\n' in its last*/
  4591. {
  4592. static const unsigned char enc3[] =
  4593. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  4594. static const unsigned char plain3[] =
  4595. {"This is a base64 decoding test."}; /* 31 chars */
  4596. EVP_EncodeInit(ctx);
  4597. ExpectIntEQ(
  4598. EVP_DecodeUpdate(
  4599. ctx,
  4600. decOutBuff,
  4601. &outl,
  4602. enc3,
  4603. sizeof(enc3)-1),
  4604. 0 /* expected result code 0: success */
  4605. );
  4606. ExpectIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  4607. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  4608. sizeof(plain3) -1 ),0);
  4609. ExpectIntEQ(
  4610. EVP_DecodeFinal(
  4611. ctx,
  4612. decOutBuff + outl,
  4613. &outl),
  4614. 1 /* expected result code 1: success */
  4615. );
  4616. ExpectIntEQ(outl,0 );
  4617. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  4618. sizeof(plain3)-1);
  4619. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  4620. sizeof(plain3) -1 ),0);
  4621. }
  4622. /* decode string which has a padding char ('=') in the illegal position*/
  4623. {
  4624. static const unsigned char enc4[] =
  4625. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  4626. EVP_EncodeInit(ctx);
  4627. ExpectIntEQ(
  4628. EVP_DecodeUpdate(
  4629. ctx,
  4630. decOutBuff,
  4631. &outl,
  4632. enc4,
  4633. sizeof(enc4)-1),
  4634. -1 /* expected result code -1: error */
  4635. );
  4636. ExpectIntEQ(outl,0);
  4637. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  4638. }
  4639. /* small data decode test */
  4640. {
  4641. static const unsigned char enc00[] = {"VG"};
  4642. static const unsigned char enc01[] = {"g=\n"};
  4643. static const unsigned char plain4[] = {"Th"};
  4644. EVP_EncodeInit(ctx);
  4645. ExpectIntEQ(
  4646. EVP_DecodeUpdate(
  4647. ctx,
  4648. decOutBuff,
  4649. &outl,
  4650. enc00,
  4651. sizeof(enc00)-1),
  4652. 1 /* expected result code 1: success */
  4653. );
  4654. ExpectIntEQ(outl,0);
  4655. ExpectIntEQ(
  4656. EVP_DecodeUpdate(
  4657. ctx,
  4658. decOutBuff + outl,
  4659. &outl,
  4660. enc01,
  4661. sizeof(enc01)-1),
  4662. 0 /* expected result code 0: success */
  4663. );
  4664. ExpectIntEQ(outl,sizeof(plain4)-1);
  4665. /* test with illegal parameters */
  4666. ExpectIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  4667. ExpectIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  4668. ExpectIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  4669. ExpectIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  4670. if (EXPECT_SUCCESS()) {
  4671. EVP_DecodeFinal(
  4672. ctx,
  4673. decOutBuff + outl,
  4674. &outl);
  4675. }
  4676. ExpectIntEQ( outl, 0);
  4677. ExpectIntEQ(
  4678. XSTRNCMP(
  4679. (const char*)decOutBuff,
  4680. (const char*)plain4,sizeof(plain4)-1 ),
  4681. 0);
  4682. }
  4683. EVP_ENCODE_CTX_free(ctx);
  4684. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4685. return EXPECT_RESULT();
  4686. }
  4687. static int test_wolfSSL_EVP_DecodeFinal(void)
  4688. {
  4689. int res = TEST_SKIPPED;
  4690. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4691. /* tests for wolfSSL_EVP_DecodeFinal are included in
  4692. * test_wolfSSL_EVP_DecodeUpdate
  4693. */
  4694. res = TEST_SUCCESS;
  4695. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4696. return res;
  4697. }
  4698. /* Test function for wolfSSL_EVP_get_cipherbynid.
  4699. */
  4700. #ifdef OPENSSL_EXTRA
  4701. static int test_wolfSSL_EVP_get_cipherbynid(void)
  4702. {
  4703. EXPECT_DECLS;
  4704. #ifndef NO_AES
  4705. const WOLFSSL_EVP_CIPHER* c;
  4706. c = wolfSSL_EVP_get_cipherbynid(419);
  4707. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4708. defined(WOLFSSL_AES_128)
  4709. ExpectNotNull(c);
  4710. ExpectNotNull(XSTRCMP("EVP_AES_128_CBC", c));
  4711. #else
  4712. ExpectNull(c);
  4713. #endif
  4714. c = wolfSSL_EVP_get_cipherbynid(423);
  4715. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4716. defined(WOLFSSL_AES_192)
  4717. ExpectNotNull(c);
  4718. ExpectNotNull(XSTRCMP("EVP_AES_192_CBC", c));
  4719. #else
  4720. ExpectNull(c);
  4721. #endif
  4722. c = wolfSSL_EVP_get_cipherbynid(427);
  4723. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4724. defined(WOLFSSL_AES_256)
  4725. ExpectNotNull(c);
  4726. ExpectNotNull(XSTRCMP("EVP_AES_256_CBC", c));
  4727. #else
  4728. ExpectNull(c);
  4729. #endif
  4730. c = wolfSSL_EVP_get_cipherbynid(904);
  4731. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  4732. ExpectNotNull(c);
  4733. ExpectNotNull(XSTRCMP("EVP_AES_128_CTR", c));
  4734. #else
  4735. ExpectNull(c);
  4736. #endif
  4737. c = wolfSSL_EVP_get_cipherbynid(905);
  4738. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  4739. ExpectNotNull(c);
  4740. ExpectNotNull(XSTRCMP("EVP_AES_192_CTR", c));
  4741. #else
  4742. ExpectNull(c);
  4743. #endif
  4744. c = wolfSSL_EVP_get_cipherbynid(906);
  4745. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  4746. ExpectNotNull(c);
  4747. ExpectNotNull(XSTRCMP("EVP_AES_256_CTR", c));
  4748. #else
  4749. ExpectNull(c);
  4750. #endif
  4751. c = wolfSSL_EVP_get_cipherbynid(418);
  4752. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  4753. ExpectNotNull(c);
  4754. ExpectNotNull(XSTRCMP("EVP_AES_128_ECB", c));
  4755. #else
  4756. ExpectNull(c);
  4757. #endif
  4758. c = wolfSSL_EVP_get_cipherbynid(422);
  4759. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  4760. ExpectNotNull(c);
  4761. ExpectNotNull(XSTRCMP("EVP_AES_192_ECB", c));
  4762. #else
  4763. ExpectNull(c);
  4764. #endif
  4765. c = wolfSSL_EVP_get_cipherbynid(426);
  4766. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  4767. ExpectNotNull(c);
  4768. ExpectNotNull(XSTRCMP("EVP_AES_256_ECB", c));
  4769. #else
  4770. ExpectNull(c);
  4771. #endif
  4772. #endif /* !NO_AES */
  4773. #ifndef NO_DES3
  4774. ExpectNotNull(XSTRCMP("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  4775. #ifdef WOLFSSL_DES_ECB
  4776. ExpectNotNull(XSTRCMP("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  4777. #endif
  4778. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  4779. #ifdef WOLFSSL_DES_ECB
  4780. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  4781. #endif
  4782. #endif /* !NO_DES3 */
  4783. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  4784. ExpectNotNull(XSTRCMP("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
  4785. #endif
  4786. /* test for nid is out of range */
  4787. ExpectNull(wolfSSL_EVP_get_cipherbynid(1));
  4788. return EXPECT_RESULT();
  4789. }
  4790. static int test_wolfSSL_EVP_CIPHER_CTX(void)
  4791. {
  4792. EXPECT_DECLS;
  4793. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  4794. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  4795. const EVP_CIPHER *init = EVP_aes_128_cbc();
  4796. const EVP_CIPHER *test;
  4797. byte key[AES_BLOCK_SIZE] = {0};
  4798. byte iv[AES_BLOCK_SIZE] = {0};
  4799. ExpectNotNull(ctx);
  4800. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  4801. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  4802. test = EVP_CIPHER_CTX_cipher(ctx);
  4803. ExpectTrue(init == test);
  4804. ExpectIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  4805. ExpectIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  4806. ExpectIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  4807. EVP_CIPHER_CTX_free(ctx);
  4808. /* test EVP_CIPHER_CTX_cleanup with NULL */
  4809. ExpectIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  4810. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  4811. return EXPECT_RESULT();
  4812. }
  4813. #endif /* OPENSSL_EXTRA */
  4814. /*----------------------------------------------------------------------------*
  4815. | IO
  4816. *----------------------------------------------------------------------------*/
  4817. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) || \
  4818. defined(HAVE_IO_TESTS_DEPENDENCIES)
  4819. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4820. #ifdef WC_SHA512_DIGEST_SIZE
  4821. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  4822. #else
  4823. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  4824. #endif
  4825. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  4826. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  4827. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  4828. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  4829. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  4830. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  4831. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  4832. defined(HAVE_AES_CBC)
  4833. typedef struct openssl_key_ctx {
  4834. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  4835. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  4836. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  4837. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  4838. } openssl_key_ctx;
  4839. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  4840. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  4841. static WC_INLINE int OpenSSLTicketInit(void)
  4842. {
  4843. int ret = wc_InitRng(&myOpenSSLKey_rng);
  4844. if (ret != 0) return ret;
  4845. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  4846. sizeof(myOpenSSLKey_ctx.name));
  4847. if (ret != 0) return ret;
  4848. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  4849. sizeof(myOpenSSLKey_ctx.key));
  4850. if (ret != 0) return ret;
  4851. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  4852. sizeof(myOpenSSLKey_ctx.hmacKey));
  4853. if (ret != 0) return ret;
  4854. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  4855. sizeof(myOpenSSLKey_ctx.iv));
  4856. if (ret != 0) return ret;
  4857. return 0;
  4858. }
  4859. static WC_INLINE int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  4860. byte name[WOLFSSL_TICKET_NAME_SZ],
  4861. byte iv[WOLFSSL_TICKET_IV_SZ],
  4862. WOLFSSL_EVP_CIPHER_CTX *ectx,
  4863. WOLFSSL_HMAC_CTX *hctx, int enc) {
  4864. (void)ssl;
  4865. if (enc) {
  4866. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  4867. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  4868. }
  4869. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  4870. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  4871. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  4872. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  4873. return 0;
  4874. }
  4875. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  4876. if (enc)
  4877. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  4878. else
  4879. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  4880. return 1;
  4881. }
  4882. static WC_INLINE void OpenSSLTicketCleanup(void)
  4883. {
  4884. wc_FreeRng(&myOpenSSLKey_rng);
  4885. }
  4886. #endif
  4887. #endif
  4888. /* helper functions */
  4889. #ifdef HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  4890. static WC_INLINE int test_ssl_memio_write_cb(WOLFSSL *ssl, char *data, int sz,
  4891. void *ctx)
  4892. {
  4893. struct test_ssl_memio_ctx *test_ctx;
  4894. byte *buf;
  4895. int *len;
  4896. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  4897. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  4898. buf = test_ctx->c_buff;
  4899. len = &test_ctx->c_len;
  4900. }
  4901. else {
  4902. buf = test_ctx->s_buff;
  4903. len = &test_ctx->s_len;
  4904. }
  4905. if ((unsigned)(*len + sz) > TEST_SSL_MEMIO_BUF_SZ)
  4906. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  4907. XMEMCPY(buf + *len, data, sz);
  4908. *len += sz;
  4909. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  4910. {
  4911. /* This can be imported into Wireshark by transforming the file with
  4912. * od -Ax -tx1 -v test_output.dump > test_output.dump.hex
  4913. * And then loading test_output.dump.hex into Wireshark using the
  4914. * "Import from Hex Dump..." option ion and selecting the TCP
  4915. * encapsulation option. */
  4916. char dump_file_name[64];
  4917. WOLFSSL_BIO *dump_file;
  4918. sprintf(dump_file_name, "%s/%s.dump", tmpDirName, currentTestName);
  4919. dump_file = wolfSSL_BIO_new_file(dump_file_name, "a");
  4920. if (dump_file != NULL) {
  4921. (void)wolfSSL_BIO_write(dump_file, data, sz);
  4922. wolfSSL_BIO_free(dump_file);
  4923. }
  4924. }
  4925. #endif
  4926. return sz;
  4927. }
  4928. static WC_INLINE int test_ssl_memio_read_cb(WOLFSSL *ssl, char *data, int sz,
  4929. void *ctx)
  4930. {
  4931. struct test_ssl_memio_ctx *test_ctx;
  4932. int read_sz;
  4933. byte *buf;
  4934. int *len;
  4935. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  4936. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  4937. buf = test_ctx->s_buff;
  4938. len = &test_ctx->s_len;
  4939. }
  4940. else {
  4941. buf = test_ctx->c_buff;
  4942. len = &test_ctx->c_len;
  4943. }
  4944. if (*len == 0)
  4945. return WOLFSSL_CBIO_ERR_WANT_READ;
  4946. read_sz = sz < *len ? sz : *len;
  4947. XMEMCPY(data, buf, read_sz);
  4948. XMEMMOVE(buf, buf + read_sz, *len - read_sz);
  4949. *len -= read_sz;
  4950. return read_sz;
  4951. }
  4952. static WC_INLINE int test_ssl_memio_setup(test_ssl_memio_ctx *ctx)
  4953. {
  4954. EXPECT_DECLS;
  4955. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4956. int c_sharedCtx = 0;
  4957. int s_sharedCtx = 0;
  4958. #endif
  4959. const char* certFile = svrCertFile;
  4960. const char* keyFile = svrKeyFile;
  4961. /********************************
  4962. * Create WOLFSSL_CTX for client.
  4963. ********************************/
  4964. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4965. if (ctx->c_ctx != NULL) {
  4966. c_sharedCtx = ctx->c_cb.isSharedCtx;
  4967. }
  4968. else
  4969. #endif
  4970. {
  4971. WOLFSSL_METHOD* method = NULL;
  4972. if (ctx->c_cb.method != NULL) {
  4973. method = ctx->c_cb.method();
  4974. }
  4975. else {
  4976. method = wolfSSLv23_client_method();
  4977. }
  4978. ExpectNotNull(ctx->c_ctx = wolfSSL_CTX_new(method));
  4979. }
  4980. wolfSSL_SetIORecv(ctx->c_ctx, test_ssl_memio_read_cb);
  4981. wolfSSL_SetIOSend(ctx->c_ctx, test_ssl_memio_write_cb);
  4982. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4983. wolfSSL_CTX_set_default_passwd_cb(ctx->c_ctx, PasswordCallBack);
  4984. #endif
  4985. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx, caCertFile, 0),
  4986. WOLFSSL_SUCCESS);
  4987. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4988. if (!c_sharedCtx)
  4989. #endif
  4990. {
  4991. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->c_ctx,
  4992. cliCertFile), WOLFSSL_SUCCESS);
  4993. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->c_ctx, cliKeyFile,
  4994. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4995. }
  4996. #ifdef HAVE_CRL
  4997. if (ctx->c_cb.crlPemFile != NULL) {
  4998. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx->c_ctx, WOLFSSL_CRL_CHECKALL),
  4999. WOLFSSL_SUCCESS);
  5000. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx->c_ctx, ctx->c_cb.crlPemFile,
  5001. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5002. }
  5003. #endif
  5004. if (ctx->c_ciphers != NULL) {
  5005. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->c_ctx, ctx->c_ciphers),
  5006. WOLFSSL_SUCCESS);
  5007. }
  5008. if (ctx->c_cb.ctx_ready != NULL) {
  5009. ExpectIntEQ(ctx->c_cb.ctx_ready(ctx->c_ctx), TEST_SUCCESS);
  5010. }
  5011. /********************************
  5012. * Create WOLFSSL_CTX for server.
  5013. ********************************/
  5014. if (ctx->s_ctx != NULL) {
  5015. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5016. s_sharedCtx = 1;
  5017. #endif
  5018. ctx->s_cb.isSharedCtx = 1;
  5019. }
  5020. else
  5021. {
  5022. WOLFSSL_METHOD* method = NULL;
  5023. if (ctx->s_cb.method != NULL) {
  5024. method = ctx->s_cb.method();
  5025. }
  5026. else {
  5027. method = wolfSSLv23_server_method();
  5028. }
  5029. ExpectNotNull(ctx->s_ctx = wolfSSL_CTX_new(method));
  5030. ctx->s_cb.isSharedCtx = 0;
  5031. }
  5032. if (!ctx->s_cb.ticNoInit && (ctx->s_ctx != NULL)) {
  5033. #if defined(HAVE_SESSION_TICKET) && \
  5034. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5035. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5036. OpenSSLTicketInit();
  5037. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx->s_ctx, myTicketEncCbOpenSSL);
  5038. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5039. TicketInit();
  5040. wolfSSL_CTX_set_TicketEncCb(ctx->s_ctx, myTicketEncCb);
  5041. #endif
  5042. #endif
  5043. }
  5044. wolfSSL_SetIORecv(ctx->s_ctx, test_ssl_memio_read_cb);
  5045. wolfSSL_SetIOSend(ctx->s_ctx, test_ssl_memio_write_cb);
  5046. wolfSSL_CTX_set_verify(ctx->s_ctx, WOLFSSL_VERIFY_PEER |
  5047. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5048. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx, cliCertFile, 0),
  5049. WOLFSSL_SUCCESS);
  5050. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5051. wolfSSL_CTX_set_default_passwd_cb(ctx->s_ctx, PasswordCallBack);
  5052. #endif
  5053. if (ctx->s_cb.certPemFile != NULL) {
  5054. certFile = ctx->s_cb.certPemFile;
  5055. }
  5056. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5057. if (!s_sharedCtx)
  5058. #endif
  5059. {
  5060. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->s_ctx,
  5061. certFile), WOLFSSL_SUCCESS);
  5062. }
  5063. if (ctx->s_cb.keyPemFile != NULL) {
  5064. keyFile = ctx->s_cb.keyPemFile;
  5065. }
  5066. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5067. if (!s_sharedCtx)
  5068. #endif
  5069. {
  5070. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->s_ctx, keyFile,
  5071. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5072. }
  5073. if (ctx->s_ciphers != NULL) {
  5074. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->s_ctx, ctx->s_ciphers),
  5075. WOLFSSL_SUCCESS);
  5076. }
  5077. if (ctx->s_cb.ctx_ready != NULL) {
  5078. ExpectIntEQ(ctx->s_cb.ctx_ready(ctx->s_ctx), TEST_SUCCESS);
  5079. }
  5080. /****************************
  5081. * Create WOLFSSL for client.
  5082. ****************************/
  5083. ExpectNotNull(ctx->c_ssl = wolfSSL_new(ctx->c_ctx));
  5084. wolfSSL_SetIOWriteCtx(ctx->c_ssl, ctx);
  5085. wolfSSL_SetIOReadCtx(ctx->c_ssl, ctx);
  5086. if (0
  5087. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5088. || c_sharedCtx
  5089. #endif
  5090. )
  5091. {
  5092. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->c_ssl, cliCertFile),
  5093. WOLFSSL_SUCCESS);
  5094. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->c_ssl, cliKeyFile,
  5095. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5096. }
  5097. if (ctx->c_cb.ssl_ready != NULL) {
  5098. ExpectIntEQ(ctx->c_cb.ssl_ready(ctx->c_ssl), TEST_SUCCESS);
  5099. }
  5100. /****************************
  5101. * Create WOLFSSL for server.
  5102. ****************************/
  5103. ExpectNotNull(ctx->s_ssl = wolfSSL_new(ctx->s_ctx));
  5104. wolfSSL_SetIOWriteCtx(ctx->s_ssl, ctx);
  5105. wolfSSL_SetIOReadCtx(ctx->s_ssl, ctx);
  5106. if (0
  5107. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5108. || s_sharedCtx
  5109. #endif
  5110. )
  5111. {
  5112. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->s_ssl, certFile),
  5113. WOLFSSL_SUCCESS);
  5114. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->s_ssl, keyFile,
  5115. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5116. }
  5117. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5118. wolfSSL_SetTmpDH_file(ctx->s_ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5119. #elif !defined(NO_DH)
  5120. /* will repick suites with DHE, higher priority than PSK */
  5121. SetDH(ctx->s_ssl);
  5122. #endif
  5123. if (ctx->s_cb.ssl_ready != NULL) {
  5124. ExpectIntEQ(ctx->s_cb.ssl_ready(ctx->s_ssl), TEST_SUCCESS);
  5125. }
  5126. return EXPECT_RESULT();
  5127. }
  5128. static int test_ssl_memio_do_handshake(test_ssl_memio_ctx* ctx, int max_rounds,
  5129. int* rounds)
  5130. {
  5131. int handshake_complete = 0;
  5132. int hs_c = 0;
  5133. int hs_s = 0;
  5134. int failing_s = 0;
  5135. int failing_c = 0;
  5136. int ret;
  5137. int err;
  5138. if (rounds != NULL) {
  5139. *rounds = 0;
  5140. }
  5141. while ((!handshake_complete) && (max_rounds > 0)) {
  5142. if (!hs_c) {
  5143. wolfSSL_SetLoggingPrefix("client");
  5144. ret = wolfSSL_connect(ctx->c_ssl);
  5145. wolfSSL_SetLoggingPrefix(NULL);
  5146. if (ret == WOLFSSL_SUCCESS) {
  5147. hs_c = 1;
  5148. }
  5149. else {
  5150. err = wolfSSL_get_error(ctx->c_ssl, ret);
  5151. if (err != WOLFSSL_ERROR_WANT_READ &&
  5152. err != WOLFSSL_ERROR_WANT_WRITE) {
  5153. failing_c = 1;
  5154. hs_c = 1;
  5155. if (failing_c && failing_s) {
  5156. break;
  5157. }
  5158. }
  5159. }
  5160. }
  5161. if (!hs_s) {
  5162. wolfSSL_SetLoggingPrefix("server");
  5163. ret = wolfSSL_accept(ctx->s_ssl);
  5164. wolfSSL_SetLoggingPrefix(NULL);
  5165. if (ret == WOLFSSL_SUCCESS) {
  5166. hs_s = 1;
  5167. }
  5168. else {
  5169. err = wolfSSL_get_error(ctx->s_ssl, ret);
  5170. if (err != WOLFSSL_ERROR_WANT_READ &&
  5171. err != WOLFSSL_ERROR_WANT_WRITE) {
  5172. failing_s = 1;
  5173. hs_s = 1;
  5174. if (failing_c && failing_s) {
  5175. break;
  5176. }
  5177. }
  5178. }
  5179. }
  5180. handshake_complete = hs_c && hs_s;
  5181. max_rounds--;
  5182. if (rounds != NULL) {
  5183. *rounds += 1;
  5184. }
  5185. }
  5186. if (!handshake_complete) {
  5187. return TEST_FAIL;
  5188. }
  5189. return TEST_SUCCESS;
  5190. }
  5191. static int test_ssl_memio_read_write(test_ssl_memio_ctx* ctx)
  5192. {
  5193. EXPECT_DECLS;
  5194. char input[1024];
  5195. int idx = 0;
  5196. const char* msg_c = "hello wolfssl!";
  5197. int msglen_c = (int)XSTRLEN(msg_c);
  5198. const char* msg_s = "I hear you fa shizzle!";
  5199. int msglen_s = (int)XSTRLEN(msg_s);
  5200. if (ctx->c_msg != NULL) {
  5201. msg_c = ctx->c_msg;
  5202. msglen_c = ctx->c_msglen;
  5203. }
  5204. if (ctx->s_msg != NULL) {
  5205. msg_s = ctx->s_msg;
  5206. msglen_s = ctx->s_msglen;
  5207. }
  5208. wolfSSL_SetLoggingPrefix("client");
  5209. ExpectIntEQ(wolfSSL_write(ctx->c_ssl, msg_c, msglen_c), msglen_c);
  5210. wolfSSL_SetLoggingPrefix("server");
  5211. ExpectIntGT(idx = wolfSSL_read(ctx->s_ssl, input, sizeof(input) - 1), 0);
  5212. if (idx >= 0) {
  5213. input[idx] = '\0';
  5214. }
  5215. ExpectIntGT(fprintf(stderr, "Client message: %s\n", input), 0);
  5216. ExpectIntEQ(wolfSSL_write(ctx->s_ssl, msg_s, msglen_s), msglen_s);
  5217. ctx->s_cb.return_code = EXPECT_RESULT();
  5218. wolfSSL_SetLoggingPrefix("client");
  5219. ExpectIntGT(idx = wolfSSL_read(ctx->c_ssl, input, sizeof(input) - 1), 0);
  5220. wolfSSL_SetLoggingPrefix(NULL);
  5221. if (idx >= 0) {
  5222. input[idx] = '\0';
  5223. }
  5224. ExpectIntGT(fprintf(stderr, "Server response: %s\n", input), 0);
  5225. ctx->c_cb.return_code = EXPECT_RESULT();
  5226. if (ctx->c_cb.on_result != NULL) {
  5227. ExpectIntEQ(ctx->c_cb.on_result(ctx->c_ssl), TEST_SUCCESS);
  5228. }
  5229. if (ctx->s_cb.on_result != NULL) {
  5230. ExpectIntEQ(ctx->s_cb.on_result(ctx->s_ssl), TEST_SUCCESS);
  5231. }
  5232. return EXPECT_RESULT();
  5233. }
  5234. static void test_ssl_memio_cleanup(test_ssl_memio_ctx* ctx)
  5235. {
  5236. ctx->c_cb.last_err = wolfSSL_get_error(ctx->c_ssl, 0);
  5237. ctx->s_cb.last_err = wolfSSL_get_error(ctx->s_ssl, 0);
  5238. if (ctx->c_cb.on_cleanup != NULL) {
  5239. ctx->c_cb.on_cleanup(ctx->c_ssl);
  5240. }
  5241. if (ctx->s_cb.on_cleanup != NULL) {
  5242. ctx->s_cb.on_cleanup(ctx->s_ssl);
  5243. }
  5244. wolfSSL_shutdown(ctx->s_ssl);
  5245. wolfSSL_shutdown(ctx->c_ssl);
  5246. wolfSSL_free(ctx->s_ssl);
  5247. wolfSSL_free(ctx->c_ssl);
  5248. if (!ctx->s_cb.isSharedCtx) {
  5249. wolfSSL_CTX_free(ctx->s_ctx);
  5250. ctx->s_ctx = NULL;
  5251. }
  5252. if (!ctx->c_cb.isSharedCtx) {
  5253. wolfSSL_CTX_free(ctx->c_ctx);
  5254. ctx->c_ctx = NULL;
  5255. }
  5256. if (!ctx->s_cb.ticNoInit) {
  5257. #if defined(HAVE_SESSION_TICKET) && \
  5258. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5259. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5260. OpenSSLTicketCleanup();
  5261. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5262. TicketCleanup();
  5263. #endif
  5264. #endif
  5265. }
  5266. }
  5267. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  5268. test_ssl_cbf* server_cb, cbType client_on_handshake)
  5269. {
  5270. EXPECT_DECLS;
  5271. struct test_ssl_memio_ctx test_ctx;
  5272. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5273. size_t msg_len;
  5274. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5275. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  5276. XMEMCPY(&test_ctx.c_cb, client_cb, sizeof(test_ssl_cbf));
  5277. XMEMCPY(&test_ctx.s_cb, server_cb, sizeof(test_ssl_cbf));
  5278. test_ctx.c_ctx = client_cb->ctx;
  5279. test_ctx.s_ctx = server_cb->ctx;
  5280. test_ctx.c_cb.return_code = TEST_FAIL;
  5281. test_ctx.s_cb.return_code = TEST_FAIL;
  5282. ExpectIntEQ(test_ssl_memio_setup(&test_ctx), TEST_SUCCESS);
  5283. ExpectIntEQ(test_ssl_memio_do_handshake(&test_ctx, 10, NULL), TEST_SUCCESS);
  5284. if (client_on_handshake != NULL) {
  5285. ExpectIntEQ(client_on_handshake(test_ctx.c_ctx, test_ctx.c_ssl),
  5286. TEST_SUCCESS);
  5287. }
  5288. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5289. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  5290. msg_len = wolfSSL_get_peer_finished(test_ctx.s_ssl, server_side_msg2,
  5291. MD_MAX_SIZE);
  5292. ExpectIntGE(msg_len, 0);
  5293. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  5294. msg_len = wolfSSL_get_finished(test_ctx.s_ssl, server_side_msg1,
  5295. MD_MAX_SIZE);
  5296. ExpectIntGE(msg_len, 0);
  5297. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5298. ExpectIntEQ(test_ssl_memio_read_write(&test_ctx), TEST_SUCCESS);
  5299. test_ssl_memio_cleanup(&test_ctx);
  5300. client_cb->return_code = test_ctx.c_cb.return_code;
  5301. client_cb->last_err = test_ctx.c_cb.last_err;
  5302. server_cb->return_code = test_ctx.s_cb.return_code;
  5303. server_cb->last_err = test_ctx.s_cb.last_err;
  5304. return EXPECT_RESULT();
  5305. }
  5306. #endif
  5307. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  5308. #ifdef WOLFSSL_SESSION_EXPORT
  5309. #ifdef WOLFSSL_DTLS
  5310. /* set up function for sending session information */
  5311. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  5312. {
  5313. WOLFSSL_CTX* ctx = NULL;
  5314. WOLFSSL* ssl = NULL;
  5315. AssertNotNull(inSsl);
  5316. AssertNotNull(buf);
  5317. AssertIntNE(0, sz);
  5318. /* Set ctx to DTLS 1.2 */
  5319. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  5320. AssertNotNull(ctx);
  5321. ssl = wolfSSL_new(ctx);
  5322. AssertNotNull(ssl);
  5323. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  5324. wolfSSL_free(ssl);
  5325. wolfSSL_CTX_free(ctx);
  5326. (void)userCtx;
  5327. return 0;
  5328. }
  5329. #endif
  5330. /* returns negative value on fail and positive (including 0) on success */
  5331. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  5332. {
  5333. int ret, err, loop_count, count, timeout = 10;
  5334. char msg[] = "I hear you fa shizzle!";
  5335. char input[1024];
  5336. loop_count = ((func_args*)args)->argc;
  5337. #ifdef WOLFSSL_ASYNC_CRYPT
  5338. err = 0; /* Reset error */
  5339. #endif
  5340. do {
  5341. #ifdef WOLFSSL_ASYNC_CRYPT
  5342. if (err == WC_PENDING_E) {
  5343. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5344. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5345. }
  5346. #endif
  5347. ret = wolfSSL_accept(ssl);
  5348. err = wolfSSL_get_error(ssl, 0);
  5349. if (err == WOLFSSL_ERROR_WANT_READ ||
  5350. err == WOLFSSL_ERROR_WANT_WRITE) {
  5351. int select_ret;
  5352. err = WC_PENDING_E;
  5353. select_ret = tcp_select(*sockfd, timeout);
  5354. if (select_ret == TEST_TIMEOUT) {
  5355. return WOLFSSL_FATAL_ERROR;
  5356. }
  5357. }
  5358. } while (err == WC_PENDING_E);
  5359. if (ret != WOLFSSL_SUCCESS) {
  5360. char buff[WOLFSSL_MAX_ERROR_SZ];
  5361. fprintf(stderr, "error = %d, %s\n", err,
  5362. wolfSSL_ERR_error_string(err, buff));
  5363. return ret;
  5364. }
  5365. for (count = 0; count < loop_count; count++) {
  5366. int select_ret;
  5367. select_ret = tcp_select(*sockfd, timeout);
  5368. if (select_ret == TEST_TIMEOUT) {
  5369. ret = WOLFSSL_FATAL_ERROR;
  5370. break;
  5371. }
  5372. do {
  5373. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  5374. if (ret > 0) {
  5375. input[ret] = '\0';
  5376. fprintf(stderr, "Client message: %s\n", input);
  5377. }
  5378. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  5379. do {
  5380. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  5381. return WOLFSSL_FATAL_ERROR;
  5382. }
  5383. err = wolfSSL_get_error(ssl, ret);
  5384. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  5385. }
  5386. return ret;
  5387. }
  5388. #endif /* WOLFSSL_SESSION_EXPORT */
  5389. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  5390. {
  5391. SOCKET_T sockfd = 0;
  5392. SOCKET_T clientfd = 0;
  5393. word16 port;
  5394. callback_functions* cbf;
  5395. WOLFSSL_CTX* ctx = NULL;
  5396. WOLFSSL* ssl = NULL;
  5397. func_args* opts = (func_args*)args;
  5398. char msg[] = "I hear you fa shizzle!";
  5399. char input[1024];
  5400. int idx;
  5401. int ret, err = 0;
  5402. int sharedCtx = 0;
  5403. int doUdp = 0;
  5404. SOCKADDR_IN_T cliAddr;
  5405. socklen_t cliLen;
  5406. const char* certFile = svrCertFile;
  5407. const char* keyFile = svrKeyFile;
  5408. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5409. size_t msg_len = 0;
  5410. #endif
  5411. wolfSSL_SetLoggingPrefix("server");
  5412. #ifdef WOLFSSL_TIRTOS
  5413. fdOpenSession(Task_self());
  5414. #endif
  5415. opts->return_code = TEST_FAIL;
  5416. cbf = opts->callbacks;
  5417. if (cbf != NULL && cbf->ctx) {
  5418. ctx = cbf->ctx;
  5419. sharedCtx = 1;
  5420. }
  5421. else
  5422. {
  5423. WOLFSSL_METHOD* method = NULL;
  5424. if (cbf != NULL && cbf->method != NULL) {
  5425. method = cbf->method();
  5426. }
  5427. else {
  5428. method = wolfSSLv23_server_method();
  5429. }
  5430. ctx = wolfSSL_CTX_new(method);
  5431. }
  5432. if (ctx == NULL) {
  5433. /* Release the wait for TCP ready. */
  5434. signal_ready(opts->signal);
  5435. goto done;
  5436. }
  5437. if (cbf == NULL || !cbf->ticNoInit) {
  5438. #if defined(HAVE_SESSION_TICKET) && \
  5439. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5440. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5441. OpenSSLTicketInit();
  5442. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  5443. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5444. TicketInit();
  5445. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  5446. #endif
  5447. #endif
  5448. }
  5449. #if defined(USE_WINDOWS_API)
  5450. port = opts->signal->port;
  5451. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  5452. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  5453. /* Let tcp_listen assign port */
  5454. port = 0;
  5455. #else
  5456. /* Use default port */
  5457. port = wolfSSLPort;
  5458. #endif
  5459. if (cbf != NULL)
  5460. doUdp = cbf->doUdp;
  5461. /* do it here to detect failure */
  5462. tcp_accept(
  5463. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  5464. if (doUdp) {
  5465. cliLen = sizeof(cliAddr);
  5466. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  5467. (struct sockaddr*)&cliAddr, &cliLen);
  5468. AssertIntGT(idx, 0);
  5469. }
  5470. else {
  5471. CloseSocket(sockfd);
  5472. }
  5473. wolfSSL_CTX_set_verify(ctx,
  5474. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5475. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5476. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5477. #endif
  5478. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  5479. != WOLFSSL_SUCCESS) {
  5480. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5481. goto done;
  5482. }
  5483. if (cbf != NULL && cbf->certPemFile != NULL)
  5484. certFile = cbf->certPemFile;
  5485. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5486. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, certFile,
  5487. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5488. #else
  5489. if (wolfSSL_CTX_use_certificate_file(ctx, certFile,
  5490. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5491. #endif
  5492. /*err_sys("can't load server cert chain file, "
  5493. "Please run from wolfSSL home dir");*/
  5494. goto done;
  5495. }
  5496. if (cbf != NULL && cbf->keyPemFile != NULL)
  5497. keyFile = cbf->keyPemFile;
  5498. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5499. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  5500. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5501. #else
  5502. if (wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  5503. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5504. #endif
  5505. /*err_sys("can't load server key file, "
  5506. "Please run from wolfSSL home dir");*/
  5507. goto done;
  5508. }
  5509. #ifdef HAVE_CRL
  5510. if (cbf != NULL && cbf->crlPemFile != NULL) {
  5511. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  5512. goto done;
  5513. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  5514. != WOLFSSL_SUCCESS)
  5515. goto done;
  5516. }
  5517. #endif
  5518. /* call ctx setup callback */
  5519. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5520. cbf->ctx_ready(ctx);
  5521. }
  5522. ssl = wolfSSL_new(ctx);
  5523. if (ssl == NULL) {
  5524. goto done;
  5525. }
  5526. if (doUdp) {
  5527. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  5528. if (err != WOLFSSL_SUCCESS)
  5529. goto done;
  5530. }
  5531. #ifdef WOLFSSL_SESSION_EXPORT
  5532. /* only add in more complex nonblocking case with session export tests */
  5533. if (args && opts->argc > 0) {
  5534. /* set as nonblock and time out for waiting on read/write */
  5535. tcp_set_nonblocking(&clientfd);
  5536. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  5537. }
  5538. #endif
  5539. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5540. if (sharedCtx && wolfSSL_use_certificate_file(ssl, certFile,
  5541. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5542. #else
  5543. if (wolfSSL_use_certificate_file(ssl, certFile,
  5544. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5545. #endif
  5546. /*err_sys("can't load server cert chain file, "
  5547. "Please run from wolfSSL home dir");*/
  5548. goto done;
  5549. }
  5550. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5551. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, keyFile,
  5552. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5553. #else
  5554. if (wolfSSL_use_PrivateKey_file(ssl, keyFile,
  5555. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5556. #endif
  5557. /*err_sys("can't load server key file, "
  5558. "Please run from wolfSSL home dir");*/
  5559. goto done;
  5560. }
  5561. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  5562. /*err_sys("SSL_set_fd failed");*/
  5563. goto done;
  5564. }
  5565. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5566. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5567. #elif !defined(NO_DH)
  5568. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  5569. #endif
  5570. /* call ssl setup callback */
  5571. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5572. cbf->ssl_ready(ssl);
  5573. }
  5574. #ifdef WOLFSSL_SESSION_EXPORT
  5575. /* only add in more complex nonblocking case with session export tests */
  5576. if (opts->argc > 0) {
  5577. ret = nonblocking_accept_read(args, ssl, &clientfd);
  5578. if (ret >= 0) {
  5579. opts->return_code = TEST_SUCCESS;
  5580. }
  5581. #ifdef WOLFSSL_TIRTOS
  5582. Task_yield();
  5583. #endif
  5584. goto done;
  5585. }
  5586. #endif
  5587. #ifdef WOLFSSL_ASYNC_CRYPT
  5588. err = 0; /* Reset error */
  5589. #endif
  5590. do {
  5591. #ifdef WOLFSSL_ASYNC_CRYPT
  5592. if (err == WC_PENDING_E) {
  5593. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5594. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5595. }
  5596. #endif
  5597. ret = wolfSSL_negotiate(ssl);
  5598. err = wolfSSL_get_error(ssl, 0);
  5599. } while (err == WC_PENDING_E);
  5600. if (ret != WOLFSSL_SUCCESS) {
  5601. char buff[WOLFSSL_MAX_ERROR_SZ];
  5602. fprintf(stderr, "error = %d, %s\n", err,
  5603. wolfSSL_ERR_error_string(err, buff));
  5604. /*err_sys("SSL_accept failed");*/
  5605. goto done;
  5606. }
  5607. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5608. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  5609. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  5610. AssertIntGE(msg_len, 0);
  5611. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  5612. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  5613. AssertIntGE(msg_len, 0);
  5614. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5615. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  5616. if (idx > 0) {
  5617. input[idx] = '\0';
  5618. fprintf(stderr, "Client message: %s\n", input);
  5619. }
  5620. else if (idx < 0) {
  5621. goto done;
  5622. }
  5623. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  5624. /*err_sys("SSL_write failed");*/
  5625. goto done;
  5626. }
  5627. if (cbf != NULL && cbf->on_result != NULL)
  5628. cbf->on_result(ssl);
  5629. #ifdef WOLFSSL_TIRTOS
  5630. Task_yield();
  5631. #endif
  5632. opts->return_code = TEST_SUCCESS;
  5633. done:
  5634. if (cbf != NULL)
  5635. cbf->last_err = err;
  5636. if (cbf != NULL && cbf->on_cleanup != NULL)
  5637. cbf->on_cleanup(ssl);
  5638. wolfSSL_shutdown(ssl);
  5639. wolfSSL_free(ssl);
  5640. if (!sharedCtx)
  5641. wolfSSL_CTX_free(ctx);
  5642. CloseSocket(clientfd);
  5643. #ifdef WOLFSSL_TIRTOS
  5644. fdCloseSession(Task_self());
  5645. #endif
  5646. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5647. && defined(HAVE_THREAD_LS)
  5648. wc_ecc_fp_free(); /* free per thread cache */
  5649. #endif
  5650. if (cbf == NULL || !cbf->ticNoInit) {
  5651. #if defined(HAVE_SESSION_TICKET) && \
  5652. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5653. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5654. OpenSSLTicketCleanup();
  5655. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5656. TicketCleanup();
  5657. #endif
  5658. #endif
  5659. }
  5660. wolfSSL_SetLoggingPrefix(NULL);
  5661. WOLFSSL_RETURN_FROM_THREAD(0);
  5662. }
  5663. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  5664. !defined(WOLFSSL_NO_TLS12)
  5665. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  5666. {
  5667. SOCKET_T sockfd = 0;
  5668. SOCKET_T clientfd = 0;
  5669. word16 port;
  5670. callback_functions* cbf;
  5671. WOLFSSL_CTX* ctx = 0;
  5672. WOLFSSL* ssl = 0;
  5673. char msg[] = "I hear you fa shizzle!";
  5674. char input[1024];
  5675. int idx;
  5676. int ret, err = 0;
  5677. int sharedCtx = 0;
  5678. func_args* opts = (func_args*)args;
  5679. int loop_count = opts->argc;
  5680. int count = 0;
  5681. #ifdef WOLFSSL_TIRTOS
  5682. fdOpenSession(Task_self());
  5683. #endif
  5684. opts->return_code = TEST_FAIL;
  5685. cbf = opts->callbacks;
  5686. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5687. if (cbf != NULL && cbf->ctx) {
  5688. ctx = cbf->ctx;
  5689. sharedCtx = 1;
  5690. }
  5691. else
  5692. #endif
  5693. {
  5694. WOLFSSL_METHOD* method = NULL;
  5695. if (cbf != NULL && cbf->method != NULL) {
  5696. method = cbf->method();
  5697. }
  5698. else {
  5699. method = wolfSSLv23_server_method();
  5700. }
  5701. ctx = wolfSSL_CTX_new(method);
  5702. }
  5703. #if defined(USE_WINDOWS_API)
  5704. port = opts->signal->port;
  5705. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  5706. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  5707. /* Let tcp_listen assign port */
  5708. port = 0;
  5709. #else
  5710. /* Use default port */
  5711. port = wolfSSLPort;
  5712. #endif
  5713. wolfSSL_CTX_set_verify(ctx,
  5714. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5715. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5716. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5717. #endif
  5718. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  5719. != WOLFSSL_SUCCESS) {
  5720. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5721. /* Release the wait for TCP ready. */
  5722. signal_ready(opts->signal);
  5723. goto done;
  5724. }
  5725. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  5726. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5727. /*err_sys("can't load server cert chain file, "
  5728. "Please run from wolfSSL home dir");*/
  5729. /* Release the wait for TCP ready. */
  5730. signal_ready(opts->signal);
  5731. goto done;
  5732. }
  5733. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  5734. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5735. /*err_sys("can't load server key file, "
  5736. "Please run from wolfSSL home dir");*/
  5737. /* Release the wait for TCP ready. */
  5738. signal_ready(opts->signal);
  5739. goto done;
  5740. }
  5741. /* call ctx setup callback */
  5742. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5743. cbf->ctx_ready(ctx);
  5744. }
  5745. while (count != loop_count) {
  5746. ssl = wolfSSL_new(ctx);
  5747. if (ssl == NULL) {
  5748. signal_ready(opts->signal);
  5749. goto done;
  5750. }
  5751. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  5752. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5753. /*err_sys("can't load server cert chain file, "
  5754. "Please run from wolfSSL home dir");*/
  5755. /* Release the wait for TCP ready. */
  5756. signal_ready(opts->signal);
  5757. goto done;
  5758. }
  5759. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  5760. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5761. /*err_sys("can't load server key file, "
  5762. "Please run from wolfSSL home dir");*/
  5763. /* Release the wait for TCP ready. */
  5764. signal_ready(opts->signal);
  5765. goto done;
  5766. }
  5767. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5768. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5769. #elif !defined(NO_DH)
  5770. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  5771. #endif
  5772. /* call ssl setup callback */
  5773. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5774. cbf->ssl_ready(ssl);
  5775. }
  5776. /* do it here to detect failure */
  5777. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0,
  5778. 0);
  5779. CloseSocket(sockfd);
  5780. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  5781. /*err_sys("SSL_set_fd failed");*/
  5782. goto done;
  5783. }
  5784. #ifdef WOLFSSL_ASYNC_CRYPT
  5785. err = 0; /* Reset error */
  5786. #endif
  5787. do {
  5788. #ifdef WOLFSSL_ASYNC_CRYPT
  5789. if (err == WC_PENDING_E) {
  5790. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5791. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5792. }
  5793. #endif
  5794. ret = wolfSSL_accept(ssl);
  5795. err = wolfSSL_get_error(ssl, 0);
  5796. } while (err == WC_PENDING_E);
  5797. if (ret != WOLFSSL_SUCCESS) {
  5798. char buff[WOLFSSL_MAX_ERROR_SZ];
  5799. fprintf(stderr, "error = %d, %s\n", err,
  5800. wolfSSL_ERR_error_string(err, buff));
  5801. /*err_sys("SSL_accept failed");*/
  5802. goto done;
  5803. }
  5804. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  5805. if (idx > 0) {
  5806. input[idx] = '\0';
  5807. fprintf(stderr, "Client message: %s\n", input);
  5808. }
  5809. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  5810. /*err_sys("SSL_write failed");*/
  5811. goto done;
  5812. }
  5813. /* free ssl for this connection */
  5814. wolfSSL_shutdown(ssl);
  5815. wolfSSL_free(ssl); ssl = NULL;
  5816. CloseSocket(clientfd);
  5817. count++;
  5818. }
  5819. #ifdef WOLFSSL_TIRTOS
  5820. Task_yield();
  5821. #endif
  5822. opts->return_code = TEST_SUCCESS;
  5823. done:
  5824. if (ssl != NULL) {
  5825. wolfSSL_shutdown(ssl);
  5826. wolfSSL_free(ssl);
  5827. }
  5828. if (!sharedCtx)
  5829. wolfSSL_CTX_free(ctx);
  5830. CloseSocket(clientfd);
  5831. #ifdef WOLFSSL_TIRTOS
  5832. fdCloseSession(Task_self());
  5833. #endif
  5834. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5835. && defined(HAVE_THREAD_LS)
  5836. wc_ecc_fp_free(); /* free per thread cache */
  5837. #endif
  5838. WOLFSSL_RETURN_FROM_THREAD(0);
  5839. }
  5840. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  5841. static int test_client_nofail(void* args, cbType cb)
  5842. {
  5843. #if !defined(NO_WOLFSSL_CLIENT)
  5844. SOCKET_T sockfd = 0;
  5845. callback_functions* cbf;
  5846. WOLFSSL_CTX* ctx = 0;
  5847. WOLFSSL* ssl = 0;
  5848. WOLFSSL_CIPHER* cipher;
  5849. char msg[64] = "hello wolfssl!";
  5850. char reply[1024];
  5851. int input;
  5852. int msgSz = (int)XSTRLEN(msg);
  5853. int ret, err = 0;
  5854. int cipherSuite;
  5855. int sharedCtx = 0;
  5856. int doUdp = 0;
  5857. const char* cipherName1, *cipherName2;
  5858. wolfSSL_SetLoggingPrefix("client");
  5859. #ifdef WOLFSSL_TIRTOS
  5860. fdOpenSession(Task_self());
  5861. #endif
  5862. ((func_args*)args)->return_code = TEST_FAIL;
  5863. cbf = ((func_args*)args)->callbacks;
  5864. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5865. if (cbf != NULL && cbf->ctx) {
  5866. ctx = cbf->ctx;
  5867. sharedCtx = cbf->isSharedCtx;
  5868. }
  5869. else
  5870. #endif
  5871. {
  5872. WOLFSSL_METHOD* method = NULL;
  5873. if (cbf != NULL && cbf->method != NULL) {
  5874. method = cbf->method();
  5875. }
  5876. else {
  5877. method = wolfSSLv23_client_method();
  5878. }
  5879. ctx = wolfSSL_CTX_new(method);
  5880. }
  5881. if (cbf != NULL)
  5882. doUdp = cbf->doUdp;
  5883. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5884. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5885. #endif
  5886. /* Do connect here so server detects failures */
  5887. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5888. doUdp, 0, NULL);
  5889. /* Connect the socket so that we don't have to set the peer later on */
  5890. if (doUdp)
  5891. udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
  5892. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  5893. {
  5894. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5895. goto done;
  5896. }
  5897. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5898. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5899. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5900. #else
  5901. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5902. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5903. #endif
  5904. /*err_sys("can't load client cert file, "
  5905. "Please run from wolfSSL home dir");*/
  5906. goto done;
  5907. }
  5908. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5909. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5910. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5911. #else
  5912. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5913. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5914. #endif
  5915. /*err_sys("can't load client key file, "
  5916. "Please run from wolfSSL home dir");*/
  5917. goto done;
  5918. }
  5919. #ifdef HAVE_CRL
  5920. if (cbf != NULL && cbf->crlPemFile != NULL) {
  5921. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  5922. goto done;
  5923. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  5924. != WOLFSSL_SUCCESS)
  5925. goto done;
  5926. }
  5927. #endif
  5928. /* call ctx setup callback */
  5929. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5930. cbf->ctx_ready(ctx);
  5931. }
  5932. ssl = wolfSSL_new(ctx);
  5933. if (ssl == NULL) {
  5934. goto done;
  5935. }
  5936. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5937. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  5938. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5939. #else
  5940. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  5941. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5942. #endif
  5943. /*err_sys("can't load client cert file, "
  5944. "Please run from wolfSSL home dir");*/
  5945. goto done;
  5946. }
  5947. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5948. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  5949. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5950. #else
  5951. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  5952. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5953. #endif
  5954. /*err_sys("can't load client key file, "
  5955. "Please run from wolfSSL home dir");*/
  5956. goto done;
  5957. }
  5958. if (!doUdp) {
  5959. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5960. /*err_sys("SSL_set_fd failed");*/
  5961. goto done;
  5962. }
  5963. }
  5964. else {
  5965. #ifdef WOLFSSL_DTLS
  5966. if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5967. /*err_sys("SSL_set_fd failed");*/
  5968. goto done;
  5969. }
  5970. #else
  5971. goto done;
  5972. #endif
  5973. }
  5974. /* call ssl setup callback */
  5975. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5976. cbf->ssl_ready(ssl);
  5977. }
  5978. #ifdef WOLFSSL_ASYNC_CRYPT
  5979. err = 0; /* Reset error */
  5980. #endif
  5981. do {
  5982. #ifdef WOLFSSL_ASYNC_CRYPT
  5983. if (err == WC_PENDING_E) {
  5984. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5985. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5986. }
  5987. #endif
  5988. ret = wolfSSL_negotiate(ssl);
  5989. err = wolfSSL_get_error(ssl, 0);
  5990. } while (err == WC_PENDING_E);
  5991. if (ret != WOLFSSL_SUCCESS) {
  5992. char buff[WOLFSSL_MAX_ERROR_SZ];
  5993. fprintf(stderr, "error = %d, %s\n", err,
  5994. wolfSSL_ERR_error_string(err, buff));
  5995. /*err_sys("SSL_connect failed");*/
  5996. goto done;
  5997. }
  5998. /* test the various get cipher methods */
  5999. /* Internal cipher suite names */
  6000. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  6001. cipherName1 = wolfSSL_get_cipher_name(ssl);
  6002. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  6003. (cipherSuite >> 8), cipherSuite & 0xFF);
  6004. AssertStrEQ(cipherName1, cipherName2);
  6005. /* IANA Cipher Suites Names */
  6006. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  6007. then it's the internal cipher suite name */
  6008. cipher = wolfSSL_get_current_cipher(ssl);
  6009. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  6010. cipherName2 = wolfSSL_get_cipher(ssl);
  6011. AssertStrEQ(cipherName1, cipherName2);
  6012. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  6013. !defined(WOLFSSL_QT)
  6014. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  6015. (cipherSuite >> 8), cipherSuite & 0xFF);
  6016. AssertStrEQ(cipherName1, cipherName2);
  6017. #endif
  6018. if (cb != NULL)
  6019. (cb)(ctx, ssl);
  6020. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6021. /*err_sys("SSL_write failed");*/
  6022. goto done;
  6023. }
  6024. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6025. if (input > 0) {
  6026. reply[input] = '\0';
  6027. fprintf(stderr, "Server response: %s\n", reply);
  6028. }
  6029. if (cbf != NULL && cbf->on_result != NULL)
  6030. cbf->on_result(ssl);
  6031. ((func_args*)args)->return_code = TEST_SUCCESS;
  6032. done:
  6033. if (cbf != NULL)
  6034. cbf->last_err = err;
  6035. if (cbf != NULL && cbf->on_cleanup != NULL)
  6036. cbf->on_cleanup(ssl);
  6037. wolfSSL_free(ssl);
  6038. if (!sharedCtx)
  6039. wolfSSL_CTX_free(ctx);
  6040. CloseSocket(sockfd);
  6041. #ifdef WOLFSSL_TIRTOS
  6042. fdCloseSession(Task_self());
  6043. #endif
  6044. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6045. && defined(HAVE_THREAD_LS)
  6046. wc_ecc_fp_free(); /* free per thread cache */
  6047. #endif
  6048. #else
  6049. (void)args;
  6050. (void)cb;
  6051. #endif /* !NO_WOLFSSL_CLIENT */
  6052. wolfSSL_SetLoggingPrefix(NULL);
  6053. return 0;
  6054. }
  6055. void test_wolfSSL_client_server_nofail_ex(callback_functions* client_cb,
  6056. callback_functions* server_cb, cbType client_on_handshake)
  6057. {
  6058. func_args client_args;
  6059. func_args server_args;
  6060. tcp_ready ready;
  6061. THREAD_TYPE serverThread;
  6062. XMEMSET(&client_args, 0, sizeof(func_args));
  6063. XMEMSET(&server_args, 0, sizeof(func_args));
  6064. #ifdef WOLFSSL_TIRTOS
  6065. fdOpenSession(Task_self());
  6066. #endif
  6067. StartTCP();
  6068. InitTcpReady(&ready);
  6069. #if defined(USE_WINDOWS_API)
  6070. /* use RNG to get random port if using windows */
  6071. ready.port = GetRandomPort();
  6072. #endif
  6073. server_args.signal = &ready;
  6074. server_args.callbacks = server_cb;
  6075. client_args.signal = &ready;
  6076. client_args.callbacks = client_cb;
  6077. start_thread(test_server_nofail, &server_args, &serverThread);
  6078. wait_tcp_ready(&server_args);
  6079. test_client_nofail(&client_args, client_on_handshake);
  6080. join_thread(serverThread);
  6081. client_cb->return_code = client_args.return_code;
  6082. server_cb->return_code = server_args.return_code;
  6083. FreeTcpReady(&ready);
  6084. #ifdef WOLFSSL_TIRTOS
  6085. fdOpenSession(Task_self());
  6086. #endif
  6087. }
  6088. void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
  6089. callback_functions* server_cb)
  6090. {
  6091. test_wolfSSL_client_server_nofail_ex(client_cb, server_cb, NULL);
  6092. }
  6093. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6094. !defined(WOLFSSL_NO_TLS12) && !defined(NO_WOLFSSL_CLIENT)
  6095. static void test_client_reuse_WOLFSSLobj(void* args, cbType cb,
  6096. void* server_args)
  6097. {
  6098. SOCKET_T sockfd = 0;
  6099. callback_functions* cbf;
  6100. WOLFSSL_CTX* ctx = 0;
  6101. WOLFSSL* ssl = 0;
  6102. WOLFSSL_SESSION* session = NULL;
  6103. char msg[64] = "hello wolfssl!";
  6104. char reply[1024];
  6105. int input;
  6106. int msgSz = (int)XSTRLEN(msg);
  6107. int ret, err = 0;
  6108. int sharedCtx = 0;
  6109. #ifdef WOLFSSL_TIRTOS
  6110. fdOpenSession(Task_self());
  6111. #endif
  6112. ((func_args*)args)->return_code = TEST_FAIL;
  6113. cbf = ((func_args*)args)->callbacks;
  6114. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6115. if (cbf != NULL && cbf->ctx) {
  6116. ctx = cbf->ctx;
  6117. sharedCtx = 1;
  6118. }
  6119. else
  6120. #endif
  6121. {
  6122. WOLFSSL_METHOD* method = NULL;
  6123. if (cbf != NULL && cbf->method != NULL) {
  6124. method = cbf->method();
  6125. }
  6126. else {
  6127. method = wolfSSLv23_client_method();
  6128. }
  6129. ctx = wolfSSL_CTX_new(method);
  6130. }
  6131. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6132. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6133. #endif
  6134. /* Do connect here so server detects failures */
  6135. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6136. 0, 0, NULL);
  6137. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) !=
  6138. WOLFSSL_SUCCESS) {
  6139. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6140. goto done;
  6141. }
  6142. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6143. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6144. /*err_sys("can't load client cert file, "
  6145. "Please run from wolfSSL home dir");*/
  6146. goto done;
  6147. }
  6148. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6149. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6150. /*err_sys("can't load client key file, "
  6151. "Please run from wolfSSL home dir");*/
  6152. goto done;
  6153. }
  6154. /* call ctx setup callback */
  6155. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6156. cbf->ctx_ready(ctx);
  6157. }
  6158. ssl = wolfSSL_new(ctx);
  6159. if (ssl == NULL) {
  6160. goto done;
  6161. }
  6162. /* keep handshake resources for re-using WOLFSSL obj */
  6163. wolfSSL_KeepArrays(ssl);
  6164. if (wolfSSL_KeepHandshakeResources(ssl)) {
  6165. /* err_sys("SSL_KeepHandshakeResources failed"); */
  6166. goto done;
  6167. }
  6168. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  6169. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6170. /*err_sys("can't load client cert file, "
  6171. "Please run from wolfSSL home dir");*/
  6172. goto done;
  6173. }
  6174. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6175. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6176. /*err_sys("can't load client key file, "
  6177. "Please run from wolfSSL home dir");*/
  6178. goto done;
  6179. }
  6180. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6181. /*err_sys("SSL_set_fd failed");*/
  6182. goto done;
  6183. }
  6184. /* call ssl setup callback */
  6185. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6186. cbf->ssl_ready(ssl);
  6187. }
  6188. #ifdef WOLFSSL_ASYNC_CRYPT
  6189. err = 0; /* Reset error */
  6190. #endif
  6191. do {
  6192. #ifdef WOLFSSL_ASYNC_CRYPT
  6193. if (err == WC_PENDING_E) {
  6194. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6195. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6196. }
  6197. #endif
  6198. ret = wolfSSL_connect(ssl);
  6199. err = wolfSSL_get_error(ssl, 0);
  6200. } while (err == WC_PENDING_E);
  6201. if (ret != WOLFSSL_SUCCESS) {
  6202. char buff[WOLFSSL_MAX_ERROR_SZ];
  6203. fprintf(stderr, "error = %d, %s\n", err,
  6204. wolfSSL_ERR_error_string(err, buff));
  6205. /*err_sys("SSL_connect failed");*/
  6206. goto done;
  6207. }
  6208. /* Build first session */
  6209. if (cb != NULL)
  6210. cb(ctx, ssl);
  6211. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6212. /*err_sys("SSL_write failed");*/
  6213. goto done;
  6214. }
  6215. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6216. if (input > 0) {
  6217. reply[input] = '\0';
  6218. fprintf(stderr, "Server response: %s\n", reply);
  6219. }
  6220. /* Session Resumption by re-using WOLFSSL object */
  6221. wolfSSL_set_quiet_shutdown(ssl, 1);
  6222. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  6223. /* err_sys ("SSL shutdown failed"); */
  6224. goto done;
  6225. }
  6226. session = wolfSSL_get1_session(ssl);
  6227. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  6228. wolfSSL_SESSION_free(session);
  6229. /* err_sys ("SSL_clear failed"); */
  6230. goto done;
  6231. }
  6232. wolfSSL_set_session(ssl, session);
  6233. wolfSSL_SESSION_free(session);
  6234. session = NULL;
  6235. /* close socket once */
  6236. CloseSocket(sockfd);
  6237. sockfd = 0;
  6238. /* wait until server ready */
  6239. wait_tcp_ready((func_args*)server_args);
  6240. fprintf(stderr, "session resumption\n");
  6241. /* Do re-connect */
  6242. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6243. 0, 0, NULL);
  6244. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6245. /*err_sys("SSL_set_fd failed");*/
  6246. goto done;
  6247. }
  6248. #ifdef WOLFSSL_ASYNC_CRYPT
  6249. err = 0; /* Reset error */
  6250. #endif
  6251. do {
  6252. #ifdef WOLFSSL_ASYNC_CRYPT
  6253. if (err == WC_PENDING_E) {
  6254. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6255. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6256. }
  6257. #endif
  6258. ret = wolfSSL_connect(ssl);
  6259. err = wolfSSL_get_error(ssl, 0);
  6260. } while (err == WC_PENDING_E);
  6261. if (ret != WOLFSSL_SUCCESS) {
  6262. char buff[WOLFSSL_MAX_ERROR_SZ];
  6263. fprintf(stderr, "error = %d, %s\n", err,
  6264. wolfSSL_ERR_error_string(err, buff));
  6265. /*err_sys("SSL_connect failed");*/
  6266. goto done;
  6267. }
  6268. /* Build first session */
  6269. if (cb != NULL)
  6270. cb(ctx, ssl);
  6271. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6272. /*err_sys("SSL_write failed");*/
  6273. goto done;
  6274. }
  6275. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6276. if (input > 0) {
  6277. reply[input] = '\0';
  6278. fprintf(stderr, "Server response: %s\n", reply);
  6279. }
  6280. ((func_args*)args)->return_code = TEST_SUCCESS;
  6281. done:
  6282. wolfSSL_free(ssl);
  6283. if (!sharedCtx)
  6284. wolfSSL_CTX_free(ctx);
  6285. CloseSocket(sockfd);
  6286. #ifdef WOLFSSL_TIRTOS
  6287. fdCloseSession(Task_self());
  6288. #endif
  6289. return;
  6290. }
  6291. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  6292. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  6293. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  6294. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  6295. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  6296. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  6297. #define HAVE_ALPN_PROTOS_SUPPORT
  6298. #endif
  6299. /* Generic TLS client / server with callbacks for API unit tests
  6300. * Used by SNI / ALPN / crypto callback helper functions */
  6301. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  6302. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  6303. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  6304. #define ENABLE_TLS_CALLBACK_TEST
  6305. #endif
  6306. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  6307. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  6308. /* TLS server for API unit testing - generic */
  6309. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  6310. {
  6311. callback_functions* callbacks = ((func_args*)args)->callbacks;
  6312. WOLFSSL_CTX* ctx = NULL;
  6313. WOLFSSL* ssl = NULL;
  6314. SOCKET_T sfd = 0;
  6315. SOCKET_T cfd = 0;
  6316. word16 port;
  6317. char msg[] = "I hear you fa shizzle!";
  6318. int len = (int) XSTRLEN(msg);
  6319. char input[1024];
  6320. int idx;
  6321. int ret, err = 0;
  6322. ((func_args*)args)->return_code = TEST_FAIL;
  6323. #if defined(USE_WINDOWS_API)
  6324. port = ((func_args*)args)->signal->port;
  6325. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6326. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6327. /* Let tcp_listen assign port */
  6328. port = 0;
  6329. #else
  6330. /* Use default port */
  6331. port = wolfSSLPort;
  6332. #endif
  6333. #ifdef WOLFSSL_DTLS
  6334. if (callbacks->method == wolfDTLS_server_method
  6335. #ifdef WOLFSSL_STATIC_MEMORY
  6336. || callbacks->method_ex == wolfDTLS_server_method_ex
  6337. #endif
  6338. #ifndef NO_OLD_TLS
  6339. || callbacks->method == wolfDTLSv1_server_method
  6340. #ifdef WOLFSSL_STATIC_MEMORY
  6341. || callbacks->method_ex == wolfDTLSv1_server_method_ex
  6342. #endif
  6343. #endif
  6344. #ifndef WOLFSSL_NO_TLS12
  6345. || callbacks->method == wolfDTLSv1_2_server_method
  6346. #ifdef WOLFSSL_STATIC_MEMORY
  6347. || callbacks->method_ex == wolfDTLSv1_2_server_method_ex
  6348. #endif
  6349. #endif
  6350. #ifdef WOLFSSL_DTLS13
  6351. || callbacks->method == wolfDTLSv1_3_server_method
  6352. #ifdef WOLFSSL_STATIC_MEMORY
  6353. || callbacks->method_ex == wolfDTLSv1_3_server_method_ex
  6354. #endif
  6355. #endif
  6356. ) {
  6357. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  6358. }
  6359. else
  6360. #endif
  6361. {
  6362. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  6363. }
  6364. #ifdef WOLFSSL_STATIC_MEMORY
  6365. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  6366. callbacks->memSz > 0) {
  6367. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  6368. callbacks->mem, callbacks->memSz, 0, 1);
  6369. if (ret != WOLFSSL_SUCCESS) {
  6370. fprintf(stderr, "CTX static new failed %d\n", ret);
  6371. goto cleanup;
  6372. }
  6373. }
  6374. #else
  6375. ctx = wolfSSL_CTX_new(callbacks->method());
  6376. #endif
  6377. if (ctx == NULL) {
  6378. fprintf(stderr, "CTX new failed\n");
  6379. goto cleanup;
  6380. }
  6381. /* set defaults */
  6382. if (callbacks->caPemFile == NULL)
  6383. callbacks->caPemFile = cliCertFile;
  6384. if (callbacks->certPemFile == NULL)
  6385. callbacks->certPemFile = svrCertFile;
  6386. if (callbacks->keyPemFile == NULL)
  6387. callbacks->keyPemFile = svrKeyFile;
  6388. #ifdef WOLFSSL_TIRTOS
  6389. fdOpenSession(Task_self());
  6390. #endif
  6391. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  6392. wolfSSL_CTX_set_verify(ctx,
  6393. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6394. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6395. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6396. #endif
  6397. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  6398. if (callbacks->method == wolfDTLSv1_2_server_method) {
  6399. if (wolfSSL_CTX_dtls_set_export(ctx, test_export) != WOLFSSL_SUCCESS)
  6400. goto cleanup;
  6401. }
  6402. #endif
  6403. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  6404. WOLFSSL_SUCCESS) {
  6405. goto cleanup;
  6406. }
  6407. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  6408. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6409. goto cleanup;
  6410. }
  6411. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  6412. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6413. goto cleanup;
  6414. }
  6415. #ifdef HAVE_CRL
  6416. if (callbacks->crlPemFile != NULL) {
  6417. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  6418. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6419. goto cleanup;
  6420. }
  6421. }
  6422. #endif
  6423. if (callbacks->ctx_ready)
  6424. callbacks->ctx_ready(ctx);
  6425. ssl = wolfSSL_new(ctx);
  6426. if (ssl == NULL) {
  6427. fprintf(stderr, "SSL new failed\n");
  6428. goto cleanup;
  6429. }
  6430. if (wolfSSL_dtls(ssl)) {
  6431. SOCKADDR_IN_T cliAddr;
  6432. socklen_t cliLen;
  6433. cliLen = sizeof(cliAddr);
  6434. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  6435. (struct sockaddr*)&cliAddr, &cliLen);
  6436. if (idx <= 0) {
  6437. goto cleanup;
  6438. }
  6439. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  6440. }
  6441. else {
  6442. CloseSocket(sfd);
  6443. }
  6444. if (wolfSSL_set_fd(ssl, cfd) != WOLFSSL_SUCCESS) {
  6445. goto cleanup;
  6446. }
  6447. if (callbacks->loadToSSL) {
  6448. wolfSSL_SetDevId(ssl, callbacks->devId);
  6449. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  6450. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6451. goto cleanup;
  6452. }
  6453. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  6454. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6455. goto cleanup;
  6456. }
  6457. }
  6458. #ifdef NO_PSK
  6459. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6460. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6461. #elif !defined(NO_DH)
  6462. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6463. #endif
  6464. #endif
  6465. if (callbacks->ssl_ready)
  6466. callbacks->ssl_ready(ssl);
  6467. #ifdef WOLFSSL_ASYNC_CRYPT
  6468. err = 0; /* Reset error */
  6469. #endif
  6470. do {
  6471. #ifdef WOLFSSL_ASYNC_CRYPT
  6472. if (err == WC_PENDING_E) {
  6473. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6474. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6475. }
  6476. #endif
  6477. ret = wolfSSL_accept(ssl);
  6478. err = wolfSSL_get_error(ssl, ret);
  6479. } while (err == WC_PENDING_E);
  6480. if (ret != WOLFSSL_SUCCESS) {
  6481. char buff[WOLFSSL_MAX_ERROR_SZ];
  6482. fprintf(stderr, "accept error = %d, %s\n", err,
  6483. wolfSSL_ERR_error_string(err, buff));
  6484. /*err_sys("SSL_accept failed");*/
  6485. }
  6486. else {
  6487. #ifdef WOLFSSL_ASYNC_CRYPT
  6488. err = 0; /* Reset error */
  6489. #endif
  6490. do {
  6491. #ifdef WOLFSSL_ASYNC_CRYPT
  6492. if (err == WC_PENDING_E) {
  6493. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6494. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6495. }
  6496. #endif
  6497. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6498. err = wolfSSL_get_error(ssl, idx);
  6499. } while (err == WC_PENDING_E);
  6500. if (idx > 0) {
  6501. input[idx] = 0;
  6502. fprintf(stderr, "Client message: %s\n", input);
  6503. }
  6504. #ifdef WOLFSSL_ASYNC_CRYPT
  6505. err = 0; /* Reset error */
  6506. #endif
  6507. do {
  6508. #ifdef WOLFSSL_ASYNC_CRYPT
  6509. if (err == WC_PENDING_E) {
  6510. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6511. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6512. }
  6513. #endif
  6514. ret = wolfSSL_write(ssl, msg, len);
  6515. err = wolfSSL_get_error(ssl, ret);
  6516. } while (err == WC_PENDING_E);
  6517. if (len != ret) {
  6518. goto cleanup;
  6519. }
  6520. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  6521. defined(WOLFSSL_DTLS)
  6522. if (wolfSSL_dtls(ssl)) {
  6523. byte* import;
  6524. word32 sz;
  6525. wolfSSL_dtls_export(ssl, NULL, &sz);
  6526. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6527. if (import == NULL) {
  6528. goto cleanup;
  6529. }
  6530. idx = wolfSSL_dtls_export(ssl, import, &sz);
  6531. if (idx < 0) {
  6532. goto cleanup;
  6533. }
  6534. if (wolfSSL_dtls_import(ssl, import, idx) < 0) {
  6535. goto cleanup;
  6536. }
  6537. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6538. }
  6539. #endif
  6540. #ifdef WOLFSSL_TIRTOS
  6541. Task_yield();
  6542. #endif
  6543. ((func_args*)args)->return_code = TEST_SUCCESS;
  6544. }
  6545. if (callbacks->on_result)
  6546. callbacks->on_result(ssl);
  6547. wolfSSL_shutdown(ssl);
  6548. cleanup:
  6549. wolfSSL_free(ssl);
  6550. wolfSSL_CTX_free(ctx);
  6551. CloseSocket(cfd);
  6552. #ifdef WOLFSSL_TIRTOS
  6553. fdCloseSession(Task_self());
  6554. #endif
  6555. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6556. && defined(HAVE_THREAD_LS)
  6557. wc_ecc_fp_free(); /* free per thread cache */
  6558. #endif
  6559. WOLFSSL_RETURN_FROM_THREAD(0);
  6560. }
  6561. /* TLS Client for API unit testing - generic */
  6562. static void run_wolfssl_client(void* args)
  6563. {
  6564. callback_functions* callbacks = ((func_args*)args)->callbacks;
  6565. WOLFSSL_CTX* ctx = NULL;
  6566. WOLFSSL* ssl = NULL;
  6567. SOCKET_T sfd = 0;
  6568. char msg[] = "hello wolfssl server!";
  6569. int len = (int) XSTRLEN(msg);
  6570. char input[1024];
  6571. int ret, err = 0;
  6572. ((func_args*)args)->return_code = TEST_FAIL;
  6573. /* set defaults */
  6574. if (callbacks->caPemFile == NULL)
  6575. callbacks->caPemFile = caCertFile;
  6576. if (callbacks->certPemFile == NULL)
  6577. callbacks->certPemFile = cliCertFile;
  6578. if (callbacks->keyPemFile == NULL)
  6579. callbacks->keyPemFile = cliKeyFile;
  6580. #ifdef WOLFSSL_STATIC_MEMORY
  6581. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  6582. callbacks->memSz > 0) {
  6583. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  6584. callbacks->mem, callbacks->memSz, 0, 1);
  6585. if (ret != WOLFSSL_SUCCESS) {
  6586. fprintf(stderr, "CTX static new failed %d\n", ret);
  6587. goto cleanup;
  6588. }
  6589. }
  6590. #else
  6591. ctx = wolfSSL_CTX_new(callbacks->method());
  6592. #endif
  6593. if (ctx == NULL) {
  6594. fprintf(stderr, "CTX new failed\n");
  6595. goto cleanup;
  6596. }
  6597. #ifdef WOLFSSL_TIRTOS
  6598. fdOpenSession(Task_self());
  6599. #endif
  6600. if (!callbacks->loadToSSL) {
  6601. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  6602. }
  6603. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6604. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6605. #endif
  6606. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  6607. WOLFSSL_SUCCESS) {
  6608. goto cleanup;
  6609. }
  6610. if (!callbacks->loadToSSL) {
  6611. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  6612. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6613. goto cleanup;
  6614. }
  6615. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  6616. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6617. goto cleanup;
  6618. }
  6619. }
  6620. #ifdef HAVE_CRL
  6621. if (callbacks->crlPemFile != NULL) {
  6622. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  6623. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6624. goto cleanup;
  6625. }
  6626. }
  6627. #endif
  6628. if (callbacks->ctx_ready)
  6629. callbacks->ctx_ready(ctx);
  6630. ssl = wolfSSL_new(ctx);
  6631. if (wolfSSL_dtls(ssl)) {
  6632. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6633. 1, 0, ssl);
  6634. }
  6635. else {
  6636. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6637. 0, 0, ssl);
  6638. }
  6639. if (wolfSSL_set_fd(ssl, sfd) != WOLFSSL_SUCCESS) {
  6640. goto cleanup;
  6641. }
  6642. if (callbacks->loadToSSL) {
  6643. wolfSSL_SetDevId(ssl, callbacks->devId);
  6644. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  6645. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6646. goto cleanup;
  6647. }
  6648. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  6649. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6650. goto cleanup;
  6651. }
  6652. }
  6653. if (callbacks->ssl_ready)
  6654. callbacks->ssl_ready(ssl);
  6655. #ifdef WOLFSSL_ASYNC_CRYPT
  6656. err = 0; /* Reset error */
  6657. #endif
  6658. do {
  6659. #ifdef WOLFSSL_ASYNC_CRYPT
  6660. if (err == WC_PENDING_E) {
  6661. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6662. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6663. }
  6664. #endif
  6665. ret = wolfSSL_connect(ssl);
  6666. err = wolfSSL_get_error(ssl, ret);
  6667. } while (err == WC_PENDING_E);
  6668. if (ret != WOLFSSL_SUCCESS) {
  6669. char buff[WOLFSSL_MAX_ERROR_SZ];
  6670. fprintf(stderr, "error = %d, %s\n", err,
  6671. wolfSSL_ERR_error_string(err, buff));
  6672. /*err_sys("SSL_connect failed");*/
  6673. }
  6674. else {
  6675. #ifdef WOLFSSL_ASYNC_CRYPT
  6676. err = 0; /* Reset error */
  6677. #endif
  6678. do {
  6679. #ifdef WOLFSSL_ASYNC_CRYPT
  6680. if (err == WC_PENDING_E) {
  6681. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6682. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6683. }
  6684. #endif
  6685. ret = wolfSSL_write(ssl, msg, len);
  6686. err = wolfSSL_get_error(ssl, ret);
  6687. } while (err == WC_PENDING_E);
  6688. if (len != ret)
  6689. goto cleanup;
  6690. #ifdef WOLFSSL_ASYNC_CRYPT
  6691. err = 0; /* Reset error */
  6692. #endif
  6693. do {
  6694. #ifdef WOLFSSL_ASYNC_CRYPT
  6695. if (err == WC_PENDING_E) {
  6696. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6697. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6698. }
  6699. #endif
  6700. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  6701. err = wolfSSL_get_error(ssl, ret);
  6702. } while (err == WC_PENDING_E);
  6703. if (ret > 0) {
  6704. input[ret] = '\0'; /* null term */
  6705. fprintf(stderr, "Server response: %s\n", input);
  6706. }
  6707. ((func_args*)args)->return_code = TEST_SUCCESS;
  6708. }
  6709. if (callbacks->on_result)
  6710. callbacks->on_result(ssl);
  6711. cleanup:
  6712. wolfSSL_free(ssl);
  6713. wolfSSL_CTX_free(ctx);
  6714. CloseSocket(sfd);
  6715. #ifdef WOLFSSL_TIRTOS
  6716. fdCloseSession(Task_self());
  6717. #endif
  6718. }
  6719. #endif /* ENABLE_TLS_CALLBACK_TEST */
  6720. static int test_wolfSSL_read_write(void)
  6721. {
  6722. /* The unit testing for read and write shall happen simultaneously, since
  6723. * one can't do anything with one without the other. (Except for a failure
  6724. * test case.) This function will call all the others that will set up,
  6725. * execute, and report their test findings.
  6726. *
  6727. * Set up the success case first. This function will become the template
  6728. * for the other tests. This should eventually be renamed
  6729. *
  6730. * The success case isn't interesting, how can this fail?
  6731. * - Do not give the client context a CA certificate. The connect should
  6732. * fail. Do not need server for this?
  6733. * - Using NULL for the ssl object on server. Do not need client for this.
  6734. * - Using NULL for the ssl object on client. Do not need server for this.
  6735. * - Good ssl objects for client and server. Client write() without server
  6736. * read().
  6737. * - Good ssl objects for client and server. Server write() without client
  6738. * read().
  6739. * - Forgetting the password callback?
  6740. */
  6741. tcp_ready ready;
  6742. func_args client_args;
  6743. func_args server_args;
  6744. THREAD_TYPE serverThread;
  6745. EXPECT_DECLS;
  6746. XMEMSET(&client_args, 0, sizeof(func_args));
  6747. XMEMSET(&server_args, 0, sizeof(func_args));
  6748. #ifdef WOLFSSL_TIRTOS
  6749. fdOpenSession(Task_self());
  6750. #endif
  6751. StartTCP();
  6752. InitTcpReady(&ready);
  6753. #if defined(USE_WINDOWS_API)
  6754. /* use RNG to get random port if using windows */
  6755. ready.port = GetRandomPort();
  6756. #endif
  6757. server_args.signal = &ready;
  6758. client_args.signal = &ready;
  6759. start_thread(test_server_nofail, &server_args, &serverThread);
  6760. wait_tcp_ready(&server_args);
  6761. test_client_nofail(&client_args, NULL);
  6762. join_thread(serverThread);
  6763. ExpectTrue(client_args.return_code);
  6764. ExpectTrue(server_args.return_code);
  6765. FreeTcpReady(&ready);
  6766. #ifdef WOLFSSL_TIRTOS
  6767. fdOpenSession(Task_self());
  6768. #endif
  6769. return EXPECT_RESULT();
  6770. }
  6771. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  6772. {
  6773. EXPECT_DECLS;
  6774. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6775. !defined(WOLFSSL_NO_TLS12)
  6776. /* The unit test for session resumption by re-using WOLFSSL object.
  6777. * WOLFSSL object is not cleared after first session. It reuse the object
  6778. * for second connection.
  6779. */
  6780. tcp_ready ready;
  6781. func_args client_args;
  6782. func_args server_args;
  6783. THREAD_TYPE serverThread;
  6784. callback_functions client_cbf;
  6785. callback_functions server_cbf;
  6786. XMEMSET(&client_args, 0, sizeof(func_args));
  6787. XMEMSET(&server_args, 0, sizeof(func_args));
  6788. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  6789. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6790. #ifdef WOLFSSL_TIRTOS
  6791. fdOpenSession(Task_self());
  6792. #endif
  6793. StartTCP();
  6794. InitTcpReady(&ready);
  6795. #if defined(USE_WINDOWS_API)
  6796. /* use RNG to get random port if using windows */
  6797. ready.port = GetRandomPort();
  6798. #endif
  6799. client_cbf.method = wolfTLSv1_2_client_method;
  6800. server_cbf.method = wolfTLSv1_2_server_method;
  6801. client_args.callbacks = &client_cbf;
  6802. server_args.callbacks = &server_cbf;
  6803. server_args.signal = &ready;
  6804. client_args.signal = &ready;
  6805. /* the var is used for loop number */
  6806. server_args.argc = 2;
  6807. start_thread(test_server_loop, &server_args, &serverThread);
  6808. wait_tcp_ready(&server_args);
  6809. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  6810. join_thread(serverThread);
  6811. ExpectTrue(client_args.return_code);
  6812. ExpectTrue(server_args.return_code);
  6813. FreeTcpReady(&ready);
  6814. #ifdef WOLFSSL_TIRTOS
  6815. fdOpenSession(Task_self());
  6816. #endif
  6817. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  6818. * !defined(WOLFSSL_TLS13) */
  6819. return EXPECT_RESULT();
  6820. }
  6821. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6822. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6823. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready(
  6824. WOLFSSL_CTX* ctx)
  6825. {
  6826. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  6827. myVerifyAction = VERIFY_USE_PREVERFIY;
  6828. wolfSSL_CTX_set_verify_depth(ctx, 2);
  6829. return TEST_SUCCESS;
  6830. }
  6831. #endif
  6832. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1(void)
  6833. {
  6834. EXPECT_DECLS;
  6835. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6836. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6837. test_ssl_cbf client_cbf;
  6838. test_ssl_cbf server_cbf;
  6839. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6840. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6841. #ifdef WOLFSSL_TLS13
  6842. client_cbf.method = wolfTLSv1_3_client_method;
  6843. #endif /* WOLFSSL_TLS13 */
  6844. client_cbf.ctx_ready =
  6845. test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready;
  6846. /* test case 1 verify depth is equal to peer chain */
  6847. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6848. &server_cbf, NULL), TEST_SUCCESS);
  6849. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  6850. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  6851. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  6852. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  6853. return EXPECT_RESULT();
  6854. }
  6855. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6856. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6857. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready(
  6858. WOLFSSL_CTX* ctx)
  6859. {
  6860. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  6861. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  6862. wolfSSL_CTX_set_verify_depth(ctx, 0);
  6863. return TEST_SUCCESS;
  6864. }
  6865. #endif
  6866. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2(void)
  6867. {
  6868. EXPECT_DECLS;
  6869. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6870. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6871. test_ssl_cbf client_cbf;
  6872. test_ssl_cbf server_cbf;
  6873. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6874. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6875. #ifdef WOLFSSL_TLS13
  6876. client_cbf.method = wolfTLSv1_3_client_method;
  6877. #endif /* WOLFSSL_TLS13 */
  6878. client_cbf.ctx_ready =
  6879. test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready;
  6880. /* test case 2
  6881. * verify depth is zero, number of peer's chain is 2.
  6882. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  6883. * callback.
  6884. */
  6885. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6886. &server_cbf, NULL), TEST_SUCCESS);
  6887. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  6888. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  6889. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  6890. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  6891. return EXPECT_RESULT();
  6892. }
  6893. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6894. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6895. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready(
  6896. WOLFSSL_CTX* ctx)
  6897. {
  6898. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  6899. myVerifyAction = VERIFY_USE_PREVERFIY;
  6900. wolfSSL_CTX_set_verify_depth(ctx, 0);
  6901. return TEST_SUCCESS;
  6902. }
  6903. #endif
  6904. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3(void)
  6905. {
  6906. EXPECT_DECLS;
  6907. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6908. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6909. test_ssl_cbf client_cbf;
  6910. test_ssl_cbf server_cbf;
  6911. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6912. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6913. #ifdef WOLFSSL_TLS13
  6914. client_cbf.method = wolfTLSv1_3_client_method;
  6915. #endif /* WOLFSSL_TLS13 */
  6916. client_cbf.ctx_ready =
  6917. test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready;
  6918. /* test case 3
  6919. * verify depth is zero, number of peer's chain is 2
  6920. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  6921. * therefore, handshake becomes failure.
  6922. */
  6923. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6924. &server_cbf, NULL), TEST_FAIL);
  6925. ExpectIntEQ(client_cbf.return_code, TEST_FAIL);
  6926. ExpectIntEQ(server_cbf.return_code, TEST_FAIL);
  6927. ExpectIntEQ(client_cbf.last_err, MAX_CHAIN_ERROR);
  6928. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  6929. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  6930. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  6931. return EXPECT_RESULT();
  6932. }
  6933. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  6934. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  6935. && !defined(NO_SHA256) && defined(HAVE_ECC)
  6936. static int test_wolfSSL_CTX_set_cipher_list_server_ctx_ready(WOLFSSL_CTX* ctx)
  6937. {
  6938. EXPECT_DECLS;
  6939. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "DEFAULT:!NULL"));
  6940. return EXPECT_RESULT();
  6941. }
  6942. static int test_wolfSSL_CTX_set_cipher_list_client_ctx_ready(WOLFSSL_CTX* ctx)
  6943. {
  6944. EXPECT_DECLS;
  6945. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256"));
  6946. return EXPECT_RESULT();
  6947. }
  6948. #endif
  6949. static int test_wolfSSL_CTX_set_cipher_list(void)
  6950. {
  6951. EXPECT_DECLS;
  6952. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  6953. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  6954. && !defined(NO_SHA256) && defined(HAVE_ECC)
  6955. WOLFSSL_CTX* ctxClient = NULL;
  6956. WOLFSSL* sslClient = NULL;
  6957. test_ssl_cbf client_cbf;
  6958. test_ssl_cbf server_cbf;
  6959. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6960. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6961. server_cbf.method = wolfTLSv1_2_server_method;
  6962. server_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_server_ctx_ready;
  6963. client_cbf.method = wolfTLSv1_2_client_method;
  6964. client_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_client_ctx_ready;
  6965. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6966. &server_cbf, NULL), TEST_SUCCESS);
  6967. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  6968. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  6969. /* check with cipher string that has '+' */
  6970. ExpectNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  6971. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE+AESGCM"));
  6972. ExpectNotNull((sslClient = wolfSSL_new(ctxClient)));
  6973. /* check for the existence of an ECDHE ECDSA cipher suite */
  6974. if (EXPECT_SUCCESS()) {
  6975. int i = 0;
  6976. int found = 0;
  6977. const char* suite;
  6978. WOLF_STACK_OF(WOLFSSL_CIPHER)* sk = NULL;
  6979. WOLFSSL_CIPHER* current;
  6980. ExpectNotNull((sk = wolfSSL_get_ciphers_compat(sslClient)));
  6981. do {
  6982. current = wolfSSL_sk_SSL_CIPHER_value(sk, i++);
  6983. if (current) {
  6984. suite = wolfSSL_CIPHER_get_name(current);
  6985. if (suite && XSTRSTR(suite, "ECDSA")) {
  6986. found = 1;
  6987. break;
  6988. }
  6989. }
  6990. } while (current);
  6991. ExpectIntEQ(found, 1);
  6992. }
  6993. wolfSSL_free(sslClient);
  6994. wolfSSL_CTX_free(ctxClient);
  6995. #endif
  6996. return EXPECT_RESULT();
  6997. }
  6998. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  6999. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7000. static int test_wolfSSL_get_finished_client_on_handshake(WOLFSSL_CTX* ctx,
  7001. WOLFSSL* ssl)
  7002. {
  7003. EXPECT_DECLS;
  7004. size_t msg_len;
  7005. (void)ctx;
  7006. /* get_finished test */
  7007. /* 1. get own sent message */
  7008. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  7009. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  7010. ExpectIntGE(msg_len, 0);
  7011. /* 2. get peer message */
  7012. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  7013. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  7014. ExpectIntGE(msg_len, 0);
  7015. return EXPECT_RESULT();
  7016. }
  7017. #endif
  7018. static int test_wolfSSL_get_finished(void)
  7019. {
  7020. EXPECT_DECLS;
  7021. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7022. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7023. test_ssl_cbf client_cbf;
  7024. test_ssl_cbf server_cbf;
  7025. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7026. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7027. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7028. &server_cbf, test_wolfSSL_get_finished_client_on_handshake),
  7029. TEST_SUCCESS);
  7030. /* test received msg vs sent msg */
  7031. ExpectIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  7032. ExpectIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  7033. #endif /* HAVE_SSL_MEMIO_TESTS_DEPENDENCIES && WOLFSSL_HAVE_TLS_UNIQUE */
  7034. return EXPECT_RESULT();
  7035. }
  7036. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7037. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7038. !defined(NO_SESSION_CACHE)
  7039. /* Sessions to restore/store */
  7040. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  7041. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  7042. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  7043. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  7044. {
  7045. /* Don't store sessions. Lookup is still enabled. */
  7046. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7047. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7048. #ifdef OPENSSL_EXTRA
  7049. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7050. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7051. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7052. #endif
  7053. /* Require both peers to provide certs */
  7054. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7055. }
  7056. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  7057. {
  7058. WOLFSSL_SESSION** sess;
  7059. if (wolfSSL_is_server(ssl))
  7060. sess = &test_wolfSSL_CTX_add_session_server_sess;
  7061. else
  7062. sess = &test_wolfSSL_CTX_add_session_client_sess;
  7063. if (*sess == NULL) {
  7064. #ifdef NO_SESSION_CACHE_REF
  7065. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7066. #else
  7067. /* Test for backwards compatibility */
  7068. if (wolfSSL_is_server(ssl)) {
  7069. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7070. }
  7071. else {
  7072. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  7073. }
  7074. #endif
  7075. /* Now save the session in the internal store to make it available
  7076. * for lookup. For TLS 1.3, we can't save the session without
  7077. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  7078. * session from cache. */
  7079. if (wolfSSL_is_server(ssl)
  7080. #ifndef WOLFSSL_TICKET_HAVE_ID
  7081. && wolfSSL_version(ssl) != TLS1_3_VERSION
  7082. #endif
  7083. )
  7084. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  7085. *sess), WOLFSSL_SUCCESS);
  7086. }
  7087. else {
  7088. /* If we have a session retrieved then remaining connections should be
  7089. * resuming on that session */
  7090. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  7091. }
  7092. /* Save CTX to be able to decrypt tickets */
  7093. if (wolfSSL_is_server(ssl) &&
  7094. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  7095. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  7096. = wolfSSL_get_SSL_CTX(ssl));
  7097. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  7098. WOLFSSL_SUCCESS);
  7099. }
  7100. #ifdef SESSION_CERTS
  7101. #ifndef WOLFSSL_TICKET_HAVE_ID
  7102. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7103. wolfSSL_session_reused(ssl))
  7104. #endif
  7105. {
  7106. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  7107. * for all connections. TLS 1.3 only has tickets so if we don't
  7108. * include the session id in the ticket then the certificates
  7109. * will not be available on resumption. */
  7110. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  7111. AssertNotNull(peer);
  7112. wolfSSL_X509_free(peer);
  7113. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7114. #ifdef OPENSSL_EXTRA
  7115. AssertNotNull(SSL_SESSION_get0_peer(*sess));
  7116. #endif
  7117. }
  7118. #endif /* SESSION_CERTS */
  7119. }
  7120. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  7121. {
  7122. /* Set the session to reuse for the client */
  7123. AssertIntEQ(wolfSSL_set_session(ssl,
  7124. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  7125. }
  7126. #endif
  7127. static int test_wolfSSL_CTX_add_session(void)
  7128. {
  7129. EXPECT_DECLS;
  7130. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7131. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7132. !defined(NO_SESSION_CACHE)
  7133. tcp_ready ready;
  7134. func_args client_args;
  7135. func_args server_args;
  7136. THREAD_TYPE serverThread;
  7137. callback_functions client_cb;
  7138. callback_functions server_cb;
  7139. method_provider methods[][2] = {
  7140. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7141. !defined(NO_DES3))
  7142. /* Without AES there are almost no ciphersuites available. This leads
  7143. * to no ciphersuites being available and an error. */
  7144. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  7145. #endif
  7146. #ifndef WOLFSSL_NO_TLS12
  7147. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  7148. #endif
  7149. /* Needs the default ticket callback since it is tied to the
  7150. * connection context and this makes it easy to carry over the ticket
  7151. * crypto context between connections */
  7152. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7153. defined(HAVE_SESSION_TICKET)
  7154. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  7155. #endif
  7156. };
  7157. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  7158. size_t i, j;
  7159. for (i = 0; i < methodsLen; i++) {
  7160. /* First run creates a connection while the second+ run will attempt
  7161. * to resume the connection. The trick is that the internal cache
  7162. * is turned off. wolfSSL_CTX_add_session should put the session in
  7163. * the cache anyway. */
  7164. test_wolfSSL_CTX_add_session_client_sess = NULL;
  7165. test_wolfSSL_CTX_add_session_server_sess = NULL;
  7166. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  7167. #ifdef NO_SESSION_CACHE_REF
  7168. for (j = 0; j < 4; j++) {
  7169. #else
  7170. /* The session may be overwritten in this case. Do only one resumption
  7171. * to stop this test from failing intermittently. */
  7172. for (j = 0; j < 2; j++) {
  7173. #endif
  7174. #ifdef WOLFSSL_TIRTOS
  7175. fdOpenSession(Task_self());
  7176. #endif
  7177. StartTCP();
  7178. InitTcpReady(&ready);
  7179. XMEMSET(&client_args, 0, sizeof(func_args));
  7180. XMEMSET(&server_args, 0, sizeof(func_args));
  7181. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7182. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7183. client_cb.method = methods[i][0];
  7184. server_cb.method = methods[i][1];
  7185. server_args.signal = &ready;
  7186. server_args.callbacks = &server_cb;
  7187. client_args.signal = &ready;
  7188. client_args.callbacks = &client_cb;
  7189. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  7190. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  7191. server_cb.isSharedCtx = 1;
  7192. }
  7193. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  7194. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  7195. if (j != 0)
  7196. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  7197. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  7198. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  7199. server_cb.ticNoInit = 1; /* Use default builtin */
  7200. start_thread(test_server_nofail, &server_args, &serverThread);
  7201. wait_tcp_ready(&server_args);
  7202. test_client_nofail(&client_args, NULL);
  7203. join_thread(serverThread);
  7204. ExpectTrue(client_args.return_code);
  7205. ExpectTrue(server_args.return_code);
  7206. FreeTcpReady(&ready);
  7207. if (EXPECT_FAIL())
  7208. break;
  7209. }
  7210. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  7211. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  7212. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  7213. if (EXPECT_FAIL())
  7214. break;
  7215. }
  7216. #endif
  7217. return EXPECT_RESULT();
  7218. }
  7219. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7220. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7221. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7222. defined(HAVE_SESSION_TICKET) && \
  7223. !defined(TITAN_SESSION_CACHE) && \
  7224. !defined(HUGE_SESSION_CACHE) && \
  7225. !defined(BIG_SESSION_CACHE) && \
  7226. !defined(MEDIUM_SESSION_CACHE)
  7227. /* twcase - prefix for test_wolfSSL_CTX_add_session_ext */
  7228. /* Sessions to restore/store */
  7229. static WOLFSSL_SESSION* twcase_server_first_session_ptr;
  7230. static WOLFSSL_SESSION* twcase_client_first_session_ptr;
  7231. static WOLFSSL_CTX* twcase_server_current_ctx_ptr;
  7232. static int twcase_new_session_called = 0;
  7233. static int twcase_remove_session_called = 0;
  7234. static int twcase_get_session_called = 0;
  7235. /* Test default, SESSIONS_PER_ROW*SESSION_ROWS = 3*11, see ssl.c */
  7236. #define SESSION_CACHE_SIZE 33
  7237. typedef struct {
  7238. const byte* key; /* key, altSessionID, session ID, NULL if empty */
  7239. WOLFSSL_SESSION* value;
  7240. } hashTable_entry;
  7241. typedef struct {
  7242. hashTable_entry entries[SESSION_CACHE_SIZE]; /* hash slots */
  7243. size_t capacity; /* size of entries */
  7244. size_t length; /* number of items in the hash table */
  7245. wolfSSL_Mutex htLock; /* lock */
  7246. }hashTable;
  7247. static hashTable server_sessionCache;
  7248. static int twcase_new_sessionCb(WOLFSSL *ssl, WOLFSSL_SESSION *sess)
  7249. {
  7250. int i;
  7251. unsigned int len;
  7252. (void)ssl;
  7253. /*
  7254. * This example uses a hash table.
  7255. * Steps you should take for a non-demo code:
  7256. * - acquire a lock for the file named according to the session id
  7257. * - open the file
  7258. * - encrypt and write the SSL_SESSION object to the file
  7259. * - release the lock
  7260. *
  7261. * Return:
  7262. * 0: The callback does not wish to hold a reference of the sess
  7263. * 1: The callback wants to hold a reference of the sess. The callback is
  7264. * now also responsible for calling wolfSSL_SESSION_free() on sess.
  7265. */
  7266. if (sess == NULL)
  7267. return 0;
  7268. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  7269. return 0;
  7270. }
  7271. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7272. if (server_sessionCache.entries[i].value == NULL) {
  7273. server_sessionCache.entries[i].key = SSL_SESSION_get_id(sess, &len);
  7274. server_sessionCache.entries[i].value = sess;
  7275. server_sessionCache.length++;
  7276. break;
  7277. }
  7278. }
  7279. ++twcase_new_session_called;
  7280. wc_UnLockMutex(&server_sessionCache.htLock);
  7281. fprintf(stderr, "\t\ttwcase_new_session_called %d\n",
  7282. twcase_new_session_called);
  7283. return 1;
  7284. }
  7285. static void twcase_remove_sessionCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  7286. {
  7287. int i;
  7288. (void)ctx;
  7289. (void)sess;
  7290. if (sess == NULL)
  7291. return;
  7292. /*
  7293. * This example uses a hash table.
  7294. * Steps you should take for a non-demo code:
  7295. * - acquire a lock for the file named according to the session id
  7296. * - remove the file
  7297. * - release the lock
  7298. */
  7299. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  7300. return;
  7301. }
  7302. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7303. if (server_sessionCache.entries[i].key != NULL &&
  7304. XMEMCMP(server_sessionCache.entries[i].key,
  7305. sess->sessionID, SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  7306. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  7307. server_sessionCache.entries[i].value = NULL;
  7308. server_sessionCache.entries[i].key = NULL;
  7309. server_sessionCache.length--;
  7310. break;
  7311. }
  7312. }
  7313. ++twcase_remove_session_called;
  7314. wc_UnLockMutex(&server_sessionCache.htLock);
  7315. fprintf(stderr, "\t\ttwcase_remove_session_called %d\n",
  7316. twcase_remove_session_called);
  7317. }
  7318. static WOLFSSL_SESSION *twcase_get_sessionCb(WOLFSSL *ssl,
  7319. const unsigned char *id, int len, int *ref)
  7320. {
  7321. int i;
  7322. (void)ssl;
  7323. (void)id;
  7324. (void)len;
  7325. /*
  7326. * This example uses a hash table.
  7327. * Steps you should take for a non-demo code:
  7328. * - acquire a lock for the file named according to the session id in the
  7329. * 2nd arg
  7330. * - read and decrypt contents of file and create a new SSL_SESSION
  7331. * - object release the lock
  7332. * - return the new session object
  7333. */
  7334. fprintf(stderr, "\t\ttwcase_get_session_called %d\n",
  7335. ++twcase_get_session_called);
  7336. /* This callback want to retain a copy of the object. If we want wolfSSL to
  7337. * be responsible for the pointer then set to 0. */
  7338. *ref = 1;
  7339. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7340. if (server_sessionCache.entries[i].key != NULL &&
  7341. XMEMCMP(server_sessionCache.entries[i].key, id,
  7342. SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  7343. return server_sessionCache.entries[i].value;
  7344. }
  7345. }
  7346. return NULL;
  7347. }
  7348. static int twcase_get_sessionCb_cleanup(void)
  7349. {
  7350. int i;
  7351. int cnt = 0;
  7352. /* If twcase_get_sessionCb sets *ref = 1, the application is responsible
  7353. * for freeing sessions */
  7354. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7355. if (server_sessionCache.entries[i].value != NULL) {
  7356. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  7357. cnt++;
  7358. }
  7359. }
  7360. fprintf(stderr, "\t\ttwcase_get_sessionCb_cleanup freed %d sessions\n",
  7361. cnt);
  7362. return TEST_SUCCESS;
  7363. }
  7364. static int twcase_cache_intOff_extOff(WOLFSSL_CTX* ctx)
  7365. {
  7366. EXPECT_DECLS;
  7367. /* off - Disable internal cache */
  7368. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7369. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7370. #ifdef OPENSSL_EXTRA
  7371. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7372. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7373. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7374. #endif
  7375. /* off - Do not setup external cache */
  7376. /* Require both peers to provide certs */
  7377. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7378. return EXPECT_RESULT();
  7379. }
  7380. static int twcase_cache_intOn_extOff(WOLFSSL_CTX* ctx)
  7381. {
  7382. /* on - internal cache is on by default */
  7383. /* off - Do not setup external cache */
  7384. /* Require both peers to provide certs */
  7385. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7386. return TEST_SUCCESS;
  7387. }
  7388. static int twcase_cache_intOff_extOn(WOLFSSL_CTX* ctx)
  7389. {
  7390. EXPECT_DECLS;
  7391. /* off - Disable internal cache */
  7392. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7393. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7394. #ifdef OPENSSL_EXTRA
  7395. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7396. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7397. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7398. #endif
  7399. /* on - Enable external cache */
  7400. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  7401. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  7402. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  7403. /* Require both peers to provide certs */
  7404. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7405. return EXPECT_RESULT();
  7406. }
  7407. static int twcase_cache_intOn_extOn(WOLFSSL_CTX* ctx)
  7408. {
  7409. /* on - internal cache is on by default */
  7410. /* on - Enable external cache */
  7411. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  7412. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  7413. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  7414. /* Require both peers to provide certs */
  7415. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7416. return TEST_SUCCESS;
  7417. }
  7418. static int twcase_cache_intOn_extOn_noTicket(WOLFSSL_CTX* ctx)
  7419. {
  7420. /* on - internal cache is on by default */
  7421. /* on - Enable external cache */
  7422. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  7423. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  7424. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  7425. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TICKET);
  7426. /* Require both peers to provide certs */
  7427. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7428. return TEST_SUCCESS;
  7429. }
  7430. static int twcase_server_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  7431. {
  7432. EXPECT_DECLS;
  7433. WOLFSSL_SESSION** sess;
  7434. if (wolfSSL_is_server(ssl))
  7435. sess = &twcase_server_first_session_ptr;
  7436. else
  7437. return TEST_SUCCESS;
  7438. if (*sess == NULL) {
  7439. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  7440. /* Now save the session in the internal store to make it available
  7441. * for lookup. For TLS 1.3, we can't save the session without
  7442. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  7443. * session from cache. */
  7444. if (wolfSSL_is_server(ssl)
  7445. #ifndef WOLFSSL_TICKET_HAVE_ID
  7446. && wolfSSL_version(ssl) != TLS1_3_VERSION
  7447. && wolfSSL_version(ssl) != DTLS1_3_VERSION
  7448. #endif
  7449. ) {
  7450. ExpectIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  7451. *sess), WOLFSSL_SUCCESS);
  7452. }
  7453. }
  7454. /* Save CTX to be able to decrypt tickets */
  7455. if (twcase_server_current_ctx_ptr == NULL) {
  7456. ExpectNotNull(twcase_server_current_ctx_ptr = wolfSSL_get_SSL_CTX(ssl));
  7457. ExpectIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  7458. WOLFSSL_SUCCESS);
  7459. }
  7460. #ifdef SESSION_CERTS
  7461. #ifndef WOLFSSL_TICKET_HAVE_ID
  7462. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7463. wolfSSL_session_reused(ssl))
  7464. #endif
  7465. {
  7466. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  7467. * for all connections. TLS 1.3 only has tickets so if we don't
  7468. * include the session id in the ticket then the certificates
  7469. * will not be available on resumption. */
  7470. WOLFSSL_X509* peer = NULL;
  7471. ExpectNotNull(peer = wolfSSL_get_peer_certificate(ssl));
  7472. wolfSSL_X509_free(peer);
  7473. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7474. }
  7475. #endif
  7476. return EXPECT_RESULT();
  7477. }
  7478. static int twcase_client_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  7479. {
  7480. EXPECT_DECLS;
  7481. WOLFSSL_SESSION** sess;
  7482. sess = &twcase_client_first_session_ptr;
  7483. if (*sess == NULL) {
  7484. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  7485. }
  7486. else {
  7487. /* If we have a session retrieved then remaining connections should be
  7488. * resuming on that session */
  7489. ExpectIntEQ(wolfSSL_session_reused(ssl), 1);
  7490. }
  7491. #ifdef SESSION_CERTS
  7492. #ifndef WOLFSSL_TICKET_HAVE_ID
  7493. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7494. wolfSSL_session_reused(ssl))
  7495. #endif
  7496. {
  7497. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  7498. ExpectNotNull(peer);
  7499. wolfSSL_X509_free(peer);
  7500. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7501. #ifdef OPENSSL_EXTRA
  7502. ExpectNotNull(wolfSSL_SESSION_get0_peer(*sess));
  7503. #endif
  7504. }
  7505. #endif
  7506. return EXPECT_RESULT();
  7507. }
  7508. static int twcase_client_set_sess_ssl_ready(WOLFSSL* ssl)
  7509. {
  7510. EXPECT_DECLS;
  7511. /* Set the session to reuse for the client */
  7512. ExpectNotNull(ssl);
  7513. ExpectNotNull(twcase_client_first_session_ptr);
  7514. ExpectIntEQ(wolfSSL_set_session(ssl,twcase_client_first_session_ptr),
  7515. WOLFSSL_SUCCESS);
  7516. return EXPECT_RESULT();
  7517. }
  7518. struct test_add_session_ext_params {
  7519. method_provider client_meth;
  7520. method_provider server_meth;
  7521. const char* tls_version;
  7522. };
  7523. static int test_wolfSSL_CTX_add_session_ext(
  7524. struct test_add_session_ext_params* param)
  7525. {
  7526. EXPECT_DECLS;
  7527. /* Test the default 33 sessions */
  7528. int j;
  7529. /* Clear cache before starting */
  7530. wolfSSL_CTX_flush_sessions(NULL, -1);
  7531. XMEMSET(&server_sessionCache, 0, sizeof(hashTable));
  7532. if (wc_InitMutex(&server_sessionCache.htLock) != 0)
  7533. return BAD_MUTEX_E;
  7534. server_sessionCache.capacity = SESSION_CACHE_SIZE;
  7535. fprintf(stderr, "\tBegin %s\n", param->tls_version);
  7536. for (j = 0; j < 5; j++) {
  7537. int tls13 = XSTRSTR(param->tls_version, "TLSv1_3") != NULL;
  7538. int dtls = XSTRSTR(param->tls_version, "DTLS") != NULL;
  7539. test_ssl_cbf client_cb;
  7540. test_ssl_cbf server_cb;
  7541. (void)dtls;
  7542. /* Test five cache configurations */
  7543. twcase_client_first_session_ptr = NULL;
  7544. twcase_server_first_session_ptr = NULL;
  7545. twcase_server_current_ctx_ptr = NULL;
  7546. twcase_new_session_called = 0;
  7547. twcase_remove_session_called = 0;
  7548. twcase_get_session_called = 0;
  7549. /* connection 1 - first connection */
  7550. fprintf(stderr, "\tconnect: %s: j=%d\n", param->tls_version, j);
  7551. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7552. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7553. client_cb.method = param->client_meth;
  7554. server_cb.method = param->server_meth;
  7555. if (dtls)
  7556. client_cb.doUdp = server_cb.doUdp = 1;
  7557. /* Setup internal and external cache */
  7558. switch (j) {
  7559. case 0:
  7560. /* SSL_OP_NO_TICKET stateful ticket case */
  7561. server_cb.ctx_ready = twcase_cache_intOn_extOn_noTicket;
  7562. break;
  7563. case 1:
  7564. server_cb.ctx_ready = twcase_cache_intOn_extOn;
  7565. break;
  7566. case 2:
  7567. server_cb.ctx_ready = twcase_cache_intOff_extOn;
  7568. break;
  7569. case 3:
  7570. server_cb.ctx_ready = twcase_cache_intOn_extOff;
  7571. break;
  7572. case 4:
  7573. server_cb.ctx_ready = twcase_cache_intOff_extOff;
  7574. break;
  7575. }
  7576. client_cb.ctx_ready = twcase_cache_intOff_extOff;
  7577. /* Add session to internal cache and save SSL session for testing */
  7578. server_cb.on_result = twcase_server_sess_ctx_pre_shutdown;
  7579. /* Save client SSL session for testing */
  7580. client_cb.on_result = twcase_client_sess_ctx_pre_shutdown;
  7581. server_cb.ticNoInit = 1; /* Use default builtin */
  7582. /* Don't free/release ctx */
  7583. server_cb.ctx = twcase_server_current_ctx_ptr;
  7584. server_cb.isSharedCtx = 1;
  7585. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  7586. &server_cb, NULL), TEST_SUCCESS);
  7587. ExpectIntEQ(twcase_get_session_called, 0);
  7588. if (EXPECT_FAIL()) {
  7589. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  7590. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  7591. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  7592. break;
  7593. }
  7594. switch (j) {
  7595. case 0:
  7596. case 1:
  7597. case 2:
  7598. /* cache cannot be searched with out a connection */
  7599. /* Add a new session */
  7600. ExpectIntEQ(twcase_new_session_called, 1);
  7601. /* In twcase_server_sess_ctx_pre_shutdown
  7602. * wolfSSL_CTX_add_session which evicts the existing session
  7603. * in cache and adds it back in */
  7604. ExpectIntLE(twcase_remove_session_called, 1);
  7605. break;
  7606. case 3:
  7607. case 4:
  7608. /* no external cache */
  7609. ExpectIntEQ(twcase_new_session_called, 0);
  7610. ExpectIntEQ(twcase_remove_session_called, 0);
  7611. break;
  7612. }
  7613. /* connection 2 - session resume */
  7614. fprintf(stderr, "\tresume: %s: j=%d\n", param->tls_version, j);
  7615. twcase_new_session_called = 0;
  7616. twcase_remove_session_called = 0;
  7617. twcase_get_session_called = 0;
  7618. server_cb.on_result = 0;
  7619. client_cb.on_result = 0;
  7620. server_cb.ticNoInit = 1; /* Use default builtin */
  7621. server_cb.ctx = twcase_server_current_ctx_ptr;
  7622. /* try session resumption */
  7623. client_cb.ssl_ready = twcase_client_set_sess_ssl_ready;
  7624. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  7625. &server_cb, NULL), TEST_SUCCESS);
  7626. /* Clear cache before checking */
  7627. wolfSSL_CTX_flush_sessions(NULL, -1);
  7628. switch (j) {
  7629. case 0:
  7630. if (tls13) {
  7631. /* (D)TLSv1.3 stateful case */
  7632. /* cache hit */
  7633. /* DTLS accesses cache once for stateless parsing and
  7634. * once for stateful parsing */
  7635. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7636. /* (D)TLSv1.3 creates a new ticket,
  7637. * updates both internal and external cache */
  7638. ExpectIntEQ(twcase_new_session_called, 1);
  7639. /* A new session ID is created for a new ticket */
  7640. ExpectIntEQ(twcase_remove_session_called, 2);
  7641. }
  7642. else {
  7643. /* non (D)TLSv1.3 case, no update */
  7644. /* DTLS accesses cache once for stateless parsing and
  7645. * once for stateful parsing */
  7646. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  7647. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7648. #else
  7649. ExpectIntEQ(twcase_get_session_called, 1);
  7650. #endif
  7651. ExpectIntEQ(twcase_new_session_called, 0);
  7652. /* Called on session added in
  7653. * twcase_server_sess_ctx_pre_shutdown */
  7654. ExpectIntEQ(twcase_remove_session_called, 1);
  7655. }
  7656. break;
  7657. case 1:
  7658. if (tls13) {
  7659. /* (D)TLSv1.3 case */
  7660. /* cache hit */
  7661. ExpectIntEQ(twcase_get_session_called, 1);
  7662. /* (D)TLSv1.3 creates a new ticket,
  7663. * updates both internal and external cache */
  7664. ExpectIntEQ(twcase_new_session_called, 1);
  7665. /* Called on session added in
  7666. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  7667. ExpectIntEQ(twcase_remove_session_called, 1);
  7668. }
  7669. else {
  7670. /* non (D)TLSv1.3 case */
  7671. /* cache hit */
  7672. /* DTLS accesses cache once for stateless parsing and
  7673. * once for stateful parsing */
  7674. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  7675. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7676. #else
  7677. ExpectIntEQ(twcase_get_session_called, 1);
  7678. #endif
  7679. ExpectIntEQ(twcase_new_session_called, 0);
  7680. /* Called on session added in
  7681. * twcase_server_sess_ctx_pre_shutdown */
  7682. ExpectIntEQ(twcase_remove_session_called, 1);
  7683. }
  7684. break;
  7685. case 2:
  7686. if (tls13) {
  7687. /* (D)TLSv1.3 case */
  7688. /* cache hit */
  7689. ExpectIntEQ(twcase_get_session_called, 1);
  7690. /* (D)TLSv1.3 creates a new ticket,
  7691. * updates both internal and external cache */
  7692. ExpectIntEQ(twcase_new_session_called, 1);
  7693. /* Called on session added in
  7694. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  7695. ExpectIntEQ(twcase_remove_session_called, 1);
  7696. }
  7697. else {
  7698. /* non (D)TLSv1.3 case */
  7699. /* cache hit */
  7700. /* DTLS accesses cache once for stateless parsing and
  7701. * once for stateful parsing */
  7702. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  7703. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7704. #else
  7705. ExpectIntEQ(twcase_get_session_called, 1);
  7706. #endif
  7707. ExpectIntEQ(twcase_new_session_called, 0);
  7708. /* Called on session added in
  7709. * twcase_server_sess_ctx_pre_shutdown */
  7710. ExpectIntEQ(twcase_remove_session_called, 1);
  7711. }
  7712. break;
  7713. case 3:
  7714. case 4:
  7715. /* no external cache */
  7716. ExpectIntEQ(twcase_get_session_called, 0);
  7717. ExpectIntEQ(twcase_new_session_called, 0);
  7718. ExpectIntEQ(twcase_remove_session_called, 0);
  7719. break;
  7720. }
  7721. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  7722. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  7723. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  7724. if (EXPECT_FAIL())
  7725. break;
  7726. }
  7727. twcase_get_sessionCb_cleanup();
  7728. XMEMSET(&server_sessionCache.entries, 0,
  7729. sizeof(server_sessionCache.entries));
  7730. fprintf(stderr, "\tEnd %s\n", param->tls_version);
  7731. wc_FreeMutex(&server_sessionCache.htLock);
  7732. return EXPECT_RESULT();
  7733. }
  7734. #endif
  7735. static int test_wolfSSL_CTX_add_session_ext_tls13(void)
  7736. {
  7737. EXPECT_DECLS;
  7738. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7739. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7740. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7741. defined(HAVE_SESSION_TICKET) && \
  7742. !defined(TITAN_SESSION_CACHE) && \
  7743. !defined(HUGE_SESSION_CACHE) && \
  7744. !defined(BIG_SESSION_CACHE) && \
  7745. !defined(MEDIUM_SESSION_CACHE)
  7746. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7747. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  7748. struct test_add_session_ext_params param[1] = {
  7749. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" }
  7750. };
  7751. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7752. #endif
  7753. #endif
  7754. return EXPECT_RESULT();
  7755. }
  7756. static int test_wolfSSL_CTX_add_session_ext_dtls13(void)
  7757. {
  7758. EXPECT_DECLS;
  7759. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7760. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7761. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7762. defined(HAVE_SESSION_TICKET) && \
  7763. !defined(TITAN_SESSION_CACHE) && \
  7764. !defined(HUGE_SESSION_CACHE) && \
  7765. !defined(BIG_SESSION_CACHE) && \
  7766. !defined(MEDIUM_SESSION_CACHE)
  7767. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7768. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  7769. #ifdef WOLFSSL_DTLS13
  7770. struct test_add_session_ext_params param[1] = {
  7771. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" }
  7772. };
  7773. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7774. #endif
  7775. #endif
  7776. #endif
  7777. return EXPECT_RESULT();
  7778. }
  7779. static int test_wolfSSL_CTX_add_session_ext_tls12(void)
  7780. {
  7781. EXPECT_DECLS;
  7782. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7783. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7784. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7785. defined(HAVE_SESSION_TICKET) && \
  7786. !defined(TITAN_SESSION_CACHE) && \
  7787. !defined(HUGE_SESSION_CACHE) && \
  7788. !defined(BIG_SESSION_CACHE) && \
  7789. !defined(MEDIUM_SESSION_CACHE)
  7790. #ifndef WOLFSSL_NO_TLS12
  7791. struct test_add_session_ext_params param[1] = {
  7792. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" }
  7793. };
  7794. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7795. #endif
  7796. #endif
  7797. return EXPECT_RESULT();
  7798. }
  7799. static int test_wolfSSL_CTX_add_session_ext_dtls12(void)
  7800. {
  7801. EXPECT_DECLS;
  7802. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7803. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7804. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7805. defined(HAVE_SESSION_TICKET) && \
  7806. !defined(TITAN_SESSION_CACHE) && \
  7807. !defined(HUGE_SESSION_CACHE) && \
  7808. !defined(BIG_SESSION_CACHE) && \
  7809. !defined(MEDIUM_SESSION_CACHE)
  7810. #ifndef WOLFSSL_NO_TLS12
  7811. #ifdef WOLFSSL_DTLS
  7812. struct test_add_session_ext_params param[1] = {
  7813. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" }
  7814. };
  7815. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7816. #endif
  7817. #endif
  7818. #endif
  7819. return EXPECT_RESULT();
  7820. }
  7821. static int test_wolfSSL_CTX_add_session_ext_tls11(void)
  7822. {
  7823. EXPECT_DECLS;
  7824. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7825. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7826. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7827. defined(HAVE_SESSION_TICKET) && \
  7828. !defined(TITAN_SESSION_CACHE) && \
  7829. !defined(HUGE_SESSION_CACHE) && \
  7830. !defined(BIG_SESSION_CACHE) && \
  7831. !defined(MEDIUM_SESSION_CACHE)
  7832. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7833. !defined(NO_DES3))
  7834. struct test_add_session_ext_params param[1] = {
  7835. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" }
  7836. };
  7837. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7838. #endif
  7839. #endif
  7840. return EXPECT_RESULT();
  7841. }
  7842. static int test_wolfSSL_CTX_add_session_ext_dtls1(void)
  7843. {
  7844. EXPECT_DECLS;
  7845. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7846. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7847. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7848. defined(HAVE_SESSION_TICKET) && \
  7849. !defined(TITAN_SESSION_CACHE) && \
  7850. !defined(HUGE_SESSION_CACHE) && \
  7851. !defined(BIG_SESSION_CACHE) && \
  7852. !defined(MEDIUM_SESSION_CACHE)
  7853. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7854. !defined(NO_DES3))
  7855. #ifdef WOLFSSL_DTLS
  7856. struct test_add_session_ext_params param[1] = {
  7857. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" }
  7858. };
  7859. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7860. #endif
  7861. #endif
  7862. #endif
  7863. return EXPECT_RESULT();
  7864. }
  7865. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  7866. /* canned export of a session using older version 3 */
  7867. static unsigned char version_3[] = {
  7868. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  7869. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  7870. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  7871. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  7872. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  7873. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7874. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  7875. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  7876. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  7877. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7878. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  7879. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  7880. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7881. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  7882. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  7883. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  7884. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  7885. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  7886. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  7887. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  7888. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7889. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7890. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7891. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7892. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7893. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7894. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7895. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7896. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7897. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7898. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7899. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  7900. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  7901. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  7902. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  7903. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  7904. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  7905. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  7906. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  7907. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  7908. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  7909. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  7910. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  7911. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  7912. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7913. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  7914. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  7915. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  7916. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  7917. 0x2E, 0x31, 0xED, 0x4F
  7918. };
  7919. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  7920. static int test_wolfSSL_dtls_export(void)
  7921. {
  7922. EXPECT_DECLS;
  7923. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  7924. tcp_ready ready;
  7925. func_args client_args;
  7926. func_args server_args;
  7927. THREAD_TYPE serverThread;
  7928. callback_functions server_cbf;
  7929. callback_functions client_cbf;
  7930. #ifdef WOLFSSL_TIRTOS
  7931. fdOpenSession(Task_self());
  7932. #endif
  7933. InitTcpReady(&ready);
  7934. #if defined(USE_WINDOWS_API)
  7935. /* use RNG to get random port if using windows */
  7936. ready.port = GetRandomPort();
  7937. #endif
  7938. /* set using dtls */
  7939. XMEMSET(&client_args, 0, sizeof(func_args));
  7940. XMEMSET(&server_args, 0, sizeof(func_args));
  7941. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7942. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  7943. server_cbf.method = wolfDTLSv1_2_server_method;
  7944. client_cbf.method = wolfDTLSv1_2_client_method;
  7945. server_args.callbacks = &server_cbf;
  7946. client_args.callbacks = &client_cbf;
  7947. server_args.signal = &ready;
  7948. client_args.signal = &ready;
  7949. start_thread(run_wolfssl_server, &server_args, &serverThread);
  7950. wait_tcp_ready(&server_args);
  7951. run_wolfssl_client(&client_args);
  7952. join_thread(serverThread);
  7953. ExpectTrue(client_args.return_code);
  7954. ExpectTrue(server_args.return_code);
  7955. FreeTcpReady(&ready);
  7956. #ifdef WOLFSSL_TIRTOS
  7957. fdOpenSession(Task_self());
  7958. #endif
  7959. if (EXPECT_SUCCESS()) {
  7960. SOCKET_T sockfd = 0;
  7961. WOLFSSL_CTX* ctx = NULL;
  7962. WOLFSSL* ssl = NULL;
  7963. char msg[64] = "hello wolfssl!";
  7964. char reply[1024];
  7965. int msgSz = (int)XSTRLEN(msg);
  7966. byte *session, *window;
  7967. unsigned int sessionSz = 0;
  7968. unsigned int windowSz = 0;
  7969. #ifndef TEST_IPV6
  7970. struct sockaddr_in peerAddr;
  7971. #else
  7972. struct sockaddr_in6 peerAddr;
  7973. #endif /* TEST_IPV6 */
  7974. int i;
  7975. /* Set ctx to DTLS 1.2 */
  7976. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  7977. ExpectNotNull(ssl = wolfSSL_new(ctx));
  7978. /* test importing version 3 */
  7979. ExpectIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  7980. /* test importing bad length and bad version */
  7981. version_3[2] += 1;
  7982. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  7983. version_3[2] -= 1; version_3[1] = 0XA0;
  7984. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  7985. wolfSSL_free(ssl);
  7986. wolfSSL_CTX_free(ctx);
  7987. /* check storing client state after connection and storing window only */
  7988. #ifdef WOLFSSL_TIRTOS
  7989. fdOpenSession(Task_self());
  7990. #endif
  7991. InitTcpReady(&ready);
  7992. #if defined(USE_WINDOWS_API)
  7993. /* use RNG to get random port if using windows */
  7994. ready.port = GetRandomPort();
  7995. #endif
  7996. /* set using dtls */
  7997. XMEMSET(&server_args, 0, sizeof(func_args));
  7998. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7999. server_cbf.method = wolfDTLSv1_2_server_method;
  8000. server_cbf.doUdp = 1;
  8001. server_args.callbacks = &server_cbf;
  8002. server_args.argc = 3; /* set loop_count to 3 */
  8003. server_args.signal = &ready;
  8004. start_thread(test_server_nofail, &server_args, &serverThread);
  8005. wait_tcp_ready(&server_args);
  8006. /* create and connect with client */
  8007. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  8008. ExpectIntEQ(WOLFSSL_SUCCESS,
  8009. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  8010. ExpectIntEQ(WOLFSSL_SUCCESS,
  8011. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  8012. ExpectIntEQ(WOLFSSL_SUCCESS,
  8013. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  8014. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  8015. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8016. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8017. /* store server information connected too */
  8018. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  8019. #ifndef TEST_IPV6
  8020. peerAddr.sin_family = AF_INET;
  8021. ExpectIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  8022. peerAddr.sin_port = XHTONS(server_args.signal->port);
  8023. #else
  8024. peerAddr.sin6_family = AF_INET6;
  8025. ExpectIntEQ(
  8026. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  8027. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  8028. #endif
  8029. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8030. WOLFSSL_SUCCESS);
  8031. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  8032. ExpectIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  8033. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8034. ExpectIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  8035. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8036. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8037. ExpectIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  8038. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8039. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8040. wolfSSL_free(ssl);
  8041. for (i = 1; EXPECT_SUCCESS() && i < server_args.argc; i++) {
  8042. /* restore state */
  8043. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8044. ExpectIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  8045. ExpectIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  8046. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8047. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8048. WOLFSSL_SUCCESS);
  8049. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8050. ExpectIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8051. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8052. wolfSSL_free(ssl);
  8053. }
  8054. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8055. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8056. wolfSSL_CTX_free(ctx);
  8057. fprintf(stderr, "done and waiting for server\n");
  8058. join_thread(serverThread);
  8059. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  8060. FreeTcpReady(&ready);
  8061. #ifdef WOLFSSL_TIRTOS
  8062. fdOpenSession(Task_self());
  8063. #endif
  8064. }
  8065. #endif
  8066. return EXPECT_RESULT();
  8067. }
  8068. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  8069. #ifdef WOLFSSL_TLS13
  8070. static const byte canned_client_tls13_session[] = {
  8071. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  8072. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8073. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  8074. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8075. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8076. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8077. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8078. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  8079. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8080. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8081. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8082. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  8083. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8084. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8085. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8086. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8087. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8088. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8089. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8090. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8091. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8092. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8093. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8094. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8095. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8096. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8097. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8098. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8099. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8100. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  8101. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  8102. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  8103. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8104. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8105. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8106. 0x00, 0x03
  8107. };
  8108. static const byte canned_server_tls13_session[] = {
  8109. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  8110. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8111. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  8112. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8113. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8114. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8115. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8116. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  8117. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8118. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8119. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8120. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  8121. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8122. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8123. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8124. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8125. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8126. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8127. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8128. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8129. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8130. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8131. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8132. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8133. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8134. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8135. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8136. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8137. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8138. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  8139. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  8140. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  8141. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8142. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8143. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8144. 0x00, 0x04
  8145. };
  8146. #endif /* WOLFSSL_TLS13 */
  8147. static const byte canned_client_session[] = {
  8148. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8149. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8150. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  8151. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8152. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8153. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8154. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8155. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  8156. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8157. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8158. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8159. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  8160. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  8161. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  8162. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  8163. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  8164. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  8165. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  8166. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  8167. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  8168. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  8169. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  8170. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  8171. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  8172. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  8173. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  8174. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  8175. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  8176. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  8177. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8178. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8179. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8180. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8181. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  8182. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  8183. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  8184. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  8185. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  8186. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  8187. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8188. 0x00, 0x03
  8189. };
  8190. static const byte canned_server_session[] = {
  8191. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8192. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8193. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  8194. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8195. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  8196. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8197. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8198. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  8199. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8200. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8201. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8202. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  8203. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  8204. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  8205. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  8206. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  8207. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  8208. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  8209. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  8210. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  8211. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  8212. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  8213. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  8214. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  8215. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  8216. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  8217. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  8218. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  8219. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  8220. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8221. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8222. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8223. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8224. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  8225. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  8226. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  8227. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  8228. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  8229. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  8230. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8231. 0x00, 0x04
  8232. };
  8233. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  8234. {
  8235. SOCKET_T sockfd = 0;
  8236. SOCKET_T clientfd = 0;
  8237. word16 port;
  8238. callback_functions* cbf;
  8239. WOLFSSL_CTX* ctx = 0;
  8240. WOLFSSL* ssl = 0;
  8241. char msg[] = "I hear you fa shizzle!";
  8242. char input[1024];
  8243. int idx;
  8244. #ifdef WOLFSSL_TIRTOS
  8245. fdOpenSession(Task_self());
  8246. #endif
  8247. ((func_args*)args)->return_code = TEST_FAIL;
  8248. cbf = ((func_args*)args)->callbacks;
  8249. #if defined(USE_WINDOWS_API)
  8250. port = ((func_args*)args)->signal->port;
  8251. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  8252. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  8253. /* Let tcp_listen assign port */
  8254. port = 0;
  8255. #else
  8256. /* Use default port */
  8257. port = wolfSSLPort;
  8258. #endif
  8259. /* do it here to detect failure */
  8260. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  8261. CloseSocket(sockfd);
  8262. {
  8263. WOLFSSL_METHOD* method = NULL;
  8264. if (cbf != NULL && cbf->method != NULL) {
  8265. method = cbf->method();
  8266. }
  8267. else {
  8268. method = wolfTLSv1_2_server_method();
  8269. }
  8270. ctx = wolfSSL_CTX_new(method);
  8271. }
  8272. if (ctx == NULL) {
  8273. goto done;
  8274. }
  8275. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  8276. /* call ctx setup callback */
  8277. if (cbf != NULL && cbf->ctx_ready != NULL) {
  8278. cbf->ctx_ready(ctx);
  8279. }
  8280. ssl = wolfSSL_new(ctx);
  8281. if (ssl == NULL) {
  8282. goto done;
  8283. }
  8284. wolfSSL_set_fd(ssl, clientfd);
  8285. /* call ssl setup callback */
  8286. if (cbf != NULL && cbf->ssl_ready != NULL) {
  8287. cbf->ssl_ready(ssl);
  8288. }
  8289. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  8290. if (idx > 0) {
  8291. input[idx] = '\0';
  8292. fprintf(stderr, "Client message export/import: %s\n", input);
  8293. }
  8294. else {
  8295. fprintf(stderr, "ret = %d error = %d\n", idx,
  8296. wolfSSL_get_error(ssl, idx));
  8297. goto done;
  8298. }
  8299. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  8300. /*err_sys("SSL_write failed");*/
  8301. WOLFSSL_RETURN_FROM_THREAD(0);
  8302. }
  8303. #ifdef WOLFSSL_TIRTOS
  8304. Task_yield();
  8305. #endif
  8306. ((func_args*)args)->return_code = TEST_SUCCESS;
  8307. done:
  8308. wolfSSL_shutdown(ssl);
  8309. wolfSSL_free(ssl);
  8310. wolfSSL_CTX_free(ctx);
  8311. CloseSocket(clientfd);
  8312. #ifdef WOLFSSL_TIRTOS
  8313. fdCloseSession(Task_self());
  8314. #endif
  8315. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  8316. && defined(HAVE_THREAD_LS)
  8317. wc_ecc_fp_free(); /* free per thread cache */
  8318. #endif
  8319. #if defined(HAVE_SESSION_TICKET) && \
  8320. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  8321. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  8322. OpenSSLTicketCleanup();
  8323. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  8324. TicketCleanup();
  8325. #endif
  8326. #endif
  8327. WOLFSSL_RETURN_FROM_THREAD(0);
  8328. }
  8329. static void load_tls12_canned_server(WOLFSSL* ssl)
  8330. {
  8331. int clientfd = wolfSSL_get_fd(ssl);
  8332. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  8333. sizeof(canned_server_session)), sizeof(canned_server_session));
  8334. wolfSSL_set_fd(ssl, clientfd);
  8335. }
  8336. #ifdef WOLFSSL_TLS13
  8337. static void load_tls13_canned_server(WOLFSSL* ssl)
  8338. {
  8339. int clientfd = wolfSSL_get_fd(ssl);
  8340. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  8341. sizeof(canned_server_tls13_session)),
  8342. sizeof(canned_server_tls13_session));
  8343. wolfSSL_set_fd(ssl, clientfd);
  8344. }
  8345. #endif
  8346. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  8347. static int test_wolfSSL_tls_export_run(int v)
  8348. {
  8349. EXPECT_DECLS;
  8350. SOCKET_T sockfd = 0;
  8351. WOLFSSL_CTX* ctx = 0;
  8352. WOLFSSL* ssl = 0;
  8353. char msg[64] = "hello wolfssl!";
  8354. char reply[1024];
  8355. word32 replySz;
  8356. int msgSz = (int)XSTRLEN(msg);
  8357. const byte* clientSession = NULL;
  8358. int clientSessionSz = 0;
  8359. tcp_ready ready;
  8360. func_args server_args;
  8361. THREAD_TYPE serverThread;
  8362. callback_functions server_cbf;
  8363. #ifdef WOLFSSL_TIRTOS
  8364. fdOpenSession(Task_self());
  8365. #endif
  8366. InitTcpReady(&ready);
  8367. #if defined(USE_WINDOWS_API)
  8368. /* use RNG to get random port if using windows */
  8369. ready.port = GetRandomPort();
  8370. #endif
  8371. XMEMSET(&server_args, 0, sizeof(func_args));
  8372. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8373. switch (v) {
  8374. case WOLFSSL_TLSV1_2:
  8375. server_cbf.method = wolfTLSv1_2_server_method;
  8376. server_cbf.ssl_ready = load_tls12_canned_server;
  8377. /* setup the client side */
  8378. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  8379. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  8380. clientSession = canned_client_session;
  8381. clientSessionSz = sizeof(canned_client_session);
  8382. break;
  8383. #ifdef WOLFSSL_TLS13
  8384. case WOLFSSL_TLSV1_3:
  8385. server_cbf.method = wolfTLSv1_3_server_method;
  8386. server_cbf.ssl_ready = load_tls13_canned_server;
  8387. /* setup the client side */
  8388. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  8389. clientSession = canned_client_tls13_session;
  8390. clientSessionSz = sizeof(canned_client_tls13_session);
  8391. break;
  8392. #endif
  8393. }
  8394. server_args.callbacks = &server_cbf;
  8395. server_args.signal = &ready;
  8396. start_thread(tls_export_server, &server_args, &serverThread);
  8397. wait_tcp_ready(&server_args);
  8398. #ifdef WOLFSSL_TIRTOS
  8399. fdOpenSession(Task_self());
  8400. #endif
  8401. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8402. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  8403. ExpectIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  8404. clientSessionSz);
  8405. replySz = sizeof(reply);
  8406. ExpectIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  8407. #if !defined(NO_PSK) && defined(HAVE_ANON)
  8408. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  8409. ExpectIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  8410. #endif
  8411. wolfSSL_set_fd(ssl, sockfd);
  8412. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8413. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  8414. wolfSSL_free(ssl);
  8415. wolfSSL_CTX_free(ctx);
  8416. CloseSocket(sockfd);
  8417. #ifdef WOLFSSL_TIRTOS
  8418. fdCloseSession(Task_self());
  8419. #endif
  8420. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  8421. && defined(HAVE_THREAD_LS)
  8422. wc_ecc_fp_free(); /* free per thread cache */
  8423. #endif
  8424. join_thread(serverThread);
  8425. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  8426. FreeTcpReady(&ready);
  8427. #ifdef WOLFSSL_TIRTOS
  8428. fdOpenSession(Task_self());
  8429. #endif
  8430. return EXPECT_RESULT();
  8431. }
  8432. #endif
  8433. static int test_wolfSSL_tls_export(void)
  8434. {
  8435. int res = TEST_SKIPPED;
  8436. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  8437. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  8438. #ifdef WOLFSSL_TLS13
  8439. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  8440. #endif
  8441. res = TEST_RES_CHECK(1);
  8442. #endif
  8443. return res;
  8444. }
  8445. /*----------------------------------------------------------------------------*
  8446. | TLS extensions tests
  8447. *----------------------------------------------------------------------------*/
  8448. #ifdef ENABLE_TLS_CALLBACK_TEST
  8449. /* Connection test runner - generic */
  8450. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  8451. callback_functions* server_callbacks)
  8452. {
  8453. tcp_ready ready;
  8454. func_args client_args;
  8455. func_args server_args;
  8456. THREAD_TYPE serverThread;
  8457. XMEMSET(&client_args, 0, sizeof(func_args));
  8458. XMEMSET(&server_args, 0, sizeof(func_args));
  8459. StartTCP();
  8460. client_args.callbacks = client_callbacks;
  8461. server_args.callbacks = server_callbacks;
  8462. #ifdef WOLFSSL_TIRTOS
  8463. fdOpenSession(Task_self());
  8464. #endif
  8465. /* RUN Server side */
  8466. InitTcpReady(&ready);
  8467. #if defined(USE_WINDOWS_API)
  8468. /* use RNG to get random port if using windows */
  8469. ready.port = GetRandomPort();
  8470. #endif
  8471. server_args.signal = &ready;
  8472. client_args.signal = &ready;
  8473. start_thread(run_wolfssl_server, &server_args, &serverThread);
  8474. wait_tcp_ready(&server_args);
  8475. /* RUN Client side */
  8476. run_wolfssl_client(&client_args);
  8477. join_thread(serverThread);
  8478. FreeTcpReady(&ready);
  8479. #ifdef WOLFSSL_TIRTOS
  8480. fdCloseSession(Task_self());
  8481. #endif
  8482. client_callbacks->return_code = client_args.return_code;
  8483. server_callbacks->return_code = server_args.return_code;
  8484. }
  8485. #endif /* ENABLE_TLS_CALLBACK_TEST */
  8486. #ifdef HAVE_SNI
  8487. static int test_wolfSSL_UseSNI_params(void)
  8488. {
  8489. EXPECT_DECLS;
  8490. #if !defined(NO_WOLFSSL_CLIENT)
  8491. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8492. WOLFSSL *ssl = wolfSSL_new(ctx);
  8493. ExpectNotNull(ctx);
  8494. ExpectNotNull(ssl);
  8495. /* invalid [ctx|ssl] */
  8496. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  8497. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  8498. /* invalid type */
  8499. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  8500. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  8501. /* invalid data */
  8502. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  8503. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  8504. /* success case */
  8505. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  8506. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  8507. wolfSSL_free(ssl);
  8508. wolfSSL_CTX_free(ctx);
  8509. #endif /* !NO_WOLFSSL_CLIENT */
  8510. return EXPECT_RESULT();
  8511. }
  8512. /* BEGIN of connection tests callbacks */
  8513. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8514. {
  8515. AssertIntEQ(WOLFSSL_SUCCESS,
  8516. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  8517. }
  8518. static void use_SNI_at_ssl(WOLFSSL* ssl)
  8519. {
  8520. AssertIntEQ(WOLFSSL_SUCCESS,
  8521. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  8522. }
  8523. static void different_SNI_at_ssl(WOLFSSL* ssl)
  8524. {
  8525. AssertIntEQ(WOLFSSL_SUCCESS,
  8526. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  8527. }
  8528. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  8529. {
  8530. use_SNI_at_ssl(ssl);
  8531. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8532. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  8533. }
  8534. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  8535. {
  8536. use_SNI_at_ssl(ssl);
  8537. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8538. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  8539. }
  8540. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8541. {
  8542. use_SNI_at_ctx(ctx);
  8543. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  8544. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8545. }
  8546. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  8547. {
  8548. use_SNI_at_ssl(ssl);
  8549. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8550. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8551. }
  8552. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8553. {
  8554. use_SNI_at_ctx(ctx);
  8555. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  8556. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8557. }
  8558. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  8559. {
  8560. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  8561. }
  8562. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  8563. {
  8564. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  8565. }
  8566. static void verify_SNI_no_matching(WOLFSSL* ssl)
  8567. {
  8568. byte type = WOLFSSL_SNI_HOST_NAME;
  8569. void* request = (void*) &type; /* to be overwritten */
  8570. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  8571. AssertNotNull(request);
  8572. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8573. AssertNull(request);
  8574. }
  8575. static void verify_SNI_real_matching(WOLFSSL* ssl)
  8576. {
  8577. byte type = WOLFSSL_SNI_HOST_NAME;
  8578. void* request = NULL;
  8579. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  8580. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8581. AssertNotNull(request);
  8582. AssertStrEQ("www.wolfssl.com", (char*)request);
  8583. }
  8584. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  8585. {
  8586. byte type = WOLFSSL_SNI_HOST_NAME;
  8587. void* request = NULL;
  8588. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  8589. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8590. AssertNotNull(request);
  8591. AssertStrEQ("ww2.wolfssl.com", (char*)request);
  8592. }
  8593. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  8594. {
  8595. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  8596. }
  8597. /* END of connection tests callbacks */
  8598. static int test_wolfSSL_UseSNI_connection(void)
  8599. {
  8600. int res = TEST_SKIPPED;
  8601. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  8602. callback_functions client_cb;
  8603. callback_functions server_cb;
  8604. size_t i;
  8605. #ifdef WOLFSSL_STATIC_MEMORY
  8606. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  8607. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  8608. #endif
  8609. struct {
  8610. method_provider client_meth;
  8611. method_provider server_meth;
  8612. #ifdef WOLFSSL_STATIC_MEMORY
  8613. wolfSSL_method_func client_meth_ex;
  8614. wolfSSL_method_func server_meth_ex;
  8615. #endif
  8616. } methods[] = {
  8617. #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
  8618. {wolfSSLv23_client_method, wolfSSLv23_server_method
  8619. #ifdef WOLFSSL_STATIC_MEMORY
  8620. ,wolfSSLv23_client_method_ex, wolfSSLv23_server_method_ex
  8621. #endif
  8622. },
  8623. #endif
  8624. #ifndef WOLFSSL_NO_TLS12
  8625. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method
  8626. #ifdef WOLFSSL_STATIC_MEMORY
  8627. ,wolfTLSv1_2_client_method_ex, wolfTLSv1_2_server_method_ex
  8628. #endif
  8629. },
  8630. #endif
  8631. #ifdef WOLFSSL_TLS13
  8632. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method
  8633. #ifdef WOLFSSL_STATIC_MEMORY
  8634. ,wolfTLSv1_3_client_method_ex, wolfTLSv1_3_server_method_ex
  8635. #endif
  8636. },
  8637. #endif
  8638. };
  8639. size_t methodsSz = sizeof(methods) / sizeof(*methods);
  8640. for (i = 0; i < methodsSz; i++) {
  8641. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  8642. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  8643. client_cb.method = methods[i].client_meth;
  8644. server_cb.method = methods[i].server_meth;
  8645. client_cb.devId = testDevId;
  8646. server_cb.devId = testDevId;
  8647. #ifdef WOLFSSL_STATIC_MEMORY
  8648. client_cb.method_ex = methods[i].client_meth_ex;
  8649. server_cb.method_ex = methods[i].server_meth_ex;
  8650. client_cb.mem = cliMem;
  8651. client_cb.memSz = (word32)sizeof(cliMem);
  8652. server_cb.mem = svrMem;
  8653. server_cb.memSz = (word32)sizeof(svrMem);;
  8654. #endif
  8655. /* success case at ctx */
  8656. fprintf(stderr, "\n\tsuccess case at ctx\n");
  8657. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8658. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  8659. test_wolfSSL_client_server(&client_cb, &server_cb);
  8660. /* success case at ssl */
  8661. fprintf(stderr, "\tsuccess case at ssl\n");
  8662. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  8663. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  8664. test_wolfSSL_client_server(&client_cb, &server_cb);
  8665. /* default mismatch behavior */
  8666. fprintf(stderr, "\tdefault mismatch behavior\n");
  8667. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  8668. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  8669. test_wolfSSL_client_server(&client_cb, &server_cb);
  8670. /* continue on mismatch */
  8671. fprintf(stderr, "\tcontinue on mismatch\n");
  8672. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  8673. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  8674. test_wolfSSL_client_server(&client_cb, &server_cb);
  8675. /* fake answer on mismatch */
  8676. fprintf(stderr, "\tfake answer on mismatch\n");
  8677. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  8678. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  8679. test_wolfSSL_client_server(&client_cb, &server_cb);
  8680. /* sni abort - success */
  8681. fprintf(stderr, "\tsni abort - success\n");
  8682. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8683. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  8684. test_wolfSSL_client_server(&client_cb, &server_cb);
  8685. /* sni abort - abort when absent (ctx) */
  8686. fprintf(stderr, "\tsni abort - abort when absent (ctx)\n");
  8687. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  8688. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  8689. test_wolfSSL_client_server(&client_cb, &server_cb);
  8690. /* sni abort - abort when absent (ssl) */
  8691. fprintf(stderr, "\tsni abort - abort when absent (ssl)\n");
  8692. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  8693. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  8694. test_wolfSSL_client_server(&client_cb, &server_cb);
  8695. /* sni abort - success when overwritten */
  8696. fprintf(stderr, "\tsni abort - success when overwritten\n");
  8697. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8698. 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;
  8699. test_wolfSSL_client_server(&client_cb, &server_cb);
  8700. /* sni abort - success when allowing mismatches */
  8701. fprintf(stderr, "\tsni abort - success when allowing mismatches\n");
  8702. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  8703. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  8704. test_wolfSSL_client_server(&client_cb, &server_cb);
  8705. }
  8706. res = TEST_RES_CHECK(1);
  8707. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  8708. return res;
  8709. }
  8710. static int test_wolfSSL_SNI_GetFromBuffer(void)
  8711. {
  8712. EXPECT_DECLS;
  8713. byte buff[] = { /* www.paypal.com */
  8714. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  8715. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  8716. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  8717. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  8718. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  8719. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  8720. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  8721. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  8722. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  8723. };
  8724. byte buff2[] = { /* api.textmate.org */
  8725. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  8726. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  8727. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  8728. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  8729. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  8730. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  8731. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  8732. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  8733. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  8734. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  8735. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  8736. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  8737. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  8738. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  8739. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  8740. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  8741. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  8742. };
  8743. byte buff3[] = { /* no sni extension */
  8744. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  8745. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  8746. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  8747. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  8748. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  8749. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  8750. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  8751. };
  8752. byte buff4[] = { /* last extension has zero size */
  8753. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  8754. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  8755. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  8756. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  8757. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  8758. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  8759. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  8760. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  8761. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  8762. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  8763. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  8764. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  8765. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  8766. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  8767. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  8768. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  8769. 0x12, 0x00, 0x00
  8770. };
  8771. byte buff5[] = { /* SSL v2.0 client hello */
  8772. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  8773. /* dummy bytes below, just to pass size check */
  8774. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  8775. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  8776. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  8777. };
  8778. byte result[32] = {0};
  8779. word32 length = 32;
  8780. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  8781. 0, result, &length));
  8782. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  8783. 0, result, &length));
  8784. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  8785. 1, result, &length));
  8786. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  8787. 0, result, &length));
  8788. buff[0] = 0x16;
  8789. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  8790. 0, result, &length));
  8791. buff[1] = 0x03;
  8792. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  8793. sizeof(buff), 0, result, &length));
  8794. buff[2] = 0x03;
  8795. ExpectIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  8796. sizeof(buff), 0, result, &length));
  8797. buff[4] = 0x64;
  8798. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  8799. 0, result, &length));
  8800. if (EXPECT_SUCCESS())
  8801. result[length] = 0;
  8802. ExpectStrEQ("www.paypal.com", (const char*) result);
  8803. length = 32;
  8804. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  8805. 0, result, &length));
  8806. if (EXPECT_SUCCESS())
  8807. result[length] = 0;
  8808. ExpectStrEQ("api.textmate.org", (const char*) result);
  8809. /* SSL v2.0 tests */
  8810. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  8811. sizeof(buff5), 0, result, &length));
  8812. buff5[2] = 0x02;
  8813. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  8814. sizeof(buff5), 0, result, &length));
  8815. buff5[2] = 0x01; buff5[6] = 0x08;
  8816. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  8817. sizeof(buff5), 0, result, &length));
  8818. buff5[6] = 0x09; buff5[8] = 0x01;
  8819. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  8820. sizeof(buff5), 0, result, &length));
  8821. return EXPECT_RESULT();
  8822. }
  8823. #endif /* HAVE_SNI */
  8824. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  8825. static int test_wolfSSL_UseTrustedCA(void)
  8826. {
  8827. EXPECT_DECLS;
  8828. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  8829. && !defined(NO_RSA)
  8830. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8831. WOLFSSL_CTX *ctx = NULL;
  8832. WOLFSSL *ssl = NULL;
  8833. byte id[20];
  8834. #ifndef NO_WOLFSSL_SERVER
  8835. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  8836. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  8837. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  8838. #else
  8839. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  8840. #endif
  8841. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  8842. XMEMSET(id, 0, sizeof(id));
  8843. /* error cases */
  8844. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  8845. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8846. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  8847. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8848. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  8849. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8850. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  8851. #ifdef NO_SHA
  8852. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8853. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  8854. #endif
  8855. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8856. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  8857. /* success cases */
  8858. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8859. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  8860. #ifndef NO_SHA
  8861. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8862. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  8863. #endif
  8864. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8865. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  8866. wolfSSL_free(ssl);
  8867. wolfSSL_CTX_free(ctx);
  8868. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8869. #endif /* HAVE_TRUSTED_CA */
  8870. return EXPECT_RESULT();
  8871. }
  8872. static int test_wolfSSL_UseMaxFragment(void)
  8873. {
  8874. EXPECT_DECLS;
  8875. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  8876. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  8877. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8878. #ifndef NO_WOLFSSL_SERVER
  8879. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  8880. #else
  8881. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8882. #endif
  8883. WOLFSSL *ssl = NULL;
  8884. #ifdef OPENSSL_EXTRA
  8885. int (*UseMaxFragment)(SSL *s, uint8_t mode);
  8886. int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
  8887. #else
  8888. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  8889. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  8890. #endif
  8891. #ifndef NO_WOLFSSL_SERVER
  8892. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  8893. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  8894. #endif
  8895. ExpectNotNull(ctx);
  8896. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8897. #ifdef OPENSSL_EXTRA
  8898. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  8899. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  8900. #else
  8901. UseMaxFragment = wolfSSL_UseMaxFragment;
  8902. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  8903. #endif
  8904. /* error cases */
  8905. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  8906. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  8907. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  8908. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  8909. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  8910. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  8911. /* success case */
  8912. #ifdef OPENSSL_EXTRA
  8913. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  8914. #else
  8915. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  8916. #endif
  8917. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  8918. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  8919. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  8920. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  8921. #ifdef OPENSSL_EXTRA
  8922. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  8923. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  8924. #else
  8925. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  8926. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  8927. #endif
  8928. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  8929. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  8930. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  8931. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  8932. #ifdef OPENSSL_EXTRA
  8933. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  8934. #else
  8935. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  8936. #endif
  8937. wolfSSL_free(ssl);
  8938. wolfSSL_CTX_free(ctx);
  8939. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8940. #endif
  8941. return EXPECT_RESULT();
  8942. }
  8943. static int test_wolfSSL_UseTruncatedHMAC(void)
  8944. {
  8945. EXPECT_DECLS;
  8946. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  8947. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  8948. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8949. #ifndef NO_WOLFSSL_SERVER
  8950. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  8951. #else
  8952. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8953. #endif
  8954. WOLFSSL *ssl = NULL;
  8955. ExpectNotNull(ctx);
  8956. #ifndef NO_WOLFSSL_SERVER
  8957. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  8958. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  8959. #endif
  8960. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8961. /* error cases */
  8962. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  8963. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  8964. /* success case */
  8965. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  8966. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  8967. wolfSSL_free(ssl);
  8968. wolfSSL_CTX_free(ctx);
  8969. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8970. #endif
  8971. return EXPECT_RESULT();
  8972. }
  8973. static int test_wolfSSL_UseSupportedCurve(void)
  8974. {
  8975. EXPECT_DECLS;
  8976. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && \
  8977. !defined(NO_TLS)
  8978. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8979. WOLFSSL *ssl = wolfSSL_new(ctx);
  8980. ExpectNotNull(ctx);
  8981. ExpectNotNull(ssl);
  8982. /* error cases */
  8983. ExpectIntNE(WOLFSSL_SUCCESS,
  8984. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  8985. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  8986. ExpectIntNE(WOLFSSL_SUCCESS,
  8987. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  8988. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  8989. /* success case */
  8990. ExpectIntEQ(WOLFSSL_SUCCESS,
  8991. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  8992. ExpectIntEQ(WOLFSSL_SUCCESS,
  8993. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  8994. wolfSSL_free(ssl);
  8995. wolfSSL_CTX_free(ctx);
  8996. #endif
  8997. return EXPECT_RESULT();
  8998. }
  8999. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  9000. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  9001. {
  9002. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  9003. }
  9004. static void use_ALPN_all(WOLFSSL* ssl)
  9005. {
  9006. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9007. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9008. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9009. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9010. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9011. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9012. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9013. }
  9014. static void use_ALPN_all_continue(WOLFSSL* ssl)
  9015. {
  9016. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9017. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9018. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9019. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9020. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9021. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9022. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9023. }
  9024. static void use_ALPN_one(WOLFSSL* ssl)
  9025. {
  9026. /* spdy/2 */
  9027. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9028. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9029. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9030. }
  9031. static void use_ALPN_unknown(WOLFSSL* ssl)
  9032. {
  9033. /* http/2.0 */
  9034. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9035. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9036. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9037. }
  9038. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  9039. {
  9040. /* http/2.0 */
  9041. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9042. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9043. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9044. }
  9045. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  9046. {
  9047. /* spdy/3 */
  9048. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9049. char *proto = NULL;
  9050. word16 protoSz = 0;
  9051. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9052. /* check value */
  9053. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  9054. if (proto) {
  9055. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  9056. }
  9057. }
  9058. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  9059. {
  9060. char *proto = NULL;
  9061. word16 protoSz = 0;
  9062. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  9063. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9064. /* check value */
  9065. AssertIntEQ(1, (0 == protoSz));
  9066. AssertIntEQ(1, (NULL == proto));
  9067. }
  9068. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  9069. {
  9070. /* http/1.1 */
  9071. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9072. char *proto;
  9073. word16 protoSz = 0;
  9074. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9075. /* check value */
  9076. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9077. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9078. }
  9079. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  9080. {
  9081. /* spdy/2 */
  9082. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9083. char *proto;
  9084. word16 protoSz = 0;
  9085. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9086. /* check value */
  9087. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9088. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9089. }
  9090. static void verify_ALPN_client_list(WOLFSSL* ssl)
  9091. {
  9092. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9093. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9094. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9095. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9096. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9097. char *clist = NULL;
  9098. word16 clistSz = 0;
  9099. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  9100. &clistSz));
  9101. /* check value */
  9102. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  9103. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  9104. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  9105. }
  9106. static int test_wolfSSL_UseALPN_connection(void)
  9107. {
  9108. int res = TEST_SKIPPED;
  9109. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9110. callback_functions client_cb;
  9111. callback_functions server_cb;
  9112. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9113. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9114. client_cb.method = wolfSSLv23_client_method;
  9115. server_cb.method = wolfSSLv23_server_method;
  9116. client_cb.devId = testDevId;
  9117. server_cb.devId = testDevId;
  9118. /* success case same list */
  9119. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9120. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  9121. test_wolfSSL_client_server(&client_cb, &server_cb);
  9122. /* success case only one for server */
  9123. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9124. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  9125. test_wolfSSL_client_server(&client_cb, &server_cb);
  9126. /* success case only one for client */
  9127. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  9128. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  9129. test_wolfSSL_client_server(&client_cb, &server_cb);
  9130. /* success case none for client */
  9131. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9132. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  9133. test_wolfSSL_client_server(&client_cb, &server_cb);
  9134. /* success case mismatch behavior but option 'continue' set */
  9135. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  9136. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  9137. test_wolfSSL_client_server(&client_cb, &server_cb);
  9138. /* success case read protocol send by client */
  9139. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9140. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  9141. test_wolfSSL_client_server(&client_cb, &server_cb);
  9142. /* mismatch behavior with same list
  9143. * the first and only this one must be taken */
  9144. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9145. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  9146. test_wolfSSL_client_server(&client_cb, &server_cb);
  9147. /* default mismatch behavior */
  9148. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9149. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  9150. test_wolfSSL_client_server(&client_cb, &server_cb);
  9151. res = TEST_RES_CHECK(1);
  9152. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9153. return res;
  9154. }
  9155. static int test_wolfSSL_UseALPN_params(void)
  9156. {
  9157. EXPECT_DECLS;
  9158. #ifndef NO_WOLFSSL_CLIENT
  9159. /* "http/1.1" */
  9160. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9161. /* "spdy/1" */
  9162. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  9163. /* "spdy/2" */
  9164. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9165. /* "spdy/3" */
  9166. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9167. char buff[256];
  9168. word32 idx;
  9169. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9170. WOLFSSL *ssl = wolfSSL_new(ctx);
  9171. ExpectNotNull(ctx);
  9172. ExpectNotNull(ssl);
  9173. /* error cases */
  9174. ExpectIntNE(WOLFSSL_SUCCESS,
  9175. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  9176. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9177. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  9178. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9179. /* success case */
  9180. /* http1 only */
  9181. ExpectIntEQ(WOLFSSL_SUCCESS,
  9182. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  9183. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9184. /* http1, spdy1 */
  9185. XMEMCPY(buff, http1, sizeof(http1));
  9186. idx = sizeof(http1);
  9187. buff[idx++] = ',';
  9188. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  9189. idx += sizeof(spdy1);
  9190. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  9191. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9192. /* http1, spdy2, spdy1 */
  9193. XMEMCPY(buff, http1, sizeof(http1));
  9194. idx = sizeof(http1);
  9195. buff[idx++] = ',';
  9196. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  9197. idx += sizeof(spdy2);
  9198. buff[idx++] = ',';
  9199. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  9200. idx += sizeof(spdy1);
  9201. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  9202. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9203. /* spdy3, http1, spdy2, spdy1 */
  9204. XMEMCPY(buff, spdy3, sizeof(spdy3));
  9205. idx = sizeof(spdy3);
  9206. buff[idx++] = ',';
  9207. XMEMCPY(buff+idx, http1, sizeof(http1));
  9208. idx += sizeof(http1);
  9209. buff[idx++] = ',';
  9210. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  9211. idx += sizeof(spdy2);
  9212. buff[idx++] = ',';
  9213. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  9214. idx += sizeof(spdy1);
  9215. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  9216. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9217. wolfSSL_free(ssl);
  9218. wolfSSL_CTX_free(ctx);
  9219. #endif
  9220. return EXPECT_RESULT();
  9221. }
  9222. #endif /* HAVE_ALPN */
  9223. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  9224. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  9225. {
  9226. unsigned char p[] = {
  9227. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  9228. 6, 's', 'p', 'd', 'y', '/', '2',
  9229. 6, 's', 'p', 'd', 'y', '/', '1',
  9230. };
  9231. unsigned char p_len = sizeof(p);
  9232. int ret;
  9233. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  9234. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  9235. AssertIntEQ(ret, 0);
  9236. #else
  9237. AssertIntEQ(ret, SSL_SUCCESS);
  9238. #endif
  9239. }
  9240. static void set_alpn_protos(SSL* ssl)
  9241. {
  9242. unsigned char p[] = {
  9243. 6, 's', 'p', 'd', 'y', '/', '3',
  9244. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  9245. 6, 's', 'p', 'd', 'y', '/', '2',
  9246. 6, 's', 'p', 'd', 'y', '/', '1',
  9247. };
  9248. unsigned char p_len = sizeof(p);
  9249. int ret;
  9250. ret = SSL_set_alpn_protos(ssl, p, p_len);
  9251. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  9252. AssertIntEQ(ret, 0);
  9253. #else
  9254. AssertIntEQ(ret, SSL_SUCCESS);
  9255. #endif
  9256. }
  9257. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  9258. {
  9259. /* "spdy/3" */
  9260. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9261. const unsigned char *proto;
  9262. unsigned int protoSz = 0;
  9263. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  9264. /* check value */
  9265. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9266. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9267. }
  9268. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  9269. {
  9270. /* "http/1.1" */
  9271. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9272. const unsigned char *proto;
  9273. unsigned int protoSz = 0;
  9274. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  9275. /* check value */
  9276. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9277. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9278. }
  9279. static int test_wolfSSL_set_alpn_protos(void)
  9280. {
  9281. int res = TEST_SKIPPED;
  9282. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9283. callback_functions client_cb;
  9284. callback_functions server_cb;
  9285. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9286. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9287. client_cb.method = wolfSSLv23_client_method;
  9288. server_cb.method = wolfSSLv23_server_method;
  9289. client_cb.devId = testDevId;
  9290. server_cb.devId = testDevId;
  9291. /* use CTX_alpn_protos */
  9292. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9293. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  9294. test_wolfSSL_client_server(&client_cb, &server_cb);
  9295. /* use set_alpn_protos */
  9296. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  9297. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  9298. test_wolfSSL_client_server(&client_cb, &server_cb);
  9299. res = TEST_SUCCESS;
  9300. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9301. return res;
  9302. }
  9303. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  9304. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  9305. {
  9306. EXPECT_DECLS;
  9307. #if defined(HAVE_EXTENDED_MASTER) && !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. /* error cases */
  9313. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  9314. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  9315. /* success cases */
  9316. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  9317. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  9318. wolfSSL_free(ssl);
  9319. wolfSSL_CTX_free(ctx);
  9320. #endif
  9321. return EXPECT_RESULT();
  9322. }
  9323. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  9324. {
  9325. EXPECT_DECLS;
  9326. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  9327. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9328. WOLFSSL *ssl = wolfSSL_new(ctx);
  9329. ExpectNotNull(ctx);
  9330. ExpectNotNull(ssl);
  9331. /* error cases */
  9332. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  9333. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  9334. /* success cases */
  9335. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  9336. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  9337. wolfSSL_free(ssl);
  9338. wolfSSL_CTX_free(ctx);
  9339. #endif
  9340. return EXPECT_RESULT();
  9341. }
  9342. /* Test reconnecting with a different ciphersuite after a renegotiation. */
  9343. static int test_wolfSSL_SCR_Reconnect(void)
  9344. {
  9345. EXPECT_DECLS;
  9346. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  9347. defined(BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) && \
  9348. defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) && \
  9349. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  9350. struct test_memio_ctx test_ctx;
  9351. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  9352. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  9353. byte data;
  9354. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  9355. test_ctx.c_ciphers = "ECDHE-RSA-AES256-GCM-SHA384";
  9356. test_ctx.s_ciphers =
  9357. "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305";
  9358. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  9359. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  9360. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_c));
  9361. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_s));
  9362. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_c));
  9363. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_s));
  9364. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9365. /* WOLFSSL_FATAL_ERROR since it will block */
  9366. ExpectIntEQ(wolfSSL_Rehandshake(ssl_s), WOLFSSL_FATAL_ERROR);
  9367. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  9368. WOLFSSL_ERROR_WANT_READ);
  9369. ExpectIntEQ(wolfSSL_read(ssl_c, &data, 1), WOLFSSL_FATAL_ERROR);
  9370. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  9371. WOLFSSL_ERROR_WANT_READ);
  9372. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9373. wolfSSL_free(ssl_c);
  9374. ssl_c = NULL;
  9375. wolfSSL_free(ssl_s);
  9376. ssl_s = NULL;
  9377. wolfSSL_CTX_free(ctx_c);
  9378. ctx_c = NULL;
  9379. test_ctx.c_ciphers = "ECDHE-RSA-CHACHA20-POLY1305";
  9380. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  9381. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  9382. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9383. wolfSSL_free(ssl_s);
  9384. wolfSSL_free(ssl_c);
  9385. wolfSSL_CTX_free(ctx_s);
  9386. wolfSSL_CTX_free(ctx_c);
  9387. #endif
  9388. return EXPECT_RESULT();
  9389. }
  9390. #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_SERVER) && \
  9391. (!defined(NO_RSA) || defined(HAVE_ECC))
  9392. /* Called when writing. */
  9393. static int DummySend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  9394. {
  9395. (void)ssl;
  9396. (void)buf;
  9397. (void)sz;
  9398. (void)ctx;
  9399. /* Force error return from wolfSSL_accept_TLSv13(). */
  9400. return WANT_WRITE;
  9401. }
  9402. /* Called when reading. */
  9403. static int BufferInfoRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  9404. {
  9405. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  9406. int len = (int)msg->length;
  9407. (void)ssl;
  9408. (void)sz;
  9409. /* Pass back as much of message as will fit in buffer. */
  9410. if (len > sz)
  9411. len = sz;
  9412. XMEMCPY(buf, msg->buffer, len);
  9413. /* Move over returned data. */
  9414. msg->buffer += len;
  9415. msg->length -= len;
  9416. /* Amount actually copied. */
  9417. return len;
  9418. }
  9419. #endif
  9420. /* Test the detection of duplicate known TLS extensions.
  9421. * Specifically in a ClientHello.
  9422. */
  9423. static int test_tls_ext_duplicate(void)
  9424. {
  9425. EXPECT_DECLS;
  9426. #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC)) && \
  9427. !defined(NO_FILESYSTEM)
  9428. const unsigned char clientHelloDupTlsExt[] = {
  9429. 0x16, 0x03, 0x03, 0x00, 0x6a, 0x01, 0x00, 0x00,
  9430. 0x66, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  9431. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  9432. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  9433. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  9434. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  9435. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  9436. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  9437. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9438. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x13, 0x01,
  9439. 0x00, 0x9e, 0x01, 0x00,
  9440. /* Extensions - duplicate signature algorithms. */
  9441. 0x00, 0x19, 0x00, 0x0d,
  9442. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01, 0x00, 0x0d,
  9443. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01,
  9444. /* Supported Versions extension for TLS 1.3. */
  9445. 0x00, 0x2b,
  9446. 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03
  9447. };
  9448. WOLFSSL_BUFFER_INFO msg;
  9449. const char* testCertFile;
  9450. const char* testKeyFile;
  9451. WOLFSSL_CTX *ctx = NULL;
  9452. WOLFSSL *ssl = NULL;
  9453. #ifndef NO_RSA
  9454. testCertFile = svrCertFile;
  9455. testKeyFile = svrKeyFile;
  9456. #elif defined(HAVE_ECC)
  9457. testCertFile = eccCertFile;
  9458. testKeyFile = eccKeyFile;
  9459. #endif
  9460. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9461. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  9462. WOLFSSL_FILETYPE_PEM));
  9463. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  9464. WOLFSSL_FILETYPE_PEM));
  9465. /* Read from 'msg'. */
  9466. wolfSSL_SetIORecv(ctx, BufferInfoRecv);
  9467. /* No where to send to - dummy sender. */
  9468. wolfSSL_SetIOSend(ctx, DummySend);
  9469. ssl = wolfSSL_new(ctx);
  9470. ExpectNotNull(ssl);
  9471. msg.buffer = (unsigned char*)clientHelloDupTlsExt;
  9472. msg.length = (unsigned int)sizeof(clientHelloDupTlsExt);
  9473. wolfSSL_SetIOReadCtx(ssl, &msg);
  9474. ExpectIntNE(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  9475. /* can return duplicate ext error or socket error if the peer closed down
  9476. * while sending alert */
  9477. if (wolfSSL_get_error(ssl, 0) != SOCKET_ERROR_E) {
  9478. ExpectIntEQ(wolfSSL_get_error(ssl, 0), DUPLICATE_TLS_EXT_E);
  9479. }
  9480. wolfSSL_free(ssl);
  9481. wolfSSL_CTX_free(ctx);
  9482. #endif
  9483. return EXPECT_RESULT();
  9484. }
  9485. /*----------------------------------------------------------------------------*
  9486. | X509 Tests
  9487. *----------------------------------------------------------------------------*/
  9488. static int test_wolfSSL_X509_NAME_get_entry(void)
  9489. {
  9490. EXPECT_DECLS;
  9491. #if !defined(NO_CERTS) && !defined(NO_RSA)
  9492. #if defined(OPENSSL_ALL) || \
  9493. (defined(OPENSSL_EXTRA) && \
  9494. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  9495. /* use openssl like name to test mapping */
  9496. X509_NAME_ENTRY* ne;
  9497. X509_NAME* name;
  9498. X509* x509 = NULL;
  9499. #ifndef NO_FILESYSTEM
  9500. ASN1_STRING* asn;
  9501. char* subCN = NULL;
  9502. #endif
  9503. int idx;
  9504. ASN1_OBJECT *object = NULL;
  9505. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  9506. defined(WOLFSSL_NGINX)
  9507. #ifndef NO_BIO
  9508. BIO* bio = NULL;
  9509. #endif
  9510. #endif
  9511. #ifndef NO_FILESYSTEM
  9512. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  9513. WOLFSSL_FILETYPE_PEM));
  9514. ExpectNotNull(name = X509_get_subject_name(x509));
  9515. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  9516. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  9517. ExpectNotNull(asn = X509_NAME_ENTRY_get_data(ne));
  9518. ExpectNotNull(subCN = (char*)ASN1_STRING_data(asn));
  9519. wolfSSL_FreeX509(x509);
  9520. x509 = NULL;
  9521. #endif
  9522. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  9523. WOLFSSL_FILETYPE_PEM));
  9524. ExpectNotNull(name = X509_get_subject_name(x509));
  9525. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  9526. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  9527. defined(WOLFSSL_NGINX)
  9528. #ifndef NO_BIO
  9529. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  9530. ExpectIntEQ(X509_NAME_print_ex(bio, name, 4,
  9531. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  9532. ExpectIntEQ(X509_NAME_print_ex_fp(stderr, name, 4,
  9533. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  9534. BIO_free(bio);
  9535. #endif
  9536. #endif
  9537. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  9538. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  9539. wolfSSL_FreeX509(x509);
  9540. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  9541. #endif /* !NO_CERTS && !NO_RSA */
  9542. return EXPECT_RESULT();
  9543. }
  9544. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  9545. static int test_wolfSSL_PKCS12(void)
  9546. {
  9547. EXPECT_DECLS;
  9548. /* .p12 file is encrypted with DES3 */
  9549. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  9550. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  9551. * Password Key
  9552. */
  9553. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  9554. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  9555. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  9556. byte buf[6000];
  9557. char file[] = "./certs/test-servercert.p12";
  9558. char order[] = "./certs/ecc-rsa-server.p12";
  9559. #ifdef WC_RC2
  9560. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  9561. #endif
  9562. char pass[] = "a password";
  9563. const char goodPsw[] = "wolfSSL test";
  9564. const char badPsw[] = "bad";
  9565. #ifdef HAVE_ECC
  9566. WOLFSSL_X509_NAME *subject = NULL;
  9567. WOLFSSL_X509 *x509 = NULL;
  9568. #endif
  9569. XFILE f = XBADFILE;
  9570. int bytes, ret, goodPswLen, badPswLen;
  9571. WOLFSSL_BIO *bio = NULL;
  9572. WOLFSSL_EVP_PKEY *pkey = NULL;
  9573. WC_PKCS12 *pkcs12 = NULL;
  9574. WC_PKCS12 *pkcs12_2 = NULL;
  9575. WOLFSSL_X509 *cert = NULL;
  9576. WOLFSSL_X509 *tmp = NULL;
  9577. WOLF_STACK_OF(WOLFSSL_X509) *ca = NULL;
  9578. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  9579. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  9580. WOLFSSL_CTX *ctx = NULL;
  9581. WOLFSSL *ssl = NULL;
  9582. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  9583. #endif
  9584. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  9585. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  9586. if (f != XBADFILE) {
  9587. XFCLOSE(f);
  9588. f = XBADFILE;
  9589. }
  9590. goodPswLen = (int)XSTRLEN(goodPsw);
  9591. badPswLen = (int)XSTRLEN(badPsw);
  9592. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  9593. ExpectIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  9594. ExpectNotNull(d2i_PKCS12_bio(bio, &pkcs12));
  9595. ExpectNotNull(pkcs12);
  9596. BIO_free(bio);
  9597. bio = NULL;
  9598. /* check verify MAC directly */
  9599. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen), 1);
  9600. /* check verify MAC fail case directly */
  9601. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen), 0);
  9602. /* check verify MAC fail case */
  9603. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  9604. ExpectNull(pkey);
  9605. ExpectNull(cert);
  9606. /* check parse with no extra certs kept */
  9607. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  9608. 1);
  9609. ExpectNotNull(pkey);
  9610. ExpectNotNull(cert);
  9611. wolfSSL_EVP_PKEY_free(pkey);
  9612. pkey = NULL;
  9613. wolfSSL_X509_free(cert);
  9614. cert = NULL;
  9615. /* check parse with extra certs kept */
  9616. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9617. 1);
  9618. ExpectNotNull(pkey);
  9619. ExpectNotNull(cert);
  9620. ExpectNotNull(ca);
  9621. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  9622. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  9623. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  9624. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9625. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  9626. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  9627. #else
  9628. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9629. #endif
  9630. /* Copy stack structure */
  9631. ExpectNotNull(tmp_ca = X509_chain_up_ref(ca));
  9632. ExpectIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  9633. /* CTX now owns the tmp_ca stack structure */
  9634. tmp_ca = NULL;
  9635. ExpectIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  9636. ExpectNotNull(tmp_ca);
  9637. ExpectIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  9638. /* Check that the main cert is also set */
  9639. ExpectNotNull(SSL_CTX_get0_certificate(ctx));
  9640. ExpectNotNull(ssl = SSL_new(ctx));
  9641. ExpectNotNull(SSL_get_certificate(ssl));
  9642. SSL_free(ssl);
  9643. SSL_CTX_free(ctx);
  9644. ctx = NULL;
  9645. #endif
  9646. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9647. /* should be 2 other certs on stack */
  9648. ExpectNotNull(tmp = sk_X509_pop(ca));
  9649. X509_free(tmp);
  9650. ExpectNotNull(tmp = sk_X509_pop(ca));
  9651. X509_free(tmp);
  9652. ExpectNull(sk_X509_pop(ca));
  9653. EVP_PKEY_free(pkey);
  9654. pkey = NULL;
  9655. X509_free(cert);
  9656. cert = NULL;
  9657. sk_X509_pop_free(ca, X509_free);
  9658. ca = NULL;
  9659. /* check PKCS12_create */
  9660. ExpectNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  9661. ExpectIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9662. SSL_SUCCESS);
  9663. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  9664. -1, -1, 100, -1, 0)));
  9665. EVP_PKEY_free(pkey);
  9666. pkey = NULL;
  9667. X509_free(cert);
  9668. cert = NULL;
  9669. sk_X509_pop_free(ca, NULL);
  9670. ca = NULL;
  9671. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  9672. SSL_SUCCESS);
  9673. PKCS12_free(pkcs12_2);
  9674. pkcs12_2 = NULL;
  9675. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  9676. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  9677. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  9678. 2000, 1, 0)));
  9679. EVP_PKEY_free(pkey);
  9680. pkey = NULL;
  9681. X509_free(cert);
  9682. cert = NULL;
  9683. sk_X509_pop_free(ca, NULL);
  9684. ca = NULL;
  9685. /* convert to DER then back and parse */
  9686. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  9687. ExpectIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  9688. PKCS12_free(pkcs12_2);
  9689. pkcs12_2 = NULL;
  9690. ExpectNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  9691. BIO_free(bio);
  9692. bio = NULL;
  9693. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  9694. SSL_SUCCESS);
  9695. /* should be 2 other certs on stack */
  9696. ExpectNotNull(tmp = sk_X509_pop(ca));
  9697. X509_free(tmp);
  9698. ExpectNotNull(tmp = sk_X509_pop(ca));
  9699. X509_free(tmp);
  9700. ExpectNull(sk_X509_pop(ca));
  9701. #ifndef NO_RC4
  9702. PKCS12_free(pkcs12_2);
  9703. pkcs12_2 = NULL;
  9704. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  9705. NID_pbe_WithSHA1And128BitRC4,
  9706. NID_pbe_WithSHA1And128BitRC4,
  9707. 2000, 1, 0)));
  9708. EVP_PKEY_free(pkey);
  9709. pkey = NULL;
  9710. X509_free(cert);
  9711. cert = NULL;
  9712. sk_X509_pop_free(ca, NULL);
  9713. ca = NULL;
  9714. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  9715. SSL_SUCCESS);
  9716. #endif /* NO_RC4 */
  9717. EVP_PKEY_free(pkey);
  9718. pkey = NULL;
  9719. X509_free(cert);
  9720. cert = NULL;
  9721. PKCS12_free(pkcs12);
  9722. pkcs12 = NULL;
  9723. PKCS12_free(pkcs12_2);
  9724. pkcs12_2 = NULL;
  9725. sk_X509_pop_free(ca, NULL);
  9726. ca = NULL;
  9727. #ifdef HAVE_ECC
  9728. /* test order of parsing */
  9729. ExpectTrue((f = XFOPEN(order, "rb")) != XBADFILE);
  9730. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  9731. if (f != XBADFILE) {
  9732. XFCLOSE(f);
  9733. f = XBADFILE;
  9734. }
  9735. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  9736. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  9737. ExpectIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  9738. WOLFSSL_SUCCESS);
  9739. /* check use of pkey after parse */
  9740. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  9741. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  9742. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9743. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  9744. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  9745. #else
  9746. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9747. #endif
  9748. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  9749. SSL_CTX_free(ctx);
  9750. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9751. #endif
  9752. ExpectNotNull(pkey);
  9753. ExpectNotNull(cert);
  9754. ExpectNotNull(ca);
  9755. /* compare subject lines of certificates */
  9756. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  9757. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  9758. SSL_FILETYPE_PEM));
  9759. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  9760. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  9761. X509_free(x509);
  9762. x509 = NULL;
  9763. /* test expected fail case */
  9764. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  9765. SSL_FILETYPE_PEM));
  9766. ExpectIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  9767. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  9768. X509_free(x509);
  9769. x509 = NULL;
  9770. X509_free(cert);
  9771. cert = NULL;
  9772. /* get subject line from ca stack */
  9773. ExpectNotNull(cert = sk_X509_pop(ca));
  9774. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  9775. /* compare subject from certificate in ca to expected */
  9776. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  9777. SSL_FILETYPE_PEM));
  9778. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  9779. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  9780. EVP_PKEY_free(pkey);
  9781. pkey = NULL;
  9782. X509_free(x509);
  9783. x509 = NULL;
  9784. X509_free(cert);
  9785. cert = NULL;
  9786. BIO_free(bio);
  9787. bio = NULL;
  9788. PKCS12_free(pkcs12);
  9789. pkcs12 = NULL;
  9790. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  9791. ca = NULL;
  9792. /* test order of parsing */
  9793. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  9794. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  9795. if (f != XBADFILE) {
  9796. XFCLOSE(f);
  9797. f = XBADFILE;
  9798. }
  9799. /* check verify MAC fail case */
  9800. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  9801. ExpectNull(pkey);
  9802. ExpectNull(cert);
  9803. /* check parse with no extra certs kept */
  9804. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  9805. 1);
  9806. ExpectNotNull(pkey);
  9807. ExpectNotNull(cert);
  9808. wolfSSL_EVP_PKEY_free(pkey);
  9809. pkey = NULL;
  9810. wolfSSL_X509_free(cert);
  9811. cert = NULL;
  9812. /* check parse with extra certs kept */
  9813. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9814. 1);
  9815. ExpectNotNull(pkey);
  9816. ExpectNotNull(cert);
  9817. ExpectNotNull(ca);
  9818. wolfSSL_EVP_PKEY_free(pkey);
  9819. pkey = NULL;
  9820. wolfSSL_X509_free(cert);
  9821. cert = NULL;
  9822. sk_X509_pop_free(ca, NULL);
  9823. ca = NULL;
  9824. PKCS12_free(pkcs12);
  9825. pkcs12 = NULL;
  9826. #endif /* HAVE_ECC */
  9827. #ifdef WC_RC2
  9828. /* test PKCS#12 with RC2 encryption */
  9829. ExpectTrue((f = XFOPEN(rc2p12, "rb")) != XBADFILE);
  9830. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  9831. if (f != XBADFILE) {
  9832. XFCLOSE(f);
  9833. f = XBADFILE;
  9834. }
  9835. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  9836. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  9837. /* check verify MAC fail case */
  9838. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  9839. ExpectNull(pkey);
  9840. ExpectNull(cert);
  9841. /* check parse with not extra certs kept */
  9842. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  9843. WOLFSSL_SUCCESS);
  9844. ExpectNotNull(pkey);
  9845. ExpectNotNull(cert);
  9846. wolfSSL_EVP_PKEY_free(pkey);
  9847. pkey = NULL;
  9848. wolfSSL_X509_free(cert);
  9849. cert = NULL;
  9850. /* check parse with extra certs kept */
  9851. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9852. WOLFSSL_SUCCESS);
  9853. ExpectNotNull(pkey);
  9854. ExpectNotNull(cert);
  9855. ExpectNotNull(ca);
  9856. wolfSSL_EVP_PKEY_free(pkey);
  9857. wolfSSL_X509_free(cert);
  9858. sk_X509_pop_free(ca, NULL);
  9859. BIO_free(bio);
  9860. bio = NULL;
  9861. PKCS12_free(pkcs12);
  9862. pkcs12 = NULL;
  9863. #endif /* WC_RC2 */
  9864. /* Test i2d_PKCS12_bio */
  9865. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  9866. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  9867. if (f != XBADFILE)
  9868. XFCLOSE(f);
  9869. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  9870. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, pkcs12), 1);
  9871. ExpectIntEQ(ret = i2d_PKCS12_bio(NULL, pkcs12), 0);
  9872. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, NULL), 0);
  9873. PKCS12_free(pkcs12);
  9874. BIO_free(bio);
  9875. (void)order;
  9876. #endif /* OPENSSL_EXTRA */
  9877. #endif /* HAVE_FIPS */
  9878. return EXPECT_RESULT();
  9879. }
  9880. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  9881. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  9882. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  9883. #define TEST_PKCS8_ENC
  9884. #endif
  9885. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  9886. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  9887. /* used to keep track if FailTestCallback was called */
  9888. static int failTestCallbackCalled = 0;
  9889. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  9890. {
  9891. (void)passwd;
  9892. (void)sz;
  9893. (void)rw;
  9894. (void)userdata;
  9895. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  9896. failTestCallbackCalled = 1;
  9897. return -1;
  9898. }
  9899. #endif
  9900. static int test_wolfSSL_no_password_cb(void)
  9901. {
  9902. EXPECT_DECLS;
  9903. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  9904. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  9905. WOLFSSL_CTX* ctx = NULL;
  9906. byte buff[FOURK_BUF];
  9907. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  9908. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  9909. XFILE f = XBADFILE;
  9910. int bytes = 0;
  9911. #ifndef NO_WOLFSSL_CLIENT
  9912. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  9913. #else
  9914. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  9915. #endif
  9916. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  9917. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  9918. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9919. if (f != XBADFILE) {
  9920. XFCLOSE(f);
  9921. f = XBADFILE;
  9922. }
  9923. ExpectIntLE(bytes, sizeof(buff));
  9924. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9925. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9926. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  9927. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9928. if (f != XBADFILE)
  9929. XFCLOSE(f);
  9930. ExpectIntLE(bytes, sizeof(buff));
  9931. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9932. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9933. wolfSSL_CTX_free(ctx);
  9934. /* Password callback should not be called by default */
  9935. ExpectIntEQ(failTestCallbackCalled, 0);
  9936. #endif
  9937. return EXPECT_RESULT();
  9938. }
  9939. #ifdef TEST_PKCS8_ENC
  9940. /* for PKCS8 test case */
  9941. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  9942. {
  9943. int flag = 0;
  9944. (void)rw;
  9945. if (userdata != NULL) {
  9946. flag = *((int*)userdata); /* user set data */
  9947. }
  9948. switch (flag) {
  9949. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  9950. * can be anything the user wishes to be passed to the callback
  9951. * associated with the WOLFSSL_CTX */
  9952. XSTRNCPY(passwd, "yassl123", sz);
  9953. return 8;
  9954. default:
  9955. return BAD_FUNC_ARG;
  9956. }
  9957. }
  9958. #endif /* TEST_PKCS8_ENC */
  9959. /* Testing functions dealing with PKCS8 */
  9960. static int test_wolfSSL_PKCS8(void)
  9961. {
  9962. EXPECT_DECLS;
  9963. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  9964. !defined(WOLFCRYPT_ONLY)
  9965. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9966. byte buff[FOURK_BUF];
  9967. byte der[FOURK_BUF];
  9968. #ifndef NO_RSA
  9969. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  9970. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  9971. #endif
  9972. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  9973. #ifdef HAVE_ECC
  9974. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  9975. #endif
  9976. XFILE f = XBADFILE;
  9977. int bytes = 0;
  9978. WOLFSSL_CTX* ctx = NULL;
  9979. #if defined(HAVE_ECC) && !defined(NO_CODING)
  9980. int ret;
  9981. ecc_key key;
  9982. word32 x = 0;
  9983. #endif
  9984. #ifdef TEST_PKCS8_ENC
  9985. #if !defined(NO_RSA) && !defined(NO_SHA)
  9986. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  9987. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  9988. #endif
  9989. #if defined(HAVE_ECC) && !defined(NO_SHA)
  9990. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  9991. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  9992. #endif
  9993. int flag;
  9994. #endif
  9995. (void)der;
  9996. #ifndef NO_WOLFSSL_CLIENT
  9997. #ifndef WOLFSSL_NO_TLS12
  9998. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  9999. #else
  10000. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10001. #endif
  10002. #else
  10003. #ifndef WOLFSSL_NO_TLS12
  10004. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10005. #else
  10006. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10007. #endif
  10008. #endif
  10009. #ifdef TEST_PKCS8_ENC
  10010. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  10011. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  10012. flag = 1; /* used by password callback as return code */
  10013. #if !defined(NO_RSA) && !defined(NO_SHA)
  10014. /* test loading PEM PKCS8 encrypted file */
  10015. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncPemFile, "rb")) != XBADFILE);
  10016. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10017. if (f != XBADFILE) {
  10018. XFCLOSE(f);
  10019. f = XBADFILE;
  10020. }
  10021. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10022. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10023. /* this next case should fail because of password callback return code */
  10024. flag = 0; /* used by password callback as return code */
  10025. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10026. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10027. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  10028. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10029. "yassl123"), 0);
  10030. /* test that error value is returned with a bad password */
  10031. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10032. "bad"), 0);
  10033. /* test loading PEM PKCS8 encrypted file */
  10034. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncDerFile, "rb")) != XBADFILE);
  10035. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10036. if (f != XBADFILE) {
  10037. XFCLOSE(f);
  10038. f = XBADFILE;
  10039. }
  10040. flag = 1; /* used by password callback as return code */
  10041. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10042. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10043. /* this next case should fail because of password callback return code */
  10044. flag = 0; /* used by password callback as return code */
  10045. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10046. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10047. #endif /* !NO_RSA && !NO_SHA */
  10048. #if defined(HAVE_ECC) && !defined(NO_SHA)
  10049. /* test loading PEM PKCS8 encrypted ECC Key file */
  10050. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb")) != XBADFILE);
  10051. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10052. if (f != XBADFILE) {
  10053. XFCLOSE(f);
  10054. f = XBADFILE;
  10055. }
  10056. flag = 1; /* used by password callback as return code */
  10057. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10058. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10059. /* this next case should fail because of password callback return code */
  10060. flag = 0; /* used by password callback as return code */
  10061. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10062. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10063. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  10064. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10065. "yassl123"), 0);
  10066. /* test that error value is returned with a bad password */
  10067. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10068. "bad"), 0);
  10069. /* test loading DER PKCS8 encrypted ECC Key file */
  10070. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb")) != XBADFILE);
  10071. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10072. if (f != XBADFILE) {
  10073. XFCLOSE(f);
  10074. f = XBADFILE;
  10075. }
  10076. flag = 1; /* used by password callback as return code */
  10077. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10078. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10079. /* this next case should fail because of password callback return code */
  10080. flag = 0; /* used by password callback as return code */
  10081. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10082. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10083. /* leave flag as "okay" */
  10084. flag = 1;
  10085. #endif /* HAVE_ECC && !NO_SHA */
  10086. #endif /* TEST_PKCS8_ENC */
  10087. #ifndef NO_RSA
  10088. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  10089. ExpectTrue((f = XFOPEN(serverKeyPkcs8DerFile, "rb")) != XBADFILE);
  10090. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10091. if (f != XBADFILE) {
  10092. XFCLOSE(f);
  10093. f = XBADFILE;
  10094. }
  10095. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10096. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10097. /* test loading PEM PKCS8 private key file (not encrypted) */
  10098. ExpectTrue((f = XFOPEN(serverKeyPkcs8PemFile, "rb")) != XBADFILE);
  10099. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10100. if (f != XBADFILE) {
  10101. XFCLOSE(f);
  10102. f = XBADFILE;
  10103. }
  10104. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10105. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10106. #endif /* !NO_RSA */
  10107. /* Test PKCS8 PEM ECC key no crypt */
  10108. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  10109. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10110. if (f != XBADFILE) {
  10111. XFCLOSE(f);
  10112. f = XBADFILE;
  10113. }
  10114. #ifdef HAVE_ECC
  10115. /* Test PKCS8 PEM ECC key no crypt */
  10116. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10117. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10118. #ifndef NO_CODING
  10119. /* decrypt PKCS8 PEM to key in DER format */
  10120. ExpectIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  10121. (word32)sizeof(der), NULL)), 0);
  10122. ret = wc_ecc_init(&key);
  10123. if (ret == 0) {
  10124. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  10125. wc_ecc_free(&key);
  10126. }
  10127. ExpectIntEQ(ret, 0);
  10128. #endif
  10129. /* Test PKCS8 DER ECC key no crypt */
  10130. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  10131. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10132. if (f != XBADFILE)
  10133. XFCLOSE(f);
  10134. /* Test using a PKCS8 ECC PEM */
  10135. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10136. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10137. #else
  10138. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  10139. ExpectIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  10140. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  10141. #endif /* HAVE_ECC */
  10142. wolfSSL_CTX_free(ctx);
  10143. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10144. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  10145. return EXPECT_RESULT();
  10146. }
  10147. static int test_wolfSSL_PKCS8_ED25519(void)
  10148. {
  10149. EXPECT_DECLS;
  10150. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  10151. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  10152. defined(HAVE_ED25519_KEY_IMPORT)
  10153. const byte encPrivKey[] = \
  10154. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  10155. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  10156. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  10157. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  10158. "2L9W4v7swXkV+X+a1ww=\n"
  10159. "-----END ENCRYPTED PRIVATE KEY-----\n";
  10160. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  10161. byte der[FOURK_BUF];
  10162. WOLFSSL_CTX* ctx = NULL;
  10163. int bytes;
  10164. XMEMSET(der, 0, sizeof(der));
  10165. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  10166. (word32)sizeof(der), password)), 0);
  10167. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10168. #ifndef NO_WOLFSSL_SERVER
  10169. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10170. #else
  10171. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10172. #endif
  10173. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  10174. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10175. wolfSSL_CTX_free(ctx);
  10176. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10177. #endif
  10178. return EXPECT_RESULT();
  10179. }
  10180. static int test_wolfSSL_PKCS8_ED448(void)
  10181. {
  10182. EXPECT_DECLS;
  10183. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  10184. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  10185. defined(HAVE_ED448_KEY_IMPORT)
  10186. const byte encPrivKey[] = \
  10187. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  10188. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  10189. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  10190. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  10191. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  10192. "-----END ENCRYPTED PRIVATE KEY-----\n";
  10193. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  10194. byte der[FOURK_BUF];
  10195. WOLFSSL_CTX* ctx = NULL;
  10196. int bytes;
  10197. XMEMSET(der, 0, sizeof(der));
  10198. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  10199. (word32)sizeof(der), password)), 0);
  10200. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10201. #ifndef NO_WOLFSSL_SERVER
  10202. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10203. #else
  10204. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10205. #endif
  10206. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  10207. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10208. wolfSSL_CTX_free(ctx);
  10209. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10210. #endif
  10211. return EXPECT_RESULT();
  10212. }
  10213. /* Testing functions dealing with PKCS5 */
  10214. static int test_wolfSSL_PKCS5(void)
  10215. {
  10216. EXPECT_DECLS;
  10217. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  10218. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  10219. const char* passwd = "myfipsPa$$W0rd";
  10220. #else
  10221. const char *passwd = "pass1234";
  10222. #endif
  10223. const unsigned char *salt = (unsigned char *)"salt1234";
  10224. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  10225. DYNAMIC_TYPE_TMP_BUFFER);
  10226. int ret = 0;
  10227. ExpectNotNull(out);
  10228. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  10229. (int)XSTRLEN((const char *) salt), 10, WC_SHA_DIGEST_SIZE,out),
  10230. WOLFSSL_SUCCESS);
  10231. #ifdef WOLFSSL_SHA512
  10232. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  10233. (int)XSTRLEN((const char *) salt), 10, wolfSSL_EVP_sha512(),
  10234. WC_SHA_DIGEST_SIZE, out), SSL_SUCCESS);
  10235. #endif
  10236. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10237. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  10238. return EXPECT_RESULT();
  10239. }
  10240. /* test parsing URI from certificate */
  10241. static int test_wolfSSL_URI(void)
  10242. {
  10243. EXPECT_DECLS;
  10244. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  10245. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  10246. defined(OPENSSL_EXTRA))
  10247. WOLFSSL_X509* x509 = NULL;
  10248. const char uri[] = "./certs/client-uri-cert.pem";
  10249. const char urn[] = "./certs/client-absolute-urn.pem";
  10250. const char badUri[] = "./certs/client-relative-uri.pem";
  10251. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(uri,
  10252. WOLFSSL_FILETYPE_PEM));
  10253. wolfSSL_FreeX509(x509);
  10254. x509 = NULL;
  10255. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(urn,
  10256. WOLFSSL_FILETYPE_PEM));
  10257. wolfSSL_FreeX509(x509);
  10258. x509 = NULL;
  10259. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  10260. && !defined(WOLFSSL_FPKI)
  10261. ExpectNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  10262. WOLFSSL_FILETYPE_PEM));
  10263. #else
  10264. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  10265. WOLFSSL_FILETYPE_PEM));
  10266. #endif
  10267. wolfSSL_FreeX509(x509);
  10268. #endif
  10269. return EXPECT_RESULT();
  10270. }
  10271. static int test_wolfSSL_TBS(void)
  10272. {
  10273. EXPECT_DECLS;
  10274. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  10275. && defined(OPENSSL_EXTRA)
  10276. WOLFSSL_X509* x509 = NULL;
  10277. const unsigned char* tbs;
  10278. int tbsSz;
  10279. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caCertFile,
  10280. WOLFSSL_FILETYPE_PEM));
  10281. ExpectNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  10282. ExpectNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  10283. ExpectNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  10284. ExpectIntEQ(tbsSz, 1003);
  10285. wolfSSL_FreeX509(x509);
  10286. #endif
  10287. return EXPECT_RESULT();
  10288. }
  10289. static int test_wolfSSL_X509_verify(void)
  10290. {
  10291. EXPECT_DECLS;
  10292. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  10293. defined(OPENSSL_EXTRA)
  10294. WOLFSSL_X509* ca = NULL;
  10295. WOLFSSL_X509* serv = NULL;
  10296. WOLFSSL_EVP_PKEY* pkey = NULL;
  10297. unsigned char buf[2048];
  10298. const unsigned char* pt = NULL;
  10299. int bufSz;
  10300. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(caCertFile,
  10301. WOLFSSL_FILETYPE_PEM));
  10302. ExpectIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  10303. WOLFSSL_SUCCESS);
  10304. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  10305. WOLFSSL_SUCCESS);
  10306. ExpectIntEQ(bufSz, 294);
  10307. bufSz = 2048;
  10308. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  10309. WOLFSSL_SUCCESS);
  10310. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  10311. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  10312. ExpectNotNull(serv = wolfSSL_X509_load_certificate_file(svrCertFile,
  10313. WOLFSSL_FILETYPE_PEM));
  10314. /* success case */
  10315. pt = buf;
  10316. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  10317. ExpectIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  10318. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  10319. wolfSSL_EVP_PKEY_free(pkey);
  10320. pkey = NULL;
  10321. /* fail case */
  10322. bufSz = 2048;
  10323. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  10324. WOLFSSL_SUCCESS);
  10325. pt = buf;
  10326. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  10327. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  10328. ExpectIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  10329. ExpectIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  10330. wolfSSL_EVP_PKEY_free(pkey);
  10331. wolfSSL_FreeX509(ca);
  10332. wolfSSL_FreeX509(serv);
  10333. #endif
  10334. return EXPECT_RESULT();
  10335. }
  10336. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  10337. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  10338. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  10339. /* create certificate with version 2 */
  10340. static int test_set_x509_badversion(WOLFSSL_CTX* ctx)
  10341. {
  10342. EXPECT_DECLS;
  10343. WOLFSSL_X509 *x509 = NULL, *x509v2 = NULL;
  10344. WOLFSSL_EVP_PKEY *priv = NULL, *pub = NULL;
  10345. unsigned char *der = NULL, *key = NULL, *pt;
  10346. char *header = NULL, *name = NULL;
  10347. int derSz;
  10348. long keySz;
  10349. XFILE fp = XBADFILE;
  10350. WOLFSSL_ASN1_TIME *notBefore = NULL, *notAfter = NULL;
  10351. time_t t;
  10352. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10353. WOLFSSL_FILETYPE_PEM));
  10354. ExpectTrue((fp = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  10355. ExpectIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  10356. WOLFSSL_SUCCESS);
  10357. if (fp != XBADFILE)
  10358. XFCLOSE(fp);
  10359. pt = key;
  10360. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  10361. (const unsigned char**)&pt, keySz));
  10362. /* create the version 2 certificate */
  10363. ExpectNotNull(x509v2 = X509_new());
  10364. ExpectIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  10365. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  10366. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  10367. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  10368. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  10369. ExpectNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  10370. ExpectIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  10371. t = time(NULL);
  10372. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  10373. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  10374. ExpectTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  10375. ExpectTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  10376. ExpectIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  10377. derSz = wolfSSL_i2d_X509(x509v2, &der);
  10378. ExpectIntGT(derSz, 0);
  10379. ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  10380. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10381. /* TODO: Replace with API call */
  10382. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  10383. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10384. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10385. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10386. wolfSSL_X509_free(x509);
  10387. wolfSSL_X509_free(x509v2);
  10388. wolfSSL_EVP_PKEY_free(priv);
  10389. wolfSSL_EVP_PKEY_free(pub);
  10390. wolfSSL_ASN1_TIME_free(notBefore);
  10391. wolfSSL_ASN1_TIME_free(notAfter);
  10392. return EXPECT_RESULT();
  10393. }
  10394. /* override certificate version error */
  10395. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  10396. {
  10397. EXPECT_DECLS;
  10398. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  10399. ExpectIntEQ(store->error, ASN_VERSION_E);
  10400. #else
  10401. ExpectIntEQ(store->error, 0);
  10402. #endif
  10403. ExpectIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  10404. (void)preverify;
  10405. return EXPECT_RESULT() == TEST_SUCCESS;
  10406. }
  10407. /* set verify callback that will override bad certificate version */
  10408. static int test_set_override_x509(WOLFSSL_CTX* ctx)
  10409. {
  10410. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  10411. return TEST_SUCCESS;
  10412. }
  10413. #endif
  10414. static int test_wolfSSL_X509_TLS_version_test_1(void)
  10415. {
  10416. EXPECT_DECLS;
  10417. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  10418. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  10419. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  10420. test_ssl_cbf func_cb_client;
  10421. test_ssl_cbf func_cb_server;
  10422. /* test server rejects a client certificate that is not version 3 */
  10423. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  10424. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  10425. func_cb_client.ctx_ready = &test_set_x509_badversion;
  10426. #ifndef WOLFSSL_NO_TLS12
  10427. func_cb_client.method = wolfTLSv1_2_client_method;
  10428. #else
  10429. func_cb_client.method = wolfTLSv1_3_client_method;
  10430. #endif
  10431. #ifndef WOLFSSL_NO_TLS12
  10432. func_cb_server.method = wolfTLSv1_2_server_method;
  10433. #else
  10434. func_cb_server.method = wolfTLSv1_3_server_method;
  10435. #endif
  10436. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  10437. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  10438. &func_cb_server, NULL), TEST_FAIL);
  10439. #else
  10440. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  10441. &func_cb_server, NULL), TEST_SUCCESS);
  10442. #endif
  10443. #endif
  10444. return EXPECT_RESULT();
  10445. }
  10446. static int test_wolfSSL_X509_TLS_version_test_2(void)
  10447. {
  10448. EXPECT_DECLS;
  10449. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  10450. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  10451. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  10452. test_ssl_cbf func_cb_client;
  10453. test_ssl_cbf func_cb_server;
  10454. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  10455. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  10456. func_cb_client.ctx_ready = &test_set_x509_badversion;
  10457. func_cb_server.ctx_ready = &test_set_override_x509;
  10458. #ifndef WOLFSSL_NO_TLS12
  10459. func_cb_client.method = wolfTLSv1_2_client_method;
  10460. #else
  10461. func_cb_client.method = wolfTLSv1_3_client_method;
  10462. #endif
  10463. #ifndef WOLFSSL_NO_TLS12
  10464. func_cb_server.method = wolfTLSv1_2_server_method;
  10465. #else
  10466. func_cb_server.method = wolfTLSv1_3_server_method;
  10467. #endif
  10468. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  10469. &func_cb_server, NULL), TEST_SUCCESS);
  10470. #endif
  10471. return EXPECT_RESULT();
  10472. }
  10473. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  10474. * version allowed.
  10475. * POST: 1 on success.
  10476. */
  10477. static int test_wolfSSL_CTX_SetMinVersion(void)
  10478. {
  10479. int res = TEST_SKIPPED;
  10480. #ifndef NO_WOLFSSL_CLIENT
  10481. int failFlag = WOLFSSL_SUCCESS;
  10482. WOLFSSL_CTX* ctx;
  10483. int itr;
  10484. #ifndef NO_OLD_TLS
  10485. const int versions[] = {
  10486. #ifdef WOLFSSL_ALLOW_TLSV10
  10487. WOLFSSL_TLSV1,
  10488. #endif
  10489. WOLFSSL_TLSV1_1,
  10490. WOLFSSL_TLSV1_2 };
  10491. #elif !defined(WOLFSSL_NO_TLS12)
  10492. const int versions[] = { WOLFSSL_TLSV1_2 };
  10493. #elif defined(WOLFSSL_TLS13)
  10494. const int versions[] = { WOLFSSL_TLSV1_3 };
  10495. #else
  10496. const int versions[0];
  10497. #endif
  10498. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10499. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  10500. if (wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr))
  10501. != WOLFSSL_SUCCESS) {
  10502. failFlag = WOLFSSL_FAILURE;
  10503. }
  10504. }
  10505. wolfSSL_CTX_free(ctx);
  10506. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  10507. #endif
  10508. return res;
  10509. } /* END test_wolfSSL_CTX_SetMinVersion */
  10510. /*----------------------------------------------------------------------------*
  10511. | OCSP Stapling
  10512. *----------------------------------------------------------------------------*/
  10513. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  10514. * need to contact the CA, lowering the cost of cert revocation checking.
  10515. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  10516. * POST: 1 returned for success.
  10517. */
  10518. static int test_wolfSSL_UseOCSPStapling(void)
  10519. {
  10520. EXPECT_DECLS;
  10521. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  10522. !defined(NO_WOLFSSL_CLIENT)
  10523. WOLFSSL_CTX* ctx = NULL;
  10524. WOLFSSL* ssl = NULL;
  10525. #ifndef NO_WOLFSSL_CLIENT
  10526. #ifndef WOLFSSL_NO_TLS12
  10527. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  10528. #else
  10529. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10530. #endif
  10531. #else
  10532. #ifndef WOLFSSL_NO_TLS12
  10533. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10534. #else
  10535. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10536. #endif
  10537. #endif
  10538. ExpectNotNull(ssl = wolfSSL_new(ctx));
  10539. ExpectIntEQ(wolfSSL_UseOCSPStapling(NULL, WOLFSSL_CSR2_OCSP,
  10540. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10541. #ifndef NO_WOLFSSL_CLIENT
  10542. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  10543. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  10544. #else
  10545. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  10546. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10547. #endif
  10548. wolfSSL_free(ssl);
  10549. wolfSSL_CTX_free(ctx);
  10550. #endif
  10551. return EXPECT_RESULT();
  10552. } /* END test_wolfSSL_UseOCSPStapling */
  10553. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  10554. * stapling eliminates the need to contact the CA and lowers cert revocation
  10555. * check.
  10556. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  10557. */
  10558. static int test_wolfSSL_UseOCSPStaplingV2(void)
  10559. {
  10560. EXPECT_DECLS;
  10561. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  10562. !defined(NO_WOLFSSL_CLIENT)
  10563. WOLFSSL_CTX* ctx = NULL;
  10564. WOLFSSL* ssl = NULL;
  10565. #ifndef NO_WOLFSSL_CLIENT
  10566. #ifndef WOLFSSL_NO_TLS12
  10567. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  10568. #else
  10569. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10570. #endif
  10571. #else
  10572. #ifndef WOLFSSL_NO_TLS12
  10573. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10574. #else
  10575. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10576. #endif
  10577. #endif
  10578. ExpectNotNull(ssl = wolfSSL_new(ctx));
  10579. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(NULL, WOLFSSL_CSR2_OCSP,
  10580. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10581. #ifndef NO_WOLFSSL_CLIENT
  10582. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  10583. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  10584. #else
  10585. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  10586. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10587. #endif
  10588. wolfSSL_free(ssl);
  10589. wolfSSL_CTX_free(ctx);
  10590. #endif
  10591. return EXPECT_RESULT();
  10592. } /* END test_wolfSSL_UseOCSPStaplingV2 */
  10593. /*----------------------------------------------------------------------------*
  10594. | Multicast Tests
  10595. *----------------------------------------------------------------------------*/
  10596. static int test_wolfSSL_mcast(void)
  10597. {
  10598. EXPECT_DECLS;
  10599. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  10600. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  10601. WOLFSSL_CTX* ctx = NULL;
  10602. WOLFSSL* ssl = NULL;
  10603. byte preMasterSecret[512];
  10604. byte clientRandom[32];
  10605. byte serverRandom[32];
  10606. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  10607. byte buf[256];
  10608. word16 newId;
  10609. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  10610. ExpectIntEQ(wolfSSL_CTX_mcast_set_member_id(ctx, 0), WOLFSSL_SUCCESS);
  10611. ExpectNotNull(ssl = wolfSSL_new(ctx));
  10612. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  10613. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  10614. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  10615. ExpectIntEQ(wolfSSL_set_secret(ssl, 23, preMasterSecret,
  10616. sizeof(preMasterSecret), clientRandom, serverRandom, suite),
  10617. WOLFSSL_SUCCESS);
  10618. ExpectIntLE(wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf)), 0);
  10619. ExpectIntLE(newId, 100);
  10620. wolfSSL_free(ssl);
  10621. wolfSSL_CTX_free(ctx);
  10622. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 ||
  10623. * WOLFSSL_SNIFFER) */
  10624. return EXPECT_RESULT();
  10625. }
  10626. /*----------------------------------------------------------------------------*
  10627. | Wolfcrypt
  10628. *----------------------------------------------------------------------------*/
  10629. /*
  10630. * Unit test for the wc_InitBlake2b()
  10631. */
  10632. static int test_wc_InitBlake2b(void)
  10633. {
  10634. EXPECT_DECLS;
  10635. #ifdef HAVE_BLAKE2
  10636. Blake2b blake;
  10637. /* Test good arg. */
  10638. ExpectIntEQ(wc_InitBlake2b(&blake, 64), 0);
  10639. /* Test bad arg. */
  10640. ExpectIntEQ(wc_InitBlake2b(NULL, 64), BAD_FUNC_ARG);
  10641. ExpectIntEQ(wc_InitBlake2b(NULL, 128), BAD_FUNC_ARG);
  10642. ExpectIntEQ(wc_InitBlake2b(&blake, 128), BAD_FUNC_ARG);
  10643. ExpectIntEQ(wc_InitBlake2b(NULL, 0), BAD_FUNC_ARG);
  10644. ExpectIntEQ(wc_InitBlake2b(&blake, 0), BAD_FUNC_ARG);
  10645. #endif
  10646. return EXPECT_RESULT();
  10647. } /* END test_wc_InitBlake2b*/
  10648. /*
  10649. * Unit test for the wc_InitBlake2b_WithKey()
  10650. */
  10651. static int test_wc_InitBlake2b_WithKey(void)
  10652. {
  10653. EXPECT_DECLS;
  10654. #ifdef HAVE_BLAKE2
  10655. Blake2b blake;
  10656. word32 digestSz = BLAKE2B_KEYBYTES;
  10657. byte key[BLAKE2B_KEYBYTES];
  10658. word32 keylen = BLAKE2B_KEYBYTES;
  10659. XMEMSET(key, 0, sizeof(key));
  10660. /* Test good arg. */
  10661. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen), 0);
  10662. /* Test bad args. */
  10663. ExpectIntEQ(wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen),
  10664. BAD_FUNC_ARG);
  10665. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, 256),
  10666. BAD_FUNC_ARG);
  10667. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen), 0);
  10668. #endif
  10669. return EXPECT_RESULT();
  10670. } /* END wc_InitBlake2b_WithKey*/
  10671. /*
  10672. * Unit test for the wc_InitBlake2s_WithKey()
  10673. */
  10674. static int test_wc_InitBlake2s_WithKey(void)
  10675. {
  10676. EXPECT_DECLS;
  10677. #ifdef HAVE_BLAKE2S
  10678. Blake2s blake;
  10679. word32 digestSz = BLAKE2S_KEYBYTES;
  10680. byte *key = (byte*)"01234567890123456789012345678901";
  10681. word32 keylen = BLAKE2S_KEYBYTES;
  10682. /* Test good arg. */
  10683. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen), 0);
  10684. /* Test bad args. */
  10685. ExpectIntEQ(wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen),
  10686. BAD_FUNC_ARG);
  10687. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, 256),
  10688. BAD_FUNC_ARG);
  10689. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen), 0);
  10690. #endif
  10691. return EXPECT_RESULT();
  10692. } /* END wc_InitBlake2s_WithKey*/
  10693. /*
  10694. * Unit test for the wc_InitMd5()
  10695. */
  10696. static int test_wc_InitMd5(void)
  10697. {
  10698. EXPECT_DECLS;
  10699. #ifndef NO_MD5
  10700. wc_Md5 md5;
  10701. /* Test good arg. */
  10702. ExpectIntEQ(wc_InitMd5(&md5), 0);
  10703. /* Test bad arg. */
  10704. ExpectIntEQ(wc_InitMd5(NULL), BAD_FUNC_ARG);
  10705. wc_Md5Free(&md5);
  10706. #endif
  10707. return EXPECT_RESULT();
  10708. } /* END test_wc_InitMd5 */
  10709. /*
  10710. * Testing wc_UpdateMd5()
  10711. */
  10712. static int test_wc_Md5Update(void)
  10713. {
  10714. EXPECT_DECLS;
  10715. #ifndef NO_MD5
  10716. wc_Md5 md5;
  10717. byte hash[WC_MD5_DIGEST_SIZE];
  10718. testVector a, b, c;
  10719. ExpectIntEQ(wc_InitMd5(&md5), 0);
  10720. /* Input */
  10721. a.input = "a";
  10722. a.inLen = XSTRLEN(a.input);
  10723. ExpectIntEQ(wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen), 0);
  10724. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  10725. /* Update input. */
  10726. a.input = "abc";
  10727. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  10728. "\x72";
  10729. a.inLen = XSTRLEN(a.input);
  10730. a.outLen = XSTRLEN(a.output);
  10731. ExpectIntEQ(wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen), 0);
  10732. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  10733. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  10734. /* Pass in bad values. */
  10735. b.input = NULL;
  10736. b.inLen = 0;
  10737. ExpectIntEQ(wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen), 0);
  10738. c.input = NULL;
  10739. c.inLen = WC_MD5_DIGEST_SIZE;
  10740. ExpectIntEQ(wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen),
  10741. BAD_FUNC_ARG);
  10742. ExpectIntEQ(wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen),
  10743. BAD_FUNC_ARG);
  10744. wc_Md5Free(&md5);
  10745. #endif
  10746. return EXPECT_RESULT();
  10747. } /* END test_wc_Md5Update() */
  10748. /*
  10749. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  10750. */
  10751. static int test_wc_Md5Final(void)
  10752. {
  10753. EXPECT_DECLS;
  10754. #ifndef NO_MD5
  10755. /* Instantiate */
  10756. wc_Md5 md5;
  10757. byte* hash_test[3];
  10758. byte hash1[WC_MD5_DIGEST_SIZE];
  10759. byte hash2[2*WC_MD5_DIGEST_SIZE];
  10760. byte hash3[5*WC_MD5_DIGEST_SIZE];
  10761. int times, i;
  10762. /* Initialize */
  10763. ExpectIntEQ(wc_InitMd5(&md5), 0);
  10764. hash_test[0] = hash1;
  10765. hash_test[1] = hash2;
  10766. hash_test[2] = hash3;
  10767. times = sizeof(hash_test)/sizeof(byte*);
  10768. for (i = 0; i < times; i++) {
  10769. ExpectIntEQ(wc_Md5Final(&md5, hash_test[i]), 0);
  10770. }
  10771. /* Test bad args. */
  10772. ExpectIntEQ(wc_Md5Final(NULL, NULL), BAD_FUNC_ARG);
  10773. ExpectIntEQ(wc_Md5Final(NULL, hash1), BAD_FUNC_ARG);
  10774. ExpectIntEQ(wc_Md5Final(&md5, NULL), BAD_FUNC_ARG);
  10775. wc_Md5Free(&md5);
  10776. #endif
  10777. return EXPECT_RESULT();
  10778. }
  10779. /*
  10780. * Unit test for the wc_InitSha()
  10781. */
  10782. static int test_wc_InitSha(void)
  10783. {
  10784. EXPECT_DECLS;
  10785. #ifndef NO_SHA
  10786. wc_Sha sha;
  10787. /* Test good arg. */
  10788. ExpectIntEQ(wc_InitSha(&sha), 0);
  10789. /* Test bad arg. */
  10790. ExpectIntEQ(wc_InitSha(NULL), BAD_FUNC_ARG);
  10791. wc_ShaFree(&sha);
  10792. #endif
  10793. return EXPECT_RESULT();
  10794. } /* END test_wc_InitSha */
  10795. /*
  10796. * Tesing wc_ShaUpdate()
  10797. */
  10798. static int test_wc_ShaUpdate(void)
  10799. {
  10800. EXPECT_DECLS;
  10801. #ifndef NO_SHA
  10802. wc_Sha sha;
  10803. byte hash[WC_SHA_DIGEST_SIZE];
  10804. testVector a, b, c;
  10805. ExpectIntEQ(wc_InitSha(&sha), 0);
  10806. /* Input. */
  10807. a.input = "a";
  10808. a.inLen = XSTRLEN(a.input);
  10809. ExpectIntEQ(wc_ShaUpdate(&sha, NULL, 0), 0);
  10810. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, 0), 0);
  10811. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  10812. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  10813. /* Update input. */
  10814. a.input = "abc";
  10815. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  10816. "\x6C\x9C\xD0\xD8\x9D";
  10817. a.inLen = XSTRLEN(a.input);
  10818. a.outLen = XSTRLEN(a.output);
  10819. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  10820. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  10821. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  10822. /* Try passing in bad values. */
  10823. b.input = NULL;
  10824. b.inLen = 0;
  10825. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen), 0);
  10826. c.input = NULL;
  10827. c.inLen = WC_SHA_DIGEST_SIZE;
  10828. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen),
  10829. BAD_FUNC_ARG);
  10830. ExpectIntEQ(wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  10831. BAD_FUNC_ARG);
  10832. wc_ShaFree(&sha);
  10833. #endif
  10834. return EXPECT_RESULT();
  10835. } /* END test_wc_ShaUpdate() */
  10836. /*
  10837. * Unit test on wc_ShaFinal
  10838. */
  10839. static int test_wc_ShaFinal(void)
  10840. {
  10841. EXPECT_DECLS;
  10842. #ifndef NO_SHA
  10843. wc_Sha sha;
  10844. byte* hash_test[3];
  10845. byte hash1[WC_SHA_DIGEST_SIZE];
  10846. byte hash2[2*WC_SHA_DIGEST_SIZE];
  10847. byte hash3[5*WC_SHA_DIGEST_SIZE];
  10848. int times, i;
  10849. /* Initialize*/
  10850. ExpectIntEQ(wc_InitSha(&sha), 0);
  10851. hash_test[0] = hash1;
  10852. hash_test[1] = hash2;
  10853. hash_test[2] = hash3;
  10854. times = sizeof(hash_test)/sizeof(byte*);
  10855. for (i = 0; i < times; i++) {
  10856. ExpectIntEQ(wc_ShaFinal(&sha, hash_test[i]), 0);
  10857. }
  10858. /* Test bad args. */
  10859. ExpectIntEQ(wc_ShaFinal(NULL, NULL), BAD_FUNC_ARG);
  10860. ExpectIntEQ(wc_ShaFinal(NULL, hash1), BAD_FUNC_ARG);
  10861. ExpectIntEQ(wc_ShaFinal(&sha, NULL), BAD_FUNC_ARG);
  10862. wc_ShaFree(&sha);
  10863. #endif
  10864. return EXPECT_RESULT();
  10865. } /* END test_wc_ShaFinal */
  10866. /*
  10867. * Unit test for wc_InitSha256()
  10868. */
  10869. static int test_wc_InitSha256(void)
  10870. {
  10871. EXPECT_DECLS;
  10872. #ifndef NO_SHA256
  10873. wc_Sha256 sha256;
  10874. /* Test good arg. */
  10875. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10876. /* Test bad arg. */
  10877. ExpectIntEQ(wc_InitSha256(NULL), BAD_FUNC_ARG);
  10878. wc_Sha256Free(&sha256);
  10879. #endif
  10880. return EXPECT_RESULT();
  10881. } /* END test_wc_InitSha256 */
  10882. /*
  10883. * Unit test for wc_Sha256Update()
  10884. */
  10885. static int test_wc_Sha256Update(void)
  10886. {
  10887. EXPECT_DECLS;
  10888. #ifndef NO_SHA256
  10889. wc_Sha256 sha256;
  10890. byte hash[WC_SHA256_DIGEST_SIZE];
  10891. byte hash_unaligned[WC_SHA256_DIGEST_SIZE+1];
  10892. testVector a, b, c;
  10893. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10894. /* Input. */
  10895. a.input = "a";
  10896. a.inLen = XSTRLEN(a.input);
  10897. ExpectIntEQ(wc_Sha256Update(&sha256, NULL, 0), 0);
  10898. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, 0), 0);
  10899. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  10900. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  10901. /* Update input. */
  10902. a.input = "abc";
  10903. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  10904. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  10905. "\x15\xAD";
  10906. a.inLen = XSTRLEN(a.input);
  10907. a.outLen = XSTRLEN(a.output);
  10908. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  10909. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  10910. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  10911. /* Unaligned check. */
  10912. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input+1, (word32)a.inLen-1),
  10913. 0);
  10914. ExpectIntEQ(wc_Sha256Final(&sha256, hash_unaligned + 1), 0);
  10915. /* Try passing in bad values */
  10916. b.input = NULL;
  10917. b.inLen = 0;
  10918. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen), 0);
  10919. c.input = NULL;
  10920. c.inLen = WC_SHA256_DIGEST_SIZE;
  10921. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen),
  10922. BAD_FUNC_ARG);
  10923. ExpectIntEQ(wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen),
  10924. BAD_FUNC_ARG);
  10925. wc_Sha256Free(&sha256);
  10926. #endif
  10927. return EXPECT_RESULT();
  10928. } /* END test_wc_Sha256Update */
  10929. /*
  10930. * Unit test function for wc_Sha256Final()
  10931. */
  10932. static int test_wc_Sha256Final(void)
  10933. {
  10934. EXPECT_DECLS;
  10935. #ifndef NO_SHA256
  10936. wc_Sha256 sha256;
  10937. byte* hash_test[3];
  10938. byte hash1[WC_SHA256_DIGEST_SIZE];
  10939. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  10940. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  10941. int times, i;
  10942. /* Initialize */
  10943. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10944. hash_test[0] = hash1;
  10945. hash_test[1] = hash2;
  10946. hash_test[2] = hash3;
  10947. times = sizeof(hash_test) / sizeof(byte*);
  10948. for (i = 0; i < times; i++) {
  10949. ExpectIntEQ(wc_Sha256Final(&sha256, hash_test[i]), 0);
  10950. }
  10951. /* Test bad args. */
  10952. ExpectIntEQ(wc_Sha256Final(NULL, NULL), BAD_FUNC_ARG);
  10953. ExpectIntEQ(wc_Sha256Final(NULL, hash1), BAD_FUNC_ARG);
  10954. ExpectIntEQ(wc_Sha256Final(&sha256, NULL), BAD_FUNC_ARG);
  10955. wc_Sha256Free(&sha256);
  10956. #endif
  10957. return EXPECT_RESULT();
  10958. } /* END test_wc_Sha256Final */
  10959. /*
  10960. * Unit test function for wc_Sha256FinalRaw()
  10961. */
  10962. static int test_wc_Sha256FinalRaw(void)
  10963. {
  10964. EXPECT_DECLS;
  10965. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  10966. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  10967. !defined(WOLFSSL_NO_HASH_RAW)
  10968. wc_Sha256 sha256;
  10969. byte* hash_test[3];
  10970. byte hash1[WC_SHA256_DIGEST_SIZE];
  10971. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  10972. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  10973. int times, i;
  10974. /* Initialize */
  10975. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10976. hash_test[0] = hash1;
  10977. hash_test[1] = hash2;
  10978. hash_test[2] = hash3;
  10979. times = sizeof(hash_test) / sizeof(byte*);
  10980. for (i = 0; i < times; i++) {
  10981. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, hash_test[i]), 0);
  10982. }
  10983. /* Test bad args. */
  10984. ExpectIntEQ(wc_Sha256FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  10985. ExpectIntEQ(wc_Sha256FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  10986. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, NULL), BAD_FUNC_ARG);
  10987. wc_Sha256Free(&sha256);
  10988. #endif
  10989. return EXPECT_RESULT();
  10990. } /* END test_wc_Sha256FinalRaw */
  10991. /*
  10992. * Unit test function for wc_Sha256GetFlags()
  10993. */
  10994. static int test_wc_Sha256GetFlags(void)
  10995. {
  10996. EXPECT_DECLS;
  10997. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  10998. wc_Sha256 sha256;
  10999. word32 flags = 0;
  11000. /* Initialize */
  11001. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11002. ExpectIntEQ(wc_Sha256GetFlags(&sha256, &flags), 0);
  11003. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11004. wc_Sha256Free(&sha256);
  11005. #endif
  11006. return EXPECT_RESULT();
  11007. } /* END test_wc_Sha256GetFlags */
  11008. /*
  11009. * Unit test function for wc_Sha256Free()
  11010. */
  11011. static int test_wc_Sha256Free(void)
  11012. {
  11013. EXPECT_DECLS;
  11014. #ifndef NO_SHA256
  11015. wc_Sha256Free(NULL);
  11016. /* Set result to SUCCESS. */
  11017. ExpectTrue(1);
  11018. #endif
  11019. return EXPECT_RESULT();
  11020. } /* END test_wc_Sha256Free */
  11021. /*
  11022. * Unit test function for wc_Sha256GetHash()
  11023. */
  11024. static int test_wc_Sha256GetHash(void)
  11025. {
  11026. EXPECT_DECLS;
  11027. #ifndef NO_SHA256
  11028. wc_Sha256 sha256;
  11029. byte hash1[WC_SHA256_DIGEST_SIZE];
  11030. /* Initialize */
  11031. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11032. ExpectIntEQ(wc_Sha256GetHash(&sha256, hash1), 0);
  11033. /* test bad arguments*/
  11034. ExpectIntEQ(wc_Sha256GetHash(NULL, NULL), BAD_FUNC_ARG);
  11035. ExpectIntEQ(wc_Sha256GetHash(NULL, hash1), BAD_FUNC_ARG);
  11036. ExpectIntEQ(wc_Sha256GetHash(&sha256, NULL), BAD_FUNC_ARG);
  11037. wc_Sha256Free(&sha256);
  11038. #endif
  11039. return EXPECT_RESULT();
  11040. } /* END test_wc_Sha256GetHash */
  11041. /*
  11042. * Unit test function for wc_Sha256Copy()
  11043. */
  11044. static int test_wc_Sha256Copy(void)
  11045. {
  11046. EXPECT_DECLS;
  11047. #ifndef NO_SHA256
  11048. wc_Sha256 sha256;
  11049. wc_Sha256 temp;
  11050. XMEMSET(&sha256, 0, sizeof(sha256));
  11051. XMEMSET(&temp, 0, sizeof(temp));
  11052. /* Initialize */
  11053. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11054. ExpectIntEQ(wc_InitSha256(&temp), 0);
  11055. ExpectIntEQ(wc_Sha256Copy(&sha256, &temp), 0);
  11056. /* test bad arguments*/
  11057. ExpectIntEQ(wc_Sha256Copy(NULL, NULL), BAD_FUNC_ARG);
  11058. ExpectIntEQ(wc_Sha256Copy(NULL, &temp), BAD_FUNC_ARG);
  11059. ExpectIntEQ(wc_Sha256Copy(&sha256, NULL), BAD_FUNC_ARG);
  11060. wc_Sha256Free(&sha256);
  11061. wc_Sha256Free(&temp);
  11062. #endif
  11063. return EXPECT_RESULT();
  11064. } /* END test_wc_Sha256Copy */
  11065. /*
  11066. * Testing wc_InitSha512()
  11067. */
  11068. static int test_wc_InitSha512(void)
  11069. {
  11070. EXPECT_DECLS;
  11071. #ifdef WOLFSSL_SHA512
  11072. wc_Sha512 sha512;
  11073. /* Test good arg. */
  11074. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11075. /* Test bad arg. */
  11076. ExpectIntEQ(wc_InitSha512(NULL), BAD_FUNC_ARG);
  11077. wc_Sha512Free(&sha512);
  11078. #endif
  11079. return EXPECT_RESULT();
  11080. } /* END test_wc_InitSha512 */
  11081. /*
  11082. * wc_Sha512Update() test.
  11083. */
  11084. static int test_wc_Sha512Update(void)
  11085. {
  11086. EXPECT_DECLS;
  11087. #ifdef WOLFSSL_SHA512
  11088. wc_Sha512 sha512;
  11089. byte hash[WC_SHA512_DIGEST_SIZE];
  11090. byte hash_unaligned[WC_SHA512_DIGEST_SIZE + 1];
  11091. testVector a, b, c;
  11092. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11093. /* Input. */
  11094. a.input = "a";
  11095. a.inLen = XSTRLEN(a.input);
  11096. ExpectIntEQ(wc_Sha512Update(&sha512, NULL, 0), 0);
  11097. ExpectIntEQ(wc_Sha512Update(&sha512,(byte*)a.input, 0), 0);
  11098. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen), 0);
  11099. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  11100. /* Update input. */
  11101. a.input = "abc";
  11102. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  11103. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  11104. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  11105. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  11106. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  11107. a.inLen = XSTRLEN(a.input);
  11108. a.outLen = XSTRLEN(a.output);
  11109. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen), 0);
  11110. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  11111. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE), 0);
  11112. /* Unaligned check. */
  11113. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input+1, (word32)a.inLen-1),
  11114. 0);
  11115. ExpectIntEQ(wc_Sha512Final(&sha512, hash_unaligned+1), 0);
  11116. /* Try passing in bad values */
  11117. b.input = NULL;
  11118. b.inLen = 0;
  11119. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen), 0);
  11120. c.input = NULL;
  11121. c.inLen = WC_SHA512_DIGEST_SIZE;
  11122. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen),
  11123. BAD_FUNC_ARG);
  11124. ExpectIntEQ(wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen),
  11125. BAD_FUNC_ARG);
  11126. wc_Sha512Free(&sha512);
  11127. #endif
  11128. return EXPECT_RESULT();
  11129. } /* END test_wc_Sha512Update */
  11130. #ifdef WOLFSSL_SHA512
  11131. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11132. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  11133. /* Performs test for
  11134. * - wc_Sha512Final/wc_Sha512FinalRaw
  11135. * - wc_Sha512_224Final/wc_Sha512_224Final
  11136. * - wc_Sha512_256Final/wc_Sha512_256Final
  11137. * parameter:
  11138. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  11139. * WC_HASH_TYPE_SHA512_256
  11140. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  11141. *return 0 on success
  11142. */
  11143. static int test_Sha512_Family_Final(int type, int isRaw)
  11144. {
  11145. EXPECT_DECLS;
  11146. wc_Sha512 sha512;
  11147. byte* hash_test[3];
  11148. byte hash1[WC_SHA512_DIGEST_SIZE];
  11149. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11150. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11151. int times, i;
  11152. int(*initFp)(wc_Sha512*);
  11153. int(*finalFp)(wc_Sha512*, byte*);
  11154. void(*freeFp)(wc_Sha512*);
  11155. if (type == WC_HASH_TYPE_SHA512) {
  11156. initFp = wc_InitSha512;
  11157. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11158. !defined(WOLFSSL_NO_HASH_RAW)
  11159. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  11160. #else
  11161. finalFp = (isRaw)? NULL : wc_Sha512Final;
  11162. #endif
  11163. freeFp = wc_Sha512Free;
  11164. }
  11165. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11166. #if !defined(WOLFSSL_NOSHA512_224)
  11167. else if (type == WC_HASH_TYPE_SHA512_224) {
  11168. initFp = wc_InitSha512_224;
  11169. #if !defined(WOLFSSL_NO_HASH_RAW)
  11170. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  11171. #else
  11172. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  11173. #endif
  11174. freeFp = wc_Sha512_224Free;
  11175. }
  11176. #endif
  11177. #if !defined(WOLFSSL_NOSHA512_256)
  11178. else if (type == WC_HASH_TYPE_SHA512_256) {
  11179. initFp = wc_InitSha512_256;
  11180. #if !defined(WOLFSSL_NO_HASH_RAW)
  11181. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  11182. #else
  11183. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  11184. #endif
  11185. freeFp = wc_Sha512_256Free;
  11186. }
  11187. #endif
  11188. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11189. else
  11190. return TEST_FAIL;
  11191. /* Initialize */
  11192. ExpectIntEQ(initFp(&sha512), 0);
  11193. hash_test[0] = hash1;
  11194. hash_test[1] = hash2;
  11195. hash_test[2] = hash3;
  11196. times = sizeof(hash_test) / sizeof(byte *);
  11197. /* Good test args. */
  11198. for (i = 0; i < times; i++) {
  11199. ExpectIntEQ(finalFp(&sha512, hash_test[i]), 0);
  11200. }
  11201. /* Test bad args. */
  11202. ExpectIntEQ(finalFp(NULL, NULL), BAD_FUNC_ARG);
  11203. ExpectIntEQ(finalFp(NULL, hash1), BAD_FUNC_ARG);
  11204. ExpectIntEQ(finalFp(&sha512, NULL), BAD_FUNC_ARG);
  11205. freeFp(&sha512);
  11206. return EXPECT_RESULT();
  11207. }
  11208. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  11209. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  11210. #endif /* WOLFSSL_SHA512 */
  11211. /*
  11212. * Unit test function for wc_Sha512Final()
  11213. */
  11214. static int test_wc_Sha512Final(void)
  11215. {
  11216. EXPECT_DECLS;
  11217. #ifdef WOLFSSL_SHA512
  11218. wc_Sha512 sha512;
  11219. byte* hash_test[3];
  11220. byte hash1[WC_SHA512_DIGEST_SIZE];
  11221. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11222. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11223. int times, i;
  11224. /* Initialize */
  11225. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11226. hash_test[0] = hash1;
  11227. hash_test[1] = hash2;
  11228. hash_test[2] = hash3;
  11229. times = sizeof(hash_test) / sizeof(byte *);
  11230. for (i = 0; i < times; i++) {
  11231. ExpectIntEQ(wc_Sha512Final(&sha512, hash_test[i]), 0);
  11232. }
  11233. /* Test bad args. */
  11234. ExpectIntEQ(wc_Sha512Final(NULL, NULL), BAD_FUNC_ARG);
  11235. ExpectIntEQ(wc_Sha512Final(NULL, hash1), BAD_FUNC_ARG);
  11236. ExpectIntEQ(wc_Sha512Final(&sha512, NULL), BAD_FUNC_ARG);
  11237. wc_Sha512Free(&sha512);
  11238. #endif
  11239. return EXPECT_RESULT();
  11240. } /* END test_wc_Sha512Final */
  11241. /*
  11242. * Unit test function for wc_Sha512GetFlags()
  11243. */
  11244. static int test_wc_Sha512GetFlags(void)
  11245. {
  11246. EXPECT_DECLS;
  11247. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  11248. wc_Sha512 sha512;
  11249. word32 flags = 0;
  11250. /* Initialize */
  11251. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11252. ExpectIntEQ(wc_Sha512GetFlags(&sha512, &flags), 0);
  11253. ExpectIntEQ((flags & WC_HASH_FLAG_ISCOPY), 0);
  11254. wc_Sha512Free(&sha512);
  11255. #endif
  11256. return EXPECT_RESULT();
  11257. } /* END test_wc_Sha512GetFlags */
  11258. /*
  11259. * Unit test function for wc_Sha512FinalRaw()
  11260. */
  11261. static int test_wc_Sha512FinalRaw(void)
  11262. {
  11263. EXPECT_DECLS;
  11264. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  11265. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  11266. !defined(WOLFSSL_NO_HASH_RAW)
  11267. wc_Sha512 sha512;
  11268. byte* hash_test[3];
  11269. byte hash1[WC_SHA512_DIGEST_SIZE];
  11270. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11271. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11272. int times, i;
  11273. /* Initialize */
  11274. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11275. hash_test[0] = hash1;
  11276. hash_test[1] = hash2;
  11277. hash_test[2] = hash3;
  11278. times = sizeof(hash_test) / sizeof(byte*);
  11279. /* Good test args. */
  11280. for (i = 0; i < times; i++) {
  11281. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, hash_test[i]), 0);
  11282. }
  11283. /* Test bad args. */
  11284. ExpectIntEQ(wc_Sha512FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  11285. ExpectIntEQ(wc_Sha512FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  11286. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, NULL), BAD_FUNC_ARG);
  11287. wc_Sha512Free(&sha512);
  11288. #endif
  11289. return EXPECT_RESULT();
  11290. } /* END test_wc_Sha512FinalRaw */
  11291. /*
  11292. * Unit test function for wc_Sha512Free()
  11293. */
  11294. static int test_wc_Sha512Free(void)
  11295. {
  11296. EXPECT_DECLS;
  11297. #ifdef WOLFSSL_SHA512
  11298. wc_Sha512Free(NULL);
  11299. /* Set result to SUCCESS. */
  11300. ExpectTrue(1);
  11301. #endif
  11302. return EXPECT_RESULT();
  11303. } /* END test_wc_Sha512Free */
  11304. #ifdef WOLFSSL_SHA512
  11305. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11306. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  11307. static int test_Sha512_Family_GetHash(int type )
  11308. {
  11309. EXPECT_DECLS;
  11310. int(*initFp)(wc_Sha512*);
  11311. int(*ghashFp)(wc_Sha512*, byte*);
  11312. wc_Sha512 sha512;
  11313. byte hash1[WC_SHA512_DIGEST_SIZE];
  11314. if (type == WC_HASH_TYPE_SHA512) {
  11315. initFp = wc_InitSha512;
  11316. ghashFp = wc_Sha512GetHash;
  11317. }
  11318. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11319. #if !defined(WOLFSSL_NOSHA512_224)
  11320. else if (type == WC_HASH_TYPE_SHA512_224) {
  11321. initFp = wc_InitSha512_224;
  11322. ghashFp = wc_Sha512_224GetHash;
  11323. }
  11324. #endif
  11325. #if !defined(WOLFSSL_NOSHA512_256)
  11326. else if (type == WC_HASH_TYPE_SHA512_256) {
  11327. initFp = wc_InitSha512_256;
  11328. ghashFp = wc_Sha512_256GetHash;
  11329. }
  11330. #endif
  11331. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11332. else {
  11333. initFp = NULL;
  11334. ghashFp = NULL;
  11335. }
  11336. if (initFp == NULL || ghashFp == NULL)
  11337. return TEST_FAIL;
  11338. ExpectIntEQ(initFp(&sha512), 0);
  11339. ExpectIntEQ(ghashFp(&sha512, hash1), 0);
  11340. /* test bad arguments*/
  11341. ExpectIntEQ(ghashFp(NULL, NULL), BAD_FUNC_ARG);
  11342. ExpectIntEQ(ghashFp(NULL, hash1), BAD_FUNC_ARG);
  11343. ExpectIntEQ(ghashFp(&sha512, NULL), BAD_FUNC_ARG);
  11344. wc_Sha512Free(&sha512);
  11345. return EXPECT_RESULT();
  11346. }
  11347. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  11348. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  11349. #endif /* WOLFSSL_SHA512 */
  11350. /*
  11351. * Unit test function for wc_Sha512GetHash()
  11352. */
  11353. static int test_wc_Sha512GetHash(void)
  11354. {
  11355. EXPECT_DECLS;
  11356. #ifdef WOLFSSL_SHA512
  11357. wc_Sha512 sha512;
  11358. byte hash1[WC_SHA512_DIGEST_SIZE];
  11359. /* Initialize */
  11360. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11361. ExpectIntEQ(wc_Sha512GetHash(&sha512, hash1), 0);
  11362. /* test bad arguments*/
  11363. ExpectIntEQ(wc_Sha512GetHash(NULL, NULL), BAD_FUNC_ARG);
  11364. ExpectIntEQ(wc_Sha512GetHash(NULL, hash1), BAD_FUNC_ARG);
  11365. ExpectIntEQ(wc_Sha512GetHash(&sha512, NULL), BAD_FUNC_ARG);
  11366. wc_Sha512Free(&sha512);
  11367. #endif
  11368. return EXPECT_RESULT();
  11369. } /* END test_wc_Sha512GetHash */
  11370. /*
  11371. * Unit test function for wc_Sha512Copy()
  11372. */
  11373. static int test_wc_Sha512Copy(void)
  11374. {
  11375. EXPECT_DECLS;
  11376. #ifdef WOLFSSL_SHA512
  11377. wc_Sha512 sha512;
  11378. wc_Sha512 temp;
  11379. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11380. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  11381. /* Initialize */
  11382. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11383. ExpectIntEQ(wc_InitSha512(&temp), 0);
  11384. ExpectIntEQ(wc_Sha512Copy(&sha512, &temp), 0);
  11385. /* test bad arguments*/
  11386. ExpectIntEQ(wc_Sha512Copy(NULL, NULL), BAD_FUNC_ARG);
  11387. ExpectIntEQ(wc_Sha512Copy(NULL, &temp), BAD_FUNC_ARG);
  11388. ExpectIntEQ(wc_Sha512Copy(&sha512, NULL), BAD_FUNC_ARG);
  11389. wc_Sha512Free(&sha512);
  11390. wc_Sha512Free(&temp);
  11391. #endif
  11392. return EXPECT_RESULT();
  11393. } /* END test_wc_Sha512Copy */
  11394. static int test_wc_InitSha512_224(void)
  11395. {
  11396. EXPECT_DECLS;
  11397. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11398. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11399. wc_Sha512 sha512;
  11400. /* Test good arg. */
  11401. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11402. /* Test bad arg. */
  11403. ExpectIntEQ(wc_InitSha512_224(NULL), BAD_FUNC_ARG);
  11404. wc_Sha512_224Free(&sha512);
  11405. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11406. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11407. return EXPECT_RESULT();
  11408. }
  11409. static int test_wc_Sha512_224Update(void)
  11410. {
  11411. EXPECT_DECLS;
  11412. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11413. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11414. wc_Sha512 sha512;
  11415. byte hash[WC_SHA512_DIGEST_SIZE];
  11416. testVector a, c;
  11417. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11418. /* Input. */
  11419. a.input = "a";
  11420. a.inLen = XSTRLEN(a.input);
  11421. ExpectIntEQ(wc_Sha512_224Update(&sha512, NULL, 0), 0);
  11422. ExpectIntEQ(wc_Sha512_224Update(&sha512,(byte*)a.input, 0), 0);
  11423. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen),
  11424. 0);
  11425. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  11426. /* Update input. */
  11427. a.input = "abc";
  11428. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  11429. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  11430. a.inLen = XSTRLEN(a.input);
  11431. a.outLen = XSTRLEN(a.output);
  11432. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen),
  11433. 0);
  11434. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  11435. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE), 0);
  11436. c.input = NULL;
  11437. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  11438. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen),
  11439. BAD_FUNC_ARG);
  11440. ExpectIntEQ(wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen),
  11441. BAD_FUNC_ARG);
  11442. wc_Sha512_224Free(&sha512);
  11443. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11444. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11445. return EXPECT_RESULT();
  11446. }
  11447. static int test_wc_Sha512_224Final(void)
  11448. {
  11449. EXPECT_DECLS;
  11450. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11451. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11452. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0),
  11453. TEST_SUCCESS);
  11454. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11455. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11456. return EXPECT_RESULT();
  11457. }
  11458. static int test_wc_Sha512_224GetFlags(void)
  11459. {
  11460. EXPECT_DECLS;
  11461. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11462. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  11463. wc_Sha512 sha512;
  11464. wc_Sha512 copy;
  11465. word32 flags = 0;
  11466. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11467. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  11468. /* Initialize */
  11469. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11470. ExpectIntEQ(wc_InitSha512_224(&copy), 0);
  11471. ExpectIntEQ(wc_Sha512_224GetFlags(&sha512, &flags), 0);
  11472. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11473. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &copy), 0);
  11474. ExpectIntEQ(wc_Sha512_224GetFlags(&copy, &flags), 0);
  11475. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  11476. wc_Sha512_224Free(&copy);
  11477. wc_Sha512_224Free(&sha512);
  11478. #endif
  11479. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11480. return EXPECT_RESULT();
  11481. }
  11482. static int test_wc_Sha512_224FinalRaw(void)
  11483. {
  11484. EXPECT_DECLS;
  11485. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11486. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  11487. !defined(WOLFSSL_NO_HASH_RAW)
  11488. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1),
  11489. TEST_SUCCESS);
  11490. #endif
  11491. return EXPECT_RESULT();
  11492. }
  11493. static int test_wc_Sha512_224Free(void)
  11494. {
  11495. EXPECT_DECLS;
  11496. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11497. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11498. wc_Sha512_224Free(NULL);
  11499. /* Set result to SUCCESS. */
  11500. ExpectTrue(1);
  11501. #endif
  11502. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11503. return EXPECT_RESULT();
  11504. }
  11505. static int test_wc_Sha512_224GetHash(void)
  11506. {
  11507. EXPECT_DECLS;
  11508. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11509. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11510. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224),
  11511. TEST_SUCCESS);
  11512. #endif
  11513. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11514. return EXPECT_RESULT();
  11515. }
  11516. static int test_wc_Sha512_224Copy(void)
  11517. {
  11518. EXPECT_DECLS;
  11519. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11520. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11521. wc_Sha512 sha512;
  11522. wc_Sha512 temp;
  11523. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11524. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  11525. /* Initialize */
  11526. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11527. ExpectIntEQ(wc_InitSha512_224(&temp), 0);
  11528. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &temp), 0);
  11529. /* test bad arguments*/
  11530. ExpectIntEQ(wc_Sha512_224Copy(NULL, NULL), BAD_FUNC_ARG);
  11531. ExpectIntEQ(wc_Sha512_224Copy(NULL, &temp), BAD_FUNC_ARG);
  11532. ExpectIntEQ(wc_Sha512_224Copy(&sha512, NULL), BAD_FUNC_ARG);
  11533. wc_Sha512_224Free(&sha512);
  11534. wc_Sha512_224Free(&temp);
  11535. #endif
  11536. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11537. return EXPECT_RESULT();
  11538. }
  11539. static int test_wc_InitSha512_256(void)
  11540. {
  11541. EXPECT_DECLS;
  11542. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11543. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11544. wc_Sha512 sha512;
  11545. /* Test good arg. */
  11546. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  11547. /* Test bad arg. */
  11548. ExpectIntEQ(wc_InitSha512_256(NULL), BAD_FUNC_ARG);
  11549. wc_Sha512_256Free(&sha512);
  11550. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11551. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11552. return EXPECT_RESULT();
  11553. }
  11554. static int test_wc_Sha512_256Update(void)
  11555. {
  11556. EXPECT_DECLS;
  11557. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11558. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11559. wc_Sha512 sha512;
  11560. byte hash[WC_SHA512_DIGEST_SIZE];
  11561. testVector a, c;
  11562. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  11563. /* Input. */
  11564. a.input = "a";
  11565. a.inLen = XSTRLEN(a.input);
  11566. ExpectIntEQ(wc_Sha512_256Update(&sha512, NULL, 0), 0);
  11567. ExpectIntEQ(wc_Sha512_256Update(&sha512,(byte*)a.input, 0), 0);
  11568. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen),
  11569. 0);
  11570. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  11571. /* Update input. */
  11572. a.input = "abc";
  11573. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  11574. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  11575. "\x07\xe7\xaf\x23";
  11576. a.inLen = XSTRLEN(a.input);
  11577. a.outLen = XSTRLEN(a.output);
  11578. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen),
  11579. 0);
  11580. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  11581. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE), 0);
  11582. c.input = NULL;
  11583. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  11584. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen),
  11585. BAD_FUNC_ARG);
  11586. ExpectIntEQ(wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen),
  11587. BAD_FUNC_ARG);
  11588. wc_Sha512_256Free(&sha512);
  11589. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11590. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11591. return EXPECT_RESULT();
  11592. }
  11593. static int test_wc_Sha512_256Final(void)
  11594. {
  11595. EXPECT_DECLS;
  11596. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11597. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11598. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0),
  11599. TEST_SUCCESS);
  11600. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11601. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11602. return EXPECT_RESULT();
  11603. }
  11604. static int test_wc_Sha512_256GetFlags(void)
  11605. {
  11606. EXPECT_DECLS;
  11607. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11608. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  11609. wc_Sha512 sha512, copy;
  11610. word32 flags = 0;
  11611. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11612. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  11613. /* Initialize */
  11614. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  11615. ExpectIntEQ(wc_InitSha512_256(&copy), 0);
  11616. ExpectIntEQ(wc_Sha512_256GetFlags(&sha512, &flags), 0);
  11617. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11618. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &copy), 0);
  11619. ExpectIntEQ(wc_Sha512_256GetFlags(&copy, &flags), 0);
  11620. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  11621. wc_Sha512_256Free(&sha512);
  11622. #endif
  11623. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11624. return EXPECT_RESULT();
  11625. }
  11626. static int test_wc_Sha512_256FinalRaw(void)
  11627. {
  11628. EXPECT_DECLS;
  11629. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11630. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  11631. !defined(WOLFSSL_NO_HASH_RAW)
  11632. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1),
  11633. TEST_SUCCESS);
  11634. #endif
  11635. return EXPECT_RESULT();
  11636. }
  11637. static int test_wc_Sha512_256Free(void)
  11638. {
  11639. EXPECT_DECLS;
  11640. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11641. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11642. wc_Sha512_256Free(NULL);
  11643. /* Set result to SUCCESS. */
  11644. ExpectTrue(1);
  11645. #endif
  11646. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11647. return EXPECT_RESULT();
  11648. }
  11649. static int test_wc_Sha512_256GetHash(void)
  11650. {
  11651. EXPECT_DECLS;
  11652. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11653. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11654. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256),
  11655. TEST_SUCCESS);
  11656. #endif
  11657. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11658. return EXPECT_RESULT();
  11659. }
  11660. static int test_wc_Sha512_256Copy(void)
  11661. {
  11662. EXPECT_DECLS;
  11663. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11664. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11665. wc_Sha512 sha512;
  11666. wc_Sha512 temp;
  11667. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11668. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  11669. /* Initialize */
  11670. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  11671. ExpectIntEQ(wc_InitSha512_256(&temp), 0);
  11672. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &temp), 0);
  11673. /* test bad arguments*/
  11674. ExpectIntEQ(wc_Sha512_256Copy(NULL, NULL), BAD_FUNC_ARG);
  11675. ExpectIntEQ(wc_Sha512_256Copy(NULL, &temp), BAD_FUNC_ARG);
  11676. ExpectIntEQ(wc_Sha512_256Copy(&sha512, NULL), BAD_FUNC_ARG);
  11677. wc_Sha512_256Free(&sha512);
  11678. wc_Sha512_256Free(&temp);
  11679. #endif
  11680. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11681. return EXPECT_RESULT();
  11682. }
  11683. /*
  11684. * Testing wc_InitSha384()
  11685. */
  11686. static int test_wc_InitSha384(void)
  11687. {
  11688. EXPECT_DECLS;
  11689. #ifdef WOLFSSL_SHA384
  11690. wc_Sha384 sha384;
  11691. /* Test good arg. */
  11692. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11693. /* Test bad arg. */
  11694. ExpectIntEQ(wc_InitSha384(NULL), BAD_FUNC_ARG);
  11695. wc_Sha384Free(&sha384);
  11696. #endif
  11697. return EXPECT_RESULT();
  11698. } /* END test_wc_InitSha384 */
  11699. /*
  11700. * test wc_Sha384Update()
  11701. */
  11702. static int test_wc_Sha384Update(void)
  11703. {
  11704. EXPECT_DECLS;
  11705. #ifdef WOLFSSL_SHA384
  11706. wc_Sha384 sha384;
  11707. byte hash[WC_SHA384_DIGEST_SIZE];
  11708. testVector a, b, c;
  11709. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11710. /* Input */
  11711. a.input = "a";
  11712. a.inLen = XSTRLEN(a.input);
  11713. ExpectIntEQ(wc_Sha384Update(&sha384, NULL, 0), 0);
  11714. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, 0), 0);
  11715. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  11716. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  11717. /* Update input. */
  11718. a.input = "abc";
  11719. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  11720. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  11721. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  11722. "\xc8\x25\xa7";
  11723. a.inLen = XSTRLEN(a.input);
  11724. a.outLen = XSTRLEN(a.output);
  11725. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  11726. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  11727. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  11728. /* Pass in bad values. */
  11729. b.input = NULL;
  11730. b.inLen = 0;
  11731. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen), 0);
  11732. c.input = NULL;
  11733. c.inLen = WC_SHA384_DIGEST_SIZE;
  11734. ExpectIntEQ( wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen),
  11735. BAD_FUNC_ARG);
  11736. ExpectIntEQ(wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen),
  11737. BAD_FUNC_ARG);
  11738. wc_Sha384Free(&sha384);
  11739. #endif
  11740. return EXPECT_RESULT();
  11741. } /* END test_wc_Sha384Update */
  11742. /*
  11743. * Unit test function for wc_Sha384Final();
  11744. */
  11745. static int test_wc_Sha384Final(void)
  11746. {
  11747. EXPECT_DECLS;
  11748. #ifdef WOLFSSL_SHA384
  11749. wc_Sha384 sha384;
  11750. byte* hash_test[3];
  11751. byte hash1[WC_SHA384_DIGEST_SIZE];
  11752. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  11753. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  11754. int times, i;
  11755. /* Initialize */
  11756. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11757. hash_test[0] = hash1;
  11758. hash_test[1] = hash2;
  11759. hash_test[2] = hash3;
  11760. times = sizeof(hash_test) / sizeof(byte*);
  11761. /* Good test args. */
  11762. for (i = 0; i < times; i++) {
  11763. ExpectIntEQ(wc_Sha384Final(&sha384, hash_test[i]), 0);
  11764. }
  11765. /* Test bad args. */
  11766. ExpectIntEQ(wc_Sha384Final(NULL, NULL), BAD_FUNC_ARG);
  11767. ExpectIntEQ(wc_Sha384Final(NULL, hash1), BAD_FUNC_ARG);
  11768. ExpectIntEQ(wc_Sha384Final(&sha384, NULL), BAD_FUNC_ARG);
  11769. wc_Sha384Free(&sha384);
  11770. #endif
  11771. return EXPECT_RESULT();
  11772. } /* END test_wc_Sha384Final */
  11773. /*
  11774. * Unit test function for wc_Sha384GetFlags()
  11775. */
  11776. static int test_wc_Sha384GetFlags(void)
  11777. {
  11778. EXPECT_DECLS;
  11779. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  11780. wc_Sha384 sha384;
  11781. word32 flags = 0;
  11782. /* Initialize */
  11783. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11784. ExpectIntEQ(wc_Sha384GetFlags(&sha384, &flags), 0);
  11785. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11786. wc_Sha384Free(&sha384);
  11787. #endif
  11788. return EXPECT_RESULT();
  11789. } /* END test_wc_Sha384GetFlags */
  11790. /*
  11791. * Unit test function for wc_Sha384FinalRaw()
  11792. */
  11793. static int test_wc_Sha384FinalRaw(void)
  11794. {
  11795. EXPECT_DECLS;
  11796. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  11797. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  11798. !defined(WOLFSSL_NO_HASH_RAW)
  11799. wc_Sha384 sha384;
  11800. byte* hash_test[3];
  11801. byte hash1[WC_SHA384_DIGEST_SIZE];
  11802. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  11803. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  11804. int times, i;
  11805. /* Initialize */
  11806. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11807. hash_test[0] = hash1;
  11808. hash_test[1] = hash2;
  11809. hash_test[2] = hash3;
  11810. times = sizeof(hash_test) / sizeof(byte*);
  11811. /* Good test args. */
  11812. for (i = 0; i < times; i++) {
  11813. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, hash_test[i]), 0);
  11814. }
  11815. /* Test bad args. */
  11816. ExpectIntEQ(wc_Sha384FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  11817. ExpectIntEQ(wc_Sha384FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  11818. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, NULL), BAD_FUNC_ARG);
  11819. wc_Sha384Free(&sha384);
  11820. #endif
  11821. return EXPECT_RESULT();
  11822. } /* END test_wc_Sha384FinalRaw */
  11823. /*
  11824. * Unit test function for wc_Sha384Free()
  11825. */
  11826. static int test_wc_Sha384Free(void)
  11827. {
  11828. EXPECT_DECLS;
  11829. #ifdef WOLFSSL_SHA384
  11830. wc_Sha384Free(NULL);
  11831. /* Set result to SUCCESS. */
  11832. ExpectTrue(1);
  11833. #endif
  11834. return EXPECT_RESULT();
  11835. } /* END test_wc_Sha384Free */
  11836. /*
  11837. * Unit test function for wc_Sha384GetHash()
  11838. */
  11839. static int test_wc_Sha384GetHash(void)
  11840. {
  11841. EXPECT_DECLS;
  11842. #ifdef WOLFSSL_SHA384
  11843. wc_Sha384 sha384;
  11844. byte hash1[WC_SHA384_DIGEST_SIZE];
  11845. /* Initialize */
  11846. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11847. ExpectIntEQ(wc_Sha384GetHash(&sha384, hash1), 0);
  11848. /* test bad arguments*/
  11849. ExpectIntEQ(wc_Sha384GetHash(NULL, NULL), BAD_FUNC_ARG);
  11850. ExpectIntEQ(wc_Sha384GetHash(NULL, hash1), BAD_FUNC_ARG);
  11851. ExpectIntEQ(wc_Sha384GetHash(&sha384, NULL), BAD_FUNC_ARG);
  11852. wc_Sha384Free(&sha384);
  11853. #endif
  11854. return EXPECT_RESULT();
  11855. } /* END test_wc_Sha384GetHash */
  11856. /*
  11857. * Unit test function for wc_Sha384Copy()
  11858. */
  11859. static int test_wc_Sha384Copy(void)
  11860. {
  11861. EXPECT_DECLS;
  11862. #ifdef WOLFSSL_SHA384
  11863. wc_Sha384 sha384;
  11864. wc_Sha384 temp;
  11865. XMEMSET(&sha384, 0, sizeof(wc_Sha384));
  11866. XMEMSET(&temp, 0, sizeof(wc_Sha384));
  11867. /* Initialize */
  11868. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11869. ExpectIntEQ(wc_InitSha384(&temp), 0);
  11870. ExpectIntEQ(wc_Sha384Copy(&sha384, &temp), 0);
  11871. /* test bad arguments*/
  11872. ExpectIntEQ(wc_Sha384Copy(NULL, NULL), BAD_FUNC_ARG);
  11873. ExpectIntEQ(wc_Sha384Copy(NULL, &temp), BAD_FUNC_ARG);
  11874. ExpectIntEQ(wc_Sha384Copy(&sha384, NULL), BAD_FUNC_ARG);
  11875. wc_Sha384Free(&sha384);
  11876. wc_Sha384Free(&temp);
  11877. #endif
  11878. return EXPECT_RESULT();
  11879. } /* END test_wc_Sha384Copy */
  11880. /*
  11881. * Testing wc_InitSha224();
  11882. */
  11883. static int test_wc_InitSha224(void)
  11884. {
  11885. EXPECT_DECLS;
  11886. #ifdef WOLFSSL_SHA224
  11887. wc_Sha224 sha224;
  11888. /* Test good arg. */
  11889. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11890. /* Test bad arg. */
  11891. ExpectIntEQ(wc_InitSha224(NULL), BAD_FUNC_ARG);
  11892. wc_Sha224Free(&sha224);
  11893. #endif
  11894. return EXPECT_RESULT();
  11895. } /* END test_wc_InitSha224 */
  11896. /*
  11897. * Unit test on wc_Sha224Update
  11898. */
  11899. static int test_wc_Sha224Update(void)
  11900. {
  11901. EXPECT_DECLS;
  11902. #ifdef WOLFSSL_SHA224
  11903. wc_Sha224 sha224;
  11904. byte hash[WC_SHA224_DIGEST_SIZE];
  11905. testVector a, b, c;
  11906. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11907. /* Input. */
  11908. a.input = "a";
  11909. a.inLen = XSTRLEN(a.input);
  11910. ExpectIntEQ(wc_Sha224Update(&sha224, NULL, 0), 0);
  11911. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, 0), 0);
  11912. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  11913. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  11914. /* Update input. */
  11915. a.input = "abc";
  11916. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  11917. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  11918. a.inLen = XSTRLEN(a.input);
  11919. a.outLen = XSTRLEN(a.output);
  11920. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  11921. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  11922. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  11923. /* Pass in bad values. */
  11924. b.input = NULL;
  11925. b.inLen = 0;
  11926. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen), 0);
  11927. c.input = NULL;
  11928. c.inLen = WC_SHA224_DIGEST_SIZE;
  11929. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen),
  11930. BAD_FUNC_ARG);
  11931. ExpectIntEQ(wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen),
  11932. BAD_FUNC_ARG);
  11933. wc_Sha224Free(&sha224);
  11934. #endif
  11935. return EXPECT_RESULT();
  11936. } /* END test_wc_Sha224Update */
  11937. /*
  11938. * Unit test for wc_Sha224Final();
  11939. */
  11940. static int test_wc_Sha224Final(void)
  11941. {
  11942. EXPECT_DECLS;
  11943. #ifdef WOLFSSL_SHA224
  11944. wc_Sha224 sha224;
  11945. byte* hash_test[3];
  11946. byte hash1[WC_SHA224_DIGEST_SIZE];
  11947. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  11948. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  11949. int times, i;
  11950. /* Initialize */
  11951. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11952. hash_test[0] = hash1;
  11953. hash_test[1] = hash2;
  11954. hash_test[2] = hash3;
  11955. times = sizeof(hash_test) / sizeof(byte*);
  11956. /* Good test args. */
  11957. /* Testing oversized buffers. */
  11958. for (i = 0; i < times; i++) {
  11959. ExpectIntEQ(wc_Sha224Final(&sha224, hash_test[i]), 0);
  11960. }
  11961. /* Test bad args. */
  11962. ExpectIntEQ(wc_Sha224Final(NULL, NULL), BAD_FUNC_ARG);
  11963. ExpectIntEQ(wc_Sha224Final(NULL, hash1), BAD_FUNC_ARG);
  11964. ExpectIntEQ(wc_Sha224Final(&sha224, NULL), BAD_FUNC_ARG);
  11965. wc_Sha224Free(&sha224);
  11966. #endif
  11967. return EXPECT_RESULT();
  11968. } /* END test_wc_Sha224Final */
  11969. /*
  11970. * Unit test function for wc_Sha224SetFlags()
  11971. */
  11972. static int test_wc_Sha224SetFlags(void)
  11973. {
  11974. EXPECT_DECLS;
  11975. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  11976. wc_Sha224 sha224;
  11977. word32 flags = WC_HASH_FLAG_WILLCOPY;
  11978. /* Initialize */
  11979. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11980. ExpectIntEQ(wc_Sha224SetFlags(&sha224, flags), 0);
  11981. flags = 0;
  11982. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  11983. ExpectTrue(flags == WC_HASH_FLAG_WILLCOPY);
  11984. wc_Sha224Free(&sha224);
  11985. #endif
  11986. return EXPECT_RESULT();
  11987. } /* END test_wc_Sha224SetFlags */
  11988. /*
  11989. * Unit test function for wc_Sha224GetFlags()
  11990. */
  11991. static int test_wc_Sha224GetFlags(void)
  11992. {
  11993. EXPECT_DECLS;
  11994. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  11995. wc_Sha224 sha224;
  11996. word32 flags = 0;
  11997. /* Initialize */
  11998. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11999. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  12000. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12001. wc_Sha224Free(&sha224);
  12002. #endif
  12003. return EXPECT_RESULT();
  12004. } /* END test_wc_Sha224GetFlags */
  12005. /*
  12006. * Unit test function for wc_Sha224Free()
  12007. */
  12008. static int test_wc_Sha224Free(void)
  12009. {
  12010. EXPECT_DECLS;
  12011. #ifdef WOLFSSL_SHA224
  12012. wc_Sha224Free(NULL);
  12013. /* Set result to SUCCESS. */
  12014. ExpectTrue(1);
  12015. #endif
  12016. return EXPECT_RESULT();
  12017. } /* END test_wc_Sha224Free */
  12018. /*
  12019. * Unit test function for wc_Sha224GetHash()
  12020. */
  12021. static int test_wc_Sha224GetHash(void)
  12022. {
  12023. EXPECT_DECLS;
  12024. #ifdef WOLFSSL_SHA224
  12025. wc_Sha224 sha224;
  12026. byte hash1[WC_SHA224_DIGEST_SIZE];
  12027. /* Initialize */
  12028. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12029. ExpectIntEQ(wc_Sha224GetHash(&sha224, hash1), 0);
  12030. /* test bad arguments*/
  12031. ExpectIntEQ(wc_Sha224GetHash(NULL, NULL), BAD_FUNC_ARG);
  12032. ExpectIntEQ(wc_Sha224GetHash(NULL, hash1), BAD_FUNC_ARG);
  12033. ExpectIntEQ(wc_Sha224GetHash(&sha224, NULL), BAD_FUNC_ARG);
  12034. wc_Sha224Free(&sha224);
  12035. #endif
  12036. return EXPECT_RESULT();
  12037. } /* END test_wc_Sha224GetHash */
  12038. /*
  12039. * Unit test function for wc_Sha224Copy()
  12040. */
  12041. static int test_wc_Sha224Copy(void)
  12042. {
  12043. EXPECT_DECLS;
  12044. #ifdef WOLFSSL_SHA224
  12045. wc_Sha224 sha224;
  12046. wc_Sha224 temp;
  12047. XMEMSET(&sha224, 0, sizeof(wc_Sha224));
  12048. XMEMSET(&temp, 0, sizeof(wc_Sha224));
  12049. /* Initialize */
  12050. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12051. ExpectIntEQ(wc_InitSha224(&temp), 0);
  12052. ExpectIntEQ(wc_Sha224Copy(&sha224, &temp), 0);
  12053. /* test bad arguments*/
  12054. ExpectIntEQ(wc_Sha224Copy(NULL, NULL), BAD_FUNC_ARG);
  12055. ExpectIntEQ(wc_Sha224Copy(NULL, &temp), BAD_FUNC_ARG);
  12056. ExpectIntEQ(wc_Sha224Copy(&sha224, NULL), BAD_FUNC_ARG);
  12057. wc_Sha224Free(&sha224);
  12058. wc_Sha224Free(&temp);
  12059. #endif
  12060. return EXPECT_RESULT();
  12061. } /* END test_wc_Sha224Copy */
  12062. /*
  12063. * Testing wc_InitRipeMd()
  12064. */
  12065. static int test_wc_InitRipeMd(void)
  12066. {
  12067. EXPECT_DECLS;
  12068. #ifdef WOLFSSL_RIPEMD
  12069. RipeMd ripemd;
  12070. /* Test good arg. */
  12071. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  12072. /* Test bad arg. */
  12073. ExpectIntEQ(wc_InitRipeMd(NULL), BAD_FUNC_ARG);
  12074. #endif
  12075. return EXPECT_RESULT();
  12076. } /* END test_wc_InitRipeMd */
  12077. /*
  12078. * Testing wc_RipeMdUpdate()
  12079. */
  12080. static int test_wc_RipeMdUpdate(void)
  12081. {
  12082. EXPECT_DECLS;
  12083. #ifdef WOLFSSL_RIPEMD
  12084. RipeMd ripemd;
  12085. byte hash[RIPEMD_DIGEST_SIZE];
  12086. testVector a, b, c;
  12087. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  12088. /* Input */
  12089. a.input = "a";
  12090. a.inLen = XSTRLEN(a.input);
  12091. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  12092. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  12093. /* Update input. */
  12094. a.input = "abc";
  12095. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  12096. "\xb0\x87\xf1\x5a\x0b\xfc";
  12097. a.inLen = XSTRLEN(a.input);
  12098. a.outLen = XSTRLEN(a.output);
  12099. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  12100. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  12101. ExpectIntEQ(XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE), 0);
  12102. /* Pass in bad values. */
  12103. b.input = NULL;
  12104. b.inLen = 0;
  12105. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen), 0);
  12106. c.input = NULL;
  12107. c.inLen = RIPEMD_DIGEST_SIZE;
  12108. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen),
  12109. BAD_FUNC_ARG);
  12110. ExpectIntEQ(wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  12111. BAD_FUNC_ARG);
  12112. #endif
  12113. return EXPECT_RESULT();
  12114. } /* END test_wc_RipeMdUdpate */
  12115. /*
  12116. * Unit test function for wc_RipeMdFinal()
  12117. */
  12118. static int test_wc_RipeMdFinal(void)
  12119. {
  12120. EXPECT_DECLS;
  12121. #ifdef WOLFSSL_RIPEMD
  12122. RipeMd ripemd;
  12123. byte* hash_test[3];
  12124. byte hash1[RIPEMD_DIGEST_SIZE];
  12125. byte hash2[2*RIPEMD_DIGEST_SIZE];
  12126. byte hash3[5*RIPEMD_DIGEST_SIZE];
  12127. int times, i;
  12128. /* Initialize */
  12129. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  12130. hash_test[0] = hash1;
  12131. hash_test[1] = hash2;
  12132. hash_test[2] = hash3;
  12133. times = sizeof(hash_test) / sizeof(byte*);
  12134. /* Testing oversized buffers. */
  12135. for (i = 0; i < times; i++) {
  12136. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash_test[i]), 0);
  12137. }
  12138. /* Test bad args. */
  12139. ExpectIntEQ(wc_RipeMdFinal(NULL, NULL), BAD_FUNC_ARG);
  12140. ExpectIntEQ(wc_RipeMdFinal(NULL, hash1), BAD_FUNC_ARG);
  12141. ExpectIntEQ(wc_RipeMdFinal(&ripemd, NULL), BAD_FUNC_ARG);
  12142. #endif
  12143. return EXPECT_RESULT();
  12144. } /* END test_wc_RipeMdFinal */
  12145. /*
  12146. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  12147. * wc_InitSha3_512
  12148. */
  12149. static int test_wc_InitSha3(void)
  12150. {
  12151. EXPECT_DECLS;
  12152. #if defined(WOLFSSL_SHA3)
  12153. wc_Sha3 sha3;
  12154. (void)sha3;
  12155. #if !defined(WOLFSSL_NOSHA3_224)
  12156. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12157. /* Test bad args. */
  12158. ExpectIntEQ(wc_InitSha3_224(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12159. wc_Sha3_224_Free(&sha3);
  12160. #endif /* NOSHA3_224 */
  12161. #if !defined(WOLFSSL_NOSHA3_256)
  12162. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12163. /* Test bad args. */
  12164. ExpectIntEQ(wc_InitSha3_256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12165. wc_Sha3_256_Free(&sha3);
  12166. #endif /* NOSHA3_256 */
  12167. #if !defined(WOLFSSL_NOSHA3_384)
  12168. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12169. /* Test bad args. */
  12170. ExpectIntEQ(wc_InitSha3_384(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12171. wc_Sha3_384_Free(&sha3);
  12172. #endif /* NOSHA3_384 */
  12173. #if !defined(WOLFSSL_NOSHA3_512)
  12174. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12175. /* Test bad args. */
  12176. ExpectIntEQ(wc_InitSha3_512(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12177. wc_Sha3_512_Free(&sha3);
  12178. #endif /* NOSHA3_512 */
  12179. #endif
  12180. return EXPECT_RESULT();
  12181. } /* END test_wc_InitSha3 */
  12182. /*
  12183. * Testing wc_Sha3_Update()
  12184. */
  12185. static int testing_wc_Sha3_Update(void)
  12186. {
  12187. EXPECT_DECLS;
  12188. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  12189. !defined(WOLFSSL_AFALG_XILINX)
  12190. wc_Sha3 sha3;
  12191. byte msg[] = "Everybody's working for the weekend.";
  12192. byte msg2[] = "Everybody gets Friday off.";
  12193. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  12194. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  12195. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  12196. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  12197. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  12198. word32 msglen = sizeof(msg) - 1;
  12199. word32 msg2len = sizeof(msg2);
  12200. word32 msgCmplen = sizeof(msgCmp);
  12201. #if !defined(WOLFSSL_NOSHA3_224)
  12202. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12203. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg, msglen), 0);
  12204. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12205. ExpectTrue(sha3.i == msglen);
  12206. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  12207. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12208. /* Pass bad args. */
  12209. ExpectIntEQ(wc_Sha3_224_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12210. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12211. wc_Sha3_224_Free(&sha3);
  12212. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12213. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 0), 0);
  12214. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  12215. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12216. wc_Sha3_224_Free(&sha3);
  12217. #endif /* SHA3_224 */
  12218. #if !defined(WOLFSSL_NOSHA3_256)
  12219. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12220. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg, msglen), 0);
  12221. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12222. ExpectTrue(sha3.i == msglen);
  12223. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  12224. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12225. /* Pass bad args. */
  12226. ExpectIntEQ(wc_Sha3_256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12227. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12228. wc_Sha3_256_Free(&sha3);
  12229. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12230. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 0), 0);
  12231. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  12232. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12233. wc_Sha3_256_Free(&sha3);
  12234. #endif /* SHA3_256 */
  12235. #if !defined(WOLFSSL_NOSHA3_384)
  12236. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12237. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg, msglen), 0);
  12238. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12239. ExpectTrue(sha3.i == msglen);
  12240. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  12241. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12242. /* Pass bad args. */
  12243. ExpectIntEQ(wc_Sha3_384_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12244. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12245. wc_Sha3_384_Free(&sha3);
  12246. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12247. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 0), 0);
  12248. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  12249. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12250. wc_Sha3_384_Free(&sha3);
  12251. #endif /* SHA3_384 */
  12252. #if !defined(WOLFSSL_NOSHA3_512)
  12253. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12254. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg, msglen), 0);
  12255. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12256. ExpectTrue(sha3.i == msglen);
  12257. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  12258. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12259. /* Pass bad args. */
  12260. ExpectIntEQ(wc_Sha3_512_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12261. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12262. wc_Sha3_512_Free(&sha3);
  12263. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12264. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 0), 0);
  12265. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  12266. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12267. wc_Sha3_512_Free(&sha3);
  12268. #endif /* SHA3_512 */
  12269. #endif /* WOLFSSL_SHA3 */
  12270. return EXPECT_RESULT();
  12271. } /* END testing_wc_Sha3_Update */
  12272. /*
  12273. * Testing wc_Sha3_224_Final()
  12274. */
  12275. static int test_wc_Sha3_224_Final(void)
  12276. {
  12277. EXPECT_DECLS;
  12278. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  12279. wc_Sha3 sha3;
  12280. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12281. "nopnopq";
  12282. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  12283. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  12284. "\x64\xea\xd0\xfc\xce\x33";
  12285. byte hash[WC_SHA3_224_DIGEST_SIZE];
  12286. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  12287. /* Init stack variables. */
  12288. XMEMSET(hash, 0, sizeof(hash));
  12289. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12290. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12291. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  12292. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE), 0);
  12293. /* Test bad args. */
  12294. ExpectIntEQ(wc_Sha3_224_Final(NULL, hash), BAD_FUNC_ARG);
  12295. ExpectIntEQ(wc_Sha3_224_Final(&sha3, NULL), BAD_FUNC_ARG);
  12296. wc_Sha3_224_Free(&sha3);
  12297. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12298. /* Init stack variables. */
  12299. XMEMSET(hash, 0, sizeof(hash));
  12300. XMEMSET(hashRet, 0, sizeof(hashRet));
  12301. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12302. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, hashRet), 0);
  12303. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  12304. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE), 0);
  12305. /* Test bad args. */
  12306. ExpectIntEQ(wc_Sha3_224_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12307. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12308. wc_Sha3_224_Free(&sha3);
  12309. #endif
  12310. return EXPECT_RESULT();
  12311. } /* END test_wc_Sha3_224_Final */
  12312. /*
  12313. * Testing wc_Sha3_256_Final()
  12314. */
  12315. static int test_wc_Sha3_256_Final(void)
  12316. {
  12317. EXPECT_DECLS;
  12318. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  12319. wc_Sha3 sha3;
  12320. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12321. "nopnopq";
  12322. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  12323. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  12324. "\xdd\x97\x49\x6d\x33\x76";
  12325. byte hash[WC_SHA3_256_DIGEST_SIZE];
  12326. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  12327. /* Init stack variables. */
  12328. XMEMSET(hash, 0, sizeof(hash));
  12329. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12330. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12331. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  12332. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE), 0);
  12333. /* Test bad args. */
  12334. ExpectIntEQ(wc_Sha3_256_Final(NULL, hash), BAD_FUNC_ARG);
  12335. ExpectIntEQ(wc_Sha3_256_Final(&sha3, NULL), BAD_FUNC_ARG);
  12336. wc_Sha3_256_Free(&sha3);
  12337. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12338. /* Init stack variables. */
  12339. XMEMSET(hash, 0, sizeof(hash));
  12340. XMEMSET(hashRet, 0, sizeof(hashRet));
  12341. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12342. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, hashRet), 0);
  12343. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  12344. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE), 0);
  12345. /* Test bad args. */
  12346. ExpectIntEQ(wc_Sha3_256_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12347. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12348. wc_Sha3_256_Free(&sha3);
  12349. #endif
  12350. return EXPECT_RESULT();
  12351. } /* END test_wc_Sha3_256_Final */
  12352. /*
  12353. * Testing wc_Sha3_384_Final()
  12354. */
  12355. static int test_wc_Sha3_384_Final(void)
  12356. {
  12357. EXPECT_DECLS;
  12358. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  12359. wc_Sha3 sha3;
  12360. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12361. "nopnopq";
  12362. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  12363. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  12364. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  12365. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  12366. byte hash[WC_SHA3_384_DIGEST_SIZE];
  12367. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  12368. /* Init stack variables. */
  12369. XMEMSET(hash, 0, sizeof(hash));
  12370. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12371. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12372. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  12373. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE), 0);
  12374. /* Test bad args. */
  12375. ExpectIntEQ(wc_Sha3_384_Final(NULL, hash), BAD_FUNC_ARG);
  12376. ExpectIntEQ(wc_Sha3_384_Final(&sha3, NULL), BAD_FUNC_ARG);
  12377. wc_Sha3_384_Free(&sha3);
  12378. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12379. /* Init stack variables. */
  12380. XMEMSET(hash, 0, sizeof(hash));
  12381. XMEMSET(hashRet, 0, sizeof(hashRet));
  12382. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12383. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, hashRet), 0);
  12384. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  12385. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE), 0);
  12386. /* Test bad args. */
  12387. ExpectIntEQ(wc_Sha3_384_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12388. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12389. wc_Sha3_384_Free(&sha3);
  12390. #endif
  12391. return EXPECT_RESULT();
  12392. } /* END test_wc_Sha3_384_Final */
  12393. /*
  12394. * Testing wc_Sha3_512_Final()
  12395. */
  12396. static int test_wc_Sha3_512_Final(void)
  12397. {
  12398. EXPECT_DECLS;
  12399. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  12400. !defined(WOLFSSL_NOSHA3_384)
  12401. wc_Sha3 sha3;
  12402. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12403. "nopnopq";
  12404. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  12405. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  12406. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  12407. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  12408. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  12409. byte hash[WC_SHA3_512_DIGEST_SIZE];
  12410. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  12411. /* Init stack variables. */
  12412. XMEMSET(hash, 0, sizeof(hash));
  12413. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12414. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12415. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  12416. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE), 0);
  12417. /* Test bad args. */
  12418. ExpectIntEQ(wc_Sha3_512_Final(NULL, hash), BAD_FUNC_ARG);
  12419. ExpectIntEQ(wc_Sha3_512_Final(&sha3, NULL), BAD_FUNC_ARG);
  12420. wc_Sha3_512_Free(&sha3);
  12421. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12422. /* Init stack variables. */
  12423. XMEMSET(hash, 0, sizeof(hash));
  12424. XMEMSET(hashRet, 0, sizeof(hashRet));
  12425. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12426. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, hashRet), 0);
  12427. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  12428. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE), 0);
  12429. /* Test bad args. */
  12430. ExpectIntEQ(wc_Sha3_512_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12431. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12432. wc_Sha3_512_Free(&sha3);
  12433. #endif
  12434. return EXPECT_RESULT();
  12435. } /* END test_wc_Sha3_512_Final */
  12436. /*
  12437. * Testing wc_Sha3_224_Copy()
  12438. */
  12439. static int test_wc_Sha3_224_Copy(void)
  12440. {
  12441. EXPECT_DECLS;
  12442. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  12443. wc_Sha3 sha3, sha3Cpy;
  12444. const char* msg = TEST_STRING;
  12445. word32 msglen = (word32)TEST_STRING_SZ;
  12446. byte hash[WC_SHA3_224_DIGEST_SIZE];
  12447. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  12448. XMEMSET(hash, 0, sizeof(hash));
  12449. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12450. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  12451. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  12452. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12453. ExpectIntEQ(wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId), 0);
  12454. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, msglen), 0);
  12455. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, &sha3), 0);
  12456. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  12457. ExpectIntEQ(wc_Sha3_224_Final(&sha3Cpy, hashCpy), 0);
  12458. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12459. /* Test bad args. */
  12460. ExpectIntEQ(wc_Sha3_224_Copy(NULL, &sha3), BAD_FUNC_ARG);
  12461. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  12462. wc_Sha3_224_Free(&sha3);
  12463. wc_Sha3_224_Free(&sha3Cpy);
  12464. #endif
  12465. return EXPECT_RESULT();
  12466. } /* END test_wc_Sha3_224_Copy */
  12467. /*
  12468. * Testing wc_Sha3_256_Copy()
  12469. */
  12470. static int test_wc_Sha3_256_Copy(void)
  12471. {
  12472. EXPECT_DECLS;
  12473. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  12474. wc_Sha3 sha3, sha3Cpy;
  12475. const char* msg = TEST_STRING;
  12476. word32 msglen = (word32)TEST_STRING_SZ;
  12477. byte hash[WC_SHA3_256_DIGEST_SIZE];
  12478. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  12479. XMEMSET(hash, 0, sizeof(hash));
  12480. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12481. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  12482. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  12483. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12484. ExpectIntEQ(wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId), 0);
  12485. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, msglen), 0);
  12486. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, &sha3), 0);
  12487. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  12488. ExpectIntEQ(wc_Sha3_256_Final(&sha3Cpy, hashCpy), 0);
  12489. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12490. /* Test bad args. */
  12491. ExpectIntEQ(wc_Sha3_256_Copy(NULL, &sha3), BAD_FUNC_ARG);
  12492. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  12493. wc_Sha3_256_Free(&sha3);
  12494. wc_Sha3_256_Free(&sha3Cpy);
  12495. #endif
  12496. return EXPECT_RESULT();
  12497. } /* END test_wc_Sha3_256_Copy */
  12498. /*
  12499. * Testing wc_Sha3_384_Copy()
  12500. */
  12501. static int test_wc_Sha3_384_Copy(void)
  12502. {
  12503. EXPECT_DECLS;
  12504. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  12505. wc_Sha3 sha3, sha3Cpy;
  12506. const char* msg = TEST_STRING;
  12507. word32 msglen = (word32)TEST_STRING_SZ;
  12508. byte hash[WC_SHA3_384_DIGEST_SIZE];
  12509. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  12510. XMEMSET(hash, 0, sizeof(hash));
  12511. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12512. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  12513. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  12514. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12515. ExpectIntEQ(wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId), 0);
  12516. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, msglen), 0);
  12517. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, &sha3), 0);
  12518. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  12519. ExpectIntEQ(wc_Sha3_384_Final(&sha3Cpy, hashCpy), 0);
  12520. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12521. /* Test bad args. */
  12522. ExpectIntEQ(wc_Sha3_384_Copy(NULL, &sha3), BAD_FUNC_ARG);
  12523. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  12524. wc_Sha3_384_Free(&sha3);
  12525. wc_Sha3_384_Free(&sha3Cpy);
  12526. #endif
  12527. return EXPECT_RESULT();
  12528. } /* END test_wc_Sha3_384_Copy */
  12529. /*
  12530. * Testing wc_Sha3_512_Copy()
  12531. */
  12532. static int test_wc_Sha3_512_Copy(void)
  12533. {
  12534. EXPECT_DECLS;
  12535. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  12536. wc_Sha3 sha3, sha3Cpy;
  12537. const char* msg = TEST_STRING;
  12538. word32 msglen = (word32)TEST_STRING_SZ;
  12539. byte hash[WC_SHA3_512_DIGEST_SIZE];
  12540. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  12541. XMEMSET(hash, 0, sizeof(hash));
  12542. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12543. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  12544. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  12545. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12546. ExpectIntEQ(wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId), 0);
  12547. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, msglen), 0);
  12548. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, &sha3), 0);
  12549. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  12550. ExpectIntEQ(wc_Sha3_512_Final(&sha3Cpy, hashCpy), 0);
  12551. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12552. /* Test bad args. */
  12553. ExpectIntEQ(wc_Sha3_512_Copy(NULL, &sha3), BAD_FUNC_ARG);
  12554. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  12555. wc_Sha3_512_Free(&sha3);
  12556. wc_Sha3_512_Free(&sha3Cpy);
  12557. #endif
  12558. return EXPECT_RESULT();
  12559. } /* END test_wc_Sha3_512_Copy */
  12560. /*
  12561. * Unit test function for wc_Sha3_GetFlags()
  12562. */
  12563. static int test_wc_Sha3_GetFlags(void)
  12564. {
  12565. EXPECT_DECLS;
  12566. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  12567. wc_Sha3 sha3;
  12568. word32 flags = 0;
  12569. /* Initialize */
  12570. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12571. ExpectIntEQ(wc_Sha3_GetFlags(&sha3, &flags), 0);
  12572. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12573. wc_Sha3_224_Free(&sha3);
  12574. #endif
  12575. return EXPECT_RESULT();
  12576. } /* END test_wc_Sha3_GetFlags */
  12577. static int test_wc_InitShake256(void)
  12578. {
  12579. EXPECT_DECLS;
  12580. #ifdef WOLFSSL_SHAKE256
  12581. wc_Shake shake;
  12582. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12583. /* Test bad args. */
  12584. ExpectIntEQ(wc_InitShake256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12585. wc_Shake256_Free(&shake);
  12586. #endif
  12587. return EXPECT_RESULT();
  12588. }
  12589. static int testing_wc_Shake256_Update(void)
  12590. {
  12591. EXPECT_DECLS;
  12592. #ifdef WOLFSSL_SHAKE256
  12593. wc_Shake shake;
  12594. byte msg[] = "Everybody's working for the weekend.";
  12595. byte msg2[] = "Everybody gets Friday off.";
  12596. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  12597. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  12598. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  12599. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  12600. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  12601. word32 msglen = sizeof(msg) - 1;
  12602. word32 msg2len = sizeof(msg2);
  12603. word32 msgCmplen = sizeof(msgCmp);
  12604. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12605. ExpectIntEQ(wc_Shake256_Update(&shake, msg, msglen), 0);
  12606. ExpectIntEQ(XMEMCMP(msg, shake.t, msglen), 0);
  12607. ExpectTrue(shake.i == msglen);
  12608. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  12609. ExpectIntEQ(XMEMCMP(shake.t, msgCmp, msgCmplen), 0);
  12610. /* Pass bad args. */
  12611. ExpectIntEQ(wc_Shake256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12612. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 5), BAD_FUNC_ARG);
  12613. wc_Shake256_Free(&shake);
  12614. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12615. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 0), 0);
  12616. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  12617. ExpectIntEQ(XMEMCMP(msg2, shake.t, msg2len), 0);
  12618. wc_Shake256_Free(&shake);
  12619. #endif /* WOLFSSL_SHAKE256 */
  12620. return EXPECT_RESULT();
  12621. }
  12622. static int test_wc_Shake256_Final(void)
  12623. {
  12624. EXPECT_DECLS;
  12625. #ifdef WOLFSSL_SHAKE256
  12626. wc_Shake shake;
  12627. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12628. "nopnopq";
  12629. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  12630. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  12631. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  12632. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  12633. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  12634. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  12635. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  12636. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  12637. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  12638. byte hash[114];
  12639. /* Init stack variables. */
  12640. XMEMSET(hash, 0, sizeof(hash));
  12641. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12642. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg)),
  12643. 0);
  12644. ExpectIntEQ(wc_Shake256_Final(&shake, hash, (word32)sizeof(hash)), 0);
  12645. ExpectIntEQ(XMEMCMP(expOut, hash, (word32)sizeof(hash)), 0);
  12646. /* Test bad args. */
  12647. ExpectIntEQ(wc_Shake256_Final(NULL, hash, (word32)sizeof(hash)),
  12648. BAD_FUNC_ARG);
  12649. ExpectIntEQ(wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash)),
  12650. BAD_FUNC_ARG);
  12651. wc_Shake256_Free(&shake);
  12652. #endif
  12653. return EXPECT_RESULT();
  12654. }
  12655. /*
  12656. * Testing wc_Shake256_Copy()
  12657. */
  12658. static int test_wc_Shake256_Copy(void)
  12659. {
  12660. EXPECT_DECLS;
  12661. #ifdef WOLFSSL_SHAKE256
  12662. wc_Shake shake, shakeCpy;
  12663. const char* msg = TEST_STRING;
  12664. word32 msglen = (word32)TEST_STRING_SZ;
  12665. byte hash[144];
  12666. byte hashCpy[144];
  12667. word32 hashLen = sizeof(hash);
  12668. word32 hashLenCpy = sizeof(hashCpy);
  12669. XMEMSET(hash, 0, sizeof(hash));
  12670. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12671. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12672. ExpectIntEQ(wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId), 0);
  12673. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, msglen), 0);
  12674. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, &shake), 0);
  12675. ExpectIntEQ(wc_Shake256_Final(&shake, hash, hashLen), 0);
  12676. ExpectIntEQ(wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy), 0);
  12677. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12678. /* Test bad args. */
  12679. ExpectIntEQ(wc_Shake256_Copy(NULL, &shake), BAD_FUNC_ARG);
  12680. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, NULL), BAD_FUNC_ARG);
  12681. wc_Shake256_Free(&shake);
  12682. wc_Shake256_Free(&shakeCpy);
  12683. #endif
  12684. return EXPECT_RESULT();
  12685. } /* END test_wc_Shake256_Copy */
  12686. /*
  12687. * Unit test function for wc_Shake256Hash()
  12688. */
  12689. static int test_wc_Shake256Hash(void)
  12690. {
  12691. EXPECT_DECLS;
  12692. #ifdef WOLFSSL_SHAKE256
  12693. const byte data[] = { /* Hello World */
  12694. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  12695. 0x72,0x6c,0x64
  12696. };
  12697. word32 len = sizeof(data);
  12698. byte hash[144];
  12699. word32 hashLen = sizeof(hash);
  12700. ExpectIntEQ(wc_Shake256Hash(data, len, hash, hashLen), 0);
  12701. #endif
  12702. return EXPECT_RESULT();
  12703. } /* END test_wc_Shake256Hash */
  12704. /*
  12705. * Testing wc_InitSm3(), wc_Sm3Free()
  12706. */
  12707. static int test_wc_InitSm3Free(void)
  12708. {
  12709. EXPECT_DECLS;
  12710. #ifdef WOLFSSL_SM3
  12711. wc_Sm3 sm3;
  12712. /* Invalid Parameters */
  12713. ExpectIntEQ(wc_InitSm3(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  12714. /* Valid Parameters */
  12715. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12716. wc_Sm3Free(NULL);
  12717. wc_Sm3Free(&sm3);
  12718. #endif
  12719. return EXPECT_RESULT();
  12720. } /* END test_wc_InitSm3 */
  12721. /*
  12722. * Testing wc_Sm3Update(), wc_Sm3Final()
  12723. */
  12724. static int test_wc_Sm3UpdateFinal(void)
  12725. {
  12726. EXPECT_DECLS;
  12727. #ifdef WOLFSSL_SM3
  12728. wc_Sm3 sm3;
  12729. byte data[WC_SM3_BLOCK_SIZE * 4];
  12730. byte hash[WC_SM3_DIGEST_SIZE];
  12731. byte calcHash[WC_SM3_DIGEST_SIZE];
  12732. byte expHash[WC_SM3_DIGEST_SIZE] = {
  12733. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  12734. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  12735. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  12736. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  12737. };
  12738. word32 chunk;
  12739. word32 i;
  12740. XMEMSET(data, 0, sizeof(data));
  12741. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12742. /* Invalid Parameters */
  12743. ExpectIntEQ(wc_Sm3Update(NULL, NULL, 1), BAD_FUNC_ARG);
  12744. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 1), BAD_FUNC_ARG);
  12745. ExpectIntEQ(wc_Sm3Update(NULL, data, 1), BAD_FUNC_ARG);
  12746. /* Valid Parameters */
  12747. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 0), 0);
  12748. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  12749. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  12750. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE), 0);
  12751. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE - 2), 0);
  12752. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE * 2), 0);
  12753. /* Ensure too many bytes for lengths. */
  12754. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_PAD_SIZE), 0);
  12755. /* Invalid Parameters */
  12756. ExpectIntEQ(wc_Sm3Final(NULL, NULL), BAD_FUNC_ARG);
  12757. ExpectIntEQ(wc_Sm3Final(&sm3, NULL), BAD_FUNC_ARG);
  12758. ExpectIntEQ(wc_Sm3Final(NULL, hash), BAD_FUNC_ARG);
  12759. /* Valid Parameters */
  12760. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  12761. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  12762. /* Chunk tests. */
  12763. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  12764. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  12765. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  12766. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  12767. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, chunk), 0);
  12768. }
  12769. if (i < (word32)sizeof(data)) {
  12770. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, (word32)sizeof(data) - i),
  12771. 0);
  12772. }
  12773. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  12774. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  12775. }
  12776. /* Not testing when the low 32-bit length overflows. */
  12777. wc_Sm3Free(&sm3);
  12778. #endif
  12779. return EXPECT_RESULT();
  12780. } /* END test_wc_Sm3Update */
  12781. /*
  12782. * Testing wc_Sm3GetHash()
  12783. */
  12784. static int test_wc_Sm3GetHash(void)
  12785. {
  12786. EXPECT_DECLS;
  12787. #ifdef WOLFSSL_SM3
  12788. wc_Sm3 sm3;
  12789. byte hash[WC_SM3_DIGEST_SIZE];
  12790. byte calcHash[WC_SM3_DIGEST_SIZE];
  12791. byte data[WC_SM3_BLOCK_SIZE];
  12792. XMEMSET(data, 0, sizeof(data));
  12793. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12794. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  12795. /* Invalid Parameters */
  12796. ExpectIntEQ(wc_Sm3GetHash(NULL, NULL), BAD_FUNC_ARG);
  12797. ExpectIntEQ(wc_Sm3GetHash(&sm3, NULL), BAD_FUNC_ARG);
  12798. ExpectIntEQ(wc_Sm3GetHash(NULL, hash), BAD_FUNC_ARG);
  12799. /* Valid Parameters */
  12800. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  12801. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  12802. /* With update. */
  12803. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  12804. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  12805. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  12806. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  12807. wc_Sm3Free(&sm3);
  12808. #endif
  12809. return EXPECT_RESULT();
  12810. } /* END test_wc_Sm3Update */
  12811. /*
  12812. * Testing wc_Sm3Copy()
  12813. */
  12814. static int test_wc_Sm3Copy(void)
  12815. {
  12816. EXPECT_DECLS;
  12817. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  12818. wc_Sm3 sm3;
  12819. wc_Sm3 sm3Copy;
  12820. byte hash[WC_SM3_DIGEST_SIZE];
  12821. byte hashCopy[WC_SM3_DIGEST_SIZE];
  12822. byte data[WC_SM3_BLOCK_SIZE + 1];
  12823. int i;
  12824. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12825. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  12826. /* Invalid Parameters */
  12827. ExpectIntEQ(wc_Sm3Copy(NULL, NULL), BAD_FUNC_ARG);
  12828. ExpectIntEQ(wc_Sm3Copy(&sm3, NULL), BAD_FUNC_ARG);
  12829. ExpectIntEQ(wc_Sm3Copy(NULL, &sm3Copy), BAD_FUNC_ARG);
  12830. /* Valid Parameters */
  12831. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  12832. /* Ensure all parts of data updated during hashing are copied. */
  12833. for (i = 0; i < WC_SM3_BLOCK_SIZE + 1; i++) {
  12834. ExpectIntEQ(wc_Sm3Update(&sm3, data, i), 0);
  12835. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  12836. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  12837. ExpectIntEQ(wc_Sm3Update(&sm3Copy, data, 1), 0);
  12838. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  12839. ExpectIntEQ(wc_Sm3Final(&sm3Copy, hashCopy), 0);
  12840. ExpectBufEQ(hash, hashCopy, WC_SM3_DIGEST_SIZE);
  12841. }
  12842. wc_Sm3Free(&sm3Copy);
  12843. wc_Sm3Free(&sm3);
  12844. #endif
  12845. return EXPECT_RESULT();
  12846. } /* END test_wc_Sm3Copy */
  12847. /*
  12848. * Testing wc_Sm3FinalRaw()
  12849. */
  12850. static int test_wc_Sm3FinalRaw(void)
  12851. {
  12852. EXPECT_DECLS;
  12853. #if defined(WOLFSSL_SM3) && !defined(HAVE_SELFTEST) && \
  12854. !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  12855. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  12856. !defined(WOLFSSL_NO_HASH_RAW)
  12857. wc_Sm3 sm3;
  12858. byte hash1[WC_SM3_DIGEST_SIZE];
  12859. byte hash2[WC_SM3_DIGEST_SIZE];
  12860. byte hash3[WC_SM3_DIGEST_SIZE];
  12861. byte* hash_test[3] = { hash1, hash2, hash3 };
  12862. int times;
  12863. int i;
  12864. XMEMSET(&sm3, 0, sizeof(sm3));
  12865. /* Initialize */
  12866. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12867. /* Invalid Parameters */
  12868. ExpectIntEQ(wc_Sm3FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12869. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, NULL), BAD_FUNC_ARG);
  12870. ExpectIntEQ(wc_Sm3FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12871. times = sizeof(hash_test) / sizeof(byte*);
  12872. for (i = 0; i < times; i++) {
  12873. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, hash_test[i]), 0);
  12874. }
  12875. wc_Sm3Free(&sm3);
  12876. #endif
  12877. return EXPECT_RESULT();
  12878. } /* END test_wc_Sm3FinalRaw */
  12879. /*
  12880. * Testing wc_Sm3GetFlags, wc_Sm3SetFlags()
  12881. */
  12882. static int test_wc_Sm3GetSetFlags(void)
  12883. {
  12884. EXPECT_DECLS;
  12885. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  12886. wc_Sm3 sm3;
  12887. wc_Sm3 sm3Copy;
  12888. word32 flags = 0;
  12889. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12890. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  12891. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  12892. ExpectIntEQ(flags, 0);
  12893. ExpectIntEQ(wc_Sm3SetFlags(NULL, WC_HASH_FLAG_WILLCOPY), 0);
  12894. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  12895. ExpectIntEQ(flags, 0);
  12896. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  12897. ExpectIntEQ(flags, 0);
  12898. ExpectIntEQ(wc_Sm3SetFlags(&sm3, WC_HASH_FLAG_WILLCOPY), 0);
  12899. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  12900. ExpectIntEQ(flags, WC_HASH_FLAG_WILLCOPY);
  12901. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  12902. ExpectIntEQ(wc_Sm3GetFlags(&sm3Copy, &flags), 0);
  12903. ExpectIntEQ(flags, WC_HASH_FLAG_ISCOPY | WC_HASH_FLAG_WILLCOPY);
  12904. wc_Sm3Free(&sm3Copy);
  12905. wc_Sm3Free(&sm3);
  12906. #endif
  12907. return EXPECT_RESULT();
  12908. } /* END test_wc_Sm3Update */
  12909. /*
  12910. * Testing wc_Sm3Hash()
  12911. */
  12912. static int test_wc_Sm3Hash(void)
  12913. {
  12914. EXPECT_DECLS;
  12915. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  12916. byte data[WC_SM3_BLOCK_SIZE];
  12917. byte hash[WC_SM3_DIGEST_SIZE];
  12918. /* Invalid parameters. */
  12919. ExpectIntEQ(wc_Sm3Hash(NULL, sizeof(data), hash), BAD_FUNC_ARG);
  12920. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), NULL), BAD_FUNC_ARG);
  12921. /* Valid parameters. */
  12922. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), hash), 0);
  12923. #endif
  12924. return EXPECT_RESULT();
  12925. } /* END test_wc_Sm3Hash */
  12926. /*
  12927. * Test function for wc_HmacSetKey
  12928. */
  12929. static int test_wc_Md5HmacSetKey(void)
  12930. {
  12931. EXPECT_DECLS;
  12932. #if !defined(NO_HMAC) && !defined(NO_MD5)
  12933. Hmac hmac;
  12934. int ret, times, itr;
  12935. const char* keys[]=
  12936. {
  12937. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  12938. #ifndef HAVE_FIPS
  12939. "Jefe", /* smaller than minimum FIPS key size */
  12940. #endif
  12941. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12942. };
  12943. times = sizeof(keys) / sizeof(char*);
  12944. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  12945. for (itr = 0; itr < times; itr++) {
  12946. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  12947. (word32)XSTRLEN(keys[itr]));
  12948. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  12949. wc_HmacFree(&hmac);
  12950. ExpectIntEQ(ret, BAD_FUNC_ARG);
  12951. #else
  12952. ExpectIntEQ(ret, 0);
  12953. #endif
  12954. }
  12955. /* Bad args. */
  12956. ExpectIntEQ(wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  12957. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12958. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0])),
  12959. BAD_FUNC_ARG);
  12960. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  12961. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12962. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  12963. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  12964. ExpectIntEQ(ret, BAD_FUNC_ARG);
  12965. #elif defined(HAVE_FIPS)
  12966. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  12967. #else
  12968. ExpectIntEQ(ret, 0);
  12969. #endif
  12970. wc_HmacFree(&hmac);
  12971. #endif
  12972. return EXPECT_RESULT();
  12973. } /* END test_wc_Md5HmacSetKey */
  12974. /*
  12975. * testing wc_HmacSetKey() on wc_Sha hash.
  12976. */
  12977. static int test_wc_ShaHmacSetKey(void)
  12978. {
  12979. EXPECT_DECLS;
  12980. #if !defined(NO_HMAC) && !defined(NO_SHA)
  12981. Hmac hmac;
  12982. int ret, times, itr;
  12983. const char* keys[]=
  12984. {
  12985. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12986. "\x0b\x0b\x0b",
  12987. #ifndef HAVE_FIPS
  12988. "Jefe", /* smaller than minimum FIPS key size */
  12989. #endif
  12990. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12991. "\xAA\xAA\xAA"
  12992. };
  12993. times = sizeof(keys) / sizeof(char*);
  12994. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  12995. for (itr = 0; itr < times; itr++) {
  12996. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  12997. (word32)XSTRLEN(keys[itr])), 0);
  12998. }
  12999. /* Bad args. */
  13000. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  13001. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13002. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0])),
  13003. BAD_FUNC_ARG);
  13004. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13005. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13006. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  13007. #ifdef HAVE_FIPS
  13008. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13009. #else
  13010. ExpectIntEQ(ret, 0);
  13011. #endif
  13012. wc_HmacFree(&hmac);
  13013. #endif
  13014. return EXPECT_RESULT();
  13015. } /* END test_wc_ShaHmacSetKey() */
  13016. /*
  13017. * testing wc_HmacSetKey() on Sha224 hash.
  13018. */
  13019. static int test_wc_Sha224HmacSetKey(void)
  13020. {
  13021. EXPECT_DECLS;
  13022. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13023. Hmac hmac;
  13024. int ret, times, itr;
  13025. const char* keys[]=
  13026. {
  13027. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13028. "\x0b\x0b\x0b",
  13029. #ifndef HAVE_FIPS
  13030. "Jefe", /* smaller than minimum FIPS key size */
  13031. #endif
  13032. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13033. "\xAA\xAA\xAA"
  13034. };
  13035. times = sizeof(keys) / sizeof(char*);
  13036. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13037. for (itr = 0; itr < times; itr++) {
  13038. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  13039. (word32)XSTRLEN(keys[itr])), 0);
  13040. }
  13041. /* Bad args. */
  13042. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  13043. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13044. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0])),
  13045. BAD_FUNC_ARG);
  13046. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13047. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13048. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  13049. #ifdef HAVE_FIPS
  13050. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13051. #else
  13052. ExpectIntEQ(ret, 0);
  13053. #endif
  13054. wc_HmacFree(&hmac);
  13055. #endif
  13056. return EXPECT_RESULT();
  13057. } /* END test_wc_Sha224HmacSetKey() */
  13058. /*
  13059. * testing wc_HmacSetKey() on Sha256 hash
  13060. */
  13061. static int test_wc_Sha256HmacSetKey(void)
  13062. {
  13063. EXPECT_DECLS;
  13064. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13065. Hmac hmac;
  13066. int ret, times, itr;
  13067. const char* keys[]=
  13068. {
  13069. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13070. "\x0b\x0b\x0b",
  13071. #ifndef HAVE_FIPS
  13072. "Jefe", /* smaller than minimum FIPS key size */
  13073. #endif
  13074. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13075. "\xAA\xAA\xAA"
  13076. };
  13077. times = sizeof(keys) / sizeof(char*);
  13078. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13079. for (itr = 0; itr < times; itr++) {
  13080. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  13081. (word32)XSTRLEN(keys[itr])), 0);
  13082. }
  13083. /* Bad args. */
  13084. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  13085. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13086. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0])),
  13087. BAD_FUNC_ARG);
  13088. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13089. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13090. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  13091. #ifdef HAVE_FIPS
  13092. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13093. #else
  13094. ExpectIntEQ(ret, 0);
  13095. #endif
  13096. wc_HmacFree(&hmac);
  13097. #endif
  13098. return EXPECT_RESULT();
  13099. } /* END test_wc_Sha256HmacSetKey() */
  13100. /*
  13101. * testing wc_HmacSetKey on Sha384 hash.
  13102. */
  13103. static int test_wc_Sha384HmacSetKey(void)
  13104. {
  13105. EXPECT_DECLS;
  13106. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13107. Hmac hmac;
  13108. int ret, times, itr;
  13109. const char* keys[]=
  13110. {
  13111. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13112. "\x0b\x0b\x0b",
  13113. #ifndef HAVE_FIPS
  13114. "Jefe", /* smaller than minimum FIPS key size */
  13115. #endif
  13116. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13117. "\xAA\xAA\xAA"
  13118. };
  13119. times = sizeof(keys) / sizeof(char*);
  13120. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13121. for (itr = 0; itr < times; itr++) {
  13122. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  13123. (word32)XSTRLEN(keys[itr])), 0);
  13124. }
  13125. /* Bad args. */
  13126. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  13127. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13128. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0])),
  13129. BAD_FUNC_ARG);
  13130. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13131. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13132. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  13133. #ifdef HAVE_FIPS
  13134. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13135. #else
  13136. ExpectIntEQ(ret, 0);
  13137. #endif
  13138. wc_HmacFree(&hmac);
  13139. #endif
  13140. return EXPECT_RESULT();
  13141. } /* END test_wc_Sha384HmacSetKey() */
  13142. /*
  13143. * testing wc_HmacUpdate on wc_Md5 hash.
  13144. */
  13145. static int test_wc_Md5HmacUpdate(void)
  13146. {
  13147. EXPECT_DECLS;
  13148. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13149. Hmac hmac;
  13150. testVector a, b;
  13151. #ifdef HAVE_FIPS
  13152. const char* keys =
  13153. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13154. #else
  13155. const char* keys = "Jefe";
  13156. #endif
  13157. a.input = "what do ya want for nothing?";
  13158. a.inLen = XSTRLEN(a.input);
  13159. b.input = "Hi There";
  13160. b.inLen = XSTRLEN(b.input);
  13161. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13162. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys,
  13163. (word32)XSTRLEN(keys)), 0);
  13164. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13165. /* Update Hmac. */
  13166. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13167. /* Test bad args. */
  13168. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13169. BAD_FUNC_ARG);
  13170. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13171. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13172. wc_HmacFree(&hmac);
  13173. #endif
  13174. return EXPECT_RESULT();
  13175. } /* END test_wc_Md5HmacUpdate */
  13176. /*
  13177. * testing wc_HmacUpdate on SHA hash.
  13178. */
  13179. static int test_wc_ShaHmacUpdate(void)
  13180. {
  13181. EXPECT_DECLS;
  13182. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13183. Hmac hmac;
  13184. testVector a, b;
  13185. #ifdef HAVE_FIPS
  13186. const char* keys =
  13187. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13188. #else
  13189. const char* keys = "Jefe";
  13190. #endif
  13191. a.input = "what do ya want for nothing?";
  13192. a.inLen = XSTRLEN(a.input);
  13193. b.input = "Hi There";
  13194. b.inLen = XSTRLEN(b.input);
  13195. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13196. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys,
  13197. (word32)XSTRLEN(keys)), 0);
  13198. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13199. /* Update Hmac. */
  13200. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13201. /* Test bad args. */
  13202. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13203. BAD_FUNC_ARG);
  13204. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13205. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13206. wc_HmacFree(&hmac);
  13207. #endif
  13208. return EXPECT_RESULT();
  13209. } /* END test_wc_ShaHmacUpdate */
  13210. /*
  13211. * testing wc_HmacUpdate on SHA224 hash.
  13212. */
  13213. static int test_wc_Sha224HmacUpdate(void)
  13214. {
  13215. EXPECT_DECLS;
  13216. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13217. Hmac hmac;
  13218. testVector a, b;
  13219. #ifdef HAVE_FIPS
  13220. const char* keys =
  13221. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13222. #else
  13223. const char* keys = "Jefe";
  13224. #endif
  13225. a.input = "what do ya want for nothing?";
  13226. a.inLen = XSTRLEN(a.input);
  13227. b.input = "Hi There";
  13228. b.inLen = XSTRLEN(b.input);
  13229. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13230. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys,
  13231. (word32)XSTRLEN(keys)), 0);
  13232. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13233. /* Update Hmac. */
  13234. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13235. /* Test bad args. */
  13236. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13237. BAD_FUNC_ARG);
  13238. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13239. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13240. wc_HmacFree(&hmac);
  13241. #endif
  13242. return EXPECT_RESULT();
  13243. } /* END test_wc_Sha224HmacUpdate */
  13244. /*
  13245. * testing wc_HmacUpdate on SHA256 hash.
  13246. */
  13247. static int test_wc_Sha256HmacUpdate(void)
  13248. {
  13249. EXPECT_DECLS;
  13250. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13251. Hmac hmac;
  13252. testVector a, b;
  13253. #ifdef HAVE_FIPS
  13254. const char* keys =
  13255. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13256. #else
  13257. const char* keys = "Jefe";
  13258. #endif
  13259. a.input = "what do ya want for nothing?";
  13260. a.inLen = XSTRLEN(a.input);
  13261. b.input = "Hi There";
  13262. b.inLen = XSTRLEN(b.input);
  13263. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13264. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys,
  13265. (word32)XSTRLEN(keys)), 0);
  13266. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13267. /* Update Hmac. */
  13268. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13269. /* Test bad args. */
  13270. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13271. BAD_FUNC_ARG);
  13272. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13273. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13274. wc_HmacFree(&hmac);
  13275. #endif
  13276. return EXPECT_RESULT();
  13277. } /* END test_wc_Sha256HmacUpdate */
  13278. /*
  13279. * testing wc_HmacUpdate on SHA384 hash.
  13280. */
  13281. static int test_wc_Sha384HmacUpdate(void)
  13282. {
  13283. EXPECT_DECLS;
  13284. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13285. Hmac hmac;
  13286. testVector a, b;
  13287. #ifdef HAVE_FIPS
  13288. const char* keys =
  13289. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13290. #else
  13291. const char* keys = "Jefe";
  13292. #endif
  13293. a.input = "what do ya want for nothing?";
  13294. a.inLen = XSTRLEN(a.input);
  13295. b.input = "Hi There";
  13296. b.inLen = XSTRLEN(b.input);
  13297. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13298. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys,
  13299. (word32)XSTRLEN(keys)), 0);
  13300. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13301. /* Update Hmac. */
  13302. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13303. /* Test bad args. */
  13304. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13305. BAD_FUNC_ARG);
  13306. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13307. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13308. wc_HmacFree(&hmac);
  13309. #endif
  13310. return EXPECT_RESULT();
  13311. } /* END test_wc_Sha384HmacUpdate */
  13312. /*
  13313. * Testing wc_HmacFinal() with MD5
  13314. */
  13315. static int test_wc_Md5HmacFinal(void)
  13316. {
  13317. EXPECT_DECLS;
  13318. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13319. Hmac hmac;
  13320. byte hash[WC_MD5_DIGEST_SIZE];
  13321. testVector a;
  13322. const char* key;
  13323. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13324. a.input = "Hi There";
  13325. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  13326. "\x9d";
  13327. a.inLen = XSTRLEN(a.input);
  13328. a.outLen = XSTRLEN(a.output);
  13329. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13330. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key)),
  13331. 0);
  13332. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13333. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13334. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  13335. /* Try bad parameters. */
  13336. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13337. #ifndef HAVE_FIPS
  13338. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13339. #endif
  13340. wc_HmacFree(&hmac);
  13341. #endif
  13342. return EXPECT_RESULT();
  13343. } /* END test_wc_Md5HmacFinal */
  13344. /*
  13345. * Testing wc_HmacFinal() with SHA
  13346. */
  13347. static int test_wc_ShaHmacFinal(void)
  13348. {
  13349. EXPECT_DECLS;
  13350. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13351. Hmac hmac;
  13352. byte hash[WC_SHA_DIGEST_SIZE];
  13353. testVector a;
  13354. const char* key;
  13355. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13356. "\x0b\x0b\x0b";
  13357. a.input = "Hi There";
  13358. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  13359. "\x8e\xf1\x46\xbe\x00";
  13360. a.inLen = XSTRLEN(a.input);
  13361. a.outLen = XSTRLEN(a.output);
  13362. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13363. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key)),
  13364. 0);
  13365. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13366. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13367. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  13368. /* Try bad parameters. */
  13369. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13370. #ifndef HAVE_FIPS
  13371. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13372. #endif
  13373. wc_HmacFree(&hmac);
  13374. #endif
  13375. return EXPECT_RESULT();
  13376. } /* END test_wc_ShaHmacFinal */
  13377. /*
  13378. * Testing wc_HmacFinal() with SHA224
  13379. */
  13380. static int test_wc_Sha224HmacFinal(void)
  13381. {
  13382. EXPECT_DECLS;
  13383. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13384. Hmac hmac;
  13385. byte hash[WC_SHA224_DIGEST_SIZE];
  13386. testVector a;
  13387. const char* key;
  13388. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13389. "\x0b\x0b\x0b";
  13390. a.input = "Hi There";
  13391. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  13392. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  13393. a.inLen = XSTRLEN(a.input);
  13394. a.outLen = XSTRLEN(a.output);
  13395. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13396. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key,
  13397. (word32)XSTRLEN(key)), 0);
  13398. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13399. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13400. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  13401. /* Try bad parameters. */
  13402. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13403. #ifndef HAVE_FIPS
  13404. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13405. #endif
  13406. wc_HmacFree(&hmac);
  13407. #endif
  13408. return EXPECT_RESULT();
  13409. } /* END test_wc_Sha224HmacFinal */
  13410. /*
  13411. * Testing wc_HmacFinal() with SHA256
  13412. */
  13413. static int test_wc_Sha256HmacFinal(void)
  13414. {
  13415. EXPECT_DECLS;
  13416. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13417. Hmac hmac;
  13418. byte hash[WC_SHA256_DIGEST_SIZE];
  13419. testVector a;
  13420. const char* key;
  13421. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13422. "\x0b\x0b\x0b";
  13423. a.input = "Hi There";
  13424. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  13425. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  13426. "\xcf\xf7";
  13427. a.inLen = XSTRLEN(a.input);
  13428. a.outLen = XSTRLEN(a.output);
  13429. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13430. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key,
  13431. (word32)XSTRLEN(key)), 0);
  13432. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13433. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13434. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  13435. /* Try bad parameters. */
  13436. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13437. #ifndef HAVE_FIPS
  13438. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13439. #endif
  13440. wc_HmacFree(&hmac);
  13441. #endif
  13442. return EXPECT_RESULT();
  13443. } /* END test_wc_Sha256HmacFinal */
  13444. /*
  13445. * Testing wc_HmacFinal() with SHA384
  13446. */
  13447. static int test_wc_Sha384HmacFinal(void)
  13448. {
  13449. EXPECT_DECLS;
  13450. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13451. Hmac hmac;
  13452. byte hash[WC_SHA384_DIGEST_SIZE];
  13453. testVector a;
  13454. const char* key;
  13455. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13456. "\x0b\x0b\x0b";
  13457. a.input = "Hi There";
  13458. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  13459. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  13460. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  13461. "\xfa\x9c\xb6";
  13462. a.inLen = XSTRLEN(a.input);
  13463. a.outLen = XSTRLEN(a.output);
  13464. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13465. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key,
  13466. (word32)XSTRLEN(key)), 0);
  13467. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13468. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13469. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  13470. /* Try bad parameters. */
  13471. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13472. #ifndef HAVE_FIPS
  13473. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13474. #endif
  13475. wc_HmacFree(&hmac);
  13476. #endif
  13477. return EXPECT_RESULT();
  13478. } /* END test_wc_Sha384HmacFinal */
  13479. /*
  13480. * Testing wc_InitCmac()
  13481. */
  13482. static int test_wc_InitCmac(void)
  13483. {
  13484. EXPECT_DECLS;
  13485. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  13486. Cmac cmac1;
  13487. Cmac cmac2;
  13488. Cmac cmac3;
  13489. /* AES 128 key. */
  13490. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13491. "\x09\x10\x11\x12\x13\x14\x15\x16";
  13492. /* AES 192 key. */
  13493. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13494. "\x09\x01\x11\x12\x13\x14\x15\x16"
  13495. "\x01\x02\x03\x04\x05\x06\x07\x08";
  13496. /* AES 256 key. */
  13497. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13498. "\x09\x01\x11\x12\x13\x14\x15\x16"
  13499. "\x01\x02\x03\x04\x05\x06\x07\x08"
  13500. "\x09\x01\x11\x12\x13\x14\x15\x16";
  13501. word32 key1Sz = (word32)sizeof(key1) - 1;
  13502. word32 key2Sz = (word32)sizeof(key2) - 1;
  13503. word32 key3Sz = (word32)sizeof(key3) - 1;
  13504. int type = WC_CMAC_AES;
  13505. (void)key1;
  13506. (void)key1Sz;
  13507. (void)key2;
  13508. (void)key2Sz;
  13509. XMEMSET(&cmac1, 0, sizeof(Cmac));
  13510. XMEMSET(&cmac2, 0, sizeof(Cmac));
  13511. XMEMSET(&cmac3, 0, sizeof(Cmac));
  13512. #ifdef WOLFSSL_AES_128
  13513. ExpectIntEQ(wc_InitCmac(&cmac1, key1, key1Sz, type, NULL), 0);
  13514. #endif
  13515. #ifdef WOLFSSL_AES_192
  13516. wc_AesFree(&cmac1.aes);
  13517. ExpectIntEQ(wc_InitCmac(&cmac2, key2, key2Sz, type, NULL), 0);
  13518. #endif
  13519. #ifdef WOLFSSL_AES_256
  13520. wc_AesFree(&cmac2.aes);
  13521. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, type, NULL), 0);
  13522. #endif
  13523. wc_AesFree(&cmac3.aes);
  13524. /* Test bad args. */
  13525. ExpectIntEQ(wc_InitCmac(NULL, key3, key3Sz, type, NULL), BAD_FUNC_ARG);
  13526. ExpectIntEQ(wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL), BAD_FUNC_ARG);
  13527. ExpectIntEQ(wc_InitCmac(&cmac3, key3, 0, type, NULL), BAD_FUNC_ARG);
  13528. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL), BAD_FUNC_ARG);
  13529. #endif
  13530. return EXPECT_RESULT();
  13531. } /* END test_wc_InitCmac */
  13532. /*
  13533. * Testing wc_CmacUpdate()
  13534. */
  13535. static int test_wc_CmacUpdate(void)
  13536. {
  13537. EXPECT_DECLS;
  13538. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13539. Cmac cmac;
  13540. byte key[] = {
  13541. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  13542. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  13543. };
  13544. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  13545. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  13546. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  13547. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  13548. "\xf4";
  13549. word32 inSz = (word32)sizeof(in) - 1;
  13550. word32 keySz = (word32)sizeof(key);
  13551. int type = WC_CMAC_AES;
  13552. XMEMSET(&cmac, 0, sizeof(Cmac));
  13553. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  13554. ExpectIntEQ(wc_CmacUpdate(&cmac, in, inSz), 0);
  13555. /* Test bad args. */
  13556. ExpectIntEQ(wc_CmacUpdate(NULL, in, inSz), BAD_FUNC_ARG);
  13557. ExpectIntEQ(wc_CmacUpdate(&cmac, NULL, 30), BAD_FUNC_ARG);
  13558. wc_AesFree(&cmac.aes);
  13559. #endif
  13560. return EXPECT_RESULT();
  13561. } /* END test_wc_CmacUpdate */
  13562. /*
  13563. * Testing wc_CmacFinal()
  13564. */
  13565. static int test_wc_CmacFinal(void)
  13566. {
  13567. EXPECT_DECLS;
  13568. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13569. Cmac cmac;
  13570. byte key[] = {
  13571. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  13572. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  13573. };
  13574. byte msg[] = {
  13575. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  13576. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  13577. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  13578. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  13579. 0xf4
  13580. };
  13581. /* Test vectors from CMACGenAES128.rsp from
  13582. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  13583. * Per RFC4493 truncation of lsb is possible.
  13584. */
  13585. byte expMac[] = {
  13586. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  13587. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  13588. };
  13589. byte mac[AES_BLOCK_SIZE];
  13590. word32 msgSz = (word32)sizeof(msg);
  13591. word32 keySz = (word32)sizeof(key);
  13592. word32 macSz = sizeof(mac);
  13593. word32 badMacSz = 17;
  13594. int expMacSz = sizeof(expMac);
  13595. int type = WC_CMAC_AES;
  13596. XMEMSET(&cmac, 0, sizeof(Cmac));
  13597. XMEMSET(mac, 0, macSz);
  13598. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  13599. ExpectIntEQ(wc_CmacUpdate(&cmac, msg, msgSz), 0);
  13600. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  13601. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  13602. /* Pass in bad args. */
  13603. ExpectIntEQ(wc_CmacFinal(NULL, mac, &macSz), BAD_FUNC_ARG);
  13604. ExpectIntEQ(wc_CmacFinal(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  13605. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &badMacSz), BUFFER_E);
  13606. #endif
  13607. return EXPECT_RESULT();
  13608. } /* END test_wc_CmacFinal */
  13609. /*
  13610. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  13611. */
  13612. static int test_wc_AesCmacGenerate(void)
  13613. {
  13614. EXPECT_DECLS;
  13615. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13616. byte key[] = {
  13617. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  13618. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  13619. };
  13620. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  13621. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  13622. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  13623. "\x3d\x32\x65\x4c\x66\x23\xc5";
  13624. byte mac[AES_BLOCK_SIZE];
  13625. word32 keySz = sizeof(key);
  13626. word32 macSz = sizeof(mac);
  13627. word32 msgSz = sizeof(msg) - 1;
  13628. word32 expMacSz = sizeof(expMac) - 1;
  13629. XMEMSET(mac, 0, macSz);
  13630. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz), 0);
  13631. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  13632. /* Pass in bad args. */
  13633. ExpectIntEQ(wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz),
  13634. BAD_FUNC_ARG);
  13635. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz),
  13636. BAD_FUNC_ARG);
  13637. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0),
  13638. BAD_FUNC_ARG);
  13639. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz),
  13640. BAD_FUNC_ARG);
  13641. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz), 0);
  13642. /* Test bad args. */
  13643. ExpectIntEQ(wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz),
  13644. BAD_FUNC_ARG);
  13645. ExpectIntEQ(wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz),
  13646. BAD_FUNC_ARG);
  13647. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz),
  13648. BAD_FUNC_ARG);
  13649. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0),
  13650. BAD_FUNC_ARG);
  13651. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz),
  13652. BAD_FUNC_ARG);
  13653. #endif
  13654. return EXPECT_RESULT();
  13655. } /* END test_wc_AesCmacGenerate */
  13656. /*
  13657. * Testing streaming AES-GCM API.
  13658. */
  13659. static int test_wc_AesGcmStream(void)
  13660. {
  13661. EXPECT_DECLS;
  13662. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  13663. defined(WOLFSSL_AESGCM_STREAM)
  13664. int i;
  13665. WC_RNG rng[1];
  13666. Aes aesEnc[1];
  13667. Aes aesDec[1];
  13668. byte tag[AES_BLOCK_SIZE];
  13669. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  13670. byte out[AES_BLOCK_SIZE * 3 + 2];
  13671. byte plain[AES_BLOCK_SIZE * 3 + 2];
  13672. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  13673. byte key[AES_128_KEY_SIZE] = { 0, };
  13674. byte iv[AES_IV_SIZE] = { 1, };
  13675. byte ivOut[AES_IV_SIZE];
  13676. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  13677. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  13678. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  13679. };
  13680. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  13681. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  13682. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  13683. };
  13684. static const byte expTag[AES_BLOCK_SIZE] = {
  13685. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  13686. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  13687. };
  13688. XMEMSET(&rng, 0, sizeof(WC_RNG));
  13689. XMEMSET(&aesEnc, 0, sizeof(Aes));
  13690. XMEMSET(&aesDec, 0, sizeof(Aes));
  13691. /* Create a random for generating IV/nonce. */
  13692. ExpectIntEQ(wc_InitRng(rng), 0);
  13693. /* Initialize data structures. */
  13694. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  13695. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  13696. /* BadParameters to streaming init. */
  13697. ExpectIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  13698. ExpectIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  13699. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  13700. BAD_FUNC_ARG);
  13701. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  13702. BAD_FUNC_ARG);
  13703. /* Bad parameters to encrypt update. */
  13704. ExpectIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  13705. BAD_FUNC_ARG);
  13706. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  13707. BAD_FUNC_ARG);
  13708. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  13709. BAD_FUNC_ARG);
  13710. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  13711. BAD_FUNC_ARG);
  13712. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  13713. BAD_FUNC_ARG);
  13714. /* Bad parameters to decrypt update. */
  13715. ExpectIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  13716. BAD_FUNC_ARG);
  13717. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  13718. BAD_FUNC_ARG);
  13719. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  13720. BAD_FUNC_ARG);
  13721. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  13722. BAD_FUNC_ARG);
  13723. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  13724. BAD_FUNC_ARG);
  13725. /* Bad parameters to encrypt final. */
  13726. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  13727. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  13728. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  13729. BAD_FUNC_ARG);
  13730. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  13731. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  13732. BAD_FUNC_ARG);
  13733. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  13734. BAD_FUNC_ARG);
  13735. /* Bad parameters to decrypt final. */
  13736. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  13737. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  13738. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  13739. BAD_FUNC_ARG);
  13740. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  13741. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  13742. BAD_FUNC_ARG);
  13743. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  13744. BAD_FUNC_ARG);
  13745. /* Check calling final before setting key fails. */
  13746. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  13747. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  13748. /* Check calling update before setting key else fails. */
  13749. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  13750. MISSING_KEY);
  13751. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  13752. MISSING_KEY);
  13753. /* Set key but not IV. */
  13754. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  13755. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  13756. /* Check calling final before setting IV fails. */
  13757. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  13758. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  13759. /* Check calling update before setting IV else fails. */
  13760. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  13761. MISSING_IV);
  13762. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  13763. MISSING_IV);
  13764. /* Set IV using fixed part IV and external IV APIs. */
  13765. ExpectIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  13766. rng), 0);
  13767. ExpectIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  13768. GCM_NONCE_MID_SZ), 0);
  13769. ExpectIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  13770. ExpectIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  13771. /* Encrypt and decrypt data. */
  13772. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  13773. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  13774. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  13775. /* Finalize and check tag matches. */
  13776. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13777. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13778. /* Set key and IV through streaming init API. */
  13779. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13780. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13781. /* Encrypt/decrypt one block and AAD of one block. */
  13782. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  13783. AES_BLOCK_SIZE), 0);
  13784. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  13785. AES_BLOCK_SIZE), 0);
  13786. ExpectIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  13787. /* Finalize and check tag matches. */
  13788. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13789. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13790. /* Set key and IV through streaming init API. */
  13791. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13792. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13793. /* No data to encrypt/decrypt one byte of AAD. */
  13794. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  13795. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  13796. /* Finalize and check tag matches. */
  13797. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13798. ExpectIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  13799. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13800. /* Set key and IV through streaming init API. */
  13801. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13802. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13803. /* Encrypt/decrypt one byte and no AAD. */
  13804. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  13805. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  13806. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  13807. /* Finalize and check tag matches. */
  13808. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13809. ExpectIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  13810. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13811. /* Set key and IV through streaming init API. */
  13812. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13813. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13814. /* Encryption AES is one byte at a time */
  13815. for (i = 0; i < (int)sizeof(aad); i++) {
  13816. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  13817. 0);
  13818. }
  13819. for (i = 0; i < (int)sizeof(in); i++) {
  13820. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  13821. 0);
  13822. }
  13823. /* Decryption AES is two bytes at a time */
  13824. for (i = 0; i < (int)sizeof(aad); i += 2) {
  13825. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  13826. 0);
  13827. }
  13828. for (i = 0; i < (int)sizeof(aad); i += 2) {
  13829. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  13830. 0), 0);
  13831. }
  13832. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  13833. /* Finalize and check tag matches. */
  13834. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13835. ExpectIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  13836. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13837. /* Check streaming encryption can be decrypted with one shot. */
  13838. ExpectIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  13839. ExpectIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  13840. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  13841. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  13842. wc_AesFree(aesEnc);
  13843. wc_AesFree(aesDec);
  13844. wc_FreeRng(rng);
  13845. #endif
  13846. return EXPECT_RESULT();
  13847. } /* END test_wc_AesGcmStream */
  13848. /*
  13849. * Testing streaming SM4 API.
  13850. */
  13851. static int test_wc_Sm4(void)
  13852. {
  13853. int res = TEST_SKIPPED;
  13854. #ifdef WOLFSSL_SM4
  13855. EXPECT_DECLS;
  13856. wc_Sm4 sm4;
  13857. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  13858. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  13859. unsigned char key[SM4_KEY_SIZE];
  13860. #endif
  13861. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  13862. unsigned char iv[SM4_IV_SIZE];
  13863. #endif
  13864. /* Invalid parameters - wc_Sm4Init */
  13865. ExpectIntEQ(wc_Sm4Init(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  13866. /* Valid cases - wc_Sm4Init */
  13867. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  13868. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  13869. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  13870. XMEMSET(key, 0, sizeof(key));
  13871. /* Invalid parameters - wc_Sm4SetKey. */
  13872. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  13873. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  13874. ExpectIntEQ(wc_Sm4SetKey(NULL, key, 0), BAD_FUNC_ARG);
  13875. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  13876. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, 0), BAD_FUNC_ARG);
  13877. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  13878. ExpectIntEQ(wc_Sm4SetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  13879. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE-1), BAD_FUNC_ARG);
  13880. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE+1), BAD_FUNC_ARG);
  13881. /* Valid cases - wc_Sm4SetKey. */
  13882. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  13883. #endif
  13884. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  13885. XMEMSET(iv, 0, sizeof(iv));
  13886. /* Invalid parameters - wc_Sm4SetIV. */
  13887. ExpectIntEQ(wc_Sm4SetIV(NULL, NULL), BAD_FUNC_ARG);
  13888. ExpectIntEQ(wc_Sm4SetIV(&sm4, NULL), BAD_FUNC_ARG);
  13889. ExpectIntEQ(wc_Sm4SetIV(NULL, iv), BAD_FUNC_ARG);
  13890. /* Valid cases - wc_Sm4SetIV. */
  13891. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13892. #endif
  13893. /* Valid cases - wc_Sm4Free */
  13894. wc_Sm4Free(NULL);
  13895. wc_Sm4Free(&sm4);
  13896. res = EXPECT_RESULT();
  13897. #endif
  13898. return res;
  13899. } /* END test_wc_Sm4 */
  13900. /*
  13901. * Testing block based SM4-ECB API.
  13902. */
  13903. static int test_wc_Sm4Ecb(void)
  13904. {
  13905. int res = TEST_SKIPPED;
  13906. #ifdef WOLFSSL_SM4_ECB
  13907. EXPECT_DECLS;
  13908. wc_Sm4 sm4;
  13909. unsigned char key[SM4_KEY_SIZE];
  13910. unsigned char in[SM4_BLOCK_SIZE * 2];
  13911. unsigned char out[SM4_BLOCK_SIZE * 2];
  13912. unsigned char out2[SM4_BLOCK_SIZE];
  13913. XMEMSET(key, 0, sizeof(key));
  13914. XMEMSET(in, 0, sizeof(in));
  13915. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  13916. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), MISSING_KEY);
  13917. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), MISSING_KEY);
  13918. /* Tested in test_wc_Sm4. */
  13919. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  13920. /* Invalid parameters - wc_Sm4EcbEncrypt. */
  13921. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  13922. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  13923. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  13924. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  13925. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  13926. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  13927. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  13928. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  13929. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  13930. /* Valid cases - wc_Sm4EcbEncrypt. */
  13931. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), 0);
  13932. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  13933. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  13934. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  13935. /* In and out are same pointer. */
  13936. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  13937. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  13938. /* Invalid parameters - wc_Sm4EcbDecrypt. */
  13939. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  13940. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  13941. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  13942. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  13943. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  13944. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  13945. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  13946. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  13947. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  13948. /* Valid cases - wc_Sm4EcbDecrypt. */
  13949. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), 0);
  13950. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  13951. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  13952. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  13953. /* In and out are same pointer. */
  13954. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  13955. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  13956. wc_Sm4Free(&sm4);
  13957. res = EXPECT_RESULT();
  13958. #endif
  13959. return res;
  13960. } /* END test_wc_Sm4Ecb */
  13961. /*
  13962. * Testing block based SM4-CBC API.
  13963. */
  13964. static int test_wc_Sm4Cbc(void)
  13965. {
  13966. int res = TEST_SKIPPED;
  13967. #ifdef WOLFSSL_SM4_CBC
  13968. EXPECT_DECLS;
  13969. wc_Sm4 sm4;
  13970. unsigned char key[SM4_KEY_SIZE];
  13971. unsigned char iv[SM4_IV_SIZE];
  13972. unsigned char in[SM4_BLOCK_SIZE * 2];
  13973. unsigned char out[SM4_BLOCK_SIZE * 2];
  13974. unsigned char out2[SM4_BLOCK_SIZE];
  13975. XMEMSET(key, 0, sizeof(key));
  13976. XMEMSET(iv, 0, sizeof(iv));
  13977. XMEMSET(in, 0, sizeof(in));
  13978. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  13979. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_KEY);
  13980. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_KEY);
  13981. /* Tested in test_wc_Sm4. */
  13982. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  13983. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_IV);
  13984. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_IV);
  13985. /* Tested in test_wc_Sm4. */
  13986. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13987. /* Invalid parameters - wc_Sm4CbcEncrypt. */
  13988. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  13989. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  13990. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  13991. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  13992. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  13993. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  13994. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  13995. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  13996. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  13997. /* Valid cases - wc_Sm4CbcEncrypt. */
  13998. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), 0);
  13999. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14000. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14001. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14002. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14003. /* In and out are same pointer. */
  14004. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14005. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14006. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14007. /* Invalid parameters - wc_Sm4CbcDecrypt. */
  14008. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14009. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14010. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14011. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14012. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14013. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14014. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14015. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14016. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14017. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14018. /* Valid cases - wc_Sm4CbcDecrypt. */
  14019. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), 0);
  14020. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14021. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14022. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14023. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14024. /* In and out are same pointer. */
  14025. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14026. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14027. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14028. wc_Sm4Free(&sm4);
  14029. res = EXPECT_RESULT();
  14030. #endif
  14031. return res;
  14032. } /* END test_wc_Sm4Cbc */
  14033. /*
  14034. * Testing streaming SM4-CTR API.
  14035. */
  14036. static int test_wc_Sm4Ctr(void)
  14037. {
  14038. int res = TEST_SKIPPED;
  14039. #ifdef WOLFSSL_SM4_CTR
  14040. EXPECT_DECLS;
  14041. wc_Sm4 sm4;
  14042. unsigned char key[SM4_KEY_SIZE];
  14043. unsigned char iv[SM4_IV_SIZE];
  14044. unsigned char in[SM4_BLOCK_SIZE * 4];
  14045. unsigned char out[SM4_BLOCK_SIZE * 4];
  14046. unsigned char out2[SM4_BLOCK_SIZE * 4];
  14047. word32 chunk;
  14048. word32 i;
  14049. XMEMSET(key, 0, sizeof(key));
  14050. XMEMSET(iv, 0, sizeof(iv));
  14051. XMEMSET(in, 0, sizeof(in));
  14052. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14053. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14054. /* Tested in test_wc_Sm4. */
  14055. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14056. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_IV);
  14057. /* Tested in test_wc_Sm4. */
  14058. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14059. /* Invalid parameters - wc_Sm4CtrEncrypt. */
  14060. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14061. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, NULL, 0), BAD_FUNC_ARG);
  14062. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, NULL, 0), BAD_FUNC_ARG);
  14063. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, in, 0), BAD_FUNC_ARG);
  14064. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14065. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14066. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14067. /* Valid cases - wc_Sm4CtrEncrypt. */
  14068. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), 0);
  14069. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, 1), 0);
  14070. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14071. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 2), 0);
  14072. ExpectIntEQ(XMEMCMP(out, out2, 1), 0);
  14073. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14074. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14075. ExpectIntEQ(XMEMCMP(out2, out, 2), 0);
  14076. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14077. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14078. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14079. /* In and out are same pointer. Also check encrypt of cipher text produces
  14080. * plaintext.
  14081. */
  14082. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14083. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, out, SM4_BLOCK_SIZE * 2), 0);
  14084. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14085. /* Chunking tests. */
  14086. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14087. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, (word32)sizeof(in)), 0);
  14088. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  14089. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14090. for (i = 0; i + chunk <= (word32)sizeof(in); i += chunk) {
  14091. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i, chunk), 0);
  14092. }
  14093. if (i < (word32)sizeof(in)) {
  14094. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i,
  14095. (word32)sizeof(in) - i), 0);
  14096. }
  14097. ExpectIntEQ(XMEMCMP(out, out2, (word32)sizeof(out)), 0);
  14098. }
  14099. for (i = 0; i < (word32)sizeof(iv); i++) {
  14100. iv[i] = 0xff;
  14101. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14102. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14103. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14104. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, out, SM4_BLOCK_SIZE * 2), 0);
  14105. ExpectIntEQ(XMEMCMP(out2, in, SM4_BLOCK_SIZE * 2), 0);
  14106. }
  14107. wc_Sm4Free(&sm4);
  14108. res = EXPECT_RESULT();
  14109. #endif
  14110. return res;
  14111. } /* END test_wc_Sm4Ctr */
  14112. /*
  14113. * Testing stream SM4-GCM API.
  14114. */
  14115. static int test_wc_Sm4Gcm(void)
  14116. {
  14117. int res = TEST_SKIPPED;
  14118. #ifdef WOLFSSL_SM4_GCM
  14119. EXPECT_DECLS;
  14120. wc_Sm4 sm4;
  14121. unsigned char key[SM4_KEY_SIZE];
  14122. unsigned char nonce[GCM_NONCE_MAX_SZ];
  14123. unsigned char in[SM4_BLOCK_SIZE * 2];
  14124. unsigned char in2[SM4_BLOCK_SIZE * 2];
  14125. unsigned char out[SM4_BLOCK_SIZE * 2];
  14126. unsigned char out2[SM4_BLOCK_SIZE * 2];
  14127. unsigned char dec[SM4_BLOCK_SIZE * 2];
  14128. unsigned char tag[SM4_BLOCK_SIZE];
  14129. unsigned char aad[SM4_BLOCK_SIZE * 2];
  14130. word32 i;
  14131. XMEMSET(key, 0, sizeof(key));
  14132. XMEMSET(nonce, 0, sizeof(nonce));
  14133. XMEMSET(in, 0, sizeof(in));
  14134. XMEMSET(in2, 0, sizeof(in2));
  14135. XMEMSET(aad, 0, sizeof(aad));
  14136. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14137. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  14138. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14139. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  14140. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14141. /* Invalid parameters - wc_Sm4GcmSetKey. */
  14142. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  14143. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  14144. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, 0), BAD_FUNC_ARG);
  14145. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14146. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, 0), BAD_FUNC_ARG);
  14147. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14148. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14149. /* Valid parameters - wc_Sm4GcmSetKey. */
  14150. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14151. /* Invalid parameters - wc_Sm4GcmEncrypt. */
  14152. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14153. 0), BAD_FUNC_ARG);
  14154. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14155. 0), BAD_FUNC_ARG);
  14156. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14157. 0), BAD_FUNC_ARG);
  14158. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14159. 0), BAD_FUNC_ARG);
  14160. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14161. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14162. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14163. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14164. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14165. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14166. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  14167. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14168. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14169. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14170. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  14171. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14172. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  14173. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14174. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  14175. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14176. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14177. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14178. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14179. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14180. /* Invalid parameters - wc_Sm4GcmDecrypt. */
  14181. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14182. 0), BAD_FUNC_ARG);
  14183. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14184. 0), BAD_FUNC_ARG);
  14185. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14186. 0), BAD_FUNC_ARG);
  14187. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14188. 0), BAD_FUNC_ARG);
  14189. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14190. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14191. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14192. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14193. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14194. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14195. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  14196. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14197. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14198. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14199. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  14200. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14201. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  14202. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14203. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  14204. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14205. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14206. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14207. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14208. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14209. /* Valid cases - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  14210. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14211. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14212. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14213. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14214. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14215. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14216. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14217. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14218. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14219. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14220. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14221. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14222. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14223. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14224. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14225. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14226. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  14227. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14228. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14229. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14230. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14231. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14232. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  14233. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14234. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14235. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  14236. /* Check vald values of nonce - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  14237. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14238. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14239. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14240. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14241. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  14242. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14243. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14244. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14245. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14246. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  14247. SM4_GCM_AUTH_E);
  14248. /* Check valid values of tag size - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  14249. for (i = WOLFSSL_MIN_AUTH_TAG_SZ; i < SM4_BLOCK_SIZE; i++) {
  14250. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14251. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  14252. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14253. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  14254. }
  14255. /* Check different in/out sizes. */
  14256. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce,
  14257. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14258. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce,
  14259. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14260. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce,
  14261. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14262. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  14263. XMEMCPY(out2, out, i - 1);
  14264. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, i, nonce, GCM_NONCE_MID_SZ,
  14265. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14266. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  14267. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, dec, out, i, nonce, GCM_NONCE_MID_SZ,
  14268. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14269. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  14270. }
  14271. /* Force the counter to roll over in first byte. */
  14272. {
  14273. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  14274. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  14275. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  14276. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14277. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  14278. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14279. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  14280. }
  14281. wc_Sm4Free(&sm4);
  14282. res = EXPECT_RESULT();
  14283. #endif
  14284. return res;
  14285. } /* END test_wc_Sm4Gcm */
  14286. /*
  14287. * Testing stream SM4-CCM API.
  14288. */
  14289. static int test_wc_Sm4Ccm(void)
  14290. {
  14291. int res = TEST_SKIPPED;
  14292. #ifdef WOLFSSL_SM4_CCM
  14293. EXPECT_DECLS;
  14294. wc_Sm4 sm4;
  14295. unsigned char key[SM4_KEY_SIZE];
  14296. unsigned char nonce[CCM_NONCE_MAX_SZ];
  14297. unsigned char in[SM4_BLOCK_SIZE * 2];
  14298. unsigned char in2[SM4_BLOCK_SIZE * 2];
  14299. unsigned char out[SM4_BLOCK_SIZE * 2];
  14300. unsigned char out2[SM4_BLOCK_SIZE * 2];
  14301. unsigned char dec[SM4_BLOCK_SIZE * 2];
  14302. unsigned char tag[SM4_BLOCK_SIZE];
  14303. unsigned char aad[SM4_BLOCK_SIZE * 2];
  14304. word32 i;
  14305. XMEMSET(key, 0, sizeof(key));
  14306. XMEMSET(nonce, 0, sizeof(nonce));
  14307. XMEMSET(in, 0, sizeof(in));
  14308. XMEMSET(in2, 0, sizeof(in2));
  14309. XMEMSET(aad, 0, sizeof(aad));
  14310. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14311. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  14312. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14313. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  14314. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14315. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14316. /* Invalid parameters - wc_Sm4CcmEncrypt. */
  14317. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14318. 0), BAD_FUNC_ARG);
  14319. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14320. 0), BAD_FUNC_ARG);
  14321. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14322. 0), BAD_FUNC_ARG);
  14323. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14324. 0), BAD_FUNC_ARG);
  14325. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14326. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14327. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14328. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14329. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14330. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14331. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14332. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14333. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14334. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14335. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  14336. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14337. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  14338. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14339. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14340. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14341. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14342. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14343. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14344. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14345. /* Invalid parameters - wc_Sm4CcmDecrypt. */
  14346. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14347. 0), BAD_FUNC_ARG);
  14348. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14349. 0), BAD_FUNC_ARG);
  14350. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14351. 0), BAD_FUNC_ARG);
  14352. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14353. 0), BAD_FUNC_ARG);
  14354. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14355. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14356. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14357. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14358. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14359. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14360. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14361. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14362. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14363. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14364. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  14365. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14366. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  14367. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14368. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14369. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14370. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14371. WOLFSSL_MIN_AUTH_TAG_SZ - 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14372. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14373. SM4_BLOCK_SIZE + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14374. /* Valid cases - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  14375. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14376. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14377. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14378. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14379. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14380. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14381. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14382. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14383. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14384. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14385. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14386. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14387. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14388. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14389. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14390. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14391. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  14392. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14393. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14394. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14395. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14396. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14397. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  14398. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14399. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14400. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  14401. /* Check vald values of nonce - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  14402. for (i = CCM_NONCE_MIN_SZ; i <= CCM_NONCE_MAX_SZ; i++) {
  14403. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14404. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14405. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14406. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14407. }
  14408. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14409. CCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  14410. SM4_CCM_AUTH_E);
  14411. /* Check invalid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  14412. for (i = 0; i < 4; i++) {
  14413. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14414. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14415. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14416. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14417. }
  14418. /* Odd values in range 4..SM4_BLOCK_SIZE. */
  14419. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  14420. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14421. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14422. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14423. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14424. }
  14425. /* Check valid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt.
  14426. * Even values in range 4..SM4_BLOCK_SIZE.
  14427. */
  14428. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  14429. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14430. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  14431. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14432. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  14433. }
  14434. /* Check different in/out sizes. */
  14435. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce,
  14436. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14437. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce,
  14438. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14439. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce,
  14440. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14441. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  14442. XMEMCPY(out2, out, i - 1);
  14443. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, i, nonce, CCM_NONCE_MAX_SZ,
  14444. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14445. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  14446. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, dec, out, i, nonce, CCM_NONCE_MAX_SZ,
  14447. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14448. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  14449. }
  14450. /* Force the counter to roll over in first byte. */
  14451. {
  14452. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  14453. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  14454. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  14455. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14456. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  14457. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14458. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  14459. }
  14460. wc_Sm4Free(&sm4);
  14461. res = EXPECT_RESULT();
  14462. #endif
  14463. return res;
  14464. } /* END test_wc_Sm4Ccm */
  14465. /*
  14466. * unit test for wc_Des3_SetIV()
  14467. */
  14468. static int test_wc_Des3_SetIV(void)
  14469. {
  14470. EXPECT_DECLS;
  14471. #ifndef NO_DES3
  14472. Des3 des;
  14473. const byte key[] = {
  14474. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14475. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14476. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14477. };
  14478. const byte iv[] = {
  14479. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14480. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14481. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14482. };
  14483. XMEMSET(&des, 0, sizeof(Des3));
  14484. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  14485. /* DES_ENCRYPTION or DES_DECRYPTION */
  14486. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  14487. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  14488. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  14489. /* Test explicitly wc_Des3_SetIV() */
  14490. ExpectIntEQ(wc_Des3_SetIV(NULL, iv), BAD_FUNC_ARG);
  14491. ExpectIntEQ(wc_Des3_SetIV(&des, NULL), 0);
  14492. #endif
  14493. wc_Des3Free(&des);
  14494. #endif
  14495. return EXPECT_RESULT();
  14496. } /* END test_wc_Des3_SetIV */
  14497. /*
  14498. * unit test for wc_Des3_SetKey()
  14499. */
  14500. static int test_wc_Des3_SetKey(void)
  14501. {
  14502. EXPECT_DECLS;
  14503. #ifndef NO_DES3
  14504. Des3 des;
  14505. const byte key[] = {
  14506. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14507. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14508. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14509. };
  14510. const byte iv[] = {
  14511. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14512. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14513. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14514. };
  14515. XMEMSET(&des, 0, sizeof(Des3));
  14516. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  14517. /* DES_ENCRYPTION or DES_DECRYPTION */
  14518. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  14519. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  14520. /* Test bad args. */
  14521. ExpectIntEQ(wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  14522. ExpectIntEQ(wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  14523. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, -1), BAD_FUNC_ARG);
  14524. /* Default case. Should return 0. */
  14525. ExpectIntEQ(wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION), 0);
  14526. wc_Des3Free(&des);
  14527. #endif
  14528. return EXPECT_RESULT();
  14529. } /* END test_wc_Des3_SetKey */
  14530. /*
  14531. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  14532. */
  14533. static int test_wc_Des3_CbcEncryptDecrypt(void)
  14534. {
  14535. EXPECT_DECLS;
  14536. #ifndef NO_DES3
  14537. Des3 des;
  14538. byte cipher[24];
  14539. byte plain[24];
  14540. const byte key[] = {
  14541. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14542. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14543. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14544. };
  14545. const byte iv[] = {
  14546. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14547. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14548. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14549. };
  14550. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  14551. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14552. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14553. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14554. };
  14555. XMEMSET(&des, 0, sizeof(Des3));
  14556. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  14557. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  14558. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, vector, 24), 0);
  14559. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION), 0);
  14560. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, cipher, 24), 0);
  14561. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  14562. /* Pass in bad args. */
  14563. ExpectIntEQ(wc_Des3_CbcEncrypt(NULL, cipher, vector, 24), BAD_FUNC_ARG);
  14564. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, NULL, vector, 24), BAD_FUNC_ARG);
  14565. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector)),
  14566. BAD_FUNC_ARG);
  14567. ExpectIntEQ(wc_Des3_CbcDecrypt(NULL, plain, cipher, 24), BAD_FUNC_ARG);
  14568. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, NULL, cipher, 24), BAD_FUNC_ARG);
  14569. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, NULL, 24), BAD_FUNC_ARG);
  14570. wc_Des3Free(&des);
  14571. #endif
  14572. return EXPECT_RESULT();
  14573. } /* END wc_Des3_CbcEncrypt */
  14574. /*
  14575. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  14576. */
  14577. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  14578. {
  14579. EXPECT_DECLS;
  14580. #ifndef NO_DES3
  14581. word32 vectorSz, cipherSz;
  14582. byte cipher[24];
  14583. byte plain[24];
  14584. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  14585. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14586. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14587. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14588. };
  14589. byte key[] = {
  14590. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14591. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14592. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14593. };
  14594. byte iv[] = {
  14595. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14596. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14597. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14598. };
  14599. vectorSz = sizeof(byte) * 24;
  14600. cipherSz = sizeof(byte) * 24;
  14601. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv),
  14602. 0);
  14603. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv), 0);
  14604. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  14605. /* pass in bad args. */
  14606. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv),
  14607. BAD_FUNC_ARG);
  14608. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv),
  14609. BAD_FUNC_ARG);
  14610. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv),
  14611. BAD_FUNC_ARG);
  14612. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, NULL),
  14613. 0);
  14614. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv),
  14615. BAD_FUNC_ARG);
  14616. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv),
  14617. BAD_FUNC_ARG);
  14618. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv),
  14619. BAD_FUNC_ARG);
  14620. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL),
  14621. 0);
  14622. #endif
  14623. return EXPECT_RESULT();
  14624. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  14625. /*
  14626. * Unit test for wc_Des3_EcbEncrypt
  14627. */
  14628. static int test_wc_Des3_EcbEncrypt(void)
  14629. {
  14630. EXPECT_DECLS;
  14631. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  14632. Des3 des;
  14633. byte cipher[24];
  14634. word32 cipherSz = sizeof(cipher);
  14635. const byte key[] = {
  14636. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14637. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14638. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14639. };
  14640. const byte iv[] = {
  14641. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14642. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14643. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14644. };
  14645. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  14646. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14647. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14648. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14649. };
  14650. XMEMSET(&des, 0, sizeof(Des3));
  14651. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  14652. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  14653. /* Bad Cases */
  14654. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14655. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz),
  14656. BAD_FUNC_ARG);
  14657. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz), BAD_FUNC_ARG);
  14658. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz), BAD_FUNC_ARG);
  14659. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, 0), 0);
  14660. /* Good Cases */
  14661. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz), 0);
  14662. wc_Des3Free(&des);
  14663. #endif
  14664. return EXPECT_RESULT();
  14665. } /* END test_wc_Des3_EcbEncrypt */
  14666. /*
  14667. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  14668. */
  14669. static int test_wc_Chacha_SetKey(void)
  14670. {
  14671. EXPECT_DECLS;
  14672. #ifdef HAVE_CHACHA
  14673. ChaCha ctx;
  14674. const byte key[] = {
  14675. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14676. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14677. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14678. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14679. };
  14680. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  14681. byte cipher[128];
  14682. XMEMSET(cipher, 0, sizeof(cipher));
  14683. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, keySz), 0);
  14684. /* Test bad args. */
  14685. ExpectIntEQ(wc_Chacha_SetKey(NULL, key, keySz), BAD_FUNC_ARG);
  14686. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  14687. ExpectIntEQ(wc_Chacha_SetIV(&ctx, cipher, 0), 0);
  14688. /* Test bad args. */
  14689. ExpectIntEQ(wc_Chacha_SetIV(NULL, cipher, 0), BAD_FUNC_ARG);
  14690. #endif
  14691. return EXPECT_RESULT();
  14692. } /* END test_wc_Chacha_SetKey */
  14693. /*
  14694. * unit test for wc_Poly1305SetKey()
  14695. */
  14696. static int test_wc_Poly1305SetKey(void)
  14697. {
  14698. EXPECT_DECLS;
  14699. #ifdef HAVE_POLY1305
  14700. Poly1305 ctx;
  14701. const byte key[] =
  14702. {
  14703. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14704. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14705. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14706. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14707. };
  14708. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  14709. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, keySz), 0);
  14710. /* Test bad args. */
  14711. ExpectIntEQ(wc_Poly1305SetKey(NULL, key,keySz), BAD_FUNC_ARG);
  14712. ExpectIntEQ(wc_Poly1305SetKey(&ctx, NULL, keySz), BAD_FUNC_ARG);
  14713. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  14714. #endif
  14715. return EXPECT_RESULT();
  14716. } /* END test_wc_Poly1305_SetKey() */
  14717. /*
  14718. * Testing wc_Chacha_Process()
  14719. */
  14720. static int test_wc_Chacha_Process(void)
  14721. {
  14722. EXPECT_DECLS;
  14723. #ifdef HAVE_CHACHA
  14724. ChaCha enc, dec;
  14725. byte cipher[128];
  14726. byte plain[128];
  14727. const byte key[] =
  14728. {
  14729. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14730. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14731. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14732. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14733. };
  14734. const char* input = "Everybody gets Friday off.";
  14735. word32 keySz = sizeof(key)/sizeof(byte);
  14736. unsigned long int inlen = XSTRLEN(input);
  14737. /* Initialize stack variables. */
  14738. XMEMSET(cipher, 0, 128);
  14739. XMEMSET(plain, 0, 128);
  14740. ExpectIntEQ(wc_Chacha_SetKey(&enc, key, keySz), 0);
  14741. ExpectIntEQ(wc_Chacha_SetKey(&dec, key, keySz), 0);
  14742. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  14743. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  14744. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen),
  14745. 0);
  14746. ExpectIntEQ(wc_Chacha_Process(&dec, plain, cipher, (word32)inlen), 0);
  14747. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  14748. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  14749. /* test checking and using leftovers, currently just in C code */
  14750. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  14751. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  14752. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input,
  14753. (word32)inlen - 2), 0);
  14754. ExpectIntEQ(wc_Chacha_Process(&enc, cipher + (inlen - 2),
  14755. (byte*)input + (inlen - 2), 2), 0);
  14756. ExpectIntEQ(wc_Chacha_Process(&dec, plain, (byte*)cipher,
  14757. (word32)inlen - 2), 0);
  14758. ExpectIntEQ(wc_Chacha_Process(&dec, cipher + (inlen - 2),
  14759. (byte*)input + (inlen - 2), 2), 0);
  14760. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  14761. /* check edge cases with counter increment */
  14762. {
  14763. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  14764. const byte expected[] = {
  14765. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  14766. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  14767. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  14768. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  14769. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  14770. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  14771. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  14772. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  14773. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  14774. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  14775. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  14776. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  14777. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  14778. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  14779. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  14780. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  14781. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  14782. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  14783. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  14784. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  14785. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  14786. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  14787. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  14788. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  14789. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  14790. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  14791. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  14792. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  14793. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  14794. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  14795. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  14796. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  14797. };
  14798. const byte iv2[] = {
  14799. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  14800. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  14801. };
  14802. byte input2[256];
  14803. int i;
  14804. for (i = 0; i < 256; i++)
  14805. input2[i] = i;
  14806. ExpectIntEQ(wc_Chacha_SetIV(&enc, iv2, 0), 0);
  14807. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2, 64), 0);
  14808. ExpectIntEQ(XMEMCMP(expected, cipher, 64), 0);
  14809. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 64, 128), 0);
  14810. ExpectIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  14811. /* partial */
  14812. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 192, 32), 0);
  14813. ExpectIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  14814. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 224, 32), 0);
  14815. ExpectIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  14816. }
  14817. #endif
  14818. /* Test bad args. */
  14819. ExpectIntEQ(wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen),
  14820. BAD_FUNC_ARG);
  14821. #endif
  14822. return EXPECT_RESULT();
  14823. } /* END test_wc_Chacha_Process */
  14824. /*
  14825. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  14826. */
  14827. static int test_wc_ChaCha20Poly1305_aead(void)
  14828. {
  14829. EXPECT_DECLS;
  14830. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  14831. const byte key[] = {
  14832. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  14833. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  14834. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  14835. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  14836. };
  14837. const byte plaintext[] = {
  14838. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  14839. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  14840. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  14841. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  14842. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  14843. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  14844. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  14845. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  14846. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  14847. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  14848. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  14849. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  14850. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  14851. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  14852. 0x74, 0x2e
  14853. };
  14854. const byte iv[] = {
  14855. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  14856. 0x44, 0x45, 0x46, 0x47
  14857. };
  14858. const byte aad[] = { /* additional data */
  14859. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  14860. 0xc4, 0xc5, 0xc6, 0xc7
  14861. };
  14862. const byte cipher[] = { /* expected output from operation */
  14863. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  14864. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  14865. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  14866. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  14867. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  14868. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  14869. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  14870. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  14871. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  14872. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  14873. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  14874. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  14875. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  14876. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  14877. 0x61, 0x16
  14878. };
  14879. const byte authTag[] = { /* expected output from operation */
  14880. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  14881. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  14882. };
  14883. byte generatedCiphertext[272];
  14884. byte generatedPlaintext[272];
  14885. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  14886. /* Initialize stack variables. */
  14887. XMEMSET(generatedCiphertext, 0, 272);
  14888. XMEMSET(generatedPlaintext, 0, 272);
  14889. /* Test Encrypt */
  14890. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14891. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  14892. 0);
  14893. ExpectIntEQ(XMEMCMP(generatedCiphertext, cipher,
  14894. sizeof(cipher)/sizeof(byte)), 0);
  14895. /* Test bad args. */
  14896. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad),
  14897. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  14898. BAD_FUNC_ARG);
  14899. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  14900. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  14901. BAD_FUNC_ARG);
  14902. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  14903. sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  14904. BAD_FUNC_ARG);
  14905. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14906. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  14907. BAD_FUNC_ARG);
  14908. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14909. plaintext, sizeof(plaintext), NULL, generatedAuthTag), BAD_FUNC_ARG);
  14910. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14911. plaintext, sizeof(plaintext), generatedCiphertext, NULL), BAD_FUNC_ARG);
  14912. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14913. sizeof(cipher), authTag, generatedPlaintext), 0);
  14914. ExpectIntEQ(XMEMCMP(generatedPlaintext, plaintext,
  14915. sizeof(plaintext)/sizeof(byte)), 0);
  14916. /* Test bad args. */
  14917. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  14918. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  14919. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  14920. cipher, sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  14921. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  14922. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  14923. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14924. sizeof(cipher), NULL, generatedPlaintext), BAD_FUNC_ARG);
  14925. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14926. sizeof(cipher), authTag, NULL), BAD_FUNC_ARG);
  14927. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  14928. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  14929. #endif
  14930. return EXPECT_RESULT();
  14931. } /* END test_wc_ChaCha20Poly1305_aead */
  14932. /*
  14933. * Testing function for wc_Rc2SetKey().
  14934. */
  14935. static int test_wc_Rc2SetKey(void)
  14936. {
  14937. EXPECT_DECLS;
  14938. #ifdef WC_RC2
  14939. Rc2 rc2;
  14940. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  14941. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  14942. /* valid key and IV */
  14943. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  14944. iv, 40), 0);
  14945. /* valid key, no IV */
  14946. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  14947. NULL, 40), 0);
  14948. /* bad arguments */
  14949. /* null Rc2 struct */
  14950. ExpectIntEQ(wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  14951. iv, 40), BAD_FUNC_ARG);
  14952. /* null key */
  14953. ExpectIntEQ(wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  14954. iv, 40), BAD_FUNC_ARG);
  14955. /* key size == 0 */
  14956. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 0, iv, 40), WC_KEY_SIZE_E);
  14957. /* key size > 128 */
  14958. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 129, iv, 40), WC_KEY_SIZE_E);
  14959. /* effective bits == 0 */
  14960. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  14961. iv, 0), WC_KEY_SIZE_E);
  14962. /* effective bits > 1024 */
  14963. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  14964. iv, 1025), WC_KEY_SIZE_E);
  14965. #endif
  14966. return EXPECT_RESULT();
  14967. } /* END test_wc_Rc2SetKey */
  14968. /*
  14969. * Testing function for wc_Rc2SetIV().
  14970. */
  14971. static int test_wc_Rc2SetIV(void)
  14972. {
  14973. EXPECT_DECLS;
  14974. #ifdef WC_RC2
  14975. Rc2 rc2;
  14976. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  14977. /* valid IV */
  14978. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  14979. /* valid NULL IV */
  14980. ExpectIntEQ(wc_Rc2SetIV(&rc2, NULL), 0);
  14981. /* bad arguments */
  14982. ExpectIntEQ(wc_Rc2SetIV(NULL, iv), BAD_FUNC_ARG);
  14983. ExpectIntEQ(wc_Rc2SetIV(NULL, NULL), BAD_FUNC_ARG);
  14984. #endif
  14985. return EXPECT_RESULT();
  14986. } /* END test_wc_Rc2SetIV */
  14987. /*
  14988. * Testing function for wc_Rc2EcbEncrypt() and wc_Rc2EcbDecrypt().
  14989. */
  14990. static int test_wc_Rc2EcbEncryptDecrypt(void)
  14991. {
  14992. EXPECT_DECLS;
  14993. #ifdef WC_RC2
  14994. Rc2 rc2;
  14995. int effectiveKeyBits = 63;
  14996. byte cipher[RC2_BLOCK_SIZE];
  14997. byte plain[RC2_BLOCK_SIZE];
  14998. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  14999. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  15000. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  15001. XMEMSET(cipher, 0, sizeof(cipher));
  15002. XMEMSET(plain, 0, sizeof(plain));
  15003. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  15004. NULL, effectiveKeyBits), 0);
  15005. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE), 0);
  15006. ExpectIntEQ(XMEMCMP(cipher, output, RC2_BLOCK_SIZE), 0);
  15007. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE), 0);
  15008. ExpectIntEQ(XMEMCMP(plain, input, RC2_BLOCK_SIZE), 0);
  15009. /* Rc2EcbEncrypt bad arguments */
  15010. /* null Rc2 struct */
  15011. ExpectIntEQ(wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE),
  15012. BAD_FUNC_ARG);
  15013. /* null out buffer */
  15014. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE),
  15015. BAD_FUNC_ARG);
  15016. /* null input buffer */
  15017. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE),
  15018. BAD_FUNC_ARG);
  15019. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  15020. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, 7), BUFFER_E);
  15021. /* Rc2EcbDecrypt bad arguments */
  15022. /* null Rc2 struct */
  15023. ExpectIntEQ(wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE),
  15024. BAD_FUNC_ARG);
  15025. /* null out buffer */
  15026. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE),
  15027. BAD_FUNC_ARG);
  15028. /* null input buffer */
  15029. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE),
  15030. BAD_FUNC_ARG);
  15031. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  15032. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, output, 7), BUFFER_E);
  15033. #endif
  15034. return EXPECT_RESULT();
  15035. } /* END test_wc_Rc2EcbEncryptDecrypt */
  15036. /*
  15037. * Testing function for wc_Rc2CbcEncrypt() and wc_Rc2CbcDecrypt().
  15038. */
  15039. static int test_wc_Rc2CbcEncryptDecrypt(void)
  15040. {
  15041. EXPECT_DECLS;
  15042. #ifdef WC_RC2
  15043. Rc2 rc2;
  15044. int effectiveKeyBits = 63;
  15045. byte cipher[RC2_BLOCK_SIZE*2];
  15046. byte plain[RC2_BLOCK_SIZE*2];
  15047. /* vector taken from test.c */
  15048. byte key[] = {
  15049. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15050. };
  15051. byte iv[] = {
  15052. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15053. };
  15054. byte input[] = {
  15055. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  15056. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15057. };
  15058. byte output[] = {
  15059. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  15060. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  15061. };
  15062. XMEMSET(cipher, 0, sizeof(cipher));
  15063. XMEMSET(plain, 0, sizeof(plain));
  15064. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  15065. iv, effectiveKeyBits), 0);
  15066. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input)), 0);
  15067. ExpectIntEQ(XMEMCMP(cipher, output, sizeof(output)), 0);
  15068. /* reset IV for decrypt */
  15069. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  15070. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher)), 0);
  15071. ExpectIntEQ(XMEMCMP(plain, input, sizeof(input)), 0);
  15072. /* Rc2CbcEncrypt bad arguments */
  15073. /* null Rc2 struct */
  15074. ExpectIntEQ(wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input)),
  15075. BAD_FUNC_ARG);
  15076. /* null out buffer */
  15077. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input)),
  15078. BAD_FUNC_ARG);
  15079. /* null input buffer */
  15080. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input)),
  15081. BAD_FUNC_ARG);
  15082. /* Rc2CbcDecrypt bad arguments */
  15083. /* in size is 0 */
  15084. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, output, 0), 0);
  15085. /* null Rc2 struct */
  15086. ExpectIntEQ(wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output)),
  15087. BAD_FUNC_ARG);
  15088. /* null out buffer */
  15089. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output)),
  15090. BAD_FUNC_ARG);
  15091. /* null input buffer */
  15092. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output)),
  15093. BAD_FUNC_ARG);
  15094. #endif
  15095. return EXPECT_RESULT();
  15096. } /* END test_wc_Rc2CbcEncryptDecrypt */
  15097. /*
  15098. * Testing function for wc_AesSetIV
  15099. */
  15100. static int test_wc_AesSetIV(void)
  15101. {
  15102. int res = TEST_SKIPPED;
  15103. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  15104. Aes aes;
  15105. int ret = 0;
  15106. byte key16[] =
  15107. {
  15108. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15109. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15110. };
  15111. byte iv1[] = "1234567890abcdef";
  15112. byte iv2[] = "0987654321fedcba";
  15113. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15114. if (ret != 0)
  15115. return ret;
  15116. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  15117. iv1, AES_ENCRYPTION);
  15118. if (ret == 0) {
  15119. ret = wc_AesSetIV(&aes, iv2);
  15120. }
  15121. /* Test bad args. */
  15122. if (ret == 0) {
  15123. ret = wc_AesSetIV(NULL, iv1);
  15124. if (ret == BAD_FUNC_ARG) {
  15125. /* NULL iv should return 0. */
  15126. ret = wc_AesSetIV(&aes, NULL);
  15127. }
  15128. else {
  15129. ret = WOLFSSL_FATAL_ERROR;
  15130. }
  15131. }
  15132. wc_AesFree(&aes);
  15133. res = TEST_RES_CHECK(ret == 0);
  15134. #endif
  15135. return res;
  15136. } /* test_wc_AesSetIV */
  15137. /*
  15138. * Testing function for wc_AesSetKey().
  15139. */
  15140. static int test_wc_AesSetKey(void)
  15141. {
  15142. EXPECT_DECLS;
  15143. #ifndef NO_AES
  15144. Aes aes;
  15145. byte key16[] = {
  15146. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15147. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15148. };
  15149. #ifdef WOLFSSL_AES_192
  15150. byte key24[] = {
  15151. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15152. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15153. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15154. };
  15155. #endif
  15156. #ifdef WOLFSSL_AES_256
  15157. byte key32[] = {
  15158. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15159. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15160. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15161. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15162. };
  15163. #endif
  15164. byte badKey16[] = {
  15165. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15166. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15167. };
  15168. byte iv[] = "1234567890abcdef";
  15169. XMEMSET(&aes, 0, sizeof(Aes));
  15170. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15171. #ifdef WOLFSSL_AES_128
  15172. ExpectIntEQ(wc_AesSetKey(&aes, key16, (word32)sizeof(key16) / sizeof(byte),
  15173. iv, AES_ENCRYPTION), 0);
  15174. #endif
  15175. #ifdef WOLFSSL_AES_192
  15176. ExpectIntEQ(wc_AesSetKey(&aes, key24, (word32)sizeof(key24) / sizeof(byte),
  15177. iv, AES_ENCRYPTION), 0);
  15178. #endif
  15179. #ifdef WOLFSSL_AES_256
  15180. ExpectIntEQ(wc_AesSetKey(&aes, key32, (word32)sizeof(key32) / sizeof(byte),
  15181. iv, AES_ENCRYPTION), 0);
  15182. #endif
  15183. /* Pass in bad args. */
  15184. ExpectIntEQ(wc_AesSetKey(NULL, key16, (word32)sizeof(key16) / sizeof(byte),
  15185. iv, AES_ENCRYPTION), BAD_FUNC_ARG);
  15186. ExpectIntEQ(wc_AesSetKey(&aes, badKey16,
  15187. (word32)sizeof(badKey16) / sizeof(byte), iv, AES_ENCRYPTION),
  15188. BAD_FUNC_ARG);
  15189. wc_AesFree(&aes);
  15190. #endif
  15191. return EXPECT_RESULT();
  15192. } /* END test_wc_AesSetKey */
  15193. /*
  15194. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  15195. * and wc_AesCbcDecryptWithKey()
  15196. */
  15197. static int test_wc_AesCbcEncryptDecrypt(void)
  15198. {
  15199. EXPECT_DECLS;
  15200. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  15201. defined(WOLFSSL_AES_256)
  15202. Aes aes;
  15203. byte key32[] = {
  15204. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15205. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15206. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15207. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15208. };
  15209. byte vector[] = { /* Now is the time for all good men w/o trailing 0 */
  15210. 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
  15211. 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20,
  15212. 0x66, 0x6f, 0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20,
  15213. 0x67, 0x6f, 0x6f, 0x64, 0x20, 0x6d, 0x65, 0x6e
  15214. };
  15215. byte iv[] = "1234567890abcdef";
  15216. byte enc[sizeof(vector)];
  15217. byte dec[sizeof(vector)];
  15218. byte dec2[sizeof(vector)];
  15219. /* Init stack variables. */
  15220. XMEMSET(&aes, 0, sizeof(Aes));
  15221. XMEMSET(enc, 0, sizeof(enc));
  15222. XMEMSET(dec, 0, sizeof(vector));
  15223. XMEMSET(dec2, 0, sizeof(vector));
  15224. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15225. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  15226. AES_ENCRYPTION), 0);
  15227. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector)), 0);
  15228. wc_AesFree(&aes);
  15229. /* Re init for decrypt and set flag. */
  15230. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  15231. AES_DECRYPTION), 0);
  15232. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector)), 0);
  15233. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  15234. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE, key32,
  15235. sizeof(key32)/sizeof(byte), iv), 0);
  15236. ExpectIntEQ(XMEMCMP(vector, dec2, AES_BLOCK_SIZE), 0);
  15237. /* Pass in bad args */
  15238. ExpectIntEQ(wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector)),
  15239. BAD_FUNC_ARG);
  15240. ExpectIntEQ(wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector)),
  15241. BAD_FUNC_ARG);
  15242. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector)),
  15243. BAD_FUNC_ARG);
  15244. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15245. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1),
  15246. BAD_LENGTH_E);
  15247. #endif
  15248. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15249. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  15250. fprintf(stderr, "Zero length inputs not supported with AESNI in FIPS "
  15251. "mode (v2), skip test");
  15252. #else
  15253. /* Test passing in size of 0 */
  15254. XMEMSET(enc, 0, sizeof(enc));
  15255. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, 0), 0);
  15256. /* Check enc was not modified */
  15257. {
  15258. int i;
  15259. for (i = 0; i < (int)sizeof(enc); i++)
  15260. ExpectIntEQ(enc[i], 0);
  15261. }
  15262. #endif
  15263. ExpectIntEQ(wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE), BAD_FUNC_ARG);
  15264. ExpectIntEQ(wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE),
  15265. BAD_FUNC_ARG);
  15266. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE),
  15267. BAD_FUNC_ARG);
  15268. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15269. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  15270. BAD_LENGTH_E);
  15271. #else
  15272. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  15273. BAD_FUNC_ARG);
  15274. #endif
  15275. /* Test passing in size of 0 */
  15276. XMEMSET(dec, 0, sizeof(dec));
  15277. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, 0), 0);
  15278. /* Check dec was not modified */
  15279. {
  15280. int i;
  15281. for (i = 0; i < (int)sizeof(dec); i++)
  15282. ExpectIntEQ(dec[i], 0);
  15283. }
  15284. ExpectIntEQ(wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  15285. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  15286. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  15287. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  15288. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15289. NULL, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  15290. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15291. key32, sizeof(key32)/sizeof(byte), NULL), BAD_FUNC_ARG);
  15292. wc_AesFree(&aes);
  15293. #endif
  15294. return EXPECT_RESULT();
  15295. } /* END test_wc_AesCbcEncryptDecrypt */
  15296. /*
  15297. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  15298. */
  15299. static int test_wc_AesCtrEncryptDecrypt(void)
  15300. {
  15301. EXPECT_DECLS;
  15302. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  15303. Aes aesEnc;
  15304. Aes aesDec;
  15305. byte key32[] = {
  15306. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15307. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15308. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15309. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15310. };
  15311. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15312. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15313. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15314. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15315. };
  15316. byte iv[] = "1234567890abcdef";
  15317. byte enc[AES_BLOCK_SIZE * 2];
  15318. byte dec[AES_BLOCK_SIZE * 2];
  15319. /* Init stack variables. */
  15320. XMEMSET(&aesEnc, 0, sizeof(Aes));
  15321. XMEMSET(&aesDec, 0, sizeof(Aes));
  15322. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  15323. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  15324. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  15325. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  15326. ExpectIntEQ(wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2, iv,
  15327. AES_ENCRYPTION), 0);
  15328. ExpectIntEQ(wc_AesCtrEncrypt(&aesEnc, enc, vector,
  15329. sizeof(vector)/sizeof(byte)), 0);
  15330. /* Decrypt with wc_AesCtrEncrypt() */
  15331. ExpectIntEQ(wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2, iv,
  15332. AES_ENCRYPTION), 0);
  15333. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte)),
  15334. 0);
  15335. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  15336. /* Test bad args. */
  15337. ExpectIntEQ(wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte)),
  15338. BAD_FUNC_ARG);
  15339. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte)),
  15340. BAD_FUNC_ARG);
  15341. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte)),
  15342. BAD_FUNC_ARG);
  15343. wc_AesFree(&aesEnc);
  15344. wc_AesFree(&aesDec);
  15345. #endif
  15346. return EXPECT_RESULT();
  15347. } /* END test_wc_AesCtrEncryptDecrypt */
  15348. /*
  15349. * test function for wc_AesGcmSetKey()
  15350. */
  15351. static int test_wc_AesGcmSetKey(void)
  15352. {
  15353. EXPECT_DECLS;
  15354. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15355. Aes aes;
  15356. #ifdef WOLFSSL_AES_128
  15357. byte key16[] = {
  15358. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15359. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15360. };
  15361. #endif
  15362. #ifdef WOLFSSL_AES_192
  15363. byte key24[] = {
  15364. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15365. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15366. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15367. };
  15368. #endif
  15369. #ifdef WOLFSSL_AES_256
  15370. byte key32[] = {
  15371. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15372. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15373. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15374. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15375. };
  15376. #endif
  15377. byte badKey16[] = {
  15378. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15379. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15380. };
  15381. byte badKey24[] = {
  15382. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15383. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15384. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  15385. };
  15386. byte badKey32[] = {
  15387. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  15388. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15389. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15390. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15391. };
  15392. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15393. #ifdef WOLFSSL_AES_128
  15394. ExpectIntEQ(wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte)), 0);
  15395. #endif
  15396. #ifdef WOLFSSL_AES_192
  15397. ExpectIntEQ(wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte)), 0);
  15398. #endif
  15399. #ifdef WOLFSSL_AES_256
  15400. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  15401. #endif
  15402. /* Pass in bad args. */
  15403. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte)),
  15404. BAD_FUNC_ARG);
  15405. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte)),
  15406. BAD_FUNC_ARG);
  15407. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte)),
  15408. BAD_FUNC_ARG);
  15409. wc_AesFree(&aes);
  15410. #endif
  15411. return EXPECT_RESULT();
  15412. } /* END test_wc_AesGcmSetKey */
  15413. /*
  15414. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  15415. */
  15416. static int test_wc_AesGcmEncryptDecrypt(void)
  15417. {
  15418. EXPECT_DECLS;
  15419. /* WOLFSSL_AFALG requires 12 byte IV */
  15420. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  15421. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  15422. Aes aes;
  15423. byte key32[] = {
  15424. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15425. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15426. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15427. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15428. };
  15429. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15430. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15431. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15432. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15433. };
  15434. const byte a[] = {
  15435. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15436. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15437. 0xab, 0xad, 0xda, 0xd2
  15438. };
  15439. byte iv[] = "1234567890a";
  15440. byte longIV[] = "1234567890abcdefghij";
  15441. byte enc[sizeof(vector)];
  15442. byte resultT[AES_BLOCK_SIZE];
  15443. byte dec[sizeof(vector)];
  15444. /* Init stack variables. */
  15445. XMEMSET(&aes, 0, sizeof(Aes));
  15446. XMEMSET(enc, 0, sizeof(vector));
  15447. XMEMSET(dec, 0, sizeof(vector));
  15448. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  15449. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15450. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  15451. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  15452. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  15453. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector), iv,
  15454. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  15455. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  15456. /* Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  15457. ExpectIntEQ(wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector), iv,
  15458. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15459. BAD_FUNC_ARG);
  15460. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  15461. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  15462. BAD_FUNC_ARG);
  15463. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  15464. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) - 5, a, sizeof(a)),
  15465. BAD_FUNC_ARG);
  15466. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15467. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  15468. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15469. /* FIPS does not check the lower bound of ivSz */
  15470. #else
  15471. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv, 0,
  15472. resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  15473. #endif
  15474. /* This case is now considered good. Long IVs are now allowed.
  15475. * Except for the original FIPS release, it still has an upper
  15476. * bound on the IV length. */
  15477. #if (!defined(HAVE_FIPS) || \
  15478. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  15479. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15480. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  15481. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15482. 0);
  15483. #else
  15484. (void)longIV;
  15485. #endif /* Old FIPS */
  15486. /* END wc_AesGcmEncrypt */
  15487. #ifdef HAVE_AES_DECRYPT
  15488. ExpectIntEQ(wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte), iv,
  15489. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15490. BAD_FUNC_ARG);
  15491. ExpectIntEQ(wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte), iv,
  15492. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15493. BAD_FUNC_ARG);
  15494. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte), iv,
  15495. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15496. BAD_FUNC_ARG);
  15497. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), NULL,
  15498. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15499. BAD_FUNC_ARG);
  15500. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  15501. sizeof(iv)/sizeof(byte), NULL, sizeof(resultT), a, sizeof(a)),
  15502. BAD_FUNC_ARG);
  15503. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  15504. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  15505. BAD_FUNC_ARG);
  15506. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15507. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  15508. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15509. /* FIPS does not check the lower bound of ivSz */
  15510. #else
  15511. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15512. iv, 0, resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  15513. #endif
  15514. #endif /* HAVE_AES_DECRYPT */
  15515. wc_AesFree(&aes);
  15516. #endif
  15517. return EXPECT_RESULT();
  15518. } /* END test_wc_AesGcmEncryptDecrypt */
  15519. /*
  15520. * test function for mixed (one-shot encrpytion + stream decryption) AES GCM
  15521. * using a long IV (older FIPS does NOT support long IVs). Relates to zd15423
  15522. */
  15523. static int test_wc_AesGcmMixedEncDecLongIV(void)
  15524. {
  15525. EXPECT_DECLS;
  15526. #if (!defined(HAVE_FIPS) || \
  15527. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  15528. !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AESGCM_STREAM)
  15529. const byte key[] = {
  15530. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15531. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15532. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15533. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15534. };
  15535. const byte in[] = {
  15536. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15537. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15538. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15539. };
  15540. const byte aad[] = {
  15541. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15542. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15543. 0xab, 0xad, 0xda, 0xd2
  15544. };
  15545. Aes aesEnc;
  15546. Aes aesDec;
  15547. byte iv[] = "1234567890abcdefghij";
  15548. byte out[sizeof(in)];
  15549. byte plain[sizeof(in)];
  15550. byte tag[AES_BLOCK_SIZE];
  15551. XMEMSET(&aesEnc, 0, sizeof(Aes));
  15552. XMEMSET(&aesDec, 0, sizeof(Aes));
  15553. XMEMSET(out, 0, sizeof(out));
  15554. XMEMSET(plain, 0, sizeof(plain));
  15555. XMEMSET(tag, 0, sizeof(tag));
  15556. /* Perform one-shot encryption using long IV */
  15557. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  15558. ExpectIntEQ(wc_AesGcmSetKey(&aesEnc, key, sizeof(key)), 0);
  15559. ExpectIntEQ(wc_AesGcmEncrypt(&aesEnc, out, in, sizeof(in), iv, sizeof(iv),
  15560. tag, sizeof(tag), aad, sizeof(aad)), 0);
  15561. /* Perform streaming decryption using long IV */
  15562. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  15563. ExpectIntEQ(wc_AesGcmInit(&aesDec, key, sizeof(key), iv, sizeof(iv)), 0);
  15564. ExpectIntEQ(wc_AesGcmDecryptUpdate(&aesDec, plain, out, sizeof(out), aad,
  15565. sizeof(aad)), 0);
  15566. ExpectIntEQ(wc_AesGcmDecryptFinal(&aesDec, tag, sizeof(tag)), 0);
  15567. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  15568. /* Free resources */
  15569. wc_AesFree(&aesEnc);
  15570. wc_AesFree(&aesDec);
  15571. #endif
  15572. return EXPECT_RESULT();
  15573. } /* END wc_AesGcmMixedEncDecLongIV */
  15574. /*
  15575. * unit test for wc_GmacSetKey()
  15576. */
  15577. static int test_wc_GmacSetKey(void)
  15578. {
  15579. EXPECT_DECLS;
  15580. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15581. Gmac gmac;
  15582. byte key16[] = {
  15583. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15584. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15585. };
  15586. #ifdef WOLFSSL_AES_192
  15587. byte key24[] = {
  15588. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15589. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15590. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15591. };
  15592. #endif
  15593. #ifdef WOLFSSL_AES_256
  15594. byte key32[] = {
  15595. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15596. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15597. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15598. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15599. };
  15600. #endif
  15601. byte badKey16[] = {
  15602. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15603. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  15604. };
  15605. byte badKey24[] = {
  15606. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  15607. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15608. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15609. };
  15610. byte badKey32[] = {
  15611. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15612. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  15613. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15614. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15615. };
  15616. XMEMSET(&gmac, 0, sizeof(Gmac));
  15617. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  15618. #ifdef WOLFSSL_AES_128
  15619. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte)), 0);
  15620. #endif
  15621. #ifdef WOLFSSL_AES_192
  15622. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  15623. #endif
  15624. #ifdef WOLFSSL_AES_256
  15625. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  15626. #endif
  15627. /* Pass in bad args. */
  15628. ExpectIntEQ(wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte)),
  15629. BAD_FUNC_ARG);
  15630. ExpectIntEQ(wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte)),
  15631. BAD_FUNC_ARG);
  15632. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte)),
  15633. BAD_FUNC_ARG);
  15634. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte)),
  15635. BAD_FUNC_ARG);
  15636. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte)),
  15637. BAD_FUNC_ARG);
  15638. wc_AesFree(&gmac.aes);
  15639. #endif
  15640. return EXPECT_RESULT();
  15641. } /* END test_wc_GmacSetKey */
  15642. /*
  15643. * unit test for wc_GmacUpdate
  15644. */
  15645. static int test_wc_GmacUpdate(void)
  15646. {
  15647. EXPECT_DECLS;
  15648. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15649. Gmac gmac;
  15650. #ifdef WOLFSSL_AES_128
  15651. const byte key16[] = {
  15652. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  15653. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  15654. };
  15655. #endif
  15656. #ifdef WOLFSSL_AES_192
  15657. byte key24[] = {
  15658. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  15659. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  15660. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  15661. };
  15662. #endif
  15663. #ifdef WOLFSSL_AES_256
  15664. byte key32[] = {
  15665. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  15666. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  15667. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  15668. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  15669. };
  15670. #endif
  15671. #ifdef WOLFSSL_AES_128
  15672. const byte authIn[] = {
  15673. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  15674. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  15675. };
  15676. #endif
  15677. #ifdef WOLFSSL_AES_192
  15678. const byte authIn2[] = {
  15679. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  15680. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  15681. };
  15682. #endif
  15683. const byte authIn3[] = {
  15684. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  15685. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  15686. };
  15687. #ifdef WOLFSSL_AES_128
  15688. const byte tag1[] = { /* Known. */
  15689. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  15690. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  15691. };
  15692. #endif
  15693. #ifdef WOLFSSL_AES_192
  15694. const byte tag2[] = { /* Known */
  15695. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  15696. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  15697. };
  15698. #endif
  15699. const byte tag3[] = { /* Known */
  15700. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  15701. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  15702. };
  15703. #ifdef WOLFSSL_AES_128
  15704. const byte iv[] = {
  15705. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  15706. 0xe2, 0x8c, 0x8f, 0x16
  15707. };
  15708. #endif
  15709. #ifdef WOLFSSL_AES_192
  15710. const byte iv2[] = {
  15711. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  15712. 0x7e, 0x1a, 0x6f, 0xbc
  15713. };
  15714. #endif
  15715. const byte iv3[] = {
  15716. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  15717. 0xc3, 0xfb, 0x6c, 0x8a
  15718. };
  15719. byte tagOut[16];
  15720. byte tagOut2[24];
  15721. byte tagOut3[32];
  15722. /* Init stack variables. */
  15723. XMEMSET(&gmac, 0, sizeof(Gmac));
  15724. XMEMSET(tagOut, 0, sizeof(tagOut));
  15725. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  15726. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  15727. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  15728. #ifdef WOLFSSL_AES_128
  15729. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)), 0);
  15730. ExpectIntEQ(wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  15731. tagOut, sizeof(tag1)), 0);
  15732. ExpectIntEQ(XMEMCMP(tag1, tagOut, sizeof(tag1)), 0);
  15733. #endif
  15734. #ifdef WOLFSSL_AES_192
  15735. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  15736. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  15737. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  15738. ExpectIntEQ(wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2, sizeof(authIn2),
  15739. tagOut2, sizeof(tag2)), 0);
  15740. ExpectIntEQ(XMEMCMP(tagOut2, tag2, sizeof(tag2)), 0);
  15741. #endif
  15742. #ifdef WOLFSSL_AES_256
  15743. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  15744. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  15745. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  15746. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  15747. tagOut3, sizeof(tag3)), 0);
  15748. ExpectIntEQ(XMEMCMP(tag3, tagOut3, sizeof(tag3)), 0);
  15749. #endif
  15750. /* Pass bad args. */
  15751. ExpectIntEQ(wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  15752. tagOut3, sizeof(tag3)), BAD_FUNC_ARG);
  15753. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  15754. tagOut3, sizeof(tag3) - 5), BAD_FUNC_ARG);
  15755. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  15756. tagOut3, sizeof(tag3) + 1), BAD_FUNC_ARG);
  15757. wc_AesFree(&gmac.aes);
  15758. #endif
  15759. return EXPECT_RESULT();
  15760. } /* END test_wc_GmacUpdate */
  15761. /*
  15762. * testing wc_CamelliaSetKey
  15763. */
  15764. static int test_wc_CamelliaSetKey(void)
  15765. {
  15766. EXPECT_DECLS;
  15767. #ifdef HAVE_CAMELLIA
  15768. Camellia camellia;
  15769. /*128-bit key*/
  15770. static const byte key16[] = {
  15771. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15772. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  15773. };
  15774. /* 192-bit key */
  15775. static const byte key24[] = {
  15776. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15777. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15778. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15779. };
  15780. /* 256-bit key */
  15781. static const byte key32[] = {
  15782. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15783. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15784. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  15785. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  15786. };
  15787. static const byte iv[] = {
  15788. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15789. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15790. };
  15791. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv),
  15792. 0);
  15793. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16),
  15794. NULL), 0);
  15795. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  15796. 0);
  15797. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  15798. NULL), 0);
  15799. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32), iv),
  15800. 0);
  15801. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32),
  15802. NULL), 0);
  15803. /* Bad args. */
  15804. ExpectIntEQ(wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv),
  15805. BAD_FUNC_ARG);
  15806. #endif
  15807. return EXPECT_RESULT();
  15808. } /* END test_wc_CammeliaSetKey */
  15809. /*
  15810. * Testing wc_CamelliaSetIV()
  15811. */
  15812. static int test_wc_CamelliaSetIV(void)
  15813. {
  15814. EXPECT_DECLS;
  15815. #ifdef HAVE_CAMELLIA
  15816. Camellia camellia;
  15817. static const byte iv[] = {
  15818. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15819. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15820. };
  15821. ExpectIntEQ(wc_CamelliaSetIV(&camellia, iv), 0);
  15822. ExpectIntEQ(wc_CamelliaSetIV(&camellia, NULL), 0);
  15823. /* Bad args. */
  15824. ExpectIntEQ(wc_CamelliaSetIV(NULL, NULL), BAD_FUNC_ARG);
  15825. ExpectIntEQ(wc_CamelliaSetIV(NULL, iv), BAD_FUNC_ARG);
  15826. #endif
  15827. return EXPECT_RESULT();
  15828. } /* END test_wc_CamelliaSetIV*/
  15829. /*
  15830. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  15831. */
  15832. static int test_wc_CamelliaEncryptDecryptDirect(void)
  15833. {
  15834. EXPECT_DECLS;
  15835. #ifdef HAVE_CAMELLIA
  15836. Camellia camellia;
  15837. static const byte key24[] = {
  15838. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15839. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15840. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15841. };
  15842. static const byte iv[] = {
  15843. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15844. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15845. };
  15846. static const byte plainT[] = {
  15847. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  15848. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  15849. };
  15850. byte enc[sizeof(plainT)];
  15851. byte dec[sizeof(enc)];
  15852. /* Init stack variables.*/
  15853. XMEMSET(enc, 0, 16);
  15854. XMEMSET(enc, 0, 16);
  15855. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  15856. 0);
  15857. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, plainT), 0);
  15858. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, enc), 0);
  15859. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  15860. /* Pass bad args. */
  15861. ExpectIntEQ(wc_CamelliaEncryptDirect(NULL, enc, plainT), BAD_FUNC_ARG);
  15862. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, NULL, plainT),
  15863. BAD_FUNC_ARG);
  15864. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, NULL), BAD_FUNC_ARG);
  15865. ExpectIntEQ(wc_CamelliaDecryptDirect(NULL, dec, enc), BAD_FUNC_ARG);
  15866. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, NULL, enc), BAD_FUNC_ARG);
  15867. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, NULL), BAD_FUNC_ARG);
  15868. #endif
  15869. return EXPECT_RESULT();
  15870. } /* END test-wc_CamelliaEncryptDecryptDirect */
  15871. /*
  15872. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  15873. */
  15874. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  15875. {
  15876. EXPECT_DECLS;
  15877. #ifdef HAVE_CAMELLIA
  15878. Camellia camellia;
  15879. static const byte key24[] = {
  15880. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15881. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15882. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15883. };
  15884. static const byte plainT[] = {
  15885. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  15886. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  15887. };
  15888. byte enc[CAMELLIA_BLOCK_SIZE];
  15889. byte dec[CAMELLIA_BLOCK_SIZE];
  15890. /* Init stack variables. */
  15891. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  15892. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  15893. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  15894. NULL), 0);
  15895. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, plainT,
  15896. CAMELLIA_BLOCK_SIZE), 0);
  15897. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  15898. NULL), 0);
  15899. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE),
  15900. 0);
  15901. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  15902. /* Pass in bad args. */
  15903. ExpectIntEQ(wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE),
  15904. BAD_FUNC_ARG);
  15905. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  15906. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  15907. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  15908. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  15909. ExpectIntEQ(wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE),
  15910. BAD_FUNC_ARG);
  15911. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  15912. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  15913. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  15914. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  15915. #endif
  15916. return EXPECT_RESULT();
  15917. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  15918. /*
  15919. * Testing wc_Arc4SetKey()
  15920. */
  15921. static int test_wc_Arc4SetKey(void)
  15922. {
  15923. EXPECT_DECLS;
  15924. #ifndef NO_RC4
  15925. Arc4 arc;
  15926. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15927. int keyLen = 8;
  15928. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, keyLen), 0);
  15929. /* Test bad args. */
  15930. ExpectIntEQ(wc_Arc4SetKey(NULL, (byte*)key, keyLen), BAD_FUNC_ARG);
  15931. ExpectIntEQ(wc_Arc4SetKey(&arc, NULL , keyLen), BAD_FUNC_ARG);
  15932. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, 0 ), BAD_FUNC_ARG);
  15933. #endif
  15934. return EXPECT_RESULT();
  15935. } /* END test_wc_Arc4SetKey */
  15936. /*
  15937. * Testing wc_Arc4Process for ENC/DEC.
  15938. */
  15939. static int test_wc_Arc4Process(void)
  15940. {
  15941. EXPECT_DECLS;
  15942. #ifndef NO_RC4
  15943. Arc4 enc;
  15944. Arc4 dec;
  15945. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15946. int keyLen = 8;
  15947. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15948. byte cipher[8];
  15949. byte plain[8];
  15950. /* Init stack variables */
  15951. XMEMSET(&enc, 0, sizeof(Arc4));
  15952. XMEMSET(&dec, 0, sizeof(Arc4));
  15953. XMEMSET(cipher, 0, sizeof(cipher));
  15954. XMEMSET(plain, 0, sizeof(plain));
  15955. /* Use for async. */
  15956. ExpectIntEQ(wc_Arc4Init(&enc, NULL, INVALID_DEVID), 0);
  15957. ExpectIntEQ(wc_Arc4Init(&dec, NULL, INVALID_DEVID), 0);
  15958. ExpectIntEQ(wc_Arc4SetKey(&enc, (byte*)key, keyLen), 0);
  15959. ExpectIntEQ(wc_Arc4SetKey(&dec, (byte*)key, keyLen), 0);
  15960. ExpectIntEQ(wc_Arc4Process(&enc, cipher, (byte*)input, keyLen), 0);
  15961. ExpectIntEQ(wc_Arc4Process(&dec, plain, cipher, keyLen), 0);
  15962. ExpectIntEQ(XMEMCMP(plain, input, keyLen), 0);
  15963. /* Bad args. */
  15964. ExpectIntEQ(wc_Arc4Process(NULL, plain, cipher, keyLen), BAD_FUNC_ARG);
  15965. ExpectIntEQ(wc_Arc4Process(&dec, NULL, cipher, keyLen), BAD_FUNC_ARG);
  15966. ExpectIntEQ(wc_Arc4Process(&dec, plain, NULL, keyLen), BAD_FUNC_ARG);
  15967. wc_Arc4Free(&enc);
  15968. wc_Arc4Free(&dec);
  15969. #endif
  15970. return EXPECT_RESULT();
  15971. } /* END test_wc_Arc4Process */
  15972. /*
  15973. * Testing wc_Init RsaKey()
  15974. */
  15975. static int test_wc_InitRsaKey(void)
  15976. {
  15977. EXPECT_DECLS;
  15978. #ifndef NO_RSA
  15979. RsaKey key;
  15980. XMEMSET(&key, 0, sizeof(RsaKey));
  15981. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  15982. /* Test bad args. */
  15983. #ifndef HAVE_USER_RSA
  15984. ExpectIntEQ(wc_InitRsaKey(NULL, HEAP_HINT), BAD_FUNC_ARG);
  15985. #else
  15986. ExpectIntEQ(wc_InitRsaKey(NULL, HEAP_HINT), USER_CRYPTO_ERROR);
  15987. #endif
  15988. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  15989. #endif
  15990. return EXPECT_RESULT();
  15991. } /* END test_wc_InitRsaKey */
  15992. /*
  15993. * Testing wc_RsaPrivateKeyDecode()
  15994. */
  15995. static int test_wc_RsaPrivateKeyDecode(void)
  15996. {
  15997. EXPECT_DECLS;
  15998. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  15999. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16000. RsaKey key;
  16001. byte* tmp = NULL;
  16002. word32 idx = 0;
  16003. int bytes = 0;
  16004. XMEMSET(&key, 0, sizeof(RsaKey));
  16005. ExpectNotNull(tmp = (byte*)XMALLOC(FOURK_BUF, NULL,
  16006. DYNAMIC_TYPE_TMP_BUFFER));
  16007. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16008. if (tmp != NULL) {
  16009. #ifdef USE_CERT_BUFFERS_1024
  16010. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  16011. bytes = sizeof_client_key_der_1024;
  16012. #else
  16013. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  16014. bytes = sizeof_client_key_der_2048;
  16015. #endif /* Use cert buffers. */
  16016. }
  16017. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes), 0);
  16018. #ifndef HAVE_USER_RSA
  16019. /* Test bad args. */
  16020. ExpectIntEQ(wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes),
  16021. BAD_FUNC_ARG);
  16022. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes),
  16023. BAD_FUNC_ARG);
  16024. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes),
  16025. BAD_FUNC_ARG);
  16026. #else
  16027. /* Test bad args. User RSA. */
  16028. ExpectIntEQ(wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes),
  16029. USER_CRYPTO_ERROR);
  16030. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes),
  16031. USER_CRYPTO_ERROR);
  16032. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes),
  16033. USER_CRYPTO_ERROR);
  16034. #endif
  16035. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16036. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16037. #endif
  16038. return EXPECT_RESULT();
  16039. } /* END test_wc_RsaPrivateKeyDecode */
  16040. /*
  16041. * Testing wc_RsaPublicKeyDecode()
  16042. */
  16043. static int test_wc_RsaPublicKeyDecode(void)
  16044. {
  16045. EXPECT_DECLS;
  16046. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  16047. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16048. RsaKey keyPub;
  16049. byte* tmp = NULL;
  16050. word32 idx = 0;
  16051. int bytes = 0;
  16052. word32 keySz = 0;
  16053. word32 tstKeySz = 0;
  16054. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  16055. XFILE f = XBADFILE;
  16056. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  16057. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  16058. byte buf[4096];
  16059. #endif
  16060. XMEMSET(&keyPub, 0, sizeof(RsaKey));
  16061. ExpectNotNull(tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  16062. ExpectIntEQ(wc_InitRsaKey(&keyPub, HEAP_HINT), 0);
  16063. if (tmp != NULL) {
  16064. #ifdef USE_CERT_BUFFERS_1024
  16065. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  16066. bytes = sizeof_client_keypub_der_1024;
  16067. keySz = 1024;
  16068. #else
  16069. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  16070. bytes = sizeof_client_keypub_der_2048;
  16071. keySz = 2048;
  16072. #endif
  16073. }
  16074. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes), 0);
  16075. #ifndef HAVE_USER_RSA
  16076. /* Pass in bad args. */
  16077. ExpectIntEQ(wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes),
  16078. BAD_FUNC_ARG);
  16079. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes),
  16080. BAD_FUNC_ARG);
  16081. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes),
  16082. BAD_FUNC_ARG);
  16083. #else
  16084. /* Pass in bad args. */
  16085. ExpectIntEQ(wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes),
  16086. USER_CRYPTO_ERROR);
  16087. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes),
  16088. USER_CRYPTO_ERROR);
  16089. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes),
  16090. USER_CRYPTO_ERROR);
  16091. #endif
  16092. DoExpectIntEQ(wc_FreeRsaKey(&keyPub), 0);
  16093. /* Test for getting modulus key size */
  16094. idx = 0;
  16095. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  16096. &tstKeySz, NULL, NULL), 0);
  16097. ExpectIntEQ(tstKeySz, keySz/8);
  16098. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  16099. ExpectTrue((f = XFOPEN(rsaPssPubKey, "rb")) != XBADFILE);
  16100. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  16101. if (f != XBADFILE) {
  16102. XFCLOSE(f);
  16103. f = XBADFILE;
  16104. }
  16105. idx = 0;
  16106. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  16107. NULL), 0);
  16108. ExpectTrue((f = XFOPEN(rsaPssPubKeyNoParams, "rb")) != XBADFILE);
  16109. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  16110. if (f != XBADFILE)
  16111. XFCLOSE(f);
  16112. idx = 0;
  16113. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  16114. NULL), 0);
  16115. #endif
  16116. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16117. #endif
  16118. return EXPECT_RESULT();
  16119. } /* END test_wc_RsaPublicKeyDecode */
  16120. /*
  16121. * Testing wc_RsaPublicKeyDecodeRaw()
  16122. */
  16123. static int test_wc_RsaPublicKeyDecodeRaw(void)
  16124. {
  16125. EXPECT_DECLS;
  16126. #if !defined(NO_RSA)
  16127. RsaKey key;
  16128. const byte n = 0x23;
  16129. const byte e = 0x03;
  16130. int nSz = sizeof(n);
  16131. int eSz = sizeof(e);
  16132. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16133. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key), 0);
  16134. #ifndef HAVE_USER_RSA
  16135. /* Pass in bad args. */
  16136. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key),
  16137. BAD_FUNC_ARG);
  16138. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key),
  16139. BAD_FUNC_ARG);
  16140. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL),
  16141. BAD_FUNC_ARG);
  16142. #else
  16143. /* Pass in bad args. User RSA. */
  16144. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key),
  16145. USER_CRYPTO_ERROR);
  16146. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key),
  16147. USER_CRYPTO_ERROR);
  16148. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL),
  16149. USER_CRYPTO_ERROR);
  16150. #endif
  16151. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16152. #endif
  16153. return EXPECT_RESULT();
  16154. } /* END test_wc_RsaPublicKeyDecodeRaw */
  16155. #if (!defined(NO_RSA) || !defined(HAVE_FAST_RSA)) && defined(WOLFSSL_KEY_GEN)
  16156. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  16157. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  16158. * trying until it gets a probable prime. */
  16159. #ifdef HAVE_FIPS
  16160. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  16161. {
  16162. int ret;
  16163. for (;;) {
  16164. ret = wc_MakeRsaKey(key, size, e, rng);
  16165. if (ret != PRIME_GEN_E) break;
  16166. fprintf(stderr, "MakeRsaKey couldn't find prime; "
  16167. "trying again.\n");
  16168. }
  16169. return ret;
  16170. }
  16171. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  16172. #else
  16173. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  16174. #endif
  16175. #endif
  16176. /*
  16177. * Testing wc_MakeRsaKey()
  16178. */
  16179. static int test_wc_MakeRsaKey(void)
  16180. {
  16181. EXPECT_DECLS;
  16182. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16183. RsaKey genKey;
  16184. WC_RNG rng;
  16185. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16186. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16187. int bits = 1024;
  16188. #else
  16189. int bits = 2048;
  16190. #endif
  16191. XMEMSET(&genKey, 0, sizeof(RsaKey));
  16192. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16193. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  16194. ExpectIntEQ(wc_InitRng(&rng), 0);
  16195. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  16196. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  16197. #ifndef HAVE_USER_RSA
  16198. /* Test bad args. */
  16199. ExpectIntEQ(MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng), BAD_FUNC_ARG);
  16200. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL),
  16201. BAD_FUNC_ARG);
  16202. /* e < 3 */
  16203. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 2, &rng), BAD_FUNC_ARG);
  16204. /* e & 1 == 0 */
  16205. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 6, &rng), BAD_FUNC_ARG);
  16206. #else
  16207. /* Test bad args. */
  16208. ExpectIntEQ(MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng),
  16209. USER_CRYPTO_ERROR);
  16210. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL),
  16211. USER_CRYPTO_ERROR);
  16212. /* e < 3 */
  16213. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 2, &rng), USER_CRYPTO_ERROR);
  16214. /* e & 1 == 0 */
  16215. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 6, &rng), USER_CRYPTO_ERROR);
  16216. #endif /* HAVE_USER_RSA */
  16217. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16218. #endif
  16219. return EXPECT_RESULT();
  16220. } /* END test_wc_MakeRsaKey */
  16221. /*
  16222. * Test the bounds checking on the cipher text versus the key modulus.
  16223. * 1. Make a new RSA key.
  16224. * 2. Set c to 1.
  16225. * 3. Decrypt c into k. (error)
  16226. * 4. Copy the key modulus to c and sub 1 from the copy.
  16227. * 5. Decrypt c into k. (error)
  16228. * Valid bounds test cases are covered by all the other RSA tests.
  16229. */
  16230. static int test_RsaDecryptBoundsCheck(void)
  16231. {
  16232. EXPECT_DECLS;
  16233. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  16234. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  16235. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  16236. WC_RNG rng;
  16237. RsaKey key;
  16238. byte flatC[256];
  16239. word32 flatCSz;
  16240. byte out[256];
  16241. word32 outSz = sizeof(out);
  16242. XMEMSET(&key, 0, sizeof(RsaKey));
  16243. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16244. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16245. ExpectIntEQ(wc_InitRng(&rng), 0);
  16246. if (EXPECT_SUCCESS()) {
  16247. const byte* derKey;
  16248. word32 derKeySz;
  16249. word32 idx = 0;
  16250. #ifdef USE_CERT_BUFFERS_1024
  16251. derKey = server_key_der_1024;
  16252. derKeySz = (word32)sizeof_server_key_der_1024;
  16253. flatCSz = 128;
  16254. #else
  16255. derKey = server_key_der_2048;
  16256. derKeySz = (word32)sizeof_server_key_der_2048;
  16257. flatCSz = 256;
  16258. #endif
  16259. ExpectIntEQ(wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz), 0);
  16260. }
  16261. if (EXPECT_SUCCESS()) {
  16262. XMEMSET(flatC, 0, flatCSz);
  16263. flatC[flatCSz-1] = 1;
  16264. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16265. RSA_PRIVATE_DECRYPT, &rng), RSA_OUT_OF_RANGE_E);
  16266. if (EXPECT_SUCCESS()) {
  16267. mp_int c;
  16268. ExpectIntEQ(mp_init_copy(&c, &key.n), 0);
  16269. ExpectIntEQ(mp_sub_d(&c, 1, &c), 0);
  16270. ExpectIntEQ(mp_to_unsigned_bin(&c, flatC), 0);
  16271. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16272. RSA_PRIVATE_DECRYPT, NULL), RSA_OUT_OF_RANGE_E);
  16273. mp_clear(&c);
  16274. }
  16275. }
  16276. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16277. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16278. #endif
  16279. return EXPECT_RESULT();
  16280. } /* END test_wc_RsaDecryptBoundsCheck */
  16281. /*
  16282. * Testing wc_SetKeyUsage()
  16283. */
  16284. static int test_wc_SetKeyUsage(void)
  16285. {
  16286. EXPECT_DECLS;
  16287. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  16288. Cert myCert;
  16289. ExpectIntEQ(wc_InitCert(&myCert), 0);
  16290. ExpectIntEQ(wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement"), 0);
  16291. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation"), 0);
  16292. ExpectIntEQ(wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly"), 0);
  16293. ExpectIntEQ(wc_SetKeyUsage(&myCert, "decipherOnly"), 0);
  16294. ExpectIntEQ(wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign"), 0);
  16295. /* Test bad args. */
  16296. ExpectIntEQ(wc_SetKeyUsage(NULL, "decipherOnly"), BAD_FUNC_ARG);
  16297. ExpectIntEQ(wc_SetKeyUsage(&myCert, NULL), BAD_FUNC_ARG);
  16298. ExpectIntEQ(wc_SetKeyUsage(&myCert, ""), KEYUSAGE_E);
  16299. ExpectIntEQ(wc_SetKeyUsage(&myCert, ","), KEYUSAGE_E);
  16300. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign"),
  16301. KEYUSAGE_E);
  16302. #endif
  16303. return EXPECT_RESULT();
  16304. } /* END test_wc_SetKeyUsage */
  16305. /*
  16306. * Testing wc_CheckProbablePrime()
  16307. */
  16308. static int test_wc_CheckProbablePrime(void)
  16309. {
  16310. EXPECT_DECLS;
  16311. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16312. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  16313. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  16314. RsaKey key;
  16315. WC_RNG rng;
  16316. byte e[3];
  16317. word32 eSz = (word32)sizeof(e);
  16318. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16319. word32 nSz = (word32)sizeof(n);
  16320. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16321. word32 dSz = (word32)sizeof(d);
  16322. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16323. word32 pSz = (word32)sizeof(p);
  16324. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16325. word32 qSz = (word32)sizeof(q);
  16326. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  16327. int* isPrime;
  16328. int test[5];
  16329. isPrime = test;
  16330. XMEMSET(&key, 0, sizeof(RsaKey));
  16331. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16332. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16333. ExpectIntEQ(wc_InitRng(&rng), 0);
  16334. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16335. ExpectIntEQ(wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS,
  16336. WC_RSA_EXPONENT, &rng), 0);
  16337. PRIVATE_KEY_UNLOCK();
  16338. ExpectIntEQ(wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q,
  16339. &qSz), 0);
  16340. PRIVATE_KEY_LOCK();
  16341. /* Bad cases */
  16342. ExpectIntEQ(wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz, nlen, isPrime),
  16343. BAD_FUNC_ARG);
  16344. ExpectIntEQ(wc_CheckProbablePrime(p, 0, q, qSz, e, eSz, nlen, isPrime),
  16345. BAD_FUNC_ARG);
  16346. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz, nlen, isPrime),
  16347. BAD_FUNC_ARG);
  16348. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, 0, e, eSz, nlen, isPrime),
  16349. BAD_FUNC_ARG);
  16350. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz, nlen, isPrime),
  16351. BAD_FUNC_ARG);
  16352. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, 0, nlen, isPrime),
  16353. BAD_FUNC_ARG);
  16354. ExpectIntEQ(wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0, nlen, isPrime),
  16355. BAD_FUNC_ARG);
  16356. /* Good case */
  16357. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz, nlen, isPrime),
  16358. 0);
  16359. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16360. wc_FreeRng(&rng);
  16361. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  16362. #endif
  16363. return EXPECT_RESULT();
  16364. } /* END test_wc_CheckProbablePrime */
  16365. /*
  16366. * Testing wc_RsaPSS_Verify()
  16367. */
  16368. static int test_wc_RsaPSS_Verify(void)
  16369. {
  16370. EXPECT_DECLS;
  16371. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16372. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16373. RsaKey key;
  16374. WC_RNG rng;
  16375. int sz = 256;
  16376. const char* szMessage = "This is the string to be signed";
  16377. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16378. unsigned char pDecrypted[2048/8];
  16379. byte* pt = pDecrypted;
  16380. word32 outLen = sizeof(pDecrypted);
  16381. XMEMSET(&key, 0, sizeof(RsaKey));
  16382. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16383. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16384. ExpectIntEQ(wc_InitRng(&rng), 0);
  16385. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16386. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16387. ExpectIntGT(sz = wc_RsaPSS_Sign((byte*)szMessage,
  16388. (word32)XSTRLEN(szMessage)+1, pSignature, sizeof(pSignature),
  16389. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  16390. /* Bad cases */
  16391. ExpectIntEQ(wc_RsaPSS_Verify(NULL, sz, pt, outLen,
  16392. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16393. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  16394. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16395. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
  16396. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16397. ExpectIntEQ(wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  16398. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16399. /* Good case */
  16400. ExpectIntGT(wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
  16401. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  16402. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16403. wc_FreeRng(&rng);
  16404. #endif
  16405. return EXPECT_RESULT();
  16406. } /* END test_wc_RsaPSS_Verify */
  16407. /*
  16408. * Testing wc_RsaPSS_VerifyCheck()
  16409. */
  16410. static int test_wc_RsaPSS_VerifyCheck(void)
  16411. {
  16412. EXPECT_DECLS;
  16413. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16414. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16415. RsaKey key;
  16416. WC_RNG rng;
  16417. int sz = 256; /* 2048/8 */
  16418. byte digest[32];
  16419. word32 digestSz = sizeof(digest);
  16420. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16421. word32 pSignatureSz = sizeof(pSignature);
  16422. unsigned char pDecrypted[2048/8];
  16423. byte* pt = pDecrypted;
  16424. word32 outLen = sizeof(pDecrypted);
  16425. XMEMSET(&key, 0, sizeof(RsaKey));
  16426. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16427. XMEMSET(digest, 0, sizeof(digest));
  16428. XMEMSET(pSignature, 0, sizeof(pSignature));
  16429. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16430. ExpectIntEQ(wc_InitRng(&rng), 0);
  16431. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16432. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16433. ExpectTrue((digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  16434. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz),
  16435. 0);
  16436. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  16437. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  16438. /* Bad cases */
  16439. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen, digest,
  16440. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16441. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen, digest,
  16442. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16443. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen, digest,
  16444. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16445. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen, digest,
  16446. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16447. /* Good case */
  16448. ExpectIntGT(wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen, digest,
  16449. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  16450. ExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16451. wc_FreeRng(&rng);
  16452. #endif
  16453. return EXPECT_RESULT();
  16454. } /* END test_wc_RsaPSS_VerifyCheck */
  16455. /*
  16456. * Testing wc_RsaPSS_VerifyCheckInline()
  16457. */
  16458. static int test_wc_RsaPSS_VerifyCheckInline(void)
  16459. {
  16460. EXPECT_DECLS;
  16461. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16462. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16463. RsaKey key;
  16464. WC_RNG rng;
  16465. int sz = 256;
  16466. byte digest[32];
  16467. word32 digestSz = sizeof(digest);
  16468. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16469. unsigned char pDecrypted[2048/8];
  16470. byte* pt = pDecrypted;
  16471. XMEMSET(&key, 0, sizeof(RsaKey));
  16472. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16473. XMEMSET(digest, 0, sizeof(digest));
  16474. XMEMSET(pSignature, 0, sizeof(pSignature));
  16475. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16476. ExpectIntEQ(wc_InitRng(&rng), 0);
  16477. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16478. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16479. ExpectTrue((digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  16480. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz),
  16481. 0);
  16482. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature,
  16483. sizeof(pSignature), WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  16484. /* Bad Cases */
  16485. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt, digest,
  16486. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16487. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL, digest,
  16488. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16489. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt, digest,
  16490. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16491. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt, digest,
  16492. digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16493. /* Good case */
  16494. ExpectIntGT(wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt, digest,
  16495. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  16496. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16497. wc_FreeRng(&rng);
  16498. #endif
  16499. return EXPECT_RESULT();
  16500. } /* END test_wc_RsaPSS_VerifyCheckInline */
  16501. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16502. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  16503. {
  16504. (void)flag;
  16505. (void)type;
  16506. (void)file;
  16507. (void)line;
  16508. }
  16509. #endif
  16510. /*
  16511. * Testing wc_LockMutex_ex
  16512. */
  16513. static int test_wc_LockMutex_ex(void)
  16514. {
  16515. EXPECT_DECLS;
  16516. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16517. int flag = CRYPTO_LOCK;
  16518. int type = 0;
  16519. const char* file = "./test-LockMutex_ex.txt";
  16520. int line = 0;
  16521. /* without SetMutexCb */
  16522. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), BAD_STATE_E);
  16523. /* with SetMutexCb */
  16524. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  16525. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), 0);
  16526. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  16527. #endif
  16528. return EXPECT_RESULT();
  16529. } /* End test_wc_LockMutex_ex*/
  16530. /*
  16531. * Testing wc_SetMutexCb
  16532. */
  16533. static int test_wc_SetMutexCb(void)
  16534. {
  16535. EXPECT_DECLS;
  16536. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16537. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  16538. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  16539. #endif
  16540. return EXPECT_RESULT();
  16541. } /* End test_wc_SetMutexCb*/
  16542. /*
  16543. * Testing wc_RsaKeyToDer()
  16544. */
  16545. static int test_wc_RsaKeyToDer(void)
  16546. {
  16547. EXPECT_DECLS;
  16548. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16549. RsaKey genKey;
  16550. WC_RNG rng;
  16551. byte* der = NULL;
  16552. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16553. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16554. int bits = 1024;
  16555. word32 derSz = 611;
  16556. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  16557. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  16558. #else
  16559. int bits = 2048;
  16560. word32 derSz = 1196;
  16561. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  16562. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  16563. #endif
  16564. XMEMSET(&rng, 0, sizeof(rng));
  16565. XMEMSET(&genKey, 0, sizeof(genKey));
  16566. ExpectNotNull(der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  16567. /* Init structures. */
  16568. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  16569. ExpectIntEQ(wc_InitRng(&rng), 0);
  16570. /* Make key. */
  16571. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  16572. ExpectIntGT(wc_RsaKeyToDer(&genKey, der, derSz), 0);
  16573. #ifndef HAVE_USER_RSA
  16574. /* Pass good/bad args. */
  16575. ExpectIntEQ(wc_RsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  16576. /* Get just the output length */
  16577. ExpectIntGT(wc_RsaKeyToDer(&genKey, NULL, 0), 0);
  16578. /* Try Public Key. */
  16579. genKey.type = 0;
  16580. ExpectIntEQ(wc_RsaKeyToDer(&genKey, der, FOURK_BUF), BAD_FUNC_ARG);
  16581. #ifdef WOLFSSL_CHECK_MEM_ZERO
  16582. /* Put back to Private Key */
  16583. genKey.type = 1;
  16584. #endif
  16585. #else
  16586. /* Pass good/bad args. */
  16587. ExpectIntEQ(wc_RsaKeyToDer(NULL, der, FOURK_BUF), USER_CRYPTO_ERROR);
  16588. /* Get just the output length */
  16589. ExpectIntGT(wc_RsaKeyToDer(&genKey, NULL, 0), 0);
  16590. /* Try Public Key. */
  16591. genKey.type = 0;
  16592. ExpectIntEQ(wc_RsaKeyToDer(&genKey, der, FOURK_BUF), USER_CRYPTO_ERROR);
  16593. #ifdef WOLFSSL_CHECK_MEM_ZERO
  16594. /* Put back to Private Key */
  16595. genKey.type = 1;
  16596. #endif
  16597. #endif
  16598. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16599. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  16600. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16601. #endif
  16602. return EXPECT_RESULT();
  16603. } /* END test_wc_RsaKeyToDer */
  16604. /*
  16605. * Testing wc_RsaKeyToPublicDer()
  16606. */
  16607. static int test_wc_RsaKeyToPublicDer(void)
  16608. {
  16609. EXPECT_DECLS;
  16610. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16611. RsaKey key;
  16612. WC_RNG rng;
  16613. byte* der = NULL;
  16614. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16615. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16616. int bits = 1024;
  16617. word32 derLen = 162;
  16618. #else
  16619. int bits = 2048;
  16620. word32 derLen = 294;
  16621. #endif
  16622. #ifndef HAVE_USER_RSA
  16623. int ret;
  16624. #endif
  16625. XMEMSET(&rng, 0, sizeof(rng));
  16626. XMEMSET(&key, 0, sizeof(key));
  16627. ExpectNotNull(der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  16628. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16629. ExpectIntEQ(wc_InitRng(&rng), 0);
  16630. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16631. /* test getting size only */
  16632. ExpectIntGT(wc_RsaKeyToPublicDer(&key, NULL, derLen), 0);
  16633. ExpectIntGT(wc_RsaKeyToPublicDer(&key, der, derLen), 0);
  16634. /* test getting size only */
  16635. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0), 0);
  16636. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0), 0);
  16637. #ifndef HAVE_USER_RSA
  16638. /* Pass in bad args. */
  16639. ExpectIntEQ(wc_RsaKeyToPublicDer(NULL, der, derLen), BAD_FUNC_ARG);
  16640. ExpectIntLT(ret = wc_RsaKeyToPublicDer(&key, der, -1), 0);
  16641. ExpectTrue((ret == BUFFER_E) || (ret == BAD_FUNC_ARG));
  16642. #else
  16643. /* Pass in bad args. */
  16644. ExpectIntEQ(wc_RsaKeyToPublicDer(NULL, der, derLen), USER_CRYPTO_ERROR);
  16645. ExpectIntEQ(wc_RsaKeyToPublicDer(&key, der, -1), USER_CRYPTO_ERROR);
  16646. #endif
  16647. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16648. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16649. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16650. #endif
  16651. return EXPECT_RESULT();
  16652. } /* END test_wc_RsaKeyToPublicDer */
  16653. /*
  16654. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  16655. */
  16656. static int test_wc_RsaPublicEncryptDecrypt(void)
  16657. {
  16658. EXPECT_DECLS;
  16659. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16660. RsaKey key;
  16661. WC_RNG rng;
  16662. const char inStr[] = TEST_STRING;
  16663. const word32 plainLen = (word32)TEST_STRING_SZ;
  16664. const word32 inLen = (word32)TEST_STRING_SZ;
  16665. int bits = TEST_RSA_BITS;
  16666. const word32 cipherLen = TEST_RSA_BYTES;
  16667. word32 cipherLenResult = cipherLen;
  16668. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16669. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16670. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  16671. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16672. ExpectNotNull(in);
  16673. ExpectNotNull(plain);
  16674. ExpectNotNull(cipher);
  16675. #endif
  16676. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  16677. /* Initialize stack structures. */
  16678. XMEMSET(&key, 0, sizeof(RsaKey));
  16679. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16680. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16681. ExpectIntEQ(wc_InitRng(&rng), 0);
  16682. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16683. /* Encrypt. */
  16684. ExpectIntGT(cipherLenResult = wc_RsaPublicEncrypt(in, inLen, cipher,
  16685. cipherLen, &key, &rng), 0);
  16686. /* Pass bad args - tested in another testing function.*/
  16687. /* Decrypt */
  16688. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  16689. /* Bind rng */
  16690. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16691. #endif
  16692. ExpectIntGE(wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen,
  16693. &key), 0);
  16694. ExpectIntEQ(XMEMCMP(plain, inStr, plainLen), 0);
  16695. /* Pass bad args - tested in another testing function.*/
  16696. WC_FREE_VAR(in, NULL);
  16697. WC_FREE_VAR(plain, NULL);
  16698. WC_FREE_VAR(cipher, NULL);
  16699. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16700. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16701. #endif
  16702. return EXPECT_RESULT();
  16703. } /* END test_wc_RsaPublicEncryptDecrypt */
  16704. /*
  16705. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  16706. */
  16707. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  16708. {
  16709. EXPECT_DECLS;
  16710. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  16711. && !defined(WC_NO_RSA_OAEP) && !defined(HAVE_USER_RSA)\
  16712. && !defined(NO_SHA256)
  16713. RsaKey key;
  16714. WC_RNG rng;
  16715. const char inStr[] = TEST_STRING;
  16716. const word32 inLen = (word32)TEST_STRING_SZ;
  16717. const word32 plainSz = (word32)TEST_STRING_SZ;
  16718. byte* res = NULL;
  16719. int idx = 0;
  16720. int bits = TEST_RSA_BITS;
  16721. const word32 cipherSz = TEST_RSA_BYTES;
  16722. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16723. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16724. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  16725. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16726. ExpectNotNull(in);
  16727. ExpectNotNull(plain);
  16728. ExpectNotNull(cipher);
  16729. #endif
  16730. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  16731. /* Initialize stack structures. */
  16732. XMEMSET(&key, 0, sizeof(RsaKey));
  16733. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16734. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID), 0);
  16735. ExpectIntEQ(wc_InitRng(&rng), 0);
  16736. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16737. /* Encrypt */
  16738. ExpectIntGE(idx = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key,
  16739. &rng, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  16740. /* Pass bad args - tested in another testing function.*/
  16741. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16742. /* Decrypt */
  16743. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  16744. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16745. #endif
  16746. ExpectIntGE(wc_RsaPrivateDecrypt_ex(cipher, (word32)idx, plain, plainSz,
  16747. &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  16748. ExpectIntEQ(XMEMCMP(plain, inStr, plainSz), 0);
  16749. /* Pass bad args - tested in another testing function.*/
  16750. ExpectIntGE(wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx, &res, &key,
  16751. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  16752. ExpectIntEQ(XMEMCMP(inStr, res, plainSz), 0);
  16753. #endif
  16754. WC_FREE_VAR(in, NULL);
  16755. WC_FREE_VAR(plain, NULL);
  16756. WC_FREE_VAR(cipher, NULL);
  16757. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16758. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16759. #endif
  16760. return EXPECT_RESULT();
  16761. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  16762. /*
  16763. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  16764. */
  16765. static int test_wc_RsaSSL_SignVerify(void)
  16766. {
  16767. EXPECT_DECLS;
  16768. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16769. RsaKey key;
  16770. WC_RNG rng;
  16771. const char inStr[] = TEST_STRING;
  16772. const word32 plainSz = (word32)TEST_STRING_SZ;
  16773. const word32 inLen = (word32)TEST_STRING_SZ;
  16774. word32 idx = 0;
  16775. int bits = TEST_RSA_BITS;
  16776. const word32 outSz = TEST_RSA_BYTES;
  16777. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16778. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  16779. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16780. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16781. ExpectNotNull(in);
  16782. ExpectNotNull(out);
  16783. ExpectNotNull(plain);
  16784. #endif
  16785. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  16786. XMEMSET(&key, 0, sizeof(RsaKey));
  16787. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16788. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16789. ExpectIntEQ(wc_InitRng(&rng), 0);
  16790. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16791. /* Sign. */
  16792. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng), (int)outSz);
  16793. idx = (int)outSz;
  16794. #ifndef HAVE_USER_RSA
  16795. /* Test bad args. */
  16796. ExpectIntEQ(wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng),
  16797. BAD_FUNC_ARG);
  16798. ExpectIntEQ(wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng),
  16799. BAD_FUNC_ARG);
  16800. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng),
  16801. BAD_FUNC_ARG);
  16802. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng),
  16803. BAD_FUNC_ARG);
  16804. #else
  16805. /* Test bad args. */
  16806. ExpectIntEQ(wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng),
  16807. USER_CRYPTO_ERROR);
  16808. ExpectIntEQ(wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng),
  16809. USER_CRYPTO_ERROR);
  16810. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng),
  16811. USER_CRYPTO_ERROR);
  16812. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng),
  16813. USER_CRYPTO_ERROR);
  16814. #endif
  16815. /* Verify. */
  16816. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, &key), (int)inLen);
  16817. #ifndef HAVE_USER_RSA
  16818. /* Pass bad args. */
  16819. ExpectIntEQ(wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key),
  16820. BAD_FUNC_ARG);
  16821. ExpectIntEQ(wc_RsaSSL_Verify(out, 0, plain, plainSz, &key),
  16822. BAD_FUNC_ARG);
  16823. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key),
  16824. BAD_FUNC_ARG);
  16825. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL),
  16826. BAD_FUNC_ARG);
  16827. #else
  16828. /* Pass bad args. */
  16829. ExpectIntEQ(wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key),
  16830. USER_CRYPTO_ERROR);
  16831. ExpectIntEQ(wc_RsaSSL_Verify(out, 0, plain, plainSz, &key),
  16832. USER_CRYPTO_ERROR);
  16833. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key),
  16834. USER_CRYPTO_ERROR);
  16835. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL),
  16836. USER_CRYPTO_ERROR);
  16837. #endif
  16838. WC_FREE_VAR(in, NULL);
  16839. WC_FREE_VAR(out, NULL);
  16840. WC_FREE_VAR(plain, NULL);
  16841. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16842. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16843. #endif
  16844. return EXPECT_RESULT();
  16845. } /* END test_wc_RsaSSL_SignVerify */
  16846. /*
  16847. * Testing wc_RsaEncryptSize()
  16848. */
  16849. static int test_wc_RsaEncryptSize(void)
  16850. {
  16851. EXPECT_DECLS;
  16852. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16853. RsaKey key;
  16854. WC_RNG rng;
  16855. XMEMSET(&key, 0, sizeof(RsaKey));
  16856. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16857. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16858. ExpectIntEQ(wc_InitRng(&rng), 0);
  16859. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16860. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16861. ExpectIntEQ(MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng), 0);
  16862. ExpectIntEQ(wc_RsaEncryptSize(&key), 128);
  16863. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16864. #endif
  16865. ExpectIntEQ(MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16866. ExpectIntEQ(wc_RsaEncryptSize(&key), 256);
  16867. /* Pass in bad arg. */
  16868. #ifndef HAVE_USER_RSA
  16869. ExpectIntEQ(wc_RsaEncryptSize(NULL), BAD_FUNC_ARG);
  16870. #else
  16871. ExpectIntEQ(wc_RsaEncryptSize(NULL), 0);
  16872. #endif
  16873. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16874. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16875. #endif
  16876. return EXPECT_RESULT();
  16877. } /* END test_wc_RsaEncryptSize*/
  16878. /*
  16879. * Testing wc_RsaFlattenPublicKey()
  16880. */
  16881. static int test_wc_RsaFlattenPublicKey(void)
  16882. {
  16883. EXPECT_DECLS;
  16884. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16885. RsaKey key;
  16886. WC_RNG rng;
  16887. byte e[256];
  16888. byte n[256];
  16889. word32 eSz = sizeof(e);
  16890. word32 nSz = sizeof(n);
  16891. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16892. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16893. int bits = 1024;
  16894. #else
  16895. int bits = 2048;
  16896. #endif
  16897. XMEMSET(&key, 0, sizeof(RsaKey));
  16898. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16899. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16900. ExpectIntEQ(wc_InitRng(&rng), 0);
  16901. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16902. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz), 0);
  16903. #ifndef HAVE_USER_RSA
  16904. /* Pass bad args. */
  16905. ExpectIntEQ(wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz),
  16906. BAD_FUNC_ARG);
  16907. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz),
  16908. BAD_FUNC_ARG);
  16909. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz),
  16910. BAD_FUNC_ARG);
  16911. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz),
  16912. BAD_FUNC_ARG);
  16913. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL),
  16914. BAD_FUNC_ARG);
  16915. #else
  16916. /* Pass bad args. */
  16917. ExpectIntEQ(wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz),
  16918. USER_CRYPTO_ERROR);
  16919. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz),
  16920. USER_CRYPTO_ERROR);
  16921. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz),
  16922. USER_CRYPTO_ERROR);
  16923. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz),
  16924. USER_CRYPTO_ERROR);
  16925. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL),
  16926. USER_CRYPTO_ERROR);
  16927. #endif
  16928. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16929. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16930. #endif
  16931. return EXPECT_RESULT();
  16932. } /* END test_wc_RsaFlattenPublicKey */
  16933. /*
  16934. * unit test for wc_AesCcmSetKey
  16935. */
  16936. static int test_wc_AesCcmSetKey(void)
  16937. {
  16938. EXPECT_DECLS;
  16939. #ifdef HAVE_AESCCM
  16940. Aes aes;
  16941. const byte key16[] = {
  16942. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  16943. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  16944. };
  16945. const byte key24[] = {
  16946. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16947. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16948. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16949. };
  16950. const byte key32[] = {
  16951. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16952. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16953. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16954. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16955. };
  16956. XMEMSET(&aes, 0, sizeof(Aes));
  16957. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16958. #ifdef WOLFSSL_AES_128
  16959. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  16960. #endif
  16961. #ifdef WOLFSSL_AES_192
  16962. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24)), 0);
  16963. #endif
  16964. #ifdef WOLFSSL_AES_256
  16965. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32)), 0);
  16966. #endif
  16967. /* Test bad args. */
  16968. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1), BAD_FUNC_ARG);
  16969. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1), BAD_FUNC_ARG);
  16970. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1), BAD_FUNC_ARG);
  16971. wc_AesFree(&aes);
  16972. #endif
  16973. return EXPECT_RESULT();
  16974. } /* END test_wc_AesCcmSetKey */
  16975. /*
  16976. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  16977. */
  16978. static int test_wc_AesCcmEncryptDecrypt(void)
  16979. {
  16980. EXPECT_DECLS;
  16981. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  16982. Aes aes;
  16983. const byte key16[] = {
  16984. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  16985. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  16986. };
  16987. /* plaintext */
  16988. const byte plainT[] = {
  16989. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  16990. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  16991. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  16992. };
  16993. /* nonce */
  16994. const byte iv[] = {
  16995. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  16996. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  16997. };
  16998. const byte c[] = { /* cipher text. */
  16999. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  17000. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  17001. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  17002. };
  17003. const byte t[] = { /* Auth tag */
  17004. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  17005. };
  17006. const byte authIn[] = {
  17007. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  17008. };
  17009. byte cipherOut[sizeof(plainT)];
  17010. byte authTag[sizeof(t)];
  17011. #ifdef HAVE_AES_DECRYPT
  17012. byte plainOut[sizeof(cipherOut)];
  17013. #endif
  17014. XMEMSET(&aes, 0, sizeof(Aes));
  17015. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  17016. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  17017. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17018. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)), 0);
  17019. ExpectIntEQ(XMEMCMP(cipherOut, c, sizeof(c)), 0);
  17020. ExpectIntEQ(XMEMCMP(t, authTag, sizeof(t)), 0);
  17021. #ifdef HAVE_AES_DECRYPT
  17022. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17023. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)), 0);
  17024. ExpectIntEQ(XMEMCMP(plainOut, plainT, sizeof(plainT)), 0);
  17025. #endif
  17026. /* Pass in bad args. Encrypt*/
  17027. ExpectIntEQ(wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  17028. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17029. BAD_FUNC_ARG);
  17030. ExpectIntEQ(wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  17031. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17032. BAD_FUNC_ARG);
  17033. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  17034. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17035. BAD_FUNC_ARG);
  17036. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17037. NULL, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17038. BAD_FUNC_ARG);
  17039. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17040. iv, sizeof(iv), NULL, sizeof(authTag), authIn , sizeof(authIn)),
  17041. BAD_FUNC_ARG);
  17042. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17043. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17044. BAD_FUNC_ARG);
  17045. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17046. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17047. BAD_FUNC_ARG);
  17048. #ifdef HAVE_AES_DECRYPT
  17049. /* Pass in bad args. Decrypt*/
  17050. ExpectIntEQ(wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  17051. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17052. BAD_FUNC_ARG);
  17053. ExpectIntEQ(wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  17054. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17055. BAD_FUNC_ARG);
  17056. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  17057. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17058. BAD_FUNC_ARG);
  17059. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17060. NULL, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17061. BAD_FUNC_ARG);
  17062. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17063. iv, sizeof(iv), NULL, sizeof(authTag), authIn, sizeof(authIn)),
  17064. BAD_FUNC_ARG);
  17065. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17066. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17067. BAD_FUNC_ARG);
  17068. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17069. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17070. BAD_FUNC_ARG);
  17071. #endif
  17072. wc_AesFree(&aes);
  17073. #endif /* HAVE_AESCCM */
  17074. return EXPECT_RESULT();
  17075. } /* END test_wc_AesCcmEncryptDecrypt */
  17076. #if defined(WOLFSSL_AES_EAX) && \
  17077. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  17078. /*
  17079. * Testing test_wc_AesEaxVectors()
  17080. */
  17081. static int test_wc_AesEaxVectors(void)
  17082. {
  17083. EXPECT_DECLS;
  17084. typedef struct {
  17085. byte key[AES_256_KEY_SIZE];
  17086. int key_length;
  17087. byte iv[AES_BLOCK_SIZE];
  17088. int iv_length;
  17089. byte aad[AES_BLOCK_SIZE * 2];
  17090. int aad_length;
  17091. byte msg[AES_BLOCK_SIZE * 5];
  17092. int msg_length;
  17093. byte ct[AES_BLOCK_SIZE * 5];
  17094. int ct_length;
  17095. byte tag[AES_BLOCK_SIZE];
  17096. int tag_length;
  17097. int valid;
  17098. } AadVector;
  17099. /* Test vectors obtained from Google wycheproof project
  17100. * https://github.com/google/wycheproof
  17101. * from testvectors/aes_eax_test.json
  17102. */
  17103. const AadVector vectors[] = {
  17104. {
  17105. /* key, key length */
  17106. {0x23, 0x39, 0x52, 0xde, 0xe4, 0xd5, 0xed, 0x5f,
  17107. 0x9b, 0x9c, 0x6d, 0x6f, 0xf8, 0x0f, 0xf4, 0x78}, 16,
  17108. /* iv, iv length */
  17109. {0x62, 0xec, 0x67, 0xf9, 0xc3, 0xa4, 0xa4, 0x07,
  17110. 0xfc, 0xb2, 0xa8, 0xc4, 0x90, 0x31, 0xa8, 0xb3}, 16,
  17111. /* aad, aad length */
  17112. {0x6b, 0xfb, 0x91, 0x4f, 0xd0, 0x7e, 0xae, 0x6b}, 8,
  17113. /* msg, msg length */
  17114. {0x00}, 0,
  17115. /* ct, ct length */
  17116. {0x00}, 0,
  17117. /* tag, tag length */
  17118. {0xe0, 0x37, 0x83, 0x0e, 0x83, 0x89, 0xf2, 0x7b,
  17119. 0x02, 0x5a, 0x2d, 0x65, 0x27, 0xe7, 0x9d, 0x01}, 16,
  17120. /* valid */
  17121. 1,
  17122. },
  17123. {
  17124. /* key, key length */
  17125. {0x91, 0x94, 0x5d, 0x3f, 0x4d, 0xcb, 0xee, 0x0b,
  17126. 0xf4, 0x5e, 0xf5, 0x22, 0x55, 0xf0, 0x95, 0xa4}, 16,
  17127. /* iv, iv length */
  17128. {0xbe, 0xca, 0xf0, 0x43, 0xb0, 0xa2, 0x3d, 0x84,
  17129. 0x31, 0x94, 0xba, 0x97, 0x2c, 0x66, 0xde, 0xbd}, 16,
  17130. /* aad, aad length */
  17131. {0xfa, 0x3b, 0xfd, 0x48, 0x06, 0xeb, 0x53, 0xfa}, 8,
  17132. /* msg, msg length */
  17133. {0xf7, 0xfb}, 2,
  17134. /* ct, ct length */
  17135. {0x19, 0xdd}, 2,
  17136. /* tag, tag length */
  17137. {0x5c, 0x4c, 0x93, 0x31, 0x04, 0x9d, 0x0b, 0xda,
  17138. 0xb0, 0x27, 0x74, 0x08, 0xf6, 0x79, 0x67, 0xe5}, 16,
  17139. /* valid */
  17140. 1,
  17141. },
  17142. {
  17143. /* key, key length */
  17144. {0x01, 0xf7, 0x4a, 0xd6, 0x40, 0x77, 0xf2, 0xe7,
  17145. 0x04, 0xc0, 0xf6, 0x0a, 0xda, 0x3d, 0xd5, 0x23}, 16,
  17146. /* iv, iv length */
  17147. {0x70, 0xc3, 0xdb, 0x4f, 0x0d, 0x26, 0x36, 0x84,
  17148. 0x00, 0xa1, 0x0e, 0xd0, 0x5d, 0x2b, 0xff, 0x5e}, 16,
  17149. /* aad, aad length */
  17150. {0x23, 0x4a, 0x34, 0x63, 0xc1, 0x26, 0x4a, 0xc6}, 8,
  17151. /* msg, msg length */
  17152. {0x1a, 0x47, 0xcb, 0x49, 0x33}, 5,
  17153. /* ct, ct length */
  17154. {0xd8, 0x51, 0xd5, 0xba, 0xe0}, 5,
  17155. /* tag, tag length */
  17156. {0x3a, 0x59, 0xf2, 0x38, 0xa2, 0x3e, 0x39, 0x19,
  17157. 0x9d, 0xc9, 0x26, 0x66, 0x26, 0xc4, 0x0f, 0x80}, 16,
  17158. /* valid */
  17159. 1,
  17160. },
  17161. {
  17162. /* key, key length */
  17163. {0xd0, 0x7c, 0xf6, 0xcb, 0xb7, 0xf3, 0x13, 0xbd,
  17164. 0xde, 0x66, 0xb7, 0x27, 0xaf, 0xd3, 0xc5, 0xe8}, 16,
  17165. /* iv, iv length */
  17166. {0x84, 0x08, 0xdf, 0xff, 0x3c, 0x1a, 0x2b, 0x12,
  17167. 0x92, 0xdc, 0x19, 0x9e, 0x46, 0xb7, 0xd6, 0x17}, 16,
  17168. /* aad, aad length */
  17169. {0x33, 0xcc, 0xe2, 0xea, 0xbf, 0xf5, 0xa7, 0x9d}, 8,
  17170. /* msg, msg length */
  17171. {0x48, 0x1c, 0x9e, 0x39, 0xb1}, 5,
  17172. /* ct, ct length */
  17173. {0x63, 0x2a, 0x9d, 0x13, 0x1a}, 5,
  17174. /* tag, tag length */
  17175. {0xd4, 0xc1, 0x68, 0xa4, 0x22, 0x5d, 0x8e, 0x1f,
  17176. 0xf7, 0x55, 0x93, 0x99, 0x74, 0xa7, 0xbe, 0xde}, 16,
  17177. /* valid */
  17178. 1,
  17179. },
  17180. {
  17181. /* key, key length */
  17182. {0x35, 0xb6, 0xd0, 0x58, 0x00, 0x05, 0xbb, 0xc1,
  17183. 0x2b, 0x05, 0x87, 0x12, 0x45, 0x57, 0xd2, 0xc2}, 16,
  17184. /* iv, iv length */
  17185. {0xfd, 0xb6, 0xb0, 0x66, 0x76, 0xee, 0xdc, 0x5c,
  17186. 0x61, 0xd7, 0x42, 0x76, 0xe1, 0xf8, 0xe8, 0x16}, 16,
  17187. /* aad, aad length */
  17188. {0xae, 0xb9, 0x6e, 0xae, 0xbe, 0x29, 0x70, 0xe9}, 8,
  17189. /* msg, msg length */
  17190. {0x40, 0xd0, 0xc0, 0x7d, 0xa5, 0xe4}, 6,
  17191. /* ct, ct length */
  17192. {0x07, 0x1d, 0xfe, 0x16, 0xc6, 0x75}, 6,
  17193. /* tag, tag length */
  17194. {0xcb, 0x06, 0x77, 0xe5, 0x36, 0xf7, 0x3a, 0xfe,
  17195. 0x6a, 0x14, 0xb7, 0x4e, 0xe4, 0x98, 0x44, 0xdd}, 16,
  17196. /* valid */
  17197. 1,
  17198. },
  17199. {
  17200. /* key, key length */
  17201. {0xbd, 0x8e, 0x6e, 0x11, 0x47, 0x5e, 0x60, 0xb2,
  17202. 0x68, 0x78, 0x4c, 0x38, 0xc6, 0x2f, 0xeb, 0x22}, 16,
  17203. /* iv, iv length */
  17204. {0x6e, 0xac, 0x5c, 0x93, 0x07, 0x2d, 0x8e, 0x85,
  17205. 0x13, 0xf7, 0x50, 0x93, 0x5e, 0x46, 0xda, 0x1b}, 16,
  17206. /* aad, aad length */
  17207. {0xd4, 0x48, 0x2d, 0x1c, 0xa7, 0x8d, 0xce, 0x0f}, 8,
  17208. /* msg, msg length */
  17209. {0x4d, 0xe3, 0xb3, 0x5c, 0x3f, 0xc0, 0x39, 0x24,
  17210. 0x5b, 0xd1, 0xfb, 0x7d}, 12,
  17211. /* ct, ct length */
  17212. {0x83, 0x5b, 0xb4, 0xf1, 0x5d, 0x74, 0x3e, 0x35,
  17213. 0x0e, 0x72, 0x84, 0x14}, 12,
  17214. /* tag, tag length */
  17215. {0xab, 0xb8, 0x64, 0x4f, 0xd6, 0xcc, 0xb8, 0x69,
  17216. 0x47, 0xc5, 0xe1, 0x05, 0x90, 0x21, 0x0a, 0x4f}, 16,
  17217. /* valid */
  17218. 1,
  17219. },
  17220. {
  17221. /* key, key length */
  17222. {0x7c, 0x77, 0xd6, 0xe8, 0x13, 0xbe, 0xd5, 0xac,
  17223. 0x98, 0xba, 0xa4, 0x17, 0x47, 0x7a, 0x2e, 0x7d}, 16,
  17224. /* iv, iv length */
  17225. {0x1a, 0x8c, 0x98, 0xdc, 0xd7, 0x3d, 0x38, 0x39,
  17226. 0x3b, 0x2b, 0xf1, 0x56, 0x9d, 0xee, 0xfc, 0x19}, 16,
  17227. /* aad, aad length */
  17228. {0x65, 0xd2, 0x01, 0x79, 0x90, 0xd6, 0x25, 0x28}, 8,
  17229. /* msg, msg length */
  17230. {0x8b, 0x0a, 0x79, 0x30, 0x6c, 0x9c, 0xe7, 0xed,
  17231. 0x99, 0xda, 0xe4, 0xf8, 0x7f, 0x8d, 0xd6, 0x16,
  17232. 0x36}, 17,
  17233. /* ct, ct length */
  17234. {0x02, 0x08, 0x3e, 0x39, 0x79, 0xda, 0x01, 0x48,
  17235. 0x12, 0xf5, 0x9f, 0x11, 0xd5, 0x26, 0x30, 0xda,
  17236. 0x30}, 17,
  17237. /* tag, tag length */
  17238. {0x13, 0x73, 0x27, 0xd1, 0x06, 0x49, 0xb0, 0xaa,
  17239. 0x6e, 0x1c, 0x18, 0x1d, 0xb6, 0x17, 0xd7, 0xf2}, 16,
  17240. /* valid */
  17241. 1,
  17242. },
  17243. {
  17244. /* key, key length */
  17245. {0x5f, 0xff, 0x20, 0xca, 0xfa, 0xb1, 0x19, 0xca,
  17246. 0x2f, 0xc7, 0x35, 0x49, 0xe2, 0x0f, 0x5b, 0x0d}, 16,
  17247. /* iv, iv length */
  17248. {0xdd, 0xe5, 0x9b, 0x97, 0xd7, 0x22, 0x15, 0x6d,
  17249. 0x4d, 0x9a, 0xff, 0x2b, 0xc7, 0x55, 0x98, 0x26}, 16,
  17250. /* aad, aad length */
  17251. {0x54, 0xb9, 0xf0, 0x4e, 0x6a, 0x09, 0x18, 0x9a}, 8,
  17252. /* msg, msg length */
  17253. {0x1b, 0xda, 0x12, 0x2b, 0xce, 0x8a, 0x8d, 0xba,
  17254. 0xf1, 0x87, 0x7d, 0x96, 0x2b, 0x85, 0x92, 0xdd,
  17255. 0x2d, 0x56}, 18,
  17256. /* ct, ct length */
  17257. {0x2e, 0xc4, 0x7b, 0x2c, 0x49, 0x54, 0xa4, 0x89,
  17258. 0xaf, 0xc7, 0xba, 0x48, 0x97, 0xed, 0xcd, 0xae,
  17259. 0x8c, 0xc3}, 18,
  17260. /* tag, tag length */
  17261. {0x3b, 0x60, 0x45, 0x05, 0x99, 0xbd, 0x02, 0xc9,
  17262. 0x63, 0x82, 0x90, 0x2a, 0xef, 0x7f, 0x83, 0x2a}, 16,
  17263. /* valid */
  17264. 1,
  17265. },
  17266. {
  17267. /* key, key length */
  17268. {0xa4, 0xa4, 0x78, 0x2b, 0xcf, 0xfd, 0x3e, 0xc5,
  17269. 0xe7, 0xef, 0x6d, 0x8c, 0x34, 0xa5, 0x61, 0x23}, 16,
  17270. /* iv, iv length */
  17271. {0xb7, 0x81, 0xfc, 0xf2, 0xf7, 0x5f, 0xa5, 0xa8,
  17272. 0xde, 0x97, 0xa9, 0xca, 0x48, 0xe5, 0x22, 0xec}, 16,
  17273. /* aad, aad length */
  17274. {0x89, 0x9a, 0x17, 0x58, 0x97, 0x56, 0x1d, 0x7e}, 8,
  17275. /* msg, msg length */
  17276. {0x6c, 0xf3, 0x67, 0x20, 0x87, 0x2b, 0x85, 0x13,
  17277. 0xf6, 0xea, 0xb1, 0xa8, 0xa4, 0x44, 0x38, 0xd5,
  17278. 0xef, 0x11}, 18,
  17279. /* ct, ct length */
  17280. {0x0d, 0xe1, 0x8f, 0xd0, 0xfd, 0xd9, 0x1e, 0x7a,
  17281. 0xf1, 0x9f, 0x1d, 0x8e, 0xe8, 0x73, 0x39, 0x38,
  17282. 0xb1, 0xe8}, 18,
  17283. /* tag, tag length */
  17284. {0xe7, 0xf6, 0xd2, 0x23, 0x16, 0x18, 0x10, 0x2f,
  17285. 0xdb, 0x7f, 0xe5, 0x5f, 0xf1, 0x99, 0x17, 0x00}, 16,
  17286. /* valid */
  17287. 1,
  17288. },
  17289. {
  17290. /* key, key length */
  17291. {0x83, 0x95, 0xfc, 0xf1, 0xe9, 0x5b, 0xeb, 0xd6,
  17292. 0x97, 0xbd, 0x01, 0x0b, 0xc7, 0x66, 0xaa, 0xc3}, 16,
  17293. /* iv, iv length */
  17294. {0x22, 0xe7, 0xad, 0xd9, 0x3c, 0xfc, 0x63, 0x93,
  17295. 0xc5, 0x7e, 0xc0, 0xb3, 0xc1, 0x7d, 0x6b, 0x44}, 16,
  17296. /* aad, aad length */
  17297. {0x12, 0x67, 0x35, 0xfc, 0xc3, 0x20, 0xd2, 0x5a}, 8,
  17298. /* msg, msg length */
  17299. {0xca, 0x40, 0xd7, 0x44, 0x6e, 0x54, 0x5f, 0xfa,
  17300. 0xed, 0x3b, 0xd1, 0x2a, 0x74, 0x0a, 0x65, 0x9f,
  17301. 0xfb, 0xbb, 0x3c, 0xea, 0xb7}, 21,
  17302. /* ct, ct length */
  17303. {0xcb, 0x89, 0x20, 0xf8, 0x7a, 0x6c, 0x75, 0xcf,
  17304. 0xf3, 0x96, 0x27, 0xb5, 0x6e, 0x3e, 0xd1, 0x97,
  17305. 0xc5, 0x52, 0xd2, 0x95, 0xa7}, 21,
  17306. /* tag, tag length */
  17307. {0xcf, 0xc4, 0x6a, 0xfc, 0x25, 0x3b, 0x46, 0x52,
  17308. 0xb1, 0xaf, 0x37, 0x95, 0xb1, 0x24, 0xab, 0x6e}, 16,
  17309. /* valid */
  17310. 1,
  17311. },
  17312. {
  17313. /* key, key length */
  17314. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17315. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17316. /* iv, iv length */
  17317. {0x3c, 0x8c, 0xc2, 0x97, 0x0a, 0x00, 0x8f, 0x75,
  17318. 0xcc, 0x5b, 0xea, 0xe2, 0x84, 0x72, 0x58, 0xc2}, 16,
  17319. /* aad, aad length */
  17320. {0x00}, 0,
  17321. /* msg, msg length */
  17322. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17323. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17324. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17325. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17326. /* ct, ct length */
  17327. {0x3c, 0x44, 0x1f, 0x32, 0xce, 0x07, 0x82, 0x23,
  17328. 0x64, 0xd7, 0xa2, 0x99, 0x0e, 0x50, 0xbb, 0x13,
  17329. 0xd7, 0xb0, 0x2a, 0x26, 0x96, 0x9e, 0x4a, 0x93,
  17330. 0x7e, 0x5e, 0x90, 0x73, 0xb0, 0xd9, 0xc9, 0x68}, 32,
  17331. /* tag, tag length */
  17332. {0xdb, 0x90, 0xbd, 0xb3, 0xda, 0x3d, 0x00, 0xaf,
  17333. 0xd0, 0xfc, 0x6a, 0x83, 0x55, 0x1d, 0xa9, 0x5e}, 16,
  17334. /* valid */
  17335. 1,
  17336. },
  17337. {
  17338. /* key, key length */
  17339. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17340. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17341. /* iv, iv length */
  17342. {0xae, 0xf0, 0x3d, 0x00, 0x59, 0x84, 0x94, 0xe9,
  17343. 0xfb, 0x03, 0xcd, 0x7d, 0x8b, 0x59, 0x08, 0x66}, 16,
  17344. /* aad, aad length */
  17345. {0x00}, 0,
  17346. /* msg, msg length */
  17347. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17348. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17349. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17350. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17351. /* ct, ct length */
  17352. {0xd1, 0x9a, 0xc5, 0x98, 0x49, 0x02, 0x6a, 0x91,
  17353. 0xaa, 0x1b, 0x9a, 0xec, 0x29, 0xb1, 0x1a, 0x20,
  17354. 0x2a, 0x4d, 0x73, 0x9f, 0xd8, 0x6c, 0x28, 0xe3,
  17355. 0xae, 0x3d, 0x58, 0x8e, 0xa2, 0x1d, 0x70, 0xc6}, 32,
  17356. /* tag, tag length */
  17357. {0xc3, 0x0f, 0x6c, 0xd9, 0x20, 0x20, 0x74, 0xed,
  17358. 0x6e, 0x2a, 0x2a, 0x36, 0x0e, 0xac, 0x8c, 0x47}, 16,
  17359. /* valid */
  17360. 1,
  17361. },
  17362. {
  17363. /* key, key length */
  17364. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17365. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17366. /* iv, iv length */
  17367. {0x55, 0xd1, 0x25, 0x11, 0xc6, 0x96, 0xa8, 0x0d,
  17368. 0x05, 0x14, 0xd1, 0xff, 0xba, 0x49, 0xca, 0xda}, 16,
  17369. /* aad, aad length */
  17370. {0x00}, 0,
  17371. /* msg, msg length */
  17372. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17373. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17374. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17375. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17376. /* ct, ct length */
  17377. {0x21, 0x08, 0x55, 0x8a, 0xc4, 0xb2, 0xc2, 0xd5,
  17378. 0xcc, 0x66, 0xce, 0xa5, 0x1d, 0x62, 0x10, 0xe0,
  17379. 0x46, 0x17, 0x7a, 0x67, 0x63, 0x1c, 0xd2, 0xdd,
  17380. 0x8f, 0x09, 0x46, 0x97, 0x33, 0xac, 0xb5, 0x17}, 32,
  17381. /* tag, tag length */
  17382. {0xfc, 0x35, 0x5e, 0x87, 0xa2, 0x67, 0xbe, 0x3a,
  17383. 0xe3, 0xe4, 0x4c, 0x0b, 0xf3, 0xf9, 0x9b, 0x2b}, 16,
  17384. /* valid */
  17385. 1,
  17386. },
  17387. {
  17388. /* key, key length */
  17389. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17390. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17391. /* iv, iv length */
  17392. {0x79, 0x42, 0x2d, 0xdd, 0x91, 0xc4, 0xee, 0xe2,
  17393. 0xde, 0xae, 0xf1, 0xf9, 0x68, 0x30, 0x53, 0x04}, 16,
  17394. /* aad, aad length */
  17395. {0x00}, 0,
  17396. /* msg, msg length */
  17397. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17398. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17399. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17400. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17401. /* ct, ct length */
  17402. {0x4d, 0x2c, 0x15, 0x24, 0xca, 0x4b, 0xaa, 0x4e,
  17403. 0xef, 0xcc, 0xe6, 0xb9, 0x1b, 0x22, 0x7e, 0xe8,
  17404. 0x3a, 0xba, 0xff, 0x81, 0x05, 0xdc, 0xaf, 0xa2,
  17405. 0xab, 0x19, 0x1f, 0x5d, 0xf2, 0x57, 0x50, 0x35}, 32,
  17406. /* tag, tag length */
  17407. {0xe2, 0xc8, 0x65, 0xce, 0x2d, 0x7a, 0xbd, 0xac,
  17408. 0x02, 0x4c, 0x6f, 0x99, 0x1a, 0x84, 0x83, 0x90}, 16,
  17409. /* valid */
  17410. 1,
  17411. },
  17412. {
  17413. /* key, key length */
  17414. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17415. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17416. /* iv, iv length */
  17417. {0x0a, 0xf5, 0xaa, 0x7a, 0x76, 0x76, 0xe2, 0x83,
  17418. 0x06, 0x30, 0x6b, 0xcd, 0x9b, 0xf2, 0x00, 0x3a}, 16,
  17419. /* aad, aad length */
  17420. {0x00}, 0,
  17421. /* msg, msg length */
  17422. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17423. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17424. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17425. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17426. /* ct, ct length */
  17427. {0x8e, 0xb0, 0x1e, 0x62, 0x18, 0x5d, 0x78, 0x2e,
  17428. 0xb9, 0x28, 0x7a, 0x34, 0x1a, 0x68, 0x62, 0xac,
  17429. 0x52, 0x57, 0xd6, 0xf9, 0xad, 0xc9, 0x9e, 0xe0,
  17430. 0xa2, 0x4d, 0x9c, 0x22, 0xb3, 0xe9, 0xb3, 0x8a}, 32,
  17431. /* tag, tag length */
  17432. {0x39, 0xc3, 0x39, 0xbc, 0x8a, 0x74, 0xc7, 0x5e,
  17433. 0x2c, 0x65, 0xc6, 0x11, 0x95, 0x44, 0xd6, 0x1e}, 16,
  17434. /* valid */
  17435. 1,
  17436. },
  17437. {
  17438. /* key, key length */
  17439. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17440. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17441. /* iv, iv length */
  17442. {0xaf, 0x5a, 0x03, 0xae, 0x7e, 0xdd, 0x73, 0x47,
  17443. 0x1b, 0xdc, 0xdf, 0xac, 0x5e, 0x19, 0x4a, 0x60}, 16,
  17444. /* aad, aad length */
  17445. {0x00}, 0,
  17446. /* msg, msg length */
  17447. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17448. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17449. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17450. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17451. /* ct, ct length */
  17452. {0x94, 0xc5, 0xd2, 0xac, 0xa6, 0xdb, 0xbc, 0xe8,
  17453. 0xc2, 0x45, 0x13, 0xa2, 0x5e, 0x09, 0x5c, 0x0e,
  17454. 0x54, 0xa9, 0x42, 0x86, 0x0d, 0x32, 0x7a, 0x22,
  17455. 0x2a, 0x81, 0x5c, 0xc7, 0x13, 0xb1, 0x63, 0xb4}, 32,
  17456. /* tag, tag length */
  17457. {0xf5, 0x0b, 0x30, 0x30, 0x4e, 0x45, 0xc9, 0xd4,
  17458. 0x11, 0xe8, 0xdf, 0x45, 0x08, 0xa9, 0x86, 0x12}, 16,
  17459. /* valid */
  17460. 1,
  17461. },
  17462. {
  17463. /* key, key length */
  17464. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17465. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17466. /* iv, iv length */
  17467. {0xb3, 0x70, 0x87, 0x68, 0x0f, 0x0e, 0xdd, 0x5a,
  17468. 0x52, 0x22, 0x8b, 0x8c, 0x7a, 0xae, 0xa6, 0x64}, 16,
  17469. /* aad, aad length */
  17470. {0x00}, 0,
  17471. /* msg, msg length */
  17472. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17473. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17474. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17475. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17476. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  17477. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  17478. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  17479. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33}, 64,
  17480. /* ct, ct length */
  17481. {0x3b, 0xb6, 0x17, 0x3e, 0x37, 0x72, 0xd4, 0xb6,
  17482. 0x2e, 0xef, 0x37, 0xf9, 0xef, 0x07, 0x81, 0xf3,
  17483. 0x60, 0xb6, 0xc7, 0x4b, 0xe3, 0xbf, 0x6b, 0x37,
  17484. 0x10, 0x67, 0xbc, 0x1b, 0x09, 0x0d, 0x9d, 0x66,
  17485. 0x22, 0xa1, 0xfb, 0xec, 0x6a, 0xc4, 0x71, 0xb3,
  17486. 0x34, 0x9c, 0xd4, 0x27, 0x7a, 0x10, 0x1d, 0x40,
  17487. 0x89, 0x0f, 0xbf, 0x27, 0xdf, 0xdc, 0xd0, 0xb4,
  17488. 0xe3, 0x78, 0x1f, 0x98, 0x06, 0xda, 0xab, 0xb6}, 64,
  17489. /* tag, tag length */
  17490. {0xa0, 0x49, 0x87, 0x45, 0xe5, 0x99, 0x99, 0xdd,
  17491. 0xc3, 0x2d, 0x5b, 0x14, 0x02, 0x41, 0x12, 0x4e}, 16,
  17492. /* valid */
  17493. 1,
  17494. },
  17495. {
  17496. /* key, key length */
  17497. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17498. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17499. /* iv, iv length */
  17500. {0x4f, 0x80, 0x2d, 0xa6, 0x2a, 0x38, 0x45, 0x55,
  17501. 0xa1, 0x9b, 0xc2, 0xb3, 0x82, 0xeb, 0x25, 0xaf}, 16,
  17502. /* aad, aad length */
  17503. {0x00}, 0,
  17504. /* msg, msg length */
  17505. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17506. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17507. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17508. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17509. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  17510. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  17511. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  17512. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  17513. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  17514. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44}, 80,
  17515. /* ct, ct length */
  17516. {0xe9, 0xb0, 0xbb, 0x88, 0x57, 0x81, 0x8c, 0xe3,
  17517. 0x20, 0x1c, 0x36, 0x90, 0xd2, 0x1d, 0xaa, 0x7f,
  17518. 0x26, 0x4f, 0xb8, 0xee, 0x93, 0xcc, 0x7a, 0x46,
  17519. 0x74, 0xea, 0x2f, 0xc3, 0x2b, 0xf1, 0x82, 0xfb,
  17520. 0x2a, 0x7e, 0x8a, 0xd5, 0x15, 0x07, 0xad, 0x4f,
  17521. 0x31, 0xce, 0xfc, 0x23, 0x56, 0xfe, 0x79, 0x36,
  17522. 0xa7, 0xf6, 0xe1, 0x9f, 0x95, 0xe8, 0x8f, 0xdb,
  17523. 0xf1, 0x76, 0x20, 0x91, 0x6d, 0x3a, 0x6f, 0x3d,
  17524. 0x01, 0xfc, 0x17, 0xd3, 0x58, 0x67, 0x2f, 0x77,
  17525. 0x7f, 0xd4, 0x09, 0x92, 0x46, 0xe4, 0x36, 0xe1}, 80,
  17526. /* tag, tag length */
  17527. {0x67, 0x91, 0x0b, 0xe7, 0x44, 0xb8, 0x31, 0x5a,
  17528. 0xe0, 0xeb, 0x61, 0x24, 0x59, 0x0c, 0x5d, 0x8b}, 16,
  17529. /* valid */
  17530. 1,
  17531. },
  17532. {
  17533. /* key, key length */
  17534. {0xb6, 0x7b, 0x1a, 0x6e, 0xfd, 0xd4, 0x0d, 0x37,
  17535. 0x08, 0x0f, 0xbe, 0x8f, 0x80, 0x47, 0xae, 0xb9}, 16,
  17536. /* iv, iv length */
  17537. {0xfa, 0x29, 0x4b, 0x12, 0x99, 0x72, 0xf7, 0xfc,
  17538. 0x5b, 0xbd, 0x5b, 0x96, 0xbb, 0xa8, 0x37, 0xc9}, 16,
  17539. /* aad, aad length */
  17540. {0x00}, 0,
  17541. /* msg, msg length */
  17542. {0x00}, 0,
  17543. /* ct, ct length */
  17544. {0x00}, 0,
  17545. /* tag, tag length */
  17546. {0xb1, 0x4b, 0x64, 0xfb, 0x58, 0x98, 0x99, 0x69,
  17547. 0x95, 0x70, 0xcc, 0x91, 0x60, 0xe3, 0x98, 0x96}, 16,
  17548. /* valid */
  17549. 1,
  17550. },
  17551. {
  17552. /* key, key length */
  17553. {0x20, 0x9e, 0x6d, 0xbf, 0x2a, 0xd2, 0x6a, 0x10,
  17554. 0x54, 0x45, 0xfc, 0x02, 0x07, 0xcd, 0x9e, 0x9a}, 16,
  17555. /* iv, iv length */
  17556. {0x94, 0x77, 0x84, 0x9d, 0x6c, 0xcd, 0xfc, 0xa1,
  17557. 0x12, 0xd9, 0x2e, 0x53, 0xfa, 0xe4, 0xa7, 0xca}, 16,
  17558. /* aad, aad length */
  17559. {0x00}, 0,
  17560. /* msg, msg length */
  17561. {0x01}, 1,
  17562. /* ct, ct length */
  17563. {0x1d}, 1,
  17564. /* tag, tag length */
  17565. {0x52, 0xa5, 0xf6, 0x00, 0xfe, 0x53, 0x38, 0x02,
  17566. 0x6a, 0x7c, 0xb0, 0x9c, 0x11, 0x64, 0x00, 0x82}, 16,
  17567. /* valid */
  17568. 1,
  17569. },
  17570. {
  17571. /* key, key length */
  17572. {0xa5, 0x49, 0x44, 0x2e, 0x35, 0x15, 0x40, 0x32,
  17573. 0xd0, 0x7c, 0x86, 0x66, 0x00, 0x6a, 0xa6, 0xa2}, 16,
  17574. /* iv, iv length */
  17575. {0x51, 0x71, 0x52, 0x45, 0x68, 0xe8, 0x1d, 0x97,
  17576. 0xe8, 0xc4, 0xde, 0x4b, 0xa5, 0x6c, 0x10, 0xa0}, 16,
  17577. /* aad, aad length */
  17578. {0x00}, 0,
  17579. /* msg, msg length */
  17580. {0x11, 0x82, 0xe9, 0x35, 0x96, 0xca, 0xc5, 0x60,
  17581. 0x89, 0x46, 0x40, 0x0b, 0xc7, 0x3f, 0x3a}, 15,
  17582. /* ct, ct length */
  17583. {0xd7, 0xb8, 0xa6, 0xb4, 0x3d, 0x2e, 0x9f, 0x98,
  17584. 0xc2, 0xb4, 0x4c, 0xe5, 0xe3, 0xcf, 0xdb}, 15,
  17585. /* tag, tag length */
  17586. {0x1b, 0xdd, 0x52, 0xfc, 0x98, 0x7d, 0xaf, 0x0e,
  17587. 0xe1, 0x92, 0x34, 0xc9, 0x05, 0xea, 0x64, 0x5f}, 16,
  17588. /* valid */
  17589. 1,
  17590. },
  17591. {
  17592. /* key, key length */
  17593. {0x95, 0x8b, 0xcd, 0xb6, 0x6a, 0x39, 0x52, 0xb5,
  17594. 0x37, 0x01, 0x58, 0x2a, 0x68, 0xa0, 0xe4, 0x74}, 16,
  17595. /* iv, iv length */
  17596. {0x0e, 0x6e, 0xc8, 0x79, 0xb0, 0x2c, 0x6f, 0x51,
  17597. 0x69, 0x76, 0xe3, 0x58, 0x98, 0x42, 0x8d, 0xa7}, 16,
  17598. /* aad, aad length */
  17599. {0x00}, 0,
  17600. /* msg, msg length */
  17601. {0x14, 0x04, 0x15, 0x82, 0x3e, 0xcc, 0x89, 0x32,
  17602. 0xa0, 0x58, 0x38, 0x4b, 0x73, 0x8e, 0xa6, 0xea,
  17603. 0x6d, 0x4d, 0xfe, 0x3b, 0xbe, 0xee}, 22,
  17604. /* ct, ct length */
  17605. {0x73, 0xe5, 0xc6, 0xf0, 0xe7, 0x03, 0xa5, 0x2d,
  17606. 0x02, 0xf7, 0xf7, 0xfa, 0xeb, 0x1b, 0x77, 0xfd,
  17607. 0x4f, 0xd0, 0xcb, 0x42, 0x1e, 0xaf}, 22,
  17608. /* tag, tag length */
  17609. {0x6c, 0x15, 0x4a, 0x85, 0x96, 0x8e, 0xdd, 0x74,
  17610. 0x77, 0x65, 0x75, 0xa4, 0x45, 0x0b, 0xd8, 0x97}, 16,
  17611. /* valid */
  17612. 1,
  17613. },
  17614. {
  17615. /* key, key length */
  17616. {0x96, 0x5b, 0x75, 0x7b, 0xa5, 0x01, 0x8a, 0x8d,
  17617. 0x66, 0xed, 0xc7, 0x8e, 0x0c, 0xee, 0xe8, 0x6b}, 16,
  17618. /* iv, iv length */
  17619. {0x2e, 0x35, 0x90, 0x1a, 0xe7, 0xd4, 0x91, 0xee,
  17620. 0xcc, 0x88, 0x38, 0xfe, 0xdd, 0x63, 0x14, 0x05}, 16,
  17621. /* aad, aad length */
  17622. {0xdf, 0x10, 0xd0, 0xd2, 0x12, 0x24, 0x24, 0x50}, 8,
  17623. /* msg, msg length */
  17624. {0x36, 0xe5, 0x7a, 0x76, 0x39, 0x58, 0xb0, 0x2c,
  17625. 0xea, 0x9d, 0x6a, 0x67, 0x6e, 0xbc, 0xe8, 0x1f}, 16,
  17626. /* ct, ct length */
  17627. {0x93, 0x6b, 0x69, 0xb6, 0xc9, 0x55, 0xad, 0xfd,
  17628. 0x15, 0x53, 0x9b, 0x9b, 0xe4, 0x98, 0x9c, 0xb6}, 16,
  17629. /* tag, tag length */
  17630. {0xee, 0x15, 0xa1, 0x45, 0x4e, 0x88, 0xfa, 0xad,
  17631. 0x8e, 0x48, 0xa8, 0xdf, 0x29, 0x83, 0xb4, 0x25}, 16,
  17632. /* valid */
  17633. 1,
  17634. },
  17635. {
  17636. /* key, key length */
  17637. {0x88, 0xd0, 0x20, 0x33, 0x78, 0x1c, 0x7b, 0x41,
  17638. 0x64, 0x71, 0x1a, 0x05, 0x42, 0x0f, 0x25, 0x6e}, 16,
  17639. /* iv, iv length */
  17640. {0x7f, 0x29, 0x85, 0x29, 0x63, 0x15, 0x50, 0x7a,
  17641. 0xa4, 0xc0, 0xa9, 0x3d, 0x5c, 0x12, 0xbd, 0x77}, 16,
  17642. /* aad, aad length */
  17643. {0x7c, 0x57, 0x1d, 0x2f, 0xbb, 0x5f, 0x62, 0x52,
  17644. 0x3c, 0x0e, 0xb3, 0x38, 0xbe, 0xf9, 0xa9}, 15,
  17645. /* msg, msg length */
  17646. {0xd9, 0x8a, 0xdc, 0x03, 0xd9, 0xd5, 0x82, 0x73,
  17647. 0x2e, 0xb0, 0x7d, 0xf2, 0x3d, 0x7b, 0x9f, 0x74}, 16,
  17648. /* ct, ct length */
  17649. {0x67, 0xca, 0xac, 0x35, 0x44, 0x3a, 0x31, 0x38,
  17650. 0xd2, 0xcb, 0x81, 0x1f, 0x0c, 0xe0, 0x4d, 0xd2}, 16,
  17651. /* tag, tag length */
  17652. {0xb7, 0x96, 0x8e, 0x0b, 0x56, 0x40, 0xe3, 0xb2,
  17653. 0x36, 0x56, 0x96, 0x53, 0x20, 0x8b, 0x9d, 0xeb}, 16,
  17654. /* valid */
  17655. 1,
  17656. },
  17657. {
  17658. /* key, key length */
  17659. {0x51, 0x58, 0x40, 0xcf, 0x67, 0xd2, 0xe4, 0x0e,
  17660. 0xb6, 0x5e, 0x54, 0xa2, 0x4c, 0x72, 0xcb, 0xf2}, 16,
  17661. /* iv, iv length */
  17662. {0xbf, 0x47, 0xaf, 0xdf, 0xd4, 0x92, 0x13, 0x7a,
  17663. 0x24, 0x23, 0x6b, 0xc3, 0x67, 0x97, 0xa8, 0x8e}, 16,
  17664. /* aad, aad length */
  17665. {0x16, 0x84, 0x3c, 0x09, 0x1d, 0x43, 0xb0, 0xa1,
  17666. 0x91, 0xd0, 0xc7, 0x3d, 0x15, 0x60, 0x1b, 0xe9}, 16,
  17667. /* msg, msg length */
  17668. {0xc8, 0x34, 0x58, 0x8c, 0xb6, 0xda, 0xf9, 0xf0,
  17669. 0x6d, 0xd2, 0x35, 0x19, 0xf4, 0xbe, 0x9f, 0x56}, 16,
  17670. /* ct, ct length */
  17671. {0x20, 0x0a, 0xc4, 0x51, 0xfb, 0xeb, 0x0f, 0x61,
  17672. 0x51, 0xd6, 0x15, 0x83, 0xa4, 0x3b, 0x73, 0x43}, 16,
  17673. /* tag, tag length */
  17674. {0x2a, 0xd4, 0x3e, 0x4c, 0xaa, 0x51, 0x98, 0x3a,
  17675. 0x9d, 0x4d, 0x24, 0x48, 0x1b, 0xf4, 0xc8, 0x39}, 16,
  17676. /* valid */
  17677. 1,
  17678. },
  17679. {
  17680. /* key, key length */
  17681. {0x2e, 0x44, 0x92, 0xd4, 0x44, 0xe5, 0xb6, 0xf4,
  17682. 0xce, 0xc8, 0xc2, 0xd3, 0x61, 0x5a, 0xc8, 0x58}, 16,
  17683. /* iv, iv length */
  17684. {0xd0, 0x2b, 0xf0, 0x76, 0x3a, 0x9f, 0xef, 0xbf,
  17685. 0x70, 0xc3, 0x3a, 0xee, 0x1e, 0x9d, 0xa1, 0xd6}, 16,
  17686. /* aad, aad length */
  17687. {0x90, 0x4d, 0x86, 0xf1, 0x33, 0xce, 0xc1, 0x5a,
  17688. 0x0c, 0x3c, 0xaf, 0x14, 0xd7, 0xe0, 0x29, 0xc8,
  17689. 0x2a, 0x07, 0x70, 0x5a, 0x23, 0xf0, 0xd0, 0x80}, 24,
  17690. /* msg, msg length */
  17691. {0x9e, 0x62, 0xd6, 0x51, 0x1b, 0x0b, 0xda, 0x7d,
  17692. 0xd7, 0x74, 0x0b, 0x61, 0x4d, 0x97, 0xba, 0xe0}, 16,
  17693. /* ct, ct length */
  17694. {0x27, 0xc6, 0xe9, 0xa6, 0x53, 0xc5, 0x25, 0x3c,
  17695. 0xa1, 0xc5, 0x67, 0x3f, 0x97, 0xb9, 0xb3, 0x3e}, 16,
  17696. /* tag, tag length */
  17697. {0x2d, 0x58, 0x12, 0x71, 0xe1, 0xfa, 0x9e, 0x36,
  17698. 0x86, 0x13, 0x6c, 0xaa, 0x8f, 0x4d, 0x6c, 0x8e}, 16,
  17699. /* valid */
  17700. 1,
  17701. },
  17702. {
  17703. /* key, key length */
  17704. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17705. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17706. /* iv, iv length */
  17707. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17708. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17709. /* aad, aad length */
  17710. {0x00}, 0,
  17711. /* msg, msg length */
  17712. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17713. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17714. /* ct, ct length */
  17715. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17716. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17717. /* tag, tag length */
  17718. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17719. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17720. /* valid */
  17721. 0,
  17722. },
  17723. {
  17724. /* key, key length */
  17725. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17726. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17727. /* iv, iv length */
  17728. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17729. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17730. /* aad, aad length */
  17731. {0x00}, 0,
  17732. /* msg, msg length */
  17733. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17734. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17735. /* ct, ct length */
  17736. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17737. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17738. /* tag, tag length */
  17739. {0xe4, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17740. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17741. /* valid */
  17742. 0,
  17743. },
  17744. {
  17745. /* key, key length */
  17746. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17747. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17748. /* iv, iv length */
  17749. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17750. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17751. /* aad, aad length */
  17752. {0x00}, 0,
  17753. /* msg, msg length */
  17754. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17755. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17756. /* ct, ct length */
  17757. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17758. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17759. /* tag, tag length */
  17760. {0x66, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17761. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17762. /* valid */
  17763. 0,
  17764. },
  17765. {
  17766. /* key, key length */
  17767. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17768. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17769. /* iv, iv length */
  17770. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17771. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17772. /* aad, aad length */
  17773. {0x00}, 0,
  17774. /* msg, msg length */
  17775. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17776. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17777. /* ct, ct length */
  17778. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17779. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17780. /* tag, tag length */
  17781. {0xe6, 0x0f, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17782. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17783. /* valid */
  17784. 0,
  17785. },
  17786. {
  17787. /* key, key length */
  17788. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17789. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17790. /* iv, iv length */
  17791. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17792. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17793. /* aad, aad length */
  17794. {0x00}, 0,
  17795. /* msg, msg length */
  17796. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17797. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17798. /* ct, ct length */
  17799. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17800. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17801. /* tag, tag length */
  17802. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0xf2,
  17803. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17804. /* valid */
  17805. 0,
  17806. },
  17807. {
  17808. /* key, key length */
  17809. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17810. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17811. /* iv, iv length */
  17812. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17813. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17814. /* aad, aad length */
  17815. {0x00}, 0,
  17816. /* msg, msg length */
  17817. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17818. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17819. /* ct, ct length */
  17820. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17821. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17822. /* tag, tag length */
  17823. {0xe6, 0x0e, 0x7c, 0x50, 0x12, 0xa6, 0xdb, 0xf2,
  17824. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17825. /* valid */
  17826. 0,
  17827. },
  17828. {
  17829. /* key, key length */
  17830. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17831. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17832. /* iv, iv length */
  17833. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17834. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17835. /* aad, aad length */
  17836. {0x00}, 0,
  17837. /* msg, msg length */
  17838. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17839. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17840. /* ct, ct length */
  17841. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17842. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17843. /* tag, tag length */
  17844. {0xe6, 0x0e, 0x7c, 0x50, 0x11, 0xa6, 0xdb, 0xf2,
  17845. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17846. /* valid */
  17847. 0,
  17848. },
  17849. {
  17850. /* key, key length */
  17851. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17852. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17853. /* iv, iv length */
  17854. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17855. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17856. /* aad, aad length */
  17857. {0x00}, 0,
  17858. /* msg, msg length */
  17859. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17860. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17861. /* ct, ct length */
  17862. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17863. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17864. /* tag, tag length */
  17865. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  17866. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17867. /* valid */
  17868. 0,
  17869. },
  17870. {
  17871. /* key, key length */
  17872. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17873. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17874. /* iv, iv length */
  17875. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17876. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17877. /* aad, aad length */
  17878. {0x00}, 0,
  17879. /* msg, msg length */
  17880. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17881. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17882. /* ct, ct length */
  17883. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17884. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17885. /* tag, tag length */
  17886. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17887. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17888. /* valid */
  17889. 0,
  17890. },
  17891. {
  17892. /* key, key length */
  17893. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17894. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17895. /* iv, iv length */
  17896. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17897. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17898. /* aad, aad length */
  17899. {0x00}, 0,
  17900. /* msg, msg length */
  17901. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17902. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17903. /* ct, ct length */
  17904. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17905. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17906. /* tag, tag length */
  17907. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17908. 0xd2, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17909. /* valid */
  17910. 0,
  17911. },
  17912. {
  17913. /* key, key length */
  17914. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17915. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17916. /* iv, iv length */
  17917. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17918. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17919. /* aad, aad length */
  17920. {0x00}, 0,
  17921. /* msg, msg length */
  17922. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17923. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17924. /* ct, ct length */
  17925. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17926. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17927. /* tag, tag length */
  17928. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17929. 0x52, 0xb8, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17930. /* valid */
  17931. 0,
  17932. },
  17933. {
  17934. /* key, key length */
  17935. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17936. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17937. /* iv, iv length */
  17938. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17939. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17940. /* aad, aad length */
  17941. {0x00}, 0,
  17942. /* msg, msg length */
  17943. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17944. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17945. /* ct, ct length */
  17946. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17947. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17948. /* tag, tag length */
  17949. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17950. 0x52, 0x98, 0xb0, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17951. /* valid */
  17952. 0,
  17953. },
  17954. {
  17955. /* key, key length */
  17956. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17957. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17958. /* iv, iv length */
  17959. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17960. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17961. /* aad, aad length */
  17962. {0x00}, 0,
  17963. /* msg, msg length */
  17964. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17965. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17966. /* ct, ct length */
  17967. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17968. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17969. /* tag, tag length */
  17970. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17971. 0x52, 0x98, 0xb1, 0x92, 0x9a, 0xc3, 0x56, 0xa7}, 16,
  17972. /* valid */
  17973. 0,
  17974. },
  17975. {
  17976. /* key, key length */
  17977. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17978. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17979. /* iv, iv length */
  17980. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17981. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17982. /* aad, aad length */
  17983. {0x00}, 0,
  17984. /* msg, msg length */
  17985. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17986. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17987. /* ct, ct length */
  17988. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17989. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17990. /* tag, tag length */
  17991. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17992. 0x52, 0x98, 0xb1, 0x92, 0x99, 0xc3, 0x56, 0xa7}, 16,
  17993. /* valid */
  17994. 0,
  17995. },
  17996. {
  17997. /* key, key length */
  17998. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17999. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18000. /* iv, iv length */
  18001. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18002. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18003. /* aad, aad length */
  18004. {0x00}, 0,
  18005. /* msg, msg length */
  18006. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18007. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18008. /* ct, ct length */
  18009. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18010. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18011. /* tag, tag length */
  18012. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18013. 0x52, 0x98, 0xb1, 0x92, 0x1b, 0xc3, 0x56, 0xa7}, 16,
  18014. /* valid */
  18015. 0,
  18016. },
  18017. {
  18018. /* key, key length */
  18019. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18020. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18021. /* iv, iv length */
  18022. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18023. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18024. /* aad, aad length */
  18025. {0x00}, 0,
  18026. /* msg, msg length */
  18027. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18028. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18029. /* ct, ct length */
  18030. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18031. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18032. /* tag, tag length */
  18033. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18034. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa6}, 16,
  18035. /* valid */
  18036. 0,
  18037. },
  18038. {
  18039. /* key, key length */
  18040. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18041. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18042. /* iv, iv length */
  18043. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18044. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18045. /* aad, aad length */
  18046. {0x00}, 0,
  18047. /* msg, msg length */
  18048. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18049. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18050. /* ct, ct length */
  18051. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18052. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18053. /* tag, tag length */
  18054. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18055. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa5}, 16,
  18056. /* valid */
  18057. 0,
  18058. },
  18059. {
  18060. /* key, key length */
  18061. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18062. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18063. /* iv, iv length */
  18064. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18065. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18066. /* aad, aad length */
  18067. {0x00}, 0,
  18068. /* msg, msg length */
  18069. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18070. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18071. /* ct, ct length */
  18072. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18073. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18074. /* tag, tag length */
  18075. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18076. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xe7}, 16,
  18077. /* valid */
  18078. 0,
  18079. },
  18080. {
  18081. /* key, key length */
  18082. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18083. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18084. /* iv, iv length */
  18085. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18086. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18087. /* aad, aad length */
  18088. {0x00}, 0,
  18089. /* msg, msg length */
  18090. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18091. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18092. /* ct, ct length */
  18093. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18094. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18095. /* tag, tag length */
  18096. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18097. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  18098. /* valid */
  18099. 0,
  18100. },
  18101. {
  18102. /* key, key length */
  18103. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18104. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18105. /* iv, iv length */
  18106. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18107. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18108. /* aad, aad length */
  18109. {0x00}, 0,
  18110. /* msg, msg length */
  18111. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18112. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18113. /* ct, ct length */
  18114. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18115. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18116. /* tag, tag length */
  18117. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18118. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18119. /* valid */
  18120. 0,
  18121. },
  18122. {
  18123. /* key, key length */
  18124. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18125. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18126. /* iv, iv length */
  18127. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18128. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18129. /* aad, aad length */
  18130. {0x00}, 0,
  18131. /* msg, msg length */
  18132. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18133. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18134. /* ct, ct length */
  18135. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18136. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18137. /* tag, tag length */
  18138. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0x72,
  18139. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18140. /* valid */
  18141. 0,
  18142. },
  18143. {
  18144. /* key, key length */
  18145. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18146. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18147. /* iv, iv length */
  18148. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18149. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18150. /* aad, aad length */
  18151. {0x00}, 0,
  18152. /* msg, msg length */
  18153. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18154. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18155. /* ct, ct length */
  18156. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18157. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18158. /* tag, tag length */
  18159. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  18160. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  18161. /* valid */
  18162. 0,
  18163. },
  18164. {
  18165. /* key, key length */
  18166. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18167. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18168. /* iv, iv length */
  18169. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18170. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18171. /* aad, aad length */
  18172. {0x00}, 0,
  18173. /* msg, msg length */
  18174. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18175. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18176. /* ct, ct length */
  18177. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18178. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18179. /* tag, tag length */
  18180. {0x19, 0xf1, 0x83, 0xaf, 0xec, 0x59, 0x24, 0x0d,
  18181. 0xad, 0x67, 0x4e, 0x6d, 0x64, 0x3c, 0xa9, 0x58}, 16,
  18182. /* valid */
  18183. 0,
  18184. },
  18185. {
  18186. /* key, key length */
  18187. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18188. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18189. /* iv, iv length */
  18190. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18191. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18192. /* aad, aad length */
  18193. {0x00}, 0,
  18194. /* msg, msg length */
  18195. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18196. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18197. /* ct, ct length */
  18198. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18199. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18200. /* tag, tag length */
  18201. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18202. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 16,
  18203. /* valid */
  18204. 0,
  18205. },
  18206. {
  18207. /* key, key length */
  18208. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18209. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18210. /* iv, iv length */
  18211. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18212. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18213. /* aad, aad length */
  18214. {0x00}, 0,
  18215. /* msg, msg length */
  18216. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18217. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18218. /* ct, ct length */
  18219. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18220. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18221. /* tag, tag length */
  18222. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  18223. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 16,
  18224. /* valid */
  18225. 0,
  18226. },
  18227. {
  18228. /* key, key length */
  18229. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18230. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18231. /* iv, iv length */
  18232. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18233. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18234. /* aad, aad length */
  18235. {0x00}, 0,
  18236. /* msg, msg length */
  18237. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18238. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18239. /* ct, ct length */
  18240. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18241. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18242. /* tag, tag length */
  18243. {0x66, 0x8e, 0xfc, 0xd0, 0x93, 0x26, 0x5b, 0x72,
  18244. 0xd2, 0x18, 0x31, 0x12, 0x1b, 0x43, 0xd6, 0x27}, 16,
  18245. /* valid */
  18246. 0,
  18247. },
  18248. {
  18249. /* key, key length */
  18250. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18251. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18252. /* iv, iv length */
  18253. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18254. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18255. /* aad, aad length */
  18256. {0x00}, 0,
  18257. /* msg, msg length */
  18258. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18259. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18260. /* ct, ct length */
  18261. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18262. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18263. /* tag, tag length */
  18264. {0xe7, 0x0f, 0x7d, 0x51, 0x12, 0xa7, 0xda, 0xf3,
  18265. 0x53, 0x99, 0xb0, 0x93, 0x9a, 0xc2, 0x57, 0xa6}, 16,
  18266. /* valid */
  18267. 0,
  18268. },
  18269. };
  18270. byte ciphertext[sizeof(vectors[0].ct)];
  18271. byte authtag[sizeof(vectors[0].tag)];
  18272. int i;
  18273. int len;
  18274. int ret;
  18275. for (i = 0; i < (int)(sizeof(vectors)/sizeof(vectors[0])); i++) {
  18276. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  18277. len = sizeof(authtag);
  18278. ExpectIntEQ(wc_AesEaxEncryptAuth(vectors[i].key, vectors[i].key_length,
  18279. ciphertext,
  18280. vectors[i].msg, vectors[i].msg_length,
  18281. vectors[i].iv, vectors[i].iv_length,
  18282. authtag, len,
  18283. vectors[i].aad, vectors[i].aad_length),
  18284. 0);
  18285. /* check ciphertext matches vector */
  18286. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].ct, vectors[i].ct_length),
  18287. 0);
  18288. /* check that computed tag matches vector only for vectors marked as valid */
  18289. ret = XMEMCMP(authtag, vectors[i].tag, len);
  18290. if (vectors[i].valid) {
  18291. ExpectIntEQ(ret, 0);
  18292. }
  18293. else {
  18294. ExpectIntNE(ret, 0);
  18295. }
  18296. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  18297. /* Decrypt, checking that the computed auth tags match */
  18298. ExpectIntEQ(wc_AesEaxDecryptAuth(vectors[i].key, vectors[i].key_length,
  18299. ciphertext,
  18300. vectors[i].ct, vectors[i].ct_length,
  18301. vectors[i].iv, vectors[i].iv_length,
  18302. authtag, len,
  18303. vectors[i].aad, vectors[i].aad_length),
  18304. 0);
  18305. /* check decrypted ciphertext matches vector plaintext */
  18306. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].msg, vectors[i].msg_length),
  18307. 0);
  18308. }
  18309. return EXPECT_RESULT();
  18310. } /* END test_wc_AesEaxVectors */
  18311. /*
  18312. * Testing test_wc_AesEaxEncryptAuth()
  18313. */
  18314. static int test_wc_AesEaxEncryptAuth(void)
  18315. {
  18316. EXPECT_DECLS;
  18317. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18318. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  18319. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18320. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  18321. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18322. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  18323. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18324. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  18325. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  18326. const byte msg[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  18327. byte ciphertext[sizeof(msg)];
  18328. byte authtag[AES_BLOCK_SIZE];
  18329. int i;
  18330. int len;
  18331. len = sizeof(authtag);
  18332. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18333. ciphertext,
  18334. msg, sizeof(msg),
  18335. iv, sizeof(iv),
  18336. authtag, len,
  18337. aad, sizeof(aad)),
  18338. 0);
  18339. /* Test null checking */
  18340. ExpectIntEQ(wc_AesEaxEncryptAuth(NULL, sizeof(key),
  18341. ciphertext,
  18342. msg, sizeof(msg),
  18343. iv, sizeof(iv),
  18344. authtag, len,
  18345. aad, sizeof(aad)),
  18346. BAD_FUNC_ARG);
  18347. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18348. NULL,
  18349. msg, sizeof(msg),
  18350. iv, sizeof(iv),
  18351. authtag, len,
  18352. aad, sizeof(aad)),
  18353. BAD_FUNC_ARG);
  18354. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18355. ciphertext,
  18356. NULL, sizeof(msg),
  18357. iv, sizeof(iv),
  18358. authtag, len,
  18359. aad, sizeof(aad)),
  18360. BAD_FUNC_ARG);
  18361. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18362. ciphertext,
  18363. msg, sizeof(msg),
  18364. NULL, sizeof(iv),
  18365. authtag, len,
  18366. aad, sizeof(aad)),
  18367. BAD_FUNC_ARG);
  18368. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18369. ciphertext,
  18370. msg, sizeof(msg),
  18371. iv, sizeof(iv),
  18372. NULL, len,
  18373. aad, sizeof(aad)),
  18374. BAD_FUNC_ARG);
  18375. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18376. ciphertext,
  18377. msg, sizeof(msg),
  18378. iv, sizeof(iv),
  18379. authtag, len,
  18380. NULL, sizeof(aad)),
  18381. BAD_FUNC_ARG);
  18382. /* Test bad key lengths */
  18383. for (i = 0; i <= 32; i++) {
  18384. int exp_ret;
  18385. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  18386. || i == AES_256_KEY_SIZE) {
  18387. exp_ret = 0;
  18388. }
  18389. else {
  18390. exp_ret = BAD_FUNC_ARG;
  18391. }
  18392. ExpectIntEQ(wc_AesEaxEncryptAuth(key, i,
  18393. ciphertext,
  18394. msg, sizeof(msg),
  18395. iv, sizeof(iv),
  18396. authtag, len,
  18397. aad, sizeof(aad)),
  18398. exp_ret);
  18399. }
  18400. /* Test auth tag size out of range */
  18401. len = AES_BLOCK_SIZE + 1;
  18402. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18403. ciphertext,
  18404. msg, sizeof(msg),
  18405. iv, sizeof(iv),
  18406. authtag, len,
  18407. aad, sizeof(aad)),
  18408. BAD_FUNC_ARG);
  18409. return EXPECT_RESULT();
  18410. } /* END test_wc_AesEaxEncryptAuth() */
  18411. /*
  18412. * Testing test_wc_AesEaxDecryptAuth()
  18413. */
  18414. static int test_wc_AesEaxDecryptAuth(void)
  18415. {
  18416. EXPECT_DECLS;
  18417. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18418. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  18419. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18420. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  18421. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18422. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  18423. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18424. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  18425. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  18426. const byte ct[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  18427. /* Garbage tag that should always fail for above aad */
  18428. const byte tag[] = {0xFE, 0xED, 0xBE, 0xEF, 0xDE, 0xAD, 0xC0, 0xDE,
  18429. 0xCA, 0xFE, 0xBE, 0xEF, 0xDE, 0xAF, 0xBE, 0xEF};
  18430. byte plaintext[sizeof(ct)];
  18431. int i;
  18432. int len;
  18433. len = sizeof(tag);
  18434. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18435. plaintext,
  18436. ct, sizeof(ct),
  18437. iv, sizeof(iv),
  18438. tag, len,
  18439. aad, sizeof(aad)),
  18440. AES_EAX_AUTH_E);
  18441. /* Test null checking */
  18442. ExpectIntEQ(wc_AesEaxDecryptAuth(NULL, sizeof(key),
  18443. plaintext,
  18444. ct, sizeof(ct),
  18445. iv, sizeof(iv),
  18446. tag, len,
  18447. aad, sizeof(aad)),
  18448. BAD_FUNC_ARG);
  18449. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18450. NULL,
  18451. ct, sizeof(ct),
  18452. iv, sizeof(iv),
  18453. tag, len,
  18454. aad, sizeof(aad)),
  18455. BAD_FUNC_ARG);
  18456. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18457. plaintext,
  18458. NULL, sizeof(ct),
  18459. iv, sizeof(iv),
  18460. tag, len,
  18461. aad, sizeof(aad)),
  18462. BAD_FUNC_ARG);
  18463. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18464. plaintext,
  18465. ct, sizeof(ct),
  18466. NULL, sizeof(iv),
  18467. tag, len,
  18468. aad, sizeof(aad)),
  18469. BAD_FUNC_ARG);
  18470. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18471. plaintext,
  18472. ct, sizeof(ct),
  18473. iv, sizeof(iv),
  18474. NULL, len,
  18475. aad, sizeof(aad)),
  18476. BAD_FUNC_ARG);
  18477. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18478. plaintext,
  18479. ct, sizeof(ct),
  18480. iv, sizeof(iv),
  18481. tag, len,
  18482. NULL, sizeof(aad)),
  18483. BAD_FUNC_ARG);
  18484. /* Test bad key lengths */
  18485. for (i = 0; i <= 32; i++) {
  18486. int exp_ret;
  18487. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  18488. || i == AES_256_KEY_SIZE) {
  18489. exp_ret = AES_EAX_AUTH_E;
  18490. }
  18491. else {
  18492. exp_ret = BAD_FUNC_ARG;
  18493. }
  18494. ExpectIntEQ(wc_AesEaxDecryptAuth(key, i,
  18495. plaintext,
  18496. ct, sizeof(ct),
  18497. iv, sizeof(iv),
  18498. tag, len,
  18499. aad, sizeof(aad)),
  18500. exp_ret);
  18501. }
  18502. /* Test auth tag size out of range */
  18503. len = AES_BLOCK_SIZE + 1;
  18504. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18505. plaintext,
  18506. ct, sizeof(ct),
  18507. iv, sizeof(iv),
  18508. tag, len,
  18509. aad, sizeof(aad)),
  18510. BAD_FUNC_ARG);
  18511. return EXPECT_RESULT();
  18512. } /* END test_wc_AesEaxDecryptAuth() */
  18513. #endif /* WOLFSSL_AES_EAX &&
  18514. * (!HAVE_FIPS || FIPS_VERSION_GE(5, 3)) && !HAVE_SELFTEST
  18515. */
  18516. /*
  18517. * Testing wc_InitDsaKey()
  18518. */
  18519. static int test_wc_InitDsaKey(void)
  18520. {
  18521. EXPECT_DECLS;
  18522. #ifndef NO_DSA
  18523. DsaKey key;
  18524. XMEMSET(&key, 0, sizeof(DsaKey));
  18525. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18526. /* Pass in bad args. */
  18527. ExpectIntEQ(wc_InitDsaKey(NULL), BAD_FUNC_ARG);
  18528. wc_FreeDsaKey(&key);
  18529. #endif
  18530. return EXPECT_RESULT();
  18531. } /* END test_wc_InitDsaKey */
  18532. /*
  18533. * Testing wc_DsaSign() and wc_DsaVerify()
  18534. */
  18535. static int test_wc_DsaSignVerify(void)
  18536. {
  18537. EXPECT_DECLS;
  18538. #if !defined(NO_DSA)
  18539. DsaKey key;
  18540. WC_RNG rng;
  18541. wc_Sha sha;
  18542. byte signature[DSA_SIG_SIZE];
  18543. byte hash[WC_SHA_DIGEST_SIZE];
  18544. word32 idx = 0;
  18545. word32 bytes;
  18546. int answer;
  18547. #ifdef USE_CERT_BUFFERS_1024
  18548. byte tmp[ONEK_BUF];
  18549. XMEMSET(tmp, 0, sizeof(tmp));
  18550. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18551. bytes = sizeof_dsa_key_der_1024;
  18552. #elif defined(USE_CERT_BUFFERS_2048)
  18553. byte tmp[TWOK_BUF];
  18554. XMEMSET(tmp, 0, sizeof(tmp));
  18555. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18556. bytes = sizeof_dsa_key_der_2048;
  18557. #else
  18558. byte tmp[TWOK_BUF];
  18559. XFILE fp = XBADFILE;
  18560. XMEMSET(tmp, 0, sizeof(tmp));
  18561. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  18562. ExpectTrue((bytes = (word32)XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  18563. if (fp != XBADFILE)
  18564. XFCLOSE(fp);
  18565. #endif /* END USE_CERT_BUFFERS_1024 */
  18566. ExpectIntEQ(wc_InitSha(&sha), 0);
  18567. ExpectIntEQ(wc_ShaUpdate(&sha, tmp, bytes), 0);
  18568. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  18569. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18570. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  18571. ExpectIntEQ(wc_InitRng(&rng), 0);
  18572. /* Sign. */
  18573. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), 0);
  18574. /* Test bad args. */
  18575. ExpectIntEQ(wc_DsaSign(NULL, signature, &key, &rng), BAD_FUNC_ARG);
  18576. ExpectIntEQ(wc_DsaSign(hash, NULL, &key, &rng), BAD_FUNC_ARG);
  18577. ExpectIntEQ(wc_DsaSign(hash, signature, NULL, &rng), BAD_FUNC_ARG);
  18578. ExpectIntEQ(wc_DsaSign(hash, signature, &key, NULL), BAD_FUNC_ARG);
  18579. /* Verify. */
  18580. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, &answer), 0);
  18581. ExpectIntEQ(answer, 1);
  18582. /* Pass in bad args. */
  18583. ExpectIntEQ(wc_DsaVerify(NULL, signature, &key, &answer), BAD_FUNC_ARG);
  18584. ExpectIntEQ(wc_DsaVerify(hash, NULL, &key, &answer), BAD_FUNC_ARG);
  18585. ExpectIntEQ(wc_DsaVerify(hash, signature, NULL, &answer), BAD_FUNC_ARG);
  18586. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, NULL), BAD_FUNC_ARG);
  18587. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  18588. /* hard set q to 0 and test fail case */
  18589. mp_free(&key.q);
  18590. mp_init(&key.q);
  18591. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  18592. mp_set(&key.q, 1);
  18593. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  18594. #endif
  18595. DoExpectIntEQ(wc_FreeRng(&rng),0);
  18596. wc_FreeDsaKey(&key);
  18597. wc_ShaFree(&sha);
  18598. #endif
  18599. return EXPECT_RESULT();
  18600. } /* END test_wc_DsaSign */
  18601. /*
  18602. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  18603. */
  18604. static int test_wc_DsaPublicPrivateKeyDecode(void)
  18605. {
  18606. EXPECT_DECLS;
  18607. #if !defined(NO_DSA)
  18608. DsaKey key;
  18609. word32 bytes;
  18610. word32 idx = 0;
  18611. int ret;
  18612. #ifdef USE_CERT_BUFFERS_1024
  18613. byte tmp[ONEK_BUF];
  18614. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18615. bytes = sizeof_dsa_key_der_1024;
  18616. #elif defined(USE_CERT_BUFFERS_2048)
  18617. byte tmp[TWOK_BUF];
  18618. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18619. bytes = sizeof_dsa_key_der_2048;
  18620. #else
  18621. byte tmp[TWOK_BUF];
  18622. XFILE fp = XBADFILE;
  18623. XMEMSET(tmp, 0, sizeof(tmp));
  18624. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  18625. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  18626. if (fp != XBADFILE)
  18627. XFCLOSE(fp);
  18628. #endif /* END USE_CERT_BUFFERS_1024 */
  18629. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18630. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  18631. /* Test bad args. */
  18632. ExpectIntEQ(wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  18633. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  18634. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  18635. ExpectIntLT(ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  18636. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  18637. wc_FreeDsaKey(&key);
  18638. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18639. idx = 0; /* Reset */
  18640. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  18641. /* Test bad args. */
  18642. ExpectIntEQ(wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  18643. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  18644. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  18645. ExpectIntLT(ret = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  18646. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  18647. wc_FreeDsaKey(&key);
  18648. #endif /* !NO_DSA */
  18649. return EXPECT_RESULT();
  18650. } /* END test_wc_DsaPublicPrivateKeyDecode */
  18651. /*
  18652. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  18653. */
  18654. static int test_wc_MakeDsaKey(void)
  18655. {
  18656. EXPECT_DECLS;
  18657. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18658. DsaKey genKey;
  18659. WC_RNG rng;
  18660. XMEMSET(&genKey, 0, sizeof(genKey));
  18661. XMEMSET(&rng, 0, sizeof(rng));
  18662. ExpectIntEQ(wc_InitDsaKey(&genKey), 0);
  18663. ExpectIntEQ(wc_InitRng(&rng), 0);
  18664. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey), 0);
  18665. /* Test bad args. */
  18666. ExpectIntEQ(wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey), BAD_FUNC_ARG);
  18667. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, NULL), BAD_FUNC_ARG);
  18668. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey),
  18669. BAD_FUNC_ARG);
  18670. ExpectIntEQ(wc_MakeDsaKey(&rng, &genKey), 0);
  18671. /* Test bad args. */
  18672. ExpectIntEQ(wc_MakeDsaKey(NULL, &genKey), BAD_FUNC_ARG);
  18673. ExpectIntEQ(wc_MakeDsaKey(&rng, NULL), BAD_FUNC_ARG);
  18674. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18675. wc_FreeDsaKey(&genKey);
  18676. #endif
  18677. return EXPECT_RESULT();
  18678. } /* END test_wc_MakeDsaKey */
  18679. /*
  18680. * Testing wc_DsaKeyToDer()
  18681. */
  18682. static int test_wc_DsaKeyToDer(void)
  18683. {
  18684. EXPECT_DECLS;
  18685. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18686. DsaKey key;
  18687. word32 bytes;
  18688. word32 idx = 0;
  18689. #ifdef USE_CERT_BUFFERS_1024
  18690. byte tmp[ONEK_BUF];
  18691. byte der[ONEK_BUF];
  18692. XMEMSET(tmp, 0, sizeof(tmp));
  18693. XMEMSET(der, 0, sizeof(der));
  18694. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18695. bytes = sizeof_dsa_key_der_1024;
  18696. #elif defined(USE_CERT_BUFFERS_2048)
  18697. byte tmp[TWOK_BUF];
  18698. byte der[TWOK_BUF];
  18699. XMEMSET(tmp, 0, sizeof(tmp));
  18700. XMEMSET(der, 0, sizeof(der));
  18701. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18702. bytes = sizeof_dsa_key_der_2048;
  18703. #else
  18704. byte tmp[TWOK_BUF];
  18705. byte der[TWOK_BUF];
  18706. XFILE fp = XBADFILE;
  18707. XMEMSET(tmp, 0, sizeof(tmp));
  18708. XMEMSET(der, 0, sizeof(der));
  18709. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  18710. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  18711. if (fp != XBADFILE)
  18712. XFCLOSE(fp);
  18713. #endif /* END USE_CERT_BUFFERS_1024 */
  18714. XMEMSET(&key, 0, sizeof(DsaKey));
  18715. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18716. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  18717. ExpectIntGE(wc_DsaKeyToDer(&key, der, bytes), 0);
  18718. ExpectIntEQ(XMEMCMP(der, tmp, bytes), 0);
  18719. /* Test bad args. */
  18720. ExpectIntEQ(wc_DsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  18721. ExpectIntEQ(wc_DsaKeyToDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  18722. wc_FreeDsaKey(&key);
  18723. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  18724. return EXPECT_RESULT();
  18725. } /* END test_wc_DsaKeyToDer */
  18726. /*
  18727. * Testing wc_DsaKeyToPublicDer()
  18728. * (indirectly testing setDsaPublicKey())
  18729. */
  18730. static int test_wc_DsaKeyToPublicDer(void)
  18731. {
  18732. EXPECT_DECLS;
  18733. #ifndef HAVE_SELFTEST
  18734. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18735. DsaKey key;
  18736. WC_RNG rng;
  18737. byte* der = NULL;
  18738. word32 sz = 0;
  18739. word32 idx = 0;
  18740. XMEMSET(&key, 0, sizeof(DsaKey));
  18741. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18742. ExpectNotNull(der = (byte*)XMALLOC(ONEK_BUF, NULL,
  18743. DYNAMIC_TYPE_TMP_BUFFER));
  18744. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18745. ExpectIntEQ(wc_InitRng(&rng), 0);
  18746. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &key), 0);
  18747. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  18748. ExpectIntGE(sz = wc_DsaKeyToPublicDer(&key, der, ONEK_BUF), 0);
  18749. wc_FreeDsaKey(&key);
  18750. idx = 0;
  18751. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  18752. /* Test without the SubjectPublicKeyInfo header */
  18753. ExpectIntGE(sz = wc_SetDsaPublicKey(der, &key, ONEK_BUF, 0), 0);
  18754. wc_FreeDsaKey(&key);
  18755. idx = 0;
  18756. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  18757. /* Test bad args. */
  18758. ExpectIntEQ(wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  18759. ExpectIntEQ(wc_DsaKeyToPublicDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  18760. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18761. wc_FreeDsaKey(&key);
  18762. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  18763. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  18764. #endif /* !HAVE_SELFTEST */
  18765. return EXPECT_RESULT();
  18766. } /* END test_wc_DsaKeyToPublicDer */
  18767. /*
  18768. * Testing wc_DsaImportParamsRaw()
  18769. */
  18770. static int test_wc_DsaImportParamsRaw(void)
  18771. {
  18772. EXPECT_DECLS;
  18773. #if !defined(NO_DSA)
  18774. DsaKey key;
  18775. /* [mod = L=1024, N=160], from CAVP KeyPair */
  18776. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  18777. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  18778. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  18779. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  18780. "47123188f8dc551054ee162b634d60f097f719076640e209"
  18781. "80a0093113a8bd73";
  18782. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  18783. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  18784. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  18785. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  18786. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  18787. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  18788. "76341a7e7d9";
  18789. /* invalid p and q parameters */
  18790. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  18791. const char* invalidQ = "96c5390a";
  18792. XMEMSET(&key, 0, sizeof(DsaKey));
  18793. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18794. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  18795. /* test bad args */
  18796. /* null key struct */
  18797. ExpectIntEQ(wc_DsaImportParamsRaw(NULL, p, q, g), BAD_FUNC_ARG);
  18798. /* null param pointers */
  18799. ExpectIntEQ(wc_DsaImportParamsRaw(&key, NULL, NULL, NULL), BAD_FUNC_ARG);
  18800. /* illegal p length */
  18801. ExpectIntEQ(wc_DsaImportParamsRaw(&key, invalidP, q, g), BAD_FUNC_ARG);
  18802. /* illegal q length */
  18803. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, invalidQ, g), BAD_FUNC_ARG);
  18804. wc_FreeDsaKey(&key);
  18805. #endif
  18806. return EXPECT_RESULT();
  18807. } /* END test_wc_DsaImportParamsRaw */
  18808. /*
  18809. * Testing wc_DsaImportParamsRawCheck()
  18810. */
  18811. static int test_wc_DsaImportParamsRawCheck(void)
  18812. {
  18813. EXPECT_DECLS;
  18814. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  18815. DsaKey key;
  18816. int trusted = 0;
  18817. /* [mod = L=1024, N=160], from CAVP KeyPair */
  18818. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  18819. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  18820. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  18821. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  18822. "47123188f8dc551054ee162b634d60f097f719076640e209"
  18823. "80a0093113a8bd73";
  18824. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  18825. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  18826. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  18827. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  18828. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  18829. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  18830. "76341a7e7d9";
  18831. /* invalid p and q parameters */
  18832. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  18833. const char* invalidQ = "96c5390a";
  18834. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18835. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL), 0);
  18836. /* test bad args */
  18837. /* null key struct */
  18838. ExpectIntEQ(wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL),
  18839. BAD_FUNC_ARG);
  18840. /* null param pointers */
  18841. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted,
  18842. NULL), BAD_FUNC_ARG);
  18843. /* illegal p length */
  18844. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL),
  18845. BAD_FUNC_ARG);
  18846. /* illegal q length */
  18847. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL),
  18848. BAD_FUNC_ARG);
  18849. wc_FreeDsaKey(&key);
  18850. #endif
  18851. return EXPECT_RESULT();
  18852. } /* END test_wc_DsaImportParamsRawCheck */
  18853. /*
  18854. * Testing wc_DsaExportParamsRaw()
  18855. */
  18856. static int test_wc_DsaExportParamsRaw(void)
  18857. {
  18858. EXPECT_DECLS;
  18859. #if !defined(NO_DSA)
  18860. DsaKey key;
  18861. /* [mod = L=1024, N=160], from CAVP KeyPair */
  18862. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  18863. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  18864. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  18865. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  18866. "47123188f8dc551054ee162b634d60f097f719076640e209"
  18867. "80a0093113a8bd73";
  18868. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  18869. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  18870. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  18871. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  18872. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  18873. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  18874. "76341a7e7d9";
  18875. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  18876. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  18877. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  18878. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  18879. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  18880. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  18881. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  18882. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  18883. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  18884. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  18885. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  18886. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  18887. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  18888. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  18889. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  18890. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  18891. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  18892. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  18893. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  18894. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  18895. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  18896. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  18897. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  18898. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  18899. byte pOut[MAX_DSA_PARAM_SIZE];
  18900. byte qOut[MAX_DSA_PARAM_SIZE];
  18901. byte gOut[MAX_DSA_PARAM_SIZE];
  18902. word32 pOutSz;
  18903. word32 qOutSz;
  18904. word32 gOutSz;
  18905. XMEMSET(&key, 0, sizeof(DsaKey));
  18906. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18907. /* first test using imported raw parameters, for expected */
  18908. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  18909. pOutSz = sizeof(pOut);
  18910. qOutSz = sizeof(qOut);
  18911. gOutSz = sizeof(gOut);
  18912. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  18913. &gOutSz), 0);
  18914. /* validate exported parameters are correct */
  18915. ExpectIntEQ(XMEMCMP(pOut, pCompare, pOutSz), 0);
  18916. ExpectIntEQ(XMEMCMP(qOut, qCompare, qOutSz), 0);
  18917. ExpectIntEQ(XMEMCMP(gOut, gCompare, gOutSz), 0);
  18918. /* test bad args */
  18919. /* null key struct */
  18920. ExpectIntEQ(wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz, gOut,
  18921. &gOutSz), BAD_FUNC_ARG);
  18922. /* null output pointers */
  18923. ExpectIntEQ(wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz, NULL,
  18924. &gOutSz), LENGTH_ONLY_E);
  18925. /* null output size pointers */
  18926. ExpectIntEQ( wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL, gOut,
  18927. NULL), BAD_FUNC_ARG);
  18928. /* p output buffer size too small */
  18929. pOutSz = 1;
  18930. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  18931. &gOutSz), BUFFER_E);
  18932. pOutSz = sizeof(pOut);
  18933. /* q output buffer size too small */
  18934. qOutSz = 1;
  18935. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  18936. &gOutSz), BUFFER_E);
  18937. qOutSz = sizeof(qOut);
  18938. /* g output buffer size too small */
  18939. gOutSz = 1;
  18940. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  18941. &gOutSz), BUFFER_E);
  18942. wc_FreeDsaKey(&key);
  18943. #endif
  18944. return EXPECT_RESULT();
  18945. } /* END test_wc_DsaExportParamsRaw */
  18946. /*
  18947. * Testing wc_DsaExportKeyRaw()
  18948. */
  18949. static int test_wc_DsaExportKeyRaw(void)
  18950. {
  18951. EXPECT_DECLS;
  18952. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18953. DsaKey key;
  18954. WC_RNG rng;
  18955. byte xOut[MAX_DSA_PARAM_SIZE];
  18956. byte yOut[MAX_DSA_PARAM_SIZE];
  18957. word32 xOutSz, yOutSz;
  18958. XMEMSET(&key, 0, sizeof(key));
  18959. XMEMSET(&rng, 0, sizeof(rng));
  18960. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18961. ExpectIntEQ(wc_InitRng(&rng), 0);
  18962. ExpectIntEQ(wc_MakeDsaParameters(&rng, 1024, &key), 0);
  18963. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  18964. /* try successful export */
  18965. xOutSz = sizeof(xOut);
  18966. yOutSz = sizeof(yOut);
  18967. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz), 0);
  18968. /* test bad args */
  18969. /* null key struct */
  18970. ExpectIntEQ(wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz),
  18971. BAD_FUNC_ARG);
  18972. /* null output pointers */
  18973. ExpectIntEQ(wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz),
  18974. LENGTH_ONLY_E);
  18975. /* null output size pointers */
  18976. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL),
  18977. BAD_FUNC_ARG);
  18978. /* x output buffer size too small */
  18979. xOutSz = 1;
  18980. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  18981. BUFFER_E);
  18982. xOutSz = sizeof(xOut);
  18983. /* y output buffer size too small */
  18984. yOutSz = 1;
  18985. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  18986. BUFFER_E);
  18987. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18988. wc_FreeDsaKey(&key);
  18989. #endif
  18990. return EXPECT_RESULT();
  18991. } /* END test_wc_DsaExportParamsRaw */
  18992. /*
  18993. * Testing wc_ed25519_make_key().
  18994. */
  18995. static int test_wc_ed25519_make_key(void)
  18996. {
  18997. EXPECT_DECLS;
  18998. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_MAKE_KEY)
  18999. ed25519_key key;
  19000. WC_RNG rng;
  19001. unsigned char pubkey[ED25519_PUB_KEY_SIZE+1];
  19002. int pubkey_sz = ED25519_PUB_KEY_SIZE;
  19003. XMEMSET(&key, 0, sizeof(ed25519_key));
  19004. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19005. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19006. ExpectIntEQ(wc_InitRng(&rng), 0);
  19007. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey, pubkey_sz),
  19008. ECC_PRIV_KEY_E);
  19009. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey+1, pubkey_sz),
  19010. ECC_PRIV_KEY_E);
  19011. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19012. /* Test bad args. */
  19013. ExpectIntEQ(wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key),
  19014. BAD_FUNC_ARG);
  19015. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL),
  19016. BAD_FUNC_ARG);
  19017. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key),
  19018. BAD_FUNC_ARG);
  19019. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key),
  19020. BAD_FUNC_ARG);
  19021. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19022. wc_ed25519_free(&key);
  19023. #endif
  19024. return EXPECT_RESULT();
  19025. } /* END test_wc_ed25519_make_key */
  19026. /*
  19027. * Testing wc_ed25519_init()
  19028. */
  19029. static int test_wc_ed25519_init(void)
  19030. {
  19031. EXPECT_DECLS;
  19032. #if defined(HAVE_ED25519)
  19033. ed25519_key key;
  19034. XMEMSET(&key, 0, sizeof(ed25519_key));
  19035. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19036. /* Test bad args. */
  19037. ExpectIntEQ(wc_ed25519_init(NULL), BAD_FUNC_ARG);
  19038. wc_ed25519_free(&key);
  19039. #endif
  19040. return EXPECT_RESULT();
  19041. } /* END test_wc_ed25519_init */
  19042. /*
  19043. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  19044. */
  19045. static int test_wc_ed25519_sign_msg(void)
  19046. {
  19047. EXPECT_DECLS;
  19048. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  19049. WC_RNG rng;
  19050. ed25519_key key;
  19051. byte msg[] = "Everybody gets Friday off.\n";
  19052. byte sig[ED25519_SIG_SIZE+1];
  19053. word32 msglen = sizeof(msg);
  19054. word32 siglen = ED25519_SIG_SIZE;
  19055. word32 badSigLen = ED25519_SIG_SIZE - 1;
  19056. #ifdef HAVE_ED25519_VERIFY
  19057. int verify_ok = 0; /*1 = Verify success.*/
  19058. #endif
  19059. /* Initialize stack variables. */
  19060. XMEMSET(&key, 0, sizeof(ed25519_key));
  19061. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19062. XMEMSET(sig, 0, sizeof(sig));
  19063. /* Initialize key. */
  19064. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19065. ExpectIntEQ(wc_InitRng(&rng), 0);
  19066. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19067. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key), 0);
  19068. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  19069. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig+1, &siglen, &key), 0);
  19070. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  19071. /* Test bad args. */
  19072. ExpectIntEQ(wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key),
  19073. BAD_FUNC_ARG);
  19074. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key),
  19075. BAD_FUNC_ARG);
  19076. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key),
  19077. BAD_FUNC_ARG);
  19078. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL),
  19079. BAD_FUNC_ARG);
  19080. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key),
  19081. BUFFER_E);
  19082. ExpectIntEQ(badSigLen, ED25519_SIG_SIZE);
  19083. badSigLen -= 1;
  19084. #ifdef HAVE_ED25519_VERIFY
  19085. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  19086. &key), 0);
  19087. ExpectIntEQ(verify_ok, 1);
  19088. /* Test bad args. */
  19089. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen - 1, msg, msglen,
  19090. &verify_ok, &key), BAD_FUNC_ARG);
  19091. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen + 1, msg, msglen,
  19092. &verify_ok, &key), BAD_FUNC_ARG);
  19093. ExpectIntEQ(wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  19094. &key), BAD_FUNC_ARG);
  19095. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, NULL, msglen, &verify_ok,
  19096. &key), BAD_FUNC_ARG);
  19097. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, NULL, &key),
  19098. BAD_FUNC_ARG);
  19099. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  19100. NULL), BAD_FUNC_ARG);
  19101. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, badSigLen, msg, msglen, &verify_ok,
  19102. &key), BAD_FUNC_ARG);
  19103. #endif /* Verify. */
  19104. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19105. wc_ed25519_free(&key);
  19106. #endif
  19107. return EXPECT_RESULT();
  19108. } /* END test_wc_ed25519_sign_msg */
  19109. /*
  19110. * Testing wc_ed25519_import_public()
  19111. */
  19112. static int test_wc_ed25519_import_public(void)
  19113. {
  19114. EXPECT_DECLS;
  19115. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19116. ed25519_key pubKey;
  19117. WC_RNG rng;
  19118. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  19119. word32 inlen = sizeof(in);
  19120. XMEMSET(&pubKey, 0, sizeof(ed25519_key));
  19121. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19122. ExpectIntEQ(wc_ed25519_init(&pubKey), 0);
  19123. ExpectIntEQ(wc_InitRng(&rng), 0);
  19124. #ifdef HAVE_ED25519_MAKE_KEY
  19125. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey), 0);
  19126. #endif
  19127. ExpectIntEQ(wc_ed25519_import_public_ex(in, inlen, &pubKey, 1), 0);
  19128. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  19129. /* Test bad args. */
  19130. ExpectIntEQ(wc_ed25519_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  19131. ExpectIntEQ(wc_ed25519_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  19132. ExpectIntEQ(wc_ed25519_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  19133. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19134. wc_ed25519_free(&pubKey);
  19135. #endif
  19136. return EXPECT_RESULT();
  19137. } /* END wc_ed25519_import_public */
  19138. /*
  19139. * Testing wc_ed25519_import_private_key()
  19140. */
  19141. static int test_wc_ed25519_import_private_key(void)
  19142. {
  19143. EXPECT_DECLS;
  19144. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19145. ed25519_key key;
  19146. WC_RNG rng;
  19147. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  19148. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  19149. word32 privKeySz = sizeof(privKey);
  19150. word32 pubKeySz = sizeof(pubKey);
  19151. #ifdef HAVE_ED25519_KEY_EXPORT
  19152. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  19153. word32 bothKeysSz = sizeof(bothKeys);
  19154. #endif
  19155. XMEMSET(&key, 0, sizeof(ed25519_key));
  19156. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19157. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19158. ExpectIntEQ(wc_InitRng(&rng), 0);
  19159. #ifdef HAVE_ED25519_MAKE_KEY
  19160. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19161. #endif
  19162. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  19163. pubKeySz, &key, 1), 0);
  19164. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  19165. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  19166. #ifdef HAVE_ED25519_KEY_EXPORT
  19167. ExpectIntEQ(wc_ed25519_export_private(&key, bothKeys, &bothKeysSz), 0);
  19168. ExpectIntEQ(wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  19169. &key, 1), 0);
  19170. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  19171. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  19172. #endif
  19173. /* Test bad args. */
  19174. ExpectIntEQ(wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  19175. &key), BAD_FUNC_ARG);
  19176. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  19177. pubKeySz, &key), BAD_FUNC_ARG);
  19178. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  19179. pubKeySz, NULL), BAD_FUNC_ARG);
  19180. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  19181. pubKeySz, &key), BAD_FUNC_ARG);
  19182. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  19183. pubKeySz - 1, &key), BAD_FUNC_ARG);
  19184. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL, 0,
  19185. &key), BAD_FUNC_ARG);
  19186. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19187. wc_ed25519_free(&key);
  19188. #endif
  19189. return EXPECT_RESULT();
  19190. } /* END test_wc_ed25519_import_private_key */
  19191. /*
  19192. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  19193. */
  19194. static int test_wc_ed25519_export(void)
  19195. {
  19196. EXPECT_DECLS;
  19197. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  19198. ed25519_key key;
  19199. WC_RNG rng;
  19200. byte priv[ED25519_PRV_KEY_SIZE];
  19201. byte pub[ED25519_PUB_KEY_SIZE];
  19202. word32 privSz = sizeof(priv);
  19203. word32 pubSz = sizeof(pub);
  19204. #ifndef HAVE_ED25519_MAKE_KEY
  19205. const byte privKey[] = {
  19206. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  19207. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  19208. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  19209. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  19210. };
  19211. const byte pubKey[] = {
  19212. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  19213. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  19214. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  19215. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  19216. };
  19217. #endif
  19218. XMEMSET(&key, 0, sizeof(ed25519_key));
  19219. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19220. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19221. ExpectIntEQ(wc_InitRng(&rng), 0);
  19222. #ifdef HAVE_ED25519_MAKE_KEY
  19223. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19224. #else
  19225. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  19226. pubKey, sizeof(pubKey), &key, 1), 0);
  19227. #endif
  19228. ExpectIntEQ(wc_ed25519_export_public(&key, pub, &pubSz), 0);
  19229. ExpectIntEQ(pubSz, ED25519_KEY_SIZE);
  19230. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  19231. /* Test bad args. */
  19232. ExpectIntEQ(wc_ed25519_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  19233. ExpectIntEQ(wc_ed25519_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  19234. ExpectIntEQ(wc_ed25519_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  19235. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, &privSz), 0);
  19236. ExpectIntEQ(privSz, ED25519_KEY_SIZE);
  19237. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  19238. /* Test bad args. */
  19239. ExpectIntEQ(wc_ed25519_export_private_only(NULL, priv, &privSz),
  19240. BAD_FUNC_ARG);
  19241. ExpectIntEQ(wc_ed25519_export_private_only(&key, NULL, &privSz),
  19242. BAD_FUNC_ARG);
  19243. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, NULL),
  19244. BAD_FUNC_ARG);
  19245. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19246. wc_ed25519_free(&key);
  19247. #endif
  19248. return EXPECT_RESULT();
  19249. } /* END test_wc_ed25519_export */
  19250. /*
  19251. * Testing wc_ed25519_size()
  19252. */
  19253. static int test_wc_ed25519_size(void)
  19254. {
  19255. EXPECT_DECLS;
  19256. #if defined(HAVE_ED25519)
  19257. ed25519_key key;
  19258. WC_RNG rng;
  19259. #ifndef HAVE_ED25519_MAKE_KEY
  19260. const byte privKey[] = {
  19261. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  19262. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  19263. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  19264. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  19265. };
  19266. const byte pubKey[] = {
  19267. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  19268. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  19269. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  19270. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  19271. };
  19272. #endif
  19273. XMEMSET(&key, 0, sizeof(ed25519_key));
  19274. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19275. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19276. ExpectIntEQ(wc_InitRng(&rng), 0);
  19277. #ifdef HAVE_ED25519_MAKE_KEY
  19278. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19279. #else
  19280. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  19281. pubKey, sizeof(pubKey), &key, 1), 0);
  19282. #endif
  19283. ExpectIntEQ(wc_ed25519_size(&key), ED25519_KEY_SIZE);
  19284. /* Test bad args. */
  19285. ExpectIntEQ(wc_ed25519_size(NULL), BAD_FUNC_ARG);
  19286. ExpectIntEQ(wc_ed25519_sig_size(&key), ED25519_SIG_SIZE);
  19287. /* Test bad args. */
  19288. ExpectIntEQ(wc_ed25519_sig_size(NULL), BAD_FUNC_ARG);
  19289. ExpectIntEQ(wc_ed25519_pub_size(&key), ED25519_PUB_KEY_SIZE);
  19290. /* Test bad args. */
  19291. ExpectIntEQ(wc_ed25519_pub_size(NULL), BAD_FUNC_ARG);
  19292. ExpectIntEQ(wc_ed25519_priv_size(&key), ED25519_PRV_KEY_SIZE);
  19293. /* Test bad args. */
  19294. ExpectIntEQ(wc_ed25519_priv_size(NULL), BAD_FUNC_ARG);
  19295. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19296. wc_ed25519_free(&key);
  19297. #endif
  19298. return EXPECT_RESULT();
  19299. } /* END test_wc_ed25519_size */
  19300. /*
  19301. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  19302. */
  19303. static int test_wc_ed25519_exportKey(void)
  19304. {
  19305. EXPECT_DECLS;
  19306. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  19307. WC_RNG rng;
  19308. ed25519_key key;
  19309. byte priv[ED25519_PRV_KEY_SIZE];
  19310. byte pub[ED25519_PUB_KEY_SIZE];
  19311. byte privOnly[ED25519_PRV_KEY_SIZE];
  19312. word32 privSz = sizeof(priv);
  19313. word32 pubSz = sizeof(pub);
  19314. word32 privOnlySz = sizeof(privOnly);
  19315. #ifndef HAVE_ED25519_MAKE_KEY
  19316. const byte privKey[] = {
  19317. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  19318. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  19319. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  19320. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  19321. };
  19322. const byte pubKey[] = {
  19323. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  19324. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  19325. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  19326. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  19327. };
  19328. #endif
  19329. XMEMSET(&key, 0, sizeof(ed25519_key));
  19330. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19331. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19332. ExpectIntEQ(wc_InitRng(&rng), 0);
  19333. #ifdef HAVE_ED25519_MAKE_KEY
  19334. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19335. #else
  19336. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  19337. pubKey, sizeof(pubKey), &key, 1), 0);
  19338. #endif
  19339. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, &privOnlySz), 0);
  19340. /* Test bad args. */
  19341. ExpectIntEQ(wc_ed25519_export_private(NULL, privOnly, &privOnlySz),
  19342. BAD_FUNC_ARG);
  19343. ExpectIntEQ(wc_ed25519_export_private(&key, NULL, &privOnlySz),
  19344. BAD_FUNC_ARG);
  19345. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  19346. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  19347. /* Test bad args. */
  19348. ExpectIntEQ(wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz),
  19349. BAD_FUNC_ARG);
  19350. ExpectIntEQ(wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz),
  19351. BAD_FUNC_ARG);
  19352. ExpectIntEQ(wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz),
  19353. BAD_FUNC_ARG);
  19354. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz),
  19355. BAD_FUNC_ARG);
  19356. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, NULL),
  19357. BAD_FUNC_ARG);
  19358. /* Cross check output. */
  19359. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  19360. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19361. wc_ed25519_free(&key);
  19362. #endif
  19363. return EXPECT_RESULT();
  19364. } /* END test_wc_ed25519_exportKey */
  19365. /*
  19366. * Testing wc_Ed25519PublicKeyToDer
  19367. */
  19368. static int test_wc_Ed25519PublicKeyToDer(void)
  19369. {
  19370. EXPECT_DECLS;
  19371. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  19372. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  19373. ed25519_key key;
  19374. byte derBuf[1024];
  19375. XMEMSET(&key, 0, sizeof(ed25519_key));
  19376. /* Test bad args */
  19377. ExpectIntEQ(wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  19378. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19379. ExpectIntEQ(wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  19380. wc_ed25519_free(&key);
  19381. /* Test good args */
  19382. if (EXPECT_SUCCESS()) {
  19383. WC_RNG rng;
  19384. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19385. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19386. ExpectIntEQ(wc_InitRng(&rng), 0);
  19387. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19388. ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  19389. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19390. wc_ed25519_free(&key);
  19391. }
  19392. #endif
  19393. return EXPECT_RESULT();
  19394. } /* END testing wc_Ed25519PublicKeyToDer */
  19395. /*
  19396. * Testing wc_curve25519_init and wc_curve25519_free.
  19397. */
  19398. static int test_wc_curve25519_init(void)
  19399. {
  19400. EXPECT_DECLS;
  19401. #if defined(HAVE_CURVE25519)
  19402. curve25519_key key;
  19403. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19404. /* Test bad args for wc_curve25519_init */
  19405. ExpectIntEQ(wc_curve25519_init(NULL), BAD_FUNC_ARG);
  19406. /* Test good args for wc_curve_25519_free */
  19407. wc_curve25519_free(&key);
  19408. /* Test bad args for wc_curve25519 free. */
  19409. wc_curve25519_free(NULL);
  19410. #endif
  19411. return EXPECT_RESULT();
  19412. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  19413. /*
  19414. * Testing test_wc_curve25519_size.
  19415. */
  19416. static int test_wc_curve25519_size(void)
  19417. {
  19418. EXPECT_DECLS;
  19419. #if defined(HAVE_CURVE25519)
  19420. curve25519_key key;
  19421. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19422. /* Test good args for wc_curve25519_size */
  19423. ExpectIntEQ(wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  19424. /* Test bad args for wc_curve25519_size */
  19425. ExpectIntEQ(wc_curve25519_size(NULL), 0);
  19426. wc_curve25519_free(&key);
  19427. #endif
  19428. return EXPECT_RESULT();
  19429. } /* END test_wc_curve25519_size*/
  19430. /*
  19431. * Testing test_wc_curve25519_export_key_raw().
  19432. */
  19433. static int test_wc_curve25519_export_key_raw(void)
  19434. {
  19435. EXPECT_DECLS;
  19436. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  19437. curve25519_key key;
  19438. WC_RNG rng;
  19439. byte privateKey[CURVE25519_KEYSIZE];
  19440. byte publicKey[CURVE25519_KEYSIZE];
  19441. word32 prvkSz;
  19442. word32 pubkSz;
  19443. byte prik[CURVE25519_KEYSIZE];
  19444. byte pubk[CURVE25519_KEYSIZE];
  19445. word32 prksz;
  19446. word32 pbksz;
  19447. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19448. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19449. ExpectIntEQ(wc_InitRng(&rng), 0);
  19450. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19451. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  19452. prvkSz = CURVE25519_KEYSIZE;
  19453. pubkSz = CURVE25519_KEYSIZE;
  19454. ExpectIntEQ(wc_curve25519_export_key_raw(NULL, privateKey, &prvkSz,
  19455. publicKey, &pubkSz), BAD_FUNC_ARG);
  19456. prvkSz = CURVE25519_KEYSIZE;
  19457. pubkSz = CURVE25519_KEYSIZE;
  19458. ExpectIntEQ(wc_curve25519_export_key_raw(&key, NULL, &prvkSz, publicKey,
  19459. &pubkSz), BAD_FUNC_ARG);
  19460. prvkSz = CURVE25519_KEYSIZE;
  19461. pubkSz = CURVE25519_KEYSIZE;
  19462. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, NULL,
  19463. publicKey, &pubkSz), BAD_FUNC_ARG);
  19464. /* prvkSz = CURVE25519_KEYSIZE; */
  19465. pubkSz = CURVE25519_KEYSIZE;
  19466. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  19467. NULL, &pubkSz), BAD_FUNC_ARG);
  19468. prvkSz = CURVE25519_KEYSIZE;
  19469. pubkSz = CURVE25519_KEYSIZE;
  19470. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  19471. publicKey, NULL), BAD_FUNC_ARG);
  19472. /* cross-testing */
  19473. prksz = CURVE25519_KEYSIZE;
  19474. ExpectIntEQ(wc_curve25519_export_private_raw(&key, prik, &prksz), 0);
  19475. pbksz = CURVE25519_KEYSIZE;
  19476. ExpectIntEQ(wc_curve25519_export_public(&key, pubk, &pbksz), 0);
  19477. prvkSz = CURVE25519_KEYSIZE;
  19478. /* pubkSz = CURVE25519_KEYSIZE; */
  19479. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  19480. publicKey, &pubkSz), 0);
  19481. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  19482. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  19483. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  19484. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  19485. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  19486. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  19487. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19488. wc_curve25519_free(&key);
  19489. #endif
  19490. return EXPECT_RESULT();
  19491. } /* end of test_wc_curve25519_export_key_raw */
  19492. /*
  19493. * Testing test_wc_curve25519_export_key_raw_ex().
  19494. */
  19495. static int test_wc_curve25519_export_key_raw_ex(void)
  19496. {
  19497. EXPECT_DECLS;
  19498. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  19499. curve25519_key key;
  19500. WC_RNG rng;
  19501. byte privateKey[CURVE25519_KEYSIZE];
  19502. byte publicKey[CURVE25519_KEYSIZE];
  19503. word32 prvkSz;
  19504. word32 pubkSz;
  19505. byte prik[CURVE25519_KEYSIZE];
  19506. byte pubk[CURVE25519_KEYSIZE];
  19507. word32 prksz;
  19508. word32 pbksz;
  19509. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19510. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19511. ExpectIntEQ(wc_InitRng(&rng), 0);
  19512. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19513. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  19514. prvkSz = CURVE25519_KEYSIZE;
  19515. pubkSz = CURVE25519_KEYSIZE;
  19516. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  19517. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  19518. prvkSz = CURVE25519_KEYSIZE;
  19519. pubkSz = CURVE25519_KEYSIZE;
  19520. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  19521. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  19522. prvkSz = CURVE25519_KEYSIZE;
  19523. pubkSz = CURVE25519_KEYSIZE;
  19524. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19525. NULL, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  19526. /* prvkSz = CURVE25519_KEYSIZE; */
  19527. pubkSz = CURVE25519_KEYSIZE;
  19528. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19529. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  19530. prvkSz = CURVE25519_KEYSIZE;
  19531. pubkSz = CURVE25519_KEYSIZE;
  19532. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19533. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  19534. prvkSz = CURVE25519_KEYSIZE;
  19535. /* pubkSz = CURVE25519_KEYSIZE; */
  19536. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  19537. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  19538. prvkSz = CURVE25519_KEYSIZE;
  19539. pubkSz = CURVE25519_KEYSIZE;
  19540. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  19541. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  19542. prvkSz = CURVE25519_KEYSIZE;
  19543. pubkSz = CURVE25519_KEYSIZE;
  19544. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19545. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  19546. /* prvkSz = CURVE25519_KEYSIZE; */
  19547. pubkSz = CURVE25519_KEYSIZE;
  19548. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19549. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  19550. prvkSz = CURVE25519_KEYSIZE;
  19551. pubkSz = CURVE25519_KEYSIZE;
  19552. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19553. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  19554. /* illegal value for endian */
  19555. prvkSz = CURVE25519_KEYSIZE;
  19556. /* pubkSz = CURVE25519_KEYSIZE; */
  19557. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  19558. publicKey, NULL, EC25519_BIG_ENDIAN + 10), BAD_FUNC_ARG);
  19559. /* cross-testing */
  19560. prksz = CURVE25519_KEYSIZE;
  19561. ExpectIntEQ(wc_curve25519_export_private_raw( &key, prik, &prksz), 0);
  19562. pbksz = CURVE25519_KEYSIZE;
  19563. ExpectIntEQ(wc_curve25519_export_public( &key, pubk, &pbksz), 0);
  19564. prvkSz = CURVE25519_KEYSIZE;
  19565. /* pubkSz = CURVE25519_KEYSIZE; */
  19566. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  19567. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  19568. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  19569. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  19570. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  19571. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  19572. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  19573. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  19574. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  19575. publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), 0);
  19576. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  19577. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  19578. /* try once with another endian */
  19579. prvkSz = CURVE25519_KEYSIZE;
  19580. pubkSz = CURVE25519_KEYSIZE;
  19581. ExpectIntEQ(wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  19582. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  19583. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  19584. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  19585. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19586. wc_curve25519_free(&key);
  19587. #endif
  19588. return EXPECT_RESULT();
  19589. } /* end of test_wc_curve25519_export_key_raw_ex */
  19590. /*
  19591. * Testing wc_curve25519_make_key
  19592. */
  19593. static int test_wc_curve25519_make_key(void)
  19594. {
  19595. EXPECT_DECLS;
  19596. #if defined(HAVE_CURVE25519)
  19597. curve25519_key key;
  19598. WC_RNG rng;
  19599. int keysize;
  19600. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19601. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19602. ExpectIntEQ(wc_InitRng(&rng), 0);
  19603. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19604. ExpectIntEQ(keysize = wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  19605. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, &key), 0);
  19606. /* test bad cases*/
  19607. ExpectIntEQ(wc_curve25519_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  19608. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  19609. ExpectIntEQ(wc_curve25519_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  19610. ExpectIntEQ(wc_curve25519_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  19611. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19612. wc_curve25519_free(&key);
  19613. #endif
  19614. return EXPECT_RESULT();
  19615. } /* END test_wc_curve25519_make_key*/
  19616. /*
  19617. * Testing wc_curve25519_shared_secret_ex
  19618. */
  19619. static int test_wc_curve25519_shared_secret_ex(void)
  19620. {
  19621. EXPECT_DECLS;
  19622. #if defined(HAVE_CURVE25519)
  19623. curve25519_key private_key;
  19624. curve25519_key public_key;
  19625. WC_RNG rng;
  19626. byte out[CURVE25519_KEYSIZE];
  19627. word32 outLen = sizeof(out);
  19628. int endian = EC25519_BIG_ENDIAN;
  19629. ExpectIntEQ(wc_curve25519_init(&private_key), 0);
  19630. ExpectIntEQ(wc_curve25519_init(&public_key), 0);
  19631. ExpectIntEQ(wc_InitRng(&rng), 0);
  19632. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key),
  19633. 0);
  19634. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key),
  19635. 0);
  19636. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19637. &outLen, endian), 0);
  19638. /* test bad cases*/
  19639. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  19640. BAD_FUNC_ARG);
  19641. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, &public_key, out, &outLen,
  19642. endian), BAD_FUNC_ARG);
  19643. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, NULL, out, &outLen,
  19644. endian), BAD_FUNC_ARG);
  19645. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  19646. &outLen, endian), BAD_FUNC_ARG);
  19647. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19648. NULL, endian), BAD_FUNC_ARG);
  19649. /* curve25519.c is checking for public_key size less than or equal to 0x7f,
  19650. * increasing to 0x8f checks for error being returned*/
  19651. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  19652. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19653. &outLen, endian), ECC_BAD_ARG_E);
  19654. outLen = outLen - 2;
  19655. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19656. &outLen, endian), BAD_FUNC_ARG);
  19657. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19658. wc_curve25519_free(&private_key);
  19659. wc_curve25519_free(&public_key);
  19660. #endif
  19661. return EXPECT_RESULT();
  19662. } /* END test_wc_curve25519_shared_secret_ex*/
  19663. /*
  19664. * Testing wc_curve25519_make_pub
  19665. */
  19666. static int test_wc_curve25519_make_pub(void)
  19667. {
  19668. EXPECT_DECLS;
  19669. #ifdef HAVE_CURVE25519
  19670. curve25519_key key;
  19671. WC_RNG rng;
  19672. byte out[CURVE25519_KEYSIZE];
  19673. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19674. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19675. ExpectIntEQ(wc_InitRng(&rng), 0);
  19676. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19677. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(out), out,
  19678. (int)sizeof(key.k), key.k), 0);
  19679. /* test bad cases*/
  19680. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k,
  19681. (int)sizeof out, out), ECC_BAD_ARG_E);
  19682. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  19683. NULL), ECC_BAD_ARG_E);
  19684. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out - 1, out,
  19685. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  19686. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, NULL,
  19687. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  19688. /* verify clamping test */
  19689. key.k[0] |= ~248;
  19690. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  19691. key.k), ECC_BAD_ARG_E);
  19692. key.k[0] &= 248;
  19693. /* repeat the expected-to-succeed test. */
  19694. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  19695. key.k), 0);
  19696. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19697. wc_curve25519_free(&key);
  19698. #endif
  19699. return EXPECT_RESULT();
  19700. } /* END test_wc_curve25519_make_pub */
  19701. /*
  19702. * Testing test_wc_curve25519_export_public_ex
  19703. */
  19704. static int test_wc_curve25519_export_public_ex(void)
  19705. {
  19706. EXPECT_DECLS;
  19707. #if defined(HAVE_CURVE25519)
  19708. curve25519_key key;
  19709. WC_RNG rng;
  19710. byte out[CURVE25519_KEYSIZE];
  19711. word32 outLen = sizeof(out);
  19712. int endian = EC25519_BIG_ENDIAN;
  19713. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19714. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19715. ExpectIntEQ(wc_InitRng(&rng), 0);
  19716. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19717. ExpectIntEQ(wc_curve25519_export_public(&key, out, &outLen), 0);
  19718. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian), 0);
  19719. /* test bad cases*/
  19720. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, NULL, NULL, endian),
  19721. BAD_FUNC_ARG);
  19722. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, out, &outLen, endian),
  19723. BAD_FUNC_ARG);
  19724. ExpectIntEQ(wc_curve25519_export_public_ex(&key, NULL, &outLen, endian),
  19725. BAD_FUNC_ARG);
  19726. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, NULL, endian),
  19727. BAD_FUNC_ARG);
  19728. outLen = outLen - 2;
  19729. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian),
  19730. ECC_BAD_ARG_E);
  19731. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19732. wc_curve25519_free(&key);
  19733. #endif
  19734. return EXPECT_RESULT();
  19735. } /* END test_wc_curve25519_export_public_ex*/
  19736. /*
  19737. * Testing test_wc_curve25519_import_private_raw_ex
  19738. */
  19739. static int test_wc_curve25519_import_private_raw_ex(void)
  19740. {
  19741. EXPECT_DECLS;
  19742. #if defined(HAVE_CURVE25519)
  19743. curve25519_key key;
  19744. WC_RNG rng;
  19745. byte priv[CURVE25519_KEYSIZE];
  19746. byte pub[CURVE25519_KEYSIZE];
  19747. word32 privSz = sizeof(priv);
  19748. word32 pubSz = sizeof(pub);
  19749. int endian = EC25519_BIG_ENDIAN;
  19750. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19751. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19752. ExpectIntEQ(wc_InitRng(&rng), 0);
  19753. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19754. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, priv, &privSz,
  19755. endian), 0);
  19756. ExpectIntEQ(wc_curve25519_export_public(&key, pub, &pubSz), 0);
  19757. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  19758. &key, endian), 0);
  19759. /* test bad cases*/
  19760. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  19761. endian), BAD_FUNC_ARG);
  19762. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  19763. &key, endian), BAD_FUNC_ARG);
  19764. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  19765. &key, endian), BAD_FUNC_ARG);
  19766. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  19767. NULL, endian), BAD_FUNC_ARG);
  19768. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  19769. &key, endian), ECC_BAD_ARG_E);
  19770. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  19771. &key, endian), ECC_BAD_ARG_E);
  19772. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  19773. &key, EC25519_LITTLE_ENDIAN), 0);
  19774. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19775. wc_curve25519_free(&key);
  19776. #endif
  19777. return EXPECT_RESULT();
  19778. } /* END test_wc_curve25519_import_private_raw_ex*/
  19779. /*
  19780. * Testing test_wc_curve25519_import_private
  19781. */
  19782. static int test_wc_curve25519_import_private(void)
  19783. {
  19784. EXPECT_DECLS;
  19785. #if defined(HAVE_CURVE25519)
  19786. curve25519_key key;
  19787. WC_RNG rng;
  19788. byte priv[CURVE25519_KEYSIZE];
  19789. word32 privSz = sizeof(priv);
  19790. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19791. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19792. ExpectIntEQ(wc_InitRng(&rng), 0);
  19793. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19794. ExpectIntEQ(wc_curve25519_export_private_raw(&key, priv, &privSz), 0);
  19795. ExpectIntEQ(wc_curve25519_import_private(priv, privSz, &key), 0);
  19796. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19797. wc_curve25519_free(&key);
  19798. #endif
  19799. return EXPECT_RESULT();
  19800. } /* END test_wc_curve25519_import*/
  19801. /*
  19802. * Testing test_wc_curve25519_export_private_raw_ex
  19803. */
  19804. static int test_wc_curve25519_export_private_raw_ex(void)
  19805. {
  19806. EXPECT_DECLS;
  19807. #if defined(HAVE_CURVE25519)
  19808. curve25519_key key;
  19809. byte out[CURVE25519_KEYSIZE];
  19810. word32 outLen = sizeof(out);
  19811. int endian = EC25519_BIG_ENDIAN;
  19812. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19813. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  19814. 0);
  19815. /* test bad cases*/
  19816. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian),
  19817. BAD_FUNC_ARG);
  19818. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian),
  19819. BAD_FUNC_ARG);
  19820. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, NULL, &outLen,
  19821. endian), BAD_FUNC_ARG);
  19822. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, NULL, endian),
  19823. BAD_FUNC_ARG);
  19824. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  19825. EC25519_LITTLE_ENDIAN), 0);
  19826. outLen = outLen - 2;
  19827. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  19828. ECC_BAD_ARG_E);
  19829. wc_curve25519_free(&key);
  19830. #endif
  19831. return EXPECT_RESULT();
  19832. } /* END test_wc_curve25519_export_private_raw_ex*/
  19833. /*
  19834. * Testing wc_ed448_make_key().
  19835. */
  19836. static int test_wc_ed448_make_key(void)
  19837. {
  19838. EXPECT_DECLS;
  19839. #if defined(HAVE_ED448)
  19840. ed448_key key;
  19841. WC_RNG rng;
  19842. unsigned char pubkey[ED448_PUB_KEY_SIZE];
  19843. XMEMSET(&key, 0, sizeof(ed448_key));
  19844. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19845. ExpectIntEQ(wc_ed448_init(&key), 0);
  19846. ExpectIntEQ(wc_InitRng(&rng), 0);
  19847. ExpectIntEQ(wc_ed448_make_public(&key, pubkey, sizeof(pubkey)),
  19848. ECC_PRIV_KEY_E);
  19849. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  19850. /* Test bad args. */
  19851. ExpectIntEQ(wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key), BAD_FUNC_ARG);
  19852. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL), BAD_FUNC_ARG);
  19853. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key),
  19854. BAD_FUNC_ARG);
  19855. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key),
  19856. BAD_FUNC_ARG);
  19857. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19858. wc_ed448_free(&key);
  19859. #endif
  19860. return EXPECT_RESULT();
  19861. } /* END test_wc_ed448_make_key */
  19862. /*
  19863. * Testing wc_ed448_init()
  19864. */
  19865. static int test_wc_ed448_init(void)
  19866. {
  19867. EXPECT_DECLS;
  19868. #if defined(HAVE_ED448)
  19869. ed448_key key;
  19870. XMEMSET(&key, 0, sizeof(ed448_key));
  19871. ExpectIntEQ(wc_ed448_init(&key), 0);
  19872. /* Test bad args. */
  19873. ExpectIntEQ(wc_ed448_init(NULL), BAD_FUNC_ARG);
  19874. wc_ed448_free(&key);
  19875. #endif
  19876. return EXPECT_RESULT();
  19877. } /* END test_wc_ed448_init */
  19878. /*
  19879. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  19880. */
  19881. static int test_wc_ed448_sign_msg(void)
  19882. {
  19883. EXPECT_DECLS;
  19884. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  19885. ed448_key key;
  19886. WC_RNG rng;
  19887. byte msg[] = "Everybody gets Friday off.\n";
  19888. byte sig[ED448_SIG_SIZE];
  19889. word32 msglen = sizeof(msg);
  19890. word32 siglen = sizeof(sig);
  19891. word32 badSigLen = sizeof(sig) - 1;
  19892. #ifdef HAVE_ED448_VERIFY
  19893. int verify_ok = 0; /*1 = Verify success.*/
  19894. #endif
  19895. /* Initialize stack variables. */
  19896. XMEMSET(&key, 0, sizeof(ed448_key));
  19897. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19898. XMEMSET(sig, 0, siglen);
  19899. /* Initialize key. */
  19900. ExpectIntEQ(wc_ed448_init(&key), 0);
  19901. ExpectIntEQ(wc_InitRng(&rng), 0);
  19902. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  19903. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0), 0);
  19904. ExpectIntEQ(siglen, ED448_SIG_SIZE);
  19905. /* Test bad args. */
  19906. ExpectIntEQ(wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0),
  19907. BAD_FUNC_ARG);
  19908. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0),
  19909. BAD_FUNC_ARG);
  19910. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0),
  19911. BAD_FUNC_ARG);
  19912. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0),
  19913. BAD_FUNC_ARG);
  19914. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key, NULL, 0),
  19915. BUFFER_E);
  19916. ExpectIntEQ(badSigLen, ED448_SIG_SIZE);
  19917. badSigLen -= 1;
  19918. #ifdef HAVE_ED448_VERIFY
  19919. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key,
  19920. NULL, 0), 0);
  19921. ExpectIntEQ(verify_ok, 1);
  19922. /* Test bad args. */
  19923. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg, msglen, &verify_ok,
  19924. &key, NULL, 0), BAD_FUNC_ARG);
  19925. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg, msglen, &verify_ok,
  19926. &key, NULL, 0), BAD_FUNC_ARG);
  19927. ExpectIntEQ(wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  19928. &key, NULL, 0), BAD_FUNC_ARG);
  19929. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, NULL, msglen, &verify_ok,
  19930. &key, NULL, 0), BAD_FUNC_ARG);
  19931. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, NULL,
  19932. &key, NULL, 0), BAD_FUNC_ARG);
  19933. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  19934. NULL, NULL, 0), BAD_FUNC_ARG);
  19935. ExpectIntEQ(wc_ed448_verify_msg(sig, badSigLen, msg, msglen, &verify_ok,
  19936. &key, NULL, 0), BAD_FUNC_ARG);
  19937. #endif /* Verify. */
  19938. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19939. wc_ed448_free(&key);
  19940. #endif
  19941. return EXPECT_RESULT();
  19942. } /* END test_wc_ed448_sign_msg */
  19943. /*
  19944. * Testing wc_ed448_import_public()
  19945. */
  19946. static int test_wc_ed448_import_public(void)
  19947. {
  19948. EXPECT_DECLS;
  19949. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  19950. ed448_key pubKey;
  19951. WC_RNG rng;
  19952. const byte in[] =
  19953. "Ed448PublicKeyUnitTest.................................\n";
  19954. word32 inlen = sizeof(in);
  19955. XMEMSET(&pubKey, 0, sizeof(ed448_key));
  19956. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19957. ExpectIntEQ(wc_ed448_init(&pubKey), 0);
  19958. ExpectIntEQ(wc_InitRng(&rng), 0);
  19959. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey), 0);
  19960. ExpectIntEQ(wc_ed448_import_public_ex(in, inlen, &pubKey, 1), 0);
  19961. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  19962. /* Test bad args. */
  19963. ExpectIntEQ(wc_ed448_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  19964. ExpectIntEQ(wc_ed448_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  19965. ExpectIntEQ(wc_ed448_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  19966. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19967. wc_ed448_free(&pubKey);
  19968. #endif
  19969. return EXPECT_RESULT();
  19970. } /* END wc_ed448_import_public */
  19971. /*
  19972. * Testing wc_ed448_import_private_key()
  19973. */
  19974. static int test_wc_ed448_import_private_key(void)
  19975. {
  19976. EXPECT_DECLS;
  19977. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  19978. ed448_key key;
  19979. WC_RNG rng;
  19980. const byte privKey[] =
  19981. "Ed448PrivateKeyUnitTest................................\n";
  19982. const byte pubKey[] =
  19983. "Ed448PublicKeyUnitTest.................................\n";
  19984. word32 privKeySz = sizeof(privKey);
  19985. word32 pubKeySz = sizeof(pubKey);
  19986. #ifdef HAVE_ED448_KEY_EXPORT
  19987. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  19988. word32 bothKeysSz = sizeof(bothKeys);
  19989. #endif
  19990. XMEMSET(&key, 0, sizeof(ed448_key));
  19991. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19992. ExpectIntEQ(wc_ed448_init(&key), 0);
  19993. ExpectIntEQ(wc_InitRng(&rng), 0);
  19994. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  19995. ExpectIntEQ(wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  19996. pubKeySz, &key, 1), 0);
  19997. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  19998. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  19999. #ifdef HAVE_ED448_KEY_EXPORT
  20000. ExpectIntEQ(wc_ed448_export_private(&key, bothKeys, &bothKeysSz), 0);
  20001. ExpectIntEQ(wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  20002. &key, 1), 0);
  20003. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20004. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20005. #endif
  20006. /* Test bad args. */
  20007. ExpectIntEQ(wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  20008. &key), BAD_FUNC_ARG);
  20009. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, pubKeySz,
  20010. &key), BAD_FUNC_ARG);
  20011. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20012. pubKeySz, NULL), BAD_FUNC_ARG);
  20013. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  20014. pubKeySz, &key), BAD_FUNC_ARG);
  20015. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20016. pubKeySz - 1, &key), BAD_FUNC_ARG);
  20017. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, 0, &key),
  20018. BAD_FUNC_ARG);
  20019. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20020. wc_ed448_free(&key);
  20021. #endif
  20022. return EXPECT_RESULT();
  20023. } /* END test_wc_ed448_import_private_key */
  20024. /*
  20025. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  20026. */
  20027. static int test_wc_ed448_export(void)
  20028. {
  20029. EXPECT_DECLS;
  20030. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20031. ed448_key key;
  20032. WC_RNG rng;
  20033. byte priv[ED448_PRV_KEY_SIZE];
  20034. byte pub[ED448_PUB_KEY_SIZE];
  20035. word32 privSz = sizeof(priv);
  20036. word32 pubSz = sizeof(pub);
  20037. XMEMSET(&key, 0, sizeof(ed448_key));
  20038. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20039. ExpectIntEQ(wc_ed448_init(&key), 0);
  20040. ExpectIntEQ(wc_InitRng(&rng), 0);
  20041. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20042. ExpectIntEQ(wc_ed448_export_public(&key, pub, &pubSz), 0);
  20043. ExpectIntEQ(pubSz, ED448_KEY_SIZE);
  20044. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  20045. /* Test bad args. */
  20046. ExpectIntEQ(wc_ed448_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  20047. ExpectIntEQ(wc_ed448_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  20048. ExpectIntEQ(wc_ed448_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  20049. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, &privSz), 0);
  20050. ExpectIntEQ(privSz, ED448_KEY_SIZE);
  20051. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  20052. /* Test bad args. */
  20053. ExpectIntEQ(wc_ed448_export_private_only(NULL, priv, &privSz),
  20054. BAD_FUNC_ARG);
  20055. ExpectIntEQ(wc_ed448_export_private_only(&key, NULL, &privSz),
  20056. BAD_FUNC_ARG);
  20057. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, NULL), BAD_FUNC_ARG);
  20058. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20059. wc_ed448_free(&key);
  20060. #endif
  20061. return EXPECT_RESULT();
  20062. } /* END test_wc_ed448_export */
  20063. /*
  20064. * Testing wc_ed448_size()
  20065. */
  20066. static int test_wc_ed448_size(void)
  20067. {
  20068. EXPECT_DECLS;
  20069. #if defined(HAVE_ED448)
  20070. ed448_key key;
  20071. WC_RNG rng;
  20072. XMEMSET(&key, 0, sizeof(ed448_key));
  20073. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20074. ExpectIntEQ(wc_ed448_init(&key), 0);
  20075. ExpectIntEQ(wc_InitRng(&rng), 0);
  20076. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20077. ExpectIntEQ(wc_ed448_size(&key), ED448_KEY_SIZE);
  20078. /* Test bad args. */
  20079. ExpectIntEQ(wc_ed448_size(NULL), BAD_FUNC_ARG);
  20080. ExpectIntEQ(wc_ed448_sig_size(&key), ED448_SIG_SIZE);
  20081. /* Test bad args. */
  20082. ExpectIntEQ(wc_ed448_sig_size(NULL), BAD_FUNC_ARG);
  20083. ExpectIntEQ(wc_ed448_pub_size(&key), ED448_PUB_KEY_SIZE);
  20084. /* Test bad args. */
  20085. ExpectIntEQ(wc_ed448_pub_size(NULL), BAD_FUNC_ARG);
  20086. ExpectIntEQ(wc_ed448_priv_size(&key), ED448_PRV_KEY_SIZE);
  20087. /* Test bad args. */
  20088. ExpectIntEQ(wc_ed448_priv_size(NULL), BAD_FUNC_ARG);
  20089. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20090. wc_ed448_free(&key);
  20091. #endif
  20092. return EXPECT_RESULT();
  20093. } /* END test_wc_ed448_size */
  20094. /*
  20095. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  20096. */
  20097. static int test_wc_ed448_exportKey(void)
  20098. {
  20099. EXPECT_DECLS;
  20100. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20101. ed448_key key;
  20102. WC_RNG rng;
  20103. byte priv[ED448_PRV_KEY_SIZE];
  20104. byte pub[ED448_PUB_KEY_SIZE];
  20105. byte privOnly[ED448_PRV_KEY_SIZE];
  20106. word32 privSz = sizeof(priv);
  20107. word32 pubSz = sizeof(pub);
  20108. word32 privOnlySz = sizeof(privOnly);
  20109. XMEMSET(&key, 0, sizeof(ed448_key));
  20110. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20111. ExpectIntEQ(wc_ed448_init(&key), 0);
  20112. ExpectIntEQ(wc_InitRng(&rng), 0);
  20113. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20114. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, &privOnlySz), 0);
  20115. /* Test bad args. */
  20116. ExpectIntEQ(wc_ed448_export_private(NULL, privOnly, &privOnlySz),
  20117. BAD_FUNC_ARG);
  20118. ExpectIntEQ(wc_ed448_export_private(&key, NULL, &privOnlySz), BAD_FUNC_ARG);
  20119. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  20120. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  20121. /* Test bad args. */
  20122. ExpectIntEQ(wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz),
  20123. BAD_FUNC_ARG);
  20124. ExpectIntEQ(wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz),
  20125. BAD_FUNC_ARG);
  20126. ExpectIntEQ(wc_ed448_export_key(&key, priv, NULL, pub, &pubSz),
  20127. BAD_FUNC_ARG);
  20128. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz),
  20129. BAD_FUNC_ARG);
  20130. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, NULL),
  20131. BAD_FUNC_ARG);
  20132. /* Cross check output. */
  20133. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  20134. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20135. wc_ed448_free(&key);
  20136. #endif
  20137. return EXPECT_RESULT();
  20138. } /* END test_wc_ed448_exportKey */
  20139. /*
  20140. * Testing wc_Ed448PublicKeyToDer
  20141. */
  20142. static int test_wc_Ed448PublicKeyToDer(void)
  20143. {
  20144. EXPECT_DECLS;
  20145. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  20146. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20147. ed448_key key;
  20148. byte derBuf[1024];
  20149. XMEMSET(&key, 0, sizeof(ed448_key));
  20150. /* Test bad args */
  20151. ExpectIntEQ(wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  20152. ExpectIntEQ(wc_ed448_init(&key), 0);
  20153. ExpectIntEQ(wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  20154. wc_ed448_free(&key);
  20155. /* Test good args */
  20156. if (EXPECT_SUCCESS()) {
  20157. WC_RNG rng;
  20158. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20159. ExpectIntEQ(wc_ed448_init(&key), 0);
  20160. ExpectIntEQ(wc_InitRng(&rng), 0);
  20161. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20162. ExpectIntGT(wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  20163. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20164. wc_ed448_free(&key);
  20165. }
  20166. #endif
  20167. return EXPECT_RESULT();
  20168. } /* END testing wc_Ed448PublicKeyToDer */
  20169. /*
  20170. * Testing wc_curve448_init and wc_curve448_free.
  20171. */
  20172. static int test_wc_curve448_init(void)
  20173. {
  20174. EXPECT_DECLS;
  20175. #if defined(HAVE_CURVE448)
  20176. curve448_key key;
  20177. /* Test bad args for wc_curve448_init */
  20178. ExpectIntEQ(wc_curve448_init(&key), 0);
  20179. /* Test bad args for wc_curve448_init */
  20180. ExpectIntEQ(wc_curve448_init(NULL), BAD_FUNC_ARG);
  20181. /* Test good args for wc_curve_448_free */
  20182. wc_curve448_free(&key);
  20183. /* Test bad args for wc_curve448_free */
  20184. wc_curve448_free(NULL);
  20185. #endif
  20186. return EXPECT_RESULT();
  20187. } /* END test_wc_curve448_init and wc_curve_448_free*/
  20188. /*
  20189. * Testing wc_curve448_make_key
  20190. */
  20191. static int test_wc_curve448_make_key(void)
  20192. {
  20193. EXPECT_DECLS;
  20194. #if defined(HAVE_CURVE448)
  20195. curve448_key key;
  20196. WC_RNG rng;
  20197. int keysize;
  20198. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20199. ExpectIntEQ(wc_curve448_init(&key), 0);
  20200. ExpectIntEQ(wc_InitRng(&rng), 0);
  20201. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20202. ExpectIntEQ(keysize = wc_curve448_size(&key), CURVE448_KEY_SIZE);
  20203. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, &key), 0);
  20204. /* test bad cases */
  20205. ExpectIntEQ(wc_curve448_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  20206. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  20207. ExpectIntEQ(wc_curve448_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  20208. ExpectIntEQ(wc_curve448_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  20209. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20210. wc_curve448_free(&key);
  20211. #endif
  20212. return EXPECT_RESULT();
  20213. } /* END test_wc_curve448_make_key*/
  20214. /*
  20215. * Testing test_wc_curve448_shared_secret_ex
  20216. */
  20217. static int test_wc_curve448_shared_secret_ex(void)
  20218. {
  20219. EXPECT_DECLS;
  20220. #if defined(HAVE_CURVE448)
  20221. curve448_key private_key;
  20222. curve448_key public_key;
  20223. WC_RNG rng;
  20224. byte out[CURVE448_KEY_SIZE];
  20225. word32 outLen = sizeof(out);
  20226. int endian = EC448_BIG_ENDIAN;
  20227. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20228. ExpectIntEQ(wc_curve448_init(&private_key), 0);
  20229. ExpectIntEQ(wc_InitRng(&rng), 0);
  20230. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key), 0);
  20231. ExpectIntEQ(wc_curve448_init(&public_key), 0);
  20232. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key), 0);
  20233. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20234. &outLen, endian), 0);
  20235. /* test bad cases */
  20236. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  20237. BAD_FUNC_ARG);
  20238. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, &public_key, out, &outLen,
  20239. endian), BAD_FUNC_ARG);
  20240. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, NULL, out, &outLen,
  20241. endian), BAD_FUNC_ARG);
  20242. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  20243. &outLen, endian), BAD_FUNC_ARG);
  20244. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20245. NULL, endian), BAD_FUNC_ARG);
  20246. outLen = outLen - 2;
  20247. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20248. &outLen, endian), BAD_FUNC_ARG);
  20249. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20250. wc_curve448_free(&private_key);
  20251. wc_curve448_free(&public_key);
  20252. #endif
  20253. return EXPECT_RESULT();
  20254. } /* END test_wc_curve448_shared_secret_ex*/
  20255. /*
  20256. * Testing test_wc_curve448_export_public_ex
  20257. */
  20258. static int test_wc_curve448_export_public_ex(void)
  20259. {
  20260. EXPECT_DECLS;
  20261. #if defined(HAVE_CURVE448)
  20262. WC_RNG rng;
  20263. curve448_key key;
  20264. byte out[CURVE448_KEY_SIZE];
  20265. word32 outLen = sizeof(out);
  20266. int endian = EC448_BIG_ENDIAN;
  20267. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20268. ExpectIntEQ(wc_curve448_init(&key), 0);
  20269. ExpectIntEQ(wc_InitRng(&rng), 0);
  20270. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20271. ExpectIntEQ(wc_curve448_export_public(&key, out, &outLen), 0);
  20272. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian), 0);
  20273. /* test bad cases*/
  20274. ExpectIntEQ(wc_curve448_export_public_ex(NULL, NULL, NULL, endian),
  20275. BAD_FUNC_ARG);
  20276. ExpectIntEQ(wc_curve448_export_public_ex(NULL, out, &outLen, endian),
  20277. BAD_FUNC_ARG);
  20278. ExpectIntEQ(wc_curve448_export_public_ex(&key, NULL, &outLen, endian),
  20279. BAD_FUNC_ARG);
  20280. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, NULL, endian),
  20281. BAD_FUNC_ARG);
  20282. outLen = outLen - 2;
  20283. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian),
  20284. ECC_BAD_ARG_E);
  20285. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20286. wc_curve448_free(&key);
  20287. #endif
  20288. return EXPECT_RESULT();
  20289. } /* END test_wc_curve448_export_public_ex*/
  20290. /*
  20291. * Testing test_wc_curve448_export_private_raw_ex
  20292. */
  20293. static int test_wc_curve448_export_private_raw_ex(void)
  20294. {
  20295. EXPECT_DECLS;
  20296. #if defined(HAVE_CURVE448)
  20297. curve448_key key;
  20298. byte out[CURVE448_KEY_SIZE];
  20299. word32 outLen = sizeof(out);
  20300. int endian = EC448_BIG_ENDIAN;
  20301. ExpectIntEQ(wc_curve448_init(&key), 0);
  20302. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  20303. 0);
  20304. /* test bad cases*/
  20305. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian),
  20306. BAD_FUNC_ARG);
  20307. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian),
  20308. BAD_FUNC_ARG);
  20309. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian),
  20310. BAD_FUNC_ARG);
  20311. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, NULL, endian),
  20312. BAD_FUNC_ARG);
  20313. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen,
  20314. EC448_LITTLE_ENDIAN), 0);
  20315. outLen = outLen - 2;
  20316. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  20317. ECC_BAD_ARG_E);
  20318. wc_curve448_free(&key);
  20319. #endif
  20320. return EXPECT_RESULT();
  20321. } /* END test_wc_curve448_export_private_raw_ex*/
  20322. /*
  20323. * Testing test_wc_curve448_import_private_raw_ex
  20324. */
  20325. static int test_wc_curve448_import_private_raw_ex(void)
  20326. {
  20327. EXPECT_DECLS;
  20328. #if defined(HAVE_CURVE448)
  20329. curve448_key key;
  20330. WC_RNG rng;
  20331. byte priv[CURVE448_KEY_SIZE];
  20332. byte pub[CURVE448_KEY_SIZE];
  20333. word32 privSz = sizeof(priv);
  20334. word32 pubSz = sizeof(pub);
  20335. int endian = EC448_BIG_ENDIAN;
  20336. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20337. ExpectIntEQ(wc_curve448_init(&key), 0);
  20338. ExpectIntEQ(wc_InitRng(&rng), 0);
  20339. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20340. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  20341. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  20342. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20343. &key, endian), 0);
  20344. /* test bad cases */
  20345. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0),
  20346. BAD_FUNC_ARG);
  20347. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20348. &key, endian), BAD_FUNC_ARG);
  20349. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20350. &key, endian), BAD_FUNC_ARG);
  20351. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20352. NULL, endian), BAD_FUNC_ARG);
  20353. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  20354. &key, endian), ECC_BAD_ARG_E);
  20355. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  20356. &key, endian), ECC_BAD_ARG_E);
  20357. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20358. &key, EC448_LITTLE_ENDIAN), 0);
  20359. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20360. wc_curve448_free(&key);
  20361. #endif
  20362. return EXPECT_RESULT();
  20363. } /* END test_wc_curve448_import_private_raw_ex*/
  20364. /*
  20365. * Testing test_curve448_export_key_raw
  20366. */
  20367. static int test_wc_curve448_export_key_raw(void)
  20368. {
  20369. EXPECT_DECLS;
  20370. #if defined(HAVE_CURVE448)
  20371. curve448_key key;
  20372. WC_RNG rng;
  20373. byte priv[CURVE448_KEY_SIZE];
  20374. byte pub[CURVE448_KEY_SIZE];
  20375. word32 privSz = sizeof(priv);
  20376. word32 pubSz = sizeof(pub);
  20377. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20378. ExpectIntEQ(wc_curve448_init(&key), 0);
  20379. ExpectIntEQ(wc_InitRng(&rng), 0);
  20380. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20381. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  20382. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  20383. ExpectIntEQ(wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz),
  20384. 0);
  20385. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20386. wc_curve448_free(&key);
  20387. #endif
  20388. return EXPECT_RESULT();
  20389. } /* END test_wc_curve448_import_private_raw_ex*/
  20390. /*
  20391. * Testing test_wc_curve448_import_private
  20392. */
  20393. static int test_wc_curve448_import_private(void)
  20394. {
  20395. EXPECT_DECLS;
  20396. #if defined(HAVE_CURVE448)
  20397. curve448_key key;
  20398. WC_RNG rng;
  20399. byte priv[CURVE448_KEY_SIZE];
  20400. word32 privSz = sizeof(priv);
  20401. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20402. ExpectIntEQ(wc_curve448_init(&key), 0);
  20403. ExpectIntEQ(wc_InitRng(&rng), 0);
  20404. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20405. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  20406. ExpectIntEQ(wc_curve448_import_private(priv, privSz, &key), 0);
  20407. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20408. wc_curve448_free(&key);
  20409. #endif
  20410. return EXPECT_RESULT();
  20411. } /* END test_wc_curve448_import*/
  20412. /*
  20413. * Testing test_wc_curve448_size.
  20414. */
  20415. static int test_wc_curve448_size(void)
  20416. {
  20417. EXPECT_DECLS;
  20418. #if defined(HAVE_CURVE448)
  20419. curve448_key key;
  20420. ExpectIntEQ(wc_curve448_init(&key), 0);
  20421. /* Test good args for wc_curve448_size */
  20422. ExpectIntEQ(wc_curve448_size(&key), CURVE448_KEY_SIZE);
  20423. /* Test bad args for wc_curve448_size */
  20424. ExpectIntEQ(wc_curve448_size(NULL), 0);
  20425. wc_curve448_free(&key);
  20426. #endif
  20427. return EXPECT_RESULT();
  20428. } /* END test_wc_curve448_size*/
  20429. /*
  20430. * Testing wc_ecc_make_key.
  20431. */
  20432. static int test_wc_ecc_make_key(void)
  20433. {
  20434. EXPECT_DECLS;
  20435. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20436. ecc_key key;
  20437. WC_RNG rng;
  20438. int ret;
  20439. XMEMSET(&key, 0, sizeof(ecc_key));
  20440. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20441. ExpectIntEQ(wc_ecc_init(&key), 0);
  20442. ExpectIntEQ(wc_InitRng(&rng), 0);
  20443. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20444. #if defined(WOLFSSL_ASYNC_CRYPT)
  20445. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20446. #endif
  20447. ExpectIntEQ(ret, 0);
  20448. /* Pass in bad args. */
  20449. ExpectIntEQ(wc_ecc_make_key(NULL, KEY14, &key), BAD_FUNC_ARG);
  20450. ExpectIntEQ(wc_ecc_make_key(&rng, KEY14, NULL), BAD_FUNC_ARG);
  20451. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20452. wc_ecc_free(&key);
  20453. #ifdef FP_ECC
  20454. wc_ecc_fp_free();
  20455. #endif
  20456. #endif
  20457. return EXPECT_RESULT();
  20458. } /* END test_wc_ecc_make_key */
  20459. /*
  20460. * Testing wc_ecc_init()
  20461. */
  20462. static int test_wc_ecc_init(void)
  20463. {
  20464. EXPECT_DECLS;
  20465. #ifdef HAVE_ECC
  20466. ecc_key key;
  20467. XMEMSET(&key, 0, sizeof(ecc_key));
  20468. ExpectIntEQ(wc_ecc_init(&key), 0);
  20469. /* Pass in bad args. */
  20470. ExpectIntEQ(wc_ecc_init(NULL), BAD_FUNC_ARG);
  20471. wc_ecc_free(&key);
  20472. #endif
  20473. return EXPECT_RESULT();
  20474. } /* END test_wc_ecc_init */
  20475. /*
  20476. * Testing wc_ecc_check_key()
  20477. */
  20478. static int test_wc_ecc_check_key(void)
  20479. {
  20480. EXPECT_DECLS;
  20481. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20482. ecc_key key;
  20483. WC_RNG rng;
  20484. int ret;
  20485. XMEMSET(&rng, 0, sizeof(rng));
  20486. XMEMSET(&key, 0, sizeof(key));
  20487. ExpectIntEQ(wc_ecc_init(&key), 0);
  20488. ExpectIntEQ(wc_InitRng(&rng), 0);
  20489. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20490. #if defined(WOLFSSL_ASYNC_CRYPT)
  20491. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20492. #endif
  20493. ExpectIntEQ(ret, 0);
  20494. ExpectIntEQ(wc_ecc_check_key(&key), 0);
  20495. /* Pass in bad args. */
  20496. ExpectIntEQ(wc_ecc_check_key(NULL), BAD_FUNC_ARG);
  20497. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20498. wc_ecc_free(&key);
  20499. #ifdef FP_ECC
  20500. wc_ecc_fp_free();
  20501. #endif
  20502. #endif
  20503. return EXPECT_RESULT();
  20504. } /* END test_wc_ecc_check_key */
  20505. /*
  20506. * Testing wc_ecc_get_generator()
  20507. */
  20508. static int test_wc_ecc_get_generator(void)
  20509. {
  20510. EXPECT_DECLS;
  20511. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  20512. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  20513. ecc_point* pt = NULL;
  20514. ExpectNotNull(pt = wc_ecc_new_point());
  20515. ExpectIntEQ(wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  20516. MP_OKAY);
  20517. /* Test bad args. */
  20518. /* Returns Zero for bad arg. */
  20519. ExpectIntNE(wc_ecc_get_generator(pt, -1), MP_OKAY);
  20520. ExpectIntNE(wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  20521. MP_OKAY);
  20522. /* If we ever get to 1000 curves increase this number */
  20523. ExpectIntNE(wc_ecc_get_generator(pt, 1000), MP_OKAY);
  20524. ExpectIntNE(wc_ecc_get_generator(NULL, -1), MP_OKAY);
  20525. wc_ecc_del_point(pt);
  20526. #endif
  20527. return EXPECT_RESULT();
  20528. } /* END test_wc_ecc_get_generator */
  20529. /*
  20530. * Testing wc_ecc_size()
  20531. */
  20532. static int test_wc_ecc_size(void)
  20533. {
  20534. EXPECT_DECLS;
  20535. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20536. WC_RNG rng;
  20537. ecc_key key;
  20538. int ret;
  20539. XMEMSET(&key, 0, sizeof(ecc_key));
  20540. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20541. ExpectIntEQ(wc_ecc_init(&key), 0);
  20542. ExpectIntEQ(wc_InitRng(&rng), 0);
  20543. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20544. #if defined(WOLFSSL_ASYNC_CRYPT)
  20545. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20546. #endif
  20547. ExpectIntEQ(ret, 0);
  20548. ExpectIntEQ(wc_ecc_size(&key), KEY14);
  20549. /* Test bad args. */
  20550. /* Returns Zero for bad arg. */
  20551. ExpectIntEQ(wc_ecc_size(NULL), 0);
  20552. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20553. wc_ecc_free(&key);
  20554. #endif
  20555. return EXPECT_RESULT();
  20556. } /* END test_wc_ecc_size */
  20557. static int test_wc_ecc_params(void)
  20558. {
  20559. EXPECT_DECLS;
  20560. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  20561. It was added after certifications */
  20562. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  20563. const ecc_set_type* ecc_set;
  20564. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  20565. /* Test for SECP256R1 curve */
  20566. int curve_id = ECC_SECP256R1;
  20567. int curve_idx;
  20568. ExpectIntNE(curve_idx = wc_ecc_get_curve_idx(curve_id), ECC_CURVE_INVALID);
  20569. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(curve_idx));
  20570. ExpectIntEQ(ecc_set->id, curve_id);
  20571. #endif
  20572. /* Test case when SECP256R1 is not enabled */
  20573. /* Test that we get curve params for index 0 */
  20574. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(0));
  20575. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  20576. return EXPECT_RESULT();
  20577. }
  20578. /*
  20579. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  20580. */
  20581. static int test_wc_ecc_signVerify_hash(void)
  20582. {
  20583. EXPECT_DECLS;
  20584. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  20585. ecc_key key;
  20586. WC_RNG rng;
  20587. int ret;
  20588. #ifdef HAVE_ECC_VERIFY
  20589. int verify = 0;
  20590. #endif
  20591. word32 siglen = ECC_BUFSIZE;
  20592. byte sig[ECC_BUFSIZE];
  20593. byte adjustedSig[ECC_BUFSIZE+1];
  20594. byte digest[] = TEST_STRING;
  20595. word32 digestlen = (word32)TEST_STRING_SZ;
  20596. /* Init stack var */
  20597. XMEMSET(&key, 0, sizeof(ecc_key));
  20598. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20599. XMEMSET(sig, 0, siglen);
  20600. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  20601. /* Init structs. */
  20602. ExpectIntEQ(wc_ecc_init(&key), 0);
  20603. ExpectIntEQ(wc_InitRng(&rng), 0);
  20604. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20605. #if defined(WOLFSSL_ASYNC_CRYPT)
  20606. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20607. #endif
  20608. ExpectIntEQ(ret, 0);
  20609. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key),
  20610. 0);
  20611. /* Check bad args. */
  20612. ExpectIntEQ(wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key),
  20613. ECC_BAD_ARG_E);
  20614. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, NULL, &siglen, &rng, &key),
  20615. ECC_BAD_ARG_E);
  20616. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, NULL, &rng, &key),
  20617. ECC_BAD_ARG_E);
  20618. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, NULL, &key),
  20619. ECC_BAD_ARG_E);
  20620. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, NULL),
  20621. ECC_BAD_ARG_E);
  20622. #ifdef HAVE_ECC_VERIFY
  20623. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  20624. &key), 0);
  20625. ExpectIntEQ(verify, 1);
  20626. /* test check on length of signature passed in */
  20627. XMEMCPY(adjustedSig, sig, siglen);
  20628. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  20629. #ifndef NO_STRICT_ECDSA_LEN
  20630. ExpectIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  20631. &verify, &key), 0);
  20632. #else
  20633. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  20634. * is allowed */
  20635. ExpectIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  20636. &verify, &key), 0);
  20637. #endif
  20638. /* Test bad args. */
  20639. ExpectIntEQ(wc_ecc_verify_hash(NULL, siglen, digest, digestlen, &verify,
  20640. &key), ECC_BAD_ARG_E);
  20641. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, NULL, digestlen, &verify, &key),
  20642. ECC_BAD_ARG_E);
  20643. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, NULL, &key),
  20644. ECC_BAD_ARG_E);
  20645. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  20646. NULL), ECC_BAD_ARG_E);
  20647. #endif /* HAVE_ECC_VERIFY */
  20648. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20649. wc_ecc_free(&key);
  20650. #ifdef FP_ECC
  20651. wc_ecc_fp_free();
  20652. #endif
  20653. #endif
  20654. return EXPECT_RESULT();
  20655. } /* END test_wc_ecc_sign_hash */
  20656. /*
  20657. * Testing wc_ecc_shared_secret()
  20658. */
  20659. static int test_wc_ecc_shared_secret(void)
  20660. {
  20661. EXPECT_DECLS;
  20662. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  20663. ecc_key key;
  20664. ecc_key pubKey;
  20665. WC_RNG rng;
  20666. #if defined(NO_ECC256)
  20667. int ret;
  20668. #endif
  20669. byte out[KEY32];
  20670. int keySz = sizeof(out);
  20671. word32 outlen = (word32)sizeof(out);
  20672. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  20673. const char* qx =
  20674. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  20675. const char* qy =
  20676. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  20677. const char* d =
  20678. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  20679. const char* curveName = "SECP256R1";
  20680. const byte expected_shared_secret[] =
  20681. {
  20682. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  20683. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  20684. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  20685. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  20686. };
  20687. #endif
  20688. PRIVATE_KEY_UNLOCK();
  20689. /* Initialize variables. */
  20690. XMEMSET(&key, 0, sizeof(ecc_key));
  20691. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  20692. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20693. XMEMSET(out, 0, keySz);
  20694. ExpectIntEQ(wc_ecc_init(&key), 0);
  20695. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  20696. ExpectIntEQ(wc_InitRng(&rng), 0);
  20697. #if !defined(NO_ECC256)
  20698. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  20699. ExpectIntEQ(wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName), 0);
  20700. #else
  20701. ret = wc_ecc_make_key(&rng, keySz, &key);
  20702. #if defined(WOLFSSL_ASYNC_CRYPT)
  20703. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20704. #endif
  20705. ExpectIntEQ(ret, 0);
  20706. ret = wc_ecc_make_key(&rng, keySz, &key);
  20707. #if defined(WOLFSSL_ASYNC_CRYPT)
  20708. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20709. #endif
  20710. ExpectIntEQ(ret, 0);
  20711. #endif
  20712. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20713. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20714. !defined(HAVE_SELFTEST)
  20715. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  20716. #endif
  20717. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen), 0);
  20718. #if !defined(NO_ECC256)
  20719. ExpectIntEQ(XMEMCMP(out, expected_shared_secret, outlen), 0);
  20720. #endif
  20721. /* Test bad args. */
  20722. ExpectIntEQ(wc_ecc_shared_secret(NULL, &pubKey, out, &outlen),
  20723. BAD_FUNC_ARG);
  20724. ExpectIntEQ(wc_ecc_shared_secret(&key, NULL, out, &outlen),
  20725. BAD_FUNC_ARG);
  20726. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen),
  20727. BAD_FUNC_ARG);
  20728. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, NULL),
  20729. BAD_FUNC_ARG);
  20730. /* Invalid length */
  20731. outlen = 1;
  20732. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen),
  20733. BUFFER_E);
  20734. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20735. wc_ecc_free(&pubKey);
  20736. wc_ecc_free(&key);
  20737. #ifdef FP_ECC
  20738. wc_ecc_fp_free();
  20739. #endif
  20740. PRIVATE_KEY_LOCK();
  20741. #endif
  20742. return EXPECT_RESULT();
  20743. } /* END tests_wc_ecc_shared_secret */
  20744. /*
  20745. * testint wc_ecc_export_x963()
  20746. */
  20747. static int test_wc_ecc_export_x963(void)
  20748. {
  20749. EXPECT_DECLS;
  20750. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20751. ecc_key key;
  20752. WC_RNG rng;
  20753. byte out[ECC_ASN963_MAX_BUF_SZ];
  20754. word32 outlen = sizeof(out);
  20755. int ret;
  20756. PRIVATE_KEY_UNLOCK();
  20757. /* Initialize variables. */
  20758. XMEMSET(&key, 0, sizeof(ecc_key));
  20759. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20760. XMEMSET(out, 0, outlen);
  20761. ExpectIntEQ(wc_ecc_init(&key), 0);
  20762. ExpectIntEQ(wc_InitRng(&rng), 0);
  20763. ret = wc_ecc_make_key(&rng, KEY20, &key);
  20764. #if defined(WOLFSSL_ASYNC_CRYPT)
  20765. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20766. #endif
  20767. ExpectIntEQ(ret, 0);
  20768. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), 0);
  20769. /* Test bad args. */
  20770. ExpectIntEQ(wc_ecc_export_x963(NULL, out, &outlen), ECC_BAD_ARG_E);
  20771. ExpectIntEQ(wc_ecc_export_x963(&key, NULL, &outlen), LENGTH_ONLY_E);
  20772. ExpectIntEQ(wc_ecc_export_x963(&key, out, NULL), ECC_BAD_ARG_E);
  20773. key.idx = -4;
  20774. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), ECC_BAD_ARG_E);
  20775. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20776. wc_ecc_free(&key);
  20777. #ifdef FP_ECC
  20778. wc_ecc_fp_free();
  20779. #endif
  20780. PRIVATE_KEY_LOCK();
  20781. #endif
  20782. return EXPECT_RESULT();
  20783. } /* END test_wc_ecc_export_x963 */
  20784. /*
  20785. * Testing wc_ecc_export_x963_ex()
  20786. * compile with --enable-compkey will use compression.
  20787. */
  20788. static int test_wc_ecc_export_x963_ex(void)
  20789. {
  20790. EXPECT_DECLS;
  20791. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20792. ecc_key key;
  20793. WC_RNG rng;
  20794. int ret;
  20795. byte out[ECC_ASN963_MAX_BUF_SZ];
  20796. word32 outlen = sizeof(out);
  20797. #ifdef HAVE_COMP_KEY
  20798. word32 badOutLen = 5;
  20799. #endif
  20800. /* Init stack variables. */
  20801. XMEMSET(&key, 0, sizeof(ecc_key));
  20802. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20803. XMEMSET(out, 0, outlen);
  20804. ExpectIntEQ(wc_ecc_init(&key), 0);
  20805. ExpectIntEQ(wc_InitRng(&rng), 0);
  20806. ret = wc_ecc_make_key(&rng, KEY64, &key);
  20807. #if defined(WOLFSSL_ASYNC_CRYPT)
  20808. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20809. #endif
  20810. ExpectIntEQ(ret, 0);
  20811. #ifdef HAVE_COMP_KEY
  20812. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), 0);
  20813. #else
  20814. ExpectIntEQ(ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP), 0);
  20815. #endif
  20816. /* Test bad args. */
  20817. #ifdef HAVE_COMP_KEY
  20818. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, COMP), BAD_FUNC_ARG);
  20819. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP), BAD_FUNC_ARG);
  20820. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, COMP), BAD_FUNC_ARG);
  20821. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  20822. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP), BUFFER_E);
  20823. #else
  20824. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP),
  20825. LENGTH_ONLY_E);
  20826. #endif
  20827. key.idx = -4;
  20828. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), ECC_BAD_ARG_E);
  20829. #else
  20830. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP),
  20831. ECC_BAD_ARG_E);
  20832. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP),
  20833. LENGTH_ONLY_E);
  20834. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, 1), NOT_COMPILED_IN);
  20835. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP),
  20836. ECC_BAD_ARG_E);
  20837. key.idx = -4;
  20838. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP),
  20839. ECC_BAD_ARG_E);
  20840. #endif
  20841. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20842. wc_ecc_free(&key);
  20843. #ifdef FP_ECC
  20844. wc_ecc_fp_free();
  20845. #endif
  20846. #endif
  20847. return EXPECT_RESULT();
  20848. } /* END test_wc_ecc_export_x963_ex */
  20849. /*
  20850. * testing wc_ecc_import_x963()
  20851. */
  20852. static int test_wc_ecc_import_x963(void)
  20853. {
  20854. EXPECT_DECLS;
  20855. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  20856. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20857. ecc_key pubKey;
  20858. ecc_key key;
  20859. WC_RNG rng;
  20860. byte x963[ECC_ASN963_MAX_BUF_SZ];
  20861. word32 x963Len = (word32)sizeof(x963);
  20862. int ret;
  20863. /* Init stack variables. */
  20864. XMEMSET(&key, 0, sizeof(ecc_key));
  20865. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  20866. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20867. XMEMSET(x963, 0, x963Len);
  20868. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  20869. ExpectIntEQ(wc_ecc_init(&key), 0);
  20870. ExpectIntEQ(wc_InitRng(&rng), 0);
  20871. ret = wc_ecc_make_key(&rng, KEY24, &key);
  20872. #if defined(WOLFSSL_ASYNC_CRYPT)
  20873. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20874. #endif
  20875. ExpectIntEQ(ret, 0);
  20876. PRIVATE_KEY_UNLOCK();
  20877. ExpectIntEQ(wc_ecc_export_x963(&key, x963, &x963Len), 0);
  20878. PRIVATE_KEY_LOCK();
  20879. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, &pubKey), 0);
  20880. /* Test bad args. */
  20881. ExpectIntEQ(wc_ecc_import_x963(NULL, x963Len, &pubKey), BAD_FUNC_ARG);
  20882. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, NULL), BAD_FUNC_ARG);
  20883. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len + 1, &pubKey), ECC_BAD_ARG_E);
  20884. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20885. wc_ecc_free(&key);
  20886. wc_ecc_free(&pubKey);
  20887. #ifdef FP_ECC
  20888. wc_ecc_fp_free();
  20889. #endif
  20890. #endif
  20891. return EXPECT_RESULT();
  20892. } /* END wc_ecc_import_x963 */
  20893. /*
  20894. * testing wc_ecc_import_private_key()
  20895. */
  20896. static int test_wc_ecc_import_private_key(void)
  20897. {
  20898. EXPECT_DECLS;
  20899. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  20900. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20901. ecc_key key;
  20902. ecc_key keyImp;
  20903. WC_RNG rng;
  20904. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  20905. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  20906. word32 privKeySz = (word32)sizeof(privKey);
  20907. word32 x963KeySz = (word32)sizeof(x963Key);
  20908. int ret;
  20909. /* Init stack variables. */
  20910. XMEMSET(&key, 0, sizeof(ecc_key));
  20911. XMEMSET(&keyImp, 0, sizeof(ecc_key));
  20912. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20913. XMEMSET(privKey, 0, privKeySz);
  20914. XMEMSET(x963Key, 0, x963KeySz);
  20915. ExpectIntEQ(wc_ecc_init(&key), 0);
  20916. ExpectIntEQ(wc_ecc_init(&keyImp), 0);
  20917. ExpectIntEQ(wc_InitRng(&rng), 0);
  20918. ret = wc_ecc_make_key(&rng, KEY48, &key);
  20919. #if defined(WOLFSSL_ASYNC_CRYPT)
  20920. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20921. #endif
  20922. ExpectIntEQ(ret, 0);
  20923. PRIVATE_KEY_UNLOCK();
  20924. ExpectIntEQ(wc_ecc_export_x963(&key, x963Key, &x963KeySz), 0);
  20925. PRIVATE_KEY_LOCK();
  20926. ExpectIntEQ(wc_ecc_export_private_only(&key, privKey, &privKeySz), 0);
  20927. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  20928. x963KeySz, &keyImp), 0);
  20929. /* Pass in bad args. */
  20930. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  20931. x963KeySz, NULL), BAD_FUNC_ARG);
  20932. ExpectIntEQ(wc_ecc_import_private_key(NULL, privKeySz, x963Key, x963KeySz,
  20933. &keyImp), BAD_FUNC_ARG);
  20934. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20935. wc_ecc_free(&keyImp);
  20936. wc_ecc_free(&key);
  20937. #ifdef FP_ECC
  20938. wc_ecc_fp_free();
  20939. #endif
  20940. #endif
  20941. return EXPECT_RESULT();
  20942. } /* END test_wc_ecc_import_private_key */
  20943. /*
  20944. * Testing wc_ecc_export_private_only()
  20945. */
  20946. static int test_wc_ecc_export_private_only(void)
  20947. {
  20948. EXPECT_DECLS;
  20949. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20950. ecc_key key;
  20951. WC_RNG rng;
  20952. byte out[ECC_PRIV_KEY_BUF];
  20953. word32 outlen = sizeof(out);
  20954. int ret;
  20955. /* Init stack variables. */
  20956. XMEMSET(&key, 0, sizeof(ecc_key));
  20957. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20958. XMEMSET(out, 0, outlen);
  20959. ExpectIntEQ(wc_ecc_init(&key), 0);
  20960. ExpectIntEQ(wc_InitRng(&rng), 0);
  20961. ret = wc_ecc_make_key(&rng, KEY32, &key);
  20962. #if defined(WOLFSSL_ASYNC_CRYPT)
  20963. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20964. #endif
  20965. ExpectIntEQ(ret, 0);
  20966. ExpectIntEQ(wc_ecc_export_private_only(&key, out, &outlen), 0);
  20967. /* Pass in bad args. */
  20968. ExpectIntEQ(wc_ecc_export_private_only(NULL, out, &outlen), BAD_FUNC_ARG);
  20969. ExpectIntEQ(wc_ecc_export_private_only(&key, NULL, &outlen), BAD_FUNC_ARG);
  20970. ExpectIntEQ(wc_ecc_export_private_only(&key, out, NULL), BAD_FUNC_ARG);
  20971. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20972. wc_ecc_free(&key);
  20973. #ifdef FP_ECC
  20974. wc_ecc_fp_free();
  20975. #endif
  20976. #endif
  20977. return EXPECT_RESULT();
  20978. } /* END test_wc_ecc_export_private_only */
  20979. /*
  20980. * Testing wc_ecc_rs_to_sig()
  20981. */
  20982. static int test_wc_ecc_rs_to_sig(void)
  20983. {
  20984. EXPECT_DECLS;
  20985. #if defined(HAVE_ECC) && !defined(NO_ASN)
  20986. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  20987. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  20988. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  20989. const char* zeroStr = "0";
  20990. byte sig[ECC_MAX_SIG_SIZE];
  20991. word32 siglen = (word32)sizeof(sig);
  20992. /* R and S max size is the order of curve. 2^192.*/
  20993. int keySz = KEY24;
  20994. byte r[KEY24];
  20995. byte s[KEY24];
  20996. word32 rlen = (word32)sizeof(r);
  20997. word32 slen = (word32)sizeof(s);
  20998. /* Init stack variables. */
  20999. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  21000. XMEMSET(r, 0, keySz);
  21001. XMEMSET(s, 0, keySz);
  21002. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, &siglen), 0);
  21003. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen), 0);
  21004. /* Test bad args. */
  21005. ExpectIntEQ(wc_ecc_rs_to_sig(NULL, S, sig, &siglen), ECC_BAD_ARG_E);
  21006. ExpectIntEQ(wc_ecc_rs_to_sig(R, NULL, sig, &siglen), ECC_BAD_ARG_E);
  21007. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, NULL), ECC_BAD_ARG_E);
  21008. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, NULL, &siglen), ECC_BAD_ARG_E);
  21009. ExpectIntEQ(wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen), MP_ZERO_E);
  21010. ExpectIntEQ(wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen), MP_ZERO_E);
  21011. ExpectIntEQ(wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen),
  21012. ECC_BAD_ARG_E);
  21013. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen),
  21014. ECC_BAD_ARG_E);
  21015. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen),
  21016. ECC_BAD_ARG_E);
  21017. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen),
  21018. ECC_BAD_ARG_E);
  21019. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL),
  21020. ECC_BAD_ARG_E);
  21021. #endif
  21022. return EXPECT_RESULT();
  21023. } /* END test_wc_ecc_rs_to_sig */
  21024. static int test_wc_ecc_import_raw(void)
  21025. {
  21026. EXPECT_DECLS;
  21027. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21028. ecc_key key;
  21029. const char* qx =
  21030. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21031. const char* qy =
  21032. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21033. const char* d =
  21034. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21035. const char* curveName = "SECP256R1";
  21036. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21037. const char* kNullStr = "";
  21038. int ret;
  21039. #endif
  21040. XMEMSET(&key, 0, sizeof(ecc_key));
  21041. ExpectIntEQ(wc_ecc_init(&key), 0);
  21042. /* Test good import */
  21043. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  21044. /* Test bad args. */
  21045. ExpectIntEQ(wc_ecc_import_raw(NULL, qx, qy, d, curveName), BAD_FUNC_ARG);
  21046. ExpectIntEQ(wc_ecc_import_raw(&key, NULL, qy, d, curveName), BAD_FUNC_ARG);
  21047. ExpectIntEQ(wc_ecc_import_raw(&key, qx, NULL, d, curveName), BAD_FUNC_ARG);
  21048. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, NULL), BAD_FUNC_ARG);
  21049. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21050. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21051. wc_ecc_free(&key);
  21052. #endif
  21053. ExpectIntLT(ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr,
  21054. curveName), 0);
  21055. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  21056. #endif
  21057. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  21058. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21059. wc_ecc_free(&key);
  21060. #endif
  21061. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21062. ExpectIntLT(ret = wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  21063. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  21064. #else
  21065. ExpectIntEQ(wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  21066. #endif
  21067. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21068. wc_ecc_free(&key);
  21069. #endif
  21070. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21071. ExpectIntLT(ret = wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  21072. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  21073. #else
  21074. ExpectIntEQ(wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  21075. #endif
  21076. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21077. wc_ecc_free(&key);
  21078. #endif
  21079. ExpectIntEQ(wc_ecc_import_raw(&key, "0", "0", d, curveName), ECC_INF_E);
  21080. #endif
  21081. wc_ecc_free(&key);
  21082. #endif
  21083. return EXPECT_RESULT();
  21084. } /* END test_wc_ecc_import_raw */
  21085. static int test_wc_ecc_import_unsigned(void)
  21086. {
  21087. EXPECT_DECLS;
  21088. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  21089. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  21090. HAVE_FIPS_VERSION >= 2))
  21091. ecc_key key;
  21092. const byte qx[] = {
  21093. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  21094. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  21095. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  21096. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  21097. };
  21098. const byte qy[] = {
  21099. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  21100. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  21101. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  21102. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  21103. };
  21104. const byte d[] = {
  21105. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  21106. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  21107. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  21108. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  21109. };
  21110. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21111. const byte nullBytes[32] = {0};
  21112. int ret;
  21113. #endif
  21114. int curveId = ECC_SECP256R1;
  21115. XMEMSET(&key, 0, sizeof(ecc_key));
  21116. ExpectIntEQ(wc_ecc_init(&key), 0);
  21117. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  21118. curveId), 0);
  21119. /* Test bad args. */
  21120. ExpectIntEQ(wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  21121. curveId), BAD_FUNC_ARG);
  21122. ExpectIntEQ(wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  21123. curveId), BAD_FUNC_ARG);
  21124. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  21125. curveId), BAD_FUNC_ARG);
  21126. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  21127. ECC_CURVE_INVALID), BAD_FUNC_ARG);
  21128. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21129. ExpectIntLT(ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  21130. (byte*)nullBytes, (byte*)nullBytes, curveId), 0);
  21131. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  21132. #endif
  21133. wc_ecc_free(&key);
  21134. #endif
  21135. return EXPECT_RESULT();
  21136. } /* END test_wc_ecc_import_unsigned */
  21137. /*
  21138. * Testing wc_ecc_sig_size()
  21139. */
  21140. static int test_wc_ecc_sig_size(void)
  21141. {
  21142. EXPECT_DECLS;
  21143. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21144. ecc_key key;
  21145. WC_RNG rng;
  21146. int keySz = KEY16;
  21147. int ret;
  21148. XMEMSET(&rng, 0, sizeof(rng));
  21149. XMEMSET(&key, 0, sizeof(key));
  21150. ExpectIntEQ(wc_ecc_init(&key), 0);
  21151. ExpectIntEQ(wc_InitRng(&rng), 0);
  21152. ret = wc_ecc_make_key(&rng, keySz, &key);
  21153. #if defined(WOLFSSL_ASYNC_CRYPT)
  21154. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21155. #endif
  21156. ExpectIntEQ(ret, 0);
  21157. ExpectIntLE(wc_ecc_sig_size(&key),
  21158. (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ));
  21159. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21160. wc_ecc_free(&key);
  21161. #endif
  21162. return EXPECT_RESULT();
  21163. } /* END test_wc_ecc_sig_size */
  21164. /*
  21165. * Testing wc_ecc_ctx_new()
  21166. */
  21167. static int test_wc_ecc_ctx_new(void)
  21168. {
  21169. EXPECT_DECLS;
  21170. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21171. WC_RNG rng;
  21172. ecEncCtx* cli = NULL;
  21173. ecEncCtx* srv = NULL;
  21174. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21175. ExpectIntEQ(wc_InitRng(&rng), 0);
  21176. ExpectNotNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  21177. ExpectNotNull(srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  21178. wc_ecc_ctx_free(cli);
  21179. cli = NULL;
  21180. wc_ecc_ctx_free(srv);
  21181. /* Test bad args. */
  21182. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  21183. ExpectNull(cli = wc_ecc_ctx_new(0, &rng));
  21184. ExpectNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL));
  21185. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21186. wc_ecc_ctx_free(cli);
  21187. #endif
  21188. return EXPECT_RESULT();
  21189. } /* END test_wc_ecc_ctx_new */
  21190. /*
  21191. * Tesing wc_ecc_reset()
  21192. */
  21193. static int test_wc_ecc_ctx_reset(void)
  21194. {
  21195. EXPECT_DECLS;
  21196. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21197. ecEncCtx* ctx = NULL;
  21198. WC_RNG rng;
  21199. XMEMSET(&rng, 0, sizeof(rng));
  21200. ExpectIntEQ(wc_InitRng(&rng), 0);
  21201. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  21202. ExpectIntEQ(wc_ecc_ctx_reset(ctx, &rng), 0);
  21203. /* Pass in bad args. */
  21204. ExpectIntEQ(wc_ecc_ctx_reset(NULL, &rng), BAD_FUNC_ARG);
  21205. ExpectIntEQ(wc_ecc_ctx_reset(ctx, NULL), BAD_FUNC_ARG);
  21206. wc_ecc_ctx_free(ctx);
  21207. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21208. #endif
  21209. return EXPECT_RESULT();
  21210. } /* END test_wc_ecc_ctx_reset */
  21211. /*
  21212. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  21213. */
  21214. static int test_wc_ecc_ctx_set_peer_salt(void)
  21215. {
  21216. EXPECT_DECLS;
  21217. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21218. WC_RNG rng;
  21219. ecEncCtx* cliCtx = NULL;
  21220. ecEncCtx* servCtx = NULL;
  21221. const byte* cliSalt = NULL;
  21222. const byte* servSalt = NULL;
  21223. XMEMSET(&rng, 0, sizeof(rng));
  21224. ExpectIntEQ(wc_InitRng(&rng), 0);
  21225. ExpectNotNull(cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  21226. ExpectNotNull(servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  21227. /* Test bad args. */
  21228. ExpectNull(cliSalt = wc_ecc_ctx_get_own_salt(NULL));
  21229. ExpectNotNull(cliSalt = wc_ecc_ctx_get_own_salt(cliCtx));
  21230. ExpectNotNull(servSalt = wc_ecc_ctx_get_own_salt(servCtx));
  21231. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, servSalt), 0);
  21232. /* Test bad args. */
  21233. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(NULL, servSalt), BAD_FUNC_ARG);
  21234. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, NULL), BAD_FUNC_ARG);
  21235. wc_ecc_ctx_free(cliCtx);
  21236. wc_ecc_ctx_free(servCtx);
  21237. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21238. #endif
  21239. return EXPECT_RESULT();
  21240. } /* END test_wc_ecc_ctx_set_peer_salt */
  21241. /*
  21242. * Testing wc_ecc_ctx_set_info()
  21243. */
  21244. static int test_wc_ecc_ctx_set_info(void)
  21245. {
  21246. EXPECT_DECLS;
  21247. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21248. ecEncCtx* ctx = NULL;
  21249. WC_RNG rng;
  21250. const char* optInfo = "Optional Test Info.";
  21251. int optInfoSz = (int)XSTRLEN(optInfo);
  21252. const char* badOptInfo = NULL;
  21253. XMEMSET(&rng, 0, sizeof(rng));
  21254. ExpectIntEQ(wc_InitRng(&rng), 0);
  21255. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  21256. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz), 0);
  21257. /* Test bad args. */
  21258. ExpectIntEQ(wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz),
  21259. BAD_FUNC_ARG);
  21260. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz),
  21261. BAD_FUNC_ARG);
  21262. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1), BAD_FUNC_ARG);
  21263. wc_ecc_ctx_free(ctx);
  21264. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21265. #endif
  21266. return EXPECT_RESULT();
  21267. } /* END test_wc_ecc_ctx_set_info */
  21268. /*
  21269. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  21270. */
  21271. static int test_wc_ecc_encryptDecrypt(void)
  21272. {
  21273. EXPECT_DECLS;
  21274. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  21275. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  21276. ecc_key srvKey;
  21277. ecc_key cliKey;
  21278. ecc_key tmpKey;
  21279. WC_RNG rng;
  21280. int ret;
  21281. const char* msg = "EccBlock Size 16";
  21282. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  21283. #ifdef WOLFSSL_ECIES_OLD
  21284. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  21285. #elif defined(WOLFSSL_ECIES_GEN_IV)
  21286. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  21287. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  21288. #else
  21289. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) +
  21290. WC_SHA256_DIGEST_SIZE];
  21291. #endif
  21292. word32 outSz = (word32)sizeof(out);
  21293. byte plain[sizeof("EccBlock Size 16")];
  21294. word32 plainSz = (word32)sizeof(plain);
  21295. int keySz = KEY20;
  21296. /* Init stack variables. */
  21297. XMEMSET(out, 0, outSz);
  21298. XMEMSET(plain, 0, plainSz);
  21299. XMEMSET(&rng, 0, sizeof(rng));
  21300. XMEMSET(&srvKey, 0, sizeof(ecc_key));
  21301. XMEMSET(&cliKey, 0, sizeof(ecc_key));
  21302. XMEMSET(&tmpKey, 0, sizeof(ecc_key));
  21303. ExpectIntEQ(wc_InitRng(&rng), 0);
  21304. ExpectIntEQ(wc_ecc_init(&cliKey), 0);
  21305. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  21306. #if defined(WOLFSSL_ASYNC_CRYPT)
  21307. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  21308. #endif
  21309. ExpectIntEQ(ret, 0);
  21310. ExpectIntEQ(wc_ecc_init(&srvKey), 0);
  21311. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  21312. #if defined(WOLFSSL_ASYNC_CRYPT)
  21313. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  21314. #endif
  21315. ExpectIntEQ(ret, 0);
  21316. ExpectIntEQ(wc_ecc_init(&tmpKey), 0);
  21317. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21318. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21319. !defined(HAVE_SELFTEST)
  21320. ExpectIntEQ(wc_ecc_set_rng(&srvKey, &rng), 0);
  21321. ExpectIntEQ(wc_ecc_set_rng(&cliKey, &rng), 0);
  21322. #endif
  21323. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  21324. &outSz, NULL), 0);
  21325. /* Test bad args. */
  21326. ExpectIntEQ(wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out, &outSz,
  21327. NULL), BAD_FUNC_ARG);
  21328. ExpectIntEQ(wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out, &outSz,
  21329. NULL), BAD_FUNC_ARG);
  21330. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out, &outSz,
  21331. NULL), BAD_FUNC_ARG);
  21332. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  21333. &outSz, NULL), BAD_FUNC_ARG);
  21334. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out, NULL,
  21335. NULL), BAD_FUNC_ARG);
  21336. #ifdef WOLFSSL_ECIES_OLD
  21337. tmpKey.dp = cliKey.dp;
  21338. ExpectIntEQ(wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey), 0);
  21339. #endif
  21340. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, &plainSz,
  21341. NULL), 0);
  21342. ExpectIntEQ(wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain, &plainSz,
  21343. NULL), BAD_FUNC_ARG);
  21344. #ifdef WOLFSSL_ECIES_OLD
  21345. /* NULL parameter allowed in new implementations - public key comes from
  21346. * the message. */
  21347. ExpectIntEQ(wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain, &plainSz,
  21348. NULL), BAD_FUNC_ARG);
  21349. #endif
  21350. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain, &plainSz,
  21351. NULL), BAD_FUNC_ARG);
  21352. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL, &plainSz,
  21353. NULL), BAD_FUNC_ARG);
  21354. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, NULL, NULL),
  21355. BAD_FUNC_ARG);
  21356. ExpectIntEQ(XMEMCMP(msg, plain, msgSz), 0);
  21357. wc_ecc_free(&tmpKey);
  21358. wc_ecc_free(&srvKey);
  21359. wc_ecc_free(&cliKey);
  21360. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21361. #endif
  21362. return EXPECT_RESULT();
  21363. } /* END test_wc_ecc_encryptDecrypt */
  21364. /*
  21365. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  21366. */
  21367. static int test_wc_ecc_del_point(void)
  21368. {
  21369. EXPECT_DECLS;
  21370. #if defined(HAVE_ECC)
  21371. ecc_point* pt = NULL;
  21372. ExpectNotNull(pt = wc_ecc_new_point());
  21373. wc_ecc_del_point(pt);
  21374. #endif
  21375. return EXPECT_RESULT();
  21376. } /* END test_wc_ecc_del_point */
  21377. /*
  21378. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  21379. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  21380. * and wc_ecc_cmp_point()
  21381. */
  21382. static int test_wc_ecc_pointFns(void)
  21383. {
  21384. EXPECT_DECLS;
  21385. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  21386. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  21387. !defined(WOLFSSL_ATECC608A)
  21388. ecc_key key;
  21389. WC_RNG rng;
  21390. int ret;
  21391. ecc_point* point = NULL;
  21392. ecc_point* cpypt = NULL;
  21393. int idx = 0;
  21394. int keySz = KEY32;
  21395. byte der[DER_SZ(KEY32)];
  21396. word32 derlenChk = 0;
  21397. word32 derSz = DER_SZ(KEY32);
  21398. /* Init stack variables. */
  21399. XMEMSET(der, 0, derSz);
  21400. XMEMSET(&key, 0, sizeof(ecc_key));
  21401. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21402. ExpectIntEQ(wc_InitRng(&rng), 0);
  21403. ExpectIntEQ(wc_ecc_init(&key), 0);
  21404. ret = wc_ecc_make_key(&rng, keySz, &key);
  21405. #if defined(WOLFSSL_ASYNC_CRYPT)
  21406. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21407. #endif
  21408. ExpectIntEQ(ret, 0);
  21409. ExpectNotNull(point = wc_ecc_new_point());
  21410. ExpectNotNull(cpypt = wc_ecc_new_point());
  21411. /* Export */
  21412. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, NULL,
  21413. &derlenChk), LENGTH_ONLY_E);
  21414. /* Check length value. */
  21415. ExpectIntEQ(derSz, derlenChk);
  21416. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  21417. &derSz), 0);
  21418. /* Test bad args. */
  21419. ExpectIntEQ(wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz),
  21420. ECC_BAD_ARG_E);
  21421. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz),
  21422. ECC_BAD_ARG_E);
  21423. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  21424. NULL), ECC_BAD_ARG_E);
  21425. /* Import */
  21426. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, point), 0);
  21427. ExpectIntEQ(wc_ecc_cmp_point(&key.pubkey, point), 0);
  21428. /* Test bad args. */
  21429. ExpectIntEQ( wc_ecc_import_point_der(NULL, derSz, idx, point),
  21430. ECC_BAD_ARG_E);
  21431. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, NULL), ECC_BAD_ARG_E);
  21432. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, -1, point), ECC_BAD_ARG_E);
  21433. ExpectIntEQ(wc_ecc_import_point_der(der, derSz + 1, idx, point),
  21434. ECC_BAD_ARG_E);
  21435. /* Copy */
  21436. ExpectIntEQ(wc_ecc_copy_point(point, cpypt), 0);
  21437. /* Test bad args. */
  21438. ExpectIntEQ(wc_ecc_copy_point(NULL, cpypt), ECC_BAD_ARG_E);
  21439. ExpectIntEQ(wc_ecc_copy_point(point, NULL), ECC_BAD_ARG_E);
  21440. /* Compare point */
  21441. ExpectIntEQ(wc_ecc_cmp_point(point, cpypt), 0);
  21442. /* Test bad args. */
  21443. ExpectIntEQ(wc_ecc_cmp_point(NULL, cpypt), BAD_FUNC_ARG);
  21444. ExpectIntEQ(wc_ecc_cmp_point(point, NULL), BAD_FUNC_ARG);
  21445. /* At infinity if return == 1, otherwise return == 0. */
  21446. ExpectIntEQ(wc_ecc_point_is_at_infinity(point), 0);
  21447. /* Test bad args. */
  21448. ExpectIntEQ(wc_ecc_point_is_at_infinity(NULL), BAD_FUNC_ARG);
  21449. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  21450. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  21451. #ifdef USE_ECC_B_PARAM
  21452. /* On curve if ret == 0 */
  21453. ExpectIntEQ(wc_ecc_point_is_on_curve(point, idx), 0);
  21454. /* Test bad args. */
  21455. ExpectIntEQ(wc_ecc_point_is_on_curve(NULL, idx), BAD_FUNC_ARG);
  21456. ExpectIntEQ(wc_ecc_point_is_on_curve(point, 1000), ECC_BAD_ARG_E);
  21457. #endif /* USE_ECC_B_PARAM */
  21458. #endif /* !HAVE_SELFTEST && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  21459. /* Free */
  21460. wc_ecc_del_point(point);
  21461. wc_ecc_del_point(cpypt);
  21462. wc_ecc_free(&key);
  21463. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21464. #endif
  21465. return EXPECT_RESULT();
  21466. } /* END test_wc_ecc_pointFns */
  21467. /*
  21468. * Testing wc_ecc_sahred_secret_ssh()
  21469. */
  21470. static int test_wc_ecc_shared_secret_ssh(void)
  21471. {
  21472. EXPECT_DECLS;
  21473. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  21474. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  21475. !defined(WOLFSSL_ATECC608A)
  21476. ecc_key key;
  21477. ecc_key key2;
  21478. WC_RNG rng;
  21479. int ret;
  21480. int keySz = KEY32;
  21481. int key2Sz = KEY24;
  21482. byte secret[KEY32];
  21483. word32 secretLen = keySz;
  21484. /* Init stack variables. */
  21485. XMEMSET(&key, 0, sizeof(ecc_key));
  21486. XMEMSET(&key2, 0, sizeof(ecc_key));
  21487. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21488. XMEMSET(secret, 0, secretLen);
  21489. /* Make keys */
  21490. ExpectIntEQ(wc_ecc_init(&key), 0);
  21491. ExpectIntEQ(wc_InitRng(&rng), 0);
  21492. ret = wc_ecc_make_key(&rng, keySz, &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. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21498. ExpectIntEQ(wc_ecc_init(&key2), 0);
  21499. ExpectIntEQ(wc_InitRng(&rng), 0);
  21500. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  21501. #if defined(WOLFSSL_ASYNC_CRYPT)
  21502. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  21503. #endif
  21504. ExpectIntEQ(ret, 0);
  21505. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21506. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21507. !defined(HAVE_SELFTEST)
  21508. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  21509. #endif
  21510. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  21511. &secretLen), 0);
  21512. /* Pass in bad args. */
  21513. ExpectIntEQ(wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret,
  21514. &secretLen), BAD_FUNC_ARG);
  21515. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen),
  21516. BAD_FUNC_ARG);
  21517. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen),
  21518. BAD_FUNC_ARG);
  21519. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL),
  21520. BAD_FUNC_ARG);
  21521. key.type = ECC_PUBLICKEY;
  21522. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  21523. &secretLen), ECC_BAD_ARG_E);
  21524. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21525. wc_ecc_free(&key);
  21526. wc_ecc_free(&key2);
  21527. #ifdef FP_ECC
  21528. wc_ecc_fp_free();
  21529. #endif
  21530. #endif
  21531. return EXPECT_RESULT();
  21532. } /* END test_wc_ecc_shared_secret_ssh */
  21533. /*
  21534. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  21535. */
  21536. static int test_wc_ecc_verify_hash_ex(void)
  21537. {
  21538. EXPECT_DECLS;
  21539. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  21540. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  21541. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  21542. ecc_key key;
  21543. WC_RNG rng;
  21544. int ret;
  21545. mp_int r;
  21546. mp_int s;
  21547. mp_int z;
  21548. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  21549. unsigned char iHash[] = "Everyone gets Friday off.......";
  21550. unsigned char shortHash[] = TEST_STRING;
  21551. word32 hashlen = sizeof(hash);
  21552. word32 iHashLen = sizeof(iHash);
  21553. word32 shortHashLen = sizeof(shortHash);
  21554. int keySz = KEY32;
  21555. int verify_ok = 0;
  21556. XMEMSET(&key, 0, sizeof(ecc_key));
  21557. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21558. XMEMSET(&r, 0, sizeof(mp_int));
  21559. XMEMSET(&s, 0, sizeof(mp_int));
  21560. XMEMSET(&z, 0, sizeof(mp_int));
  21561. /* Initialize r, s and z. */
  21562. ExpectIntEQ(mp_init_multi(&r, &s, &z, NULL, NULL, NULL), MP_OKAY);
  21563. ExpectIntEQ(wc_ecc_init(&key), 0);
  21564. ExpectIntEQ(wc_InitRng(&rng), 0);
  21565. ret = wc_ecc_make_key(&rng, keySz, &key);
  21566. #if defined(WOLFSSL_ASYNC_CRYPT)
  21567. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21568. #endif
  21569. ExpectIntEQ(ret, 0);
  21570. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s), 0);
  21571. /* verify_ok should be 1. */
  21572. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key),
  21573. 0);
  21574. ExpectIntEQ(verify_ok, 1);
  21575. /* verify_ok should be 0 */
  21576. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen, &verify_ok,
  21577. &key), 0);
  21578. ExpectIntEQ(verify_ok, 0);
  21579. /* verify_ok should be 0. */
  21580. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  21581. &verify_ok, &key), 0);
  21582. ExpectIntEQ(verify_ok, 0);
  21583. /* Test bad args. */
  21584. ExpectIntEQ(wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s),
  21585. ECC_BAD_ARG_E);
  21586. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s),
  21587. ECC_BAD_ARG_E);
  21588. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s),
  21589. ECC_BAD_ARG_E);
  21590. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s),
  21591. ECC_BAD_ARG_E);
  21592. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL),
  21593. ECC_BAD_ARG_E);
  21594. /* Test bad args. */
  21595. ExpectIntEQ(wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen,
  21596. &verify_ok, &key), ECC_BAD_ARG_E);
  21597. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  21598. &verify_ok, &key), ECC_BAD_ARG_E);
  21599. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen,
  21600. &verify_ok, &key), MP_ZERO_E);
  21601. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen,
  21602. &verify_ok, &key), MP_ZERO_E);
  21603. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen,
  21604. &verify_ok, &key), MP_ZERO_E);
  21605. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  21606. &key), ECC_BAD_ARG_E);
  21607. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen, NULL,
  21608. &key), ECC_BAD_ARG_E);
  21609. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  21610. &verify_ok, NULL), ECC_BAD_ARG_E);
  21611. wc_ecc_free(&key);
  21612. mp_free(&r);
  21613. mp_free(&s);
  21614. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21615. #endif
  21616. return EXPECT_RESULT();
  21617. } /* END test_wc_ecc_verify_hash_ex */
  21618. /*
  21619. * Testing wc_ecc_mulmod()
  21620. */
  21621. static int test_wc_ecc_mulmod(void)
  21622. {
  21623. EXPECT_DECLS;
  21624. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  21625. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  21626. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  21627. ecc_key key1;
  21628. ecc_key key2;
  21629. ecc_key key3;
  21630. WC_RNG rng;
  21631. int ret;
  21632. XMEMSET(&key1, 0, sizeof(ecc_key));
  21633. XMEMSET(&key2, 0, sizeof(ecc_key));
  21634. XMEMSET(&key3, 0, sizeof(ecc_key));
  21635. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21636. ExpectIntEQ(wc_ecc_init(&key1), 0);
  21637. ExpectIntEQ(wc_ecc_init(&key2), 0);
  21638. ExpectIntEQ(wc_ecc_init(&key3), 0);
  21639. ExpectIntEQ(wc_InitRng(&rng), 0);
  21640. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  21641. #if defined(WOLFSSL_ASYNC_CRYPT)
  21642. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  21643. #endif
  21644. ExpectIntEQ(ret, 0);
  21645. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21646. ExpectIntEQ(wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy,
  21647. key1.dp->Af, ECC_SECP256R1), 0);
  21648. ExpectIntEQ(wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  21649. key1.dp->prime, ECC_SECP256R1), 0);
  21650. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  21651. &key3.pubkey, wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3),
  21652. 1), 0);
  21653. /* Test bad args. */
  21654. ExpectIntEQ(ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey,
  21655. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  21656. ECC_BAD_ARG_E);
  21657. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), NULL, &key3.pubkey,
  21658. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  21659. ECC_BAD_ARG_E);
  21660. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey, NULL,
  21661. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  21662. ECC_BAD_ARG_E);
  21663. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  21664. &key3.pubkey, wc_ecc_key_get_priv(&key2), NULL, 1), ECC_BAD_ARG_E);
  21665. wc_ecc_free(&key1);
  21666. wc_ecc_free(&key2);
  21667. wc_ecc_free(&key3);
  21668. #ifdef FP_ECC
  21669. wc_ecc_fp_free();
  21670. #endif
  21671. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  21672. return EXPECT_RESULT();
  21673. } /* END test_wc_ecc_mulmod */
  21674. /*
  21675. * Testing wc_ecc_is_valid_idx()
  21676. */
  21677. static int test_wc_ecc_is_valid_idx(void)
  21678. {
  21679. EXPECT_DECLS;
  21680. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21681. ecc_key key;
  21682. WC_RNG rng;
  21683. int ret;
  21684. int iVal = -2;
  21685. int iVal2 = 3000;
  21686. XMEMSET(&key, 0, sizeof(ecc_key));
  21687. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21688. ExpectIntEQ(wc_ecc_init(&key), 0);
  21689. ExpectIntEQ(wc_InitRng(&rng), 0);
  21690. ret = wc_ecc_make_key(&rng, 32, &key);
  21691. #if defined(WOLFSSL_ASYNC_CRYPT)
  21692. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21693. #endif
  21694. ExpectIntEQ(ret, 0);
  21695. ExpectIntEQ(wc_ecc_is_valid_idx(key.idx), 1);
  21696. /* Test bad args. */
  21697. ExpectIntEQ(wc_ecc_is_valid_idx(iVal), 0);
  21698. ExpectIntEQ(wc_ecc_is_valid_idx(iVal2), 0);
  21699. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21700. wc_ecc_free(&key);
  21701. #ifdef FP_ECC
  21702. wc_ecc_fp_free();
  21703. #endif
  21704. #endif
  21705. return EXPECT_RESULT();
  21706. } /* END test_wc_ecc_is_valid_idx */
  21707. /*
  21708. * Testing wc_ecc_get_curve_id_from_oid()
  21709. */
  21710. static int test_wc_ecc_get_curve_id_from_oid(void)
  21711. {
  21712. EXPECT_DECLS;
  21713. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  21714. !defined(HAVE_FIPS)
  21715. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  21716. word32 len = sizeof(oid);
  21717. /* Bad Cases */
  21718. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(NULL, len), BAD_FUNC_ARG);
  21719. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, 0), ECC_CURVE_INVALID);
  21720. /* Good Case */
  21721. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, len), ECC_SECP256R1);
  21722. #endif
  21723. return EXPECT_RESULT();
  21724. } /* END test_wc_ecc_get_curve_id_from_oid */
  21725. /*
  21726. * Testing wc_ecc_sig_size_calc()
  21727. */
  21728. static int test_wc_ecc_sig_size_calc(void)
  21729. {
  21730. EXPECT_DECLS;
  21731. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  21732. ecc_key key;
  21733. WC_RNG rng;
  21734. int sz = 0;
  21735. int ret;
  21736. XMEMSET(&key, 0, sizeof(ecc_key));
  21737. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21738. ExpectIntEQ(wc_ecc_init(&key), 0);
  21739. ExpectIntEQ(wc_InitRng(&rng), 0);
  21740. ret = wc_ecc_make_key(&rng, 16, &key);
  21741. #if defined(WOLFSSL_ASYNC_CRYPT)
  21742. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21743. #endif
  21744. ExpectIntEQ(ret, 0);
  21745. sz = key.dp->size;
  21746. ExpectIntGT(wc_ecc_sig_size_calc(sz), 0);
  21747. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21748. wc_ecc_free(&key);
  21749. #endif
  21750. return EXPECT_RESULT();
  21751. } /* END test_wc_ecc_sig_size_calc */
  21752. /*
  21753. * Testing wc_ecc_sm2_make_key()
  21754. */
  21755. static int test_wc_ecc_sm2_make_key(void)
  21756. {
  21757. int res = TEST_SKIPPED;
  21758. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  21759. EXPECT_DECLS;
  21760. WC_RNG rng[1];
  21761. ecc_key key[1];
  21762. XMEMSET(rng, 0, sizeof(*rng));
  21763. XMEMSET(key, 0, sizeof(*key));
  21764. ExpectIntEQ(wc_InitRng(rng), 0);
  21765. ExpectIntEQ(wc_ecc_init(key), 0);
  21766. /* Test invalid parameters. */
  21767. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, NULL, WC_ECC_FLAG_NONE),
  21768. BAD_FUNC_ARG);
  21769. ExpectIntEQ(wc_ecc_sm2_make_key(rng, NULL, WC_ECC_FLAG_NONE),
  21770. BAD_FUNC_ARG);
  21771. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, key, WC_ECC_FLAG_NONE),
  21772. BAD_FUNC_ARG);
  21773. /* Test valid parameters. */
  21774. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  21775. ExpectIntEQ(key->dp->id, ECC_SM2P256V1);
  21776. wc_ecc_free(key);
  21777. wc_FreeRng(rng);
  21778. #ifdef FP_ECC
  21779. wc_ecc_fp_free();
  21780. #endif
  21781. res = EXPECT_RESULT();
  21782. #endif
  21783. return res;
  21784. }
  21785. /*
  21786. * Testing wc_ecc_sm2_shared_secret()
  21787. */
  21788. static int test_wc_ecc_sm2_shared_secret(void)
  21789. {
  21790. int res = TEST_SKIPPED;
  21791. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  21792. EXPECT_DECLS;
  21793. WC_RNG rng[1];
  21794. ecc_key keyA[1];
  21795. ecc_key keyB[1];
  21796. byte outA[32];
  21797. byte outB[32];
  21798. word32 outALen = 32;
  21799. word32 outBLen = 32;
  21800. XMEMSET(rng, 0, sizeof(*rng));
  21801. XMEMSET(keyA, 0, sizeof(*keyA));
  21802. XMEMSET(keyB, 0, sizeof(*keyB));
  21803. ExpectIntEQ(wc_InitRng(rng), 0);
  21804. ExpectIntEQ(wc_ecc_init(keyA), 0);
  21805. ExpectIntEQ(wc_ecc_init(keyB), 0);
  21806. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyA, WC_ECC_FLAG_NONE), 0);
  21807. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyB, WC_ECC_FLAG_NONE), 0);
  21808. #ifdef ECC_TIMING_RESISTANT
  21809. ExpectIntEQ(wc_ecc_set_rng(keyA, rng), 0);
  21810. ExpectIntEQ(wc_ecc_set_rng(keyB, rng), 0);
  21811. #endif
  21812. /* Test invalid parameters. */
  21813. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  21814. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, NULL, NULL), BAD_FUNC_ARG);
  21815. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, NULL, NULL), BAD_FUNC_ARG);
  21816. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, outA, NULL), BAD_FUNC_ARG);
  21817. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, &outALen),
  21818. BAD_FUNC_ARG);
  21819. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, outA, &outALen),
  21820. BAD_FUNC_ARG);
  21821. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, outA, &outALen),
  21822. BAD_FUNC_ARG);
  21823. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, NULL, &outALen),
  21824. BAD_FUNC_ARG);
  21825. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, NULL), BAD_FUNC_ARG);
  21826. /* Test valid parameters. */
  21827. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, &outALen), 0);
  21828. ExpectIntLE(outALen, 32);
  21829. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyB, keyA, outB, &outBLen), 0);
  21830. ExpectIntLE(outBLen, 32);
  21831. ExpectIntEQ(outALen, outBLen);
  21832. ExpectBufEQ(outA, outB, outALen);
  21833. wc_ecc_free(keyB);
  21834. wc_ecc_free(keyA);
  21835. wc_FreeRng(rng);
  21836. #ifdef FP_ECC
  21837. wc_ecc_fp_free();
  21838. #endif
  21839. res = EXPECT_RESULT();
  21840. #endif
  21841. return res;
  21842. }
  21843. /*
  21844. * Testing wc_ecc_sm2_create_digest()
  21845. */
  21846. static int test_wc_ecc_sm2_create_digest(void)
  21847. {
  21848. int res = TEST_SKIPPED;
  21849. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && !defined(NO_HASH_WRAPPER) && \
  21850. (defined(WOLFSSL_SM3) || !defined(NO_SHA256))
  21851. EXPECT_DECLS;
  21852. ecc_key key[1];
  21853. enum wc_HashType hashType;
  21854. unsigned char pub[] = {
  21855. 0x04,
  21856. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  21857. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  21858. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  21859. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  21860. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  21861. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  21862. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  21863. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  21864. };
  21865. unsigned char id[] = {
  21866. 0x01, 0x02, 0x03,
  21867. };
  21868. unsigned char msg[] = {
  21869. 0x01, 0x02, 0x03,
  21870. };
  21871. unsigned char hash[32];
  21872. #ifdef WOLFSSL_SM3
  21873. unsigned char expHash[32] = {
  21874. 0xc1, 0xdd, 0x92, 0xc5, 0x60, 0xd3, 0x94, 0x28,
  21875. 0xeb, 0x0f, 0x57, 0x79, 0x3f, 0xc9, 0x96, 0xc5,
  21876. 0xfa, 0xf5, 0x90, 0xb2, 0x64, 0x2f, 0xaf, 0x9c,
  21877. 0xc8, 0x57, 0x21, 0x6a, 0x52, 0x7e, 0xf1, 0x95
  21878. };
  21879. #else
  21880. unsigned char expHash[32] = {
  21881. 0xea, 0x41, 0x55, 0x21, 0x61, 0x00, 0x5c, 0x9a,
  21882. 0x57, 0x35, 0x6b, 0x49, 0xca, 0x8f, 0x65, 0xc2,
  21883. 0x0e, 0x29, 0x0c, 0xa0, 0x1d, 0xa7, 0xc4, 0xed,
  21884. 0xdd, 0x51, 0x12, 0xf6, 0xe7, 0x55, 0xc5, 0xf4
  21885. };
  21886. #endif
  21887. #ifdef WOLFSSL_SM3
  21888. hashType = WC_HASH_TYPE_SM3;
  21889. #else
  21890. hashType = WC_HASH_TYPE_SHA256;
  21891. #endif
  21892. XMEMSET(key, 0, sizeof(*key));
  21893. ExpectIntEQ(wc_ecc_init(key), 0);
  21894. /* Test with no curve set. */
  21895. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  21896. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  21897. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  21898. /* Test invalid parameters. */
  21899. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  21900. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  21901. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  21902. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  21903. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  21904. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  21905. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  21906. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  21907. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  21908. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  21909. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  21910. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  21911. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  21912. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  21913. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  21914. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  21915. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  21916. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  21917. /* Bad hash type. */
  21918. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  21919. -1, hash, 0, key), BAD_FUNC_ARG);
  21920. /* Bad hash size. */
  21921. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  21922. hashType, hash, 0, key), BUFFER_E);
  21923. /* Test valid parameters. */
  21924. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  21925. hashType, hash, sizeof(hash), key), 0);
  21926. ExpectBufEQ(hash, expHash, sizeof(expHash));
  21927. wc_ecc_free(key);
  21928. res = EXPECT_RESULT();
  21929. #endif
  21930. return res;
  21931. }
  21932. /*
  21933. * Testing wc_ecc_sm2_verify_hash_ex()
  21934. */
  21935. static int test_wc_ecc_sm2_verify_hash_ex(void)
  21936. {
  21937. int res = TEST_SKIPPED;
  21938. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY) && \
  21939. defined(WOLFSSL_PUBLIC_MP)
  21940. EXPECT_DECLS;
  21941. ecc_key key[1];
  21942. mp_int r[1];
  21943. mp_int s[1];
  21944. int verified;
  21945. unsigned char pub[] = {
  21946. 0x04,
  21947. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  21948. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  21949. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  21950. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  21951. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  21952. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  21953. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  21954. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  21955. };
  21956. unsigned char hash[] = {
  21957. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  21958. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  21959. 0x50, 0x69, 0x5B, 0x20
  21960. };
  21961. unsigned char rData[] = {
  21962. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  21963. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  21964. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  21965. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE7,
  21966. };
  21967. unsigned char sData[] = {
  21968. 0x1D,
  21969. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  21970. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  21971. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  21972. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  21973. };
  21974. unsigned char rBadData[] = {
  21975. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  21976. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  21977. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  21978. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE8,
  21979. };
  21980. XMEMSET(key, 0, sizeof(*key));
  21981. XMEMSET(r, 0, sizeof(*r));
  21982. XMEMSET(s, 0, sizeof(*s));
  21983. ExpectIntEQ(mp_init(r), 0);
  21984. ExpectIntEQ(mp_init(s), 0);
  21985. ExpectIntEQ(mp_read_unsigned_bin(r, rData, sizeof(rData)), 0);
  21986. ExpectIntEQ(mp_read_unsigned_bin(s, sData, sizeof(sData)), 0);
  21987. ExpectIntEQ(wc_ecc_init(key), 0);
  21988. /* Test with no curve set. */
  21989. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  21990. &verified, key), BAD_FUNC_ARG);
  21991. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  21992. /* Test invalid parameters. */
  21993. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  21994. NULL, NULL), BAD_FUNC_ARG);
  21995. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, NULL, sizeof(hash),
  21996. NULL, NULL), BAD_FUNC_ARG);
  21997. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, NULL, sizeof(hash),
  21998. NULL, NULL), BAD_FUNC_ARG);
  21999. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, hash, sizeof(hash),
  22000. NULL, NULL), BAD_FUNC_ARG);
  22001. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22002. &verified, NULL), BAD_FUNC_ARG);
  22003. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22004. NULL, key), BAD_FUNC_ARG);
  22005. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, hash, sizeof(hash),
  22006. &verified, key), BAD_FUNC_ARG);
  22007. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, hash, sizeof(hash),
  22008. &verified, key), BAD_FUNC_ARG);
  22009. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, NULL, sizeof(hash),
  22010. &verified, key), BAD_FUNC_ARG);
  22011. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22012. NULL, key), BAD_FUNC_ARG);
  22013. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22014. &verified, NULL), BAD_FUNC_ARG);
  22015. /* Make key not on the SM2 curve. */
  22016. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22017. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22018. &verified, key), BAD_FUNC_ARG);
  22019. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22020. /* Test valid parameters. */
  22021. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22022. &verified, key), 0);
  22023. ExpectIntEQ(verified, 1);
  22024. ExpectIntEQ(mp_read_unsigned_bin(r, rBadData, sizeof(rBadData)), 0);
  22025. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22026. &verified, key), 0);
  22027. ExpectIntEQ(verified, 0);
  22028. mp_free(s);
  22029. mp_free(r);
  22030. wc_ecc_free(key);
  22031. #ifdef FP_ECC
  22032. wc_ecc_fp_free();
  22033. #endif
  22034. res = EXPECT_RESULT();
  22035. #endif
  22036. return res;
  22037. }
  22038. /*
  22039. * Testing wc_ecc_sm2_verify_hash()
  22040. */
  22041. static int test_wc_ecc_sm2_verify_hash(void)
  22042. {
  22043. int res = TEST_SKIPPED;
  22044. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY)
  22045. EXPECT_DECLS;
  22046. ecc_key key[1];
  22047. int verified;
  22048. unsigned char pub[] = {
  22049. 0x04,
  22050. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22051. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22052. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22053. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22054. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22055. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22056. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22057. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22058. };
  22059. unsigned char hash[] = {
  22060. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  22061. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  22062. 0x50, 0x69, 0x5B, 0x20
  22063. };
  22064. unsigned char sig[] = {
  22065. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  22066. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  22067. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  22068. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  22069. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  22070. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22071. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22072. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22073. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  22074. };
  22075. unsigned char sigBad[] = {
  22076. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  22077. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  22078. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  22079. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  22080. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  22081. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22082. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22083. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22084. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDD
  22085. };
  22086. XMEMSET(key, 0, sizeof(*key));
  22087. ExpectIntEQ(wc_ecc_init(key), 0);
  22088. /* Test with no curve set. */
  22089. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22090. &verified, key), BAD_FUNC_ARG);
  22091. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22092. /* Test invalid parameters. */
  22093. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  22094. NULL, NULL), BAD_FUNC_ARG);
  22095. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  22096. NULL, NULL), BAD_FUNC_ARG);
  22097. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  22098. NULL, NULL), BAD_FUNC_ARG);
  22099. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  22100. &verified, NULL), BAD_FUNC_ARG);
  22101. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  22102. NULL, key), BAD_FUNC_ARG);
  22103. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  22104. &verified, key), BAD_FUNC_ARG);
  22105. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  22106. &verified, key), BAD_FUNC_ARG);
  22107. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22108. NULL, key), BAD_FUNC_ARG);
  22109. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22110. &verified, NULL), BAD_FUNC_ARG);
  22111. /* Make key not on the SM2 curve. */
  22112. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22113. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22114. &verified, key), BAD_FUNC_ARG);
  22115. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22116. /* Test valid parameters. */
  22117. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22118. &verified, key), 0);
  22119. ExpectIntEQ(verified, 1);
  22120. ExpectIntEQ(wc_ecc_sm2_verify_hash(sigBad, sizeof(sigBad), hash,
  22121. sizeof(hash), &verified, key), 0);
  22122. ExpectIntEQ(verified, 0);
  22123. wc_ecc_free(key);
  22124. #ifdef FP_ECC
  22125. wc_ecc_fp_free();
  22126. #endif
  22127. res = EXPECT_RESULT();
  22128. #endif
  22129. return res;
  22130. }
  22131. /*
  22132. * Testing wc_ecc_sm2_verify_hash_ex()
  22133. */
  22134. static int test_wc_ecc_sm2_sign_hash_ex(void)
  22135. {
  22136. int res = TEST_SKIPPED;
  22137. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN) && \
  22138. defined(WOLFSSL_PUBLIC_MP)
  22139. EXPECT_DECLS;
  22140. WC_RNG rng[1];
  22141. ecc_key key[1];
  22142. mp_int r[1];
  22143. mp_int s[1];
  22144. unsigned char hash[32];
  22145. #ifdef HAVE_ECC_VERIFY
  22146. int verified;
  22147. #endif
  22148. XMEMSET(rng, 0, sizeof(*rng));
  22149. XMEMSET(key, 0, sizeof(*key));
  22150. XMEMSET(r, 0, sizeof(*r));
  22151. XMEMSET(s, 0, sizeof(*s));
  22152. ExpectIntEQ(wc_InitRng(rng), 0);
  22153. ExpectIntEQ(mp_init(r), 0);
  22154. ExpectIntEQ(mp_init(s), 0);
  22155. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  22156. ExpectIntEQ(wc_ecc_init(key), 0);
  22157. /* Test with no curve set. */
  22158. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  22159. BAD_FUNC_ARG);
  22160. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  22161. /* Test invalid parameters. */
  22162. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  22163. NULL), BAD_FUNC_ARG);
  22164. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, NULL, NULL,
  22165. NULL), BAD_FUNC_ARG);
  22166. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, NULL, NULL,
  22167. NULL), BAD_FUNC_ARG);
  22168. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, key, NULL,
  22169. NULL), BAD_FUNC_ARG);
  22170. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, r,
  22171. NULL), BAD_FUNC_ARG);
  22172. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  22173. s), BAD_FUNC_ARG);
  22174. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, key, r, s),
  22175. BAD_FUNC_ARG);
  22176. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, key, r, s),
  22177. BAD_FUNC_ARG);
  22178. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, NULL, r, s),
  22179. BAD_FUNC_ARG);
  22180. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, NULL, s),
  22181. BAD_FUNC_ARG);
  22182. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, NULL),
  22183. BAD_FUNC_ARG);
  22184. /* Make key not on the SM2 curve. */
  22185. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22186. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  22187. BAD_FUNC_ARG);
  22188. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22189. #ifdef WOLFSSL_SP_MATH_ALL
  22190. {
  22191. mp_int smallR[1];
  22192. sp_init_size(smallR, 1);
  22193. /* Force failure in _ecc_sm2_calc_r_s by r being too small. */
  22194. ExpectIntLT(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key,
  22195. smallR, s), 0);
  22196. }
  22197. #endif
  22198. /* Test valid parameters. */
  22199. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  22200. 0);
  22201. #ifdef HAVE_ECC_VERIFY
  22202. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash), &verified,
  22203. key), 0);
  22204. ExpectIntEQ(verified, 1);
  22205. #endif
  22206. mp_free(s);
  22207. mp_free(r);
  22208. wc_ecc_free(key);
  22209. wc_FreeRng(rng);
  22210. #ifdef FP_ECC
  22211. wc_ecc_fp_free();
  22212. #endif
  22213. res = EXPECT_RESULT();
  22214. #endif
  22215. return res;
  22216. }
  22217. /*
  22218. * Testing wc_ecc_sm2_verify_hash()
  22219. */
  22220. static int test_wc_ecc_sm2_sign_hash(void)
  22221. {
  22222. int res = TEST_SKIPPED;
  22223. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN)
  22224. EXPECT_DECLS;
  22225. WC_RNG rng[1];
  22226. ecc_key key[1];
  22227. unsigned char hash[32];
  22228. unsigned char sig[72];
  22229. word32 sigSz = sizeof(sig);
  22230. #ifdef HAVE_ECC_VERIFY
  22231. int verified;
  22232. #endif
  22233. XMEMSET(rng, 0, sizeof(*rng));
  22234. XMEMSET(key, 0, sizeof(*key));
  22235. ExpectIntEQ(wc_InitRng(rng), 0);
  22236. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  22237. ExpectIntEQ(wc_ecc_init(key), 0);
  22238. /* Test with no curve set. */
  22239. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  22240. BAD_FUNC_ARG);
  22241. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  22242. /* Test invalid parameters. */
  22243. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  22244. NULL), BAD_FUNC_ARG);
  22245. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, NULL, NULL,
  22246. NULL), BAD_FUNC_ARG);
  22247. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, NULL, NULL,
  22248. NULL), BAD_FUNC_ARG);
  22249. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, &sigSz, NULL,
  22250. NULL), BAD_FUNC_ARG);
  22251. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, rng,
  22252. NULL), BAD_FUNC_ARG);
  22253. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  22254. key), BAD_FUNC_ARG);
  22255. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, &sigSz, rng,
  22256. key), BAD_FUNC_ARG);
  22257. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, &sigSz, rng,
  22258. key), BAD_FUNC_ARG);
  22259. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, NULL, rng,
  22260. key), BAD_FUNC_ARG);
  22261. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, NULL,
  22262. key), BAD_FUNC_ARG);
  22263. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng,
  22264. NULL), BAD_FUNC_ARG);
  22265. /* Make key not on the SM2 curve. */
  22266. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22267. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  22268. BAD_FUNC_ARG);
  22269. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22270. /* Test valid parameters. */
  22271. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  22272. 0);
  22273. #ifdef HAVE_ECC_VERIFY
  22274. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sigSz, hash, sizeof(hash),
  22275. &verified, key), 0);
  22276. ExpectIntEQ(verified, 1);
  22277. #endif
  22278. wc_ecc_free(key);
  22279. wc_FreeRng(rng);
  22280. #ifdef FP_ECC
  22281. wc_ecc_fp_free();
  22282. #endif
  22283. res = EXPECT_RESULT();
  22284. #endif
  22285. return res;
  22286. }
  22287. /*
  22288. * Testing ToTraditional
  22289. */
  22290. static int test_ToTraditional(void)
  22291. {
  22292. EXPECT_DECLS;
  22293. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  22294. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  22295. defined(OPENSSL_EXTRA_X509_SMALL)) && !defined(NO_FILESYSTEM)
  22296. XFILE f = XBADFILE;
  22297. byte input[TWOK_BUF];
  22298. word32 sz;
  22299. ExpectTrue((f = XFOPEN("./certs/server-keyPkcs8.der", "rb")) != XBADFILE);
  22300. ExpectTrue((sz = (word32)XFREAD(input, 1, sizeof(input), f)) > 0);
  22301. if (f != XBADFILE)
  22302. XFCLOSE(f);
  22303. /* Good case */
  22304. ExpectIntGT(ToTraditional(input, sz), 0);
  22305. /* Bad cases */
  22306. ExpectIntEQ(ToTraditional(NULL, 0), BAD_FUNC_ARG);
  22307. ExpectIntEQ(ToTraditional(NULL, sz), BAD_FUNC_ARG);
  22308. #ifdef WOLFSSL_ASN_TEMPLATE
  22309. ExpectIntEQ(ToTraditional(input, 0), BUFFER_E);
  22310. #else
  22311. ExpectIntEQ(ToTraditional(input, 0), ASN_PARSE_E);
  22312. #endif
  22313. #endif
  22314. return EXPECT_RESULT();
  22315. } /* End test_ToTraditional*/
  22316. /*
  22317. * Testing wc_EccPrivateKeyToDer
  22318. */
  22319. static int test_wc_EccPrivateKeyToDer(void)
  22320. {
  22321. EXPECT_DECLS;
  22322. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22323. byte output[ONEK_BUF];
  22324. ecc_key eccKey;
  22325. WC_RNG rng;
  22326. word32 inLen;
  22327. word32 outLen = 0;
  22328. int ret;
  22329. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  22330. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22331. ExpectIntEQ(wc_InitRng(&rng), 0);
  22332. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  22333. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  22334. #if defined(WOLFSSL_ASYNC_CRYPT)
  22335. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22336. #endif
  22337. ExpectIntEQ(ret, 0);
  22338. inLen = (word32)sizeof(output);
  22339. /* Bad Cases */
  22340. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22341. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22342. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, NULL, inLen), LENGTH_ONLY_E);
  22343. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, output, 0), BAD_FUNC_ARG);
  22344. /* Good Case */
  22345. ExpectIntGT(outLen = wc_EccPrivateKeyToDer(&eccKey, output, inLen), 0);
  22346. wc_ecc_free(&eccKey);
  22347. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22348. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  22349. {
  22350. /* test importing private only into a PKEY struct */
  22351. EC_KEY* ec = NULL;
  22352. EVP_PKEY* pkey = NULL;
  22353. const unsigned char* der;
  22354. der = output;
  22355. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, outLen));
  22356. der = output;
  22357. ExpectNotNull(ec = d2i_ECPrivateKey(NULL, &der, outLen));
  22358. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  22359. if (EXPECT_FAIL()) {
  22360. EC_KEY_free(ec);
  22361. }
  22362. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  22363. }
  22364. #endif
  22365. #endif
  22366. return EXPECT_RESULT();
  22367. } /* End test_wc_EccPrivateKeyToDer*/
  22368. /*
  22369. * Testing wc_DhPublicKeyDecode
  22370. */
  22371. static int test_wc_DhPublicKeyDecode(void)
  22372. {
  22373. EXPECT_DECLS;
  22374. #ifndef NO_DH
  22375. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  22376. DhKey key;
  22377. word32 inOutIdx;
  22378. XMEMSET(&key, 0, sizeof(DhKey));
  22379. ExpectIntEQ(wc_InitDhKey(&key), 0);
  22380. ExpectIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0), BAD_FUNC_ARG);
  22381. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  22382. BAD_FUNC_ARG);
  22383. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  22384. BAD_FUNC_ARG);
  22385. inOutIdx = 0;
  22386. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  22387. BAD_FUNC_ARG);
  22388. inOutIdx = 0;
  22389. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  22390. BAD_FUNC_ARG);
  22391. inOutIdx = 0;
  22392. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  22393. sizeof_dh_pub_key_der_2048), 0);
  22394. ExpectIntNE(key.p.used, 0);
  22395. ExpectIntNE(key.g.used, 0);
  22396. ExpectIntEQ(key.q.used, 0);
  22397. ExpectIntNE(key.pub.used, 0);
  22398. ExpectIntEQ(key.priv.used, 0);
  22399. DoExpectIntEQ(wc_FreeDhKey(&key), 0);
  22400. #endif
  22401. #endif /* !NO_DH */
  22402. return EXPECT_RESULT();
  22403. }
  22404. /*
  22405. * Testing wc_Ed25519KeyToDer
  22406. */
  22407. static int test_wc_Ed25519KeyToDer(void)
  22408. {
  22409. EXPECT_DECLS;
  22410. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  22411. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22412. byte output[ONEK_BUF];
  22413. ed25519_key ed25519Key;
  22414. WC_RNG rng;
  22415. word32 inLen;
  22416. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  22417. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22418. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  22419. ExpectIntEQ(wc_InitRng(&rng), 0);
  22420. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  22421. inLen = (word32)sizeof(output);
  22422. /* Bad Cases */
  22423. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22424. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22425. ExpectIntEQ(wc_Ed25519KeyToDer(&ed25519Key, output, 0), BAD_FUNC_ARG);
  22426. /* Good Cases */
  22427. /* length only */
  22428. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen), 0);
  22429. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, output, inLen), 0);
  22430. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22431. wc_ed25519_free(&ed25519Key);
  22432. #endif
  22433. return EXPECT_RESULT();
  22434. } /* End test_wc_Ed25519KeyToDer*/
  22435. /*
  22436. * Testing wc_Ed25519PrivateKeyToDer
  22437. */
  22438. static int test_wc_Ed25519PrivateKeyToDer(void)
  22439. {
  22440. EXPECT_DECLS;
  22441. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  22442. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22443. byte output[ONEK_BUF];
  22444. ed25519_key ed25519PrivKey;
  22445. WC_RNG rng;
  22446. word32 inLen;
  22447. XMEMSET(&ed25519PrivKey, 0, sizeof(ed25519_key));
  22448. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22449. ExpectIntEQ(wc_ed25519_init(&ed25519PrivKey), 0);
  22450. ExpectIntEQ(wc_InitRng(&rng), 0);
  22451. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey),
  22452. 0);
  22453. inLen = (word32)sizeof(output);
  22454. /* Bad Cases */
  22455. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22456. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22457. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0),
  22458. BAD_FUNC_ARG);
  22459. /* Good Cases */
  22460. /* length only */
  22461. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen), 0);
  22462. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen), 0);
  22463. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22464. wc_ed25519_free(&ed25519PrivKey);
  22465. #endif
  22466. return EXPECT_RESULT();
  22467. } /* End test_wc_Ed25519PrivateKeyToDer*/
  22468. /*
  22469. * Testing wc_Ed448KeyToDer
  22470. */
  22471. static int test_wc_Ed448KeyToDer(void)
  22472. {
  22473. EXPECT_DECLS;
  22474. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  22475. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22476. byte output[ONEK_BUF];
  22477. ed448_key ed448Key;
  22478. WC_RNG rng;
  22479. word32 inLen;
  22480. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  22481. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22482. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  22483. ExpectIntEQ(wc_InitRng(&rng), 0);
  22484. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  22485. inLen = (word32)sizeof(output);
  22486. /* Bad Cases */
  22487. ExpectIntEQ(wc_Ed448KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22488. ExpectIntEQ(wc_Ed448KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22489. ExpectIntEQ(wc_Ed448KeyToDer(&ed448Key, output, 0), BAD_FUNC_ARG);
  22490. /* Good Cases */
  22491. /* length only */
  22492. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, NULL, inLen), 0);
  22493. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, output, inLen), 0);
  22494. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22495. wc_ed448_free(&ed448Key);
  22496. #endif
  22497. return EXPECT_RESULT();
  22498. } /* End test_wc_Ed448KeyToDer*/
  22499. /*
  22500. * Testing wc_Ed448PrivateKeyToDer
  22501. */
  22502. static int test_wc_Ed448PrivateKeyToDer(void)
  22503. {
  22504. EXPECT_DECLS;
  22505. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  22506. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22507. byte output[ONEK_BUF];
  22508. ed448_key ed448PrivKey;
  22509. WC_RNG rng;
  22510. word32 inLen;
  22511. XMEMSET(&ed448PrivKey, 0, sizeof(ed448_key));
  22512. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22513. ExpectIntEQ(wc_ed448_init(&ed448PrivKey), 0);
  22514. ExpectIntEQ(wc_InitRng(&rng), 0);
  22515. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey),
  22516. 0);
  22517. inLen = (word32)sizeof(output);
  22518. /* Bad Cases */
  22519. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22520. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22521. ExpectIntEQ(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0),
  22522. BAD_FUNC_ARG);
  22523. /* Good cases */
  22524. /* length only */
  22525. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen), 0);
  22526. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen), 0);
  22527. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22528. wc_ed448_free(&ed448PrivKey);
  22529. #endif
  22530. return EXPECT_RESULT();
  22531. } /* End test_wc_Ed448PrivateKeyToDer*/
  22532. /*
  22533. * Testing wc_SetSubjectBuffer
  22534. */
  22535. static int test_wc_SetSubjectBuffer(void)
  22536. {
  22537. EXPECT_DECLS;
  22538. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  22539. Cert cert;
  22540. XFILE file = XBADFILE;
  22541. byte* der = NULL;
  22542. word32 derSz;
  22543. derSz = FOURK_BUF;
  22544. ExpectNotNull(der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  22545. DYNAMIC_TYPE_TMP_BUFFER));
  22546. ExpectTrue((file = XFOPEN("./certs/ca-cert.der", "rb")) != XBADFILE);
  22547. ExpectTrue((derSz = (word32)XFREAD(der, 1, FOURK_BUF, file)) > 0);
  22548. if (file != XBADFILE)
  22549. XFCLOSE(file);
  22550. ExpectIntEQ(wc_InitCert(&cert), 0);
  22551. ExpectIntEQ(wc_SetSubjectBuffer(&cert, der, derSz), 0);
  22552. ExpectIntEQ(wc_SetSubjectBuffer(NULL, der, derSz), BAD_FUNC_ARG);
  22553. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22554. #endif
  22555. return EXPECT_RESULT();
  22556. } /* End test_wc_SetSubjectBuffer*/
  22557. /*
  22558. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  22559. */
  22560. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  22561. {
  22562. EXPECT_DECLS;
  22563. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  22564. WC_RNG rng;
  22565. Cert cert;
  22566. #if !defined(NO_RSA) && defined(HAVE_RSA)
  22567. RsaKey rsaKey;
  22568. int bits = 2048;
  22569. #endif
  22570. #if defined(HAVE_ECC)
  22571. ecc_key eccKey;
  22572. int ret;
  22573. #endif
  22574. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  22575. ed25519_key ed25519Key;
  22576. #endif
  22577. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  22578. ed448_key ed448Key;
  22579. #endif
  22580. #ifndef HAVE_FIPS
  22581. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  22582. #else
  22583. ExpectIntEQ(wc_InitRng(&rng), 0);
  22584. #endif
  22585. ExpectIntEQ(wc_InitCert(&cert), 0);
  22586. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  22587. /* RSA */
  22588. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  22589. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  22590. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  22591. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey),
  22592. 0);
  22593. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  22594. #endif
  22595. #if defined(HAVE_ECC)
  22596. /* ECC */
  22597. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  22598. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  22599. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  22600. #if defined(WOLFSSL_ASYNC_CRYPT)
  22601. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22602. #endif
  22603. ExpectIntEQ(ret, 0);
  22604. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey),
  22605. 0);
  22606. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  22607. #endif
  22608. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  22609. /* ED25519 */
  22610. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  22611. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  22612. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  22613. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  22614. &ed25519Key), 0);
  22615. wc_ed25519_free(&ed25519Key);
  22616. #endif
  22617. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  22618. /* ED448 */
  22619. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  22620. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  22621. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  22622. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  22623. &ed448Key), 0);
  22624. wc_ed448_free(&ed448Key);
  22625. #endif
  22626. wc_FreeRng(&rng);
  22627. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22628. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  22629. return EXPECT_RESULT();
  22630. } /* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  22631. /*
  22632. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  22633. */
  22634. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  22635. {
  22636. EXPECT_DECLS;
  22637. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  22638. WC_RNG rng;
  22639. Cert cert;
  22640. #if !defined(NO_RSA) && defined(HAVE_RSA)
  22641. RsaKey rsaKey;
  22642. int bits = 2048;
  22643. #endif
  22644. #if defined(HAVE_ECC)
  22645. ecc_key eccKey;
  22646. int ret;
  22647. #endif
  22648. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  22649. ed25519_key ed25519Key;
  22650. #endif
  22651. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  22652. ed448_key ed448Key;
  22653. #endif
  22654. #ifndef HAVE_FIPS
  22655. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  22656. #else
  22657. ExpectIntEQ(wc_InitRng(&rng), 0);
  22658. #endif
  22659. ExpectIntEQ(wc_InitCert(&cert), 0);
  22660. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  22661. /* RSA */
  22662. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  22663. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  22664. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  22665. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey), 0);
  22666. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  22667. #endif
  22668. #if defined(HAVE_ECC)
  22669. /* ECC */
  22670. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  22671. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  22672. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  22673. #if defined(WOLFSSL_ASYNC_CRYPT)
  22674. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22675. #endif
  22676. ExpectIntEQ(ret, 0);
  22677. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey), 0);
  22678. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  22679. #endif
  22680. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  22681. /* ED25519 */
  22682. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  22683. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  22684. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  22685. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  22686. &ed25519Key), 0);
  22687. wc_ed25519_free(&ed25519Key);
  22688. #endif
  22689. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  22690. /* ED448 */
  22691. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  22692. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  22693. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  22694. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, &ed448Key),
  22695. 0);
  22696. wc_ed448_free(&ed448Key);
  22697. #endif
  22698. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22699. #endif /* defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  22700. return EXPECT_RESULT();
  22701. } /* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  22702. /*
  22703. * Testing wc_PKCS7_New()
  22704. */
  22705. static int test_wc_PKCS7_New(void)
  22706. {
  22707. EXPECT_DECLS;
  22708. #if defined(HAVE_PKCS7)
  22709. PKCS7* pkcs7 = NULL;
  22710. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, testDevId));
  22711. wc_PKCS7_Free(pkcs7);
  22712. #endif
  22713. return EXPECT_RESULT();
  22714. } /* END test-wc_PKCS7_New */
  22715. /*
  22716. * Testing wc_PKCS7_Init()
  22717. */
  22718. static int test_wc_PKCS7_Init(void)
  22719. {
  22720. EXPECT_DECLS;
  22721. #if defined(HAVE_PKCS7)
  22722. PKCS7* pkcs7 = NULL;
  22723. void* heap = NULL;
  22724. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  22725. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  22726. /* Pass in bad args. */
  22727. ExpectIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  22728. wc_PKCS7_Free(pkcs7);
  22729. #endif
  22730. return EXPECT_RESULT();
  22731. } /* END test-wc_PKCS7_Init */
  22732. /*
  22733. * Testing wc_PKCS7_InitWithCert()
  22734. */
  22735. static int test_wc_PKCS7_InitWithCert(void)
  22736. {
  22737. EXPECT_DECLS;
  22738. #if defined(HAVE_PKCS7)
  22739. PKCS7* pkcs7 = NULL;
  22740. #ifndef NO_RSA
  22741. #if defined(USE_CERT_BUFFERS_2048)
  22742. unsigned char cert[sizeof(client_cert_der_2048)];
  22743. int certSz = (int)sizeof(cert);
  22744. XMEMSET(cert, 0, certSz);
  22745. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  22746. #elif defined(USE_CERT_BUFFERS_1024)
  22747. unsigned char cert[sizeof(client_cert_der_1024)];
  22748. int certSz = (int)sizeof(cert);
  22749. XMEMSET(cert, 0, certSz);
  22750. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  22751. #else
  22752. unsigned char cert[ONEK_BUF];
  22753. XFILE fp = XBADFILE;
  22754. int certSz;
  22755. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  22756. XBADFILE);
  22757. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  22758. fp), 0);
  22759. if (fp != XBADFILE)
  22760. XFCLOSE(fp);
  22761. #endif
  22762. #elif defined(HAVE_ECC)
  22763. #if defined(USE_CERT_BUFFERS_256)
  22764. unsigned char cert[sizeof(cliecc_cert_der_256)];
  22765. int certSz = (int)sizeof(cert);
  22766. XMEMSET(cert, 0, certSz);
  22767. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  22768. #else
  22769. unsigned char cert[ONEK_BUF];
  22770. XFILE fp = XBADFILE;
  22771. int certSz;
  22772. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  22773. XBADFILE);
  22774. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256),
  22775. fp), 0);
  22776. if (fp != XBADFILE)
  22777. XFCLOSE(fp);
  22778. #endif
  22779. #else
  22780. #error PKCS7 requires ECC or RSA
  22781. #endif
  22782. #ifdef HAVE_ECC
  22783. {
  22784. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  22785. static unsigned char certWithInvalidEccKey[] = {
  22786. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  22787. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  22788. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  22789. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  22790. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  22791. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  22792. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  22793. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  22794. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  22795. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  22796. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  22797. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  22798. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  22799. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  22800. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  22801. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  22802. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  22803. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  22804. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  22805. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  22806. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  22807. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  22808. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  22809. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  22810. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  22811. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  22812. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  22813. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  22814. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  22815. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  22816. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  22817. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  22818. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  22819. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  22820. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  22821. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  22822. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  22823. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  22824. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  22825. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  22826. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  22827. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  22828. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  22829. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  22830. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  22831. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  22832. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  22833. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  22834. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  22835. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  22836. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  22837. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  22838. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  22839. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  22840. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  22841. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  22842. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  22843. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  22844. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  22845. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  22846. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  22847. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  22848. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  22849. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  22850. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  22851. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  22852. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  22853. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  22854. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  22855. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  22856. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  22857. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  22858. 0x08, 0xA8, 0xB4
  22859. };
  22860. #endif
  22861. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22862. /* If initialization is not successful, it's free'd in init func. */
  22863. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz),
  22864. 0);
  22865. wc_PKCS7_Free(pkcs7);
  22866. pkcs7 = NULL;
  22867. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22868. /* Valid initialization usage. */
  22869. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22870. /* Pass in bad args. No need free for null checks, free at end.*/
  22871. ExpectIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  22872. BAD_FUNC_ARG);
  22873. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  22874. BAD_FUNC_ARG);
  22875. #ifdef HAVE_ECC
  22876. ExpectIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  22877. sizeof(certWithInvalidEccKey)), 0);
  22878. }
  22879. #endif
  22880. wc_PKCS7_Free(pkcs7);
  22881. #endif
  22882. return EXPECT_RESULT();
  22883. } /* END test_wc_PKCS7_InitWithCert */
  22884. /*
  22885. * Testing wc_PKCS7_EncodeData()
  22886. */
  22887. static int test_wc_PKCS7_EncodeData(void)
  22888. {
  22889. EXPECT_DECLS;
  22890. #if defined(HAVE_PKCS7)
  22891. PKCS7* pkcs7 = NULL;
  22892. byte output[FOURK_BUF];
  22893. byte data[] = "My encoded DER cert.";
  22894. #ifndef NO_RSA
  22895. #if defined(USE_CERT_BUFFERS_2048)
  22896. unsigned char cert[sizeof(client_cert_der_2048)];
  22897. unsigned char key[sizeof(client_key_der_2048)];
  22898. int certSz = (int)sizeof(cert);
  22899. int keySz = (int)sizeof(key);
  22900. XMEMSET(cert, 0, certSz);
  22901. XMEMSET(key, 0, keySz);
  22902. XMEMCPY(cert, client_cert_der_2048, certSz);
  22903. XMEMCPY(key, client_key_der_2048, keySz);
  22904. #elif defined(USE_CERT_BUFFERS_1024)
  22905. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  22906. unsigned char key[sizeof_client_key_der_1024];
  22907. int certSz = (int)sizeof(cert);
  22908. int keySz = (int)sizeof(key);
  22909. XMEMSET(cert, 0, certSz);
  22910. XMEMSET(key, 0, keySz);
  22911. XMEMCPY(cert, client_cert_der_1024, certSz);
  22912. XMEMCPY(key, client_key_der_1024, keySz);
  22913. #else
  22914. unsigned char cert[ONEK_BUF];
  22915. unsigned char key[ONEK_BUF];
  22916. XFILE fp = XBADFILE;
  22917. int certSz;
  22918. int keySz;
  22919. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  22920. XBADFILE);
  22921. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  22922. fp), 0);
  22923. if (fp != XBADFILE) {
  22924. XFCLOSE(fp);
  22925. fp = XBADFILE;
  22926. }
  22927. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  22928. XBADFILE);
  22929. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  22930. 0);
  22931. if (fp != XBADFILE)
  22932. XFCLOSE(fp);
  22933. #endif
  22934. #elif defined(HAVE_ECC)
  22935. #if defined(USE_CERT_BUFFERS_256)
  22936. unsigned char cert[sizeof(cliecc_cert_der_256)];
  22937. unsigned char key[sizeof(ecc_clikey_der_256)];
  22938. int certSz = (int)sizeof(cert);
  22939. int keySz = (int)sizeof(key);
  22940. XMEMSET(cert, 0, certSz);
  22941. XMEMSET(key, 0, keySz);
  22942. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  22943. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  22944. #else
  22945. unsigned char cert[ONEK_BUF];
  22946. unsigned char key[ONEK_BUF];
  22947. XFILE fp = XBADFILE;
  22948. int certSz, keySz;
  22949. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  22950. XBADFILE);
  22951. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  22952. fp), 0);
  22953. if (fp != XBADFILE) {
  22954. XFCLOSE(fp);
  22955. fp = XBADFILE;
  22956. }
  22957. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  22958. XBADFILE);
  22959. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  22960. 0);
  22961. if (fp != XBADFILE)
  22962. XFCLOSE(fp);
  22963. #endif
  22964. #endif
  22965. XMEMSET(output, 0, sizeof(output));
  22966. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22967. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  22968. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  22969. if (pkcs7 != NULL) {
  22970. pkcs7->content = data;
  22971. pkcs7->contentSz = sizeof(data);
  22972. pkcs7->privateKey = key;
  22973. pkcs7->privateKeySz = keySz;
  22974. }
  22975. ExpectIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  22976. /* Test bad args. */
  22977. ExpectIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  22978. BAD_FUNC_ARG);
  22979. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  22980. BAD_FUNC_ARG);
  22981. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  22982. wc_PKCS7_Free(pkcs7);
  22983. #endif
  22984. return EXPECT_RESULT();
  22985. } /* END test_wc_PKCS7_EncodeData */
  22986. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  22987. !defined(NO_RSA) && !defined(NO_SHA256)
  22988. /* RSA sign raw digest callback */
  22989. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  22990. byte* out, word32 outSz, byte* privateKey,
  22991. word32 privateKeySz, int devid, int hashOID)
  22992. {
  22993. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  22994. byte digInfoEncoding[] = {
  22995. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  22996. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  22997. 0x00, 0x04, 0x20
  22998. };
  22999. int ret;
  23000. byte digestInfo[ONEK_BUF];
  23001. byte sig[FOURK_BUF];
  23002. word32 digestInfoSz = 0;
  23003. word32 idx = 0;
  23004. RsaKey rsa;
  23005. /* SHA-256 required only for this example callback due to above
  23006. * digInfoEncoding[] */
  23007. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  23008. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  23009. (hashOID != SHA256h)) {
  23010. return -1;
  23011. }
  23012. /* build DigestInfo */
  23013. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  23014. digestInfoSz += sizeof(digInfoEncoding);
  23015. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  23016. digestInfoSz += digestSz;
  23017. /* set up RSA key */
  23018. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  23019. if (ret != 0) {
  23020. return ret;
  23021. }
  23022. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  23023. /* sign DigestInfo */
  23024. if (ret == 0) {
  23025. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  23026. &rsa, pkcs7->rng);
  23027. if (ret > 0) {
  23028. if (ret > (int)outSz) {
  23029. /* output buffer too small */
  23030. ret = -1;
  23031. }
  23032. else {
  23033. /* success, ret holds sig size */
  23034. XMEMCPY(out, sig, ret);
  23035. }
  23036. }
  23037. }
  23038. wc_FreeRsaKey(&rsa);
  23039. return ret;
  23040. }
  23041. #endif
  23042. /*
  23043. * Testing wc_PKCS7_EncodeSignedData()
  23044. */
  23045. static int test_wc_PKCS7_EncodeSignedData(void)
  23046. {
  23047. EXPECT_DECLS;
  23048. #if defined(HAVE_PKCS7)
  23049. PKCS7* pkcs7 = NULL;
  23050. WC_RNG rng;
  23051. byte output[FOURK_BUF];
  23052. byte badOut[1];
  23053. word32 outputSz = (word32)sizeof(output);
  23054. word32 badOutSz = 0;
  23055. byte data[] = "Test data to encode.";
  23056. #ifndef NO_RSA
  23057. #if defined(USE_CERT_BUFFERS_2048)
  23058. byte key[sizeof(client_key_der_2048)];
  23059. byte cert[sizeof(client_cert_der_2048)];
  23060. word32 keySz = (word32)sizeof(key);
  23061. word32 certSz = (word32)sizeof(cert);
  23062. XMEMSET(key, 0, keySz);
  23063. XMEMSET(cert, 0, certSz);
  23064. XMEMCPY(key, client_key_der_2048, keySz);
  23065. XMEMCPY(cert, client_cert_der_2048, certSz);
  23066. #elif defined(USE_CERT_BUFFERS_1024)
  23067. byte key[sizeof_client_key_der_1024];
  23068. byte cert[sizeof(sizeof_client_cert_der_1024)];
  23069. word32 keySz = (word32)sizeof(key);
  23070. word32 certSz = (word32)sizeof(cert);
  23071. XMEMSET(key, 0, keySz);
  23072. XMEMSET(cert, 0, certSz);
  23073. XMEMCPY(key, client_key_der_1024, keySz);
  23074. XMEMCPY(cert, client_cert_der_1024, certSz);
  23075. #else
  23076. unsigned char cert[ONEK_BUF];
  23077. unsigned char key[ONEK_BUF];
  23078. XFILE fp = XBADFILE;
  23079. int certSz;
  23080. int keySz;
  23081. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23082. XBADFILE);
  23083. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23084. fp), 0);
  23085. if (fp != XBADFILE) {
  23086. XFCLOSE(fp);
  23087. fp = XBADFILE;
  23088. }
  23089. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  23090. XBADFILE);
  23091. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  23092. 0);
  23093. if (fp != XBADFILE)
  23094. XFCLOSE(fp);
  23095. #endif
  23096. #elif defined(HAVE_ECC)
  23097. #if defined(USE_CERT_BUFFERS_256)
  23098. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23099. unsigned char key[sizeof(ecc_clikey_der_256)];
  23100. int certSz = (int)sizeof(cert);
  23101. int keySz = (int)sizeof(key);
  23102. XMEMSET(cert, 0, certSz);
  23103. XMEMSET(key, 0, keySz);
  23104. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  23105. XMEMCPY(key, ecc_clikey_der_256, keySz);
  23106. #else
  23107. unsigned char cert[ONEK_BUF];
  23108. unsigned char key[ONEK_BUF];
  23109. XFILE fp = XBADFILE;
  23110. int certSz;
  23111. int keySz;
  23112. ExpectTrue((fp = XOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23113. XBADFILE);
  23114. ExpectIntGT(certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp), 0);
  23115. if (fp != XBADFILE) {
  23116. XFCLOSE(fp);
  23117. fp = XBADFILE;
  23118. }
  23119. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  23120. XBADFILE);
  23121. ExpectIntGT(keySz = (int)XFREAD(key, 1, ONEK_BUF, fp), 0);
  23122. if (fp != XBADFILE)
  23123. XFCLOSE(fp);
  23124. #endif
  23125. #endif
  23126. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23127. XMEMSET(output, 0, outputSz);
  23128. ExpectIntEQ(wc_InitRng(&rng), 0);
  23129. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23130. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23131. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23132. if (pkcs7 != NULL) {
  23133. pkcs7->content = data;
  23134. pkcs7->contentSz = (word32)sizeof(data);
  23135. pkcs7->privateKey = key;
  23136. pkcs7->privateKeySz = (word32)sizeof(key);
  23137. pkcs7->encryptOID = RSAk;
  23138. #ifdef NO_SHA
  23139. pkcs7->hashOID = SHA256h;
  23140. #else
  23141. pkcs7->hashOID = SHAh;
  23142. #endif
  23143. pkcs7->rng = &rng;
  23144. }
  23145. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  23146. wc_PKCS7_Free(pkcs7);
  23147. pkcs7 = NULL;
  23148. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23149. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23150. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23151. /* Pass in bad args. */
  23152. ExpectIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz),
  23153. BAD_FUNC_ARG);
  23154. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  23155. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  23156. badOutSz), BAD_FUNC_ARG);
  23157. if (pkcs7 != NULL) {
  23158. pkcs7->hashOID = 0; /* bad hashOID */
  23159. }
  23160. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz),
  23161. BAD_FUNC_ARG);
  23162. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  23163. !defined(NO_RSA) && !defined(NO_SHA256)
  23164. /* test RSA sign raw digest callback, if using RSA and compiled in.
  23165. * Example callback assumes SHA-256, so only run test if compiled in. */
  23166. wc_PKCS7_Free(pkcs7);
  23167. pkcs7 = NULL;
  23168. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23169. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23170. if (pkcs7 != NULL) {
  23171. pkcs7->content = data;
  23172. pkcs7->contentSz = (word32)sizeof(data);
  23173. pkcs7->privateKey = key;
  23174. pkcs7->privateKeySz = (word32)sizeof(key);
  23175. pkcs7->encryptOID = RSAk;
  23176. pkcs7->hashOID = SHA256h;
  23177. pkcs7->rng = &rng;
  23178. }
  23179. ExpectIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  23180. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  23181. #endif
  23182. wc_PKCS7_Free(pkcs7);
  23183. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23184. #endif
  23185. return EXPECT_RESULT();
  23186. } /* END test_wc_PKCS7_EncodeSignedData */
  23187. /*
  23188. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  23189. */
  23190. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  23191. {
  23192. EXPECT_DECLS;
  23193. #if defined(HAVE_PKCS7)
  23194. int i;
  23195. PKCS7* pkcs7 = NULL;
  23196. WC_RNG rng;
  23197. byte outputHead[FOURK_BUF/2];
  23198. byte outputFoot[FOURK_BUF/2];
  23199. word32 outputHeadSz = (word32)sizeof(outputHead);
  23200. word32 outputFootSz = (word32)sizeof(outputFoot);
  23201. byte data[FOURK_BUF];
  23202. wc_HashAlg hash;
  23203. #ifdef NO_SHA
  23204. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  23205. #else
  23206. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  23207. #endif
  23208. byte hashBuf[WC_MAX_DIGEST_SIZE];
  23209. word32 hashSz = wc_HashGetDigestSize(hashType);
  23210. #ifndef NO_RSA
  23211. #if defined(USE_CERT_BUFFERS_2048)
  23212. byte key[sizeof(client_key_der_2048)];
  23213. byte cert[sizeof(client_cert_der_2048)];
  23214. word32 keySz = (word32)sizeof(key);
  23215. word32 certSz = (word32)sizeof(cert);
  23216. XMEMSET(key, 0, keySz);
  23217. XMEMSET(cert, 0, certSz);
  23218. XMEMCPY(key, client_key_der_2048, keySz);
  23219. XMEMCPY(cert, client_cert_der_2048, certSz);
  23220. #elif defined(USE_CERT_BUFFERS_1024)
  23221. byte key[sizeof_client_key_der_1024];
  23222. byte cert[sizeof(sizeof_client_cert_der_1024)];
  23223. word32 keySz = (word32)sizeof(key);
  23224. word32 certSz = (word32)sizeof(cert);
  23225. XMEMSET(key, 0, keySz);
  23226. XMEMSET(cert, 0, certSz);
  23227. XMEMCPY(key, client_key_der_1024, keySz);
  23228. XMEMCPY(cert, client_cert_der_1024, certSz);
  23229. #else
  23230. unsigned char cert[ONEK_BUF];
  23231. unsigned char key[ONEK_BUF];
  23232. XFILE fp = XBADFILE;
  23233. int certSz;
  23234. int keySz;
  23235. ExpectTure((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23236. XBADFILE);
  23237. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23238. fp), 0);
  23239. if (fp != XBADFILE) {
  23240. XFCLOSE(fp);
  23241. fp = XBADFILE;
  23242. }
  23243. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  23244. XBADFILE);
  23245. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  23246. 0);
  23247. if (fp != XBADFILE)
  23248. XFCLOSE(fp);
  23249. #endif
  23250. #elif defined(HAVE_ECC)
  23251. #if defined(USE_CERT_BUFFERS_256)
  23252. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23253. unsigned char key[sizeof(ecc_clikey_der_256)];
  23254. int certSz = (int)sizeof(cert);
  23255. int keySz = (int)sizeof(key);
  23256. XMEMSET(cert, 0, certSz);
  23257. XMEMSET(key, 0, keySz);
  23258. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23259. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23260. #else
  23261. unsigned char cert[ONEK_BUF];
  23262. unsigned char key[ONEK_BUF];
  23263. XFILE fp = XBADFILE;
  23264. int certSz;
  23265. int keySz;
  23266. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23267. XBADFILE);
  23268. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  23269. fp), 0);
  23270. if (fp != XBADFILE) {
  23271. XFCLOSE(fp);
  23272. fp = XBADFILE;
  23273. }
  23274. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  23275. XBADFILE);
  23276. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  23277. 0);
  23278. if (fp != XBADFILE)
  23279. XFCLOSE(fp);
  23280. #endif
  23281. #endif
  23282. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23283. /* initialize large data with sequence */
  23284. for (i=0; i<(int)sizeof(data); i++)
  23285. data[i] = i & 0xff;
  23286. XMEMSET(outputHead, 0, outputHeadSz);
  23287. XMEMSET(outputFoot, 0, outputFootSz);
  23288. ExpectIntEQ(wc_InitRng(&rng), 0);
  23289. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23290. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23291. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23292. if (pkcs7 != NULL) {
  23293. pkcs7->content = NULL; /* not used for ex */
  23294. pkcs7->contentSz = (word32)sizeof(data);
  23295. pkcs7->privateKey = key;
  23296. pkcs7->privateKeySz = (word32)sizeof(key);
  23297. pkcs7->encryptOID = RSAk;
  23298. #ifdef NO_SHA
  23299. pkcs7->hashOID = SHA256h;
  23300. #else
  23301. pkcs7->hashOID = SHAh;
  23302. #endif
  23303. pkcs7->rng = &rng;
  23304. }
  23305. /* calculate hash for content */
  23306. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  23307. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  23308. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  23309. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  23310. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  23311. /* Perform PKCS7 sign using hash directly */
  23312. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23313. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  23314. ExpectIntGT(outputHeadSz, 0);
  23315. ExpectIntGT(outputFootSz, 0);
  23316. wc_PKCS7_Free(pkcs7);
  23317. pkcs7 = NULL;
  23318. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23319. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23320. /* required parameter even on verify when using _ex, if using outputHead
  23321. * and outputFoot */
  23322. if (pkcs7 != NULL) {
  23323. pkcs7->contentSz = (word32)sizeof(data);
  23324. }
  23325. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23326. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  23327. wc_PKCS7_Free(pkcs7);
  23328. pkcs7 = NULL;
  23329. /* assembly complete PKCS7 sign and use normal verify */
  23330. {
  23331. byte* output = NULL;
  23332. word32 outputSz = 0;
  23333. ExpectNotNull(output = (byte*)XMALLOC(
  23334. outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT,
  23335. DYNAMIC_TYPE_TMP_BUFFER));
  23336. if (output != NULL) {
  23337. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  23338. outputSz += outputHeadSz;
  23339. XMEMCPY(&output[outputSz], data, sizeof(data));
  23340. outputSz += sizeof(data);
  23341. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  23342. outputSz += outputFootSz;
  23343. }
  23344. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23345. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23346. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23347. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23348. }
  23349. /* Pass in bad args. */
  23350. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  23351. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23352. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  23353. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23354. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  23355. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23356. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  23357. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23358. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23359. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23360. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23361. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  23362. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23363. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  23364. if (pkcs7 != NULL) {
  23365. pkcs7->hashOID = 0; /* bad hashOID */
  23366. }
  23367. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23368. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23369. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  23370. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23371. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  23372. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23373. #ifndef NO_PKCS7_STREAM
  23374. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  23375. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  23376. #else
  23377. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  23378. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  23379. #endif
  23380. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  23381. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23382. #ifndef NO_PKCS7_STREAM
  23383. /* can pass in 0 buffer length with streaming API */
  23384. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23385. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  23386. #else
  23387. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23388. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23389. #endif
  23390. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23391. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  23392. #ifndef NO_PKCS7_STREAM
  23393. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23394. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  23395. #else
  23396. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23397. outputHead, outputHeadSz, outputFoot, 0), BUFFER_E);
  23398. #endif
  23399. wc_PKCS7_Free(pkcs7);
  23400. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23401. #endif
  23402. return EXPECT_RESULT();
  23403. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  23404. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  23405. /**
  23406. * Loads certs/keys from files or buffers into the argument buffers,
  23407. * helper function called by CreatePKCS7SignedData().
  23408. *
  23409. * Returns 0 on success, negative on error.
  23410. */
  23411. static int LoadPKCS7SignedDataCerts(
  23412. int useIntermediateCertChain, int pkAlgoType,
  23413. byte* intCARoot, word32* intCARootSz,
  23414. byte* intCA1, word32* intCA1Sz,
  23415. byte* intCA2, word32* intCA2Sz,
  23416. byte* cert, word32* certSz,
  23417. byte* key, word32* keySz)
  23418. {
  23419. EXPECT_DECLS;
  23420. int ret = 0;
  23421. XFILE fp = XBADFILE;
  23422. #ifndef NO_RSA
  23423. const char* intCARootRSA = "./certs/ca-cert.der";
  23424. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  23425. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  23426. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  23427. const char* intServKeyRSA = "./certs/server-key.der";
  23428. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  23429. const char* cli1024Cert = "./certs/1024/client-cert.der";
  23430. const char* cli1024Key = "./certs/1024/client-key.der";
  23431. #endif
  23432. #endif
  23433. #ifdef HAVE_ECC
  23434. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  23435. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  23436. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  23437. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  23438. const char* intServKeyECC = "./certs/ecc-key.der";
  23439. #ifndef USE_CERT_BUFFERS_256
  23440. const char* cliEccCert = "./certs/client-ecc-cert.der";
  23441. const char* cliEccKey = "./certs/client-ecc-key.der";
  23442. #endif
  23443. #endif
  23444. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  23445. ((useIntermediateCertChain == 1) &&
  23446. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  23447. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  23448. return BAD_FUNC_ARG;
  23449. }
  23450. /* Read/load certs and keys to use for signing based on PK type and chain */
  23451. switch (pkAlgoType) {
  23452. #ifndef NO_RSA
  23453. case RSA_TYPE:
  23454. if (useIntermediateCertChain == 1) {
  23455. ExpectTrue((fp = XFOPEN(intCARootRSA, "rb")) != XBADFILE);
  23456. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  23457. if (fp != XBADFILE) {
  23458. XFCLOSE(fp);
  23459. fp = XBADFILE;
  23460. }
  23461. ExpectIntGT(*intCARootSz, 0);
  23462. ExpectTrue((fp = XFOPEN(intCA1RSA, "rb")) != XBADFILE);
  23463. if (fp != XBADFILE) {
  23464. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  23465. XFCLOSE(fp);
  23466. fp = XBADFILE;
  23467. }
  23468. ExpectIntGT(*intCA1Sz, 0);
  23469. ExpectTrue((fp = XFOPEN(intCA2RSA, "rb")) != XBADFILE);
  23470. if (fp != XBADFILE) {
  23471. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  23472. XFCLOSE(fp);
  23473. fp = XBADFILE;
  23474. }
  23475. ExpectIntGT(*intCA2Sz, 0);
  23476. ExpectTrue((fp = XFOPEN(intServCertRSA, "rb")) != XBADFILE);
  23477. if (fp != XBADFILE) {
  23478. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  23479. XFCLOSE(fp);
  23480. fp = XBADFILE;
  23481. }
  23482. ExpectIntGT(*certSz, 0);
  23483. ExpectTrue((fp = XFOPEN(intServKeyRSA, "rb")) != XBADFILE);
  23484. if (fp != XBADFILE) {
  23485. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  23486. XFCLOSE(fp);
  23487. fp = XBADFILE;
  23488. }
  23489. ExpectIntGT(*keySz, 0);
  23490. }
  23491. else {
  23492. #if defined(USE_CERT_BUFFERS_2048)
  23493. *keySz = sizeof_client_key_der_2048;
  23494. *certSz = sizeof_client_cert_der_2048;
  23495. XMEMCPY(key, client_key_der_2048, *keySz);
  23496. XMEMCPY(cert, client_cert_der_2048, *certSz);
  23497. #elif defined(USE_CERT_BUFFERS_1024)
  23498. *keySz = sizeof_client_key_der_1024;
  23499. *certSz = sizeof_client_cert_der_1024;
  23500. XMEMCPY(key, client_key_der_1024, *keySz);
  23501. XMEMCPY(cert, client_cert_der_1024, *certSz);
  23502. #else
  23503. ExpectTrue((fp = XFOPEN(cli1024Key, "rb")) != XBADFILE);
  23504. if (fp != XBADFILE) {
  23505. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  23506. XFCLOSE(fp);
  23507. fp = XBADFILE;
  23508. }
  23509. ExpectIntGT(*keySz, 0);
  23510. ExpectTrue((fp = XFOPEN(cli1024Cert, "rb")) != XBADFILE);
  23511. if (fp != XBADFILE) {
  23512. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  23513. XFCLOSE(fp);
  23514. fp = XBADFILE;
  23515. }
  23516. ExpectIntGT(*certSz, 0);
  23517. #endif /* USE_CERT_BUFFERS_2048 */
  23518. }
  23519. break;
  23520. #endif /* !NO_RSA */
  23521. #ifdef HAVE_ECC
  23522. case ECC_TYPE:
  23523. if (useIntermediateCertChain == 1) {
  23524. ExpectTrue((fp = XFOPEN(intCARootECC, "rb")) != XBADFILE);
  23525. if (fp != XBADFILE) {
  23526. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz,
  23527. fp);
  23528. XFCLOSE(fp);
  23529. fp = XBADFILE;
  23530. }
  23531. ExpectIntGT(*intCARootSz, 0);
  23532. ExpectTrue((fp = XFOPEN(intCA1ECC, "rb")) != XBADFILE);
  23533. if (fp != XBADFILE) {
  23534. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  23535. XFCLOSE(fp);
  23536. fp = XBADFILE;
  23537. }
  23538. ExpectIntGT(*intCA1Sz, 0);
  23539. ExpectTrue((fp = XFOPEN(intCA2ECC, "rb")) != XBADFILE);
  23540. if (fp != XBADFILE) {
  23541. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  23542. XFCLOSE(fp);
  23543. fp = XBADFILE;
  23544. }
  23545. ExpectIntGT(*intCA2Sz, 0);
  23546. ExpectTrue((fp = XFOPEN(intServCertECC, "rb")) != XBADFILE);
  23547. if (fp != XBADFILE) {
  23548. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  23549. XFCLOSE(fp);
  23550. fp = XBADFILE;
  23551. }
  23552. ExpectIntGT(*certSz, 0);
  23553. ExpectTrue((fp = XFOPEN(intServKeyECC, "rb")) != XBADFILE);
  23554. if (fp != XBADFILE) {
  23555. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  23556. XFCLOSE(fp);
  23557. fp = XBADFILE;
  23558. }
  23559. ExpectIntGT(*keySz, 0);
  23560. }
  23561. else {
  23562. #if defined(USE_CERT_BUFFERS_256)
  23563. *keySz = sizeof_ecc_clikey_der_256;
  23564. *certSz = sizeof_cliecc_cert_der_256;
  23565. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  23566. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  23567. #else
  23568. ExpectTrue((fp = XFOPEN(cliEccKey, "rb")) != XBADFILE);
  23569. if (fp != XBADFILE) {
  23570. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  23571. XFCLOSE(fp);
  23572. fp = XBADFILE;
  23573. }
  23574. ExpectIntGT(*keySz, 0);
  23575. ExpectTrue((fp = XFOPEN(cliEccCert, "rb")) != XBADFILE);
  23576. if (fp != XBADFILE) {
  23577. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  23578. XFCLOSE(fp);
  23579. fp = XBADFILE;
  23580. }
  23581. ExpectIntGT(*certSz, 0);
  23582. #endif /* USE_CERT_BUFFERS_256 */
  23583. }
  23584. break;
  23585. #endif /* HAVE_ECC */
  23586. default:
  23587. WOLFSSL_MSG("Unsupported SignedData PK type");
  23588. ret = BAD_FUNC_ARG;
  23589. break;
  23590. }
  23591. if (EXPECT_FAIL() && (ret == 0)) {
  23592. ret = BAD_FUNC_ARG;
  23593. }
  23594. return ret;
  23595. }
  23596. /**
  23597. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  23598. *
  23599. * output output buffer to place SignedData
  23600. * outputSz size of output buffer
  23601. * data data buffer to be signed
  23602. * dataSz size of data buffer
  23603. * withAttribs [1/0] include attributes in SignedData message
  23604. * detachedSig [1/0] create detached signature, no content
  23605. * useIntCertChain [1/0] use certificate chain and include intermediate and
  23606. * root CAs in bundle
  23607. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  23608. *
  23609. * Return size of bundle created on success, negative on error */
  23610. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  23611. byte* data, word32 dataSz,
  23612. int withAttribs, int detachedSig,
  23613. int useIntermediateCertChain,
  23614. int pkAlgoType)
  23615. {
  23616. EXPECT_DECLS;
  23617. int ret = 0;
  23618. WC_RNG rng;
  23619. PKCS7* pkcs7 = NULL;
  23620. static byte messageTypeOid[] =
  23621. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  23622. 0x09, 0x02 };
  23623. static byte messageType[] = { 0x13, 2, '1', '9' };
  23624. PKCS7Attrib attribs[] =
  23625. {
  23626. { messageTypeOid, sizeof(messageTypeOid), messageType,
  23627. sizeof(messageType) }
  23628. };
  23629. byte intCARoot[TWOK_BUF];
  23630. byte intCA1[TWOK_BUF];
  23631. byte intCA2[TWOK_BUF];
  23632. byte cert[TWOK_BUF];
  23633. byte key[TWOK_BUF];
  23634. word32 intCARootSz = sizeof(intCARoot);
  23635. word32 intCA1Sz = sizeof(intCA1);
  23636. word32 intCA2Sz = sizeof(intCA2);
  23637. word32 certSz = sizeof(cert);
  23638. word32 keySz = sizeof(key);
  23639. XMEMSET(intCARoot, 0, intCARootSz);
  23640. XMEMSET(intCA1, 0, intCA1Sz);
  23641. XMEMSET(intCA2, 0, intCA2Sz);
  23642. XMEMSET(cert, 0, certSz);
  23643. XMEMSET(key, 0, keySz);
  23644. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  23645. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  23646. cert, &certSz, key, &keySz);
  23647. ExpectIntEQ(ret, 0);
  23648. XMEMSET(output, 0, outputSz);
  23649. ExpectIntEQ(wc_InitRng(&rng), 0);
  23650. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23651. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23652. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23653. if (useIntermediateCertChain == 1) {
  23654. /* Add intermediate and root CA certs into SignedData Certs SET */
  23655. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  23656. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  23657. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  23658. }
  23659. if (pkcs7 != NULL) {
  23660. pkcs7->content = data;
  23661. pkcs7->contentSz = dataSz;
  23662. pkcs7->privateKey = key;
  23663. pkcs7->privateKeySz = (word32)sizeof(key);
  23664. if (pkAlgoType == RSA_TYPE) {
  23665. pkcs7->encryptOID = RSAk;
  23666. }
  23667. else {
  23668. pkcs7->encryptOID = ECDSAk;
  23669. }
  23670. #ifdef NO_SHA
  23671. pkcs7->hashOID = SHA256h;
  23672. #else
  23673. pkcs7->hashOID = SHAh;
  23674. #endif
  23675. pkcs7->rng = &rng;
  23676. if (withAttribs) {
  23677. /* include a signed attribute */
  23678. pkcs7->signedAttribs = attribs;
  23679. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  23680. }
  23681. }
  23682. if (detachedSig) {
  23683. ExpectIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  23684. }
  23685. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz);
  23686. ExpectIntGT(outputSz, 0);
  23687. wc_PKCS7_Free(pkcs7);
  23688. pkcs7 = NULL;
  23689. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23690. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23691. if (detachedSig && (pkcs7 != NULL)) {
  23692. pkcs7->content = data;
  23693. pkcs7->contentSz = dataSz;
  23694. }
  23695. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23696. wc_PKCS7_Free(pkcs7);
  23697. wc_FreeRng(&rng);
  23698. if (EXPECT_FAIL()) {
  23699. outputSz = 0;
  23700. }
  23701. return outputSz;
  23702. }
  23703. #endif
  23704. /*
  23705. * Testing wc_PKCS_VerifySignedData()
  23706. */
  23707. static int test_wc_PKCS7_VerifySignedData_RSA(void)
  23708. {
  23709. EXPECT_DECLS;
  23710. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  23711. PKCS7* pkcs7 = NULL;
  23712. byte output[6000]; /* Large size needed for bundles with int CA certs */
  23713. word32 outputSz = sizeof(output);
  23714. byte data[] = "Test data to encode.";
  23715. byte badOut[1];
  23716. word32 badOutSz = 0;
  23717. byte badContent[] = "This is different content than was signed";
  23718. wc_HashAlg hash;
  23719. #ifdef NO_SHA
  23720. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  23721. #else
  23722. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  23723. #endif
  23724. byte hashBuf[WC_MAX_DIGEST_SIZE];
  23725. word32 hashSz = wc_HashGetDigestSize(hashType);
  23726. #ifndef NO_RSA
  23727. PKCS7DecodedAttrib* decodedAttrib = NULL;
  23728. /* contentType OID (1.2.840.113549.1.9.3) */
  23729. static const byte contentTypeOid[] =
  23730. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01, 0x09, 0x03 };
  23731. /* PKCS#7 DATA content type (contentType defaults to DATA) */
  23732. static const byte dataType[] =
  23733. { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01 };
  23734. /* messageDigest OID (1.2.840.113549.1.9.4) */
  23735. static const byte messageDigestOid[] =
  23736. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04 };
  23737. #ifndef NO_ASN_TIME
  23738. /* signingTime OID () */
  23739. static const byte signingTimeOid[] =
  23740. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05};
  23741. #endif
  23742. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  23743. int dateLength = 0;
  23744. byte dateFormat;
  23745. const byte* datePart = NULL;
  23746. struct tm timearg;
  23747. time_t now;
  23748. struct tm* nowTm = NULL;
  23749. #ifdef NEED_TMP_TIME
  23750. struct tm tmpTimeStorage;
  23751. struct tm* tmpTime = &tmpTimeStorage;
  23752. #endif
  23753. #endif /* !NO_ASN && !NO_ASN_TIME */
  23754. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  23755. /* Success test with RSA certs/key */
  23756. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  23757. (word32)sizeof(data), 0, 0, 0, RSA_TYPE)), 0);
  23758. /* calculate hash for content, used later */
  23759. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  23760. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  23761. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  23762. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  23763. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23764. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23765. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23766. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23767. /* Check that decoded signed attributes are correct */
  23768. /* messageDigest should be first */
  23769. if (pkcs7 != NULL) {
  23770. decodedAttrib = pkcs7->decodedAttrib;
  23771. }
  23772. ExpectNotNull(decodedAttrib);
  23773. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(messageDigestOid));
  23774. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, messageDigestOid,
  23775. decodedAttrib->oidSz), 0);
  23776. /* + 2 for OCTET STRING and length bytes */
  23777. ExpectIntEQ(decodedAttrib->valueSz, hashSz + 2);
  23778. ExpectNotNull(decodedAttrib->value);
  23779. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, hashBuf, hashSz), 0);
  23780. #ifndef NO_ASN_TIME
  23781. /* signingTime should be second */
  23782. if (decodedAttrib != NULL) {
  23783. decodedAttrib = decodedAttrib->next;
  23784. }
  23785. ExpectNotNull(decodedAttrib);
  23786. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(signingTimeOid));
  23787. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, signingTimeOid,
  23788. decodedAttrib->oidSz), 0);
  23789. ExpectIntGT(decodedAttrib->valueSz, 0);
  23790. ExpectNotNull(decodedAttrib->value);
  23791. #endif
  23792. /* Verify signingTime if ASN and time are available */
  23793. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  23794. ExpectIntEQ(wc_GetDateInfo(decodedAttrib->value, decodedAttrib->valueSz,
  23795. &datePart, &dateFormat, &dateLength), 0);
  23796. ExpectNotNull(datePart);
  23797. ExpectIntGT(dateLength, 0);
  23798. XMEMSET(&timearg, 0, sizeof(timearg));
  23799. ExpectIntEQ(wc_GetDateAsCalendarTime(datePart, dateLength, dateFormat,
  23800. &timearg), 0);
  23801. /* Get current time and compare year/month/day against attribute value */
  23802. ExpectIntEQ(wc_GetTime(&now, sizeof(now)), 0);
  23803. nowTm = (struct tm*)XGMTIME((time_t*)&now, tmpTime);
  23804. ExpectNotNull(nowTm);
  23805. ExpectIntEQ(timearg.tm_year, nowTm->tm_year);
  23806. ExpectIntEQ(timearg.tm_mon, nowTm->tm_mon);
  23807. ExpectIntEQ(timearg.tm_mday, nowTm->tm_mday);
  23808. #endif /* !NO_ASN && !NO_ASN_TIME */
  23809. /* contentType should be third */
  23810. if (decodedAttrib != NULL) {
  23811. decodedAttrib = decodedAttrib->next;
  23812. }
  23813. ExpectNotNull(decodedAttrib);
  23814. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(contentTypeOid));
  23815. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, contentTypeOid,
  23816. decodedAttrib->oidSz), 0);
  23817. ExpectIntEQ(decodedAttrib->valueSz, (int)sizeof(dataType) + 2);
  23818. ExpectNotNull(decodedAttrib->value);
  23819. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, dataType, sizeof(dataType)),
  23820. 0);
  23821. #endif /* !NO_RSA */
  23822. /* Test bad args. */
  23823. ExpectIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  23824. BAD_FUNC_ARG);
  23825. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  23826. BAD_FUNC_ARG);
  23827. #ifndef NO_PKCS7_STREAM
  23828. /* can pass in 0 buffer length with streaming API */
  23829. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  23830. badOutSz), WC_PKCS7_WANT_READ_E);
  23831. #else
  23832. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  23833. badOutSz), BAD_FUNC_ARG);
  23834. #endif
  23835. wc_PKCS7_Free(pkcs7);
  23836. pkcs7 = NULL;
  23837. #ifndef NO_RSA
  23838. /* Try RSA certs/key/sig first */
  23839. outputSz = sizeof(output);
  23840. XMEMSET(output, 0, outputSz);
  23841. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  23842. (word32)sizeof(data),
  23843. 1, 1, 0, RSA_TYPE)), 0);
  23844. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23845. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23846. if (pkcs7 != NULL) {
  23847. pkcs7->content = badContent;
  23848. pkcs7->contentSz = sizeof(badContent);
  23849. }
  23850. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  23851. SIG_VERIFY_E);
  23852. wc_PKCS7_Free(pkcs7);
  23853. pkcs7 = NULL;
  23854. /* Test success case with detached signature and valid content */
  23855. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23856. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23857. if (pkcs7 != NULL) {
  23858. pkcs7->content = data;
  23859. pkcs7->contentSz = sizeof(data);
  23860. }
  23861. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23862. wc_PKCS7_Free(pkcs7);
  23863. pkcs7 = NULL;
  23864. /* verify using pre-computed content digest only (no content) */
  23865. {
  23866. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23867. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  23868. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23869. output, outputSz, NULL, 0), 0);
  23870. wc_PKCS7_Free(pkcs7);
  23871. pkcs7 = NULL;
  23872. }
  23873. #endif /* !NO_RSA */
  23874. /* Test verify on signedData containing intermediate/root CA certs */
  23875. #ifndef NO_RSA
  23876. outputSz = sizeof(output);
  23877. XMEMSET(output, 0, outputSz);
  23878. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  23879. (word32)sizeof(data),
  23880. 0, 0, 1, RSA_TYPE)), 0);
  23881. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23882. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23883. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23884. wc_PKCS7_Free(pkcs7);
  23885. pkcs7 = NULL;
  23886. #endif /* !NO_RSA */
  23887. #endif
  23888. return EXPECT_RESULT();
  23889. } /* END test_wc_PKCS7_VerifySignedData()_RSA */
  23890. /*
  23891. * Testing wc_PKCS_VerifySignedData()
  23892. */
  23893. static int test_wc_PKCS7_VerifySignedData_ECC(void)
  23894. {
  23895. EXPECT_DECLS;
  23896. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  23897. PKCS7* pkcs7 = NULL;
  23898. byte output[6000]; /* Large size needed for bundles with int CA certs */
  23899. word32 outputSz = sizeof(output);
  23900. byte data[] = "Test data to encode.";
  23901. byte badContent[] = "This is different content than was signed";
  23902. wc_HashAlg hash;
  23903. #ifdef NO_SHA
  23904. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  23905. #else
  23906. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  23907. #endif
  23908. byte hashBuf[WC_MAX_DIGEST_SIZE];
  23909. word32 hashSz = wc_HashGetDigestSize(hashType);
  23910. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  23911. /* Success test with ECC certs/key */
  23912. outputSz = sizeof(output);
  23913. XMEMSET(output, 0, outputSz);
  23914. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  23915. (word32)sizeof(data), 0, 0, 0, ECC_TYPE)), 0);
  23916. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23917. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23918. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23919. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23920. wc_PKCS7_Free(pkcs7);
  23921. pkcs7 = NULL;
  23922. /* Invalid content should error, use detached signature so we can
  23923. * easily change content */
  23924. outputSz = sizeof(output);
  23925. XMEMSET(output, 0, outputSz);
  23926. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  23927. (word32)sizeof(data), 1, 1, 0, ECC_TYPE)), 0);
  23928. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23929. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23930. if (pkcs7 != NULL) {
  23931. pkcs7->content = badContent;
  23932. pkcs7->contentSz = sizeof(badContent);
  23933. }
  23934. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  23935. SIG_VERIFY_E);
  23936. wc_PKCS7_Free(pkcs7);
  23937. pkcs7 = NULL;
  23938. /* Test success case with detached signature and valid content */
  23939. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23940. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23941. if (pkcs7 != NULL) {
  23942. pkcs7->content = data;
  23943. pkcs7->contentSz = sizeof(data);
  23944. }
  23945. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23946. wc_PKCS7_Free(pkcs7);
  23947. pkcs7 = NULL;
  23948. /* verify using pre-computed content digest only (no content) */
  23949. {
  23950. /* calculate hash for content */
  23951. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  23952. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  23953. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  23954. ExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  23955. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23956. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  23957. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23958. output, outputSz, NULL, 0), 0);
  23959. wc_PKCS7_Free(pkcs7);
  23960. pkcs7 = NULL;
  23961. }
  23962. /* Test verify on signedData containing intermediate/root CA certs */
  23963. outputSz = sizeof(output);
  23964. XMEMSET(output, 0, outputSz);
  23965. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  23966. (word32)sizeof(data), 0, 0, 1, ECC_TYPE)), 0);
  23967. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23968. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23969. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23970. wc_PKCS7_Free(pkcs7);
  23971. pkcs7 = NULL;
  23972. #endif
  23973. return EXPECT_RESULT();
  23974. } /* END test_wc_PKCS7_VerifySignedData_ECC() */
  23975. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  23976. !defined(NO_AES_256)
  23977. static const byte defKey[] = {
  23978. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  23979. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  23980. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  23981. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  23982. };
  23983. static byte aesHandle[32]; /* simulated hardware key handle */
  23984. /* return 0 on success */
  23985. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  23986. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  23987. byte* in, int inSz, byte* out, void* usrCtx)
  23988. {
  23989. int ret;
  23990. Aes aes;
  23991. if (usrCtx == NULL) {
  23992. /* no simulated handle passed in */
  23993. return -1;
  23994. }
  23995. switch (encryptOID) {
  23996. case AES256CBCb:
  23997. if (ivSz != AES_BLOCK_SIZE)
  23998. return BAD_FUNC_ARG;
  23999. break;
  24000. default:
  24001. WOLFSSL_MSG("Unsupported content cipher type for test");
  24002. return ALGO_ID_E;
  24003. };
  24004. /* simulate using handle to get key */
  24005. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  24006. if (ret == 0) {
  24007. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  24008. if (ret == 0)
  24009. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  24010. wc_AesFree(&aes);
  24011. }
  24012. (void)aad;
  24013. (void)aadSz;
  24014. (void)authTag;
  24015. (void)authTagSz;
  24016. (void)pkcs7;
  24017. return ret;
  24018. }
  24019. /* returns key size on success */
  24020. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  24021. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  24022. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  24023. {
  24024. int ret = -1;
  24025. if (out == NULL)
  24026. return BAD_FUNC_ARG;
  24027. if (keyId[0] != 0x00) {
  24028. return -1;
  24029. }
  24030. if (type != (int)PKCS7_KEKRI) {
  24031. return -1;
  24032. }
  24033. switch (keyWrapAlgo) {
  24034. case AES256_WRAP:
  24035. /* simulate setting a handle for later decryption but use key
  24036. * as handle in the test case here */
  24037. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  24038. aesHandle, sizeof(aesHandle), NULL);
  24039. if (ret < 0)
  24040. return ret;
  24041. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  24042. if (ret < 0)
  24043. return ret;
  24044. /* return key size on success */
  24045. return sizeof(defKey);
  24046. default:
  24047. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  24048. return BAD_KEYWRAP_ALG_E;
  24049. };
  24050. (void)cekSz;
  24051. (void)cek;
  24052. (void)outSz;
  24053. (void)keyIdSz;
  24054. (void)direction;
  24055. (void)orginKey; /* used with KAKRI */
  24056. (void)orginKeySz;
  24057. return ret;
  24058. }
  24059. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  24060. /*
  24061. * Testing wc_PKCS7_EncodeEnvelopedData()
  24062. */
  24063. static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
  24064. {
  24065. EXPECT_DECLS;
  24066. #if defined(HAVE_PKCS7)
  24067. PKCS7* pkcs7 = NULL;
  24068. #ifdef ECC_TIMING_RESISTANT
  24069. WC_RNG rng;
  24070. #endif
  24071. word32 tempWrd32 = 0;
  24072. byte* tmpBytePtr = NULL;
  24073. const char input[] = "Test data to encode.";
  24074. int i;
  24075. int testSz = 0;
  24076. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  24077. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24078. byte* rsaCert = NULL;
  24079. byte* rsaPrivKey = NULL;
  24080. word32 rsaCertSz;
  24081. word32 rsaPrivKeySz;
  24082. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  24083. !defined(USE_CERT_BUFFERS_2048) )
  24084. static const char* rsaClientCert = "./certs/client-cert.der";
  24085. static const char* rsaClientKey = "./certs/client-key.der";
  24086. rsaCertSz = (word32)sizeof(rsaClientCert);
  24087. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  24088. #endif
  24089. #endif
  24090. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24091. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24092. byte* eccCert = NULL;
  24093. byte* eccPrivKey = NULL;
  24094. word32 eccCertSz;
  24095. word32 eccPrivKeySz;
  24096. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  24097. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  24098. static const char* eccClientKey = "./certs/ecc-client-key.der";
  24099. #endif
  24100. #endif
  24101. /* Generic buffer size. */
  24102. byte output[ONEK_BUF];
  24103. byte decoded[sizeof(input)/sizeof(char)];
  24104. int decodedSz = 0;
  24105. #ifndef NO_FILESYSTEM
  24106. XFILE certFile = XBADFILE;
  24107. XFILE keyFile = XBADFILE;
  24108. #endif
  24109. #ifdef ECC_TIMING_RESISTANT
  24110. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24111. #endif
  24112. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24113. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24114. /* RSA certs and keys. */
  24115. #if defined(USE_CERT_BUFFERS_1024)
  24116. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  24117. /* Allocate buffer space. */
  24118. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  24119. DYNAMIC_TYPE_TMP_BUFFER));
  24120. /* Init buffer. */
  24121. if (rsaCert != NULL) {
  24122. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  24123. }
  24124. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  24125. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  24126. DYNAMIC_TYPE_TMP_BUFFER));
  24127. if (rsaPrivKey != NULL) {
  24128. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  24129. }
  24130. #elif defined(USE_CERT_BUFFERS_2048)
  24131. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  24132. /* Allocate buffer */
  24133. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  24134. DYNAMIC_TYPE_TMP_BUFFER));
  24135. /* Init buffer. */
  24136. if (rsaCert != NULL) {
  24137. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  24138. }
  24139. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  24140. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  24141. DYNAMIC_TYPE_TMP_BUFFER));
  24142. if (rsaPrivKey != NULL) {
  24143. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  24144. }
  24145. #else
  24146. /* File system. */
  24147. ExpectTrue((certFile = XFOPEN(rsaClientCert, "rb")) != XBADFILE);
  24148. rsaCertSz = (word32)FOURK_BUF;
  24149. ExpectNotNull(rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24150. DYNAMIC_TYPE_TMP_BUFFER));
  24151. ExpectTrue((rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz,
  24152. certFile)) > 0);
  24153. if (certFile != XBADFILE)
  24154. XFCLOSE(certFile);
  24155. ExpectTrue((keyFile = XFOPEN(rsaClientKey, "rb")) != XBADFILE);
  24156. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24157. DYNAMIC_TYPE_TMP_BUFFER));
  24158. rsaPrivKeySz = (word32)FOURK_BUF;
  24159. ExpectTrue((rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz,
  24160. keyFile)) > 0);
  24161. if (keyFile != XBADFILE)
  24162. XFCLOSE(keyFile);
  24163. #endif /* USE_CERT_BUFFERS */
  24164. #endif /* NO_RSA */
  24165. /* ECC */
  24166. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24167. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24168. #ifdef USE_CERT_BUFFERS_256
  24169. ExpectNotNull(eccCert = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  24170. DYNAMIC_TYPE_TMP_BUFFER));
  24171. /* Init buffer. */
  24172. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  24173. if (eccCert != NULL) {
  24174. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  24175. }
  24176. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  24177. DYNAMIC_TYPE_TMP_BUFFER));
  24178. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  24179. if (eccPrivKey != NULL) {
  24180. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  24181. }
  24182. #else /* File system. */
  24183. ExpectTrue((certFile = XFOPEN(eccClientCert, "rb")) != XBADFILE);
  24184. eccCertSz = (word32)FOURK_BUF;
  24185. ExpectNotNull(eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24186. DYNAMIC_TYPE_TMP_BUFFER));
  24187. ExpectTrue((eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz,
  24188. certFile)) > 0);
  24189. if (certFile != XBADFILE) {
  24190. XFCLOSE(certFile);
  24191. }
  24192. ExpectTrue((keyFile = XFOPEN(eccClientKey, "rb")) != XBADFILE);
  24193. eccPrivKeySz = (word32)FOURK_BUF;
  24194. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24195. DYNAMIC_TYPE_TMP_BUFFER));
  24196. ExpectTrue((eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz,
  24197. keyFile)) > 0);
  24198. if (keyFile != XBADFILE) {
  24199. XFCLOSE(keyFile);
  24200. }
  24201. #endif /* USE_CERT_BUFFERS_256 */
  24202. #endif /* END HAVE_ECC */
  24203. #ifndef NO_FILESYSTEM
  24204. /* Silence. */
  24205. (void)keyFile;
  24206. (void)certFile;
  24207. #endif
  24208. {
  24209. const pkcs7EnvelopedVector testVectors[] = {
  24210. /* DATA is a global variable defined in the makefile. */
  24211. #if !defined(NO_RSA)
  24212. #ifndef NO_DES3
  24213. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  24214. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24215. #endif /* NO_DES3 */
  24216. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24217. #ifndef NO_AES_128
  24218. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  24219. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24220. #endif
  24221. #ifndef NO_AES_192
  24222. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  24223. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24224. #endif
  24225. #ifndef NO_AES_256
  24226. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  24227. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24228. #endif
  24229. #endif /* NO_AES && HAVE_AES_CBC */
  24230. #endif /* NO_RSA */
  24231. #if defined(HAVE_ECC)
  24232. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24233. #if !defined(NO_SHA) && !defined(NO_AES_128)
  24234. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  24235. AES128CBCb, AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme,
  24236. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  24237. #endif
  24238. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  24239. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  24240. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme,
  24241. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  24242. #endif
  24243. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  24244. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  24245. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme,
  24246. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  24247. #endif
  24248. #endif /* NO_AES && HAVE_AES_CBC*/
  24249. #endif /* END HAVE_ECC */
  24250. }; /* END pkcs7EnvelopedVector */
  24251. #ifdef ECC_TIMING_RESISTANT
  24252. ExpectIntEQ(wc_InitRng(&rng), 0);
  24253. #endif
  24254. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24255. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  24256. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  24257. for (i = 0; i < testSz; i++) {
  24258. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  24259. (word32)(testVectors + i)->certSz), 0);
  24260. if (pkcs7 != NULL) {
  24261. #ifdef ECC_TIMING_RESISTANT
  24262. pkcs7->rng = &rng;
  24263. #endif
  24264. pkcs7->content = (byte*)(testVectors + i)->content;
  24265. pkcs7->contentSz = (testVectors + i)->contentSz;
  24266. pkcs7->contentOID = (testVectors + i)->contentOID;
  24267. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  24268. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  24269. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  24270. pkcs7->privateKey = (testVectors + i)->privateKey;
  24271. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  24272. }
  24273. ExpectIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  24274. (word32)sizeof(output)), 0);
  24275. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24276. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  24277. ExpectIntGE(decodedSz, 0);
  24278. /* Verify the size of each buffer. */
  24279. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  24280. /* Don't free the last time through the loop. */
  24281. if (i < testSz - 1) {
  24282. wc_PKCS7_Free(pkcs7);
  24283. pkcs7 = NULL;
  24284. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24285. }
  24286. } /* END test loop. */
  24287. }
  24288. /* Test bad args. */
  24289. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  24290. (word32)sizeof(output)), BAD_FUNC_ARG);
  24291. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  24292. (word32)sizeof(output)), BAD_FUNC_ARG);
  24293. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  24294. /* Decode. */
  24295. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  24296. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24297. BAD_FUNC_ARG);
  24298. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24299. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24300. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24301. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  24302. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  24303. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24304. BAD_FUNC_ARG);
  24305. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  24306. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24307. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  24308. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  24309. /* only a failure for KARI test cases */
  24310. if (pkcs7 != NULL) {
  24311. tempWrd32 = pkcs7->singleCertSz;
  24312. pkcs7->singleCertSz = 0;
  24313. }
  24314. #if defined(WOLFSSL_ASN_TEMPLATE)
  24315. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24316. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24317. BUFFER_E);
  24318. #else
  24319. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24320. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24321. ASN_PARSE_E);
  24322. #endif
  24323. if (pkcs7 != NULL) {
  24324. pkcs7->singleCertSz = tempWrd32;
  24325. tmpBytePtr = pkcs7->singleCert;
  24326. pkcs7->singleCert = NULL;
  24327. }
  24328. #ifndef NO_RSA
  24329. #if defined(NO_PKCS7_STREAM)
  24330. /* when none streaming mode is used and PKCS7 is in bad state buffer error
  24331. * is returned from kari parse which gets set to bad func arg */
  24332. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24333. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24334. BAD_FUNC_ARG);
  24335. #else
  24336. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24337. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24338. ASN_PARSE_E);
  24339. #endif
  24340. #endif /* !NO_RSA */
  24341. if (pkcs7 != NULL) {
  24342. pkcs7->singleCert = tmpBytePtr;
  24343. }
  24344. #endif
  24345. if (pkcs7 != NULL) {
  24346. tempWrd32 = pkcs7->privateKeySz;
  24347. pkcs7->privateKeySz = 0;
  24348. }
  24349. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24350. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24351. BAD_FUNC_ARG);
  24352. if (pkcs7 != NULL) {
  24353. pkcs7->privateKeySz = tempWrd32;
  24354. tmpBytePtr = pkcs7->privateKey;
  24355. pkcs7->privateKey = NULL;
  24356. }
  24357. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24358. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24359. BAD_FUNC_ARG);
  24360. if (pkcs7 != NULL) {
  24361. pkcs7->privateKey = tmpBytePtr;
  24362. }
  24363. wc_PKCS7_Free(pkcs7);
  24364. pkcs7 = NULL;
  24365. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  24366. /* test of decrypt callback with KEKRI enveloped data */
  24367. {
  24368. int envelopedSz = 0;
  24369. const byte keyId[] = { 0x00 };
  24370. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24371. if (pkcs7 != NULL) {
  24372. pkcs7->content = (byte*)input;
  24373. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  24374. pkcs7->contentOID = DATA;
  24375. pkcs7->encryptOID = AES256CBCb;
  24376. }
  24377. ExpectIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  24378. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  24379. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  24380. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  24381. ExpectIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  24382. (word32)sizeof(output))), 0);
  24383. wc_PKCS7_Free(pkcs7);
  24384. pkcs7 = NULL;
  24385. /* decode envelopedData */
  24386. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24387. ExpectIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  24388. ExpectIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  24389. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24390. envelopedSz, decoded, sizeof(decoded))), 0);
  24391. wc_PKCS7_Free(pkcs7);
  24392. pkcs7 = NULL;
  24393. }
  24394. #endif /* !NO_AES && !NO_AES_256 */
  24395. #ifndef NO_RSA
  24396. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24397. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24398. #endif /* NO_RSA */
  24399. #ifdef HAVE_ECC
  24400. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24401. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24402. #endif /* HAVE_ECC */
  24403. #ifdef ECC_TIMING_RESISTANT
  24404. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  24405. #endif
  24406. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && \
  24407. !defined(NO_RSA) && !defined(NO_SHA)
  24408. {
  24409. byte out[7];
  24410. byte *cms = NULL;
  24411. word32 cmsSz;
  24412. XFILE cmsFile = XBADFILE;
  24413. XMEMSET(out, 0, sizeof(out));
  24414. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24415. ExpectTrue((cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb"))
  24416. != XBADFILE);
  24417. cmsSz = (word32)FOURK_BUF;
  24418. ExpectNotNull(cms = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24419. DYNAMIC_TYPE_TMP_BUFFER));
  24420. ExpectTrue((cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile)) > 0);
  24421. if (cmsFile != XBADFILE)
  24422. XFCLOSE(cmsFile);
  24423. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  24424. sizeof_client_cert_der_2048), 0);
  24425. if (pkcs7 != NULL) {
  24426. pkcs7->privateKey = (byte*)client_key_der_2048;
  24427. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  24428. }
  24429. ExpectIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  24430. 2), 0);
  24431. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  24432. sizeof(out)), 0);
  24433. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24434. ExpectIntEQ(XMEMCMP(out, "test", 4), 0);
  24435. wc_PKCS7_Free(pkcs7);
  24436. pkcs7 = NULL;
  24437. }
  24438. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 && !NO_RSA && !NO_SHA */
  24439. #endif /* HAVE_PKCS7 */
  24440. return EXPECT_RESULT();
  24441. } /* END test_wc_PKCS7_EncodeDecodeEnvelopedData() */
  24442. /*
  24443. * Testing wc_PKCS7_EncodeEncryptedData()
  24444. */
  24445. static int test_wc_PKCS7_EncodeEncryptedData(void)
  24446. {
  24447. EXPECT_DECLS;
  24448. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  24449. PKCS7* pkcs7 = NULL;
  24450. byte* tmpBytePtr = NULL;
  24451. byte encrypted[TWOK_BUF];
  24452. byte decoded[TWOK_BUF];
  24453. word32 tmpWrd32 = 0;
  24454. int tmpInt = 0;
  24455. int decodedSz;
  24456. int encryptedSz = 0;
  24457. int testSz;
  24458. int i;
  24459. const byte data[] = { /* Hello World */
  24460. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  24461. 0x72,0x6c,0x64
  24462. };
  24463. #ifndef NO_DES3
  24464. byte desKey[] = {
  24465. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  24466. };
  24467. byte des3Key[] = {
  24468. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  24469. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  24470. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  24471. };
  24472. #endif
  24473. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24474. #ifndef NO_AES_128
  24475. byte aes128Key[] = {
  24476. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24477. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24478. };
  24479. #endif
  24480. #ifndef NO_AES_192
  24481. byte aes192Key[] = {
  24482. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24483. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24484. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24485. };
  24486. #endif
  24487. #ifndef NO_AES_256
  24488. byte aes256Key[] = {
  24489. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24490. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24491. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24492. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24493. };
  24494. #endif
  24495. #endif /* !NO_AES && HAVE_AES_CBC */
  24496. const pkcs7EncryptedVector testVectors[] =
  24497. {
  24498. #ifndef NO_DES3
  24499. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  24500. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  24501. #endif /* !NO_DES3 */
  24502. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24503. #ifndef NO_AES_128
  24504. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  24505. sizeof(aes128Key)},
  24506. #endif
  24507. #ifndef NO_AES_192
  24508. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  24509. sizeof(aes192Key)},
  24510. #endif
  24511. #ifndef NO_AES_256
  24512. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  24513. sizeof(aes256Key)},
  24514. #endif
  24515. #endif /* !NO_AES && HAVE_AES_CBC */
  24516. };
  24517. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  24518. for (i = 0; i < testSz; i++) {
  24519. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24520. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  24521. if (pkcs7 != NULL) {
  24522. pkcs7->content = (byte*)testVectors[i].content;
  24523. pkcs7->contentSz = testVectors[i].contentSz;
  24524. pkcs7->contentOID = testVectors[i].contentOID;
  24525. pkcs7->encryptOID = testVectors[i].encryptOID;
  24526. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  24527. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  24528. pkcs7->heap = HEAP_HINT;
  24529. }
  24530. /* encode encryptedData */
  24531. ExpectIntGT(encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24532. sizeof(encrypted)), 0);
  24533. /* Decode encryptedData */
  24534. ExpectIntGT(decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted,
  24535. encryptedSz, decoded, sizeof(decoded)), 0);
  24536. ExpectIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  24537. /* Keep values for last itr. */
  24538. if (i < testSz - 1) {
  24539. wc_PKCS7_Free(pkcs7);
  24540. pkcs7 = NULL;
  24541. }
  24542. }
  24543. if (pkcs7 == NULL || testSz == 0) {
  24544. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24545. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  24546. }
  24547. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  24548. sizeof(encrypted)),BAD_FUNC_ARG);
  24549. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  24550. sizeof(encrypted)), BAD_FUNC_ARG);
  24551. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24552. 0), BAD_FUNC_ARG);
  24553. /* Testing the struct. */
  24554. if (pkcs7 != NULL) {
  24555. tmpBytePtr = pkcs7->content;
  24556. pkcs7->content = NULL;
  24557. }
  24558. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24559. sizeof(encrypted)), BAD_FUNC_ARG);
  24560. if (pkcs7 != NULL) {
  24561. pkcs7->content = tmpBytePtr;
  24562. tmpWrd32 = pkcs7->contentSz;
  24563. pkcs7->contentSz = 0;
  24564. }
  24565. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24566. sizeof(encrypted)), BAD_FUNC_ARG);
  24567. if (pkcs7 != NULL) {
  24568. pkcs7->contentSz = tmpWrd32;
  24569. tmpInt = pkcs7->encryptOID;
  24570. pkcs7->encryptOID = 0;
  24571. }
  24572. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24573. sizeof(encrypted)), BAD_FUNC_ARG);
  24574. if (pkcs7 != NULL) {
  24575. pkcs7->encryptOID = tmpInt;
  24576. tmpBytePtr = pkcs7->encryptionKey;
  24577. pkcs7->encryptionKey = NULL;
  24578. }
  24579. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24580. sizeof(encrypted)), BAD_FUNC_ARG);
  24581. if (pkcs7 != NULL) {
  24582. pkcs7->encryptionKey = tmpBytePtr;
  24583. tmpWrd32 = pkcs7->encryptionKeySz;
  24584. pkcs7->encryptionKeySz = 0;
  24585. }
  24586. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24587. sizeof(encrypted)), BAD_FUNC_ARG);
  24588. if (pkcs7 != NULL) {
  24589. pkcs7->encryptionKeySz = tmpWrd32;
  24590. }
  24591. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  24592. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24593. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  24594. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24595. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  24596. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24597. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24598. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  24599. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24600. decoded, 0), BAD_FUNC_ARG);
  24601. /* Test struct fields */
  24602. if (pkcs7 != NULL) {
  24603. tmpBytePtr = pkcs7->encryptionKey;
  24604. pkcs7->encryptionKey = NULL;
  24605. }
  24606. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24607. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24608. if (pkcs7 != NULL) {
  24609. pkcs7->encryptionKey = tmpBytePtr;
  24610. pkcs7->encryptionKeySz = 0;
  24611. }
  24612. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24613. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24614. wc_PKCS7_Free(pkcs7);
  24615. #endif
  24616. return EXPECT_RESULT();
  24617. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  24618. /*
  24619. * Testing wc_PKCS7_Degenerate()
  24620. */
  24621. static int test_wc_PKCS7_Degenerate(void)
  24622. {
  24623. EXPECT_DECLS;
  24624. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  24625. PKCS7* pkcs7 = NULL;
  24626. char fName[] = "./certs/test-degenerate.p7b";
  24627. XFILE f = XBADFILE;
  24628. byte der[4096];
  24629. word32 derSz = 0;
  24630. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  24631. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  24632. if (f != XBADFILE)
  24633. XFCLOSE(f);
  24634. /* test degenerate success */
  24635. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24636. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24637. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24638. #ifndef NO_RSA
  24639. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24640. #else
  24641. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24642. #endif
  24643. wc_PKCS7_Free(pkcs7);
  24644. pkcs7 = NULL;
  24645. /* test with turning off degenerate cases */
  24646. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24647. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24648. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24649. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  24650. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz),
  24651. PKCS7_NO_SIGNER_E);
  24652. wc_PKCS7_Free(pkcs7);
  24653. #endif
  24654. return EXPECT_RESULT();
  24655. } /* END test_wc_PKCS7_Degenerate() */
  24656. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  24657. defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
  24658. static byte berContent[] = {
  24659. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24660. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  24661. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  24662. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  24663. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  24664. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  24665. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  24666. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  24667. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  24668. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  24669. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  24670. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  24671. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  24672. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  24673. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  24674. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  24675. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  24676. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  24677. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  24678. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  24679. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  24680. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  24681. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  24682. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  24683. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  24684. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  24685. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  24686. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  24687. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  24688. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  24689. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  24690. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  24691. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  24692. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  24693. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  24694. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  24695. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  24696. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  24697. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  24698. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  24699. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  24700. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  24701. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  24702. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  24703. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24704. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  24705. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  24706. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  24707. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  24708. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  24709. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  24710. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  24711. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  24712. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  24713. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  24714. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  24715. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  24716. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  24717. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  24718. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  24719. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  24720. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  24721. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  24722. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  24723. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  24724. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  24725. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  24726. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  24727. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  24728. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  24729. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  24730. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  24731. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  24732. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  24733. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  24734. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  24735. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  24736. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  24737. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  24738. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  24739. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  24740. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  24741. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  24742. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  24743. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  24744. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  24745. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  24746. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  24747. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  24748. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  24749. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  24750. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  24751. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  24752. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  24753. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  24754. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  24755. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  24756. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  24757. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  24758. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  24759. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  24760. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  24761. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  24762. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  24763. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  24764. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  24765. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  24766. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  24767. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  24768. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  24769. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  24770. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  24771. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  24772. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  24773. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  24774. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  24775. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  24776. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  24777. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  24778. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  24779. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  24780. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  24781. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  24782. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  24783. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  24784. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  24785. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  24786. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  24787. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  24788. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  24789. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  24790. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  24791. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  24792. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  24793. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  24794. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  24795. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  24796. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  24797. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  24798. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  24799. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  24800. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  24801. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  24802. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  24803. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  24804. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  24805. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  24806. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  24807. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  24808. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  24809. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  24810. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  24811. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  24812. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  24813. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  24814. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  24815. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  24816. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  24817. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  24818. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  24819. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  24820. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  24821. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  24822. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  24823. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  24824. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  24825. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  24826. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  24827. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  24828. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  24829. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  24830. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  24831. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  24832. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  24833. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  24834. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  24835. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  24836. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  24837. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  24838. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  24839. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  24840. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  24841. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  24842. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  24843. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  24844. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  24845. 0x00, 0x00, 0x00, 0x00, 0x00
  24846. };
  24847. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
  24848. * !NO_DES3 && !NO_SHA
  24849. */
  24850. /*
  24851. * Testing wc_PKCS7_BER()
  24852. */
  24853. static int test_wc_PKCS7_BER(void)
  24854. {
  24855. EXPECT_DECLS;
  24856. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  24857. !defined(NO_SHA) && defined(ASN_BER_TO_DER)
  24858. PKCS7* pkcs7 = NULL;
  24859. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  24860. XFILE f = XBADFILE;
  24861. byte der[4096];
  24862. #ifndef NO_DES3
  24863. byte decoded[2048];
  24864. #endif
  24865. word32 derSz = 0;
  24866. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  24867. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  24868. if (f != XBADFILE) {
  24869. XFCLOSE(f);
  24870. f = XBADFILE;
  24871. }
  24872. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24873. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24874. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24875. #ifndef NO_RSA
  24876. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24877. #else
  24878. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24879. #endif
  24880. wc_PKCS7_Free(pkcs7);
  24881. pkcs7 = NULL;
  24882. #ifndef NO_DES3
  24883. /* decode BER content */
  24884. ExpectTrue((f = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  24885. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  24886. if (f != XBADFILE) {
  24887. XFCLOSE(f);
  24888. f = XBADFILE;
  24889. }
  24890. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24891. #ifndef NO_RSA
  24892. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  24893. #else
  24894. ExpectIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  24895. #endif
  24896. ExpectTrue((f = XFOPEN("./certs/1024/client-key.der", "rb")) != XBADFILE);
  24897. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  24898. if (f != XBADFILE) {
  24899. XFCLOSE(f);
  24900. f = XBADFILE;
  24901. }
  24902. if (pkcs7 != NULL) {
  24903. pkcs7->privateKey = der;
  24904. pkcs7->privateKeySz = derSz;
  24905. }
  24906. #ifndef NO_RSA
  24907. #ifdef WOLFSSL_SP_MATH
  24908. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  24909. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  24910. #else
  24911. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  24912. sizeof(berContent), decoded, sizeof(decoded)), 0);
  24913. #endif
  24914. #else
  24915. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  24916. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  24917. #endif
  24918. wc_PKCS7_Free(pkcs7);
  24919. #endif /* !NO_DES3 */
  24920. #endif
  24921. return EXPECT_RESULT();
  24922. } /* END test_wc_PKCS7_BER() */
  24923. static int test_wc_PKCS7_signed_enveloped(void)
  24924. {
  24925. EXPECT_DECLS;
  24926. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  24927. !defined(NO_FILESYSTEM)
  24928. XFILE f = XBADFILE;
  24929. PKCS7* pkcs7 = NULL;
  24930. #ifdef HAVE_AES_CBC
  24931. PKCS7* inner = NULL;
  24932. #endif
  24933. void* pt = NULL;
  24934. WC_RNG rng;
  24935. unsigned char key[FOURK_BUF/2];
  24936. unsigned char cert[FOURK_BUF/2];
  24937. unsigned char env[FOURK_BUF];
  24938. int envSz = FOURK_BUF;
  24939. int keySz = 0;
  24940. int certSz = 0;
  24941. unsigned char sig[FOURK_BUF * 2];
  24942. int sigSz = FOURK_BUF * 2;
  24943. #ifdef HAVE_AES_CBC
  24944. unsigned char decoded[FOURK_BUF];
  24945. int decodedSz = FOURK_BUF;
  24946. #endif
  24947. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24948. /* load cert */
  24949. ExpectTrue((f = XFOPEN(cliCertDerFile, "rb")) != XBADFILE);
  24950. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  24951. if (f != XBADFILE) {
  24952. XFCLOSE(f);
  24953. f = XBADFILE;
  24954. }
  24955. /* load key */
  24956. ExpectTrue((f = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  24957. ExpectIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  24958. if (f != XBADFILE) {
  24959. XFCLOSE(f);
  24960. f = XBADFILE;
  24961. }
  24962. ExpectIntGT(keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL), 0);
  24963. /* sign cert for envelope */
  24964. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  24965. ExpectIntEQ(wc_InitRng(&rng), 0);
  24966. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24967. if (pkcs7 != NULL) {
  24968. pkcs7->content = cert;
  24969. pkcs7->contentSz = certSz;
  24970. pkcs7->contentOID = DATA;
  24971. pkcs7->privateKey = key;
  24972. pkcs7->privateKeySz = keySz;
  24973. pkcs7->encryptOID = RSAk;
  24974. pkcs7->hashOID = SHA256h;
  24975. pkcs7->rng = &rng;
  24976. }
  24977. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  24978. wc_PKCS7_Free(pkcs7);
  24979. pkcs7 = NULL;
  24980. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  24981. #ifdef HAVE_AES_CBC
  24982. /* create envelope */
  24983. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  24984. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24985. if (pkcs7 != NULL) {
  24986. pkcs7->content = sig;
  24987. pkcs7->contentSz = sigSz;
  24988. pkcs7->contentOID = DATA;
  24989. pkcs7->encryptOID = AES256CBCb;
  24990. pkcs7->privateKey = key;
  24991. pkcs7->privateKeySz = keySz;
  24992. }
  24993. ExpectIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  24994. ExpectIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  24995. wc_PKCS7_Free(pkcs7);
  24996. pkcs7 = NULL;
  24997. #endif
  24998. /* create bad signed enveloped data */
  24999. sigSz = FOURK_BUF * 2;
  25000. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25001. ExpectIntEQ(wc_InitRng(&rng), 0);
  25002. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25003. if (pkcs7 != NULL) {
  25004. pkcs7->content = env;
  25005. pkcs7->contentSz = envSz;
  25006. pkcs7->contentOID = DATA;
  25007. pkcs7->privateKey = key;
  25008. pkcs7->privateKeySz = keySz;
  25009. pkcs7->encryptOID = RSAk;
  25010. pkcs7->hashOID = SHA256h;
  25011. pkcs7->rng = &rng;
  25012. }
  25013. /* Set no certs in bundle for this test. Hang on to the pointer though to
  25014. * free it later. */
  25015. if (pkcs7 != NULL) {
  25016. pt = (void*)pkcs7->certList;
  25017. pkcs7->certList = NULL; /* no certs in bundle */
  25018. }
  25019. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  25020. if (pkcs7 != NULL) {
  25021. /* restore pointer for PKCS7 free call */
  25022. pkcs7->certList = (Pkcs7Cert*)pt;
  25023. }
  25024. wc_PKCS7_Free(pkcs7);
  25025. pkcs7 = NULL;
  25026. /* check verify fails */
  25027. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25028. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25029. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  25030. PKCS7_SIGNEEDS_CHECK);
  25031. /* try verifying the signature manually */
  25032. {
  25033. RsaKey rKey;
  25034. word32 idx = 0;
  25035. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  25036. WC_MAX_DIGEST_SIZE];
  25037. int digestSz;
  25038. ExpectIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  25039. ExpectIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  25040. ExpectIntGT(digestSz = wc_RsaSSL_Verify(pkcs7->signature,
  25041. pkcs7->signatureSz, digest, sizeof(digest), &rKey), 0);
  25042. ExpectIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  25043. ExpectIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  25044. ExpectIntEQ(wc_FreeRsaKey(&rKey), 0);
  25045. /* verify was success */
  25046. }
  25047. wc_PKCS7_Free(pkcs7);
  25048. pkcs7 = NULL;
  25049. /* initializing the PKCS7 struct with the signing certificate should pass */
  25050. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25051. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25052. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  25053. wc_PKCS7_Free(pkcs7);
  25054. pkcs7 = NULL;
  25055. /* create valid degenerate bundle */
  25056. sigSz = FOURK_BUF * 2;
  25057. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25058. if (pkcs7 != NULL) {
  25059. pkcs7->content = env;
  25060. pkcs7->contentSz = envSz;
  25061. pkcs7->contentOID = DATA;
  25062. pkcs7->privateKey = key;
  25063. pkcs7->privateKeySz = keySz;
  25064. pkcs7->encryptOID = RSAk;
  25065. pkcs7->hashOID = SHA256h;
  25066. pkcs7->rng = &rng;
  25067. }
  25068. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  25069. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  25070. wc_PKCS7_Free(pkcs7);
  25071. pkcs7 = NULL;
  25072. wc_FreeRng(&rng);
  25073. /* check verify */
  25074. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25075. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25076. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  25077. ExpectNotNull(pkcs7->content);
  25078. #ifdef HAVE_AES_CBC
  25079. /* check decode */
  25080. ExpectNotNull(inner = wc_PKCS7_New(NULL, 0));
  25081. ExpectIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  25082. if (inner != NULL) {
  25083. inner->privateKey = key;
  25084. inner->privateKeySz = keySz;
  25085. }
  25086. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  25087. pkcs7->contentSz, decoded, decodedSz)), 0);
  25088. wc_PKCS7_Free(inner);
  25089. inner = NULL;
  25090. #endif
  25091. wc_PKCS7_Free(pkcs7);
  25092. pkcs7 = NULL;
  25093. #ifdef HAVE_AES_CBC
  25094. /* check cert set */
  25095. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25096. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25097. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  25098. ExpectNotNull(pkcs7->singleCert);
  25099. ExpectIntNE(pkcs7->singleCertSz, 0);
  25100. wc_PKCS7_Free(pkcs7);
  25101. pkcs7 = NULL;
  25102. #endif
  25103. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  25104. return EXPECT_RESULT();
  25105. }
  25106. static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
  25107. {
  25108. EXPECT_DECLS;
  25109. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25110. && !defined(NO_AES)
  25111. PKCS7* pkcs7 = NULL;
  25112. void* heap = NULL;
  25113. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  25114. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  25115. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  25116. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  25117. wc_PKCS7_Free(pkcs7);
  25118. #endif
  25119. return EXPECT_RESULT();
  25120. }
  25121. static int test_wc_PKCS7_SetOriEncryptCtx(void)
  25122. {
  25123. EXPECT_DECLS;
  25124. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25125. && !defined(NO_AES)
  25126. PKCS7* pkcs7 = NULL;
  25127. void* heap = NULL;
  25128. WOLFSSL_CTX* ctx = NULL;
  25129. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  25130. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  25131. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  25132. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  25133. wc_PKCS7_Free(pkcs7);
  25134. #endif
  25135. return EXPECT_RESULT();
  25136. }
  25137. static int test_wc_PKCS7_SetOriDecryptCtx(void)
  25138. {
  25139. EXPECT_DECLS;
  25140. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25141. && !defined(NO_AES)
  25142. PKCS7* pkcs7 = NULL;
  25143. void* heap = NULL;
  25144. WOLFSSL_CTX* ctx = NULL;
  25145. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  25146. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  25147. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  25148. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  25149. wc_PKCS7_Free(pkcs7);
  25150. #endif
  25151. return EXPECT_RESULT();
  25152. }
  25153. static int test_wc_PKCS7_DecodeCompressedData(void)
  25154. {
  25155. EXPECT_DECLS;
  25156. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25157. && !defined(NO_AES) && defined(HAVE_LIBZ)
  25158. PKCS7* pkcs7 = NULL;
  25159. void* heap = NULL;
  25160. byte out[4096];
  25161. byte* decompressed = NULL;
  25162. int outSz;
  25163. int decompressedSz;
  25164. const char* cert = "./certs/client-cert.pem";
  25165. byte* cert_buf = NULL;
  25166. size_t cert_sz = 0;
  25167. ExpectIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  25168. ExpectNotNull((decompressed = (byte*)XMALLOC(cert_sz, heap,
  25169. DYNAMIC_TYPE_TMP_BUFFER)));
  25170. decompressedSz = (int)cert_sz;
  25171. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  25172. if (pkcs7 != NULL) {
  25173. pkcs7->content = (byte*)cert_buf;
  25174. pkcs7->contentSz = (word32)cert_sz;
  25175. pkcs7->contentOID = DATA;
  25176. }
  25177. ExpectIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  25178. sizeof(out))), 0);
  25179. wc_PKCS7_Free(pkcs7);
  25180. pkcs7 = NULL;
  25181. /* compressed key should be smaller than when started */
  25182. ExpectIntLT(outSz, cert_sz);
  25183. /* test decompression */
  25184. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  25185. ExpectIntEQ(pkcs7->contentOID, 0);
  25186. /* fail case with out buffer too small */
  25187. ExpectIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  25188. decompressed, outSz), 0);
  25189. /* success case */
  25190. ExpectIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  25191. decompressed, decompressedSz), cert_sz);
  25192. ExpectIntEQ(pkcs7->contentOID, DATA);
  25193. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  25194. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  25195. decompressed = NULL;
  25196. /* test decompression function with different 'max' inputs */
  25197. outSz = sizeof(out);
  25198. ExpectIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  25199. 0);
  25200. ExpectIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  25201. out, outSz, 0, heap), 0);
  25202. ExpectNull(decompressed);
  25203. ExpectIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  25204. out, outSz, 0, heap), 0);
  25205. ExpectNotNull(decompressed);
  25206. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  25207. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  25208. decompressed = NULL;
  25209. ExpectIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  25210. out, outSz, 0, heap), 0);
  25211. ExpectNotNull(decompressed);
  25212. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  25213. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  25214. if (cert_buf != NULL)
  25215. free(cert_buf);
  25216. wc_PKCS7_Free(pkcs7);
  25217. #endif
  25218. return EXPECT_RESULT();
  25219. }
  25220. static int test_wc_i2d_PKCS12(void)
  25221. {
  25222. EXPECT_DECLS;
  25223. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  25224. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25225. && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
  25226. WC_PKCS12* pkcs12 = NULL;
  25227. unsigned char der[FOURK_BUF * 2];
  25228. unsigned char* pt;
  25229. int derSz = 0;
  25230. unsigned char out[FOURK_BUF * 2];
  25231. int outSz = FOURK_BUF * 2;
  25232. const char p12_f[] = "./certs/test-servercert.p12";
  25233. XFILE f = XBADFILE;
  25234. ExpectTrue((f = XFOPEN(p12_f, "rb")) != XBADFILE);
  25235. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  25236. if (f != XBADFILE)
  25237. XFCLOSE(f);
  25238. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  25239. ExpectIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  25240. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  25241. ExpectIntEQ(outSz, derSz);
  25242. outSz = derSz - 1;
  25243. pt = out;
  25244. ExpectIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  25245. outSz = derSz;
  25246. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  25247. ExpectIntEQ((pt == out), 0);
  25248. pt = NULL;
  25249. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  25250. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  25251. wc_PKCS12_free(pkcs12);
  25252. pkcs12 = NULL;
  25253. /* Run the same test but use wc_d2i_PKCS12_fp. */
  25254. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  25255. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  25256. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  25257. ExpectIntEQ(outSz, derSz);
  25258. wc_PKCS12_free(pkcs12);
  25259. pkcs12 = NULL;
  25260. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  25261. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  25262. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  25263. ExpectIntEQ(outSz, derSz);
  25264. wc_PKCS12_free(pkcs12);
  25265. pkcs12 = NULL;
  25266. #endif
  25267. return EXPECT_RESULT();
  25268. }
  25269. /* Testing wc_SignatureGetSize() for signature type ECC */
  25270. static int test_wc_SignatureGetSize_ecc(void)
  25271. {
  25272. EXPECT_DECLS;
  25273. #if !defined(NO_SIG_WRAPPER) && defined(HAVE_ECC) && !defined(NO_ECC256)
  25274. enum wc_SignatureType sig_type;
  25275. word32 key_len;
  25276. ecc_key ecc;
  25277. const char* qx =
  25278. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  25279. const char* qy =
  25280. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  25281. const char* d =
  25282. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  25283. XMEMSET(&ecc, 0, sizeof(ecc_key));
  25284. ExpectIntEQ(wc_ecc_init(&ecc), 0);
  25285. ExpectIntEQ(wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1"), 0);
  25286. /* Input for signature type ECC */
  25287. sig_type = WC_SIGNATURE_TYPE_ECC;
  25288. key_len = sizeof(ecc_key);
  25289. ExpectIntGT(wc_SignatureGetSize(sig_type, &ecc, key_len), 0);
  25290. /* Test bad args */
  25291. sig_type = (enum wc_SignatureType) 100;
  25292. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  25293. sig_type = WC_SIGNATURE_TYPE_ECC;
  25294. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
  25295. key_len = (word32)0;
  25296. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  25297. DoExpectIntEQ(wc_ecc_free(&ecc), 0);
  25298. #endif /* !NO_SIG_WRAPPER && HAVE_ECC && !NO_ECC256 */
  25299. return EXPECT_RESULT();
  25300. } /* END test_wc_SignatureGetSize_ecc() */
  25301. /* Testing wc_SignatureGetSize() for signature type rsa */
  25302. static int test_wc_SignatureGetSize_rsa(void)
  25303. {
  25304. EXPECT_DECLS;
  25305. #if !defined(NO_SIG_WRAPPER) && !defined(NO_RSA)
  25306. enum wc_SignatureType sig_type;
  25307. word32 key_len;
  25308. word32 idx = 0;
  25309. RsaKey rsa_key;
  25310. byte* tmp = NULL;
  25311. size_t bytes;
  25312. XMEMSET(&rsa_key, 0, sizeof(RsaKey));
  25313. #ifdef USE_CERT_BUFFERS_1024
  25314. bytes = (size_t)sizeof_client_key_der_1024;
  25315. if (bytes < (size_t)sizeof_client_key_der_1024)
  25316. bytes = (size_t)sizeof_client_cert_der_1024;
  25317. #elif defined(USE_CERT_BUFFERS_2048)
  25318. bytes = (size_t)sizeof_client_key_der_2048;
  25319. if (bytes < (size_t)sizeof_client_cert_der_2048)
  25320. bytes = (size_t)sizeof_client_cert_der_2048;
  25321. #else
  25322. bytes = FOURK_BUF;
  25323. #endif
  25324. ExpectNotNull(tmp = (byte*)XMALLOC(bytes, HEAP_HINT,
  25325. DYNAMIC_TYPE_TMP_BUFFER));
  25326. if (tmp != NULL) {
  25327. #ifdef USE_CERT_BUFFERS_1024
  25328. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  25329. #elif defined(USE_CERT_BUFFERS_2048)
  25330. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  25331. #elif !defined(NO_FILESYSTEM)
  25332. XFILE file = XBADFILE;
  25333. ExpectTrue((file = XFOPEN(clientKey, "rb")) != XBADFILE);
  25334. ExpectIntGT(bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file), 0);
  25335. if (file != XBADFILE)
  25336. XFCLOSE(file);
  25337. }
  25338. #else
  25339. ExpectFail();
  25340. #endif
  25341. }
  25342. ExpectIntEQ(wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId), 0);
  25343. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes), 0);
  25344. /* Input for signature type RSA */
  25345. sig_type = WC_SIGNATURE_TYPE_RSA;
  25346. key_len = sizeof(RsaKey);
  25347. ExpectIntGT(wc_SignatureGetSize(sig_type, &rsa_key, key_len), 0);
  25348. /* Test bad args */
  25349. sig_type = (enum wc_SignatureType)100;
  25350. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  25351. sig_type = WC_SIGNATURE_TYPE_RSA;
  25352. #ifndef HAVE_USER_RSA
  25353. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), BAD_FUNC_ARG);
  25354. #else
  25355. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
  25356. #endif
  25357. key_len = (word32)0;
  25358. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  25359. DoExpectIntEQ(wc_FreeRsaKey(&rsa_key), 0);
  25360. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25361. #endif /* !NO_SIG_WRAPPER && !NO_RSA */
  25362. return EXPECT_RESULT();
  25363. } /* END test_wc_SignatureGetSize_rsa(void) */
  25364. /*----------------------------------------------------------------------------*
  25365. | hash.h Tests
  25366. *----------------------------------------------------------------------------*/
  25367. static int test_wc_HashInit(void)
  25368. {
  25369. EXPECT_DECLS;
  25370. int i; /* 0 indicates tests passed, 1 indicates failure */
  25371. wc_HashAlg hash;
  25372. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25373. enum wc_HashType enumArray[] = {
  25374. #ifndef NO_MD5
  25375. WC_HASH_TYPE_MD5,
  25376. #endif
  25377. #ifndef NO_SHA
  25378. WC_HASH_TYPE_SHA,
  25379. #endif
  25380. #ifdef WOLFSSL_SHA224
  25381. WC_HASH_TYPE_SHA224,
  25382. #endif
  25383. #ifndef NO_SHA256
  25384. WC_HASH_TYPE_SHA256,
  25385. #endif
  25386. #ifdef WOLFSSL_SHA384
  25387. WC_HASH_TYPE_SHA384,
  25388. #endif
  25389. #ifdef WOLFSSL_SHA512
  25390. WC_HASH_TYPE_SHA512,
  25391. #endif
  25392. };
  25393. /* dynamically finds the length */
  25394. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25395. /* For loop to test various arguments... */
  25396. for (i = 0; i < enumlen; i++) {
  25397. /* check for bad args */
  25398. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  25399. wc_HashFree(&hash, enumArray[i]);
  25400. /* check for null ptr */
  25401. ExpectIntEQ(wc_HashInit(NULL, enumArray[i]), BAD_FUNC_ARG);
  25402. } /* end of for loop */
  25403. return EXPECT_RESULT();
  25404. } /* end of test_wc_HashInit */
  25405. /*
  25406. * Unit test function for wc_HashSetFlags()
  25407. */
  25408. static int test_wc_HashSetFlags(void)
  25409. {
  25410. EXPECT_DECLS;
  25411. #ifdef WOLFSSL_HASH_FLAGS
  25412. wc_HashAlg hash;
  25413. word32 flags = 0;
  25414. int i, j;
  25415. int notSupportedLen;
  25416. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25417. enum wc_HashType enumArray[] = {
  25418. #ifndef NO_MD5
  25419. WC_HASH_TYPE_MD5,
  25420. #endif
  25421. #ifndef NO_SHA
  25422. WC_HASH_TYPE_SHA,
  25423. #endif
  25424. #ifdef WOLFSSL_SHA224
  25425. WC_HASH_TYPE_SHA224,
  25426. #endif
  25427. #ifndef NO_SHA256
  25428. WC_HASH_TYPE_SHA256,
  25429. #endif
  25430. #ifdef WOLFSSL_SHA384
  25431. WC_HASH_TYPE_SHA384,
  25432. #endif
  25433. #ifdef WOLFSSL_SHA512
  25434. WC_HASH_TYPE_SHA512,
  25435. #endif
  25436. #ifdef WOLFSSL_SHA3
  25437. WC_HASH_TYPE_SHA3_224,
  25438. #endif
  25439. };
  25440. enum wc_HashType notSupported[] = {
  25441. WC_HASH_TYPE_MD5_SHA,
  25442. WC_HASH_TYPE_MD2,
  25443. WC_HASH_TYPE_MD4,
  25444. WC_HASH_TYPE_BLAKE2B,
  25445. WC_HASH_TYPE_BLAKE2S,
  25446. WC_HASH_TYPE_NONE,
  25447. };
  25448. /* dynamically finds the length */
  25449. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25450. /* For loop to test various arguments... */
  25451. for (i = 0; i < enumlen; i++) {
  25452. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  25453. ExpectIntEQ(wc_HashSetFlags(&hash, enumArray[i], flags), 0);
  25454. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  25455. ExpectIntEQ(wc_HashSetFlags(NULL, enumArray[i], flags), BAD_FUNC_ARG);
  25456. wc_HashFree(&hash, enumArray[i]);
  25457. }
  25458. /* For loop to test not supported cases */
  25459. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  25460. for (j = 0; j < notSupportedLen; j++) {
  25461. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  25462. ExpectIntEQ(wc_HashSetFlags(&hash, notSupported[j], flags),
  25463. BAD_FUNC_ARG);
  25464. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  25465. }
  25466. #endif
  25467. return EXPECT_RESULT();
  25468. } /* END test_wc_HashSetFlags */
  25469. /*
  25470. * Unit test function for wc_HashGetFlags()
  25471. */
  25472. static int test_wc_HashGetFlags(void)
  25473. {
  25474. EXPECT_DECLS;
  25475. #ifdef WOLFSSL_HASH_FLAGS
  25476. wc_HashAlg hash;
  25477. word32 flags = 0;
  25478. int i, j;
  25479. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25480. enum wc_HashType enumArray[] = {
  25481. #ifndef NO_MD5
  25482. WC_HASH_TYPE_MD5,
  25483. #endif
  25484. #ifndef NO_SHA
  25485. WC_HASH_TYPE_SHA,
  25486. #endif
  25487. #ifdef WOLFSSL_SHA224
  25488. WC_HASH_TYPE_SHA224,
  25489. #endif
  25490. #ifndef NO_SHA256
  25491. WC_HASH_TYPE_SHA256,
  25492. #endif
  25493. #ifdef WOLFSSL_SHA384
  25494. WC_HASH_TYPE_SHA384,
  25495. #endif
  25496. #ifdef WOLFSSL_SHA512
  25497. WC_HASH_TYPE_SHA512,
  25498. #endif
  25499. #ifdef WOLFSSL_SHA3
  25500. WC_HASH_TYPE_SHA3_224,
  25501. #endif
  25502. };
  25503. enum wc_HashType notSupported[] = {
  25504. WC_HASH_TYPE_MD5_SHA,
  25505. WC_HASH_TYPE_MD2,
  25506. WC_HASH_TYPE_MD4,
  25507. WC_HASH_TYPE_BLAKE2B,
  25508. WC_HASH_TYPE_BLAKE2S,
  25509. WC_HASH_TYPE_NONE,
  25510. };
  25511. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25512. int notSupportedLen;
  25513. /* For loop to test various arguments... */
  25514. for (i = 0; i < enumlen; i++) {
  25515. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  25516. ExpectIntEQ(wc_HashGetFlags(&hash, enumArray[i], &flags), 0);
  25517. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  25518. ExpectIntEQ(wc_HashGetFlags(NULL, enumArray[i], &flags), BAD_FUNC_ARG);
  25519. wc_HashFree(&hash, enumArray[i]);
  25520. }
  25521. /* For loop to test not supported cases */
  25522. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  25523. for (j = 0; j < notSupportedLen; j++) {
  25524. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  25525. ExpectIntEQ(wc_HashGetFlags(&hash, notSupported[j], &flags),
  25526. BAD_FUNC_ARG);
  25527. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  25528. }
  25529. #endif
  25530. return EXPECT_RESULT();
  25531. } /* END test_wc_HashGetFlags */
  25532. /*----------------------------------------------------------------------------*
  25533. | Compatibility Tests
  25534. *----------------------------------------------------------------------------*/
  25535. /*----------------------------------------------------------------------------*
  25536. | ASN.1 Tests
  25537. *----------------------------------------------------------------------------*/
  25538. static int test_wolfSSL_ASN1_BIT_STRING(void)
  25539. {
  25540. EXPECT_DECLS;
  25541. #if !defined(NO_CERTS) && defined(OPENSSL_ALL)
  25542. ASN1_BIT_STRING* str = NULL;
  25543. ExpectNotNull(str = ASN1_BIT_STRING_new());
  25544. /* Empty data testing. */
  25545. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 1), 0);
  25546. ASN1_BIT_STRING_free(str);
  25547. str = NULL;
  25548. ExpectNotNull(str = ASN1_BIT_STRING_new());
  25549. /* Invalid parameter testing. */
  25550. ExpectIntEQ(ASN1_BIT_STRING_set_bit(NULL, 42, 1), 0);
  25551. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, -1, 1), 0);
  25552. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 2), 0);
  25553. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, -1), 0);
  25554. /* No bit string - bit is always 0. */
  25555. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, 42), 0);
  25556. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, -1), 0);
  25557. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  25558. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 0), 0);
  25559. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  25560. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  25561. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  25562. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  25563. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  25564. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  25565. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  25566. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 91, 0), 1);
  25567. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 91), 0);
  25568. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 89, 0), 1);
  25569. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 89), 0);
  25570. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 0), 1);
  25571. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 0);
  25572. ASN1_BIT_STRING_free(str);
  25573. ASN1_BIT_STRING_free(NULL);
  25574. #endif
  25575. return EXPECT_RESULT();
  25576. }
  25577. static int test_wolfSSL_ASN1_INTEGER(void)
  25578. {
  25579. EXPECT_DECLS;
  25580. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  25581. ASN1_INTEGER* a = NULL;
  25582. ASN1_INTEGER* dup = NULL;
  25583. const unsigned char invalidLenDer[] = {
  25584. 0x02, 0x20, 0x00
  25585. };
  25586. const unsigned char longDer[] = {
  25587. 0x02, 0x20,
  25588. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  25589. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  25590. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  25591. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
  25592. };
  25593. const unsigned char* p;
  25594. /* Invalid parameter testing. */
  25595. ASN1_INTEGER_free(NULL);
  25596. ExpectNull(wolfSSL_ASN1_INTEGER_dup(NULL));
  25597. ExpectNotNull(a = ASN1_INTEGER_new());
  25598. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  25599. ASN1_INTEGER_free(dup);
  25600. dup = NULL;
  25601. ASN1_INTEGER_free(a);
  25602. a = NULL;
  25603. p = longDer;
  25604. ExpectNull(d2i_ASN1_INTEGER(NULL, &p, sizeof(invalidLenDer)));
  25605. p = longDer;
  25606. ExpectNotNull(a = d2i_ASN1_INTEGER(NULL, &p, sizeof(longDer)));
  25607. ExpectPtrNE(p, longDer);
  25608. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  25609. ASN1_INTEGER_free(dup);
  25610. ASN1_INTEGER_free(a);
  25611. #endif
  25612. return EXPECT_RESULT();
  25613. }
  25614. static int test_wolfSSL_ASN1_INTEGER_cmp(void)
  25615. {
  25616. EXPECT_DECLS;
  25617. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  25618. ASN1_INTEGER* a = NULL;
  25619. ASN1_INTEGER* b = NULL;
  25620. ExpectNotNull(a = ASN1_INTEGER_new());
  25621. ExpectNotNull(b = ASN1_INTEGER_new());
  25622. ExpectIntEQ(ASN1_INTEGER_set(a, 1), 1);
  25623. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  25624. /* Invalid parameter testing. */
  25625. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, NULL), -1);
  25626. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, NULL), -1);
  25627. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, b), -1);
  25628. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  25629. ExpectIntEQ(ASN1_INTEGER_set(b, -1), 1);
  25630. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  25631. ExpectIntEQ(ASN1_INTEGER_set(a, -2), 1);
  25632. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  25633. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  25634. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  25635. ExpectIntEQ(ASN1_INTEGER_set(a, 0x01), 1);
  25636. ExpectIntEQ(ASN1_INTEGER_set(b, 0x1000), 1);
  25637. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  25638. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(b, a), 0);
  25639. ASN1_INTEGER_free(b);
  25640. ASN1_INTEGER_free(a);
  25641. #endif
  25642. return EXPECT_RESULT();
  25643. }
  25644. static int test_wolfSSL_ASN1_INTEGER_BN(void)
  25645. {
  25646. EXPECT_DECLS;
  25647. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  25648. ASN1_INTEGER* ai = NULL;
  25649. ASN1_INTEGER* ai2 = NULL;
  25650. BIGNUM* bn = NULL;
  25651. BIGNUM* bn2 = NULL;
  25652. ExpectNotNull(ai = ASN1_INTEGER_new());
  25653. ExpectNotNull(bn2 = BN_new());
  25654. /* Invalid parameter testing. */
  25655. ExpectNull(bn = ASN1_INTEGER_to_BN(NULL, NULL));
  25656. ExpectNull(ai2 = BN_to_ASN1_INTEGER(NULL, NULL));
  25657. /* at the moment hard setting since no set function */
  25658. if (ai != NULL) {
  25659. ai->data[0] = 0xff; /* No DER encoding. */
  25660. ai->length = 1;
  25661. }
  25662. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  25663. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  25664. BN_free(bn);
  25665. bn = NULL;
  25666. #else
  25667. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  25668. #endif
  25669. if (ai != NULL) {
  25670. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  25671. ai->data[1] = 0x04; /* bad length of integer */
  25672. ai->data[2] = 0x03;
  25673. ai->length = 3;
  25674. }
  25675. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  25676. /* Interpreted as a number 0x020403. */
  25677. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  25678. BN_free(bn);
  25679. bn = NULL;
  25680. #else
  25681. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  25682. #endif
  25683. if (ai != NULL) {
  25684. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  25685. ai->data[1] = 0x01; /* length of integer */
  25686. ai->data[2] = 0x03;
  25687. ai->length = 3;
  25688. }
  25689. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  25690. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, NULL));
  25691. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  25692. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  25693. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  25694. if (ai != NULL) {
  25695. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  25696. ai->data[1] = 0x02; /* length of integer */
  25697. ai->data[2] = 0x00; /* padding byte to ensure positive */
  25698. ai->data[3] = 0xff;
  25699. ai->length = 4;
  25700. }
  25701. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  25702. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  25703. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  25704. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  25705. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  25706. if (ai != NULL) {
  25707. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  25708. ai->data[1] = 0x01; /* length of integer */
  25709. ai->data[2] = 0x00;
  25710. ai->length = 3;
  25711. }
  25712. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  25713. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  25714. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  25715. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  25716. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  25717. if (ai != NULL) {
  25718. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  25719. ai->data[1] = 0x01; /* length of integer */
  25720. ai->data[2] = 0x01;
  25721. ai->length = 3;
  25722. ai->negative = 1;
  25723. }
  25724. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  25725. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  25726. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  25727. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  25728. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  25729. BN_free(bn2);
  25730. BN_free(bn);
  25731. ASN1_INTEGER_free(ai2);
  25732. ASN1_INTEGER_free(ai);
  25733. #endif
  25734. return EXPECT_RESULT();
  25735. }
  25736. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  25737. {
  25738. EXPECT_DECLS;
  25739. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  25740. ASN1_INTEGER *a = NULL;
  25741. long val;
  25742. ExpectNotNull(a = ASN1_INTEGER_new());
  25743. /* Invalid parameter testing. */
  25744. ExpectIntEQ(ASN1_INTEGER_get(NULL), 0);
  25745. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  25746. ExpectIntEQ(ASN1_INTEGER_get(a), 0);
  25747. #else
  25748. ExpectIntEQ(ASN1_INTEGER_get(a), -1);
  25749. #endif
  25750. ASN1_INTEGER_free(a);
  25751. a = NULL;
  25752. ExpectNotNull(a = ASN1_INTEGER_new());
  25753. val = 0;
  25754. ExpectIntEQ(ASN1_INTEGER_set(NULL, val), 0);
  25755. ASN1_INTEGER_free(a);
  25756. a = NULL;
  25757. /* 0 */
  25758. ExpectNotNull(a = ASN1_INTEGER_new());
  25759. val = 0;
  25760. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25761. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25762. ASN1_INTEGER_free(a);
  25763. a = NULL;
  25764. /* 40 */
  25765. ExpectNotNull(a = ASN1_INTEGER_new());
  25766. val = 40;
  25767. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25768. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25769. ASN1_INTEGER_free(a);
  25770. a = NULL;
  25771. /* -40 */
  25772. ExpectNotNull(a = ASN1_INTEGER_new());
  25773. val = -40;
  25774. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25775. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25776. ASN1_INTEGER_free(a);
  25777. a = NULL;
  25778. /* 128 */
  25779. ExpectNotNull(a = ASN1_INTEGER_new());
  25780. val = 128;
  25781. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25782. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25783. ASN1_INTEGER_free(a);
  25784. a = NULL;
  25785. /* -128 */
  25786. ExpectNotNull(a = ASN1_INTEGER_new());
  25787. val = -128;
  25788. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25789. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25790. ASN1_INTEGER_free(a);
  25791. a = NULL;
  25792. /* 200 */
  25793. ExpectNotNull(a = ASN1_INTEGER_new());
  25794. val = 200;
  25795. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25796. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25797. ASN1_INTEGER_free(a);
  25798. a = NULL;
  25799. /* int max (2147483647) */
  25800. ExpectNotNull(a = ASN1_INTEGER_new());
  25801. val = 2147483647;
  25802. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25803. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25804. ASN1_INTEGER_free(a);
  25805. a = NULL;
  25806. /* int min (-2147483648) */
  25807. ExpectNotNull(a = ASN1_INTEGER_new());
  25808. val = -2147483647 - 1;
  25809. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25810. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25811. ASN1_INTEGER_free(a);
  25812. a = NULL;
  25813. /* long max positive */
  25814. ExpectNotNull(a = ASN1_INTEGER_new());
  25815. val = (long)(((unsigned long)-1) >> 1);
  25816. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25817. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25818. ASN1_INTEGER_free(a);
  25819. #endif
  25820. return EXPECT_RESULT();
  25821. }
  25822. #if defined(OPENSSL_EXTRA)
  25823. typedef struct ASN1IntTestVector {
  25824. const byte* der;
  25825. const size_t derSz;
  25826. const long value;
  25827. } ASN1IntTestVector;
  25828. #endif
  25829. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  25830. {
  25831. EXPECT_DECLS;
  25832. #if defined(OPENSSL_EXTRA)
  25833. size_t i;
  25834. WOLFSSL_ASN1_INTEGER* a = NULL;
  25835. WOLFSSL_ASN1_INTEGER* b = NULL;
  25836. WOLFSSL_ASN1_INTEGER* c = NULL;
  25837. const byte* p = NULL;
  25838. byte* p2 = NULL;
  25839. byte* reEncoded = NULL;
  25840. int reEncodedSz;
  25841. static const byte zeroDer[] = {
  25842. 0x02, 0x01, 0x00
  25843. };
  25844. static const byte oneDer[] = {
  25845. 0x02, 0x01, 0x01
  25846. };
  25847. static const byte negativeDer[] = {
  25848. 0x02, 0x03, 0xC1, 0x16, 0x0D
  25849. };
  25850. static const byte positiveDer[] = {
  25851. 0x02, 0x03, 0x01, 0x00, 0x01
  25852. };
  25853. static const byte primeDer[] = {
  25854. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  25855. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  25856. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  25857. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  25858. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  25859. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  25860. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  25861. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  25862. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  25863. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  25864. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  25865. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  25866. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  25867. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  25868. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  25869. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  25870. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  25871. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  25872. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  25873. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  25874. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  25875. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  25876. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  25877. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  25878. };
  25879. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  25880. static const ASN1IntTestVector testVectors[] = {
  25881. {zeroDer, sizeof(zeroDer), 0},
  25882. {oneDer, sizeof(oneDer), 1},
  25883. {negativeDer, sizeof(negativeDer), -4123123},
  25884. {positiveDer, sizeof(positiveDer), 65537},
  25885. {primeDer, sizeof(primeDer), 0}
  25886. };
  25887. static const size_t NUM_TEST_VECTORS =
  25888. sizeof(testVectors)/sizeof(testVectors[0]);
  25889. /* Check d2i error conditions */
  25890. /* NULL pointer to input. */
  25891. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  25892. ExpectNull(b);
  25893. /* NULL input. */
  25894. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  25895. ExpectNull(b);
  25896. /* 0 length. */
  25897. p = testVectors[0].der;
  25898. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  25899. ExpectNull(b);
  25900. /* Negative length. */
  25901. p = testVectors[0].der;
  25902. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  25903. ExpectNull(b);
  25904. /* Garbage DER input. */
  25905. p = garbageDer;
  25906. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  25907. ExpectNull(b);
  25908. /* Check i2d error conditions */
  25909. /* NULL input. */
  25910. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  25911. /* 0 length input data buffer (a->length == 0). */
  25912. ExpectNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  25913. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  25914. if (a != NULL)
  25915. a->data = NULL;
  25916. /* NULL input data buffer. */
  25917. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  25918. if (a != NULL) {
  25919. /* Reset a->data. */
  25920. a->data = a->intData;
  25921. }
  25922. /* Set a to valid value. */
  25923. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  25924. /* NULL output buffer. */
  25925. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  25926. wolfSSL_ASN1_INTEGER_free(a);
  25927. a = NULL;
  25928. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  25929. p = testVectors[i].der;
  25930. ExpectNotNull(a = wolfSSL_d2i_ASN1_INTEGER(&b, &p,
  25931. testVectors[i].derSz));
  25932. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  25933. if (testVectors[i].derSz <= sizeof(long)) {
  25934. ExpectNotNull(c = wolfSSL_ASN1_INTEGER_new());
  25935. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value), 1);
  25936. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  25937. wolfSSL_ASN1_INTEGER_free(c);
  25938. c = NULL;
  25939. }
  25940. /* Convert to DER without a pre-allocated output buffer. */
  25941. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  25942. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  25943. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  25944. /* Convert to DER with a pre-allocated output buffer. In this case, the
  25945. * output buffer pointer should be incremented just past the end of the
  25946. * encoded data. */
  25947. p2 = reEncoded;
  25948. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &p2)), 0);
  25949. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  25950. ExpectPtrEq(reEncoded, p2 - reEncodedSz);
  25951. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  25952. XFREE(reEncoded, NULL, DYNAMIC_TYPE_ASN1);
  25953. reEncoded = NULL;
  25954. wolfSSL_ASN1_INTEGER_free(a);
  25955. a = NULL;
  25956. }
  25957. #endif /* OPENSSL_EXTRA */
  25958. return EXPECT_RESULT();
  25959. }
  25960. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  25961. {
  25962. EXPECT_DECLS;
  25963. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  25964. BIO* bio = NULL;
  25965. BIO* out = NULL;
  25966. BIO* fixed = NULL;
  25967. ASN1_INTEGER* ai = NULL;
  25968. char buf[] = "123456\n12345\n1123456789123456\\\n78901234567890 \r\n\n";
  25969. char tmp[1024];
  25970. int tmpSz;
  25971. const char expected1[] = "123456";
  25972. const char expected2[] = "112345678912345678901234567890";
  25973. char longStr[] = "123456781234567812345678123456781234567812345678\n"
  25974. "123456781234567812345678123456781234567812345678\\\n12345678\n";
  25975. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  25976. ExpectNotNull(ai = ASN1_INTEGER_new());
  25977. ExpectNotNull(bio = BIO_new_mem_buf(buf, -1));
  25978. /* Invalid parameter testing. */
  25979. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, -1), 0);
  25980. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, NULL, -1), 0);
  25981. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, NULL, -1), 0);
  25982. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, tmp, -1), 0);
  25983. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, 1024), 0);
  25984. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, tmp, 1024), 0);
  25985. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, tmp, 1024), 0);
  25986. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, NULL, 1024), 0);
  25987. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, -1), 0);
  25988. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, NULL), 0);
  25989. ExpectIntEQ(i2a_ASN1_INTEGER(bio, NULL), 0);
  25990. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, ai), 0);
  25991. /* No data to read from BIO. */
  25992. ExpectIntEQ(a2i_ASN1_INTEGER(out, ai, tmp, 1024), 0);
  25993. /* read first line */
  25994. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  25995. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  25996. XMEMSET(tmp, 0, 1024);
  25997. tmpSz = BIO_read(out, tmp, 1024);
  25998. ExpectIntEQ(tmpSz, 6);
  25999. ExpectIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  26000. /* fail on second line (not % 2) */
  26001. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  26002. /* read 3rd long line */
  26003. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  26004. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  26005. XMEMSET(tmp, 0, 1024);
  26006. tmpSz = BIO_read(out, tmp, 1024);
  26007. ExpectIntEQ(tmpSz, 30);
  26008. ExpectIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  26009. /* fail on empty line */
  26010. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  26011. BIO_free(bio);
  26012. bio = NULL;
  26013. /* Make long integer, requiring dynamic memory, even longer. */
  26014. ExpectNotNull(bio = BIO_new_mem_buf(longStr, -1));
  26015. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  26016. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 48);
  26017. XMEMSET(tmp, 0, 1024);
  26018. tmpSz = BIO_read(out, tmp, 1024);
  26019. ExpectIntEQ(tmpSz, 48);
  26020. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  26021. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 56);
  26022. XMEMSET(tmp, 0, 1024);
  26023. tmpSz = BIO_read(out, tmp, 1024);
  26024. ExpectIntEQ(tmpSz, 56);
  26025. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(ai, 1), 1);
  26026. BIO_free(bio);
  26027. BIO_free(out);
  26028. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  26029. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26030. /* Ensure there is 0 bytes available to write into. */
  26031. ExpectIntEQ(BIO_write(fixed, tmp, 1), 1);
  26032. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  26033. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26034. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  26035. BIO_free(fixed);
  26036. ASN1_INTEGER_free(ai);
  26037. #endif
  26038. return EXPECT_RESULT();
  26039. }
  26040. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  26041. {
  26042. EXPECT_DECLS;
  26043. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  26044. ASN1_INTEGER *a = NULL;
  26045. unsigned char *pp,*tpp;
  26046. int ret;
  26047. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  26048. /* Invalid parameter testing. */
  26049. /* Set pp to an invalid value. */
  26050. pp = NULL;
  26051. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, &pp), 0);
  26052. ExpectIntEQ(i2c_ASN1_INTEGER(a, &pp), 0);
  26053. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, NULL), 0);
  26054. /* 40 */
  26055. if (a != NULL) {
  26056. a->intData[0] = ASN_INTEGER;
  26057. a->intData[1] = 1;
  26058. a->intData[2] = 40;
  26059. }
  26060. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  26061. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26062. DYNAMIC_TYPE_TMP_BUFFER));
  26063. tpp = pp;
  26064. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26065. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  26066. tpp--;
  26067. ExpectIntEQ(*tpp, 40);
  26068. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26069. pp = NULL;
  26070. /* 128 */
  26071. if (a != NULL) {
  26072. a->intData[0] = ASN_INTEGER;
  26073. a->intData[1] = 1;
  26074. a->intData[2] = 128;
  26075. }
  26076. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  26077. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26078. DYNAMIC_TYPE_TMP_BUFFER));
  26079. tpp = pp;
  26080. if (tpp != NULL) {
  26081. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26082. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  26083. tpp--;
  26084. ExpectIntEQ(*(tpp--), 128);
  26085. ExpectIntEQ(*tpp, 0);
  26086. }
  26087. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26088. pp = NULL;
  26089. /* -40 */
  26090. if (a != NULL) {
  26091. a->intData[0] = ASN_INTEGER;
  26092. a->intData[1] = 1;
  26093. a->intData[2] = 40;
  26094. a->negative = 1;
  26095. }
  26096. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  26097. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26098. DYNAMIC_TYPE_TMP_BUFFER));
  26099. tpp = pp;
  26100. if (tpp != NULL) {
  26101. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26102. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  26103. tpp--;
  26104. ExpectIntEQ(*tpp, 216);
  26105. }
  26106. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26107. pp = NULL;
  26108. /* -128 */
  26109. if (a != NULL) {
  26110. a->intData[0] = ASN_INTEGER;
  26111. a->intData[1] = 1;
  26112. a->intData[2] = 128;
  26113. a->negative = 1;
  26114. }
  26115. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  26116. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26117. DYNAMIC_TYPE_TMP_BUFFER));
  26118. tpp = pp;
  26119. if (tpp != NULL) {
  26120. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26121. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  26122. tpp--;
  26123. ExpectIntEQ(*tpp, 128);
  26124. }
  26125. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26126. pp = NULL;
  26127. /* -200 */
  26128. if (a != NULL) {
  26129. a->intData[0] = ASN_INTEGER;
  26130. a->intData[1] = 1;
  26131. a->intData[2] = 200;
  26132. a->negative = 1;
  26133. }
  26134. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  26135. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26136. DYNAMIC_TYPE_TMP_BUFFER));
  26137. tpp = pp;
  26138. if (tpp != NULL) {
  26139. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26140. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  26141. tpp--;
  26142. ExpectIntEQ(*(tpp--), 56);
  26143. ExpectIntEQ(*tpp, 255);
  26144. }
  26145. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26146. pp = NULL;
  26147. /* Empty */
  26148. if (a != NULL) {
  26149. a->intData[0] = ASN_INTEGER;
  26150. a->intData[1] = 0;
  26151. a->negative = 0;
  26152. }
  26153. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  26154. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26155. DYNAMIC_TYPE_TMP_BUFFER));
  26156. tpp = pp;
  26157. if (tpp != NULL) {
  26158. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26159. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  26160. tpp--;
  26161. ExpectIntEQ(*tpp, 0);
  26162. }
  26163. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26164. pp = NULL;
  26165. /* 0 */
  26166. if (a != NULL) {
  26167. a->intData[0] = ASN_INTEGER;
  26168. a->intData[1] = 1;
  26169. a->intData[2] = 0;
  26170. a->negative = 1;
  26171. }
  26172. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  26173. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26174. DYNAMIC_TYPE_TMP_BUFFER));
  26175. if (tpp != NULL) {
  26176. tpp = pp;
  26177. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26178. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  26179. tpp--;
  26180. ExpectIntEQ(*tpp, 0);
  26181. }
  26182. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26183. pp = NULL;
  26184. /* 0x100 */
  26185. if (a != NULL) {
  26186. a->intData[0] = ASN_INTEGER;
  26187. a->intData[1] = 2;
  26188. a->intData[2] = 0x01;
  26189. a->intData[3] = 0x00;
  26190. a->negative = 0;
  26191. }
  26192. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  26193. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26194. DYNAMIC_TYPE_TMP_BUFFER));
  26195. if (tpp != NULL) {
  26196. tpp = pp;
  26197. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26198. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  26199. tpp -= 2;
  26200. ExpectIntEQ(tpp[0], 0x01);
  26201. ExpectIntEQ(tpp[1], 0x00);
  26202. }
  26203. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26204. pp = NULL;
  26205. /* -0x8000 => 0x8000 */
  26206. if (a != NULL) {
  26207. a->intData[0] = ASN_INTEGER;
  26208. a->intData[1] = 2;
  26209. a->intData[2] = 0x80;
  26210. a->intData[3] = 0x00;
  26211. a->negative = 1;
  26212. }
  26213. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  26214. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26215. DYNAMIC_TYPE_TMP_BUFFER));
  26216. tpp = pp;
  26217. if (tpp != NULL) {
  26218. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26219. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  26220. tpp -= 2;
  26221. ExpectIntEQ(tpp[0], 0x80);
  26222. ExpectIntEQ(tpp[1], 0x00);
  26223. }
  26224. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26225. pp = NULL;
  26226. /* -0x8001 => 0xFF7FFF */
  26227. if (a != NULL) {
  26228. a->intData[0] = ASN_INTEGER;
  26229. a->intData[1] = 2;
  26230. a->intData[2] = 0x80;
  26231. a->intData[3] = 0x01;
  26232. a->negative = 1;
  26233. }
  26234. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 3);
  26235. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26236. DYNAMIC_TYPE_TMP_BUFFER));
  26237. tpp = pp;
  26238. if (tpp != NULL) {
  26239. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26240. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 3);
  26241. tpp -= 3;
  26242. ExpectIntEQ(tpp[0], 0xFF);
  26243. ExpectIntEQ(tpp[1], 0x7F);
  26244. ExpectIntEQ(tpp[2], 0xFF);
  26245. }
  26246. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26247. wolfSSL_ASN1_INTEGER_free(a);
  26248. #endif /* OPENSSL_EXTRA && !NO_ASN */
  26249. return EXPECT_RESULT();
  26250. }
  26251. static int test_wolfSSL_ASN1_OBJECT(void)
  26252. {
  26253. EXPECT_DECLS;
  26254. #if defined(OPENSSL_EXTRA)
  26255. ASN1_OBJECT* a = NULL;
  26256. ASN1_OBJECT s;
  26257. const unsigned char der[] = { 0x06, 0x01, 0x00 };
  26258. /* Invalid parameter testing. */
  26259. ASN1_OBJECT_free(NULL);
  26260. ExpectNull(wolfSSL_ASN1_OBJECT_dup(NULL));
  26261. /* Test that a static ASN1_OBJECT can be freed. */
  26262. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  26263. ASN1_OBJECT_free(&s);
  26264. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  26265. ASN1_OBJECT_free(a);
  26266. a = NULL;
  26267. s.obj = der;
  26268. s.objSz = sizeof(der);
  26269. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  26270. ASN1_OBJECT_free(a);
  26271. ASN1_OBJECT_free(&s);
  26272. #endif /* OPENSSL_EXTRA */
  26273. return EXPECT_RESULT();
  26274. }
  26275. static int test_wolfSSL_ASN1_get_object(void)
  26276. {
  26277. EXPECT_DECLS;
  26278. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  26279. const unsigned char* derBuf = cliecc_cert_der_256;
  26280. const unsigned char* nullPtr = NULL;
  26281. const unsigned char objDerInvalidLen[] = { 0x30, 0x81 };
  26282. const unsigned char objDerBadLen[] = { 0x30, 0x04 };
  26283. const unsigned char objDerNotObj[] = { 0x02, 0x01, 0x00 };
  26284. const unsigned char objDerNoData[] = { 0x06, 0x00 };
  26285. const unsigned char* p;
  26286. unsigned char objDer[8];
  26287. unsigned char* der;
  26288. unsigned char* derPtr;
  26289. int len = sizeof_cliecc_cert_der_256;
  26290. long asnLen = 0;
  26291. int tag = 0;
  26292. int cls = 0;
  26293. ASN1_OBJECT* a = NULL;
  26294. ASN1_OBJECT s;
  26295. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  26296. /* Invalid encoding at length. */
  26297. p = objDerInvalidLen;
  26298. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  26299. 0x80);
  26300. p = objDerBadLen;
  26301. /* Error = 0x80, Constructed = 0x20 */
  26302. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  26303. 0x80 | 0x20);
  26304. /* Read a couple TLV triplets and make sure they match the expected values
  26305. */
  26306. /* SEQUENCE */
  26307. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  26308. ExpectIntEQ(asnLen, 862);
  26309. ExpectIntEQ(tag, 0x10);
  26310. ExpectIntEQ(cls, 0);
  26311. /* SEQUENCE */
  26312. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  26313. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  26314. ExpectIntEQ(asnLen, 772);
  26315. ExpectIntEQ(tag, 0x10);
  26316. ExpectIntEQ(cls, 0);
  26317. /* [0] */
  26318. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  26319. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  26320. ExpectIntEQ(asnLen, 3);
  26321. ExpectIntEQ(tag, 0);
  26322. ExpectIntEQ(cls, 0x80);
  26323. /* INTEGER */
  26324. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  26325. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  26326. ExpectIntEQ(asnLen, 1);
  26327. ExpectIntEQ(tag, 0x2);
  26328. ExpectIntEQ(cls, 0);
  26329. derBuf += asnLen;
  26330. /* INTEGER */
  26331. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  26332. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  26333. ExpectIntEQ(asnLen, 20);
  26334. ExpectIntEQ(tag, 0x2);
  26335. ExpectIntEQ(cls, 0);
  26336. derBuf += asnLen;
  26337. /* SEQUENCE */
  26338. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  26339. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  26340. ExpectIntEQ(asnLen, 10);
  26341. ExpectIntEQ(tag, 0x10);
  26342. ExpectIntEQ(cls, 0);
  26343. /* Found OBJECT_ID. */
  26344. /* Invalid parameter testing. */
  26345. ExpectIntEQ(ASN1_get_object(NULL, NULL, NULL, NULL, 0), 0x80);
  26346. ExpectIntEQ(ASN1_get_object(&nullPtr, NULL, NULL, NULL, 0), 0x80);
  26347. ExpectIntEQ(ASN1_get_object(NULL, &asnLen, &tag, &cls, len), 0x80);
  26348. ExpectIntEQ(ASN1_get_object(&nullPtr, &asnLen, &tag, &cls, len), 0x80);
  26349. ExpectIntEQ(ASN1_get_object(&derBuf, NULL, &tag, &cls, len), 0x80);
  26350. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, NULL, &cls, len), 0x80);
  26351. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, NULL, len), 0x80);
  26352. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, 0), 0x80);
  26353. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, -1), 0x80);
  26354. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, -1));
  26355. ExpectNull(d2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  26356. ExpectNull(d2i_ASN1_OBJECT(NULL, &derBuf, -1));
  26357. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, 0));
  26358. ExpectNull(d2i_ASN1_OBJECT(&a, NULL, len));
  26359. ExpectNull(d2i_ASN1_OBJECT(&a, &nullPtr, len));
  26360. ExpectNull(d2i_ASN1_OBJECT(&a, &derBuf, -1));
  26361. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, -1));
  26362. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  26363. ExpectNull(c2i_ASN1_OBJECT(NULL, &derBuf, -1));
  26364. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, 1));
  26365. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, 1));
  26366. /* Invalid encoding at length. */
  26367. p = objDerInvalidLen;
  26368. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerInvalidLen)));
  26369. p = objDerBadLen;
  26370. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerBadLen)));
  26371. p = objDerNotObj;
  26372. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNotObj)));
  26373. p = objDerNoData;
  26374. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNoData)));
  26375. /* Create an ASN OBJECT from content */
  26376. p = derBuf + 2;
  26377. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 8));
  26378. ASN1_OBJECT_free(a);
  26379. a = NULL;
  26380. /* Create an ASN OBJECT from DER */
  26381. ExpectNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  26382. /* Invalid parameter testing. */
  26383. ExpectIntEQ(i2d_ASN1_OBJECT(NULL, NULL), 0);
  26384. ExpectIntEQ(i2d_ASN1_OBJECT(&s, NULL), 0);
  26385. ExpectIntEQ(i2d_ASN1_OBJECT(a, NULL), 8);
  26386. der = NULL;
  26387. ExpectIntEQ(i2d_ASN1_OBJECT(a, &der), 8);
  26388. derPtr = objDer;
  26389. ExpectIntEQ(i2d_ASN1_OBJECT(a, &derPtr), 8);
  26390. ExpectPtrNE(derPtr, objDer);
  26391. ExpectIntEQ(XMEMCMP(der, objDer, 8), 0);
  26392. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  26393. ASN1_OBJECT_free(a);
  26394. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  26395. return EXPECT_RESULT();
  26396. }
  26397. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  26398. {
  26399. EXPECT_DECLS;
  26400. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  26401. ASN1_OBJECT* obj = NULL;
  26402. ASN1_OBJECT* a = NULL;
  26403. BIO *bio = NULL;
  26404. const unsigned char notObjDer[] = { 0x04, 0x01, 0xff };
  26405. const unsigned char badLenDer[] = { 0x06, 0x04, 0x01 };
  26406. const unsigned char goodDer[] = { 0x06, 0x01, 0x01 };
  26407. const unsigned char* p;
  26408. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  26409. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  26410. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  26411. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  26412. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  26413. /* No DER encoding in ASN1_OBJECT. */
  26414. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_new());
  26415. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  26416. ASN1_OBJECT_free(a);
  26417. a = NULL;
  26418. /* DER encoding - not OBJECT_ID */
  26419. p = notObjDer;
  26420. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  26421. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  26422. ASN1_OBJECT_free(a);
  26423. a = NULL;
  26424. /* Bad length encoding. */
  26425. p = badLenDer;
  26426. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  26427. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  26428. ASN1_OBJECT_free(a);
  26429. a = NULL;
  26430. /* Good encoding - but unknown. */
  26431. p = goodDer;
  26432. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  26433. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  26434. ASN1_OBJECT_free(a);
  26435. BIO_free(bio);
  26436. ASN1_OBJECT_free(obj);
  26437. #endif
  26438. return EXPECT_RESULT();
  26439. }
  26440. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  26441. {
  26442. EXPECT_DECLS;
  26443. #if defined(OPENSSL_EXTRA) && \
  26444. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  26445. char buf[50] = {0};
  26446. ASN1_OBJECT* obj;
  26447. const char* oid = "2.5.29.19";
  26448. const char* ln = "X509v3 Basic Constraints";
  26449. obj = NULL;
  26450. ExpectIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), 0);
  26451. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), 0);
  26452. ExpectIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), 0);
  26453. ExpectNotNull(obj = OBJ_txt2obj(oid, 0));
  26454. XMEMSET(buf, 0, sizeof(buf));
  26455. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  26456. ExpectIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  26457. ASN1_OBJECT_free(obj);
  26458. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  26459. return EXPECT_RESULT();
  26460. }
  26461. static int test_wolfSSL_sk_ASN1_OBJECT(void)
  26462. {
  26463. EXPECT_DECLS;
  26464. #if !defined(NO_ASN) && (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  26465. WOLFSSL_STACK* sk = NULL;
  26466. WOLFSSL_ASN1_OBJECT* obj;
  26467. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  26468. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  26469. wolfSSL_sk_ASN1_OBJECT_free(sk);
  26470. sk = NULL;
  26471. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  26472. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, NULL), 0);
  26473. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, NULL), 0);
  26474. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, obj), 0);
  26475. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  26476. wolfSSL_sk_ASN1_OBJECT_pop_free(sk, NULL);
  26477. sk = NULL;
  26478. /* obj freed in pop_free call. */
  26479. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  26480. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  26481. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  26482. ExpectPtrEq(obj, wolfSSL_sk_ASN1_OBJECT_pop(sk));
  26483. wolfSSL_sk_ASN1_OBJECT_free(sk);
  26484. wolfSSL_ASN1_OBJECT_free(obj);
  26485. #endif /* !NO_ASN && (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) */
  26486. return EXPECT_RESULT();
  26487. }
  26488. static int test_wolfSSL_ASN1_STRING(void)
  26489. {
  26490. EXPECT_DECLS;
  26491. #if defined(OPENSSL_EXTRA)
  26492. ASN1_STRING* str = NULL;
  26493. ASN1_STRING* c = NULL;
  26494. const char data[] = "hello wolfSSL";
  26495. const char data2[] = "Same len data";
  26496. const char longData[] =
  26497. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  26498. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  26499. ASN1_STRING_free(str);
  26500. str = NULL;
  26501. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  26502. ExpectIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  26503. ExpectIntEQ(ASN1_STRING_type(NULL), 0);
  26504. /* Check setting to NULL works. */
  26505. ExpectIntEQ(ASN1_STRING_set(str, NULL, 0), 1);
  26506. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  26507. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  26508. ExpectIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  26509. ExpectIntEQ(ASN1_STRING_set(NULL, NULL, 0), 0);
  26510. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, NULL), 0);
  26511. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(str, NULL), 0);
  26512. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, str), 0);
  26513. ExpectNull(wolfSSL_ASN1_STRING_dup(NULL));
  26514. ExpectNotNull(c = wolfSSL_ASN1_STRING_dup(str));
  26515. ExpectIntEQ(ASN1_STRING_cmp(NULL, NULL), -1);
  26516. ExpectIntEQ(ASN1_STRING_cmp(str, NULL), -1);
  26517. ExpectIntEQ(ASN1_STRING_cmp(NULL, c), -1);
  26518. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  26519. ExpectIntEQ(ASN1_STRING_set(c, (const void*)data2, -1), 1);
  26520. ExpectIntGT(ASN1_STRING_cmp(str, c), 0);
  26521. ExpectIntEQ(ASN1_STRING_set(str, (const void*)longData, -1), 1);
  26522. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(c, str), 1);
  26523. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  26524. /* Check setting back to smaller size frees dynamic data. */
  26525. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  26526. ExpectIntLT(ASN1_STRING_cmp(str, c), 0);
  26527. ExpectIntGT(ASN1_STRING_cmp(c, str), 0);
  26528. ExpectNull(ASN1_STRING_get0_data(NULL));
  26529. ExpectNotNull(ASN1_STRING_get0_data(str));
  26530. ExpectNull(ASN1_STRING_data(NULL));
  26531. ExpectNotNull(ASN1_STRING_data(str));
  26532. ExpectIntEQ(ASN1_STRING_length(NULL), 0);
  26533. ExpectIntGT(ASN1_STRING_length(str), 0);
  26534. ASN1_STRING_free(c);
  26535. ASN1_STRING_free(str);
  26536. ASN1_STRING_free(NULL);
  26537. #ifndef NO_WOLFSSL_STUB
  26538. ExpectNull(d2i_DISPLAYTEXT(NULL, NULL, 0));
  26539. #endif
  26540. #endif
  26541. return EXPECT_RESULT();
  26542. }
  26543. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  26544. {
  26545. EXPECT_DECLS;
  26546. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_RSA) && \
  26547. !defined(NO_FILESYSTEM)
  26548. WOLFSSL_X509* x509 = NULL;
  26549. WOLFSSL_X509_NAME* subject = NULL;
  26550. WOLFSSL_X509_NAME_ENTRY* e = NULL;
  26551. WOLFSSL_ASN1_STRING* a = NULL;
  26552. FILE* file = XBADFILE;
  26553. int idx = 0;
  26554. char targetOutput[16] = "www.wolfssl.com";
  26555. unsigned char* actual_output = NULL;
  26556. int len = 0;
  26557. ExpectNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  26558. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  26559. if (file != NULL)
  26560. fclose(file);
  26561. /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
  26562. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  26563. ExpectIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  26564. NID_commonName, -1)), 5);
  26565. ExpectNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  26566. ExpectNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  26567. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  26568. ExpectIntEQ(strncmp((const char*)actual_output, targetOutput, len), 0);
  26569. a = NULL;
  26570. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
  26571. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)), -1);
  26572. /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
  26573. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)), -1);
  26574. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
  26575. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)), -1);
  26576. wolfSSL_X509_free(x509);
  26577. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26578. ExpectNotNull(a = ASN1_STRING_new());
  26579. ExpectIntEQ(wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a), -1);
  26580. ASN1_STRING_free(a);
  26581. #endif
  26582. return EXPECT_RESULT();
  26583. }
  26584. static int test_wolfSSL_i2s_ASN1_STRING(void)
  26585. {
  26586. EXPECT_DECLS;
  26587. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  26588. WOLFSSL_ASN1_STRING* str = NULL;
  26589. const char* data = "test_wolfSSL_i2s_ASN1_STRING";
  26590. char* ret = NULL;
  26591. ExpectNotNull(str = ASN1_STRING_new());
  26592. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, NULL));
  26593. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26594. ret = NULL;
  26595. /* No data. */
  26596. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  26597. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26598. ret = NULL;
  26599. ExpectIntEQ(ASN1_STRING_set(str, data, 0), 1);
  26600. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  26601. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26602. ret = NULL;
  26603. ExpectIntEQ(ASN1_STRING_set(str, data, -1), 1);
  26604. /* No type. */
  26605. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  26606. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26607. ASN1_STRING_free(str);
  26608. #endif
  26609. return EXPECT_RESULT();
  26610. }
  26611. static int test_wolfSSL_ASN1_STRING_canon(void)
  26612. {
  26613. EXPECT_DECLS;
  26614. #if defined(WOLFSSL_TEST_STATIC_BUILD)
  26615. #if !defined(NO_CERTS) && (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
  26616. defined(OPENSSL_EXTRA_X509_SMALL))
  26617. WOLFSSL_ASN1_STRING* orig = NULL;
  26618. WOLFSSL_ASN1_STRING* canon = NULL;
  26619. const char* data = "test_wolfSSL_ASN1_STRING_canon";
  26620. const char* whitespaceOnly = "\t\r\n";
  26621. const char* modData = " \x01\f\t\x02\r\n\v\xff\nTt \n";
  26622. const char* canonData = "\x01 \x02 \xff tt";
  26623. const char longData[] =
  26624. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  26625. ExpectNotNull(orig = ASN1_STRING_new());
  26626. ExpectNotNull(canon = ASN1_STRING_new());
  26627. /* Invalid parameter testing. */
  26628. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, NULL), BAD_FUNC_ARG);
  26629. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, NULL), BAD_FUNC_ARG);
  26630. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, orig), BAD_FUNC_ARG);
  26631. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  26632. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  26633. ExpectIntEQ(ASN1_STRING_set(orig, longData, (int)XSTRLEN(data)), 1);
  26634. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  26635. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  26636. ExpectIntEQ(ASN1_STRING_set(orig, data, (int)XSTRLEN(data)), 1);
  26637. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  26638. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  26639. ASN1_STRING_free(orig);
  26640. orig = NULL;
  26641. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  26642. ExpectIntEQ(ASN1_STRING_set(orig, modData, 15), 1);
  26643. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  26644. ExpectIntEQ(ASN1_STRING_set(orig, canonData, 8), 1);
  26645. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  26646. ASN1_STRING_free(orig);
  26647. orig = NULL;
  26648. ExpectNotNull(orig = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  26649. ExpectIntEQ(ASN1_STRING_set(orig, whitespaceOnly, 3), 1);
  26650. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  26651. ASN1_STRING_free(orig);
  26652. orig = NULL;
  26653. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  26654. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  26655. ASN1_STRING_free(orig);
  26656. ASN1_STRING_free(canon);
  26657. #endif
  26658. #endif
  26659. return EXPECT_RESULT();
  26660. }
  26661. static int test_wolfSSL_ASN1_STRING_print(void)
  26662. {
  26663. EXPECT_DECLS;
  26664. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS) && \
  26665. !defined(NO_BIO)
  26666. ASN1_STRING* asnStr = NULL;
  26667. const char HELLO_DATA[]= \
  26668. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  26669. #define MAX_UNPRINTABLE_CHAR 32
  26670. #define MAX_BUF 255
  26671. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  26672. unsigned char expected[sizeof(unprintableData)+1];
  26673. unsigned char rbuf[MAX_BUF];
  26674. BIO *bio = NULL;
  26675. int p_len;
  26676. int i;
  26677. /* setup */
  26678. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  26679. unprintableData[i] = HELLO_DATA[i];
  26680. expected[i] = HELLO_DATA[i];
  26681. }
  26682. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  26683. unprintableData[sizeof(HELLO_DATA)+i] = i;
  26684. if (i == (int)'\n' || i == (int)'\r')
  26685. expected[sizeof(HELLO_DATA)+i] = i;
  26686. else
  26687. expected[sizeof(HELLO_DATA)+i] = '.';
  26688. }
  26689. unprintableData[sizeof(unprintableData)-1] = '\0';
  26690. expected[sizeof(expected)-1] = '\0';
  26691. XMEMSET(rbuf, 0, MAX_BUF);
  26692. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  26693. ExpectIntEQ(BIO_set_write_buf_size(bio, MAX_BUF), 0);
  26694. ExpectNotNull(asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  26695. ExpectIntEQ(ASN1_STRING_set(asnStr,(const void*)unprintableData,
  26696. (int)sizeof(unprintableData)), 1);
  26697. /* test */
  26698. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, NULL), 0);
  26699. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, NULL), 0);
  26700. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, asnStr), 0);
  26701. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print(bio, asnStr), 46);
  26702. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 46), 46);
  26703. ExpectStrEQ((char*)rbuf, (const char*)expected);
  26704. BIO_free(bio);
  26705. bio = NULL;
  26706. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  26707. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  26708. /* Ensure there is 0 bytes available to write into. */
  26709. ExpectIntEQ(BIO_write(bio, rbuf, 1), 1);
  26710. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  26711. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  26712. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  26713. ExpectIntEQ(BIO_set_write_buf_size(bio, 45), 1);
  26714. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  26715. BIO_free(bio);
  26716. ASN1_STRING_free(asnStr);
  26717. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS && !NO_BIO */
  26718. return EXPECT_RESULT();
  26719. }
  26720. static int test_wolfSSL_ASN1_STRING_print_ex(void)
  26721. {
  26722. EXPECT_DECLS;
  26723. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  26724. ASN1_STRING* asn_str = NULL;
  26725. const char data[] = "Hello wolfSSL!";
  26726. ASN1_STRING* esc_str = NULL;
  26727. const char esc_data[] = "a+;<>";
  26728. ASN1_STRING* neg_int = NULL;
  26729. const char neg_int_data[] = "\xff";
  26730. ASN1_STRING* neg_enum = NULL;
  26731. const char neg_enum_data[] = "\xff";
  26732. BIO *bio = NULL;
  26733. BIO *fixed = NULL;
  26734. unsigned long flags;
  26735. int p_len;
  26736. unsigned char rbuf[255];
  26737. /* setup */
  26738. XMEMSET(rbuf, 0, 255);
  26739. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  26740. ExpectIntEQ(BIO_set_write_buf_size(bio, 255), 0);
  26741. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  26742. ExpectNotNull(asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  26743. ExpectIntEQ(ASN1_STRING_set(asn_str, (const void*)data, sizeof(data)), 1);
  26744. ExpectNotNull(esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  26745. ExpectIntEQ(ASN1_STRING_set(esc_str, (const void*)esc_data,
  26746. sizeof(esc_data)), 1);
  26747. ExpectNotNull(neg_int = ASN1_STRING_type_new(V_ASN1_NEG_INTEGER));
  26748. ExpectIntEQ(ASN1_STRING_set(neg_int, (const void*)neg_int_data,
  26749. sizeof(neg_int_data) - 1), 1);
  26750. ExpectNotNull(neg_enum = ASN1_STRING_type_new(V_ASN1_NEG_ENUMERATED));
  26751. ExpectIntEQ(ASN1_STRING_set(neg_enum, (const void*)neg_enum_data,
  26752. sizeof(neg_enum_data) - 1), 1);
  26753. /* Invalid parameter testing. */
  26754. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, NULL, 0), 0);
  26755. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(bio, NULL, 0), 0);
  26756. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, asn_str, 0), 0);
  26757. /* no flags */
  26758. XMEMSET(rbuf, 0, 255);
  26759. flags = 0;
  26760. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 15);
  26761. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 15), 15);
  26762. ExpectStrEQ((char*)rbuf, "Hello wolfSSL!");
  26763. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26764. /* Ensure there is 0 bytes available to write into. */
  26765. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  26766. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26767. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26768. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26769. ExpectIntEQ(BIO_set_write_buf_size(fixed, 14), 1);
  26770. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26771. /* RFC2253 Escape */
  26772. XMEMSET(rbuf, 0, 255);
  26773. flags = ASN1_STRFLGS_ESC_2253;
  26774. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags), 9);
  26775. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 9), 9);
  26776. ExpectStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  26777. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26778. /* Ensure there is 0 bytes available to write into. */
  26779. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  26780. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  26781. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26782. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  26783. ExpectIntEQ(BIO_set_write_buf_size(fixed, 8), 1);
  26784. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  26785. /* Show type */
  26786. XMEMSET(rbuf, 0, 255);
  26787. flags = ASN1_STRFLGS_SHOW_TYPE;
  26788. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 28);
  26789. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 28), 28);
  26790. ExpectStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  26791. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26792. /* Ensure there is 0 bytes available to write into. */
  26793. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  26794. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26795. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26796. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26797. ExpectIntEQ(BIO_set_write_buf_size(fixed, 12), 1);
  26798. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26799. ExpectIntEQ(BIO_set_write_buf_size(fixed, 27), 1);
  26800. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26801. /* Dump All */
  26802. XMEMSET(rbuf, 0, 255);
  26803. flags = ASN1_STRFLGS_DUMP_ALL;
  26804. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 31);
  26805. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 31), 31);
  26806. ExpectStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  26807. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26808. /* Ensure there is 0 bytes available to write into. */
  26809. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  26810. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26811. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26812. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26813. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  26814. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26815. /* Dump Der */
  26816. XMEMSET(rbuf, 0, 255);
  26817. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  26818. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 35);
  26819. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 35), 35);
  26820. ExpectStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  26821. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26822. /* Ensure there is 0 bytes available to write into. */
  26823. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  26824. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26825. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26826. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26827. ExpectIntEQ(BIO_set_write_buf_size(fixed, 2), 1);
  26828. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26829. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  26830. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26831. /* Dump All + Show type */
  26832. XMEMSET(rbuf, 0, 255);
  26833. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  26834. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 44);
  26835. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 44), 44);
  26836. ExpectStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  26837. /* Dump All + Show type - Negative Integer. */
  26838. XMEMSET(rbuf, 0, 255);
  26839. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  26840. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_int, flags), 11);
  26841. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 11), 11);
  26842. ExpectStrEQ((char*)rbuf, "INTEGER:#FF");
  26843. /* Dump All + Show type - Negative Enumerated. */
  26844. XMEMSET(rbuf, 0, 255);
  26845. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  26846. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_enum, flags), 14);
  26847. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 14), 14);
  26848. ExpectStrEQ((char*)rbuf, "ENUMERATED:#FF");
  26849. BIO_free(fixed);
  26850. BIO_free(bio);
  26851. ASN1_STRING_free(asn_str);
  26852. ASN1_STRING_free(esc_str);
  26853. ASN1_STRING_free(neg_int);
  26854. ASN1_STRING_free(neg_enum);
  26855. ExpectStrEQ(wolfSSL_ASN1_tag2str(-1), "(unknown)");
  26856. ExpectStrEQ(wolfSSL_ASN1_tag2str(31), "(unknown)");
  26857. #endif
  26858. return EXPECT_RESULT();
  26859. }
  26860. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  26861. {
  26862. EXPECT_DECLS;
  26863. #if defined(OPENSSL_ALL) && !defined(NO_ASN)
  26864. ASN1_STRING* asn1str_test = NULL;
  26865. ASN1_STRING* asn1str_answer = NULL;
  26866. /* Each character is encoded using 4 bytes */
  26867. char input[] = {
  26868. 0, 0, 0, 'T',
  26869. 0, 0, 0, 'e',
  26870. 0, 0, 0, 's',
  26871. 0, 0, 0, 't',
  26872. };
  26873. char output[] = "Test";
  26874. char badInput[] = {
  26875. 1, 0, 0, 'T',
  26876. 0, 1, 0, 'e',
  26877. 0, 0, 1, 's',
  26878. };
  26879. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(NULL), 0);
  26880. /* Test wrong type. */
  26881. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  26882. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  26883. ASN1_STRING_free(asn1str_test);
  26884. asn1str_test = NULL;
  26885. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  26886. /* Test bad length. */
  26887. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input) - 1), 1);
  26888. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  26889. /* Test bad input. */
  26890. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 0, 4), 1);
  26891. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  26892. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 4, 4), 1);
  26893. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  26894. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 8, 4), 1);
  26895. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  26896. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  26897. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  26898. ExpectNotNull(
  26899. asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  26900. ExpectIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  26901. ExpectIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  26902. ASN1_STRING_free(asn1str_test);
  26903. ASN1_STRING_free(asn1str_answer);
  26904. #endif /* OPENSSL_ALL && !NO_ASN */
  26905. return EXPECT_RESULT();
  26906. }
  26907. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  26908. {
  26909. EXPECT_DECLS;
  26910. #if defined(OPENSSL_EXTRA)
  26911. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime = NULL;
  26912. unsigned char nullstr[32];
  26913. XMEMSET(nullstr, 0, 32);
  26914. ExpectNotNull(asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  26915. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL, DYNAMIC_TYPE_TMP_BUFFER));
  26916. if (asn1_gtime != NULL) {
  26917. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  26918. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  26919. ExpectIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  26920. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26921. }
  26922. wolfSSL_ASN1_GENERALIZEDTIME_free(NULL);
  26923. #endif /* OPENSSL_EXTRA */
  26924. return EXPECT_RESULT();
  26925. }
  26926. static int test_wolfSSL_ASN1_GENERALIZEDTIME_print(void)
  26927. {
  26928. EXPECT_DECLS;
  26929. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  26930. WOLFSSL_ASN1_GENERALIZEDTIME gtime;
  26931. BIO* bio = NULL;
  26932. unsigned char buf[24];
  26933. int i;
  26934. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  26935. BIO_set_write_buf_size(bio, 24);
  26936. XMEMSET(&gtime, 0, sizeof(WOLFSSL_ASN1_GENERALIZEDTIME));
  26937. XMEMCPY(gtime.data, "20180504123500Z", ASN_GENERALIZED_TIME_SIZE);
  26938. gtime.length = ASN_GENERALIZED_TIME_SIZE;
  26939. /* Type not set. */
  26940. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  26941. gtime.type = V_ASN1_GENERALIZEDTIME;
  26942. /* Invalid parameters testing. */
  26943. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, NULL), BAD_FUNC_ARG);
  26944. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, NULL), BAD_FUNC_ARG);
  26945. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, &gtime), BAD_FUNC_ARG);
  26946. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 1);
  26947. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 20);
  26948. ExpectIntEQ(XMEMCMP(buf, "May 04 12:35:00 2018", 20), 0);
  26949. BIO_free(bio);
  26950. bio = NULL;
  26951. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  26952. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  26953. /* Ensure there is 0 bytes available to write into. */
  26954. ExpectIntEQ(BIO_write(bio, buf, 1), 1);
  26955. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  26956. for (i = 1; i < 20; i++) {
  26957. ExpectIntEQ(BIO_set_write_buf_size(bio, i), 1);
  26958. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  26959. }
  26960. BIO_free(bio);
  26961. wolfSSL_ASN1_GENERALIZEDTIME_free(&gtime);
  26962. #endif /* OPENSSL_EXTRA */
  26963. return EXPECT_RESULT();
  26964. }
  26965. static int test_wolfSSL_ASN1_TIME(void)
  26966. {
  26967. EXPECT_DECLS;
  26968. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  26969. WOLFSSL_ASN1_TIME* asn_time = NULL;
  26970. unsigned char *data;
  26971. ExpectNotNull(asn_time = ASN1_TIME_new());
  26972. #ifndef NO_WOLFSSL_STUB
  26973. ExpectNotNull(ASN1_TIME_set(asn_time, 1));
  26974. #endif
  26975. ExpectIntEQ(ASN1_TIME_set_string(NULL, NULL), 0);
  26976. ExpectIntEQ(ASN1_TIME_set_string(asn_time, NULL), 0);
  26977. ExpectIntEQ(ASN1_TIME_set_string(NULL,
  26978. "String longer than CTC_DATA_SIZE that is 32 bytes"), 0);
  26979. ExpectIntEQ(ASN1_TIME_set_string(NULL, "101219181011Z"), 1);
  26980. ExpectIntEQ(ASN1_TIME_set_string(asn_time, "101219181011Z"), 1);
  26981. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(NULL), 0);
  26982. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(asn_time), ASN_UTC_TIME_SIZE - 1);
  26983. ExpectNull(wolfSSL_ASN1_TIME_get_data(NULL));
  26984. ExpectNotNull(data = wolfSSL_ASN1_TIME_get_data(asn_time));
  26985. ExpectIntEQ(XMEMCMP(data, "101219181011Z", 14), 0);
  26986. ExpectIntEQ(ASN1_TIME_check(NULL), 0);
  26987. ExpectIntEQ(ASN1_TIME_check(asn_time), 1);
  26988. ASN1_TIME_free(asn_time);
  26989. ASN1_TIME_free(NULL);
  26990. #endif
  26991. return EXPECT_RESULT();
  26992. }
  26993. static int test_wolfSSL_ASN1_TIME_to_string(void)
  26994. {
  26995. EXPECT_DECLS;
  26996. #ifndef NO_ASN_TIME
  26997. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  26998. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  26999. WOLFSSL_ASN1_TIME* t = NULL;
  27000. char buf[ASN_GENERALIZED_TIME_SIZE];
  27001. ExpectNotNull((t = ASN1_TIME_new()));
  27002. ExpectIntEQ(ASN1_TIME_set_string(t, "030222211515Z"), 1);
  27003. /* Invalid parameter testing. */
  27004. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 4));
  27005. ExpectNull(ASN1_TIME_to_string(t, NULL, 4));
  27006. ExpectNull(ASN1_TIME_to_string(NULL, buf, 4));
  27007. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 5));
  27008. ExpectNull(ASN1_TIME_to_string(NULL, buf, 5));
  27009. ExpectNull(ASN1_TIME_to_string(t, NULL, 5));
  27010. ExpectNull(ASN1_TIME_to_string(t, buf, 4));
  27011. /* Buffer needs to be longer than minimum of 5 characters. */
  27012. ExpectNull(ASN1_TIME_to_string(t, buf, 5));
  27013. ASN1_TIME_free(t);
  27014. #endif
  27015. #endif /* NO_ASN_TIME */
  27016. return EXPECT_RESULT();
  27017. }
  27018. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  27019. {
  27020. EXPECT_DECLS;
  27021. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  27022. ASN1_TIME* fromTime = NULL;
  27023. ASN1_TIME* closeToTime = NULL;
  27024. ASN1_TIME* toTime = NULL;
  27025. ASN1_TIME* invalidTime = NULL;
  27026. int daysDiff;
  27027. int secsDiff;
  27028. ExpectNotNull((fromTime = ASN1_TIME_new()));
  27029. /* Feb 22, 2003, 21:15:15 */
  27030. ExpectIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), 1);
  27031. ExpectNotNull((closeToTime = ASN1_TIME_new()));
  27032. /* Feb 22, 2003, 21:16:15 */
  27033. ExpectIntEQ(ASN1_TIME_set_string(closeToTime, "030222211615Z"), 1);
  27034. ExpectNotNull((toTime = ASN1_TIME_new()));
  27035. /* Dec 19, 2010, 18:10:11 */
  27036. ExpectIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), 1);
  27037. ExpectNotNull((invalidTime = ASN1_TIME_new()));
  27038. /* Dec 19, 2010, 18:10:11 but 'U' instead of 'Z' which is invalid. */
  27039. ExpectIntEQ(ASN1_TIME_set_string(invalidTime, "102519181011U"), 1);
  27040. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, invalidTime), 0);
  27041. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, invalidTime, toTime), 0);
  27042. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  27043. /* Error conditions. */
  27044. ExpectIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 0);
  27045. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime), 0);
  27046. /* If both times are NULL, difference is 0. */
  27047. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL), 1);
  27048. ExpectIntEQ(daysDiff, 0);
  27049. ExpectIntEQ(secsDiff, 0);
  27050. /* If one time is NULL, it defaults to the current time. */
  27051. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime), 1);
  27052. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL), 1);
  27053. /* Normal operation. Both times non-NULL. */
  27054. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  27055. ExpectIntEQ(daysDiff, 2856);
  27056. ExpectIntEQ(secsDiff, 75296);
  27057. /* Swapping the times should return negative values. */
  27058. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime), 1);
  27059. ExpectIntEQ(daysDiff, -2856);
  27060. ExpectIntEQ(secsDiff, -75296);
  27061. /* Compare with invalid time string. */
  27062. ExpectIntEQ(ASN1_TIME_compare(fromTime, invalidTime), -2);
  27063. ExpectIntEQ(ASN1_TIME_compare(invalidTime, toTime), -2);
  27064. /* Compare with days difference of 0. */
  27065. ExpectIntEQ(ASN1_TIME_compare(fromTime, closeToTime), -1);
  27066. ExpectIntEQ(ASN1_TIME_compare(closeToTime, fromTime), 1);
  27067. /* Days and seconds differences not 0. */
  27068. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  27069. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  27070. /* Same time. */
  27071. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  27072. /* Compare regression test: No seconds difference, just difference in days.
  27073. */
  27074. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  27075. ASN1_TIME_set_string(toTime, "19800101000000Z");
  27076. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  27077. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  27078. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  27079. /* Edge case with Unix epoch. */
  27080. ExpectNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  27081. ExpectNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  27082. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  27083. ExpectIntEQ(daysDiff, 3652);
  27084. ExpectIntEQ(secsDiff, 0);
  27085. /* Edge case with year > 2038 (year 2038 problem). */
  27086. ExpectNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  27087. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  27088. ExpectIntEQ(daysDiff, 2932896);
  27089. ExpectIntEQ(secsDiff, 86399);
  27090. ASN1_TIME_free(fromTime);
  27091. ASN1_TIME_free(closeToTime);
  27092. ASN1_TIME_free(toTime);
  27093. ASN1_TIME_free(invalidTime);
  27094. #endif
  27095. return EXPECT_RESULT();
  27096. }
  27097. static int test_wolfSSL_ASN1_TIME_adj(void)
  27098. {
  27099. EXPECT_DECLS;
  27100. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  27101. !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  27102. const int year = 365*24*60*60;
  27103. const int day = 24*60*60;
  27104. const int hour = 60*60;
  27105. const int mini = 60;
  27106. const byte asn_utc_time = ASN_UTC_TIME;
  27107. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  27108. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  27109. #endif
  27110. WOLFSSL_ASN1_TIME* asn_time = NULL;
  27111. WOLFSSL_ASN1_TIME* s = NULL;
  27112. int offset_day;
  27113. long offset_sec;
  27114. char date_str[CTC_DATE_SIZE + 1];
  27115. time_t t;
  27116. ExpectNotNull(s = wolfSSL_ASN1_TIME_new());
  27117. /* UTC notation test */
  27118. /* 2000/2/15 20:30:00 */
  27119. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  27120. offset_day = 7;
  27121. offset_sec = 45 * mini;
  27122. /* offset_sec = -45 * min;*/
  27123. ExpectNotNull(asn_time =
  27124. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  27125. ExpectTrue(asn_time->type == asn_utc_time);
  27126. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  27127. CTC_DATE_SIZE));
  27128. date_str[CTC_DATE_SIZE] = '\0';
  27129. ExpectIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  27130. /* negative offset */
  27131. offset_sec = -45 * mini;
  27132. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  27133. ExpectNotNull(asn_time);
  27134. ExpectTrue(asn_time->type == asn_utc_time);
  27135. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  27136. CTC_DATE_SIZE));
  27137. date_str[CTC_DATE_SIZE] = '\0';
  27138. ExpectIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  27139. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  27140. s = NULL;
  27141. XMEMSET(date_str, 0, sizeof(date_str));
  27142. /* Generalized time will overflow time_t if not long */
  27143. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  27144. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  27145. DYNAMIC_TYPE_OPENSSL);
  27146. /* GeneralizedTime notation test */
  27147. /* 2055/03/01 09:00:00 */
  27148. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  27149. offset_day = 12;
  27150. offset_sec = 10 * mini;
  27151. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  27152. offset_sec));
  27153. ExpectTrue(asn_time->type == asn_gen_time);
  27154. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  27155. CTC_DATE_SIZE));
  27156. date_str[CTC_DATE_SIZE] = '\0';
  27157. ExpectIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  27158. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  27159. s = NULL;
  27160. XMEMSET(date_str, 0, sizeof(date_str));
  27161. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  27162. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  27163. s = NULL;
  27164. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  27165. offset_day = 7;
  27166. offset_sec = 45 * mini;
  27167. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  27168. offset_sec));
  27169. ExpectTrue(asn_time->type == asn_utc_time);
  27170. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  27171. CTC_DATE_SIZE));
  27172. date_str[CTC_DATE_SIZE] = '\0';
  27173. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  27174. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  27175. asn_time = NULL;
  27176. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day,
  27177. offset_sec));
  27178. ExpectTrue(asn_time->type == asn_utc_time);
  27179. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  27180. CTC_DATE_SIZE));
  27181. date_str[CTC_DATE_SIZE] = '\0';
  27182. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  27183. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  27184. #endif
  27185. return EXPECT_RESULT();
  27186. }
  27187. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  27188. {
  27189. EXPECT_DECLS;
  27190. #if (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  27191. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  27192. defined(OPENSSL_ALL)) && !defined(NO_ASN_TIME)
  27193. ASN1_TIME asnTime;
  27194. struct tm tm;
  27195. time_t testTime = 1683926567; /* Fri May 12 09:22:47 PM UTC 2023 */
  27196. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  27197. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  27198. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, NULL), 1);
  27199. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  27200. ExpectIntEQ(tm.tm_sec, 15);
  27201. ExpectIntEQ(tm.tm_min, 15);
  27202. ExpectIntEQ(tm.tm_hour, 21);
  27203. ExpectIntEQ(tm.tm_mday, 22);
  27204. ExpectIntEQ(tm.tm_mon, 1);
  27205. ExpectIntEQ(tm.tm_year, 100);
  27206. ExpectIntEQ(tm.tm_isdst, 0);
  27207. #ifdef XMKTIME
  27208. ExpectIntEQ(tm.tm_wday, 2);
  27209. ExpectIntEQ(tm.tm_yday, 52);
  27210. #endif
  27211. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "500222211515Z"), 1);
  27212. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  27213. ExpectIntEQ(tm.tm_year, 50);
  27214. /* Get current time. */
  27215. ExpectIntEQ(ASN1_TIME_to_tm(NULL, NULL), 0);
  27216. ExpectIntEQ(ASN1_TIME_to_tm(NULL, &tm), 1);
  27217. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  27218. /* 0 length. */
  27219. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27220. /* No type. */
  27221. asnTime.length = 1;
  27222. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27223. /* Not UTCTIME length. */
  27224. asnTime.type = V_ASN1_UTCTIME;
  27225. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27226. /* Not GENERALIZEDTIME length. */
  27227. asnTime.type = V_ASN1_GENERALIZEDTIME;
  27228. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27229. /* Not Zulu timezone. */
  27230. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515U"), 1);
  27231. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27232. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "20000222211515U"), 1);
  27233. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27234. #ifdef XMKTIME
  27235. ExpectNotNull(ASN1_TIME_adj(&asnTime, testTime, 0, 0));
  27236. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  27237. ExpectIntEQ(tm.tm_sec, 47);
  27238. ExpectIntEQ(tm.tm_min, 22);
  27239. ExpectIntEQ(tm.tm_hour, 21);
  27240. ExpectIntEQ(tm.tm_mday, 12);
  27241. ExpectIntEQ(tm.tm_mon, 4);
  27242. ExpectIntEQ(tm.tm_year, 123);
  27243. ExpectIntEQ(tm.tm_wday, 5);
  27244. ExpectIntEQ(tm.tm_yday, 131);
  27245. /* Confirm that when used with a tm struct from ASN1_TIME_adj, all other
  27246. fields are zeroed out as expected. */
  27247. ExpectIntEQ(tm.tm_isdst, 0);
  27248. #endif
  27249. #endif
  27250. return EXPECT_RESULT();
  27251. }
  27252. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
  27253. {
  27254. EXPECT_DECLS;
  27255. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  27256. WOLFSSL_ASN1_TIME *t = NULL;
  27257. WOLFSSL_ASN1_TIME *out = NULL;
  27258. WOLFSSL_ASN1_TIME *gtime = NULL;
  27259. int tlen = 0;
  27260. unsigned char *data = NULL;
  27261. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  27262. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(NULL, &out));
  27263. /* type not set. */
  27264. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  27265. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27266. t = NULL;
  27267. /* UTC Time test */
  27268. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  27269. if (t != NULL) {
  27270. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  27271. t->type = ASN_UTC_TIME;
  27272. t->length = ASN_UTC_TIME_SIZE;
  27273. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  27274. }
  27275. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t), ASN_UTC_TIME_SIZE);
  27276. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)), "050727123456Z");
  27277. out = NULL;
  27278. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  27279. wolfSSL_ASN1_TIME_free(gtime);
  27280. gtime = NULL;
  27281. ExpectNotNull(out = wolfSSL_ASN1_TIME_new());
  27282. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  27283. ExpectPtrEq(gtime, out);
  27284. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  27285. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  27286. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  27287. /* Generalized Time test */
  27288. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  27289. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  27290. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  27291. if (t != NULL) {
  27292. t->type = ASN_GENERALIZED_TIME;
  27293. t->length = ASN_GENERALIZED_TIME_SIZE;
  27294. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  27295. }
  27296. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t),
  27297. ASN_GENERALIZED_TIME_SIZE);
  27298. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)),
  27299. "20050727123456Z");
  27300. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  27301. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  27302. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  27303. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  27304. /* UTC Time to Generalized Time 1900's test */
  27305. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  27306. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  27307. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  27308. if (t != NULL) {
  27309. t->type = ASN_UTC_TIME;
  27310. t->length = ASN_UTC_TIME_SIZE;
  27311. XMEMCPY(t->data, "500727123456Z", ASN_UTC_TIME_SIZE);
  27312. }
  27313. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  27314. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  27315. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  27316. ExpectStrEQ((char*)gtime->data, "19500727123456Z");
  27317. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27318. /* Null parameter test */
  27319. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  27320. gtime = NULL;
  27321. out = NULL;
  27322. if (t != NULL) {
  27323. t->type = ASN_UTC_TIME;
  27324. t->length = ASN_UTC_TIME_SIZE;
  27325. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  27326. }
  27327. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  27328. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  27329. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  27330. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  27331. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27332. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27333. #endif
  27334. return EXPECT_RESULT();
  27335. }
  27336. static int test_wolfSSL_ASN1_TIME_print(void)
  27337. {
  27338. EXPECT_DECLS;
  27339. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_BIO) && \
  27340. (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  27341. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  27342. defined(OPENSSL_ALL)) && defined(USE_CERT_BUFFERS_2048) && \
  27343. !defined(NO_ASN_TIME)
  27344. BIO* bio = NULL;
  27345. BIO* fixed = NULL;
  27346. X509* x509 = NULL;
  27347. const unsigned char* der = client_cert_der_2048;
  27348. ASN1_TIME* notAfter;
  27349. ASN1_TIME* notBefore;
  27350. unsigned char buf[25];
  27351. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  27352. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  27353. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  27354. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  27355. ExpectNotNull(notBefore = X509_get_notBefore(x509));
  27356. ExpectIntEQ(ASN1_TIME_print(NULL, NULL), 0);
  27357. ExpectIntEQ(ASN1_TIME_print(bio, NULL), 0);
  27358. ExpectIntEQ(ASN1_TIME_print(NULL, notBefore), 0);
  27359. ExpectIntEQ(ASN1_TIME_print(bio, notBefore), 1);
  27360. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  27361. ExpectIntEQ(XMEMCMP(buf, "Dec 16 21:17:49 2022 GMT", sizeof(buf) - 1), 0);
  27362. /* Test BIO_write fails. */
  27363. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27364. /* Ensure there is 0 bytes available to write into. */
  27365. ExpectIntEQ(BIO_write(fixed, buf, 1), 1);
  27366. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  27367. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27368. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  27369. ExpectIntEQ(BIO_set_write_buf_size(fixed, 23), 1);
  27370. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  27371. /* create a bad time and test results */
  27372. ExpectNotNull(notAfter = X509_get_notAfter(x509));
  27373. ExpectIntEQ(ASN1_TIME_check(notAfter), 1);
  27374. if (EXPECT_SUCCESS()) {
  27375. notAfter->data[8] = 0;
  27376. notAfter->data[3] = 0;
  27377. }
  27378. ExpectIntNE(ASN1_TIME_print(bio, notAfter), 1);
  27379. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  27380. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  27381. ExpectIntEQ(ASN1_TIME_check(notAfter), 0);
  27382. BIO_free(bio);
  27383. BIO_free(fixed);
  27384. X509_free(x509);
  27385. #endif
  27386. return EXPECT_RESULT();
  27387. }
  27388. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  27389. {
  27390. EXPECT_DECLS;
  27391. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  27392. BIO* bio = NULL;
  27393. ASN1_UTCTIME* utc = NULL;
  27394. unsigned char buf[25];
  27395. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  27396. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  27397. const char* genDate = "20190424111501Z"; /* GEN = YYYYMMDDHHMMSSZ */
  27398. /* Valid date */
  27399. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  27400. ExpectNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  27401. DYNAMIC_TYPE_ASN1));
  27402. if (utc != NULL) {
  27403. utc->type = ASN_UTC_TIME;
  27404. utc->length = ASN_UTC_TIME_SIZE;
  27405. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  27406. }
  27407. ExpectIntEQ(ASN1_UTCTIME_print(NULL, NULL), 0);
  27408. ExpectIntEQ(ASN1_UTCTIME_print(bio, NULL), 0);
  27409. ExpectIntEQ(ASN1_UTCTIME_print(NULL, utc), 0);
  27410. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  27411. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  27412. ExpectIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  27413. XMEMSET(buf, 0, sizeof(buf));
  27414. BIO_free(bio);
  27415. bio = NULL;
  27416. /* Invalid format */
  27417. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  27418. if (utc != NULL) {
  27419. utc->type = ASN_UTC_TIME;
  27420. utc->length = ASN_UTC_TIME_SIZE;
  27421. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  27422. }
  27423. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  27424. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  27425. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  27426. /* Invalid type */
  27427. if (utc != NULL) {
  27428. utc->type = ASN_GENERALIZED_TIME;
  27429. utc->length = ASN_GENERALIZED_TIME_SIZE;
  27430. XMEMCPY(utc->data, (byte*)genDate, ASN_GENERALIZED_TIME_SIZE);
  27431. }
  27432. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  27433. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  27434. BIO_free(bio);
  27435. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  27436. return EXPECT_RESULT();
  27437. }
  27438. static int test_wolfSSL_ASN1_TYPE(void)
  27439. {
  27440. EXPECT_DECLS;
  27441. #if defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD) || \
  27442. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  27443. WOLFSSL_ASN1_TYPE* t = NULL;
  27444. WOLFSSL_ASN1_OBJECT* obj = NULL;
  27445. #ifndef NO_ASN_TIME
  27446. WOLFSSL_ASN1_TIME* time = NULL;
  27447. #endif
  27448. WOLFSSL_ASN1_STRING* str = NULL;
  27449. unsigned char data[] = { 0x00 };
  27450. ASN1_TYPE_set(NULL, V_ASN1_NULL, NULL);
  27451. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27452. ASN1_TYPE_set(t, V_ASN1_EOC, NULL);
  27453. wolfSSL_ASN1_TYPE_free(t);
  27454. t = NULL;
  27455. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27456. ASN1_TYPE_set(t, V_ASN1_NULL, NULL);
  27457. ASN1_TYPE_set(t, V_ASN1_NULL, data);
  27458. wolfSSL_ASN1_TYPE_free(t);
  27459. t = NULL;
  27460. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27461. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  27462. ASN1_TYPE_set(t, V_ASN1_OBJECT, obj);
  27463. wolfSSL_ASN1_TYPE_free(t);
  27464. t = NULL;
  27465. #ifndef NO_ASN_TIME
  27466. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27467. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  27468. ASN1_TYPE_set(t, V_ASN1_UTCTIME, time);
  27469. wolfSSL_ASN1_TYPE_free(t);
  27470. t = NULL;
  27471. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27472. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  27473. ASN1_TYPE_set(t, V_ASN1_GENERALIZEDTIME, time);
  27474. wolfSSL_ASN1_TYPE_free(t);
  27475. t = NULL;
  27476. #endif
  27477. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27478. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  27479. ASN1_TYPE_set(t, V_ASN1_UTF8STRING, str);
  27480. wolfSSL_ASN1_TYPE_free(t);
  27481. t = NULL;
  27482. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27483. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  27484. ASN1_TYPE_set(t, V_ASN1_PRINTABLESTRING, str);
  27485. wolfSSL_ASN1_TYPE_free(t);
  27486. t = NULL;
  27487. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27488. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  27489. ASN1_TYPE_set(t, V_ASN1_T61STRING, str);
  27490. wolfSSL_ASN1_TYPE_free(t);
  27491. t = NULL;
  27492. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27493. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  27494. ASN1_TYPE_set(t, V_ASN1_IA5STRING, str);
  27495. wolfSSL_ASN1_TYPE_free(t);
  27496. t = NULL;
  27497. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27498. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  27499. ASN1_TYPE_set(t, V_ASN1_UNIVERSALSTRING, str);
  27500. wolfSSL_ASN1_TYPE_free(t);
  27501. t = NULL;
  27502. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27503. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  27504. ASN1_TYPE_set(t, V_ASN1_SEQUENCE, str);
  27505. wolfSSL_ASN1_TYPE_free(t);
  27506. t = NULL;
  27507. #endif
  27508. return EXPECT_RESULT();
  27509. }
  27510. /* Testing code used in dpp.c in hostap */
  27511. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  27512. typedef struct {
  27513. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  27514. * as an OID identifying the curve */
  27515. X509_ALGOR *alg;
  27516. /* Compressed format public key per ANSI X9.63 */
  27517. ASN1_BIT_STRING *pub_key;
  27518. } DPP_BOOTSTRAPPING_KEY;
  27519. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  27520. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  27521. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  27522. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  27523. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  27524. typedef struct {
  27525. ASN1_INTEGER *integer;
  27526. } TEST_ASN1;
  27527. ASN1_SEQUENCE(TEST_ASN1) = {
  27528. ASN1_SIMPLE(TEST_ASN1, integer, ASN1_INTEGER),
  27529. } ASN1_SEQUENCE_END(TEST_ASN1)
  27530. IMPLEMENT_ASN1_FUNCTIONS(TEST_ASN1)
  27531. typedef struct {
  27532. ASN1_OCTET_STRING *octet_string;
  27533. } TEST_FAIL_ASN1;
  27534. #define WOLFSSL_ASN1_OCTET_STRING_ASN1 4
  27535. ASN1_SEQUENCE(TEST_FAIL_ASN1) = {
  27536. ASN1_SIMPLE(TEST_FAIL_ASN1, octet_string, ASN1_OCTET_STRING),
  27537. } ASN1_SEQUENCE_END(TEST_FAIL_ASN1)
  27538. IMPLEMENT_ASN1_FUNCTIONS(TEST_FAIL_ASN1)
  27539. #endif
  27540. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  27541. {
  27542. EXPECT_DECLS;
  27543. /* Testing code used in dpp.c in hostap */
  27544. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  27545. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  27546. EC_KEY *eckey = NULL;
  27547. EVP_PKEY *key = NULL;
  27548. size_t len;
  27549. unsigned char *der = NULL;
  27550. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  27551. const unsigned char *in = ecc_clikey_der_256;
  27552. WOLFSSL_ASN1_OBJECT* ec_obj = NULL;
  27553. WOLFSSL_ASN1_OBJECT* group_obj = NULL;
  27554. const EC_GROUP *group = NULL;
  27555. const EC_POINT *point = NULL;
  27556. int nid;
  27557. TEST_ASN1 *test_asn1 = NULL;
  27558. TEST_FAIL_ASN1 test_fail_asn1;
  27559. const unsigned char badObjDer[] = { 0x06, 0x00 };
  27560. const unsigned char goodObjDer[] = {
  27561. 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
  27562. };
  27563. WOLFSSL_ASN1_ITEM emptyTemplate;
  27564. XMEMSET(&emptyTemplate, 0, sizeof(WOLFSSL_ASN1_ITEM));
  27565. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  27566. der = NULL;
  27567. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(NULL, &der), 0);
  27568. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, NULL), 0);
  27569. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  27570. ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  27571. (long)sizeof_ecc_clikey_der_256));
  27572. ExpectNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  27573. ExpectNotNull(group = EC_KEY_get0_group(eckey));
  27574. ExpectNotNull(point = EC_KEY_get0_public_key(eckey));
  27575. nid = EC_GROUP_get_curve_name(group);
  27576. ec_obj = OBJ_nid2obj(EVP_PKEY_EC);
  27577. group_obj = OBJ_nid2obj(nid);
  27578. if ((ec_obj != NULL) && (group_obj != NULL)) {
  27579. ExpectIntEQ(X509_ALGOR_set0(bootstrap->alg, ec_obj, V_ASN1_OBJECT,
  27580. group_obj), 1);
  27581. if (EXPECT_SUCCESS()) {
  27582. ec_obj = NULL;
  27583. group_obj = NULL;
  27584. }
  27585. }
  27586. wolfSSL_ASN1_OBJECT_free(group_obj);
  27587. wolfSSL_ASN1_OBJECT_free(ec_obj);
  27588. ExpectIntEQ(EC_POINT_point2oct(group, point, 0, NULL, 0, NULL), 0);
  27589. #ifdef HAVE_COMP_KEY
  27590. ExpectIntGT((len = EC_POINT_point2oct(
  27591. group, point, POINT_CONVERSION_COMPRESSED,
  27592. NULL, 0, NULL)), 0);
  27593. #else
  27594. ExpectIntGT((len = EC_POINT_point2oct(
  27595. group, point, POINT_CONVERSION_UNCOMPRESSED,
  27596. NULL, 0, NULL)), 0);
  27597. #endif
  27598. ExpectNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  27599. #ifdef HAVE_COMP_KEY
  27600. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  27601. der, len-1, NULL), 0);
  27602. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  27603. der, len, NULL), len);
  27604. #else
  27605. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  27606. der, len-1, NULL), 0);
  27607. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  27608. der, len, NULL), len);
  27609. #endif
  27610. if (EXPECT_SUCCESS()) {
  27611. bootstrap->pub_key->data = der;
  27612. bootstrap->pub_key->length = (int)len;
  27613. /* Not actually used */
  27614. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  27615. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  27616. }
  27617. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, NULL), 0);
  27618. der = NULL;
  27619. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  27620. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  27621. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  27622. EVP_PKEY_free(key);
  27623. EC_KEY_free(eckey);
  27624. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  27625. bootstrap = NULL;
  27626. DPP_BOOTSTRAPPING_KEY_free(NULL);
  27627. /* Create bootstrap key with bad OBJECT_ID DER data, parameter that is
  27628. * a NULL and an empty BIT_STRING. */
  27629. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  27630. ExpectNotNull(bootstrap->alg->algorithm = wolfSSL_ASN1_OBJECT_new());
  27631. if (EXPECT_SUCCESS()) {
  27632. bootstrap->alg->algorithm->obj = badObjDer;
  27633. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(badObjDer);
  27634. }
  27635. ExpectNotNull(bootstrap->alg->parameter = wolfSSL_ASN1_TYPE_new());
  27636. if (EXPECT_SUCCESS()) {
  27637. bootstrap->alg->parameter->type = V_ASN1_NULL;
  27638. bootstrap->alg->parameter->value.ptr = NULL;
  27639. bootstrap->pub_key->data = NULL;
  27640. bootstrap->pub_key->length = 0;
  27641. /* Not actually used */
  27642. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  27643. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  27644. }
  27645. /* Encode with bad OBJECT_ID. */
  27646. der = NULL;
  27647. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  27648. /* Fix OBJECT_ID and encode with empty BIT_STRING. */
  27649. if (EXPECT_SUCCESS()) {
  27650. bootstrap->alg->algorithm->obj = goodObjDer;
  27651. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(goodObjDer);
  27652. bootstrap->alg->algorithm->grp = 2;
  27653. }
  27654. der = NULL;
  27655. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 16);
  27656. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, &emptyTemplate), 0);
  27657. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  27658. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  27659. /* Test integer */
  27660. ExpectNotNull(test_asn1 = TEST_ASN1_new());
  27661. der = NULL;
  27662. ExpectIntEQ(ASN1_INTEGER_set(test_asn1->integer, 100), 1);
  27663. ExpectIntEQ(i2d_TEST_ASN1(test_asn1, &der), 5);
  27664. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  27665. TEST_ASN1_free(test_asn1);
  27666. /* Test integer cases. */
  27667. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  27668. TEST_ASN1_free(NULL);
  27669. /* Test error cases. */
  27670. ExpectNull(TEST_FAIL_ASN1_new());
  27671. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  27672. TEST_FAIL_ASN1_free(NULL);
  27673. XMEMSET(&test_fail_asn1, 0, sizeof(TEST_FAIL_ASN1));
  27674. ExpectIntEQ(i2d_TEST_FAIL_ASN1(&test_fail_asn1, &der), 0);
  27675. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  27676. #endif /* OPENSSL_ALL && HAVE_ECC && USE_CERT_BUFFERS_256 */
  27677. return EXPECT_RESULT();
  27678. }
  27679. static int test_wolfSSL_lhash(void)
  27680. {
  27681. EXPECT_DECLS;
  27682. #ifdef OPENSSL_ALL
  27683. const char testStr[] = "Like a true nature's child\n"
  27684. "We were born\n"
  27685. "Born to be wild";
  27686. #ifdef NO_SHA
  27687. ExpectIntEQ(lh_strhash(testStr), 0xf9dc8a43);
  27688. #else
  27689. ExpectIntEQ(lh_strhash(testStr), 0x5b7541dc);
  27690. #endif
  27691. #endif
  27692. return EXPECT_RESULT();
  27693. }
  27694. static int test_wolfSSL_X509_NAME(void)
  27695. {
  27696. EXPECT_DECLS;
  27697. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  27698. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  27699. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  27700. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  27701. defined(OPENSSL_EXTRA))
  27702. X509* x509 = NULL;
  27703. const unsigned char* c;
  27704. unsigned char buf[4096];
  27705. int bytes;
  27706. XFILE f = XBADFILE;
  27707. const X509_NAME* a = NULL;
  27708. const X509_NAME* b = NULL;
  27709. X509_NAME* d2i_name = NULL;
  27710. int sz = 0;
  27711. unsigned char* tmp;
  27712. char file[] = "./certs/ca-cert.der";
  27713. #ifndef OPENSSL_EXTRA_X509_SMALL
  27714. byte empty[] = { /* CN=empty emailAddress= */
  27715. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  27716. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  27717. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  27718. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  27719. 0x01, 0x16, 0x00
  27720. };
  27721. #endif
  27722. #ifndef OPENSSL_EXTRA_X509_SMALL
  27723. /* test compile of deprecated function, returns 0 */
  27724. ExpectIntEQ(CRYPTO_thread_id(), 0);
  27725. #endif
  27726. ExpectNotNull(a = X509_NAME_new());
  27727. X509_NAME_free((X509_NAME*)a);
  27728. a = NULL;
  27729. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  27730. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  27731. if (f != XBADFILE)
  27732. XFCLOSE(f);
  27733. c = buf;
  27734. ExpectNotNull(x509 = wolfSSL_X509_d2i(NULL, c, bytes));
  27735. /* test cmp function */
  27736. ExpectNotNull(a = X509_get_issuer_name(x509));
  27737. ExpectNotNull(b = X509_get_subject_name(x509));
  27738. #ifndef OPENSSL_EXTRA_X509_SMALL
  27739. ExpectIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  27740. #endif
  27741. tmp = buf;
  27742. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  27743. if (sz > 0 && tmp == buf) {
  27744. fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
  27745. __LINE__);
  27746. fprintf(stderr, " Expected pointer to be incremented\n");
  27747. abort();
  27748. }
  27749. #ifndef OPENSSL_EXTRA_X509_SMALL
  27750. tmp = buf;
  27751. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  27752. #endif
  27753. /* if output parameter is NULL, should still return required size. */
  27754. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  27755. /* retry but with the function creating a buffer */
  27756. tmp = NULL;
  27757. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  27758. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  27759. tmp = NULL;
  27760. #ifdef WOLFSSL_CERT_NAME_ALL
  27761. /* test for givenName and name */
  27762. {
  27763. WOLFSSL_X509_NAME_ENTRY* entry = NULL;
  27764. const byte gName[] = "test-given-name";
  27765. const byte name[] = "test-name";
  27766. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  27767. NID_givenName, ASN_UTF8STRING, gName, sizeof(gName)));
  27768. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  27769. 1);
  27770. wolfSSL_X509_NAME_ENTRY_free(entry);
  27771. entry = NULL;
  27772. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  27773. NID_name, ASN_UTF8STRING, name, sizeof(name)));
  27774. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  27775. 1);
  27776. wolfSSL_X509_NAME_ENTRY_free(entry);
  27777. tmp = NULL;
  27778. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  27779. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  27780. }
  27781. #endif
  27782. b = NULL;
  27783. ExpectNotNull(b = X509_NAME_dup((X509_NAME*)a));
  27784. #ifndef OPENSSL_EXTRA_X509_SMALL
  27785. ExpectIntEQ(X509_NAME_cmp(a, b), 0);
  27786. #endif
  27787. X509_NAME_free((X509_NAME*)b);
  27788. X509_NAME_free(d2i_name);
  27789. d2i_name = NULL;
  27790. X509_free(x509);
  27791. #ifndef OPENSSL_EXTRA_X509_SMALL
  27792. /* test with an empty domain component */
  27793. tmp = empty;
  27794. sz = sizeof(empty);
  27795. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  27796. ExpectIntEQ(X509_NAME_entry_count(d2i_name), 2);
  27797. /* size of empty emailAddress will be 0 */
  27798. tmp = buf;
  27799. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  27800. (char*)tmp, sizeof(buf)), 0);
  27801. /* should contain no organization name */
  27802. tmp = buf;
  27803. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  27804. (char*)tmp, sizeof(buf)), -1);
  27805. X509_NAME_free(d2i_name);
  27806. #endif
  27807. #endif
  27808. return EXPECT_RESULT();
  27809. }
  27810. static int test_wolfSSL_X509_NAME_hash(void)
  27811. {
  27812. EXPECT_DECLS;
  27813. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  27814. !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  27815. BIO* bio = NULL;
  27816. X509* x509 = NULL;
  27817. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  27818. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  27819. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  27820. ExpectIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  27821. ExpectIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  27822. X509_free(x509);
  27823. BIO_free(bio);
  27824. #endif
  27825. return EXPECT_RESULT();
  27826. }
  27827. static int test_wolfSSL_X509_NAME_print_ex(void)
  27828. {
  27829. EXPECT_DECLS;
  27830. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  27831. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  27832. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  27833. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  27834. !defined(NO_BIO) && !defined(NO_RSA)
  27835. int memSz;
  27836. byte* mem = NULL;
  27837. BIO* bio = NULL;
  27838. BIO* membio = NULL;
  27839. X509* x509 = NULL;
  27840. X509_NAME* name = NULL;
  27841. const char* expNormal = "C=US, CN=wolfssl.com";
  27842. const char* expReverse = "CN=wolfssl.com, C=US";
  27843. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  27844. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  27845. const char* expRFC5523 =
  27846. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  27847. /* Test with real cert (svrCertFile) first */
  27848. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  27849. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  27850. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  27851. ExpectNotNull(name = X509_get_subject_name(x509));
  27852. /* Test without flags */
  27853. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27854. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  27855. BIO_free(membio);
  27856. membio = NULL;
  27857. /* Test flag: XN_FLAG_RFC2253 */
  27858. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27859. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  27860. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  27861. BIO_free(membio);
  27862. membio = NULL;
  27863. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  27864. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27865. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  27866. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  27867. BIO_free(membio);
  27868. membio = NULL;
  27869. X509_free(x509);
  27870. BIO_free(bio);
  27871. name = NULL;
  27872. /* Test normal case without escaped characters */
  27873. {
  27874. /* Create name: "/C=US/CN=wolfssl.com" */
  27875. ExpectNotNull(name = X509_NAME_new());
  27876. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  27877. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  27878. WOLFSSL_SUCCESS);
  27879. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  27880. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  27881. WOLFSSL_SUCCESS);
  27882. /* Test without flags */
  27883. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27884. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  27885. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  27886. ExpectIntEQ(memSz, XSTRLEN(expNormal));
  27887. ExpectIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  27888. BIO_free(membio);
  27889. membio = NULL;
  27890. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  27891. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27892. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  27893. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  27894. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  27895. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  27896. BIO_free(membio);
  27897. membio = NULL;
  27898. /* Test flags: XN_FLAG_DN_REV - reversed */
  27899. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27900. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  27901. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  27902. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  27903. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  27904. ExpectIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  27905. BIO_free(membio);
  27906. membio = NULL;
  27907. X509_NAME_free(name);
  27908. name = NULL;
  27909. }
  27910. /* Test RFC2253 characters are escaped with backslashes */
  27911. {
  27912. ExpectNotNull(name = X509_NAME_new());
  27913. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  27914. /* space at beginning and end, and: ,+"\ */
  27915. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  27916. WOLFSSL_SUCCESS);
  27917. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  27918. /* # at beginning, and: <>;*/
  27919. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  27920. WOLFSSL_SUCCESS);
  27921. /* Test without flags */
  27922. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27923. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  27924. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  27925. ExpectIntEQ(memSz, XSTRLEN(expNotEscaped));
  27926. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  27927. XSTRLEN(expNotEscaped)), 0);
  27928. BIO_free(membio);
  27929. membio = NULL;
  27930. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  27931. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27932. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  27933. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  27934. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  27935. ExpectIntEQ(memSz, XSTRLEN(expRFC5523));
  27936. ExpectIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  27937. BIO_free(membio);
  27938. membio = NULL;
  27939. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  27940. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27941. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  27942. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  27943. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  27944. ExpectIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  27945. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  27946. XSTRLEN(expNotEscapedRev)), 0);
  27947. BIO_free(membio);
  27948. X509_NAME_free(name);
  27949. }
  27950. #endif
  27951. return EXPECT_RESULT();
  27952. }
  27953. #ifndef NO_BIO
  27954. static int test_wolfSSL_X509_INFO_multiple_info(void)
  27955. {
  27956. EXPECT_DECLS;
  27957. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  27958. STACK_OF(X509_INFO) *info_stack = NULL;
  27959. X509_INFO *info = NULL;
  27960. int len;
  27961. int i;
  27962. const char* files[] = {
  27963. cliCertFile,
  27964. cliKeyFile,
  27965. /* This needs to be the order as svrCertFile contains the
  27966. * intermediate cert as well. */
  27967. svrKeyFile,
  27968. svrCertFile,
  27969. NULL,
  27970. };
  27971. const char** curFile;
  27972. BIO *fileBIO = NULL;
  27973. BIO *concatBIO = NULL;
  27974. byte tmp[FOURK_BUF];
  27975. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  27976. * to group objects together. */
  27977. ExpectNotNull(concatBIO = BIO_new(BIO_s_mem()));
  27978. for (curFile = files; EXPECT_SUCCESS() && *curFile != NULL; curFile++) {
  27979. int fileLen;
  27980. ExpectNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  27981. ExpectIntGT(fileLen = wolfSSL_BIO_get_len(fileBIO), 0);
  27982. if (EXPECT_SUCCESS()) {
  27983. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  27984. ExpectIntEQ(BIO_write(concatBIO, tmp, len), len);
  27985. fileLen -= len;
  27986. if (EXPECT_FAIL())
  27987. break;
  27988. }
  27989. /* Make sure we read the entire file */
  27990. ExpectIntEQ(fileLen, 0);
  27991. }
  27992. BIO_free(fileBIO);
  27993. fileBIO = NULL;
  27994. }
  27995. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  27996. NULL));
  27997. ExpectIntEQ(sk_X509_INFO_num(info_stack), 3);
  27998. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  27999. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  28000. ExpectNotNull(info->x509);
  28001. ExpectNull(info->crl);
  28002. if (i != 0) {
  28003. ExpectNotNull(info->x_pkey);
  28004. ExpectIntEQ(X509_check_private_key(info->x509,
  28005. info->x_pkey->dec_pkey), 1);
  28006. }
  28007. else {
  28008. ExpectNull(info->x_pkey);
  28009. }
  28010. }
  28011. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  28012. BIO_free(concatBIO);
  28013. #endif
  28014. return EXPECT_RESULT();
  28015. }
  28016. #endif
  28017. #ifndef NO_BIO
  28018. static int test_wolfSSL_X509_INFO(void)
  28019. {
  28020. EXPECT_DECLS;
  28021. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  28022. STACK_OF(X509_INFO) *info_stack = NULL;
  28023. X509_INFO *info = NULL;
  28024. BIO *cert = NULL;
  28025. int i;
  28026. /* PEM in hex format to avoid null terminator */
  28027. byte data[] = {
  28028. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  28029. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  28030. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  28031. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  28032. 0x2d, 0x2d, 0x2d
  28033. };
  28034. /* PEM in hex format to avoid null terminator */
  28035. byte data2[] = {
  28036. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  28037. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  28038. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  28039. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  28040. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  28041. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  28042. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  28043. };
  28044. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  28045. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  28046. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  28047. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  28048. ExpectNotNull(info->x509);
  28049. ExpectNull(info->crl);
  28050. ExpectNull(info->x_pkey);
  28051. }
  28052. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  28053. info_stack = NULL;
  28054. BIO_free(cert);
  28055. cert = NULL;
  28056. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  28057. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  28058. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  28059. info_stack = NULL;
  28060. BIO_free(cert);
  28061. cert = NULL;
  28062. /* This case should fail due to invalid input. */
  28063. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  28064. ExpectIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  28065. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  28066. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  28067. info_stack = NULL;
  28068. BIO_free(cert);
  28069. cert = NULL;
  28070. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  28071. ExpectIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  28072. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  28073. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  28074. BIO_free(cert);
  28075. #endif
  28076. return EXPECT_RESULT();
  28077. }
  28078. #endif
  28079. static int test_wolfSSL_X509_subject_name_hash(void)
  28080. {
  28081. EXPECT_DECLS;
  28082. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  28083. !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  28084. X509* x509 = NULL;
  28085. X509_NAME* subjectName = NULL;
  28086. unsigned long ret1 = 0;
  28087. unsigned long ret2 = 0;
  28088. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  28089. SSL_FILETYPE_PEM));
  28090. ExpectNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  28091. /* These two
  28092. * - X509_subject_name_hash(x509)
  28093. * - X509_NAME_hash(X509_get_subject_name(x509))
  28094. * should give the same hash, if !defined(NO_SHA) is true. */
  28095. ret1 = X509_subject_name_hash(x509);
  28096. ExpectIntNE(ret1, 0);
  28097. #if !defined(NO_SHA)
  28098. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  28099. ExpectIntNE(ret2, 0);
  28100. ExpectIntEQ(ret1, ret2);
  28101. #else
  28102. (void) ret2;
  28103. #endif
  28104. X509_free(x509);
  28105. #endif
  28106. return EXPECT_RESULT();
  28107. }
  28108. static int test_wolfSSL_X509_issuer_name_hash(void)
  28109. {
  28110. EXPECT_DECLS;
  28111. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  28112. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  28113. X509* x509 = NULL;
  28114. X509_NAME* issuertName = NULL;
  28115. unsigned long ret1 = 0;
  28116. unsigned long ret2 = 0;
  28117. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  28118. SSL_FILETYPE_PEM));
  28119. ExpectNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  28120. /* These two
  28121. * - X509_issuer_name_hash(x509)
  28122. * - X509_NAME_hash(X509_get_issuer_name(x509))
  28123. * should give the same hash, if !defined(NO_SHA) is true. */
  28124. ret1 = X509_issuer_name_hash(x509);
  28125. ExpectIntNE(ret1, 0);
  28126. #if !defined(NO_SHA)
  28127. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  28128. ExpectIntNE(ret2, 0);
  28129. ExpectIntEQ(ret1, ret2);
  28130. #else
  28131. (void) ret2;
  28132. #endif
  28133. X509_free(x509);
  28134. #endif
  28135. return EXPECT_RESULT();
  28136. }
  28137. static int test_wolfSSL_X509_check_host(void)
  28138. {
  28139. EXPECT_DECLS;
  28140. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  28141. && !defined(NO_SHA) && !defined(NO_RSA)
  28142. X509* x509 = NULL;
  28143. const char altName[] = "example.com";
  28144. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  28145. SSL_FILETYPE_PEM));
  28146. ExpectIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  28147. WOLFSSL_SUCCESS);
  28148. ExpectIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  28149. WOLFSSL_FAILURE);
  28150. X509_free(x509);
  28151. ExpectIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  28152. WOLFSSL_FAILURE);
  28153. #endif
  28154. return EXPECT_RESULT();
  28155. }
  28156. static int test_wolfSSL_X509_check_email(void)
  28157. {
  28158. EXPECT_DECLS;
  28159. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  28160. X509* x509 = NULL;
  28161. const char goodEmail[] = "info@wolfssl.com";
  28162. const char badEmail[] = "disinfo@wolfssl.com";
  28163. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  28164. SSL_FILETYPE_PEM));
  28165. /* Should fail on non-matching email address */
  28166. ExpectIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  28167. WOLFSSL_FAILURE);
  28168. /* Should succeed on matching email address */
  28169. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  28170. WOLFSSL_SUCCESS);
  28171. /* Should compute length internally when not provided */
  28172. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  28173. WOLFSSL_SUCCESS);
  28174. /* Should fail when email address is NULL */
  28175. ExpectIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  28176. WOLFSSL_FAILURE);
  28177. X509_free(x509);
  28178. /* Should fail when x509 is NULL */
  28179. ExpectIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  28180. WOLFSSL_FAILURE);
  28181. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  28182. return EXPECT_RESULT();
  28183. }
  28184. static int test_wc_PemToDer(void)
  28185. {
  28186. EXPECT_DECLS;
  28187. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  28188. int ret;
  28189. DerBuffer* pDer = NULL;
  28190. const char* ca_cert = "./certs/server-cert.pem";
  28191. byte* cert_buf = NULL;
  28192. size_t cert_sz = 0;
  28193. int eccKey = 0;
  28194. EncryptedInfo info;
  28195. XMEMSET(&info, 0, sizeof(info));
  28196. ExpectIntEQ(ret = load_file(ca_cert, &cert_buf, &cert_sz), 0);
  28197. ExpectIntEQ(ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE, &pDer, NULL,
  28198. &info, &eccKey), 0);
  28199. wc_FreeDer(&pDer);
  28200. pDer = NULL;
  28201. if (cert_buf != NULL) {
  28202. free(cert_buf);
  28203. cert_buf = NULL;
  28204. }
  28205. #ifdef HAVE_ECC
  28206. {
  28207. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  28208. byte key_buf[256] = {0};
  28209. /* Test fail of loading a key with cert type */
  28210. ExpectIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  28211. key_buf[0] = '\n';
  28212. ExpectNotNull(XMEMCPY(key_buf + 1, cert_buf, cert_sz));
  28213. ExpectIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  28214. &pDer, NULL, &info, &eccKey)), 0);
  28215. #ifdef OPENSSL_EXTRA
  28216. ExpectIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  28217. &pDer, NULL, &info, &eccKey)), 0);
  28218. #endif
  28219. wc_FreeDer(&pDer);
  28220. if (cert_buf != NULL)
  28221. free(cert_buf);
  28222. }
  28223. #endif
  28224. #endif
  28225. return EXPECT_RESULT();
  28226. }
  28227. static int test_wc_AllocDer(void)
  28228. {
  28229. EXPECT_DECLS;
  28230. #if !defined(NO_CERTS)
  28231. DerBuffer* pDer = NULL;
  28232. word32 testSize = 1024;
  28233. ExpectIntEQ(wc_AllocDer(NULL, testSize, CERT_TYPE, HEAP_HINT),
  28234. BAD_FUNC_ARG);
  28235. ExpectIntEQ(wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT), 0);
  28236. ExpectNotNull(pDer);
  28237. wc_FreeDer(&pDer);
  28238. #endif
  28239. return EXPECT_RESULT();
  28240. }
  28241. static int test_wc_CertPemToDer(void)
  28242. {
  28243. EXPECT_DECLS;
  28244. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  28245. const char* ca_cert = "./certs/ca-cert.pem";
  28246. byte* cert_buf = NULL;
  28247. size_t cert_sz = 0;
  28248. size_t cert_dersz = 0;
  28249. byte* cert_der = NULL;
  28250. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  28251. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  28252. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28253. ExpectIntGE(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  28254. (int)cert_dersz, CERT_TYPE), 0);
  28255. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, -1, CERT_TYPE),
  28256. BAD_FUNC_ARG);
  28257. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL, -1, CERT_TYPE),
  28258. BAD_FUNC_ARG);
  28259. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der, -1, CERT_TYPE),
  28260. BAD_FUNC_ARG);
  28261. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, (int)cert_dersz,
  28262. CERT_TYPE), BAD_FUNC_ARG);
  28263. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der,
  28264. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  28265. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL,
  28266. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  28267. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der, -1,
  28268. CERT_TYPE), BAD_FUNC_ARG);
  28269. if (cert_der != NULL)
  28270. free(cert_der);
  28271. if (cert_buf != NULL)
  28272. free(cert_buf);
  28273. #endif
  28274. return EXPECT_RESULT();
  28275. }
  28276. static int test_wc_KeyPemToDer(void)
  28277. {
  28278. EXPECT_DECLS;
  28279. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28280. int ret;
  28281. const byte cert_buf[] = \
  28282. "-----BEGIN PRIVATE KEY-----\n"
  28283. "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDMG5KgWxP002pA\n"
  28284. "QJIdA4H5N0oM1Wf0LrHcos5RYUlrHDkC2b5p2BUpVRPmgDAFD2+8leim98x0BvcB\n"
  28285. "k48TNzrVynuwyVEY664+iQyzEBO5v27HPRydOddprbLCvRO036XINGIjauy1jHFi\n"
  28286. "HaDVx3bexSwgp9aefUGAszFXi4q1J4GacV7Cr2b/wBqUHqWv4ZXPu6R9/UYngTkD\n"
  28287. "UDJL5gLlLfcLzNyyodKPHPCIAKdWn6mSVdcHk8XVpK4y9lgz4E7YDWA6ohKZgWgG\n"
  28288. "2RDha8CMilFMDgYa0G0SiS9g3PQx0qh3AMXJJsKSVhScFCZufAE0kV6KvjP7jAqP\n"
  28289. "XBiSkRGPAgMBAAECggEAW7hmRyY2jRX2UMJThrM9VIs6fRLnYI0dQ0tsEJj536ay\n"
  28290. "nevQjArc05KWW0Yujg+WRDZPcry3RUqd9Djlmhp/F3Si6dpF1b+PMS3wJYVrf9Sd\n"
  28291. "SO5W7faArU4vnyBNe0HnY1Ta5xSVI65lg1RSIs88RTZwsooJwXYDGf0shq0/21CE\n"
  28292. "V8HOb27DDYNcEnm35lzaONjFnMqQQT2Vs9anRrPiSEXNleEvTgLVXZtGTyCGTz6v\n"
  28293. "x86Y8eSWL9YNHvPE1I+mDPuocfSR7eRNgRu7SK3mn94W5mqd7Ns072YKX/2XN1mO\n"
  28294. "66+ZFHO6v4dK1u7cSjuwrU1EhLHpUsgDz6Bna5InyQKBgQDv5l8RPy8UneKSADaf\n"
  28295. "M5L/5675I/5t4nqVjvbnQje00YveLTAEjlJBNR93Biln3sYgnvNamYDCxyEuUZ/I\n"
  28296. "S/vmBL9PoxfGZow4FcsIBOEbIn3E0SYJgCBNWthquUvGpKsYDnThJuhO+1cVmxAJ\n"
  28297. "BUOjLFnJYHM0a+Vmk9GexT2OBwKBgQDZzkUBOK7Im3eiYytFocUJyhqMH30d49X9\n"
  28298. "ujC7kGw4UWAqVe7YCSvlBa8nzWpRWK2kRpu3M0272RU0V4geyWqT+nr/SvRRPtNP\n"
  28299. "F5dY8l3yR7hjtSejqqjOfBcZT6ETJxI4tiG0+Nl5BlfM5M+0nxnkWpRcHuOR3j79\n"
  28300. "YUFERyN+OQKBgQCjlOKeUAc6d65W/+4/AFvsQ378Q57qLtSHxsR1TKHPmlNVXFqx\n"
  28301. "wJo1/JNIBduWCEHxXHF0BdfW+RGXE/FwEt/hKLuLAhrkHmjelX2sKieU6R/5ZOQa\n"
  28302. "9lMQbDHGFDOncAF6leD85hriQGBRSzrT69MDIOrYdfwYcroqCAGX0cb3YQKBgQC8\n"
  28303. "iIFQylj5SyHmjcMSNjKSA8CxFDzAV8yPIdE3Oo+CvGXqn5HsrRuy1hXE9VmXapR8\n"
  28304. "A6ackSszdHiXY0FvrNe1mfdH7wDHJwPQjdIzazCJHS3uGQxj7sDKY7226ie6pXJv\n"
  28305. "ZrCMr2/IBAaSVGm6ppHKCeIsT4ybYm7R85KEYLPHeQKBgBeJOMBinXQfWN/1jT9b\n"
  28306. "6Ywrutvp2zP8hVxQGSZJ0WG4iewZyFLsPUlbWRXOSYNPElHmdD0ZomdLVm+lSpAA\n"
  28307. "XSH5FJ/IFCwqq7Eft6Gf8NFRV+NjPMUny+PnjHe4oFP8YK/Ek22K3ttNG8Hw69Aw\n"
  28308. "AQue5o6oVfhgLiJzMdo/77gw\n"
  28309. "-----END PRIVATE KEY-----\n";
  28310. const int cert_sz = sizeof(cert_buf);
  28311. const char cert_pw[] = "password";
  28312. int cert_dersz = 0;
  28313. byte* cert_der = NULL;
  28314. /* Bad arg: Cert buffer is NULL */
  28315. ExpectIntEQ(wc_KeyPemToDer(NULL, cert_sz, cert_der, cert_dersz, ""),
  28316. BAD_FUNC_ARG);
  28317. /* Bad arg: Cert DER buffer non-NULL but size zero (or less) */
  28318. ExpectIntEQ(wc_KeyPemToDer(cert_buf, cert_sz, (byte*)&cert_der, 0, ""),
  28319. BAD_FUNC_ARG);
  28320. /* Test normal operation */
  28321. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  28322. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28323. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  28324. cert_pw), 0);
  28325. ExpectIntLE(ret, cert_sz);
  28326. if (cert_der != NULL) {
  28327. free(cert_der);
  28328. cert_der = NULL;
  28329. }
  28330. /* Test NULL for DER buffer to return needed DER buffer size */
  28331. ExpectIntGT(ret = wc_KeyPemToDer(cert_buf, cert_sz, NULL, 0, ""), 0);
  28332. ExpectIntLE(ret, cert_sz);
  28333. if (EXPECT_SUCCESS())
  28334. cert_dersz = ret;
  28335. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28336. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  28337. cert_pw), 0);
  28338. ExpectIntLE(ret, cert_sz);
  28339. if (cert_der != NULL)
  28340. free(cert_der);
  28341. #endif
  28342. return EXPECT_RESULT();
  28343. }
  28344. static int test_wc_PubKeyPemToDer(void)
  28345. {
  28346. EXPECT_DECLS;
  28347. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  28348. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  28349. int ret = 0;
  28350. const char* key = "./certs/ecc-client-keyPub.pem";
  28351. byte* cert_buf = NULL;
  28352. size_t cert_sz = 0, cert_dersz = 0;
  28353. byte* cert_der = NULL;
  28354. ExpectIntEQ(wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  28355. cert_der, (int)cert_dersz), BAD_FUNC_ARG);
  28356. ExpectIntEQ(load_file(key, &cert_buf, &cert_sz), 0);
  28357. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  28358. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28359. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  28360. (int)cert_dersz), 0);
  28361. if (cert_der != NULL) {
  28362. free(cert_der);
  28363. cert_der = NULL;
  28364. }
  28365. /* Test NULL for DER buffer to return needed DER buffer size */
  28366. ExpectIntGT(ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, NULL, 0), 0);
  28367. ExpectIntLE(ret, cert_sz);
  28368. cert_dersz = ret;
  28369. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28370. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  28371. (int)cert_dersz), 0);
  28372. if (cert_der != NULL) {
  28373. free(cert_der);
  28374. }
  28375. if (cert_buf != NULL) {
  28376. free(cert_buf);
  28377. }
  28378. #endif
  28379. return EXPECT_RESULT();
  28380. }
  28381. static int test_wc_PemPubKeyToDer(void)
  28382. {
  28383. EXPECT_DECLS;
  28384. #if !defined(NO_FILESYSTEM) && \
  28385. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  28386. const char* key = "./certs/ecc-client-keyPub.pem";
  28387. size_t cert_dersz = 1024;
  28388. byte* cert_der = NULL;
  28389. ExpectIntGE(wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz),
  28390. BAD_FUNC_ARG);
  28391. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28392. ExpectIntGE(wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz), 0);
  28393. if (cert_der != NULL) {
  28394. free(cert_der);
  28395. }
  28396. #endif
  28397. return EXPECT_RESULT();
  28398. }
  28399. static int test_wc_GetPubKeyDerFromCert(void)
  28400. {
  28401. EXPECT_DECLS;
  28402. #if !defined(NO_RSA) || defined(HAVE_ECC)
  28403. int ret;
  28404. word32 idx = 0;
  28405. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  28406. word32 keyDerSz = (word32)sizeof(keyDer);
  28407. DecodedCert decoded;
  28408. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  28409. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  28410. word32 certBufSz = sizeof(certBuf);
  28411. #endif
  28412. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  28413. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  28414. XFILE fp = XBADFILE;
  28415. #endif
  28416. #ifndef NO_RSA
  28417. RsaKey rsaKey;
  28418. #if defined(USE_CERT_BUFFERS_2048)
  28419. byte* rsaCertDer = (byte*)client_cert_der_2048;
  28420. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  28421. #elif defined(USE_CERT_BUFFERS_1024)
  28422. byte* rsaCertDer = (byte*)client_cert_der_1024;
  28423. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  28424. #else
  28425. unsigned char rsaCertDer[TWOK_BUF];
  28426. word32 rsaCertDerSz;
  28427. #endif
  28428. #endif
  28429. #ifdef HAVE_ECC
  28430. ecc_key eccKey;
  28431. #if defined(USE_CERT_BUFFERS_256)
  28432. byte* eccCert = (byte*)cliecc_cert_der_256;
  28433. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  28434. #else
  28435. unsigned char eccCert[ONEK_BUF];
  28436. word32 eccCertSz;
  28437. XFILE fp2 = XBADFILE;
  28438. #endif
  28439. #endif
  28440. #ifndef NO_RSA
  28441. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  28442. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  28443. ExpectIntGT(rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer),
  28444. fp), 0);
  28445. if (fp != XBADFILE) {
  28446. XFCLOSE(fp);
  28447. fp = XBADFILE;
  28448. }
  28449. #endif
  28450. /* good test case - RSA DER cert */
  28451. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  28452. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  28453. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  28454. ExpectIntGT(keyDerSz, 0);
  28455. /* sanity check, verify we can import DER public key */
  28456. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  28457. ExpectIntEQ(ret, 0);
  28458. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  28459. if (ret == 0) {
  28460. wc_FreeRsaKey(&rsaKey);
  28461. }
  28462. /* test LENGTH_ONLY_E case */
  28463. keyDerSz = 0;
  28464. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  28465. LENGTH_ONLY_E);
  28466. ExpectIntGT(keyDerSz, 0);
  28467. /* bad args: DecodedCert NULL */
  28468. ExpectIntEQ(wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz), BAD_FUNC_ARG);
  28469. /* bad args: output key buff size */
  28470. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL), BAD_FUNC_ARG);
  28471. /* bad args: zero size output key buffer */
  28472. keyDerSz = 0;
  28473. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  28474. BAD_FUNC_ARG);
  28475. wc_FreeDecodedCert(&decoded);
  28476. /* Certificate Request Tests */
  28477. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  28478. {
  28479. XMEMSET(certBuf, 0, sizeof(certBuf));
  28480. ExpectTrue((fp = XFOPEN("./certs/csr.signed.der", "rb")) != XBADFILE);
  28481. ExpectIntGT(certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp), 0);
  28482. if (fp != XBADFILE) {
  28483. XFCLOSE(fp);
  28484. }
  28485. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  28486. ExpectIntEQ(wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL), 0);
  28487. /* good test case - RSA DER certificate request */
  28488. keyDerSz = sizeof(keyDer);
  28489. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  28490. 0);
  28491. ExpectIntGT(keyDerSz, 0);
  28492. /* sanity check, verify we can import DER public key */
  28493. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  28494. ExpectIntEQ(ret, 0);
  28495. idx = 0;
  28496. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  28497. if (ret == 0) {
  28498. wc_FreeRsaKey(&rsaKey);
  28499. }
  28500. wc_FreeDecodedCert(&decoded);
  28501. }
  28502. #endif /* WOLFSSL_CERT_REQ */
  28503. #endif /* NO_RSA */
  28504. #ifdef HAVE_ECC
  28505. #ifndef USE_CERT_BUFFERS_256
  28506. ExpectTrue((fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  28507. XBADFILE);
  28508. ExpectIntGT(eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2), 0);
  28509. if (fp2 != XBADFILE) {
  28510. XFCLOSE(fp2);
  28511. }
  28512. #endif
  28513. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  28514. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  28515. /* good test case - ECC */
  28516. XMEMSET(keyDer, 0, sizeof(keyDer));
  28517. keyDerSz = sizeof(keyDer);
  28518. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  28519. ExpectIntGT(keyDerSz, 0);
  28520. /* sanity check, verify we can import DER public key */
  28521. ret = wc_ecc_init(&eccKey);
  28522. ExpectIntEQ(ret, 0);
  28523. idx = 0; /* reset idx to 0, used above in RSA case */
  28524. ExpectIntEQ(wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz), 0);
  28525. if (ret == 0) {
  28526. wc_ecc_free(&eccKey);
  28527. }
  28528. /* test LENGTH_ONLY_E case */
  28529. keyDerSz = 0;
  28530. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  28531. LENGTH_ONLY_E);
  28532. ExpectIntGT(keyDerSz, 0);
  28533. wc_FreeDecodedCert(&decoded);
  28534. #endif
  28535. #endif /* !NO_RSA || HAVE_ECC */
  28536. return EXPECT_RESULT();
  28537. }
  28538. static int test_wc_CheckCertSigPubKey(void)
  28539. {
  28540. EXPECT_DECLS;
  28541. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  28542. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  28543. int ret;
  28544. const char* ca_cert = "./certs/ca-cert.pem";
  28545. byte* cert_buf = NULL;
  28546. size_t cert_sz = 0;
  28547. byte* cert_der = NULL;
  28548. word32 cert_dersz = 0;
  28549. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  28550. word32 keyDerSz = (word32)sizeof(keyDer);
  28551. DecodedCert decoded;
  28552. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  28553. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  28554. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28555. ExpectIntGE(ret = wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  28556. (int)cert_dersz, CERT_TYPE), 0);
  28557. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  28558. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  28559. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  28560. ExpectIntGT(keyDerSz, 0);
  28561. /* Good test case. */
  28562. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  28563. keyDerSz, RSAk), 0);
  28564. /* No certificate. */
  28565. ExpectIntEQ(wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  28566. ECDSAk), BAD_FUNC_ARG);
  28567. /* Bad cert size. */
  28568. ExpectIntNE(ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  28569. RSAk), 0);
  28570. ExpectTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  28571. /* No public key. */
  28572. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL,
  28573. keyDerSz, RSAk), ASN_NO_SIGNER_E);
  28574. /* Bad public key size. */
  28575. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  28576. RSAk), BAD_FUNC_ARG);
  28577. /* Wrong aglo. */
  28578. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  28579. keyDerSz, ECDSAk), ASN_PARSE_E);
  28580. wc_FreeDecodedCert(&decoded);
  28581. if (cert_der != NULL)
  28582. free(cert_der);
  28583. if (cert_buf != NULL)
  28584. free(cert_buf);
  28585. #endif
  28586. return EXPECT_RESULT();
  28587. }
  28588. static int test_wolfSSL_certs(void)
  28589. {
  28590. EXPECT_DECLS;
  28591. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  28592. !defined(NO_RSA)
  28593. X509* x509ext = NULL;
  28594. #ifdef OPENSSL_ALL
  28595. X509* x509 = NULL;
  28596. WOLFSSL_X509_EXTENSION* ext = NULL;
  28597. ASN1_OBJECT* obj = NULL;
  28598. #endif
  28599. WOLFSSL* ssl = NULL;
  28600. WOLFSSL_CTX* ctx = NULL;
  28601. STACK_OF(ASN1_OBJECT)* sk = NULL;
  28602. ASN1_STRING* asn1_str = NULL;
  28603. AUTHORITY_KEYID* akey = NULL;
  28604. BASIC_CONSTRAINTS* bc = NULL;
  28605. int crit;
  28606. #ifndef NO_WOLFSSL_SERVER
  28607. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  28608. #else
  28609. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  28610. #endif
  28611. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  28612. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  28613. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  28614. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28615. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  28616. #endif
  28617. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  28618. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28619. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  28620. #endif
  28621. ExpectNotNull(ssl = SSL_new(ctx));
  28622. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28623. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28624. #endif
  28625. #ifdef HAVE_PK_CALLBACKS
  28626. ExpectIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  28627. #endif /* HAVE_PK_CALLBACKS */
  28628. /* create and use x509 */
  28629. #ifdef OPENSSL_ALL
  28630. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  28631. WOLFSSL_FILETYPE_PEM));
  28632. #endif
  28633. ExpectNotNull(x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  28634. WOLFSSL_FILETYPE_PEM));
  28635. ExpectIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  28636. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28637. /* with loading in a new cert the check on private key should now fail */
  28638. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28639. #endif
  28640. #if defined(USE_CERT_BUFFERS_2048)
  28641. ExpectIntEQ(SSL_use_certificate_ASN1(ssl,
  28642. (unsigned char*)server_cert_der_2048,
  28643. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  28644. #endif
  28645. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  28646. /************* Get Digest of Certificate ******************/
  28647. {
  28648. byte digest[64]; /* max digest size */
  28649. word32 digestSz;
  28650. XMEMSET(digest, 0, sizeof(digest));
  28651. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  28652. WOLFSSL_SUCCESS);
  28653. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  28654. WOLFSSL_SUCCESS);
  28655. ExpectIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  28656. WOLFSSL_FAILURE);
  28657. }
  28658. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  28659. /* test and checkout X509 extensions */
  28660. ExpectNotNull(bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext,
  28661. NID_basic_constraints, &crit, NULL));
  28662. ExpectIntEQ(crit, 0);
  28663. #ifdef OPENSSL_ALL
  28664. ExpectNotNull(ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc));
  28665. X509_EXTENSION_free(ext);
  28666. ext = NULL;
  28667. ExpectNotNull(ext = X509_EXTENSION_new());
  28668. X509_EXTENSION_set_critical(ext, 1);
  28669. ExpectNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  28670. ExpectIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  28671. ASN1_OBJECT_free(obj);
  28672. obj = NULL;
  28673. X509_EXTENSION_free(ext);
  28674. ext = NULL;
  28675. ExpectNotNull(ext = X509_EXTENSION_new());
  28676. X509_EXTENSION_set_critical(ext, 0);
  28677. ExpectIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  28678. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  28679. NULL);
  28680. ExpectIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  28681. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  28682. * and X509_get_ext_d2i has created new */
  28683. asn1_str = NULL;
  28684. X509_EXTENSION_free(ext);
  28685. ext = NULL;
  28686. #endif
  28687. BASIC_CONSTRAINTS_free(bc);
  28688. bc = NULL;
  28689. ExpectNotNull(asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext,
  28690. NID_key_usage, &crit, NULL));
  28691. ExpectIntEQ(crit, 1);
  28692. ExpectIntEQ(asn1_str->type, NID_key_usage);
  28693. #ifdef OPENSSL_ALL
  28694. ExpectNotNull(ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str));
  28695. X509_EXTENSION_free(ext);
  28696. ext = NULL;
  28697. #endif
  28698. ASN1_STRING_free(asn1_str);
  28699. asn1_str = NULL;
  28700. #ifdef OPENSSL_ALL
  28701. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  28702. NID_ext_key_usage, &crit, NULL));
  28703. ExpectNotNull(ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk));
  28704. X509_EXTENSION_free(ext);
  28705. ext = NULL;
  28706. EXTENDED_KEY_USAGE_free(sk);
  28707. sk = NULL;
  28708. #else
  28709. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  28710. &crit, NULL);
  28711. ExpectNull(sk);
  28712. #endif
  28713. ExpectNotNull(akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  28714. NID_authority_key_identifier, &crit, NULL));
  28715. #ifdef OPENSSL_ALL
  28716. ExpectNotNull(ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit,
  28717. akey));
  28718. X509_EXTENSION_free(ext);
  28719. ext = NULL;
  28720. #endif
  28721. wolfSSL_AUTHORITY_KEYID_free(akey);
  28722. akey = NULL;
  28723. /* NID not yet supported */
  28724. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28725. NID_private_key_usage_period, &crit, NULL));
  28726. ExpectIntEQ(crit, -1);
  28727. sk_ASN1_OBJECT_free(sk);
  28728. sk = NULL;
  28729. ExpectNotNull(sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext,
  28730. NID_subject_alt_name, &crit, NULL));
  28731. {
  28732. int i;
  28733. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  28734. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  28735. ExpectIntEQ(gen->type, GEN_DNS);
  28736. ExpectIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  28737. }
  28738. }
  28739. sk_GENERAL_NAME_free(sk);
  28740. sk = NULL;
  28741. /* NID not yet supported */
  28742. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28743. NID_issuer_alt_name, &crit, NULL));
  28744. ExpectIntEQ(crit, -1);
  28745. sk_ASN1_OBJECT_free(sk);
  28746. sk = NULL;
  28747. /* NID not yet supported */
  28748. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28749. NID_info_access, &crit, NULL));
  28750. sk_ASN1_OBJECT_free(sk);
  28751. sk = NULL;
  28752. /* NID not yet supported */
  28753. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28754. NID_sinfo_access, &crit, NULL));
  28755. ExpectIntEQ(crit, -1);
  28756. sk_ASN1_OBJECT_free(sk);
  28757. sk = NULL;
  28758. /* NID not yet supported */
  28759. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28760. NID_name_constraints, &crit, NULL));
  28761. ExpectIntEQ(crit, -1);
  28762. sk_ASN1_OBJECT_free(sk);
  28763. sk = NULL;
  28764. /* no cert policy set */
  28765. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28766. NID_certificate_policies, &crit, NULL));
  28767. sk_ASN1_OBJECT_free(sk);
  28768. sk = NULL;
  28769. /* NID not yet supported */
  28770. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28771. NID_policy_mappings, &crit, NULL));
  28772. ExpectIntEQ(crit, -1);
  28773. sk_ASN1_OBJECT_free(sk);
  28774. sk = NULL;
  28775. /* NID not yet supported */
  28776. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28777. NID_policy_constraints, &crit, NULL));
  28778. ExpectIntEQ(crit, -1);
  28779. sk_ASN1_OBJECT_free(sk);
  28780. sk = NULL;
  28781. /* NID not yet supported */
  28782. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28783. NID_inhibit_any_policy, &crit, NULL));
  28784. ExpectIntEQ(crit, -1);
  28785. sk_ASN1_OBJECT_free(sk);
  28786. sk = NULL;
  28787. /* NID not yet supported */
  28788. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28789. NID_tlsfeature, &crit, NULL));
  28790. ExpectIntEQ(crit, -1);
  28791. sk_ASN1_OBJECT_free(sk);
  28792. sk = NULL;
  28793. /* test invalid cases */
  28794. crit = 0;
  28795. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit,
  28796. NULL));
  28797. ExpectIntEQ(crit, -1);
  28798. /* NULL passed for criticality. */
  28799. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL,
  28800. NID_tlsfeature, NULL, NULL));
  28801. ExpectIntEQ(SSL_get_hit(ssl), 0);
  28802. #ifdef OPENSSL_ALL
  28803. X509_free(x509);
  28804. #endif
  28805. X509_free(x509ext);
  28806. SSL_free(ssl);
  28807. SSL_CTX_free(ctx);
  28808. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  28809. return EXPECT_RESULT();
  28810. }
  28811. static int test_wolfSSL_X509_check_private_key(void)
  28812. {
  28813. EXPECT_DECLS;
  28814. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  28815. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  28816. X509* x509 = NULL;
  28817. EVP_PKEY* pkey = NULL;
  28818. const byte* key;
  28819. /* Check with correct key */
  28820. ExpectNotNull((x509 = X509_load_certificate_file(cliCertFile,
  28821. SSL_FILETYPE_PEM)));
  28822. key = client_key_der_2048;
  28823. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  28824. (long)sizeof_client_key_der_2048));
  28825. ExpectIntEQ(X509_check_private_key(x509, pkey), 1);
  28826. EVP_PKEY_free(pkey);
  28827. pkey = NULL;
  28828. /* Check with wrong key */
  28829. key = server_key_der_2048;
  28830. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  28831. (long)sizeof_server_key_der_2048));
  28832. ExpectIntEQ(X509_check_private_key(x509, pkey), 0);
  28833. /* test for incorrect parameter */
  28834. ExpectIntEQ(X509_check_private_key(NULL, pkey), 0);
  28835. ExpectIntEQ(X509_check_private_key(x509, NULL), 0);
  28836. ExpectIntEQ(X509_check_private_key(NULL, NULL), 0);
  28837. EVP_PKEY_free(pkey);
  28838. X509_free(x509);
  28839. #endif
  28840. return EXPECT_RESULT();
  28841. }
  28842. static int test_wolfSSL_private_keys(void)
  28843. {
  28844. EXPECT_DECLS;
  28845. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28846. !defined(NO_FILESYSTEM)
  28847. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  28848. WOLFSSL* ssl = NULL;
  28849. WOLFSSL_CTX* ctx = NULL;
  28850. EVP_PKEY* pkey = NULL;
  28851. OpenSSL_add_all_digests();
  28852. OpenSSL_add_all_algorithms();
  28853. #ifndef NO_RSA
  28854. #ifndef NO_WOLFSSL_SERVER
  28855. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28856. #else
  28857. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28858. #endif
  28859. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  28860. /* Have to load a cert before you can check the private key against that
  28861. * certificates public key! */
  28862. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28863. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  28864. #endif
  28865. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  28866. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28867. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  28868. #endif
  28869. ExpectNotNull(ssl = SSL_new(ctx));
  28870. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28871. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28872. #endif
  28873. #ifdef USE_CERT_BUFFERS_2048
  28874. {
  28875. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  28876. unsigned char buf[FOURK_BUF];
  28877. word32 bufSz;
  28878. ExpectIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  28879. (unsigned char*)client_key_der_2048,
  28880. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  28881. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28882. /* Should mismatch now that a different private key loaded */
  28883. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28884. #endif
  28885. ExpectIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  28886. (unsigned char*)server_key,
  28887. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  28888. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28889. /* After loading back in DER format of original key, should match */
  28890. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28891. #endif
  28892. /* test loading private key to the WOLFSSL_CTX */
  28893. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  28894. (unsigned char*)client_key_der_2048,
  28895. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  28896. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28897. /* Should mismatch now that a different private key loaded */
  28898. ExpectIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  28899. #endif
  28900. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  28901. (unsigned char*)server_key,
  28902. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  28903. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28904. /* After loading back in DER format of original key, should match */
  28905. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  28906. #endif
  28907. /* pkey not set yet, expecting to fail */
  28908. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  28909. /* set PKEY and test again */
  28910. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  28911. &server_key, (long)sizeof_server_key_der_2048));
  28912. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  28913. /* reuse PKEY structure and test
  28914. * this should be checked with a memory management sanity checker */
  28915. ExpectFalse(server_key == (const unsigned char*)server_key_der_2048);
  28916. server_key = (const unsigned char*)server_key_der_2048;
  28917. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  28918. &server_key, (long)sizeof_server_key_der_2048));
  28919. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  28920. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  28921. bufSz = FOURK_BUF;
  28922. ExpectIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  28923. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  28924. RSAk, NULL, 0)), 0);
  28925. server_key = (const unsigned char*)buf;
  28926. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  28927. (long)bufSz));
  28928. }
  28929. #endif
  28930. EVP_PKEY_free(pkey);
  28931. pkey = NULL;
  28932. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  28933. ssl = NULL;
  28934. SSL_CTX_free(ctx);
  28935. ctx = NULL;
  28936. #endif /* end of RSA private key match tests */
  28937. #ifdef HAVE_ECC
  28938. #ifndef NO_WOLFSSL_SERVER
  28939. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28940. #else
  28941. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28942. #endif
  28943. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  28944. WOLFSSL_FILETYPE_PEM));
  28945. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  28946. WOLFSSL_FILETYPE_PEM));
  28947. ExpectNotNull(ssl = SSL_new(ctx));
  28948. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28949. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28950. #endif
  28951. SSL_free(ssl);
  28952. ssl = NULL;
  28953. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  28954. WOLFSSL_FILETYPE_PEM));
  28955. ExpectNotNull(ssl = SSL_new(ctx));
  28956. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  28957. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28958. #endif
  28959. SSL_free(ssl);
  28960. ssl = NULL;
  28961. SSL_CTX_free(ctx);
  28962. ctx = NULL;
  28963. #endif /* end of ECC private key match tests */
  28964. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  28965. #ifndef NO_WOLFSSL_SERVER
  28966. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28967. #else
  28968. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28969. #endif
  28970. ExpectTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  28971. WOLFSSL_FILETYPE_PEM));
  28972. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  28973. WOLFSSL_FILETYPE_PEM));
  28974. ExpectNotNull(ssl = SSL_new(ctx));
  28975. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28976. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28977. #endif
  28978. SSL_free(ssl);
  28979. ssl = NULL;
  28980. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  28981. WOLFSSL_FILETYPE_PEM));
  28982. ExpectNotNull(ssl = SSL_new(ctx));
  28983. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28984. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28985. #endif
  28986. SSL_free(ssl);
  28987. ssl = NULL;
  28988. SSL_CTX_free(ctx);
  28989. ctx = NULL;
  28990. #endif /* end of Ed25519 private key match tests */
  28991. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  28992. #ifndef NO_WOLFSSL_SERVER
  28993. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28994. #else
  28995. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28996. #endif
  28997. ExpectTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  28998. WOLFSSL_FILETYPE_PEM));
  28999. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  29000. WOLFSSL_FILETYPE_PEM));
  29001. ExpectNotNull(ssl = SSL_new(ctx));
  29002. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  29003. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29004. #endif
  29005. SSL_free(ssl);
  29006. ssl = NULL;
  29007. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  29008. WOLFSSL_FILETYPE_PEM));
  29009. ExpectNotNull(ssl = SSL_new(ctx));
  29010. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  29011. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29012. #endif
  29013. SSL_free(ssl);
  29014. ssl = NULL;
  29015. SSL_CTX_free(ctx);
  29016. ctx = NULL;
  29017. #endif /* end of Ed448 private key match tests */
  29018. EVP_cleanup();
  29019. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  29020. CONF_modules_free();
  29021. ENGINE_cleanup();
  29022. CONF_modules_unload();
  29023. (void)ssl;
  29024. (void)ctx;
  29025. (void)pkey;
  29026. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  29027. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  29028. return EXPECT_RESULT();
  29029. }
  29030. static int test_wolfSSL_PEM_read_PrivateKey(void)
  29031. {
  29032. EXPECT_DECLS;
  29033. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  29034. && !defined(NO_FILESYSTEM)
  29035. XFILE file = XBADFILE;
  29036. const char* fname = "./certs/server-key.pem";
  29037. EVP_PKEY* pkey = NULL;
  29038. RSA* rsa = NULL;
  29039. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  29040. unsigned char* sig = NULL;
  29041. size_t sigLen;
  29042. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  29043. size_t tbsLen = sizeof(tbs);
  29044. /* Check error case. */
  29045. ExpectNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  29046. /* Read in an RSA key. */
  29047. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29048. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  29049. if (file != XBADFILE)
  29050. XFCLOSE(file);
  29051. /* Make sure the key is usable by signing some data with it. */
  29052. ExpectNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  29053. ExpectIntGT((sigLen = RSA_size(rsa)), 0);
  29054. ExpectNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  29055. DYNAMIC_TYPE_TMP_BUFFER));
  29056. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  29057. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  29058. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  29059. WOLFSSL_SUCCESS);
  29060. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29061. EVP_PKEY_CTX_free(ctx);
  29062. EVP_PKEY_free(pkey);
  29063. #endif
  29064. return EXPECT_RESULT();
  29065. }
  29066. static int test_wolfSSL_PEM_read_PUBKEY(void)
  29067. {
  29068. EXPECT_DECLS;
  29069. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  29070. && !defined(NO_FILESYSTEM)
  29071. XFILE file = XBADFILE;
  29072. const char* fname = "./certs/client-keyPub.pem";
  29073. EVP_PKEY* pkey = NULL;
  29074. /* Check error case. */
  29075. ExpectNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  29076. /* Read in an RSA key. */
  29077. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29078. ExpectNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  29079. EVP_PKEY_free(pkey);
  29080. if (file != XBADFILE)
  29081. XFCLOSE(file);
  29082. #endif
  29083. return EXPECT_RESULT();
  29084. }
  29085. static int test_wolfSSL_PEM_PrivateKey(void)
  29086. {
  29087. EXPECT_DECLS;
  29088. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29089. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  29090. #ifndef NO_BIO
  29091. BIO* bio = NULL;
  29092. #endif
  29093. EVP_PKEY* pkey = NULL;
  29094. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  29095. #ifndef NO_BIO
  29096. /* test creating new EVP_PKEY with bad arg */
  29097. ExpectNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  29098. /* test loading RSA key using BIO */
  29099. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  29100. {
  29101. XFILE file = XBADFILE;
  29102. const char* fname = "./certs/server-key.pem";
  29103. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  29104. size_t sz;
  29105. byte* buf = NULL;
  29106. EVP_PKEY* pkey2 = NULL;
  29107. EVP_PKEY* pkey3 = NULL;
  29108. RSA* rsa_key = NULL;
  29109. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29110. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  29111. ExpectIntGT(sz = XFTELL(file), 0);
  29112. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  29113. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  29114. if (buf != NULL) {
  29115. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  29116. }
  29117. if (file != XBADFILE) {
  29118. XFCLOSE(file);
  29119. file = XBADFILE;
  29120. }
  29121. /* Test using BIO new mem and loading PEM private key */
  29122. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  29123. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  29124. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  29125. buf = NULL;
  29126. BIO_free(bio);
  29127. bio = NULL;
  29128. ExpectNotNull(pkey2 = EVP_PKEY_new());
  29129. if (pkey2 != NULL) {
  29130. pkey2->type = EVP_PKEY_RSA;
  29131. }
  29132. /* Test parameter copy */
  29133. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  29134. EVP_PKEY_free(pkey2);
  29135. EVP_PKEY_free(pkey);
  29136. pkey = NULL;
  29137. /* Qt unit test case : rsa pkcs8 key */
  29138. ExpectTrue((file = XFOPEN(fname_rsa_p8, "rb")) != XBADFILE);
  29139. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  29140. ExpectIntGT(sz = XFTELL(file), 0);
  29141. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  29142. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  29143. if (buf) {
  29144. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  29145. }
  29146. if (file != XBADFILE) {
  29147. XFCLOSE(file);
  29148. file = XBADFILE;
  29149. }
  29150. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  29151. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  29152. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  29153. buf = NULL;
  29154. BIO_free(bio);
  29155. bio = NULL;
  29156. ExpectNotNull(pkey3 = EVP_PKEY_new());
  29157. ExpectNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  29158. ExpectIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  29159. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  29160. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  29161. #else
  29162. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  29163. #endif
  29164. RSA_free(rsa_key);
  29165. EVP_PKEY_free(pkey3);
  29166. EVP_PKEY_free(pkey);
  29167. pkey = NULL;
  29168. }
  29169. #endif
  29170. /* test loading ECC key using BIO */
  29171. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  29172. {
  29173. XFILE file = XBADFILE;
  29174. const char* fname = "./certs/ecc-key.pem";
  29175. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  29176. size_t sz = 0;
  29177. byte* buf = NULL;
  29178. EVP_PKEY* pkey2 = NULL;
  29179. EVP_PKEY* pkey3 = NULL;
  29180. EC_KEY* ec_key = NULL;
  29181. int nid = 0;
  29182. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29183. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  29184. ExpectIntGT(sz = XFTELL(file), 0);
  29185. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  29186. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  29187. if (buf) {
  29188. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  29189. }
  29190. if (file != XBADFILE) {
  29191. XFCLOSE(file);
  29192. file = XBADFILE;
  29193. }
  29194. /* Test using BIO new mem and loading PEM private key */
  29195. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  29196. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  29197. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  29198. buf = NULL;
  29199. BIO_free(bio);
  29200. bio = NULL;
  29201. ExpectNotNull(pkey2 = EVP_PKEY_new());
  29202. ExpectNotNull(pkey3 = EVP_PKEY_new());
  29203. if (pkey2 != NULL) {
  29204. pkey2->type = EVP_PKEY_EC;
  29205. }
  29206. /* Test parameter copy */
  29207. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  29208. /* Qt unit test case 1*/
  29209. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  29210. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  29211. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  29212. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  29213. #else
  29214. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  29215. #endif
  29216. /* Test default digest */
  29217. ExpectIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  29218. ExpectIntEQ(nid, NID_sha256);
  29219. EC_KEY_free(ec_key);
  29220. ec_key = NULL;
  29221. EVP_PKEY_free(pkey3);
  29222. pkey3 = NULL;
  29223. EVP_PKEY_free(pkey2);
  29224. pkey2 = NULL;
  29225. EVP_PKEY_free(pkey);
  29226. pkey = NULL;
  29227. /* Qt unit test case ec pkcs8 key */
  29228. ExpectTrue((file = XFOPEN(fname_ecc_p8, "rb")) != XBADFILE);
  29229. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  29230. ExpectIntGT(sz = XFTELL(file), 0);
  29231. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  29232. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  29233. if (buf) {
  29234. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  29235. }
  29236. if (file != XBADFILE) {
  29237. XFCLOSE(file);
  29238. file = XBADFILE;
  29239. }
  29240. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  29241. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  29242. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  29243. buf = NULL;
  29244. BIO_free(bio);
  29245. bio = NULL;
  29246. ExpectNotNull(pkey3 = EVP_PKEY_new());
  29247. /* Qt unit test case */
  29248. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  29249. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  29250. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  29251. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  29252. #else
  29253. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  29254. #endif
  29255. EC_KEY_free(ec_key);
  29256. EVP_PKEY_free(pkey3);
  29257. EVP_PKEY_free(pkey);
  29258. pkey = NULL;
  29259. }
  29260. #endif
  29261. #if !defined(NO_BIO) && !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || \
  29262. defined(WOLFSSL_CERT_GEN))
  29263. {
  29264. #define BIO_PEM_TEST_CHAR 'a'
  29265. EVP_PKEY* pkey2 = NULL;
  29266. unsigned char extra[10];
  29267. int i;
  29268. BIO* pub_bio = NULL;
  29269. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  29270. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29271. ExpectIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  29272. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29273. ExpectIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  29274. ExpectNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
  29275. &server_key, (long)sizeof_server_key_der_2048));
  29276. ExpectNull(pkey);
  29277. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  29278. &server_key, (long)sizeof_server_key_der_2048));
  29279. ExpectIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  29280. NULL), WOLFSSL_FAILURE);
  29281. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  29282. NULL), WOLFSSL_FAILURE);
  29283. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  29284. NULL), WOLFSSL_SUCCESS);
  29285. ExpectIntGT(BIO_pending(bio), 0);
  29286. ExpectIntEQ(BIO_pending(bio), 1679);
  29287. /* Check if the pubkey API writes only the public key */
  29288. #ifdef WOLFSSL_KEY_GEN
  29289. ExpectIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  29290. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  29291. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  29292. ExpectIntGT(BIO_pending(pub_bio), 0);
  29293. /* Previously both the private key and the pubkey calls would write
  29294. * out the private key and the PEM header was the only difference.
  29295. * The public PEM should be significantly shorter than the
  29296. * private key versison. */
  29297. ExpectIntEQ(BIO_pending(pub_bio), 451);
  29298. #endif
  29299. /* test creating new EVP_PKEY with good args */
  29300. ExpectNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  29301. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr)
  29302. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz), 0);
  29303. /* test of reuse of EVP_PKEY */
  29304. ExpectNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  29305. ExpectIntEQ(BIO_pending(bio), 0);
  29306. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  29307. SSL_SUCCESS);
  29308. ExpectIntEQ(BIO_write(bio, extra, 10), 10); /* add 10 extra bytes after PEM */
  29309. ExpectNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  29310. ExpectNotNull(pkey);
  29311. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  29312. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  29313. }
  29314. ExpectIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
  29315. ExpectIntEQ(BIO_read(bio, extra, 10), 10);
  29316. for (i = 0; i < 10; i++) {
  29317. ExpectIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  29318. }
  29319. BIO_free(pub_bio);
  29320. BIO_free(bio);
  29321. bio = NULL;
  29322. EVP_PKEY_free(pkey);
  29323. pkey = NULL;
  29324. EVP_PKEY_free(pkey2);
  29325. }
  29326. #endif
  29327. /* key is DES encrypted */
  29328. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  29329. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  29330. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && \
  29331. !defined(HAVE_USER_RSA) && !defined(NO_RSA)
  29332. {
  29333. XFILE f = XBADFILE;
  29334. wc_pem_password_cb* passwd_cb = NULL;
  29335. void* passwd_cb_userdata;
  29336. SSL_CTX* ctx = NULL;
  29337. char passwd[] = "bad password";
  29338. #ifndef WOLFSSL_NO_TLS12
  29339. #ifndef NO_WOLFSSL_SERVER
  29340. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  29341. #else
  29342. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  29343. #endif
  29344. #else
  29345. #ifndef NO_WOLFSSL_SERVER
  29346. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  29347. #else
  29348. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  29349. #endif
  29350. #endif
  29351. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  29352. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  29353. ExpectNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  29354. ExpectNull(passwd_cb_userdata =
  29355. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  29356. /* fail case with password call back */
  29357. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  29358. (void*)passwd));
  29359. BIO_free(bio);
  29360. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  29361. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  29362. (void*)passwd));
  29363. BIO_free(bio);
  29364. ExpectTrue((f = XFOPEN("./certs/server-keyEnc.pem", "rb")) != XBADFILE);
  29365. ExpectNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  29366. if ((bio == NULL) && (f != XBADFILE)) {
  29367. XFCLOSE(f);
  29368. }
  29369. /* use callback that works */
  29370. ExpectNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  29371. (void*)"yassl123"));
  29372. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  29373. EVP_PKEY_free(pkey);
  29374. pkey = NULL;
  29375. BIO_free(bio);
  29376. bio = NULL;
  29377. SSL_CTX_free(ctx);
  29378. }
  29379. #endif /* !defined(NO_DES3) */
  29380. #endif /* !NO_BIO */
  29381. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  29382. {
  29383. unsigned char buf[2048];
  29384. size_t bytes = 0;
  29385. XFILE f = XBADFILE;
  29386. SSL_CTX* ctx = NULL;
  29387. #ifndef WOLFSSL_NO_TLS12
  29388. #ifndef NO_WOLFSSL_SERVER
  29389. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  29390. #else
  29391. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  29392. #endif
  29393. #else
  29394. #ifndef NO_WOLFSSL_SERVER
  29395. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  29396. #else
  29397. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  29398. #endif
  29399. #endif
  29400. ExpectTrue((f = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  29401. ExpectIntGT(bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f), 0);
  29402. if (f != XBADFILE)
  29403. XFCLOSE(f);
  29404. server_key = buf;
  29405. pkey = NULL;
  29406. ExpectNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  29407. ExpectNull(pkey);
  29408. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  29409. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  29410. EVP_PKEY_free(pkey);
  29411. pkey = NULL;
  29412. SSL_CTX_free(ctx);
  29413. server_key = NULL;
  29414. }
  29415. #endif
  29416. #ifndef NO_BIO
  29417. (void)bio;
  29418. #endif
  29419. (void)pkey;
  29420. (void)server_key;
  29421. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  29422. return EXPECT_RESULT();
  29423. }
  29424. static int test_wolfSSL_PEM_file_RSAKey(void)
  29425. {
  29426. EXPECT_DECLS;
  29427. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  29428. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  29429. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  29430. RSA* rsa = NULL;
  29431. XFILE fp = XBADFILE;
  29432. ExpectTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  29433. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  29434. if (fp != XBADFILE)
  29435. XFCLOSE(fp);
  29436. ExpectIntEQ(RSA_size(rsa), 256);
  29437. ExpectIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  29438. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
  29439. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
  29440. ExpectIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  29441. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  29442. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
  29443. RSA_free(rsa);
  29444. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  29445. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  29446. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  29447. return EXPECT_RESULT();
  29448. }
  29449. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  29450. {
  29451. EXPECT_DECLS;
  29452. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  29453. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && \
  29454. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  29455. RSA* rsa = NULL;
  29456. XFILE f = NULL;
  29457. ExpectTrue((f = XFOPEN(svrKeyFile, "r")) != XBADFILE);
  29458. ExpectNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  29459. ExpectIntEQ(RSA_size(rsa), 256);
  29460. if (f != XBADFILE) {
  29461. XFCLOSE(f);
  29462. f = XBADFILE;
  29463. }
  29464. ExpectIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  29465. NULL), WOLFSSL_FAILURE);
  29466. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  29467. NULL), WOLFSSL_FAILURE);
  29468. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
  29469. WOLFSSL_SUCCESS);
  29470. RSA_free(rsa);
  29471. #ifdef HAVE_ECC
  29472. ExpectTrue((f = XFOPEN(eccKeyFile, "r")) != XBADFILE);
  29473. ExpectNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  29474. if (f != XBADFILE)
  29475. XFCLOSE(f);
  29476. #endif /* HAVE_ECC */
  29477. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  29478. return EXPECT_RESULT();
  29479. }
  29480. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  29481. {
  29482. EXPECT_DECLS;
  29483. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29484. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29485. XFILE file = XBADFILE;
  29486. const char* fname = "./certs/client-keyPub.pem";
  29487. RSA *rsa = NULL;
  29488. ExpectNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  29489. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29490. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  29491. ExpectIntEQ(RSA_size(rsa), 256);
  29492. RSA_free(rsa);
  29493. if (file != XBADFILE)
  29494. XFCLOSE(file);
  29495. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  29496. return EXPECT_RESULT();
  29497. }
  29498. #ifndef NO_BIO
  29499. static int test_wolfSSL_PEM_bio_RSAKey(void)
  29500. {
  29501. EXPECT_DECLS;
  29502. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  29503. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  29504. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  29505. RSA* rsa = NULL;
  29506. BIO* bio = NULL;
  29507. /* PrivateKey */
  29508. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  29509. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  29510. ExpectNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  29511. ExpectNotNull(rsa);
  29512. ExpectIntEQ(RSA_size(rsa), 256);
  29513. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  29514. NULL), WOLFSSL_FAILURE);
  29515. BIO_free(bio);
  29516. bio = NULL;
  29517. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29518. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  29519. NULL), WOLFSSL_SUCCESS);
  29520. BIO_free(bio);
  29521. bio = NULL;
  29522. RSA_free(rsa);
  29523. rsa = NULL;
  29524. /* PUBKEY */
  29525. ExpectNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  29526. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  29527. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  29528. ExpectIntEQ(RSA_size(rsa), 256);
  29529. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  29530. BIO_free(bio);
  29531. bio = NULL;
  29532. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29533. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  29534. BIO_free(bio);
  29535. bio = NULL;
  29536. RSA_free(rsa);
  29537. rsa = NULL;
  29538. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  29539. ExpectNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  29540. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  29541. BIO_free(bio);
  29542. bio = NULL;
  29543. RSA_free(rsa);
  29544. rsa = NULL;
  29545. #ifdef HAVE_ECC
  29546. /* ensure that non-rsa keys do not work */
  29547. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  29548. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  29549. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  29550. BIO_free(bio);
  29551. bio = NULL;
  29552. RSA_free(rsa);
  29553. rsa = NULL;
  29554. #endif /* HAVE_ECC */
  29555. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  29556. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  29557. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  29558. return EXPECT_RESULT();
  29559. }
  29560. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  29561. {
  29562. EXPECT_DECLS;
  29563. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29564. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29565. RSA* rsa = NULL;
  29566. RSA* rsa_dup = NULL;
  29567. BIO* bio = NULL;
  29568. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  29569. ExpectNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  29570. ExpectIntEQ(RSA_size(rsa), 256);
  29571. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  29572. ExpectNull(rsa_dup = RSAPublicKey_dup(NULL));
  29573. /* Test duplicating empty key. */
  29574. ExpectNotNull(rsa_dup = RSA_new());
  29575. ExpectNull(RSAPublicKey_dup(rsa_dup));
  29576. RSA_free(rsa_dup);
  29577. rsa_dup = NULL;
  29578. ExpectNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  29579. ExpectPtrNE(rsa_dup, rsa);
  29580. #endif
  29581. /* test if valgrind complains about unreleased memory */
  29582. RSA_up_ref(rsa);
  29583. RSA_free(rsa);
  29584. BIO_free(bio);
  29585. bio = NULL;
  29586. RSA_free(rsa);
  29587. rsa = NULL;
  29588. RSA_free(rsa_dup);
  29589. rsa_dup = NULL;
  29590. #ifdef HAVE_ECC
  29591. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  29592. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  29593. BIO_free(bio);
  29594. #endif /* HAVE_ECC */
  29595. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  29596. return EXPECT_RESULT();
  29597. }
  29598. static int test_wolfSSL_PEM_bio_DSAKey(void)
  29599. {
  29600. EXPECT_DECLS;
  29601. #ifndef HAVE_SELFTEST
  29602. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  29603. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  29604. DSA* dsa = NULL;
  29605. BIO* bio = NULL;
  29606. /* PrivateKey */
  29607. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  29608. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  29609. ExpectNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  29610. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  29611. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  29612. NULL), WOLFSSL_FAILURE);
  29613. BIO_free(bio);
  29614. bio = NULL;
  29615. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29616. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL,
  29617. NULL), WOLFSSL_SUCCESS);
  29618. BIO_free(bio);
  29619. bio = NULL;
  29620. DSA_free(dsa);
  29621. dsa = NULL;
  29622. /* PUBKEY */
  29623. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  29624. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  29625. ExpectNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  29626. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  29627. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  29628. BIO_free(bio);
  29629. bio = NULL;
  29630. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29631. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  29632. BIO_free(bio);
  29633. bio = NULL;
  29634. DSA_free(dsa);
  29635. dsa = NULL;
  29636. #ifdef HAVE_ECC
  29637. /* ensure that non-dsa keys do not work */
  29638. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  29639. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  29640. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  29641. BIO_free(bio);
  29642. bio = NULL;
  29643. DSA_free(dsa);
  29644. dsa = NULL;
  29645. #endif /* HAVE_ECC */
  29646. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  29647. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  29648. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  29649. #endif /* HAVE_SELFTEST */
  29650. return EXPECT_RESULT();
  29651. }
  29652. static int test_wolfSSL_PEM_bio_ECKey(void)
  29653. {
  29654. EXPECT_DECLS;
  29655. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  29656. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  29657. EC_KEY* ec = NULL;
  29658. EC_KEY* ec2;
  29659. BIO* bio = NULL;
  29660. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  29661. unsigned char* pem = NULL;
  29662. int pLen;
  29663. #endif
  29664. static char ec_key_bad_1[] = "-----BEGIN PUBLIC KEY-----\n"
  29665. "MAA=\n"
  29666. "-----END PUBLIC KEY-----";
  29667. static char ec_priv_key_bad_1[] = "-----BEGIN EC PRIVATE KEY-----\n"
  29668. "MAA=\n"
  29669. "-----END EC PRIVATE KEY-----";
  29670. /* PrivateKey */
  29671. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  29672. ExpectNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  29673. ec2 = NULL;
  29674. ExpectNotNull((ec = PEM_read_bio_ECPrivateKey(bio, &ec2, NULL, NULL)));
  29675. ExpectIntEQ(ec == ec2, 1);
  29676. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  29677. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  29678. NULL), WOLFSSL_FAILURE);
  29679. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  29680. NULL), WOLFSSL_FAILURE);
  29681. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, ec, NULL, NULL, 0, NULL, NULL),
  29682. WOLFSSL_FAILURE);
  29683. BIO_free(bio);
  29684. bio = NULL;
  29685. /* Public key data - fail. */
  29686. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  29687. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  29688. BIO_free(bio);
  29689. bio = NULL;
  29690. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29691. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  29692. NULL), WOLFSSL_SUCCESS);
  29693. BIO_free(bio);
  29694. bio = NULL;
  29695. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, NULL, NULL, NULL, 0, NULL,
  29696. NULL),WOLFSSL_FAILURE);
  29697. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, NULL, NULL, NULL, 0, NULL, NULL),
  29698. WOLFSSL_FAILURE);
  29699. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, ec, NULL, NULL, 0, NULL, NULL),
  29700. WOLFSSL_FAILURE);
  29701. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, ec, NULL, NULL, 0, NULL, NULL),
  29702. WOLFSSL_SUCCESS);
  29703. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  29704. NULL), 0);
  29705. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  29706. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  29707. NULL), 0);
  29708. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  29709. NULL), 0);
  29710. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  29711. &pLen), 0);
  29712. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  29713. &pLen), 0);
  29714. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  29715. &pLen), 0);
  29716. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  29717. NULL), 0);
  29718. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  29719. &pLen), 1);
  29720. ExpectIntGT(pLen, 0);
  29721. XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29722. #endif
  29723. EC_KEY_free(ec);
  29724. ec = NULL;
  29725. /* PUBKEY */
  29726. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  29727. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  29728. ec2 = NULL;
  29729. ExpectNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, &ec2, NULL, NULL)));
  29730. ExpectIntEQ(ec == ec2, 1);
  29731. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  29732. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  29733. BIO_free(bio);
  29734. bio = NULL;
  29735. /* Test 0x30, 0x00 fails. */
  29736. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_key_bad_1,
  29737. sizeof(ec_key_bad_1)));
  29738. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  29739. BIO_free(bio);
  29740. bio = NULL;
  29741. /* Private key data - fail. */
  29742. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  29743. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  29744. BIO_free(bio);
  29745. bio = NULL;
  29746. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29747. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  29748. BIO_free(bio);
  29749. bio = NULL;
  29750. /* Same test as above, but with a file pointer rather than a BIO. */
  29751. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  29752. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  29753. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  29754. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
  29755. EC_KEY_free(ec);
  29756. ec = NULL;
  29757. #ifndef NO_RSA
  29758. /* ensure that non-ec keys do not work */
  29759. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  29760. ExpectNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  29761. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  29762. BIO_free(bio);
  29763. bio = NULL;
  29764. EC_KEY_free(ec);
  29765. ec = NULL;
  29766. #endif /* !NO_RSA */
  29767. /* Test 0x30, 0x00 fails. */
  29768. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_priv_key_bad_1,
  29769. sizeof(ec_priv_key_bad_1)));
  29770. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  29771. BIO_free(bio);
  29772. bio = NULL;
  29773. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  29774. return EXPECT_RESULT();
  29775. }
  29776. static int test_wolfSSL_PEM_PUBKEY(void)
  29777. {
  29778. EXPECT_DECLS;
  29779. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  29780. BIO* bio = NULL;
  29781. EVP_PKEY* pkey = NULL;
  29782. /* test creating new EVP_PKEY with bad arg */
  29783. ExpectNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  29784. /* test loading ECC key using BIO */
  29785. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  29786. {
  29787. XFILE file = XBADFILE;
  29788. const char* fname = "./certs/ecc-client-keyPub.pem";
  29789. size_t sz;
  29790. byte* buf = NULL;
  29791. EVP_PKEY* pkey2 = NULL;
  29792. EC_KEY* ec_key = NULL;
  29793. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29794. ExpectIntEQ(XFSEEK(file, 0, XSEEK_END), 0);
  29795. ExpectIntGT(sz = XFTELL(file), 0);
  29796. ExpectIntEQ(XFSEEK(file, 0, XSEEK_SET), 0);
  29797. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  29798. if (buf != NULL) {
  29799. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  29800. }
  29801. if (file != XBADFILE) {
  29802. XFCLOSE(file);
  29803. }
  29804. /* Test using BIO new mem and loading PEM private key */
  29805. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  29806. ExpectNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  29807. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  29808. BIO_free(bio);
  29809. bio = NULL;
  29810. /* Qt unit test case*/
  29811. ExpectNotNull(pkey2 = EVP_PKEY_new());
  29812. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  29813. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  29814. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  29815. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  29816. #else
  29817. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  29818. #endif
  29819. EC_KEY_free(ec_key);
  29820. EVP_PKEY_free(pkey2);
  29821. EVP_PKEY_free(pkey);
  29822. pkey = NULL;
  29823. }
  29824. #endif
  29825. (void)bio;
  29826. (void)pkey;
  29827. #endif
  29828. return EXPECT_RESULT();
  29829. }
  29830. #endif /* !NO_BIO */
  29831. static int test_DSA_do_sign_verify(void)
  29832. {
  29833. EXPECT_DECLS;
  29834. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  29835. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  29836. !defined(NO_DSA)
  29837. unsigned char digest[WC_SHA_DIGEST_SIZE];
  29838. DSA_SIG* sig = NULL;
  29839. DSA* dsa = NULL;
  29840. word32 bytes;
  29841. byte sigBin[DSA_SIG_SIZE];
  29842. int dsacheck;
  29843. #ifdef USE_CERT_BUFFERS_1024
  29844. byte tmp[ONEK_BUF];
  29845. XMEMSET(tmp, 0, sizeof(tmp));
  29846. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  29847. bytes = sizeof_dsa_key_der_1024;
  29848. #elif defined(USE_CERT_BUFFERS_2048)
  29849. byte tmp[TWOK_BUF];
  29850. XMEMSET(tmp, 0, sizeof(tmp));
  29851. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  29852. bytes = sizeof_dsa_key_der_2048;
  29853. #else
  29854. byte tmp[TWOK_BUF];
  29855. XFILE fp = XBADFILE;
  29856. XMEMSET(tmp, 0, sizeof(tmp));
  29857. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb") != XBADFILE);
  29858. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  29859. if (fp != XBADFILE)
  29860. XFCLOSE(fp);
  29861. #endif /* END USE_CERT_BUFFERS_1024 */
  29862. XMEMSET(digest, 202, sizeof(digest));
  29863. ExpectNotNull(dsa = DSA_new());
  29864. ExpectIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  29865. ExpectIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  29866. ExpectIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  29867. ExpectNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  29868. ExpectIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  29869. DSA_SIG_free(sig);
  29870. DSA_free(dsa);
  29871. #endif
  29872. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  29873. return EXPECT_RESULT();
  29874. }
  29875. static int test_wolfSSL_tmp_dh(void)
  29876. {
  29877. EXPECT_DECLS;
  29878. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29879. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  29880. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  29881. byte buff[6000];
  29882. char file[] = "./certs/dsaparams.pem";
  29883. XFILE f = XBADFILE;
  29884. int bytes = 0;
  29885. DSA* dsa = NULL;
  29886. DH* dh = NULL;
  29887. #if defined(WOLFSSL_DH_EXTRA) && \
  29888. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  29889. DH* dh2 = NULL;
  29890. #endif
  29891. BIO* bio = NULL;
  29892. SSL* ssl = NULL;
  29893. SSL_CTX* ctx = NULL;
  29894. #ifndef NO_WOLFSSL_SERVER
  29895. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29896. #else
  29897. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29898. #endif
  29899. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  29900. WOLFSSL_FILETYPE_PEM));
  29901. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  29902. WOLFSSL_FILETYPE_PEM));
  29903. ExpectNotNull(ssl = SSL_new(ctx));
  29904. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  29905. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  29906. if (f != XBADFILE)
  29907. XFCLOSE(f);
  29908. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  29909. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  29910. ExpectNotNull(dsa);
  29911. dh = wolfSSL_DSA_dup_DH(dsa);
  29912. ExpectNotNull(dh);
  29913. #if defined(WOLFSSL_DH_EXTRA) && \
  29914. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  29915. ExpectNotNull(dh2 = wolfSSL_DH_dup(dh));
  29916. #endif
  29917. ExpectIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  29918. #ifndef NO_WOLFSSL_SERVER
  29919. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  29920. #else
  29921. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  29922. #endif
  29923. BIO_free(bio);
  29924. DSA_free(dsa);
  29925. DH_free(dh);
  29926. #if defined(WOLFSSL_DH_EXTRA) && \
  29927. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  29928. DH_free(dh2);
  29929. #endif
  29930. SSL_free(ssl);
  29931. SSL_CTX_free(ctx);
  29932. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  29933. #endif
  29934. return EXPECT_RESULT();
  29935. }
  29936. static int test_wolfSSL_ctrl(void)
  29937. {
  29938. EXPECT_DECLS;
  29939. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  29940. byte buff[6000];
  29941. BIO* bio = NULL;
  29942. int bytes;
  29943. BUF_MEM* ptr = NULL;
  29944. XMEMSET(buff, 0, sizeof(buff));
  29945. bytes = sizeof(buff);
  29946. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  29947. ExpectNotNull(BIO_s_socket());
  29948. ExpectIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  29949. /* needs tested after stubs filled out @TODO
  29950. SSL_ctrl
  29951. SSL_CTX_ctrl
  29952. */
  29953. BIO_free(bio);
  29954. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  29955. return EXPECT_RESULT();
  29956. }
  29957. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  29958. {
  29959. EXPECT_DECLS;
  29960. #ifdef OPENSSL_EXTRA
  29961. static const unsigned char pw[] = "password";
  29962. static const int pwSz = sizeof(pw) - 1;
  29963. size_t checkPwSz = 0;
  29964. const unsigned char* checkPw = NULL;
  29965. WOLFSSL_EVP_PKEY* key = NULL;
  29966. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  29967. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  29968. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  29969. pwSz));
  29970. if (key != NULL) {
  29971. ExpectIntEQ(key->type, EVP_PKEY_HMAC);
  29972. ExpectIntEQ(key->save_type, EVP_PKEY_HMAC);
  29973. ExpectIntEQ(key->pkey_sz, pwSz);
  29974. ExpectIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  29975. }
  29976. ExpectNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  29977. ExpectIntEQ((int)checkPwSz, pwSz);
  29978. ExpectIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  29979. wolfSSL_EVP_PKEY_free(key);
  29980. key = NULL;
  29981. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  29982. 0));
  29983. ExpectIntEQ(key->pkey_sz, 0);
  29984. if (EXPECT_SUCCESS()) {
  29985. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  29986. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  29987. }
  29988. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  29989. ExpectIntEQ((int)checkPwSz, 0);
  29990. wolfSSL_EVP_PKEY_free(key);
  29991. key = NULL;
  29992. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL,
  29993. 0));
  29994. ExpectIntEQ(key->pkey_sz, 0);
  29995. if (EXPECT_SUCCESS()) {
  29996. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  29997. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  29998. }
  29999. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  30000. ExpectIntEQ((int)checkPwSz, 0);
  30001. wolfSSL_EVP_PKEY_free(key);
  30002. key = NULL;
  30003. #endif /* OPENSSL_EXTRA */
  30004. return EXPECT_RESULT();
  30005. }
  30006. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  30007. {
  30008. EXPECT_DECLS;
  30009. #ifdef OPENSSL_EXTRA
  30010. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  30011. const char *priv = "ABCDEFGHIJKLMNOP";
  30012. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  30013. WOLFSSL_EVP_PKEY* key = NULL;
  30014. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  30015. NULL, NULL, AES_128_KEY_SIZE, cipher));
  30016. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  30017. NULL, (const unsigned char *)priv, 0, cipher));
  30018. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  30019. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  30020. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  30021. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  30022. wolfSSL_EVP_PKEY_free(key);
  30023. #endif /* WOLFSSL_CMAC && !NO_AES && WOLFSSL_AES_DIRECT */
  30024. #endif /* OPENSSL_EXTRA */
  30025. return EXPECT_RESULT();
  30026. }
  30027. static int test_wolfSSL_EVP_Digest(void)
  30028. {
  30029. EXPECT_DECLS;
  30030. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  30031. const char* in = "abc";
  30032. int inLen = (int)XSTRLEN(in);
  30033. byte out[WC_SHA256_DIGEST_SIZE];
  30034. unsigned int outLen;
  30035. const char* expOut =
  30036. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  30037. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  30038. "\x15\xAD";
  30039. ExpectIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen,
  30040. "SHA256", NULL), 1);
  30041. ExpectIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  30042. ExpectIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  30043. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  30044. return EXPECT_RESULT();
  30045. }
  30046. static int test_wolfSSL_EVP_Digest_all(void)
  30047. {
  30048. EXPECT_DECLS;
  30049. #ifdef OPENSSL_EXTRA
  30050. const char* digests[] = {
  30051. #ifndef NO_MD5
  30052. "MD5",
  30053. #endif
  30054. #ifndef NO_SHA
  30055. "SHA",
  30056. #endif
  30057. #ifdef WOLFSSL_SHA224
  30058. "SHA224",
  30059. #endif
  30060. #ifndef NO_SHA256
  30061. "SHA256",
  30062. #endif
  30063. #ifdef WOLFSSL_SHA384
  30064. "SHA384",
  30065. #endif
  30066. #ifdef WOLFSSL_SHA512
  30067. "SHA512",
  30068. #endif
  30069. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  30070. "SHA512_224",
  30071. #endif
  30072. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  30073. "SHA512_256",
  30074. #endif
  30075. #ifdef WOLFSSL_SHA3
  30076. #ifndef WOLFSSL_NOSHA3_224
  30077. "SHA3_224",
  30078. #endif
  30079. #ifndef WOLFSSL_NOSHA3_256
  30080. "SHA3_256",
  30081. #endif
  30082. "SHA3_384",
  30083. #ifndef WOLFSSL_NOSHA3_512
  30084. "SHA3_512",
  30085. #endif
  30086. #endif /* WOLFSSL_SHA3 */
  30087. NULL
  30088. };
  30089. const char** d;
  30090. const unsigned char in[] = "abc";
  30091. int inLen = XSTR_SIZEOF(in);
  30092. byte out[WC_MAX_DIGEST_SIZE];
  30093. unsigned int outLen;
  30094. for (d = digests; *d != NULL; d++) {
  30095. ExpectIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  30096. ExpectIntGT(outLen, 0);
  30097. ExpectIntEQ(EVP_MD_size(*d), outLen);
  30098. }
  30099. #endif
  30100. return EXPECT_RESULT();
  30101. }
  30102. static int test_wolfSSL_EVP_MD_size(void)
  30103. {
  30104. EXPECT_DECLS;
  30105. #ifdef OPENSSL_EXTRA
  30106. WOLFSSL_EVP_MD_CTX mdCtx;
  30107. #ifdef WOLFSSL_SHA3
  30108. #ifndef WOLFSSL_NOSHA3_224
  30109. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30110. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  30111. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  30112. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  30113. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30114. #endif
  30115. #ifndef WOLFSSL_NOSHA3_256
  30116. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30117. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  30118. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  30119. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  30120. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30121. #endif
  30122. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30123. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  30124. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  30125. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  30126. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30127. #ifndef WOLFSSL_NOSHA3_512
  30128. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30129. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  30130. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  30131. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  30132. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30133. #endif
  30134. #endif /* WOLFSSL_SHA3 */
  30135. #ifndef NO_SHA256
  30136. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30137. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  30138. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30139. WC_SHA256_DIGEST_SIZE);
  30140. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30141. WC_SHA256_BLOCK_SIZE);
  30142. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  30143. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  30144. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30145. #endif
  30146. #ifndef NO_MD5
  30147. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30148. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  30149. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30150. WC_MD5_DIGEST_SIZE);
  30151. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30152. WC_MD5_BLOCK_SIZE);
  30153. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  30154. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  30155. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30156. #endif
  30157. #ifdef WOLFSSL_SHA224
  30158. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30159. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  30160. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30161. WC_SHA224_DIGEST_SIZE);
  30162. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30163. WC_SHA224_BLOCK_SIZE);
  30164. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  30165. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  30166. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30167. #endif
  30168. #ifdef WOLFSSL_SHA384
  30169. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30170. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  30171. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30172. WC_SHA384_DIGEST_SIZE);
  30173. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30174. WC_SHA384_BLOCK_SIZE);
  30175. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  30176. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  30177. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30178. #endif
  30179. #ifdef WOLFSSL_SHA512
  30180. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30181. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  30182. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30183. WC_SHA512_DIGEST_SIZE);
  30184. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30185. WC_SHA512_BLOCK_SIZE);
  30186. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  30187. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  30188. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30189. #endif
  30190. #ifndef NO_SHA
  30191. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30192. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  30193. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30194. WC_SHA_DIGEST_SIZE);
  30195. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30196. WC_SHA_BLOCK_SIZE);
  30197. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  30198. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  30199. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30200. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30201. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  30202. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30203. WC_SHA_DIGEST_SIZE);
  30204. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30205. WC_SHA_BLOCK_SIZE);
  30206. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  30207. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  30208. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30209. #endif
  30210. /* error case */
  30211. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30212. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  30213. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30214. BAD_FUNC_ARG);
  30215. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  30216. /* Cleanup is valid on uninit'ed struct */
  30217. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30218. #endif /* OPENSSL_EXTRA */
  30219. return EXPECT_RESULT();
  30220. }
  30221. static int test_wolfSSL_EVP_MD_pkey_type(void)
  30222. {
  30223. EXPECT_DECLS;
  30224. #ifdef OPENSSL_EXTRA
  30225. const WOLFSSL_EVP_MD* md;
  30226. #ifndef NO_MD5
  30227. ExpectNotNull(md = EVP_md5());
  30228. ExpectIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  30229. #endif
  30230. #ifndef NO_SHA
  30231. ExpectNotNull(md = EVP_sha1());
  30232. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  30233. #endif
  30234. #ifdef WOLFSSL_SHA224
  30235. ExpectNotNull(md = EVP_sha224());
  30236. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  30237. #endif
  30238. ExpectNotNull(md = EVP_sha256());
  30239. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  30240. #ifdef WOLFSSL_SHA384
  30241. ExpectNotNull(md = EVP_sha384());
  30242. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  30243. #endif
  30244. #ifdef WOLFSSL_SHA512
  30245. ExpectNotNull(md = EVP_sha512());
  30246. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  30247. #endif
  30248. #endif
  30249. return EXPECT_RESULT();
  30250. }
  30251. #ifdef OPENSSL_EXTRA
  30252. static int test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  30253. size_t testKeySz, const char* testData, size_t testDataSz,
  30254. const byte* testResult, size_t testResultSz)
  30255. {
  30256. EXPECT_DECLS;
  30257. unsigned char check[WC_MAX_DIGEST_SIZE];
  30258. size_t checkSz = -1;
  30259. WOLFSSL_EVP_PKEY* key = NULL;
  30260. WOLFSSL_EVP_MD_CTX mdCtx;
  30261. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  30262. testKey, (int)testKeySz));
  30263. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30264. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  30265. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  30266. (unsigned int)testDataSz), 1);
  30267. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30268. ExpectIntEQ((int)checkSz, (int)testResultSz);
  30269. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30270. ExpectIntEQ((int)checkSz,(int)testResultSz);
  30271. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  30272. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30273. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  30274. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  30275. (unsigned int)testDataSz), 1);
  30276. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  30277. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30278. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30279. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  30280. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  30281. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30282. ExpectIntEQ((int)checkSz, (int)testResultSz);
  30283. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30284. ExpectIntEQ((int)checkSz,(int)testResultSz);
  30285. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  30286. (unsigned int)testDataSz - 4), 1);
  30287. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30288. ExpectIntEQ((int)checkSz,(int)testResultSz);
  30289. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  30290. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30291. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  30292. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  30293. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  30294. (unsigned int)testDataSz - 4), 1);
  30295. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  30296. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30297. wolfSSL_EVP_PKEY_free(key);
  30298. return EXPECT_RESULT();
  30299. }
  30300. #endif
  30301. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  30302. {
  30303. EXPECT_DECLS;
  30304. #ifdef OPENSSL_EXTRA
  30305. static const unsigned char testKey[] =
  30306. {
  30307. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  30308. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  30309. 0x0b, 0x0b, 0x0b, 0x0b
  30310. };
  30311. static const char testData[] = "Hi There";
  30312. #ifdef WOLFSSL_SHA224
  30313. static const unsigned char testResultSha224[] =
  30314. {
  30315. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  30316. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  30317. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  30318. 0x53, 0x68, 0x4b, 0x22
  30319. };
  30320. #endif
  30321. #ifndef NO_SHA256
  30322. static const unsigned char testResultSha256[] =
  30323. {
  30324. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  30325. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  30326. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  30327. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  30328. };
  30329. #endif
  30330. #ifdef WOLFSSL_SHA384
  30331. static const unsigned char testResultSha384[] =
  30332. {
  30333. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  30334. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  30335. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  30336. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  30337. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  30338. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  30339. };
  30340. #endif
  30341. #ifdef WOLFSSL_SHA512
  30342. static const unsigned char testResultSha512[] =
  30343. {
  30344. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  30345. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  30346. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  30347. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  30348. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  30349. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  30350. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  30351. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  30352. };
  30353. #endif
  30354. #ifdef WOLFSSL_SHA3
  30355. #ifndef WOLFSSL_NOSHA3_224
  30356. static const unsigned char testResultSha3_224[] =
  30357. {
  30358. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  30359. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  30360. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  30361. 0xf3, 0xc8, 0x60, 0xf7
  30362. };
  30363. #endif
  30364. #ifndef WOLFSSL_NOSHA3_256
  30365. static const unsigned char testResultSha3_256[] =
  30366. {
  30367. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  30368. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  30369. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  30370. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  30371. };
  30372. #endif
  30373. #ifndef WOLFSSL_NOSHA3_384
  30374. static const unsigned char testResultSha3_384[] =
  30375. {
  30376. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  30377. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  30378. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  30379. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  30380. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  30381. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  30382. };
  30383. #endif
  30384. #ifndef WOLFSSL_NOSHA3_512
  30385. static const unsigned char testResultSha3_512[] =
  30386. {
  30387. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  30388. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  30389. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  30390. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  30391. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  30392. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  30393. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  30394. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  30395. };
  30396. #endif
  30397. #endif
  30398. #ifndef NO_SHA256
  30399. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha256(), testKey,
  30400. sizeof(testKey), testData, XSTRLEN(testData), testResultSha256,
  30401. sizeof(testResultSha256)), TEST_SUCCESS);
  30402. #endif
  30403. #ifdef WOLFSSL_SHA224
  30404. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha224(), testKey,
  30405. sizeof(testKey), testData, XSTRLEN(testData), testResultSha224,
  30406. sizeof(testResultSha224)), TEST_SUCCESS);
  30407. #endif
  30408. #ifdef WOLFSSL_SHA384
  30409. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha384(), testKey,
  30410. sizeof(testKey), testData, XSTRLEN(testData), testResultSha384,
  30411. sizeof(testResultSha384)), TEST_SUCCESS);
  30412. #endif
  30413. #ifdef WOLFSSL_SHA512
  30414. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha512(), testKey,
  30415. sizeof(testKey), testData, XSTRLEN(testData), testResultSha512,
  30416. sizeof(testResultSha512)), TEST_SUCCESS);
  30417. #endif
  30418. #ifdef WOLFSSL_SHA3
  30419. #ifndef WOLFSSL_NOSHA3_224
  30420. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey,
  30421. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_224,
  30422. sizeof(testResultSha3_224)), TEST_SUCCESS);
  30423. #endif
  30424. #ifndef WOLFSSL_NOSHA3_256
  30425. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey,
  30426. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_256,
  30427. sizeof(testResultSha3_256)), TEST_SUCCESS);
  30428. #endif
  30429. #ifndef WOLFSSL_NOSHA3_384
  30430. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey,
  30431. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_384,
  30432. sizeof(testResultSha3_384)), TEST_SUCCESS);
  30433. #endif
  30434. #ifndef WOLFSSL_NOSHA3_512
  30435. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey,
  30436. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_512,
  30437. sizeof(testResultSha3_512)), TEST_SUCCESS);
  30438. #endif
  30439. #endif
  30440. #endif /* OPENSSL_EXTRA */
  30441. return EXPECT_RESULT();
  30442. }
  30443. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  30444. {
  30445. EXPECT_DECLS;
  30446. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  30447. defined(USE_CERT_BUFFERS_2048)
  30448. WOLFSSL_EVP_PKEY* privKey = NULL;
  30449. WOLFSSL_EVP_PKEY* pubKey = NULL;
  30450. WOLFSSL_EVP_PKEY_CTX* keyCtx = NULL;
  30451. const char testData[] = "Hi There";
  30452. WOLFSSL_EVP_MD_CTX mdCtx;
  30453. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  30454. int ret;
  30455. size_t checkSz = -1;
  30456. int sz = 2048 / 8;
  30457. const unsigned char* cp;
  30458. const unsigned char* p;
  30459. unsigned char check[2048/8];
  30460. size_t i;
  30461. int paddings[] = {
  30462. RSA_PKCS1_PADDING,
  30463. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  30464. RSA_PKCS1_PSS_PADDING,
  30465. #endif
  30466. };
  30467. cp = client_key_der_2048;
  30468. ExpectNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  30469. sizeof_client_key_der_2048)));
  30470. p = client_keypub_der_2048;
  30471. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  30472. sizeof_client_keypub_der_2048)));
  30473. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30474. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  30475. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30476. NULL, privKey), 1);
  30477. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  30478. (unsigned int)XSTRLEN(testData)), 1);
  30479. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30480. ExpectIntEQ((int)checkSz, sz);
  30481. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30482. ExpectIntEQ((int)checkSz,sz);
  30483. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  30484. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  30485. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy);
  30486. ExpectIntEQ(ret, 1);
  30487. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30488. ExpectIntEQ(ret, 1);
  30489. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30490. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30491. NULL, pubKey), 1);
  30492. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  30493. (unsigned int)XSTRLEN(testData)),
  30494. 1);
  30495. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  30496. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30497. ExpectIntEQ(ret, 1);
  30498. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30499. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30500. NULL, privKey), 1);
  30501. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  30502. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30503. ExpectIntEQ((int)checkSz, sz);
  30504. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30505. ExpectIntEQ((int)checkSz, sz);
  30506. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  30507. (unsigned int)XSTRLEN(testData) - 4), 1);
  30508. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30509. ExpectIntEQ((int)checkSz, sz);
  30510. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30511. ExpectIntEQ(ret, 1);
  30512. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30513. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30514. NULL, pubKey), 1);
  30515. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  30516. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  30517. (unsigned int)XSTRLEN(testData) - 4),
  30518. 1);
  30519. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  30520. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30521. ExpectIntEQ(ret, 1);
  30522. /* Check all signing padding types */
  30523. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  30524. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30525. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  30526. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  30527. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  30528. paddings[i]), 1);
  30529. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  30530. (unsigned int)XSTRLEN(testData)), 1);
  30531. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30532. ExpectIntEQ((int)checkSz, sz);
  30533. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30534. ExpectIntEQ((int)checkSz,sz);
  30535. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30536. ExpectIntEQ(ret, 1);
  30537. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30538. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  30539. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  30540. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  30541. paddings[i]), 1);
  30542. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  30543. (unsigned int)XSTRLEN(testData)), 1);
  30544. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  30545. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30546. ExpectIntEQ(ret, 1);
  30547. }
  30548. wolfSSL_EVP_PKEY_free(pubKey);
  30549. wolfSSL_EVP_PKEY_free(privKey);
  30550. #endif
  30551. return EXPECT_RESULT();
  30552. }
  30553. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  30554. {
  30555. EXPECT_DECLS;
  30556. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  30557. WOLFSSL_EVP_PKEY* privKey = NULL;
  30558. WOLFSSL_EVP_PKEY* pubKey = NULL;
  30559. const char testData[] = "Hi There";
  30560. WOLFSSL_EVP_MD_CTX mdCtx;
  30561. int ret;
  30562. size_t checkSz = -1;
  30563. const unsigned char* cp;
  30564. const unsigned char* p;
  30565. unsigned char check[2048/8];
  30566. cp = ecc_clikey_der_256;
  30567. ExpectNotNull(privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  30568. sizeof_ecc_clikey_der_256));
  30569. p = ecc_clikeypub_der_256;
  30570. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  30571. sizeof_ecc_clikeypub_der_256)));
  30572. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30573. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30574. NULL, privKey), 1);
  30575. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  30576. (unsigned int)XSTRLEN(testData)), 1);
  30577. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30578. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30579. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30580. ExpectIntEQ(ret, 1);
  30581. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30582. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30583. NULL, pubKey), 1);
  30584. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  30585. (unsigned int)XSTRLEN(testData)),
  30586. 1);
  30587. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  30588. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30589. ExpectIntEQ(ret, 1);
  30590. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30591. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30592. NULL, privKey), 1);
  30593. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  30594. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30595. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30596. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  30597. (unsigned int)XSTRLEN(testData) - 4), 1);
  30598. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30599. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30600. ExpectIntEQ(ret, 1);
  30601. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30602. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30603. NULL, pubKey), 1);
  30604. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  30605. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  30606. (unsigned int)XSTRLEN(testData) - 4),
  30607. 1);
  30608. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  30609. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30610. ExpectIntEQ(ret, 1);
  30611. wolfSSL_EVP_PKEY_free(pubKey);
  30612. wolfSSL_EVP_PKEY_free(privKey);
  30613. #endif
  30614. return EXPECT_RESULT();
  30615. }
  30616. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  30617. {
  30618. EXPECT_DECLS;
  30619. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30620. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  30621. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30622. char caFile[] = "./certs/client-ca.pem";
  30623. char clientFile[] = "./certs/client-cert.pem";
  30624. SSL_CTX* ctx = NULL;
  30625. X509* x509 = NULL;
  30626. BIO *bio = NULL;
  30627. X509 *cert = NULL;
  30628. X509 *ca = NULL;
  30629. STACK_OF(X509) *chain = NULL;
  30630. STACK_OF(X509) *chain2 = NULL;
  30631. #ifndef NO_WOLFSSL_SERVER
  30632. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30633. #else
  30634. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30635. #endif
  30636. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  30637. WOLFSSL_FILETYPE_PEM));
  30638. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  30639. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  30640. WOLFSSL_FILETYPE_PEM));
  30641. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  30642. /* additional test of getting EVP_PKEY key size from X509
  30643. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  30644. * allowed with user RSA */
  30645. {
  30646. EVP_PKEY* pkey = NULL;
  30647. #if defined(HAVE_ECC)
  30648. X509* ecX509 = NULL;
  30649. #endif /* HAVE_ECC */
  30650. ExpectNotNull(pkey = X509_get_pubkey(x509));
  30651. /* current RSA key is 2048 bit (256 bytes) */
  30652. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  30653. EVP_PKEY_free(pkey);
  30654. pkey = NULL;
  30655. #if defined(HAVE_ECC)
  30656. #if defined(USE_CERT_BUFFERS_256)
  30657. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  30658. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  30659. SSL_FILETYPE_ASN1));
  30660. #else
  30661. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  30662. cliEccCertFile, SSL_FILETYPE_PEM));
  30663. #endif
  30664. pkey = X509_get_pubkey(ecX509);
  30665. ExpectNotNull(pkey);
  30666. /* current ECC key is 256 bit (32 bytes) */
  30667. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  30668. X509_free(ecX509);
  30669. ecX509 = NULL;
  30670. EVP_PKEY_free(pkey);
  30671. pkey = NULL;
  30672. #endif /* HAVE_ECC */
  30673. }
  30674. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  30675. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  30676. if (EXPECT_SUCCESS()) {
  30677. x509 = NULL;
  30678. }
  30679. #ifdef WOLFSSL_ENCRYPTED_KEYS
  30680. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  30681. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  30682. #endif
  30683. SSL_CTX_free(ctx);
  30684. ctx = NULL;
  30685. #ifndef NO_WOLFSSL_SERVER
  30686. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30687. #else
  30688. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30689. #endif
  30690. /* Test haproxy use case */
  30691. ExpectNotNull(bio = BIO_new_file(svrCertFile, "r"));
  30692. /* Read Certificate */
  30693. ExpectNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  30694. ExpectNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  30695. ExpectNotNull(chain = sk_X509_new_null());
  30696. ExpectIntEQ(sk_X509_push(chain, ca), 1);
  30697. if (EXPECT_SUCCESS()) {
  30698. ca = NULL;
  30699. }
  30700. ExpectNotNull(chain2 = X509_chain_up_ref(chain));
  30701. ExpectNotNull(ca = sk_X509_shift(chain2));
  30702. ExpectIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  30703. ExpectIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  30704. if (EXPECT_SUCCESS()) {
  30705. ca = NULL;
  30706. }
  30707. BIO_free(bio);
  30708. X509_free(cert);
  30709. X509_free(ca);
  30710. X509_free(x509);
  30711. sk_X509_pop_free(chain, X509_free);
  30712. sk_X509_pop_free(chain2, X509_free);
  30713. SSL_CTX_free(ctx);
  30714. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30715. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30716. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  30717. return EXPECT_RESULT();
  30718. }
  30719. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  30720. static int test_wolfSSL_ERR_peek_last_error_line(void)
  30721. {
  30722. EXPECT_DECLS;
  30723. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30724. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  30725. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  30726. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  30727. callback_functions client_cb;
  30728. callback_functions server_cb;
  30729. int line = 0;
  30730. int flag = ERR_TXT_STRING;
  30731. const char* file = NULL;
  30732. const char* data = NULL;
  30733. /* create a failed connection and inspect the error */
  30734. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  30735. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  30736. client_cb.method = wolfTLSv1_1_client_method;
  30737. server_cb.method = wolfTLSv1_2_server_method;
  30738. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  30739. ExpectIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  30740. ExpectNotNull(data);
  30741. /* check clearing error state */
  30742. ERR_remove_state(0);
  30743. ExpectIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  30744. ERR_peek_last_error_line(NULL, &line);
  30745. ExpectIntEQ(line, 0);
  30746. ERR_peek_last_error_line(&file, NULL);
  30747. ExpectNull(file);
  30748. /* retry connection to fill error queue */
  30749. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  30750. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  30751. client_cb.method = wolfTLSv1_1_client_method;
  30752. server_cb.method = wolfTLSv1_2_server_method;
  30753. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  30754. /* check that error code was stored */
  30755. ExpectIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  30756. ERR_peek_last_error_line(NULL, &line);
  30757. ExpectIntNE(line, 0);
  30758. ERR_peek_last_error_line(&file, NULL);
  30759. ExpectNotNull(file);
  30760. fprintf(stderr, "\nTesting error print out\n");
  30761. ERR_print_errors_fp(stderr);
  30762. fprintf(stderr, "Done testing print out\n\n");
  30763. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  30764. * !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  30765. return EXPECT_RESULT();
  30766. }
  30767. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  30768. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30769. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30770. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  30771. {
  30772. (void) ok;
  30773. (void) ctx;
  30774. fprintf(stderr, "ENTER verify_cb\n");
  30775. return SSL_SUCCESS;
  30776. }
  30777. #endif
  30778. static int test_wolfSSL_X509_Name_canon(void)
  30779. {
  30780. EXPECT_DECLS;
  30781. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  30782. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  30783. defined(WOLFSSL_CERT_GEN) && \
  30784. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  30785. const long ex_hash1 = 0x0fdb2da4;
  30786. const long ex_hash2 = 0x9f3e8c9e;
  30787. X509_NAME *name = NULL;
  30788. X509 *x509 = NULL;
  30789. XFILE file = XBADFILE;
  30790. unsigned long hash = 0;
  30791. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  30792. byte *pbuf = NULL;
  30793. word32 len = 0;
  30794. (void) ex_hash2;
  30795. ExpectTrue((file = XFOPEN(caCertFile, "rb")) != XBADFILE);
  30796. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  30797. ExpectNotNull(name = X509_get_issuer_name(x509));
  30798. /* When output buffer is NULL, should return necessary output buffer
  30799. * length.*/
  30800. ExpectIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  30801. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  30802. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  30803. hash = (((unsigned long)digest[3] << 24) |
  30804. ((unsigned long)digest[2] << 16) |
  30805. ((unsigned long)digest[1] << 8) |
  30806. ((unsigned long)digest[0]));
  30807. ExpectIntEQ(hash, ex_hash1);
  30808. if (file != XBADFILE) {
  30809. XFCLOSE(file);
  30810. file = XBADFILE;
  30811. }
  30812. X509_free(x509);
  30813. x509 = NULL;
  30814. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  30815. pbuf = NULL;
  30816. ExpectTrue((file = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  30817. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  30818. ExpectNotNull(name = X509_get_issuer_name(x509));
  30819. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  30820. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  30821. hash = (((unsigned long)digest[3] << 24) |
  30822. ((unsigned long)digest[2] << 16) |
  30823. ((unsigned long)digest[1] << 8) |
  30824. ((unsigned long)digest[0]));
  30825. ExpectIntEQ(hash, ex_hash2);
  30826. if (file != XBADFILE)
  30827. XFCLOSE(file);
  30828. X509_free(x509);
  30829. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  30830. #endif
  30831. return EXPECT_RESULT();
  30832. }
  30833. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  30834. {
  30835. EXPECT_DECLS;
  30836. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  30837. const int MAX_DIR = 4;
  30838. const char paths[][32] = {
  30839. "./certs/ed25519",
  30840. "./certs/ecc",
  30841. "./certs/crl",
  30842. "./certs/",
  30843. };
  30844. char CertCrl_path[MAX_FILENAME_SZ];
  30845. char *p;
  30846. X509_STORE* str = NULL;
  30847. X509_LOOKUP* lookup = NULL;
  30848. WOLFSSL_STACK* sk = NULL;
  30849. int len, total_len, i;
  30850. (void)sk;
  30851. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  30852. /* illegal string */
  30853. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  30854. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  30855. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  30856. SSL_FILETYPE_PEM,NULL), 0);
  30857. /* free store */
  30858. X509_STORE_free(str);
  30859. str = NULL;
  30860. /* short folder string */
  30861. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  30862. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  30863. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  30864. SSL_FILETYPE_PEM,NULL), 1);
  30865. #if defined(WOLFSSL_INT_H)
  30866. /* only available when including internal.h */
  30867. ExpectNotNull(sk = lookup->dirs->dir_entry);
  30868. #endif
  30869. /* free store */
  30870. X509_STORE_free(str);
  30871. str = NULL;
  30872. /* typical function check */
  30873. p = &CertCrl_path[0];
  30874. total_len = 0;
  30875. for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  30876. len = (int)XSTRLEN((const char*)&paths[i]);
  30877. total_len += len;
  30878. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  30879. p += len;
  30880. if (i != 0) *(p++) = SEPARATOR_CHAR;
  30881. }
  30882. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  30883. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  30884. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  30885. SSL_FILETYPE_PEM,NULL), 1);
  30886. #if defined(WOLFSSL_INT_H)
  30887. /* only available when including internal.h */
  30888. ExpectNotNull(sk = lookup->dirs->dir_entry);
  30889. #endif
  30890. X509_STORE_free(str);
  30891. #endif
  30892. return EXPECT_RESULT();
  30893. }
  30894. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  30895. {
  30896. EXPECT_DECLS;
  30897. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  30898. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  30899. defined(WOLFSSL_SIGNER_DER_CERT)
  30900. X509_STORE_CTX* ctx = NULL;
  30901. X509_STORE* str = NULL;
  30902. X509_LOOKUP* lookup = NULL;
  30903. X509* cert1 = NULL;
  30904. X509* x509Ca = NULL;
  30905. X509* x509Svr = NULL;
  30906. X509* issuer = NULL;
  30907. WOLFSSL_STACK* sk = NULL;
  30908. X509_NAME* caName = NULL;
  30909. X509_NAME* issuerName = NULL;
  30910. XFILE file1 = XBADFILE;
  30911. int i;
  30912. int cert_count = 0;
  30913. int cmp;
  30914. char der[] = "certs/ca-cert.der";
  30915. #ifdef HAVE_CRL
  30916. char pem[][100] = {
  30917. "./certs/crl/crl.pem",
  30918. "./certs/crl/crl2.pem",
  30919. "./certs/crl/caEccCrl.pem",
  30920. "./certs/crl/eccCliCRL.pem",
  30921. "./certs/crl/eccSrvCRL.pem",
  30922. ""
  30923. };
  30924. #endif
  30925. ExpectTrue((file1 = XFOPEN("./certs/ca-cert.pem", "rb")) != XBADFILE);
  30926. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  30927. if (file1 != XBADFILE)
  30928. XFCLOSE(file1);
  30929. ExpectNotNull(ctx = X509_STORE_CTX_new());
  30930. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  30931. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  30932. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  30933. SSL_FILETYPE_PEM,NULL), 1);
  30934. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  30935. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  30936. /* check if CA cert is loaded into the store */
  30937. for (i = 0; i < cert_count; i++) {
  30938. x509Ca = sk_X509_value(sk, i);
  30939. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  30940. }
  30941. ExpectNotNull((x509Svr =
  30942. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  30943. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  30944. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  30945. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  30946. ExpectNotNull(issuer);
  30947. caName = X509_get_subject_name(x509Ca);
  30948. ExpectNotNull(caName);
  30949. issuerName = X509_get_subject_name(issuer);
  30950. ExpectNotNull(issuerName);
  30951. cmp = X509_NAME_cmp(caName, issuerName);
  30952. ExpectIntEQ(cmp, 0);
  30953. /* load der format */
  30954. X509_free(issuer);
  30955. issuer = NULL;
  30956. X509_STORE_CTX_free(ctx);
  30957. ctx = NULL;
  30958. X509_STORE_free(str);
  30959. str = NULL;
  30960. sk_X509_pop_free(sk, NULL);
  30961. sk = NULL;
  30962. X509_free(x509Svr);
  30963. x509Svr = NULL;
  30964. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  30965. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  30966. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  30967. SSL_FILETYPE_ASN1,NULL), 1);
  30968. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  30969. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  30970. /* check if CA cert is loaded into the store */
  30971. for (i = 0; i < cert_count; i++) {
  30972. x509Ca = sk_X509_value(sk, i);
  30973. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  30974. }
  30975. X509_STORE_free(str);
  30976. str = NULL;
  30977. sk_X509_pop_free(sk, NULL);
  30978. sk = NULL;
  30979. X509_free(cert1);
  30980. cert1 = NULL;
  30981. #ifdef HAVE_CRL
  30982. ExpectNotNull(str = wolfSSL_X509_STORE_new());
  30983. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  30984. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  30985. SSL_FILETYPE_PEM,NULL), 1);
  30986. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  30987. "certs/server-revoked-cert.pem",
  30988. SSL_FILETYPE_PEM,NULL), 1);
  30989. if (str) {
  30990. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  30991. WOLFSSL_FILETYPE_PEM), 1);
  30992. /* since store hasn't yet known the revoked cert*/
  30993. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  30994. "certs/server-revoked-cert.pem",
  30995. WOLFSSL_FILETYPE_PEM), 1);
  30996. }
  30997. for (i = 0; pem[i][0] != '\0'; i++)
  30998. {
  30999. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  31000. SSL_FILETYPE_PEM, NULL), 1);
  31001. }
  31002. if (str) {
  31003. /* since store knows crl list */
  31004. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  31005. "certs/server-revoked-cert.pem",
  31006. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  31007. }
  31008. ExpectIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  31009. X509_STORE_free(str);
  31010. #endif
  31011. #endif
  31012. return EXPECT_RESULT();
  31013. }
  31014. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  31015. {
  31016. int res = TEST_SKIPPED;
  31017. #if defined(OPENSSL_EXTRA)
  31018. X509_STORE_CTX_cleanup(NULL);
  31019. X509_STORE_CTX_trusted_stack(NULL, NULL);
  31020. res = TEST_SUCCESS;
  31021. #endif
  31022. return res;
  31023. }
  31024. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  31025. {
  31026. EXPECT_DECLS;
  31027. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31028. X509_STORE_CTX* ctx = NULL;
  31029. X509_STORE* str = NULL;
  31030. X509* x509Ca = NULL;
  31031. X509* x509Svr = NULL;
  31032. X509* issuer = NULL;
  31033. X509_NAME* caName = NULL;
  31034. X509_NAME* issuerName = NULL;
  31035. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31036. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31037. ExpectNotNull((x509Ca =
  31038. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  31039. ExpectIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  31040. ExpectNotNull((x509Svr =
  31041. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  31042. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  31043. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  31044. ExpectNotNull(issuer = X509_STORE_CTX_get0_current_issuer(ctx));
  31045. ExpectNotNull(caName = X509_get_subject_name(x509Ca));
  31046. ExpectNotNull(issuerName = X509_get_subject_name(issuer));
  31047. #ifdef WOLFSSL_SIGNER_DER_CERT
  31048. ExpectIntEQ(X509_NAME_cmp(caName, issuerName), 0);
  31049. #endif
  31050. X509_free(issuer);
  31051. X509_STORE_CTX_free(ctx);
  31052. X509_free(x509Svr);
  31053. X509_STORE_free(str);
  31054. X509_free(x509Ca);
  31055. #endif
  31056. return EXPECT_RESULT();
  31057. }
  31058. static int test_wolfSSL_PKCS7_certs(void)
  31059. {
  31060. EXPECT_DECLS;
  31061. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  31062. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  31063. STACK_OF(X509)* sk = NULL;
  31064. STACK_OF(X509_INFO)* info_sk = NULL;
  31065. PKCS7 *p7 = NULL;
  31066. BIO* bio = NULL;
  31067. const byte* p = NULL;
  31068. int buflen = 0;
  31069. int i;
  31070. /* Test twice. Once with d2i and once without to test
  31071. * that everything is free'd correctly. */
  31072. for (i = 0; i < 2; i++) {
  31073. ExpectNotNull(p7 = PKCS7_new());
  31074. if (p7 != NULL) {
  31075. p7->version = 1;
  31076. #ifdef NO_SHA
  31077. p7->hashOID = SHA256h;
  31078. #else
  31079. p7->hashOID = SHAh;
  31080. #endif
  31081. }
  31082. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  31083. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  31084. ExpectNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  31085. ExpectIntEQ(sk_X509_INFO_num(info_sk), 2);
  31086. ExpectNotNull(sk = sk_X509_new_null());
  31087. while (EXPECT_SUCCESS() && (sk_X509_INFO_num(info_sk) > 0)) {
  31088. X509_INFO* info = NULL;
  31089. ExpectNotNull(info = sk_X509_INFO_shift(info_sk));
  31090. ExpectIntEQ(sk_X509_push(sk, info->x509), 1);
  31091. if (EXPECT_SUCCESS() && (info != NULL)) {
  31092. info->x509 = NULL;
  31093. }
  31094. X509_INFO_free(info);
  31095. }
  31096. sk_X509_INFO_pop_free(info_sk, X509_INFO_free);
  31097. info_sk = NULL;
  31098. BIO_free(bio);
  31099. bio = NULL;
  31100. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  31101. ExpectIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  31102. if ((sk != NULL) && ((p7 == NULL) || (bio == NULL))) {
  31103. sk_X509_pop_free(sk, X509_free);
  31104. }
  31105. sk = NULL;
  31106. ExpectIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  31107. if (i == 0) {
  31108. PKCS7_free(p7);
  31109. p7 = NULL;
  31110. ExpectNotNull(d2i_PKCS7(&p7, &p, buflen));
  31111. if (p7 != NULL) {
  31112. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate
  31113. * them */
  31114. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  31115. }
  31116. /* PKCS7_free free's the certs */
  31117. ExpectNotNull(wolfSSL_PKCS7_to_stack(p7));
  31118. }
  31119. BIO_free(bio);
  31120. bio = NULL;
  31121. PKCS7_free(p7);
  31122. p7 = NULL;
  31123. }
  31124. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  31125. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  31126. return EXPECT_RESULT();
  31127. }
  31128. static int test_wolfSSL_X509_STORE_CTX(void)
  31129. {
  31130. EXPECT_DECLS;
  31131. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31132. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31133. X509_STORE_CTX* ctx = NULL;
  31134. X509_STORE* str = NULL;
  31135. X509* x509 = NULL;
  31136. #ifdef OPENSSL_ALL
  31137. X509* x5092 = NULL;
  31138. STACK_OF(X509) *sk = NULL;
  31139. STACK_OF(X509) *sk2 = NULL;
  31140. STACK_OF(X509) *sk3 = NULL;
  31141. #endif
  31142. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31143. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31144. ExpectNotNull((x509 =
  31145. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  31146. ExpectIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  31147. #ifdef OPENSSL_ALL
  31148. /* sk_X509_new only in OPENSSL_ALL */
  31149. sk = sk_X509_new_null();
  31150. ExpectNotNull(sk);
  31151. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  31152. #else
  31153. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  31154. #endif
  31155. ExpectIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  31156. X509_STORE_CTX_set_error(ctx, -5);
  31157. X509_STORE_CTX_set_error(NULL, -5);
  31158. X509_STORE_CTX_free(ctx);
  31159. ctx = NULL;
  31160. #ifdef OPENSSL_ALL
  31161. sk_X509_pop_free(sk, NULL);
  31162. sk = NULL;
  31163. #endif
  31164. X509_STORE_free(str);
  31165. str = NULL;
  31166. X509_free(x509);
  31167. x509 = NULL;
  31168. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31169. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  31170. X509_STORE_CTX_free(ctx);
  31171. ctx = NULL;
  31172. #ifdef OPENSSL_ALL
  31173. /* test X509_STORE_CTX_get(1)_chain */
  31174. ExpectNotNull((x509 = X509_load_certificate_file(svrCertFile,
  31175. SSL_FILETYPE_PEM)));
  31176. ExpectNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  31177. SSL_FILETYPE_PEM)));
  31178. ExpectNotNull((sk = sk_X509_new_null()));
  31179. ExpectIntEQ(sk_X509_push(sk, x509), 1);
  31180. if (EXPECT_FAIL()) {
  31181. X509_free(x509);
  31182. x509 = NULL;
  31183. }
  31184. ExpectNotNull((str = X509_STORE_new()));
  31185. ExpectNotNull((ctx = X509_STORE_CTX_new()));
  31186. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  31187. ExpectNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  31188. ExpectNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  31189. ExpectIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  31190. ExpectNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  31191. ExpectNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  31192. ExpectIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  31193. X509_STORE_CTX_free(ctx);
  31194. ctx = NULL;
  31195. X509_STORE_free(str);
  31196. str = NULL;
  31197. /* CTX certs not freed yet */
  31198. X509_free(x5092);
  31199. x5092 = NULL;
  31200. sk_X509_pop_free(sk, NULL);
  31201. sk = NULL;
  31202. /* sk3 is dup so free here */
  31203. sk_X509_pop_free(sk3, NULL);
  31204. sk3 = NULL;
  31205. #endif
  31206. /* test X509_STORE_CTX_get/set_ex_data */
  31207. {
  31208. int i = 0, tmpData = 5;
  31209. void* tmpDataRet;
  31210. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31211. #ifdef HAVE_EX_DATA
  31212. for (i = 0; i < MAX_EX_DATA; i++) {
  31213. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  31214. WOLFSSL_SUCCESS);
  31215. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  31216. ExpectNotNull(tmpDataRet);
  31217. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  31218. }
  31219. #else
  31220. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  31221. WOLFSSL_FAILURE);
  31222. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  31223. ExpectNull(tmpDataRet);
  31224. #endif
  31225. X509_STORE_CTX_free(ctx);
  31226. ctx = NULL;
  31227. }
  31228. /* test X509_STORE_get/set_ex_data */
  31229. {
  31230. int i = 0, tmpData = 99;
  31231. void* tmpDataRet;
  31232. ExpectNotNull(str = X509_STORE_new());
  31233. #ifdef HAVE_EX_DATA
  31234. for (i = 0; i < MAX_EX_DATA; i++) {
  31235. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  31236. WOLFSSL_SUCCESS);
  31237. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  31238. ExpectNotNull(tmpDataRet);
  31239. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  31240. }
  31241. #else
  31242. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  31243. WOLFSSL_FAILURE);
  31244. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  31245. ExpectNull(tmpDataRet);
  31246. #endif
  31247. X509_STORE_free(str);
  31248. str = NULL;
  31249. }
  31250. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31251. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  31252. return EXPECT_RESULT();
  31253. }
  31254. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31255. static int test_X509_STORE_untrusted_load_cert_to_stack(const char* filename,
  31256. STACK_OF(X509)* chain)
  31257. {
  31258. EXPECT_DECLS;
  31259. XFILE fp = XBADFILE;
  31260. X509* cert = NULL;
  31261. ExpectTrue((fp = XFOPEN(filename, "rb"))
  31262. != XBADFILE);
  31263. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  31264. if (fp != XBADFILE) {
  31265. XFCLOSE(fp);
  31266. fp = XBADFILE;
  31267. }
  31268. ExpectIntEQ(sk_X509_push(chain, cert), 1);
  31269. if (EXPECT_FAIL())
  31270. X509_free(cert);
  31271. return EXPECT_RESULT();
  31272. }
  31273. static int test_X509_STORE_untrusted_certs(const char** filenames, int ret,
  31274. int err, int loadCA)
  31275. {
  31276. EXPECT_DECLS;
  31277. X509_STORE_CTX* ctx = NULL;
  31278. X509_STORE* str = NULL;
  31279. XFILE fp = XBADFILE;
  31280. X509* cert = NULL;
  31281. STACK_OF(X509)* untrusted = NULL;
  31282. ExpectTrue((fp = XFOPEN("./certs/intermediate/server-int-cert.pem", "rb"))
  31283. != XBADFILE);
  31284. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  31285. if (fp != XBADFILE) {
  31286. XFCLOSE(fp);
  31287. fp = XBADFILE;
  31288. }
  31289. ExpectNotNull(str = X509_STORE_new());
  31290. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31291. ExpectNotNull(untrusted = sk_X509_new_null());
  31292. ExpectIntEQ(X509_STORE_set_flags(str, 0), 1);
  31293. if (loadCA) {
  31294. ExpectIntEQ(X509_STORE_load_locations(str, "./certs/ca-cert.pem", NULL),
  31295. 1);
  31296. }
  31297. for (; *filenames; filenames++) {
  31298. ExpectIntEQ(test_X509_STORE_untrusted_load_cert_to_stack(*filenames,
  31299. untrusted), TEST_SUCCESS);
  31300. }
  31301. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, cert, untrusted), 1);
  31302. ExpectIntEQ(X509_verify_cert(ctx), ret);
  31303. ExpectIntEQ(X509_STORE_CTX_get_error(ctx), err);
  31304. X509_free(cert);
  31305. X509_STORE_free(str);
  31306. X509_STORE_CTX_free(ctx);
  31307. sk_X509_pop_free(untrusted, NULL);
  31308. return EXPECT_RESULT();
  31309. }
  31310. #endif
  31311. static int test_X509_STORE_untrusted(void)
  31312. {
  31313. EXPECT_DECLS;
  31314. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31315. const char* untrusted1[] = {
  31316. "./certs/intermediate/ca-int2-cert.pem",
  31317. NULL
  31318. };
  31319. const char* untrusted2[] = {
  31320. "./certs/intermediate/ca-int-cert.pem",
  31321. "./certs/intermediate/ca-int2-cert.pem",
  31322. NULL
  31323. };
  31324. const char* untrusted3[] = {
  31325. "./certs/intermediate/ca-int-cert.pem",
  31326. "./certs/intermediate/ca-int2-cert.pem",
  31327. "./certs/ca-cert.pem",
  31328. NULL
  31329. };
  31330. /* Adding unrelated certs that should be ignored */
  31331. const char* untrusted4[] = {
  31332. "./certs/client-ca.pem",
  31333. "./certs/intermediate/ca-int-cert.pem",
  31334. "./certs/server-cert.pem",
  31335. "./certs/intermediate/ca-int2-cert.pem",
  31336. NULL
  31337. };
  31338. /* Only immediate issuer in untrusted chain. Fails since can't build chain
  31339. * to loaded CA. */
  31340. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted1, 0,
  31341. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 1), TEST_SUCCESS);
  31342. /* Succeeds because path to loaded CA is available. */
  31343. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted2, 1, 0, 1),
  31344. TEST_SUCCESS);
  31345. /* Fails because root CA is in the untrusted stack */
  31346. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted3, 0,
  31347. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 0), TEST_SUCCESS);
  31348. /* Succeeds because path to loaded CA is available. */
  31349. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted4, 1, 0, 1),
  31350. TEST_SUCCESS);
  31351. #endif
  31352. return EXPECT_RESULT();
  31353. }
  31354. static int test_wolfSSL_X509_STORE_set_flags(void)
  31355. {
  31356. EXPECT_DECLS;
  31357. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31358. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31359. X509_STORE* store = NULL;
  31360. X509* x509 = NULL;
  31361. ExpectNotNull((store = wolfSSL_X509_STORE_new()));
  31362. ExpectNotNull((x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  31363. WOLFSSL_FILETYPE_PEM)));
  31364. ExpectIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  31365. #ifdef HAVE_CRL
  31366. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  31367. WOLFSSL_SUCCESS);
  31368. #else
  31369. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  31370. NOT_COMPILED_IN);
  31371. #endif
  31372. wolfSSL_X509_free(x509);
  31373. wolfSSL_X509_STORE_free(store);
  31374. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  31375. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  31376. return EXPECT_RESULT();
  31377. }
  31378. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  31379. {
  31380. EXPECT_DECLS;
  31381. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  31382. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  31383. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  31384. WOLFSSL_X509_STORE* store = NULL;
  31385. WOLFSSL_X509_LOOKUP* lookup = NULL;
  31386. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  31387. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  31388. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  31389. X509_FILETYPE_PEM), 1);
  31390. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  31391. X509_FILETYPE_PEM), 1);
  31392. if (store != NULL) {
  31393. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  31394. WOLFSSL_FILETYPE_PEM), 1);
  31395. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  31396. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  31397. }
  31398. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  31399. X509_FILETYPE_PEM), 1);
  31400. if (store != NULL) {
  31401. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  31402. WOLFSSL_FILETYPE_PEM), 1);
  31403. }
  31404. wolfSSL_X509_STORE_free(store);
  31405. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) &&
  31406. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  31407. return EXPECT_RESULT();
  31408. }
  31409. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  31410. {
  31411. EXPECT_DECLS;
  31412. #if defined(OPENSSL_EXTRA)
  31413. WOLFSSL_X509_STORE_CTX* ctx = NULL;
  31414. time_t c_time;
  31415. ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  31416. c_time = 365*24*60*60;
  31417. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  31418. ExpectTrue((ctx->param->flags & WOLFSSL_USE_CHECK_TIME) ==
  31419. WOLFSSL_USE_CHECK_TIME);
  31420. ExpectTrue(ctx->param->check_time == c_time);
  31421. wolfSSL_X509_STORE_CTX_free(ctx);
  31422. #endif /* OPENSSL_EXTRA */
  31423. return EXPECT_RESULT();
  31424. }
  31425. static int test_wolfSSL_CTX_get0_set1_param(void)
  31426. {
  31427. EXPECT_DECLS;
  31428. #if defined(OPENSSL_EXTRA)
  31429. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31430. SSL_CTX* ctx = NULL;
  31431. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  31432. WOLFSSL_X509_VERIFY_PARAM* pvpm = NULL;
  31433. char testIPv4[] = "127.0.0.1";
  31434. char testhostName[] = "foo.hoge.com";
  31435. #ifndef NO_WOLFSSL_SERVER
  31436. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31437. #else
  31438. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31439. #endif
  31440. ExpectNull(SSL_CTX_get0_param(NULL));
  31441. ExpectNotNull(pParam = SSL_CTX_get0_param(ctx));
  31442. ExpectNotNull(pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  31443. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL));
  31444. ExpectNotNull(XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM)));
  31445. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  31446. (int)XSTRLEN(testhostName)), WOLFSSL_SUCCESS);
  31447. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4),
  31448. WOLFSSL_SUCCESS);
  31449. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  31450. ExpectIntEQ(SSL_CTX_set1_param(ctx, pvpm), 1);
  31451. ExpectIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  31452. (int)XSTRLEN(testhostName)));
  31453. ExpectIntEQ(0x01, pParam->hostFlags);
  31454. ExpectIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  31455. /* test for incorrect parameter */
  31456. ExpectIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  31457. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  31458. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  31459. SSL_CTX_free(ctx);
  31460. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  31461. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31462. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  31463. return EXPECT_RESULT();
  31464. }
  31465. static int test_wolfSSL_get0_param(void)
  31466. {
  31467. EXPECT_DECLS;
  31468. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31469. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31470. SSL_CTX* ctx = NULL;
  31471. SSL* ssl = NULL;
  31472. #ifndef NO_WOLFSSL_SERVER
  31473. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31474. #else
  31475. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31476. #endif
  31477. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31478. SSL_FILETYPE_PEM));
  31479. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31480. ExpectNotNull(ssl = SSL_new(ctx));
  31481. ExpectNotNull(SSL_get0_param(ssl));
  31482. SSL_free(ssl);
  31483. SSL_CTX_free(ctx);
  31484. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31485. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  31486. return EXPECT_RESULT();
  31487. }
  31488. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  31489. {
  31490. EXPECT_DECLS;
  31491. #if defined(OPENSSL_EXTRA)
  31492. const char host[] = "www.example.com";
  31493. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  31494. ExpectNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  31495. sizeof(WOLFSSL_X509_VERIFY_PARAM), HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  31496. if (pParam != NULL) {
  31497. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  31498. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  31499. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  31500. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  31501. ExpectIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  31502. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  31503. }
  31504. #endif /* OPENSSL_EXTRA */
  31505. return EXPECT_RESULT();
  31506. }
  31507. static int test_wolfSSL_set1_host(void)
  31508. {
  31509. EXPECT_DECLS;
  31510. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31511. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31512. const char host[] = "www.test_wolfSSL_set1_host.com";
  31513. const char emptyStr[] = "";
  31514. SSL_CTX* ctx = NULL;
  31515. SSL* ssl = NULL;
  31516. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  31517. #ifndef NO_WOLFSSL_SERVER
  31518. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31519. #else
  31520. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31521. #endif
  31522. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31523. SSL_FILETYPE_PEM));
  31524. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31525. ExpectNotNull(ssl = SSL_new(ctx));
  31526. pParam = SSL_get0_param(ssl);
  31527. /* we should get back host string */
  31528. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  31529. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  31530. /* we should get back empty string */
  31531. ExpectIntEQ(SSL_set1_host(ssl, emptyStr), WOLFSSL_SUCCESS);
  31532. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  31533. /* we should get back host string */
  31534. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  31535. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  31536. /* we should get back empty string */
  31537. ExpectIntEQ(SSL_set1_host(ssl, NULL), WOLFSSL_SUCCESS);
  31538. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  31539. SSL_free(ssl);
  31540. SSL_CTX_free(ctx);
  31541. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31542. #endif /* OPENSSL_EXTRA */
  31543. return EXPECT_RESULT();
  31544. }
  31545. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  31546. {
  31547. EXPECT_DECLS;
  31548. #if defined(OPENSSL_EXTRA)
  31549. unsigned char buf[16] = {0};
  31550. WOLFSSL_X509_VERIFY_PARAM* param = NULL;
  31551. ExpectNotNull(param = X509_VERIFY_PARAM_new());
  31552. /* test 127.0.0.1 */
  31553. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  31554. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  31555. ExpectIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  31556. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  31557. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  31558. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  31559. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  31560. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  31561. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  31562. ExpectIntEQ(XSTRNCMP(param->ipasc,
  31563. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  31564. /* test 2001:db8:: */
  31565. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  31566. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  31567. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  31568. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  31569. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  31570. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  31571. /* test ::1234:5678 */
  31572. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  31573. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  31574. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  31575. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  31576. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  31577. ExpectIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  31578. /* test 2001:db8::1234:5678 */
  31579. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  31580. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  31581. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  31582. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  31583. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  31584. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  31585. sizeof(param->ipasc)), 0);
  31586. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  31587. /* 2001:db8:1::ab9:c0a8:102 */
  31588. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  31589. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  31590. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  31591. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  31592. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  31593. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  31594. sizeof(param->ipasc)), 0);
  31595. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  31596. #endif /* OPENSSL_EXTRA */
  31597. return EXPECT_RESULT();
  31598. }
  31599. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  31600. {
  31601. EXPECT_DECLS;
  31602. #if defined(OPENSSL_EXTRA)
  31603. X509_STORE* store = NULL;
  31604. X509_STORE_CTX* ctx = NULL;
  31605. X509_STORE_CTX* ctx_no_init = NULL;
  31606. ExpectNotNull((store = X509_STORE_new()));
  31607. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31608. ExpectNotNull(ctx_no_init = X509_STORE_CTX_new());
  31609. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  31610. ExpectNull(X509_STORE_CTX_get0_store(NULL));
  31611. /* should return NULL if ctx has not bee initialized */
  31612. ExpectNull(X509_STORE_CTX_get0_store(ctx_no_init));
  31613. ExpectNotNull(X509_STORE_CTX_get0_store(ctx));
  31614. wolfSSL_X509_STORE_CTX_free(ctx);
  31615. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  31616. X509_STORE_free(store);
  31617. #endif /* OPENSSL_EXTRA */
  31618. return EXPECT_RESULT();
  31619. }
  31620. static int test_wolfSSL_CTX_set_client_CA_list(void)
  31621. {
  31622. EXPECT_DECLS;
  31623. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  31624. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  31625. WOLFSSL_CTX* ctx = NULL;
  31626. WOLFSSL* ssl = NULL;
  31627. X509_NAME* name = NULL;
  31628. STACK_OF(X509_NAME)* names = NULL;
  31629. STACK_OF(X509_NAME)* ca_list = NULL;
  31630. int names_len = 0;
  31631. int i;
  31632. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  31633. /* Send two X501 names in cert request */
  31634. names = SSL_load_client_CA_file(cliCertFile);
  31635. ExpectNotNull(names);
  31636. ca_list = SSL_load_client_CA_file(caCertFile);
  31637. ExpectNotNull(ca_list);
  31638. ExpectNotNull(name = sk_X509_NAME_value(ca_list, 0));
  31639. ExpectIntEQ(sk_X509_NAME_push(names, name), 1);
  31640. if (EXPECT_FAIL()) {
  31641. wolfSSL_X509_NAME_free(name);
  31642. name = NULL;
  31643. }
  31644. SSL_CTX_set_client_CA_list(ctx, names);
  31645. /* This should only free the stack structure */
  31646. sk_X509_NAME_free(ca_list);
  31647. ca_list = NULL;
  31648. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  31649. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  31650. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  31651. for (i = 0; i < names_len; i++) {
  31652. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  31653. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  31654. }
  31655. /* Needed to be able to create ssl object */
  31656. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31657. SSL_FILETYPE_PEM));
  31658. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31659. ExpectNotNull(ssl = wolfSSL_new(ctx));
  31660. /* load again as old names are responsibility of ctx to free*/
  31661. names = SSL_load_client_CA_file(cliCertFile);
  31662. ExpectNotNull(names);
  31663. SSL_set_client_CA_list(ssl, names);
  31664. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl));
  31665. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  31666. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  31667. for (i = 0; i < names_len; i++) {
  31668. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  31669. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  31670. }
  31671. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  31672. {
  31673. tcp_ready ready;
  31674. func_args server_args;
  31675. callback_functions server_cb;
  31676. THREAD_TYPE serverThread;
  31677. WOLFSSL* ssl_client = NULL;
  31678. WOLFSSL_CTX* ctx_client = NULL;
  31679. SOCKET_T sockfd = 0;
  31680. /* wolfSSL_get_client_CA_list() with handshake */
  31681. StartTCP();
  31682. InitTcpReady(&ready);
  31683. XMEMSET(&server_args, 0, sizeof(func_args));
  31684. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  31685. server_args.signal = &ready;
  31686. server_args.callbacks = &server_cb;
  31687. /* we are responsible for free'ing WOLFSSL_CTX */
  31688. server_cb.ctx = ctx;
  31689. server_cb.isSharedCtx = 1;
  31690. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  31691. cliCertFile, 0));
  31692. start_thread(test_server_nofail, &server_args, &serverThread);
  31693. wait_tcp_ready(&server_args);
  31694. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  31695. ExpectNotNull(ctx_client =
  31696. wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  31697. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(
  31698. ctx_client, caCertFile, 0));
  31699. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(
  31700. ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  31701. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(
  31702. ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  31703. ExpectNotNull(ssl_client = wolfSSL_new(ctx_client));
  31704. ExpectIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  31705. ExpectIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  31706. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  31707. /* We are expecting two cert names to be sent */
  31708. ExpectIntEQ(sk_X509_NAME_num(ca_list), 2);
  31709. ExpectNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  31710. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  31711. ExpectNotNull(name = sk_X509_NAME_value(ca_list, i));
  31712. ExpectIntGE(sk_X509_NAME_find(names, name), 0);
  31713. }
  31714. wolfSSL_shutdown(ssl_client);
  31715. wolfSSL_free(ssl_client);
  31716. wolfSSL_CTX_free(ctx_client);
  31717. CloseSocket(sockfd);
  31718. join_thread(serverThread);
  31719. FreeTcpReady(&ready);
  31720. }
  31721. #endif
  31722. wolfSSL_free(ssl);
  31723. wolfSSL_CTX_free(ctx);
  31724. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
  31725. * !NO_BIO */
  31726. return EXPECT_RESULT();
  31727. }
  31728. static int test_wolfSSL_CTX_add_client_CA(void)
  31729. {
  31730. EXPECT_DECLS;
  31731. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  31732. !defined(NO_WOLFSSL_CLIENT)
  31733. WOLFSSL_CTX* ctx = NULL;
  31734. WOLFSSL_X509* x509 = NULL;
  31735. WOLFSSL_X509* x509_a = NULL;
  31736. STACK_OF(X509_NAME)* ca_list = NULL;
  31737. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31738. /* Add client cert */
  31739. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  31740. SSL_FILETYPE_PEM));
  31741. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509), SSL_SUCCESS);
  31742. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  31743. /* Add another client cert */
  31744. ExpectNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  31745. SSL_FILETYPE_PEM));
  31746. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  31747. /* test for incorrect parameter */
  31748. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  31749. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  31750. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  31751. X509_free(x509);
  31752. X509_free(x509_a);
  31753. SSL_CTX_free(ctx);
  31754. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  31755. return EXPECT_RESULT();
  31756. }
  31757. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  31758. defined(HAVE_IO_TESTS_DEPENDENCIES)
  31759. static THREAD_RETURN WOLFSSL_THREAD server_task_ech(void* args)
  31760. {
  31761. callback_functions* callbacks = ((func_args*)args)->callbacks;
  31762. WOLFSSL_CTX* ctx = callbacks->ctx;
  31763. WOLFSSL* ssl = NULL;
  31764. SOCKET_T sfd = 0;
  31765. SOCKET_T cfd = 0;
  31766. word16 port;
  31767. char input[1024];
  31768. int idx;
  31769. int ret, err = 0;
  31770. const char* privateName = "ech-private-name.com";
  31771. int privateNameLen = (int)XSTRLEN(privateName);
  31772. ((func_args*)args)->return_code = TEST_FAIL;
  31773. port = ((func_args*)args)->signal->port;
  31774. AssertIntEQ(WOLFSSL_SUCCESS,
  31775. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  31776. AssertIntEQ(WOLFSSL_SUCCESS,
  31777. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  31778. WOLFSSL_FILETYPE_PEM));
  31779. AssertIntEQ(WOLFSSL_SUCCESS,
  31780. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  31781. WOLFSSL_FILETYPE_PEM));
  31782. if (callbacks->ctx_ready)
  31783. callbacks->ctx_ready(ctx);
  31784. ssl = wolfSSL_new(ctx);
  31785. /* set the sni for the server */
  31786. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, privateName, privateNameLen);
  31787. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  31788. CloseSocket(sfd);
  31789. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  31790. if (callbacks->ssl_ready)
  31791. callbacks->ssl_ready(ssl);
  31792. do {
  31793. err = 0; /* Reset error */
  31794. ret = wolfSSL_accept(ssl);
  31795. if (ret != WOLFSSL_SUCCESS) {
  31796. err = wolfSSL_get_error(ssl, 0);
  31797. }
  31798. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  31799. if (ret != WOLFSSL_SUCCESS) {
  31800. char buff[WOLFSSL_MAX_ERROR_SZ];
  31801. fprintf(stderr, "error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  31802. }
  31803. else {
  31804. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  31805. input[idx] = 0;
  31806. fprintf(stderr, "Client message: %s\n", input);
  31807. }
  31808. AssertIntEQ(privateNameLen, wolfSSL_write(ssl, privateName,
  31809. privateNameLen));
  31810. ((func_args*)args)->return_code = TEST_SUCCESS;
  31811. }
  31812. if (callbacks->on_result)
  31813. callbacks->on_result(ssl);
  31814. wolfSSL_shutdown(ssl);
  31815. wolfSSL_free(ssl);
  31816. wolfSSL_CTX_free(ctx);
  31817. CloseSocket(cfd);
  31818. #ifdef FP_ECC
  31819. wc_ecc_fp_free();
  31820. #endif
  31821. WOLFSSL_RETURN_FROM_THREAD(0);
  31822. }
  31823. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  31824. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  31825. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  31826. {
  31827. AssertNotNull(ssl);
  31828. AssertNotNull(line);
  31829. XFILE fp;
  31830. const byte lf = '\n';
  31831. fp = XFOPEN("./MyKeyLog.txt", "a");
  31832. XFWRITE( line, 1, strlen(line),fp);
  31833. XFWRITE( (void*)&lf,1,1,fp);
  31834. XFFLUSH(fp);
  31835. XFCLOSE(fp);
  31836. }
  31837. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  31838. static int test_wolfSSL_CTX_set_keylog_callback(void)
  31839. {
  31840. EXPECT_DECLS;
  31841. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  31842. !defined(NO_WOLFSSL_CLIENT)
  31843. SSL_CTX* ctx = NULL;
  31844. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31845. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  31846. SSL_CTX_free(ctx);
  31847. SSL_CTX_set_keylog_callback(NULL, NULL);
  31848. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  31849. return EXPECT_RESULT();
  31850. }
  31851. static int test_wolfSSL_CTX_get_keylog_callback(void)
  31852. {
  31853. EXPECT_DECLS;
  31854. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  31855. !defined(NO_WOLFSSL_CLIENT)
  31856. SSL_CTX* ctx = NULL;
  31857. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31858. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  31859. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  31860. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  31861. SSL_CTX_set_keylog_callback(ctx, NULL );
  31862. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  31863. SSL_CTX_free(ctx);
  31864. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  31865. return EXPECT_RESULT();
  31866. }
  31867. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  31868. static int test_wolfSSL_Tls12_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  31869. {
  31870. /* set keylog callback */
  31871. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  31872. return TEST_SUCCESS;
  31873. }
  31874. #endif
  31875. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  31876. {
  31877. EXPECT_DECLS;
  31878. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  31879. /* This test is intended for checking whether keylog callback is called
  31880. * in client during TLS handshake between the client and a server.
  31881. */
  31882. test_ssl_cbf server_cbf;
  31883. test_ssl_cbf client_cbf;
  31884. XFILE fp = XBADFILE;
  31885. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  31886. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  31887. server_cbf.method = wolfTLSv1_2_server_method;
  31888. client_cbf.ctx_ready = &test_wolfSSL_Tls12_Key_Logging_client_ctx_ready;
  31889. /* clean up keylog file */
  31890. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  31891. if (fp != XBADFILE) {
  31892. XFCLOSE(fp);
  31893. fp = XBADFILE;
  31894. }
  31895. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  31896. &server_cbf, NULL), TEST_SUCCESS);
  31897. /* check if the keylog file exists */
  31898. char buff[300] = {0};
  31899. int found = 0;
  31900. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  31901. while (EXPECT_SUCCESS() && XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  31902. if (0 == strncmp(buff,"CLIENT_RANDOM ", sizeof("CLIENT_RANDOM ")-1)) {
  31903. found = 1;
  31904. break;
  31905. }
  31906. }
  31907. if (fp != XBADFILE) {
  31908. XFCLOSE(fp);
  31909. }
  31910. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  31911. ExpectIntEQ(found, 1);
  31912. /* clean up */
  31913. ExpectIntEQ(rem_file("./MyKeyLog.txt"), 0);
  31914. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  31915. return EXPECT_RESULT();
  31916. }
  31917. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  31918. defined(HAVE_SECRET_CALLBACK)
  31919. static int test_wolfSSL_Tls13_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  31920. {
  31921. /* set keylog callback */
  31922. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  31923. return TEST_SUCCESS;
  31924. }
  31925. #endif
  31926. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  31927. {
  31928. EXPECT_DECLS;
  31929. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  31930. defined(HAVE_SECRET_CALLBACK)
  31931. /* This test is intended for checking whether keylog callback is called
  31932. * in client during TLS handshake between the client and a server.
  31933. */
  31934. test_ssl_cbf server_cbf;
  31935. test_ssl_cbf client_cbf;
  31936. XFILE fp = XBADFILE;
  31937. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  31938. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  31939. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  31940. client_cbf.ctx_ready = &test_wolfSSL_Tls13_Key_Logging_client_ctx_ready;
  31941. /* clean up keylog file */
  31942. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  31943. if (fp != XBADFILE) {
  31944. XFCLOSE(fp);
  31945. fp = XBADFILE;
  31946. }
  31947. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  31948. &server_cbf, NULL), TEST_SUCCESS);
  31949. /* check if the keylog file exists */
  31950. {
  31951. char buff[300] = {0};
  31952. int found[4] = {0};
  31953. int numfnd = 0;
  31954. int i;
  31955. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  31956. while (EXPECT_SUCCESS() &&
  31957. XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  31958. if (0 == strncmp(buff, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  31959. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  31960. found[0] = 1;
  31961. continue;
  31962. }
  31963. else if (0 == strncmp(buff, "SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  31964. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  31965. found[1] = 1;
  31966. continue;
  31967. }
  31968. else if (0 == strncmp(buff, "CLIENT_TRAFFIC_SECRET_0 ",
  31969. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  31970. found[2] = 1;
  31971. continue;
  31972. }
  31973. else if (0 == strncmp(buff, "SERVER_TRAFFIC_SECRET_0 ",
  31974. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  31975. found[3] = 1;
  31976. continue;
  31977. }
  31978. }
  31979. if (fp != XBADFILE)
  31980. XFCLOSE(fp);
  31981. for (i = 0; i < 4; i++) {
  31982. if (found[i] != 0)
  31983. numfnd++;
  31984. }
  31985. ExpectIntEQ(numfnd, 4);
  31986. }
  31987. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  31988. return EXPECT_RESULT();
  31989. }
  31990. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  31991. defined(HAVE_IO_TESTS_DEPENDENCIES)
  31992. static int test_wolfSSL_Tls13_ECH_params(void)
  31993. {
  31994. EXPECT_DECLS;
  31995. #if !defined(NO_WOLFSSL_CLIENT)
  31996. word32 outputLen = 0;
  31997. byte testBuf[72];
  31998. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  31999. WOLFSSL *ssl = wolfSSL_new(ctx);
  32000. ExpectNotNull(ctx);
  32001. ExpectNotNull(ssl);
  32002. /* invalid ctx */
  32003. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(NULL,
  32004. "ech-public-name.com", 0, 0, 0));
  32005. /* invalid public name */
  32006. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx, NULL, 0,
  32007. 0, 0));
  32008. /* invalid algorithms */
  32009. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx,
  32010. "ech-public-name.com", 1000, 1000, 1000));
  32011. /* invalid ctx */
  32012. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(NULL, NULL,
  32013. &outputLen));
  32014. /* invalid output len */
  32015. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(ctx, NULL, NULL));
  32016. /* invalid ssl */
  32017. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(NULL,
  32018. (char*)testBuf, sizeof(testBuf)));
  32019. /* invalid configs64 */
  32020. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl, NULL,
  32021. sizeof(testBuf)));
  32022. /* invalid size */
  32023. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl,
  32024. (char*)testBuf, 0));
  32025. /* invalid ssl */
  32026. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(NULL, testBuf,
  32027. sizeof(testBuf)));
  32028. /* invalid configs */
  32029. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, NULL,
  32030. sizeof(testBuf)));
  32031. /* invalid size */
  32032. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, testBuf, 0));
  32033. /* invalid ssl */
  32034. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(NULL, NULL, &outputLen));
  32035. /* invalid size */
  32036. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(ssl, NULL, NULL));
  32037. wolfSSL_free(ssl);
  32038. wolfSSL_CTX_free(ctx);
  32039. #endif /* !NO_WOLFSSL_CLIENT */
  32040. return EXPECT_RESULT();
  32041. }
  32042. static int test_wolfSSL_Tls13_ECH(void)
  32043. {
  32044. EXPECT_DECLS;
  32045. tcp_ready ready;
  32046. func_args client_args;
  32047. func_args server_args;
  32048. THREAD_TYPE serverThread;
  32049. callback_functions server_cbf;
  32050. callback_functions client_cbf;
  32051. SOCKET_T sockfd = 0;
  32052. WOLFSSL_CTX* ctx = NULL;
  32053. WOLFSSL* ssl = NULL;
  32054. const char* publicName = "ech-public-name.com";
  32055. const char* privateName = "ech-private-name.com";
  32056. int privateNameLen = 20;
  32057. char reply[1024];
  32058. int replyLen = 0;
  32059. byte rawEchConfig[128];
  32060. word32 rawEchConfigLen = sizeof(rawEchConfig);
  32061. InitTcpReady(&ready);
  32062. ready.port = 22222;
  32063. XMEMSET(&client_args, 0, sizeof(func_args));
  32064. XMEMSET(&server_args, 0, sizeof(func_args));
  32065. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  32066. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  32067. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  32068. /* create the server context here so we can get the ech config */
  32069. ExpectNotNull(server_cbf.ctx =
  32070. wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  32071. /* generate ech config */
  32072. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(server_cbf.ctx,
  32073. publicName, 0, 0, 0));
  32074. /* get the config for the client to use */
  32075. ExpectIntEQ(WOLFSSL_SUCCESS,
  32076. wolfSSL_CTX_GetEchConfigs(server_cbf.ctx, rawEchConfig,
  32077. &rawEchConfigLen));
  32078. server_args.callbacks = &server_cbf;
  32079. server_args.signal = &ready;
  32080. /* start server task */
  32081. start_thread(server_task_ech, &server_args, &serverThread);
  32082. wait_tcp_ready(&server_args);
  32083. /* run as a TLS1.3 client */
  32084. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  32085. ExpectIntEQ(WOLFSSL_SUCCESS,
  32086. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  32087. ExpectIntEQ(WOLFSSL_SUCCESS,
  32088. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  32089. ExpectIntEQ(WOLFSSL_SUCCESS,
  32090. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  32091. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  32092. /* get connected the server task */
  32093. ExpectNotNull(ssl = wolfSSL_new(ctx));
  32094. /* set the ech configs for the client */
  32095. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, rawEchConfig,
  32096. rawEchConfigLen));
  32097. /* set the sni for the client */
  32098. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
  32099. privateName, privateNameLen));
  32100. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  32101. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  32102. ExpectIntEQ(wolfSSL_write(ssl, privateName, privateNameLen),
  32103. privateNameLen);
  32104. ExpectIntGT((replyLen = wolfSSL_read(ssl, reply, sizeof(reply))), 0);
  32105. /* add th null terminator for string compare */
  32106. reply[replyLen] = 0;
  32107. /* check that the server replied with the private name */
  32108. ExpectStrEQ(privateName, reply);
  32109. wolfSSL_free(ssl);
  32110. wolfSSL_CTX_free(ctx);
  32111. CloseSocket(sockfd);
  32112. join_thread(serverThread);
  32113. FreeTcpReady(&ready);
  32114. return EXPECT_RESULT();
  32115. }
  32116. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  32117. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  32118. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32119. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  32120. static int post_auth_version_cb(WOLFSSL* ssl)
  32121. {
  32122. EXPECT_DECLS;
  32123. /* do handshake and then test version error */
  32124. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  32125. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  32126. return EXPECT_RESULT();
  32127. }
  32128. static int post_auth_version_client_cb(WOLFSSL* ssl)
  32129. {
  32130. EXPECT_DECLS;
  32131. /* do handshake and then test version error */
  32132. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  32133. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  32134. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  32135. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  32136. /* check was added to error queue */
  32137. ExpectIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  32138. /* check the string matches expected string */
  32139. ExpectStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  32140. "WRONG_SSL_VERSION");
  32141. #endif
  32142. return EXPECT_RESULT();
  32143. }
  32144. static int post_auth_cb(WOLFSSL* ssl)
  32145. {
  32146. EXPECT_DECLS;
  32147. WOLFSSL_X509* x509 = NULL;
  32148. /* do handshake and then test version error */
  32149. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  32150. ExpectStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  32151. ExpectNull(x509 = wolfSSL_get_peer_certificate(ssl));
  32152. wolfSSL_X509_free(x509);
  32153. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  32154. return EXPECT_RESULT();
  32155. }
  32156. static int set_post_auth_cb(WOLFSSL* ssl)
  32157. {
  32158. if (!wolfSSL_is_server(ssl)) {
  32159. EXPECT_DECLS;
  32160. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  32161. return EXPECT_RESULT();
  32162. }
  32163. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  32164. return TEST_SUCCESS;
  32165. }
  32166. #endif
  32167. static int test_wolfSSL_Tls13_postauth(void)
  32168. {
  32169. EXPECT_DECLS;
  32170. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  32171. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32172. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  32173. test_ssl_cbf server_cbf;
  32174. test_ssl_cbf client_cbf;
  32175. /* test version failure doing post auth with TLS 1.2 connection */
  32176. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  32177. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  32178. server_cbf.method = wolfTLSv1_2_server_method;
  32179. server_cbf.ssl_ready = set_post_auth_cb;
  32180. server_cbf.on_result = post_auth_version_cb;
  32181. client_cbf.ssl_ready = set_post_auth_cb;
  32182. client_cbf.on_result = post_auth_version_client_cb;
  32183. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  32184. &server_cbf, NULL), TEST_SUCCESS);
  32185. /* tests on post auth with TLS 1.3 */
  32186. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  32187. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  32188. server_cbf.method = wolfTLSv1_3_server_method;
  32189. server_cbf.ssl_ready = set_post_auth_cb;
  32190. client_cbf.ssl_ready = set_post_auth_cb;
  32191. server_cbf.on_result = post_auth_cb;
  32192. client_cbf.on_result = NULL;
  32193. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  32194. &server_cbf, NULL), TEST_SUCCESS);
  32195. #endif
  32196. return EXPECT_RESULT();
  32197. }
  32198. static int test_wolfSSL_X509_NID(void)
  32199. {
  32200. EXPECT_DECLS;
  32201. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  32202. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  32203. int sigType;
  32204. int nameSz;
  32205. X509* cert = NULL;
  32206. EVP_PKEY* pubKeyTmp = NULL;
  32207. X509_NAME* name = NULL;
  32208. char commonName[80];
  32209. char countryName[80];
  32210. char localityName[80];
  32211. char stateName[80];
  32212. char orgName[80];
  32213. char orgUnit[80];
  32214. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  32215. /* convert cert from DER to internal WOLFSSL_X509 struct */
  32216. ExpectNotNull(cert = wolfSSL_X509_d2i(&cert, client_cert_der_2048,
  32217. sizeof_client_cert_der_2048));
  32218. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  32219. /* extract PUBLIC KEY from cert */
  32220. ExpectNotNull(pubKeyTmp = X509_get_pubkey(cert));
  32221. /* extract signatureType */
  32222. ExpectIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  32223. /* extract subjectName info */
  32224. ExpectNotNull(name = X509_get_subject_name(cert));
  32225. ExpectIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  32226. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  32227. NULL, 0)), 0);
  32228. ExpectIntEQ(nameSz, 15);
  32229. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  32230. commonName, sizeof(commonName))), 0);
  32231. ExpectIntEQ(nameSz, 15);
  32232. ExpectIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  32233. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  32234. commonName, 9)), 0);
  32235. ExpectIntEQ(nameSz, 8);
  32236. ExpectIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  32237. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  32238. countryName, sizeof(countryName))), 0);
  32239. ExpectIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  32240. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  32241. localityName, sizeof(localityName))), 0);
  32242. ExpectIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  32243. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  32244. NID_stateOrProvinceName, stateName, sizeof(stateName))), 0);
  32245. ExpectIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  32246. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  32247. orgName, sizeof(orgName))), 0);
  32248. ExpectIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  32249. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  32250. NID_organizationalUnitName, orgUnit, sizeof(orgUnit))), 0);
  32251. ExpectIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  32252. EVP_PKEY_free(pubKeyTmp);
  32253. X509_free(cert);
  32254. #endif
  32255. return EXPECT_RESULT();
  32256. }
  32257. static int test_wolfSSL_CTX_set_srp_username(void)
  32258. {
  32259. EXPECT_DECLS;
  32260. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  32261. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  32262. WOLFSSL_CTX* ctx = NULL;
  32263. WOLFSSL* ssl = NULL;
  32264. const char *username = "TESTUSER";
  32265. const char *password = "TESTPASSWORD";
  32266. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32267. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  32268. SSL_SUCCESS);
  32269. wolfSSL_CTX_free(ctx);
  32270. ctx = NULL;
  32271. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32272. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  32273. SSL_SUCCESS);
  32274. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  32275. SSL_SUCCESS);
  32276. ExpectNotNull(ssl = SSL_new(ctx));
  32277. ExpectNotNull(SSL_get_srp_username(ssl));
  32278. ExpectStrEQ(SSL_get_srp_username(ssl), username);
  32279. wolfSSL_free(ssl);
  32280. wolfSSL_CTX_free(ctx);
  32281. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  32282. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  32283. return EXPECT_RESULT();
  32284. }
  32285. static int test_wolfSSL_CTX_set_srp_password(void)
  32286. {
  32287. EXPECT_DECLS;
  32288. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) && \
  32289. !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  32290. WOLFSSL_CTX* ctx = NULL;
  32291. const char *username = "TESTUSER";
  32292. const char *password = "TESTPASSWORD";
  32293. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32294. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  32295. SSL_SUCCESS);
  32296. wolfSSL_CTX_free(ctx);
  32297. ctx = NULL;
  32298. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32299. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  32300. SSL_SUCCESS);
  32301. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  32302. SSL_SUCCESS);
  32303. wolfSSL_CTX_free(ctx);
  32304. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  32305. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  32306. return EXPECT_RESULT();
  32307. }
  32308. static int test_wolfSSL_X509_STORE(void)
  32309. {
  32310. EXPECT_DECLS;
  32311. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32312. X509_STORE *store = NULL;
  32313. #ifdef HAVE_CRL
  32314. X509_STORE_CTX *storeCtx = NULL;
  32315. X509_CRL *crl = NULL;
  32316. X509 *ca = NULL;
  32317. X509 *cert = NULL;
  32318. const char crlPem[] = "./certs/crl/crl.revoked";
  32319. const char srvCert[] = "./certs/server-revoked-cert.pem";
  32320. const char caCert[] = "./certs/ca-cert.pem";
  32321. XFILE fp = XBADFILE;
  32322. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  32323. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  32324. SSL_FILETYPE_PEM)));
  32325. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  32326. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  32327. SSL_FILETYPE_PEM)));
  32328. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  32329. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  32330. ExpectIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  32331. X509_STORE_free(store);
  32332. store = NULL;
  32333. X509_STORE_CTX_free(storeCtx);
  32334. storeCtx = NULL;
  32335. X509_free(cert);
  32336. cert = NULL;
  32337. X509_free(ca);
  32338. ca = NULL;
  32339. /* should fail to verify now after adding in CRL */
  32340. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  32341. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  32342. SSL_FILETYPE_PEM)));
  32343. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  32344. ExpectTrue((fp = XFOPEN(crlPem, "rb")) != XBADFILE);
  32345. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  32346. NULL, NULL));
  32347. if (fp != XBADFILE)
  32348. XFCLOSE(fp);
  32349. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  32350. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  32351. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  32352. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  32353. SSL_FILETYPE_PEM)));
  32354. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  32355. ExpectIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  32356. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx),
  32357. WOLFSSL_X509_V_ERR_CERT_REVOKED);
  32358. X509_CRL_free(crl);
  32359. crl = NULL;
  32360. X509_STORE_free(store);
  32361. store = NULL;
  32362. X509_STORE_CTX_free(storeCtx);
  32363. storeCtx = NULL;
  32364. X509_free(cert);
  32365. cert = NULL;
  32366. X509_free(ca);
  32367. ca = NULL;
  32368. #endif /* HAVE_CRL */
  32369. #ifndef WOLFCRYPT_ONLY
  32370. {
  32371. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32372. SSL_CTX* ctx = NULL;
  32373. SSL* ssl = NULL;
  32374. int i;
  32375. for (i = 0; i < 2; i++) {
  32376. #ifndef NO_WOLFSSL_SERVER
  32377. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32378. #else
  32379. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32380. #endif
  32381. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  32382. SSL_CTX_set_cert_store(ctx, store);
  32383. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  32384. SSL_CTX_set_cert_store(ctx, store);
  32385. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  32386. ExpectIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  32387. SSL_FILETYPE_PEM), SSL_SUCCESS);
  32388. ExpectIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  32389. SSL_FILETYPE_PEM), SSL_SUCCESS);
  32390. ExpectNotNull(ssl = SSL_new(ctx));
  32391. if (i == 0) {
  32392. ExpectIntEQ(SSL_set0_verify_cert_store(ssl, store),
  32393. SSL_SUCCESS);
  32394. }
  32395. else {
  32396. ExpectIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  32397. #ifdef OPENSSL_ALL
  32398. ExpectIntEQ(SSL_CTX_set1_verify_cert_store(ctx, store), SSL_SUCCESS);
  32399. #endif
  32400. }
  32401. if (EXPECT_FAIL() || (i == 1)) {
  32402. X509_STORE_free(store);
  32403. store = NULL;
  32404. }
  32405. SSL_free(ssl);
  32406. ssl = NULL;
  32407. SSL_CTX_free(ctx);
  32408. ctx = NULL;
  32409. }
  32410. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32411. }
  32412. #endif
  32413. #endif
  32414. return EXPECT_RESULT();
  32415. }
  32416. static int test_wolfSSL_X509_STORE_load_locations(void)
  32417. {
  32418. EXPECT_DECLS;
  32419. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  32420. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  32421. SSL_CTX *ctx = NULL;
  32422. X509_STORE *store = NULL;
  32423. const char ca_file[] = "./certs/ca-cert.pem";
  32424. const char client_pem_file[] = "./certs/client-cert.pem";
  32425. const char client_der_file[] = "./certs/client-cert.der";
  32426. const char ecc_file[] = "./certs/ecc-key.pem";
  32427. const char certs_path[] = "./certs/";
  32428. const char bad_path[] = "./bad-path/";
  32429. #ifdef HAVE_CRL
  32430. const char crl_path[] = "./certs/crl/";
  32431. const char crl_file[] = "./certs/crl/crl.pem";
  32432. #endif
  32433. #ifndef NO_WOLFSSL_SERVER
  32434. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  32435. #else
  32436. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  32437. #endif
  32438. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  32439. ExpectIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL),
  32440. WOLFSSL_SUCCESS);
  32441. /* Test bad arguments */
  32442. ExpectIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL),
  32443. WOLFSSL_FAILURE);
  32444. ExpectIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  32445. ExpectIntEQ(X509_STORE_load_locations(store, client_der_file, NULL),
  32446. WOLFSSL_FAILURE);
  32447. ExpectIntEQ(X509_STORE_load_locations(store, ecc_file, NULL),
  32448. WOLFSSL_FAILURE);
  32449. ExpectIntEQ(X509_STORE_load_locations(store, NULL, bad_path),
  32450. WOLFSSL_FAILURE);
  32451. #ifdef HAVE_CRL
  32452. /* Test with CRL */
  32453. ExpectIntEQ(X509_STORE_load_locations(store, crl_file, NULL),
  32454. WOLFSSL_SUCCESS);
  32455. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crl_path),
  32456. WOLFSSL_SUCCESS);
  32457. #endif
  32458. /* Test with CA */
  32459. ExpectIntEQ(X509_STORE_load_locations(store, ca_file, NULL),
  32460. WOLFSSL_SUCCESS);
  32461. /* Test with client_cert and certs path */
  32462. ExpectIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL),
  32463. WOLFSSL_SUCCESS);
  32464. ExpectIntEQ(X509_STORE_load_locations(store, NULL, certs_path),
  32465. WOLFSSL_SUCCESS);
  32466. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  32467. /* Clear nodes */
  32468. ERR_clear_error();
  32469. #endif
  32470. SSL_CTX_free(ctx);
  32471. #endif
  32472. return EXPECT_RESULT();
  32473. }
  32474. static int test_X509_STORE_get0_objects(void)
  32475. {
  32476. EXPECT_DECLS;
  32477. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  32478. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  32479. X509_STORE *store = NULL;
  32480. X509_STORE *store_cpy = NULL;
  32481. SSL_CTX *ctx = NULL;
  32482. X509_OBJECT *obj = NULL;
  32483. STACK_OF(X509_OBJECT) *objs = NULL;
  32484. int i;
  32485. /* Setup store */
  32486. #ifndef NO_WOLFSSL_SERVER
  32487. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  32488. #else
  32489. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  32490. #endif
  32491. ExpectNotNull(store_cpy = X509_STORE_new());
  32492. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  32493. ExpectIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL),
  32494. WOLFSSL_SUCCESS);
  32495. ExpectIntEQ(X509_STORE_load_locations(store, caCertFile, NULL),
  32496. WOLFSSL_SUCCESS);
  32497. ExpectIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL),
  32498. WOLFSSL_SUCCESS);
  32499. #ifdef HAVE_CRL
  32500. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir),
  32501. WOLFSSL_SUCCESS);
  32502. #endif
  32503. /* Store ready */
  32504. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  32505. ExpectNotNull(objs = X509_STORE_get0_objects(store));
  32506. #ifdef HAVE_CRL
  32507. #ifdef WOLFSSL_SIGNER_DER_CERT
  32508. ExpectIntEQ(sk_X509_OBJECT_num(objs), 4);
  32509. #else
  32510. ExpectIntEQ(sk_X509_OBJECT_num(objs), 1);
  32511. #endif
  32512. #else
  32513. #ifdef WOLFSSL_SIGNER_DER_CERT
  32514. ExpectIntEQ(sk_X509_OBJECT_num(objs), 3);
  32515. #else
  32516. ExpectIntEQ(sk_X509_OBJECT_num(objs), 0);
  32517. #endif
  32518. #endif
  32519. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  32520. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  32521. switch (X509_OBJECT_get_type(obj)) {
  32522. case X509_LU_X509:
  32523. {
  32524. WOLFSSL_X509* x509;
  32525. ExpectNotNull(x509 = X509_OBJECT_get0_X509(obj));
  32526. ExpectIntEQ(X509_STORE_add_cert(store_cpy, x509), WOLFSSL_SUCCESS);
  32527. break;
  32528. }
  32529. case X509_LU_CRL:
  32530. #ifdef HAVE_CRL
  32531. {
  32532. WOLFSSL_CRL* crl = NULL;
  32533. ExpectNotNull(crl = X509_OBJECT_get0_X509_CRL(obj));
  32534. ExpectIntEQ(X509_STORE_add_crl(store_cpy, crl), WOLFSSL_SUCCESS);
  32535. break;
  32536. }
  32537. #endif
  32538. case X509_LU_NONE:
  32539. default:
  32540. Fail(("X509_OBJECT_get_type should return x509 or crl "
  32541. "(when built with crl support)"),
  32542. ("Unrecognized X509_OBJECT type or none"));
  32543. }
  32544. }
  32545. X509_STORE_free(store_cpy);
  32546. SSL_CTX_free(ctx);
  32547. #endif
  32548. return EXPECT_RESULT();
  32549. }
  32550. static int test_wolfSSL_BN_CTX(void)
  32551. {
  32552. EXPECT_DECLS;
  32553. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  32554. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  32555. WOLFSSL_BN_CTX* bn_ctx = NULL;
  32556. WOLFSSL_BIGNUM* t = NULL;
  32557. ExpectNotNull(bn_ctx = wolfSSL_BN_CTX_new());
  32558. /* No implementation. */
  32559. BN_CTX_init(NULL);
  32560. ExpectNotNull(t = BN_CTX_get(NULL));
  32561. BN_free(t);
  32562. ExpectNotNull(t = BN_CTX_get(bn_ctx));
  32563. BN_free(t);
  32564. #ifndef NO_WOLFSSL_STUB
  32565. /* No implementation. */
  32566. BN_CTX_start(NULL);
  32567. BN_CTX_start(bn_ctx);
  32568. #endif
  32569. BN_CTX_free(NULL);
  32570. BN_CTX_free(bn_ctx);
  32571. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  32572. return EXPECT_RESULT();
  32573. }
  32574. static int test_wolfSSL_BN(void)
  32575. {
  32576. EXPECT_DECLS;
  32577. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  32578. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  32579. BIGNUM* a = NULL;
  32580. BIGNUM* b = NULL;
  32581. BIGNUM* c = NULL;
  32582. BIGNUM* d = NULL;
  32583. BIGNUM emptyBN;
  32584. /* Setup */
  32585. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  32586. /* internal not set emptyBN. */
  32587. ExpectNotNull(a = BN_new());
  32588. ExpectNotNull(b = BN_new());
  32589. ExpectNotNull(c = BN_dup(b));
  32590. ExpectNotNull(d = BN_new());
  32591. /* Invalid parameter testing. */
  32592. BN_free(NULL);
  32593. ExpectNull(BN_dup(NULL));
  32594. ExpectNull(BN_dup(&emptyBN));
  32595. ExpectNull(BN_copy(NULL, NULL));
  32596. ExpectNull(BN_copy(b, NULL));
  32597. ExpectNull(BN_copy(NULL, c));
  32598. ExpectNull(BN_copy(b, &emptyBN));
  32599. ExpectNull(BN_copy(&emptyBN, c));
  32600. BN_clear(NULL);
  32601. BN_clear(&emptyBN);
  32602. ExpectIntEQ(BN_num_bytes(NULL), 0);
  32603. ExpectIntEQ(BN_num_bytes(&emptyBN), 0);
  32604. ExpectIntEQ(BN_num_bits(NULL), 0);
  32605. ExpectIntEQ(BN_num_bits(&emptyBN), 0);
  32606. ExpectIntEQ(BN_is_negative(NULL), 0);
  32607. ExpectIntEQ(BN_is_negative(&emptyBN), 0);
  32608. /* END Invalid Parameters */
  32609. ExpectIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  32610. ExpectIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  32611. ExpectIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  32612. ExpectIntEQ(BN_num_bits(a), 2);
  32613. ExpectIntEQ(BN_num_bytes(a), 1);
  32614. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  32615. defined(WOLFSSL_SP_INT_NEGATIVE))
  32616. ExpectIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  32617. ExpectIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  32618. ExpectIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  32619. ExpectIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  32620. ExpectIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  32621. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  32622. {
  32623. /* Do additional tests on negative BN conversions. */
  32624. char* ret = NULL;
  32625. ASN1_INTEGER* asn1 = NULL;
  32626. BIGNUM* tmp = NULL;
  32627. /* Sanity check we have a negative BN. */
  32628. ExpectIntEQ(BN_is_negative(c), 1);
  32629. ExpectNotNull(ret = BN_bn2dec(c));
  32630. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  32631. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  32632. ret = NULL;
  32633. /* Convert to ASN1_INTEGER and back to BN. */
  32634. ExpectNotNull(asn1 = BN_to_ASN1_INTEGER(c, NULL));
  32635. ExpectNotNull(tmp = ASN1_INTEGER_to_BN(asn1, NULL));
  32636. /* After converting back BN should be negative and correct. */
  32637. ExpectIntEQ(BN_is_negative(tmp), 1);
  32638. ExpectNotNull(ret = BN_bn2dec(tmp));
  32639. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  32640. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  32641. ASN1_INTEGER_free(asn1);
  32642. BN_free(tmp);
  32643. }
  32644. #endif
  32645. ExpectIntEQ(BN_get_word(c), 4);
  32646. #endif
  32647. ExpectIntEQ(BN_set_word(a, 3), 1);
  32648. ExpectIntEQ(BN_set_word(b, 3), 1);
  32649. ExpectIntEQ(BN_set_word(c, 4), 1);
  32650. /* NULL == NULL, NULL < num, num > NULL */
  32651. ExpectIntEQ(BN_cmp(NULL, NULL), 0);
  32652. ExpectIntEQ(BN_cmp(&emptyBN, &emptyBN), 0);
  32653. ExpectIntLT(BN_cmp(NULL, b), 0);
  32654. ExpectIntLT(BN_cmp(&emptyBN, b), 0);
  32655. ExpectIntGT(BN_cmp(a, NULL), 0);
  32656. ExpectIntGT(BN_cmp(a, &emptyBN), 0);
  32657. ExpectIntEQ(BN_cmp(a, b), 0);
  32658. ExpectIntLT(BN_cmp(a, c), 0);
  32659. ExpectIntGT(BN_cmp(c, b), 0);
  32660. ExpectIntEQ(BN_print_fp(XBADFILE, NULL), 0);
  32661. ExpectIntEQ(BN_print_fp(XBADFILE, &emptyBN), 0);
  32662. ExpectIntEQ(BN_print_fp(stderr, NULL), 0);
  32663. ExpectIntEQ(BN_print_fp(stderr, &emptyBN), 0);
  32664. ExpectIntEQ(BN_print_fp(XBADFILE, a), 0);
  32665. ExpectIntEQ(BN_print_fp(stderr, a), 1);
  32666. BN_clear(a);
  32667. BN_free(a);
  32668. BN_free(b);
  32669. BN_free(c);
  32670. BN_clear_free(d);
  32671. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  32672. return EXPECT_RESULT();
  32673. }
  32674. static int test_wolfSSL_BN_init(void)
  32675. {
  32676. EXPECT_DECLS;
  32677. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  32678. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  32679. #if !defined(USE_INTEGER_HEAP_MATH) && !defined(HAVE_WOLF_BIGINT)
  32680. BIGNUM* ap = NULL;
  32681. BIGNUM bv;
  32682. BIGNUM cv;
  32683. BIGNUM dv;
  32684. ExpectNotNull(ap = BN_new());
  32685. BN_init(NULL);
  32686. XMEMSET(&bv, 0, sizeof(bv));
  32687. ExpectNull(BN_dup(&bv));
  32688. BN_init(&bv);
  32689. BN_init(&cv);
  32690. BN_init(&dv);
  32691. ExpectIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  32692. ExpectIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  32693. ExpectIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  32694. /* a^b mod c = */
  32695. ExpectIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  32696. ExpectIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  32697. /* check result 3^2 mod 5 */
  32698. ExpectIntEQ(BN_get_word(&dv), 4);
  32699. /* a*b mod c = */
  32700. ExpectIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  32701. ExpectIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  32702. /* check result 3*2 mod 5 */
  32703. ExpectIntEQ(BN_get_word(&dv), 1);
  32704. BN_free(ap);
  32705. #endif
  32706. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  32707. return EXPECT_RESULT();
  32708. }
  32709. static int test_wolfSSL_BN_enc_dec(void)
  32710. {
  32711. EXPECT_DECLS;
  32712. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  32713. BIGNUM* a = NULL;
  32714. BIGNUM* b = NULL;
  32715. BIGNUM* c = NULL;
  32716. BIGNUM emptyBN;
  32717. char* str = NULL;
  32718. const char* emptyStr = "";
  32719. const char* numberStr = "12345";
  32720. const char* badStr = "g12345";
  32721. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  32722. const char* twoStr = "2";
  32723. #endif
  32724. unsigned char binNum[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  32725. unsigned char outNum[5];
  32726. /* Setup */
  32727. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  32728. ExpectNotNull(a = BN_new());
  32729. ExpectNotNull(b = BN_new());
  32730. ExpectIntEQ(BN_set_word(a, 2), 1);
  32731. /* Invalid parameters */
  32732. ExpectIntEQ(BN_bn2bin(NULL, NULL), -1);
  32733. ExpectIntEQ(BN_bn2bin(&emptyBN, NULL), -1);
  32734. ExpectIntEQ(BN_bn2bin(NULL, outNum), -1);
  32735. ExpectIntEQ(BN_bn2bin(&emptyBN, outNum), -1);
  32736. ExpectNull(BN_bn2hex(NULL));
  32737. ExpectNull(BN_bn2hex(&emptyBN));
  32738. ExpectNull(BN_bn2dec(NULL));
  32739. ExpectNull(BN_bn2dec(&emptyBN));
  32740. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), NULL));
  32741. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), a));
  32742. ExpectNull(BN_bin2bn(binNum, -1, a));
  32743. ExpectNull(BN_bin2bn(binNum, -1, NULL));
  32744. ExpectNull(BN_bin2bn(binNum, sizeof(binNum), &emptyBN));
  32745. ExpectIntEQ(BN_hex2bn(NULL, NULL), 0);
  32746. ExpectIntEQ(BN_hex2bn(NULL, numberStr), 0);
  32747. ExpectIntEQ(BN_hex2bn(&a, NULL), 0);
  32748. ExpectIntEQ(BN_hex2bn(&a, emptyStr), 0);
  32749. ExpectIntEQ(BN_hex2bn(&a, badStr), 0);
  32750. ExpectIntEQ(BN_hex2bn(&c, badStr), 0);
  32751. ExpectIntEQ(BN_dec2bn(NULL, NULL), 0);
  32752. ExpectIntEQ(BN_dec2bn(NULL, numberStr), 0);
  32753. ExpectIntEQ(BN_dec2bn(&a, NULL), 0);
  32754. ExpectIntEQ(BN_dec2bn(&a, emptyStr), 0);
  32755. ExpectIntEQ(BN_dec2bn(&a, badStr), 0);
  32756. ExpectIntEQ(BN_dec2bn(&c, badStr), 0);
  32757. ExpectIntEQ(BN_set_word(a, 2), 1);
  32758. ExpectIntEQ(BN_bn2bin(a, NULL), 1);
  32759. ExpectIntEQ(BN_bn2bin(a, outNum), 1);
  32760. ExpectNotNull(BN_bin2bn(outNum, 1, b));
  32761. ExpectIntEQ(BN_cmp(a, b), 0);
  32762. ExpectNotNull(BN_bin2bn(binNum, sizeof(binNum), b));
  32763. ExpectIntEQ(BN_cmp(a, b), -1);
  32764. ExpectNotNull(str = BN_bn2hex(a));
  32765. ExpectNotNull(BN_hex2bn(&b, str));
  32766. ExpectIntEQ(BN_cmp(a, b), 0);
  32767. ExpectNotNull(BN_hex2bn(&b, numberStr));
  32768. ExpectIntEQ(BN_cmp(a, b), -1);
  32769. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  32770. str = NULL;
  32771. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  32772. ExpectNotNull(str = BN_bn2dec(a));
  32773. ExpectStrEQ(str, twoStr);
  32774. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  32775. str = NULL;
  32776. #ifndef NO_RSA
  32777. ExpectNotNull(str = BN_bn2dec(a));
  32778. ExpectNotNull(BN_dec2bn(&b, str));
  32779. ExpectIntEQ(BN_cmp(a, b), 0);
  32780. ExpectNotNull(BN_dec2bn(&b, numberStr));
  32781. ExpectIntEQ(BN_cmp(a, b), -1);
  32782. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  32783. str = NULL;
  32784. #else
  32785. /* No implementation - fail with good parameters. */
  32786. ExpectIntEQ(BN_dec2bn(&a, numberStr), 0);
  32787. #endif
  32788. #endif
  32789. BN_free(b);
  32790. BN_free(a);
  32791. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  32792. return EXPECT_RESULT();
  32793. }
  32794. static int test_wolfSSL_BN_word(void)
  32795. {
  32796. EXPECT_DECLS;
  32797. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  32798. BIGNUM* a = NULL;
  32799. BIGNUM* b = NULL;
  32800. BIGNUM* c = NULL;
  32801. BIGNUM av;
  32802. ExpectNotNull(a = BN_new());
  32803. ExpectNotNull(b = BN_new());
  32804. ExpectNotNull(c = BN_new());
  32805. XMEMSET(&av, 0, sizeof(av));
  32806. /* Invalid parameter. */
  32807. ExpectIntEQ(BN_add_word(NULL, 3), 0);
  32808. ExpectIntEQ(BN_add_word(&av, 3), 0);
  32809. ExpectIntEQ(BN_sub_word(NULL, 3), 0);
  32810. ExpectIntEQ(BN_sub_word(&av, 3), 0);
  32811. ExpectIntEQ(BN_set_word(NULL, 3), 0);
  32812. ExpectIntEQ(BN_set_word(&av, 3), 0);
  32813. ExpectIntEQ(BN_get_word(NULL), 0);
  32814. ExpectIntEQ(BN_get_word(&av), 0);
  32815. ExpectIntEQ(BN_is_word(NULL, 3), 0);
  32816. ExpectIntEQ(BN_is_word(&av, 3), 0);
  32817. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  32818. !defined(NO_DSA))
  32819. ExpectIntEQ(BN_mod_word(NULL, 3), -1);
  32820. ExpectIntEQ(BN_mod_word(&av, 3), -1);
  32821. #endif
  32822. ExpectIntEQ(BN_one(NULL), 0);
  32823. ExpectIntEQ(BN_one(&av), 0);
  32824. BN_zero(NULL);
  32825. BN_zero(&av);
  32826. ExpectIntEQ(BN_is_one(NULL), 0);
  32827. ExpectIntEQ(BN_is_one(&av), 0);
  32828. ExpectIntEQ(BN_is_zero(NULL), 0);
  32829. ExpectIntEQ(BN_is_zero(&av), 0);
  32830. ExpectIntEQ(BN_set_word(a, 3), 1);
  32831. ExpectIntEQ(BN_set_word(b, 2), 1);
  32832. ExpectIntEQ(BN_set_word(c, 5), 1);
  32833. /* a + 3 = */
  32834. ExpectIntEQ(BN_add_word(a, 3), 1);
  32835. /* check result 3 + 3*/
  32836. ExpectIntEQ(BN_get_word(a), 6);
  32837. ExpectIntEQ(BN_is_word(a, 6), 1);
  32838. ExpectIntEQ(BN_is_word(a, 5), 0);
  32839. /* set a back to 3 */
  32840. ExpectIntEQ(BN_set_word(a, 3), 1);
  32841. /* a - 3 = */
  32842. ExpectIntEQ(BN_sub_word(a, 3), 1);
  32843. /* check result 3 - 3*/
  32844. ExpectIntEQ(BN_get_word(a), 0);
  32845. ExpectIntEQ(BN_one(a), 1);
  32846. ExpectIntEQ(BN_is_word(a, 1), 1);
  32847. ExpectIntEQ(BN_is_word(a, 0), 0);
  32848. ExpectIntEQ(BN_is_one(a), 1);
  32849. ExpectIntEQ(BN_is_zero(a), 0);
  32850. BN_zero(a);
  32851. ExpectIntEQ(BN_is_word(a, 0), 1);
  32852. ExpectIntEQ(BN_is_word(a, 1), 0);
  32853. ExpectIntEQ(BN_is_zero(a), 1);
  32854. ExpectIntEQ(BN_is_one(a), 0);
  32855. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  32856. !defined(NO_DSA))
  32857. ExpectIntEQ(BN_set_word(a, 5), 1);
  32858. ExpectIntEQ(BN_mod_word(a, 3), 2);
  32859. ExpectIntEQ(BN_mod_word(a, 0), -1);
  32860. #endif
  32861. BN_free(c);
  32862. BN_free(b);
  32863. BN_free(a);
  32864. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  32865. return EXPECT_RESULT();
  32866. }
  32867. static int test_wolfSSL_BN_bits(void)
  32868. {
  32869. EXPECT_DECLS;
  32870. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  32871. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  32872. BIGNUM* a = NULL;
  32873. BIGNUM emptyBN;
  32874. /* Setup */
  32875. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  32876. ExpectNotNull(a = BN_new());
  32877. /* Invalid parameters. */
  32878. ExpectIntEQ(BN_set_bit(NULL, 1), 0);
  32879. ExpectIntEQ(BN_set_bit(&emptyBN, 1), 0);
  32880. ExpectIntEQ(BN_set_bit(a, -1), 0);
  32881. ExpectIntEQ(BN_clear_bit(NULL, 1), 0);
  32882. ExpectIntEQ(BN_clear_bit(&emptyBN, 1), 0);
  32883. ExpectIntEQ(BN_clear_bit(a, -1), 0);
  32884. ExpectIntEQ(BN_is_bit_set(NULL, 1), 0);
  32885. ExpectIntEQ(BN_is_bit_set(&emptyBN, 1), 0);
  32886. ExpectIntEQ(BN_is_bit_set(a, -1), 0);
  32887. ExpectIntEQ(BN_is_odd(NULL), 0);
  32888. ExpectIntEQ(BN_is_odd(&emptyBN), 0);
  32889. ExpectIntEQ(BN_set_word(a, 0), 1);
  32890. ExpectIntEQ(BN_is_zero(a), 1);
  32891. ExpectIntEQ(BN_set_bit(a, 0x45), 1);
  32892. ExpectIntEQ(BN_is_zero(a), 0);
  32893. ExpectIntEQ(BN_is_bit_set(a, 0x45), 1);
  32894. ExpectIntEQ(BN_clear_bit(a, 0x45), 1);
  32895. ExpectIntEQ(BN_is_bit_set(a, 0x45), 0);
  32896. ExpectIntEQ(BN_is_zero(a), 1);
  32897. ExpectIntEQ(BN_set_bit(a, 0), 1);
  32898. ExpectIntEQ(BN_is_odd(a), 1);
  32899. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  32900. ExpectIntEQ(BN_is_odd(a), 0);
  32901. ExpectIntEQ(BN_set_bit(a, 1), 1);
  32902. ExpectIntEQ(BN_is_odd(a), 0);
  32903. ExpectIntEQ(BN_set_bit(a, 129), 1);
  32904. ExpectIntEQ(BN_get_word(a), WOLFSSL_BN_MAX_VAL);
  32905. #ifndef NO_WOLFSSL_STUB
  32906. ExpectIntEQ(BN_mask_bits(a, 1), 0);
  32907. #endif
  32908. BN_free(a);
  32909. #endif
  32910. return EXPECT_RESULT();
  32911. }
  32912. static int test_wolfSSL_BN_shift(void)
  32913. {
  32914. EXPECT_DECLS;
  32915. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  32916. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  32917. BIGNUM* a = NULL;
  32918. BIGNUM* b = NULL;
  32919. BIGNUM emptyBN;
  32920. /* Setup */
  32921. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  32922. ExpectNotNull(a = BN_new());
  32923. ExpectNotNull(b = BN_new());
  32924. /* Invalid parameters. */
  32925. ExpectIntEQ(BN_lshift(NULL, NULL, 1), 0);
  32926. ExpectIntEQ(BN_lshift(&emptyBN, NULL, 1), 0);
  32927. ExpectIntEQ(BN_lshift(NULL, &emptyBN, 1), 0);
  32928. ExpectIntEQ(BN_lshift(b, NULL, 1), 0);
  32929. ExpectIntEQ(BN_lshift(b, &emptyBN, 1), 0);
  32930. ExpectIntEQ(BN_lshift(NULL, a, 1), 0);
  32931. ExpectIntEQ(BN_lshift(&emptyBN, a, 1), 0);
  32932. ExpectIntEQ(BN_lshift(b, a, -1), 0);
  32933. ExpectIntEQ(BN_rshift(NULL, NULL, 1), 0);
  32934. ExpectIntEQ(BN_rshift(&emptyBN, NULL, 1), 0);
  32935. ExpectIntEQ(BN_rshift(NULL, &emptyBN, 1), 0);
  32936. ExpectIntEQ(BN_rshift(b, NULL, 1), 0);
  32937. ExpectIntEQ(BN_rshift(b, &emptyBN, 1), 0);
  32938. ExpectIntEQ(BN_rshift(NULL, a, 1), 0);
  32939. ExpectIntEQ(BN_rshift(&emptyBN, a, 1), 0);
  32940. ExpectIntEQ(BN_rshift(b, a, -1), 0);
  32941. ExpectIntEQ(BN_set_word(a, 1), 1);
  32942. ExpectIntEQ(BN_lshift(b, a, 1), 1);
  32943. ExpectIntEQ(BN_is_word(b, 2), 1);
  32944. ExpectIntEQ(BN_lshift(a, a, 1), 1);
  32945. ExpectIntEQ(BN_is_word(a, 2), 1);
  32946. ExpectIntEQ(BN_rshift(b, a, 1), 1);
  32947. ExpectIntEQ(BN_is_word(b, 1), 1);
  32948. ExpectIntEQ(BN_rshift(a, a, 1), 1);
  32949. ExpectIntEQ(BN_is_word(a, 1), 1);
  32950. BN_free(b);
  32951. BN_free(a);
  32952. #endif
  32953. return EXPECT_RESULT();
  32954. }
  32955. static int test_wolfSSL_BN_math(void)
  32956. {
  32957. EXPECT_DECLS;
  32958. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  32959. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  32960. BIGNUM* a = NULL;
  32961. BIGNUM* b = NULL;
  32962. BIGNUM* r = NULL;
  32963. BIGNUM* rem = NULL;
  32964. BIGNUM emptyBN;
  32965. BN_ULONG val1;
  32966. BN_ULONG val2;
  32967. /* Setup */
  32968. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  32969. ExpectNotNull(a = BN_new());
  32970. ExpectNotNull(b = BN_new());
  32971. ExpectNotNull(r = BN_new());
  32972. ExpectNotNull(rem = BN_new());
  32973. /* Invalid parameters. */
  32974. ExpectIntEQ(BN_add(NULL, NULL, NULL), 0);
  32975. ExpectIntEQ(BN_add(r, NULL, NULL), 0);
  32976. ExpectIntEQ(BN_add(NULL, a, NULL), 0);
  32977. ExpectIntEQ(BN_add(NULL, NULL, b), 0);
  32978. ExpectIntEQ(BN_add(r, a, NULL), 0);
  32979. ExpectIntEQ(BN_add(r, NULL, b), 0);
  32980. ExpectIntEQ(BN_add(NULL, a, b), 0);
  32981. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, &emptyBN), 0);
  32982. ExpectIntEQ(BN_add(r, &emptyBN, &emptyBN), 0);
  32983. ExpectIntEQ(BN_add(&emptyBN, a, &emptyBN), 0);
  32984. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, b), 0);
  32985. ExpectIntEQ(BN_add(r, a, &emptyBN), 0);
  32986. ExpectIntEQ(BN_add(r, &emptyBN, b), 0);
  32987. ExpectIntEQ(BN_add(&emptyBN, a, b), 0);
  32988. ExpectIntEQ(BN_sub(NULL, NULL, NULL), 0);
  32989. ExpectIntEQ(BN_sub(r, NULL, NULL), 0);
  32990. ExpectIntEQ(BN_sub(NULL, a, NULL), 0);
  32991. ExpectIntEQ(BN_sub(NULL, NULL, b), 0);
  32992. ExpectIntEQ(BN_sub(r, a, NULL), 0);
  32993. ExpectIntEQ(BN_sub(r, NULL, b), 0);
  32994. ExpectIntEQ(BN_sub(NULL, a, b), 0);
  32995. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, &emptyBN), 0);
  32996. ExpectIntEQ(BN_sub(r, &emptyBN, &emptyBN), 0);
  32997. ExpectIntEQ(BN_sub(&emptyBN, a, &emptyBN), 0);
  32998. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, b), 0);
  32999. ExpectIntEQ(BN_sub(r, a, &emptyBN), 0);
  33000. ExpectIntEQ(BN_sub(r, &emptyBN, b), 0);
  33001. ExpectIntEQ(BN_sub(&emptyBN, a, b), 0);
  33002. ExpectIntEQ(BN_mul(NULL, NULL, NULL, NULL), 0);
  33003. ExpectIntEQ(BN_mul(r, NULL, NULL, NULL), 0);
  33004. ExpectIntEQ(BN_mul(NULL, a, NULL, NULL), 0);
  33005. ExpectIntEQ(BN_mul(NULL, NULL, b, NULL), 0);
  33006. ExpectIntEQ(BN_mul(r, a, NULL, NULL), 0);
  33007. ExpectIntEQ(BN_mul(r, NULL, b, NULL), 0);
  33008. ExpectIntEQ(BN_mul(NULL, a, b, NULL), 0);
  33009. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33010. ExpectIntEQ(BN_mul(r, &emptyBN, &emptyBN, NULL), 0);
  33011. ExpectIntEQ(BN_mul(&emptyBN, a, &emptyBN, NULL), 0);
  33012. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, b, NULL), 0);
  33013. ExpectIntEQ(BN_mul(r, a, &emptyBN, NULL), 0);
  33014. ExpectIntEQ(BN_mul(r, &emptyBN, b, NULL), 0);
  33015. ExpectIntEQ(BN_mul(&emptyBN, a, b, NULL), 0);
  33016. ExpectIntEQ(BN_div(NULL, NULL, NULL, NULL, NULL), 0);
  33017. ExpectIntEQ(BN_div(r, NULL, NULL, NULL, NULL), 0);
  33018. ExpectIntEQ(BN_div(NULL, rem, NULL, NULL, NULL), 0);
  33019. ExpectIntEQ(BN_div(NULL, NULL, a, NULL, NULL), 0);
  33020. ExpectIntEQ(BN_div(NULL, NULL, NULL, b, NULL), 0);
  33021. ExpectIntEQ(BN_div(NULL, rem, a, b, NULL), 0);
  33022. ExpectIntEQ(BN_div(r, NULL, a, b, NULL), 0);
  33023. ExpectIntEQ(BN_div(r, rem, NULL, b, NULL), 0);
  33024. ExpectIntEQ(BN_div(r, rem, a, NULL, NULL), 0);
  33025. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33026. ExpectIntEQ(BN_div(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33027. ExpectIntEQ(BN_div(&emptyBN, rem, &emptyBN, &emptyBN, NULL), 0);
  33028. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, a, &emptyBN, NULL), 0);
  33029. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, b, NULL), 0);
  33030. ExpectIntEQ(BN_div(&emptyBN, rem, a, b, NULL), 0);
  33031. ExpectIntEQ(BN_div(r, &emptyBN, a, b, NULL), 0);
  33032. ExpectIntEQ(BN_div(r, rem, &emptyBN, b, NULL), 0);
  33033. ExpectIntEQ(BN_div(r, rem, a, &emptyBN, NULL), 0);
  33034. ExpectIntEQ(BN_mod(NULL, NULL, NULL, NULL), 0);
  33035. ExpectIntEQ(BN_mod(r, NULL, NULL, NULL), 0);
  33036. ExpectIntEQ(BN_mod(NULL, a, NULL, NULL), 0);
  33037. ExpectIntEQ(BN_mod(NULL, NULL, b, NULL), 0);
  33038. ExpectIntEQ(BN_mod(r, a, NULL, NULL), 0);
  33039. ExpectIntEQ(BN_mod(r, NULL, b, NULL), 0);
  33040. ExpectIntEQ(BN_mod(NULL, a, b, NULL), 0);
  33041. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33042. ExpectIntEQ(BN_mod(r, &emptyBN, &emptyBN, NULL), 0);
  33043. ExpectIntEQ(BN_mod(&emptyBN, a, &emptyBN, NULL), 0);
  33044. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, b, NULL), 0);
  33045. ExpectIntEQ(BN_mod(r, a, &emptyBN, NULL), 0);
  33046. ExpectIntEQ(BN_mod(r, &emptyBN, b, NULL), 0);
  33047. ExpectIntEQ(BN_mod(&emptyBN, a, b, NULL), 0);
  33048. /* END Invalid parameters. */
  33049. val1 = 8;
  33050. val2 = 3;
  33051. ExpectIntEQ(BN_set_word(a, val1), 1);
  33052. ExpectIntEQ(BN_set_word(b, val2), 1);
  33053. ExpectIntEQ(BN_add(r, a, b), 1);
  33054. ExpectIntEQ(BN_is_word(r, val1 + val2), 1);
  33055. ExpectIntEQ(BN_sub(r, a, b), 1);
  33056. ExpectIntEQ(BN_is_word(r, val1 - val2), 1);
  33057. ExpectIntEQ(BN_mul(r, a, b, NULL), 1);
  33058. ExpectIntEQ(BN_is_word(r, val1 * val2), 1);
  33059. ExpectIntEQ(BN_div(r, rem, a, b, NULL), 1);
  33060. ExpectIntEQ(BN_is_word(r, val1 / val2), 1);
  33061. ExpectIntEQ(BN_is_word(rem, val1 % val2), 1);
  33062. ExpectIntEQ(BN_mod(r, a, b, NULL), 1);
  33063. ExpectIntEQ(BN_is_word(r, val1 % val2), 1);
  33064. BN_free(rem);
  33065. BN_free(r);
  33066. BN_free(b);
  33067. BN_free(a);
  33068. #endif
  33069. return EXPECT_RESULT();
  33070. }
  33071. static int test_wolfSSL_BN_math_mod(void)
  33072. {
  33073. EXPECT_DECLS;
  33074. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33075. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33076. BIGNUM* a = NULL;
  33077. BIGNUM* b = NULL;
  33078. BIGNUM* m = NULL;
  33079. BIGNUM* r = NULL;
  33080. BIGNUM* t = NULL;
  33081. BIGNUM emptyBN;
  33082. BN_ULONG val1;
  33083. BN_ULONG val2;
  33084. BN_ULONG val3;
  33085. /* Setup */
  33086. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33087. ExpectNotNull(a = BN_new());
  33088. ExpectNotNull(b = BN_new());
  33089. ExpectNotNull(m = BN_new());
  33090. ExpectNotNull(r = BN_new());
  33091. /* Invalid parameters. */
  33092. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, NULL, NULL), 0);
  33093. ExpectIntEQ(BN_mod_add(r, NULL, NULL, NULL, NULL), 0);
  33094. ExpectIntEQ(BN_mod_add(NULL, a, NULL, NULL, NULL), 0);
  33095. ExpectIntEQ(BN_mod_add(NULL, NULL, b, NULL, NULL), 0);
  33096. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, m, NULL), 0);
  33097. ExpectIntEQ(BN_mod_add(NULL, a, b, m, NULL), 0);
  33098. ExpectIntEQ(BN_mod_add(r, NULL, b, m, NULL), 0);
  33099. ExpectIntEQ(BN_mod_add(r, a, NULL, m, NULL), 0);
  33100. ExpectIntEQ(BN_mod_add(r, a, m, NULL, NULL), 0);
  33101. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33102. ExpectIntEQ(BN_mod_add(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33103. ExpectIntEQ(BN_mod_add(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  33104. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  33105. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  33106. ExpectIntEQ(BN_mod_add(&emptyBN, a, b, m, NULL), 0);
  33107. ExpectIntEQ(BN_mod_add(r, &emptyBN, b, m, NULL), 0);
  33108. ExpectIntEQ(BN_mod_add(r, a, &emptyBN, m, NULL), 0);
  33109. ExpectIntEQ(BN_mod_add(r, a, m, &emptyBN, NULL), 0);
  33110. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, NULL, NULL), 0);
  33111. ExpectIntEQ(BN_mod_mul(r, NULL, NULL, NULL, NULL), 0);
  33112. ExpectIntEQ(BN_mod_mul(NULL, a, NULL, NULL, NULL), 0);
  33113. ExpectIntEQ(BN_mod_mul(NULL, NULL, b, NULL, NULL), 0);
  33114. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, m, NULL), 0);
  33115. ExpectIntEQ(BN_mod_mul(NULL, a, b, m, NULL), 0);
  33116. ExpectIntEQ(BN_mod_mul(r, NULL, b, m, NULL), 0);
  33117. ExpectIntEQ(BN_mod_mul(r, a, NULL, m, NULL), 0);
  33118. ExpectIntEQ(BN_mod_mul(r, a, m, NULL, NULL), 0);
  33119. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33120. ExpectIntEQ(BN_mod_mul(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33121. ExpectIntEQ(BN_mod_mul(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  33122. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  33123. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  33124. ExpectIntEQ(BN_mod_mul(&emptyBN, a, b, m, NULL), 0);
  33125. ExpectIntEQ(BN_mod_mul(r, &emptyBN, b, m, NULL), 0);
  33126. ExpectIntEQ(BN_mod_mul(r, a, &emptyBN, m, NULL), 0);
  33127. ExpectIntEQ(BN_mod_mul(r, a, m, &emptyBN, NULL), 0);
  33128. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, NULL, NULL), 0);
  33129. ExpectIntEQ(BN_mod_exp(r, NULL, NULL, NULL, NULL), 0);
  33130. ExpectIntEQ(BN_mod_exp(NULL, a, NULL, NULL, NULL), 0);
  33131. ExpectIntEQ(BN_mod_exp(NULL, NULL, b, NULL, NULL), 0);
  33132. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, m, NULL), 0);
  33133. ExpectIntEQ(BN_mod_exp(NULL, a, b, m, NULL), 0);
  33134. ExpectIntEQ(BN_mod_exp(r, NULL, b, m, NULL), 0);
  33135. ExpectIntEQ(BN_mod_exp(r, a, NULL, m, NULL), 0);
  33136. ExpectIntEQ(BN_mod_exp(r, a, m, NULL, NULL), 0);
  33137. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33138. ExpectIntEQ(BN_mod_exp(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33139. ExpectIntEQ(BN_mod_exp(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  33140. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  33141. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  33142. ExpectIntEQ(BN_mod_exp(&emptyBN, a, b, m, NULL), 0);
  33143. ExpectIntEQ(BN_mod_exp(r, &emptyBN, b, m, NULL), 0);
  33144. ExpectIntEQ(BN_mod_exp(r, a, &emptyBN, m, NULL), 0);
  33145. ExpectIntEQ(BN_mod_exp(r, a, m, &emptyBN, NULL), 0);
  33146. ExpectNull(BN_mod_inverse(r, NULL, NULL, NULL));
  33147. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  33148. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  33149. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  33150. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  33151. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, &emptyBN, NULL));
  33152. ExpectNull(BN_mod_inverse(r, &emptyBN, &emptyBN, NULL));
  33153. ExpectNull(BN_mod_inverse(&emptyBN, a, &emptyBN, NULL));
  33154. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, m, NULL));
  33155. ExpectNull(BN_mod_inverse(&emptyBN, a, m, NULL));
  33156. ExpectNull(BN_mod_inverse(r, &emptyBN, m, NULL));
  33157. ExpectNull(BN_mod_inverse(r, a, &emptyBN, NULL));
  33158. /* END Invalid parameters. */
  33159. val1 = 9;
  33160. val2 = 13;
  33161. val3 = 5;
  33162. ExpectIntEQ(BN_set_word(a, val1), 1);
  33163. ExpectIntEQ(BN_set_word(b, val2), 1);
  33164. ExpectIntEQ(BN_set_word(m, val3), 1);
  33165. ExpectIntEQ(BN_mod_add(r, a, b, m, NULL), 1);
  33166. ExpectIntEQ(BN_is_word(r, (val1 + val2) % val3), 1);
  33167. ExpectIntEQ(BN_mod_mul(r, a, b, m, NULL), 1);
  33168. ExpectIntEQ(BN_is_word(r, (val1 * val2) % val3), 1);
  33169. ExpectIntEQ(BN_set_word(a, 2), 1);
  33170. ExpectIntEQ(BN_set_word(b, 3), 1);
  33171. ExpectIntEQ(BN_set_word(m, 5), 1);
  33172. /* (2 ^ 3) % 5 = 8 % 5 = 3 */
  33173. ExpectIntEQ(BN_mod_exp(r, a, b, m, NULL), 1);
  33174. ExpectIntEQ(BN_is_word(r, 3), 1);
  33175. /* (2 * 3) % 5 = 6 % 5 = 1 => inv = 3 */
  33176. ExpectNotNull(BN_mod_inverse(r, a, m, NULL));
  33177. ExpectIntEQ(BN_is_word(r, 3), 1);
  33178. ExpectNotNull(t = BN_mod_inverse(NULL, a, m, NULL));
  33179. ExpectIntEQ(BN_is_word(t, 3), 1);
  33180. BN_free(t);
  33181. /* No inverse case. No inverse when a divides b. */
  33182. ExpectIntEQ(BN_set_word(a, 3), 1);
  33183. ExpectIntEQ(BN_set_word(m, 9), 1);
  33184. ExpectNull(BN_mod_inverse(r, a, m, NULL));
  33185. BN_free(r);
  33186. BN_free(m);
  33187. BN_free(b);
  33188. BN_free(a);
  33189. #endif
  33190. return EXPECT_RESULT();
  33191. }
  33192. static int test_wolfSSL_BN_math_other(void)
  33193. {
  33194. EXPECT_DECLS;
  33195. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33196. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33197. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  33198. BIGNUM* a = NULL;
  33199. BIGNUM* b = NULL;
  33200. BIGNUM* r = NULL;
  33201. BIGNUM emptyBN;
  33202. /* Setup */
  33203. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33204. ExpectNotNull(a = BN_new());
  33205. ExpectNotNull(b = BN_new());
  33206. ExpectNotNull(r = BN_new());
  33207. /* Invalid parameters. */
  33208. ExpectIntEQ(BN_gcd(NULL, NULL, NULL, NULL), 0);
  33209. ExpectIntEQ(BN_gcd(r, NULL, NULL, NULL), 0);
  33210. ExpectIntEQ(BN_gcd(NULL, a, NULL, NULL), 0);
  33211. ExpectIntEQ(BN_gcd(NULL, NULL, b, NULL), 0);
  33212. ExpectIntEQ(BN_gcd(NULL, a, b, NULL), 0);
  33213. ExpectIntEQ(BN_gcd(r, NULL, b, NULL), 0);
  33214. ExpectIntEQ(BN_gcd(r, a, NULL, NULL), 0);
  33215. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33216. ExpectIntEQ(BN_gcd(r, &emptyBN, &emptyBN, NULL), 0);
  33217. ExpectIntEQ(BN_gcd(&emptyBN, a, &emptyBN, NULL), 0);
  33218. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, b, NULL), 0);
  33219. ExpectIntEQ(BN_gcd(&emptyBN, a, b, NULL), 0);
  33220. ExpectIntEQ(BN_gcd(r, &emptyBN, b, NULL), 0);
  33221. ExpectIntEQ(BN_gcd(r, a, &emptyBN, NULL), 0);
  33222. /* END Invalid parameters. */
  33223. /* No common factors between 2 and 3. */
  33224. ExpectIntEQ(BN_set_word(a, 2), 1);
  33225. ExpectIntEQ(BN_set_word(b, 3), 1);
  33226. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  33227. ExpectIntEQ(BN_is_word(r, 1), 1);
  33228. /* 3 is largest value that divides both 6 and 9. */
  33229. ExpectIntEQ(BN_set_word(a, 6), 1);
  33230. ExpectIntEQ(BN_set_word(b, 9), 1);
  33231. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  33232. ExpectIntEQ(BN_is_word(r, 3), 1);
  33233. /* GCD of 0 and 0 is undefined. */
  33234. ExpectIntEQ(BN_set_word(a, 0), 1);
  33235. ExpectIntEQ(BN_set_word(b, 0), 1);
  33236. ExpectIntEQ(BN_gcd(r, a, b, NULL), 0);
  33237. /* Teardown */
  33238. BN_free(r);
  33239. BN_free(b);
  33240. BN_free(a);
  33241. #endif
  33242. #endif
  33243. return EXPECT_RESULT();
  33244. }
  33245. static int test_wolfSSL_BN_rand(void)
  33246. {
  33247. EXPECT_DECLS;
  33248. #if defined(OPENSSL_EXTRA) && !defined(OPENSSL_EXTRA_NO_BN)
  33249. BIGNUM* bn = NULL;
  33250. BIGNUM* range = NULL;
  33251. BIGNUM emptyBN;
  33252. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33253. ExpectNotNull(bn = BN_new());
  33254. ExpectNotNull(range = BN_new());
  33255. /* Invalid parameters. */
  33256. ExpectIntEQ(BN_rand(NULL, -1, 0, 0), 0);
  33257. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  33258. ExpectIntEQ(BN_rand(NULL, 1, 0, 0), 0);
  33259. ExpectIntEQ(BN_rand(&emptyBN, -1, 0, 0), 0);
  33260. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  33261. ExpectIntEQ(BN_rand(&emptyBN, 1, 0, 0), 0);
  33262. ExpectIntEQ(BN_pseudo_rand(NULL, -1, 0, 0), 0);
  33263. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  33264. ExpectIntEQ(BN_pseudo_rand(NULL, 1, 0, 0), 0);
  33265. ExpectIntEQ(BN_pseudo_rand(&emptyBN, -1, 0, 0), 0);
  33266. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  33267. ExpectIntEQ(BN_pseudo_rand(&emptyBN, 1, 0, 0), 0);
  33268. ExpectIntEQ(BN_rand_range(NULL, NULL), 0);
  33269. ExpectIntEQ(BN_rand_range(bn, NULL), 0);
  33270. ExpectIntEQ(BN_rand_range(NULL, range), 0);
  33271. ExpectIntEQ(BN_rand_range(&emptyBN, &emptyBN), 0);
  33272. ExpectIntEQ(BN_rand_range(bn, &emptyBN), 0);
  33273. ExpectIntEQ(BN_rand_range(&emptyBN, range), 0);
  33274. /* 0 bit random value must be 0 and so cannot set bit in any position. */
  33275. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  33276. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33277. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  33278. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33279. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  33280. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33281. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  33282. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33283. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  33284. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33285. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  33286. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33287. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  33288. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33289. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  33290. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33291. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  33292. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33293. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  33294. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33295. /* 1 bit random value must have no more than one top bit set. */
  33296. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  33297. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33298. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  33299. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33300. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  33301. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33302. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  33303. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33304. /* END Invalid parameters. */
  33305. /* 0 bit random: 0. */
  33306. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  33307. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33308. ExpectIntEQ(BN_is_zero(bn), 1);
  33309. ExpectIntEQ(BN_set_word(bn, 2), 1); /* Make sure not zero. */
  33310. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  33311. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33312. ExpectIntEQ(BN_is_zero(bn), 1);
  33313. /* 1 bit random: 0 or 1. */
  33314. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  33315. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33316. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  33317. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  33318. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33319. ExpectIntEQ(BN_get_word(bn), 1);
  33320. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  33321. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  33322. ExpectIntEQ(BN_get_word(bn), 1);
  33323. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  33324. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33325. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  33326. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  33327. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33328. ExpectIntEQ(BN_get_word(bn), 1);
  33329. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  33330. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  33331. ExpectIntEQ(BN_get_word(bn), 1);
  33332. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  33333. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33334. ExpectIntEQ(BN_num_bits(bn), 8);
  33335. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  33336. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  33337. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33338. ExpectIntEQ(BN_num_bits(bn), 8);
  33339. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  33340. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  33341. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33342. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  33343. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  33344. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  33345. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33346. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  33347. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  33348. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  33349. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  33350. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  33351. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  33352. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  33353. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  33354. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  33355. * requested number of bits up to the nearest multiple of 8. E.g. in this
  33356. * case, requesting a 13-bit random number would actually return a 16-bit
  33357. * random number. */
  33358. ExpectIntEQ(BN_rand(bn, 13, WOLFSSL_BN_RAND_TOP_ONE,
  33359. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33360. ExpectIntEQ(BN_num_bits(bn), 13);
  33361. ExpectIntEQ(BN_rand(range, 64, WOLFSSL_BN_RAND_TOP_ONE,
  33362. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33363. ExpectIntEQ(BN_rand_range(bn, range), 1);
  33364. ExpectIntEQ(BN_set_word(range, 0), 1);
  33365. ExpectIntEQ(BN_rand_range(bn, range), 1);
  33366. ExpectIntEQ(BN_set_word(range, 1), 1);
  33367. ExpectIntEQ(BN_rand_range(bn, range), 1);
  33368. BN_free(bn);
  33369. BN_free(range);
  33370. #endif
  33371. return EXPECT_RESULT();
  33372. }
  33373. static int test_wolfSSL_BN_prime(void)
  33374. {
  33375. EXPECT_DECLS;
  33376. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33377. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33378. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  33379. BIGNUM* a = NULL;
  33380. BIGNUM* add = NULL;
  33381. BIGNUM* rem = NULL;
  33382. BIGNUM emptyBN;
  33383. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33384. ExpectNotNull(a = BN_new());
  33385. ExpectNotNull(add = BN_new());
  33386. ExpectNotNull(rem = BN_new());
  33387. /* Invalid parameters. */
  33388. /* BN_generate_prime_ex()
  33389. * prime - must have valid BIGNUM
  33390. * bits - Greater then 0
  33391. * safe - not supported, must be 0
  33392. * add - not supported, must be NULL
  33393. * rem - not supported, must be NULL
  33394. * cb - anything
  33395. */
  33396. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, rem, NULL), 0);
  33397. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, rem, NULL), 0);
  33398. ExpectIntEQ(BN_generate_prime_ex(a, -1, 1, add, rem, NULL), 0);
  33399. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 1, add, rem, NULL), 0);
  33400. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 1, add, rem, NULL), 0);
  33401. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 0, add, rem, NULL), 0);
  33402. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 0, add, rem, NULL), 0);
  33403. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, NULL, rem, NULL), 0);
  33404. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, NULL, rem, NULL), 0);
  33405. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, NULL, NULL), 0);
  33406. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, NULL, NULL), 0);
  33407. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 0, NULL, NULL, NULL), 0);
  33408. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 0, NULL, NULL, NULL), 0);
  33409. ExpectIntEQ(BN_generate_prime_ex(a, -1, 0, NULL, NULL, NULL), 0);
  33410. ExpectIntEQ(BN_generate_prime_ex(a, 0, 0, NULL, NULL, NULL), 0);
  33411. ExpectIntEQ(BN_generate_prime_ex(a, 2, 1, NULL, NULL, NULL), 0);
  33412. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, add, NULL, NULL), 0);
  33413. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, NULL, rem, NULL), 0);
  33414. ExpectIntEQ(BN_is_prime_ex(NULL, -1, NULL, NULL), -1);
  33415. ExpectIntEQ(BN_is_prime_ex(&emptyBN, -1, NULL, NULL), -1);
  33416. ExpectIntEQ(BN_is_prime_ex(a, -1, NULL, NULL), -1);
  33417. ExpectIntEQ(BN_is_prime_ex(a, 2048, NULL, NULL), -1);
  33418. ExpectIntEQ(BN_is_prime_ex(NULL, 1, NULL, NULL), -1);
  33419. ExpectIntEQ(BN_is_prime_ex(&emptyBN, 1, NULL, NULL), -1);
  33420. /* END Invalid parameters. */
  33421. ExpectIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL), 1);
  33422. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 1);
  33423. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  33424. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 0);
  33425. BN_free(rem);
  33426. BN_free(add);
  33427. BN_free(a);
  33428. #endif
  33429. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  33430. return EXPECT_RESULT();
  33431. }
  33432. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33433. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33434. #define TEST_ARG 0x1234
  33435. static void msg_cb(int write_p, int version, int content_type,
  33436. const void *buf, size_t len, SSL *ssl, void *arg)
  33437. {
  33438. (void)write_p;
  33439. (void)version;
  33440. (void)content_type;
  33441. (void)buf;
  33442. (void)len;
  33443. (void)ssl;
  33444. AssertTrue(arg == (void*)TEST_ARG);
  33445. }
  33446. #endif
  33447. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  33448. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  33449. #if defined(SESSION_CERTS)
  33450. #include "wolfssl/internal.h"
  33451. #endif
  33452. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  33453. {
  33454. EXPECT_DECLS;
  33455. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  33456. STACK_OF(X509)* sk = NULL;
  33457. X509* x509 = NULL;
  33458. int i, num;
  33459. BIO* bio = NULL;
  33460. #endif
  33461. ExpectNotNull(ctx);
  33462. ExpectNotNull(ssl);
  33463. fprintf(stderr, "\n===== msgcb called ====\n");
  33464. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  33465. ExpectTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  33466. ExpectIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  33467. ExpectNotNull(SSL_get0_verified_chain(ssl));
  33468. #endif
  33469. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  33470. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  33471. ExpectNotNull(sk = SSL_get_peer_cert_chain(ssl));
  33472. if (sk == NULL) {
  33473. BIO_free(bio);
  33474. return TEST_FAIL;
  33475. }
  33476. num = sk_X509_num(sk);
  33477. ExpectTrue(num > 0);
  33478. for (i = 0; i < num; i++) {
  33479. ExpectNotNull(x509 = sk_X509_value(sk,i));
  33480. if (x509 == NULL)
  33481. break;
  33482. fprintf(stderr, "Certificate at index [%d] = :\n",i);
  33483. X509_print(bio,x509);
  33484. fprintf(stderr, "\n\n");
  33485. }
  33486. BIO_free(bio);
  33487. #endif
  33488. return EXPECT_RESULT();
  33489. }
  33490. #endif
  33491. static int test_wolfSSL_msgCb(void)
  33492. {
  33493. EXPECT_DECLS;
  33494. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  33495. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  33496. test_ssl_cbf client_cb;
  33497. test_ssl_cbf server_cb;
  33498. XMEMSET(&client_cb, 0, sizeof(client_cb));
  33499. XMEMSET(&server_cb, 0, sizeof(server_cb));
  33500. #ifndef WOLFSSL_NO_TLS12
  33501. client_cb.method = wolfTLSv1_2_client_method;
  33502. server_cb.method = wolfTLSv1_2_server_method;
  33503. #else
  33504. client_cb.method = wolfTLSv1_3_client_method;
  33505. server_cb.method = wolfTLSv1_3_server_method;
  33506. #endif
  33507. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  33508. &server_cb, msgCb), TEST_SUCCESS);
  33509. #endif
  33510. return EXPECT_RESULT();
  33511. }
  33512. static int test_wolfSSL_either_side(void)
  33513. {
  33514. EXPECT_DECLS;
  33515. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  33516. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  33517. test_ssl_cbf client_cb;
  33518. test_ssl_cbf server_cb;
  33519. XMEMSET(&client_cb, 0, sizeof(client_cb));
  33520. XMEMSET(&server_cb, 0, sizeof(server_cb));
  33521. /* Use different CTX for client and server */
  33522. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  33523. ExpectNotNull(client_cb.ctx);
  33524. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  33525. ExpectNotNull(server_cb.ctx);
  33526. /* we are responsible for free'ing WOLFSSL_CTX */
  33527. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  33528. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  33529. &server_cb, NULL), TEST_SUCCESS);
  33530. wolfSSL_CTX_free(client_cb.ctx);
  33531. wolfSSL_CTX_free(server_cb.ctx);
  33532. #endif
  33533. return EXPECT_RESULT();
  33534. }
  33535. static int test_wolfSSL_DTLS_either_side(void)
  33536. {
  33537. EXPECT_DECLS;
  33538. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  33539. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  33540. test_ssl_cbf client_cb;
  33541. test_ssl_cbf server_cb;
  33542. XMEMSET(&client_cb, 0, sizeof(client_cb));
  33543. XMEMSET(&server_cb, 0, sizeof(server_cb));
  33544. /* Use different CTX for client and server */
  33545. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  33546. ExpectNotNull(client_cb.ctx);
  33547. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  33548. ExpectNotNull(server_cb.ctx);
  33549. /* we are responsible for free'ing WOLFSSL_CTX */
  33550. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  33551. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  33552. &server_cb, NULL), TEST_SUCCESS);
  33553. wolfSSL_CTX_free(client_cb.ctx);
  33554. wolfSSL_CTX_free(server_cb.ctx);
  33555. #endif
  33556. return EXPECT_RESULT();
  33557. }
  33558. static int test_generate_cookie(void)
  33559. {
  33560. EXPECT_DECLS;
  33561. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  33562. SSL_CTX* ctx = NULL;
  33563. SSL* ssl = NULL;
  33564. byte buf[FOURK_BUF] = {0};
  33565. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  33566. ExpectNotNull(ssl = SSL_new(ctx));
  33567. /* Test unconnected */
  33568. ExpectIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  33569. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  33570. wolfSSL_SetCookieCtx(ssl, ctx);
  33571. ExpectNotNull(wolfSSL_GetCookieCtx(ssl));
  33572. ExpectNull(wolfSSL_GetCookieCtx(NULL));
  33573. SSL_free(ssl);
  33574. SSL_CTX_free(ctx);
  33575. #endif
  33576. return EXPECT_RESULT();
  33577. }
  33578. static int test_wolfSSL_set_options(void)
  33579. {
  33580. EXPECT_DECLS;
  33581. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33582. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33583. WOLFSSL* ssl = NULL;
  33584. WOLFSSL_CTX* ctx = NULL;
  33585. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  33586. char appData[] = "extra msg";
  33587. #endif
  33588. #ifdef OPENSSL_EXTRA
  33589. unsigned char protos[] = {
  33590. 7, 't', 'l', 's', '/', '1', '.', '2',
  33591. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  33592. };
  33593. unsigned int len = sizeof(protos);
  33594. void *arg = (void *)TEST_ARG;
  33595. #endif
  33596. #ifndef NO_WOLFSSL_SERVER
  33597. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  33598. #else
  33599. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33600. #endif
  33601. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  33602. WOLFSSL_FILETYPE_PEM));
  33603. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  33604. WOLFSSL_FILETYPE_PEM));
  33605. ExpectTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  33606. == WOLFSSL_OP_NO_TLSv1);
  33607. ExpectTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  33608. ExpectIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  33609. WOLFSSL_OP_NO_SSLv2)), 0);
  33610. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  33611. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  33612. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  33613. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  33614. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  33615. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  33616. ExpectFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  33617. WOLFSSL_OP_NO_COMPRESSION));
  33618. wolfSSL_CTX_free(ctx);
  33619. ctx = NULL;
  33620. #ifndef NO_WOLFSSL_SERVER
  33621. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  33622. #else
  33623. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33624. #endif
  33625. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  33626. WOLFSSL_FILETYPE_PEM));
  33627. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  33628. WOLFSSL_FILETYPE_PEM));
  33629. #ifdef OPENSSL_EXTRA
  33630. ExpectTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  33631. #endif
  33632. ExpectNotNull(ssl = wolfSSL_new(ctx));
  33633. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  33634. #ifdef HAVE_EX_DATA
  33635. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  33636. ExpectNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  33637. if (ssl != NULL) {
  33638. ExpectIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  33639. appData, sizeof(appData)), 0);
  33640. }
  33641. #else
  33642. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  33643. ExpectNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  33644. #endif
  33645. #endif
  33646. ExpectTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  33647. WOLFSSL_OP_NO_TLSv1);
  33648. ExpectTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  33649. ExpectIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  33650. WOLFSSL_OP_NO_SSLv2)), 0);
  33651. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  33652. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  33653. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  33654. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  33655. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  33656. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  33657. #ifdef OPENSSL_EXTRA
  33658. ExpectFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  33659. WOLFSSL_OP_NO_COMPRESSION));
  33660. #endif
  33661. #ifdef OPENSSL_EXTRA
  33662. ExpectTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  33663. wolfSSL_set_msg_callback_arg(ssl, arg);
  33664. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  33665. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  33666. #else
  33667. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  33668. #endif
  33669. #endif
  33670. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  33671. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  33672. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  33673. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  33674. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  33675. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  33676. #else
  33677. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  33678. #endif
  33679. #endif /* HAVE_ALPN && !NO_BIO */
  33680. #endif
  33681. wolfSSL_free(ssl);
  33682. wolfSSL_CTX_free(ctx);
  33683. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33684. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33685. return EXPECT_RESULT();
  33686. }
  33687. static int test_wolfSSL_sk_SSL_CIPHER(void)
  33688. {
  33689. EXPECT_DECLS;
  33690. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  33691. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33692. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33693. SSL* ssl = NULL;
  33694. SSL_CTX* ctx = NULL;
  33695. STACK_OF(SSL_CIPHER) *sk = NULL;
  33696. STACK_OF(SSL_CIPHER) *dupSk = NULL;
  33697. #ifndef NO_WOLFSSL_SERVER
  33698. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33699. #else
  33700. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33701. #endif
  33702. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  33703. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33704. ExpectNotNull(ssl = SSL_new(ctx));
  33705. ExpectNotNull(sk = SSL_get_ciphers(ssl));
  33706. ExpectNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  33707. ExpectIntGT(sk_SSL_CIPHER_num(sk), 0);
  33708. ExpectIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  33709. /* error case because connection has not been established yet */
  33710. ExpectIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  33711. sk_SSL_CIPHER_free(dupSk);
  33712. /* sk is pointer to internal struct that should be free'd in SSL_free */
  33713. SSL_free(ssl);
  33714. SSL_CTX_free(ctx);
  33715. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33716. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33717. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33718. return EXPECT_RESULT();
  33719. }
  33720. static int test_wolfSSL_set1_curves_list(void)
  33721. {
  33722. EXPECT_DECLS;
  33723. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  33724. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33725. SSL* ssl = NULL;
  33726. SSL_CTX* ctx = NULL;
  33727. #ifndef NO_WOLFSSL_SERVER
  33728. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33729. #else
  33730. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33731. #endif
  33732. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  33733. SSL_FILETYPE_PEM));
  33734. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  33735. ExpectNotNull(ssl = SSL_new(ctx));
  33736. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  33737. #ifdef HAVE_ECC
  33738. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  33739. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  33740. #endif
  33741. #ifdef HAVE_CURVE25519
  33742. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  33743. #else
  33744. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  33745. #endif
  33746. #ifdef HAVE_CURVE448
  33747. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  33748. #else
  33749. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  33750. #endif
  33751. ExpectIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  33752. #ifdef HAVE_ECC
  33753. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  33754. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  33755. #endif
  33756. #ifdef HAVE_CURVE25519
  33757. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  33758. #else
  33759. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  33760. #endif
  33761. #ifdef HAVE_CURVE448
  33762. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  33763. #else
  33764. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  33765. #endif
  33766. SSL_free(ssl);
  33767. SSL_CTX_free(ctx);
  33768. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33769. #endif
  33770. return EXPECT_RESULT();
  33771. }
  33772. static int test_wolfSSL_set1_sigalgs_list(void)
  33773. {
  33774. EXPECT_DECLS;
  33775. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  33776. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33777. SSL* ssl = NULL;
  33778. SSL_CTX* ctx = NULL;
  33779. #ifndef NO_WOLFSSL_SERVER
  33780. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33781. #else
  33782. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33783. #endif
  33784. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33785. SSL_FILETYPE_PEM));
  33786. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33787. ExpectNotNull(ssl = SSL_new(ctx));
  33788. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  33789. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  33790. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  33791. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  33792. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  33793. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  33794. #ifndef NO_RSA
  33795. #ifndef NO_SHA256
  33796. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  33797. WOLFSSL_FAILURE);
  33798. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  33799. WOLFSSL_FAILURE);
  33800. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  33801. WOLFSSL_SUCCESS);
  33802. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  33803. WOLFSSL_SUCCESS);
  33804. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  33805. WOLFSSL_FAILURE);
  33806. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  33807. WOLFSSL_FAILURE);
  33808. #ifdef WC_RSA_PSS
  33809. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  33810. WOLFSSL_SUCCESS);
  33811. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  33812. WOLFSSL_SUCCESS);
  33813. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  33814. WOLFSSL_SUCCESS);
  33815. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  33816. WOLFSSL_SUCCESS);
  33817. #endif
  33818. #ifdef WOLFSSL_SHA512
  33819. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  33820. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  33821. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  33822. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  33823. #elif defined(WOLFSSL_SHA384)
  33824. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  33825. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  33826. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  33827. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  33828. #endif
  33829. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  33830. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  33831. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  33832. WOLFSSL_FAILURE);
  33833. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  33834. WOLFSSL_FAILURE);
  33835. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  33836. WOLFSSL_FAILURE);
  33837. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  33838. WOLFSSL_FAILURE);
  33839. #endif
  33840. #endif
  33841. #ifdef HAVE_ECC
  33842. #ifndef NO_SHA256
  33843. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  33844. WOLFSSL_SUCCESS);
  33845. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"),
  33846. WOLFSSL_SUCCESS);
  33847. #ifdef WOLFSSL_SHA512
  33848. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  33849. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  33850. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  33851. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  33852. #elif defined(WOLFSSL_SHA384)
  33853. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  33854. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  33855. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  33856. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  33857. #endif
  33858. #endif
  33859. #endif
  33860. #ifdef HAVE_ED25519
  33861. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  33862. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  33863. #endif
  33864. #ifdef HAVE_ED448
  33865. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  33866. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  33867. #endif
  33868. #ifndef NO_DSA
  33869. #ifndef NO_SHA256
  33870. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  33871. WOLFSSL_SUCCESS);
  33872. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  33873. WOLFSSL_SUCCESS);
  33874. #endif
  33875. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  33876. defined(WOLFSSL_ALLOW_TLS_SHA1))
  33877. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  33878. WOLFSSL_SUCCESS);
  33879. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  33880. WOLFSSL_SUCCESS);
  33881. #endif
  33882. #endif
  33883. SSL_free(ssl);
  33884. SSL_CTX_free(ctx);
  33885. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33886. #endif
  33887. return EXPECT_RESULT();
  33888. }
  33889. /* Testing wolfSSL_set_tlsext_status_type function.
  33890. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  33891. */
  33892. static int test_wolfSSL_set_tlsext_status_type(void)
  33893. {
  33894. EXPECT_DECLS;
  33895. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  33896. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  33897. SSL* ssl = NULL;
  33898. SSL_CTX* ctx = NULL;
  33899. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33900. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33901. SSL_FILETYPE_PEM));
  33902. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33903. ExpectNotNull(ssl = SSL_new(ctx));
  33904. ExpectIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  33905. SSL_SUCCESS);
  33906. ExpectIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  33907. SSL_free(ssl);
  33908. SSL_CTX_free(ctx);
  33909. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  33910. return EXPECT_RESULT();
  33911. }
  33912. #ifndef NO_BIO
  33913. static int test_wolfSSL_PEM_read_bio(void)
  33914. {
  33915. EXPECT_DECLS;
  33916. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33917. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33918. byte buff[6000];
  33919. XFILE f = XBADFILE;
  33920. int bytes;
  33921. X509* x509 = NULL;
  33922. BIO* bio = NULL;
  33923. BUF_MEM* buf = NULL;
  33924. ExpectTrue((f = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  33925. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  33926. if (f != XBADFILE)
  33927. XFCLOSE(f);
  33928. ExpectNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  33929. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  33930. ExpectIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  33931. ExpectNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  33932. ExpectIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  33933. /* BIO should return the set EOF value */
  33934. ExpectIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  33935. ExpectIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  33936. ExpectIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  33937. ExpectIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  33938. BIO_free(bio);
  33939. BUF_MEM_free(buf);
  33940. X509_free(x509);
  33941. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  33942. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33943. return EXPECT_RESULT();
  33944. }
  33945. #if defined(OPENSSL_EXTRA)
  33946. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  33947. long argl, long ret)
  33948. {
  33949. (void)bio;
  33950. (void)cmd;
  33951. (void)argp;
  33952. (void)argi;
  33953. (void)argl;
  33954. return ret;
  33955. }
  33956. #endif
  33957. static int test_wolfSSL_BIO(void)
  33958. {
  33959. EXPECT_DECLS;
  33960. #if defined(OPENSSL_EXTRA)
  33961. const unsigned char* p = NULL;
  33962. byte buff[20];
  33963. BIO* bio1 = NULL;
  33964. BIO* bio2 = NULL;
  33965. BIO* bio3 = NULL;
  33966. char* bufPt = NULL;
  33967. int i;
  33968. for (i = 0; i < 20; i++) {
  33969. buff[i] = i;
  33970. }
  33971. /* test BIO_free with NULL */
  33972. ExpectIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  33973. /* Creating and testing type BIO_s_bio */
  33974. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  33975. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  33976. ExpectNotNull(bio3 = BIO_new(BIO_s_bio()));
  33977. /* read/write before set up */
  33978. ExpectIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  33979. ExpectIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  33980. ExpectIntEQ(BIO_set_nbio(bio1, 1), 1);
  33981. ExpectIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  33982. ExpectIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  33983. ExpectIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  33984. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  33985. ExpectNotNull(XMEMCPY(bufPt, buff, 10));
  33986. ExpectIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  33987. /* write buffer full */
  33988. ExpectIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  33989. ExpectIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  33990. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 0);
  33991. /* write the other direction with pair */
  33992. ExpectIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  33993. ExpectNotNull(XMEMCPY(bufPt, buff, 8));
  33994. ExpectIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  33995. /* try read */
  33996. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 8);
  33997. ExpectIntEQ((int)BIO_ctrl_pending(bio2), 20);
  33998. /* try read using ctrl function */
  33999. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  34000. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  34001. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  34002. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  34003. ExpectIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  34004. for (i = 0; i < 20; i++) {
  34005. ExpectIntEQ((int)bufPt[i], i);
  34006. }
  34007. ExpectIntEQ(BIO_nread(bio2, &bufPt, 1), WOLFSSL_BIO_ERROR);
  34008. ExpectIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  34009. for (i = 0; i < 8; i++) {
  34010. ExpectIntEQ((int)bufPt[i], i);
  34011. }
  34012. ExpectIntEQ(BIO_nread(bio1, &bufPt, 1), WOLFSSL_BIO_ERROR);
  34013. ExpectIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  34014. /* new pair */
  34015. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  34016. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  34017. bio2 = NULL;
  34018. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  34019. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  34020. ExpectIntEQ(BIO_nread(bio3, &bufPt, 10), WOLFSSL_BIO_ERROR);
  34021. /* test wrap around... */
  34022. ExpectIntEQ(BIO_reset(bio1), 0);
  34023. ExpectIntEQ(BIO_reset(bio3), 0);
  34024. /* fill write buffer, read only small amount then write again */
  34025. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  34026. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  34027. ExpectIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  34028. for (i = 0; i < 4; i++) {
  34029. ExpectIntEQ(bufPt[i], i);
  34030. }
  34031. /* try writing over read index */
  34032. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  34033. ExpectNotNull(XMEMSET(bufPt, 0, 4));
  34034. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 20);
  34035. /* read and write 0 bytes */
  34036. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  34037. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  34038. /* should read only to end of write buffer then need to read again */
  34039. ExpectIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  34040. for (i = 0; i < 16; i++) {
  34041. ExpectIntEQ(bufPt[i], buff[4 + i]);
  34042. }
  34043. ExpectIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  34044. ExpectIntEQ(BIO_nread0(bio3, &bufPt), 4);
  34045. for (i = 0; i < 4; i++) {
  34046. ExpectIntEQ(bufPt[i], 0);
  34047. }
  34048. /* read index should not have advanced with nread0 */
  34049. ExpectIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  34050. for (i = 0; i < 4; i++) {
  34051. ExpectIntEQ(bufPt[i], 0);
  34052. }
  34053. /* write and fill up buffer checking reset of index state */
  34054. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  34055. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  34056. /* test reset on data in bio1 write buffer */
  34057. ExpectIntEQ(BIO_reset(bio1), 0);
  34058. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  34059. ExpectIntEQ(BIO_nread(bio3, &bufPt, 3), WOLFSSL_BIO_ERROR);
  34060. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  34061. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  34062. ExpectNotNull(p);
  34063. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  34064. ExpectIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  34065. for (i = 0; i < 6; i++) {
  34066. ExpectIntEQ(bufPt[i], i);
  34067. }
  34068. /* test case of writing twice with offset read index */
  34069. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  34070. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  34071. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  34072. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  34073. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  34074. ExpectIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  34075. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  34076. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  34077. BIO_free(bio1);
  34078. bio1 = NULL;
  34079. BIO_free(bio3);
  34080. bio3 = NULL;
  34081. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  34082. {
  34083. BIO* bioA = NULL;
  34084. BIO* bioB = NULL;
  34085. ExpectIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  34086. ExpectIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  34087. BIO_free(bioA);
  34088. bioA = NULL;
  34089. BIO_free(bioB);
  34090. bioB = NULL;
  34091. }
  34092. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  34093. /* BIOs with file pointers */
  34094. #if !defined(NO_FILESYSTEM)
  34095. {
  34096. XFILE f1 = XBADFILE;
  34097. XFILE f2 = XBADFILE;
  34098. BIO* f_bio1 = NULL;
  34099. BIO* f_bio2 = NULL;
  34100. unsigned char cert[300];
  34101. char testFile[] = "tests/bio_write_test.txt";
  34102. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  34103. ExpectNotNull(f_bio1 = BIO_new(BIO_s_file()));
  34104. ExpectNotNull(f_bio2 = BIO_new(BIO_s_file()));
  34105. /* Failure due to wrong BIO type */
  34106. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  34107. ExpectIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  34108. ExpectTrue((f1 = XFOPEN(svrCertFile, "rwb")) != XBADFILE);
  34109. ExpectIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  34110. ExpectIntEQ(BIO_write_filename(f_bio2, testFile),
  34111. WOLFSSL_SUCCESS);
  34112. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  34113. ExpectIntEQ(BIO_tell(f_bio1),sizeof(cert));
  34114. ExpectIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  34115. ExpectIntEQ(BIO_tell(f_bio2),sizeof(msg));
  34116. ExpectIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  34117. ExpectIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  34118. ExpectIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  34119. ExpectIntEQ(BIO_reset(f_bio2), 0);
  34120. ExpectIntEQ(BIO_tell(NULL),-1);
  34121. ExpectIntEQ(BIO_tell(f_bio2),0);
  34122. ExpectIntEQ(BIO_seek(f_bio2, 4), 0);
  34123. ExpectIntEQ(BIO_tell(f_bio2),4);
  34124. BIO_free(f_bio1);
  34125. f_bio1 = NULL;
  34126. BIO_free(f_bio2);
  34127. f_bio2 = NULL;
  34128. ExpectNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  34129. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  34130. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  34131. BIO_free(f_bio1);
  34132. f_bio1 = NULL;
  34133. }
  34134. #endif /* !defined(NO_FILESYSTEM) */
  34135. /* BIO info callback */
  34136. {
  34137. const char* testArg = "test";
  34138. BIO* cb_bio = NULL;
  34139. ExpectNotNull(cb_bio = BIO_new(BIO_s_mem()));
  34140. BIO_set_callback(cb_bio, bioCallback);
  34141. ExpectNotNull(BIO_get_callback(cb_bio));
  34142. BIO_set_callback(cb_bio, NULL);
  34143. ExpectNull(BIO_get_callback(cb_bio));
  34144. BIO_set_callback_arg(cb_bio, (char*)testArg);
  34145. ExpectStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  34146. ExpectNull(BIO_get_callback_arg(NULL));
  34147. BIO_free(cb_bio);
  34148. cb_bio = NULL;
  34149. }
  34150. /* BIO_vfree */
  34151. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  34152. BIO_vfree(NULL);
  34153. BIO_vfree(bio1);
  34154. #endif
  34155. return EXPECT_RESULT();
  34156. }
  34157. #endif /* !NO_BIO */
  34158. static int test_wolfSSL_a2i_IPADDRESS(void)
  34159. {
  34160. EXPECT_DECLS;
  34161. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  34162. const unsigned char* data = NULL;
  34163. int dataSz = 0;
  34164. ASN1_OCTET_STRING *st = NULL;
  34165. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  34166. const unsigned char ipv6_exp[] = {
  34167. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  34168. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  34169. };
  34170. const unsigned char ipv6_home[] = {
  34171. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  34172. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  34173. };
  34174. ExpectNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  34175. ExpectNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  34176. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  34177. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP4_ADDR_LEN);
  34178. ExpectIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  34179. ASN1_STRING_free(st);
  34180. st = NULL;
  34181. ExpectNotNull(st = a2i_IPADDRESS("::1"));
  34182. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  34183. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  34184. ExpectIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  34185. ASN1_STRING_free(st);
  34186. st = NULL;
  34187. ExpectNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  34188. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  34189. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  34190. ExpectIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  34191. ASN1_STRING_free(st);
  34192. #endif
  34193. return EXPECT_RESULT();
  34194. }
  34195. static int test_wolfSSL_X509_cmp_time(void)
  34196. {
  34197. EXPECT_DECLS;
  34198. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  34199. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  34200. WOLFSSL_ASN1_TIME asn_time;
  34201. time_t t;
  34202. ExpectIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  34203. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  34204. ExpectIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  34205. ExpectIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  34206. ExpectIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  34207. #endif
  34208. return EXPECT_RESULT();
  34209. }
  34210. static int test_wolfSSL_X509_time_adj(void)
  34211. {
  34212. EXPECT_DECLS;
  34213. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  34214. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  34215. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  34216. !defined(NO_ASN_TIME)
  34217. X509* x509 = NULL;
  34218. time_t t;
  34219. time_t not_before;
  34220. time_t not_after;
  34221. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  34222. client_cert_der_2048, sizeof_client_cert_der_2048,
  34223. WOLFSSL_FILETYPE_ASN1));
  34224. t = 0;
  34225. not_before = wc_Time(0);
  34226. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  34227. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  34228. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  34229. /* Check X509_gmtime_adj, too. */
  34230. ExpectNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  34231. X509_free(x509);
  34232. #endif
  34233. return EXPECT_RESULT();
  34234. }
  34235. static int test_wolfSSL_X509(void)
  34236. {
  34237. EXPECT_DECLS;
  34238. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  34239. !defined(NO_RSA)
  34240. X509* x509 = NULL;
  34241. #ifndef NO_BIO
  34242. BIO* bio = NULL;
  34243. X509_STORE_CTX* ctx = NULL;
  34244. X509_STORE* store = NULL;
  34245. #endif
  34246. char der[] = "certs/ca-cert.der";
  34247. XFILE fp = XBADFILE;
  34248. ExpectNotNull(x509 = X509_new());
  34249. X509_free(x509);
  34250. x509 = NULL;
  34251. #ifndef NO_BIO
  34252. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  34253. SSL_FILETYPE_PEM));
  34254. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  34255. #ifdef WOLFSSL_CERT_GEN
  34256. ExpectIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  34257. #endif
  34258. ExpectNotNull(ctx = X509_STORE_CTX_new());
  34259. ExpectIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  34260. ExpectNotNull(store = X509_STORE_new());
  34261. ExpectIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  34262. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  34263. ExpectIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  34264. X509_STORE_CTX_free(ctx);
  34265. X509_STORE_free(store);
  34266. X509_free(x509);
  34267. x509 = NULL;
  34268. BIO_free(bio);
  34269. #endif
  34270. /** d2i_X509_fp test **/
  34271. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  34272. ExpectNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  34273. ExpectNotNull(x509);
  34274. X509_free(x509);
  34275. x509 = NULL;
  34276. if (fp != XBADFILE) {
  34277. XFCLOSE(fp);
  34278. fp = XBADFILE;
  34279. }
  34280. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  34281. ExpectNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  34282. ExpectNotNull(x509);
  34283. X509_free(x509);
  34284. if (fp != XBADFILE)
  34285. XFCLOSE(fp);
  34286. /* X509_up_ref test */
  34287. ExpectIntEQ(X509_up_ref(NULL), 0);
  34288. ExpectNotNull(x509 = X509_new()); /* refCount = 1 */
  34289. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  34290. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  34291. X509_free(x509); /* refCount = 2 */
  34292. X509_free(x509); /* refCount = 1 */
  34293. X509_free(x509); /* refCount = 0, free */
  34294. #endif
  34295. return EXPECT_RESULT();
  34296. }
  34297. static int test_wolfSSL_X509_get_ext_count(void)
  34298. {
  34299. EXPECT_DECLS;
  34300. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  34301. !defined(NO_RSA)
  34302. int ret = 0;
  34303. WOLFSSL_X509* x509 = NULL;
  34304. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  34305. XFILE f = XBADFILE;
  34306. /* NULL parameter check */
  34307. ExpectIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  34308. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  34309. SSL_FILETYPE_PEM));
  34310. ExpectIntEQ(X509_get_ext_count(x509), 5);
  34311. wolfSSL_X509_free(x509);
  34312. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  34313. SSL_FILETYPE_PEM));
  34314. ExpectIntEQ(X509_get_ext_count(x509), 5);
  34315. wolfSSL_X509_free(x509);
  34316. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  34317. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  34318. if (f != XBADFILE)
  34319. XFCLOSE(f);
  34320. /* wolfSSL_X509_get_ext_count() valid input */
  34321. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  34322. /* wolfSSL_X509_get_ext_count() NULL argument */
  34323. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  34324. wolfSSL_X509_free(x509);
  34325. #endif
  34326. return EXPECT_RESULT();
  34327. }
  34328. static int test_wolfSSL_X509_sign2(void)
  34329. {
  34330. EXPECT_DECLS;
  34331. /* test requires WOLFSSL_AKID_NAME to match expected output */
  34332. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  34333. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  34334. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_AKID_NAME) && \
  34335. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  34336. defined(WOLFSSL_IP_ALT_NAME))
  34337. WOLFSSL_X509 *x509 = NULL;
  34338. WOLFSSL_X509 *ca = NULL;
  34339. const unsigned char *der = NULL;
  34340. const unsigned char *pt = NULL;
  34341. WOLFSSL_EVP_PKEY *priv = NULL;
  34342. WOLFSSL_X509_NAME *name = NULL;
  34343. int derSz;
  34344. #ifndef NO_ASN_TIME
  34345. WOLFSSL_ASN1_TIME *notBefore = NULL;
  34346. WOLFSSL_ASN1_TIME *notAfter = NULL;
  34347. const int year = 365*24*60*60;
  34348. const int day = 24*60*60;
  34349. const int hour = 60*60;
  34350. const int mini = 60;
  34351. time_t t;
  34352. #endif
  34353. const unsigned char expected[] = {
  34354. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xFB, 0xA0, 0x03, 0x02, 0x01,
  34355. 0x02, 0x02, 0x14, 0x73, 0xFB, 0x54, 0xD6, 0x03, 0x7D, 0x4C, 0x07, 0x84,
  34356. 0xE2, 0x00, 0x11, 0x8C, 0xDD, 0x90, 0xDC, 0x48, 0x8D, 0xEA, 0x53, 0x30,
  34357. 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
  34358. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
  34359. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  34360. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
  34361. 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  34362. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03,
  34363. 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6F, 0x6F, 0x74,
  34364. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x0A,
  34365. 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x18,
  34366. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77,
  34367. 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  34368. 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  34369. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F,
  34370. 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, 0x17,
  34371. 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  34372. 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  34373. 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30, 0x09,
  34374. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  34375. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74,
  34376. 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07,
  34377. 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15, 0x30,
  34378. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C, 0x66,
  34379. 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  34380. 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67, 0x72,
  34381. 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32, 0x30, 0x34, 0x38, 0x31,
  34382. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  34383. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  34384. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  34385. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  34386. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x82,
  34387. 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  34388. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
  34389. 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE,
  34390. 0x39, 0xA4, 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74,
  34391. 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07,
  34392. 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4, 0x81,
  34393. 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, 0xBB, 0xA1, 0x75, 0xC8, 0x36,
  34394. 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC,
  34395. 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F,
  34396. 0x65, 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, 0xC5,
  34397. 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 0x7A, 0x78, 0xE1, 0x01, 0x56,
  34398. 0x56, 0x91, 0xA6, 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C, 0xEF,
  34399. 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C, 0xA1, 0x3B, 0xF5, 0xF1, 0xA3,
  34400. 0x4A, 0x35, 0xE4, 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E, 0x97,
  34401. 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81, 0xAF, 0x20, 0x0B, 0x43, 0x14,
  34402. 0xC5, 0x74, 0x67, 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88, 0x40,
  34403. 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72, 0x22, 0x17, 0xD7, 0x52, 0x65,
  34404. 0x24, 0x73, 0xB0, 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C, 0x7B,
  34405. 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D, 0x50, 0x6D, 0x3B, 0xA3, 0x3B,
  34406. 0xA3, 0x99, 0x5E, 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A, 0xD9,
  34407. 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB, 0xFF, 0x25, 0x4C, 0xC4, 0xD1,
  34408. 0x79, 0xF4, 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5, 0x72,
  34409. 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7,
  34410. 0x7F, 0xC0, 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3, 0x02,
  34411. 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x4F, 0x30, 0x82, 0x01, 0x4B,
  34412. 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  34413. 0x01, 0xFF, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30,
  34414. 0x13, 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63,
  34415. 0x6F, 0x6D, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03,
  34416. 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  34417. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  34418. 0x85, 0x65, 0xC0, 0x30, 0x81, 0xDE, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04,
  34419. 0x81, 0xD6, 0x30, 0x81, 0xD3, 0x80, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  34420. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  34421. 0x85, 0x65, 0xC0, 0xA1, 0x81, 0xA4, 0xA4, 0x81, 0xA1, 0x30, 0x81, 0x9E,
  34422. 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  34423. 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07,
  34424. 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06,
  34425. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61,
  34426. 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C,
  34427. 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38,
  34428. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50,
  34429. 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32,
  34430. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  34431. 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  34432. 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A,
  34433. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
  34434. 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  34435. 0x6F, 0x6D, 0x82, 0x14, 0x73, 0xFB, 0x54, 0xD6, 0x03, 0x7D, 0x4C, 0x07,
  34436. 0x84, 0xE2, 0x00, 0x11, 0x8C, 0xDD, 0x90, 0xDC, 0x48, 0x8D, 0xEA, 0x53,
  34437. 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  34438. 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2B,
  34439. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  34440. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
  34441. 0x01, 0x01, 0x00, 0x4A, 0xFD, 0x81, 0xC9, 0xE9, 0xE6, 0x2D, 0xC7, 0x1F,
  34442. 0xFA, 0x0A, 0xDC, 0x80, 0x21, 0xCE, 0xD9, 0x27, 0xD4, 0xA4, 0xA1, 0xEC,
  34443. 0x87, 0x50, 0xA9, 0xE4, 0x6D, 0xF6, 0x04, 0x93, 0x5A, 0x1E, 0x51, 0xF4,
  34444. 0x8F, 0x92, 0x3E, 0x58, 0x90, 0xD7, 0xE5, 0xD7, 0x4A, 0x3D, 0xF3, 0xC6,
  34445. 0x1E, 0xE4, 0x78, 0x57, 0xCB, 0xE7, 0xED, 0x3F, 0x6A, 0x7D, 0x1E, 0xE2,
  34446. 0xF1, 0x9F, 0xAA, 0x18, 0x0A, 0xC9, 0x1A, 0xD6, 0x78, 0x71, 0xB3, 0xB6,
  34447. 0xE9, 0x55, 0x84, 0x27, 0x36, 0xA0, 0x89, 0x5C, 0x5A, 0x0A, 0x97, 0x53,
  34448. 0x95, 0x36, 0x68, 0x39, 0xA9, 0x17, 0x51, 0x84, 0x2A, 0x68, 0x5F, 0xAE,
  34449. 0xF3, 0x26, 0x32, 0x57, 0x99, 0x4A, 0x65, 0xE2, 0x14, 0x1E, 0xD8, 0x00,
  34450. 0x24, 0xC1, 0xD1, 0x75, 0x56, 0xD3, 0x99, 0xD3, 0x55, 0x10, 0x88, 0xEC,
  34451. 0x13, 0x05, 0x89, 0x18, 0x58, 0x55, 0x86, 0xFF, 0xA1, 0x2C, 0xB1, 0x96,
  34452. 0xE5, 0x63, 0x1C, 0x83, 0xCA, 0xF6, 0x58, 0x0C, 0xD5, 0xD2, 0x27, 0x70,
  34453. 0x61, 0x87, 0xCC, 0x17, 0x36, 0x6A, 0x75, 0x55, 0xB1, 0x13, 0xB6, 0xC8,
  34454. 0x94, 0x0B, 0x1F, 0xE0, 0x32, 0xCA, 0x94, 0xA2, 0x46, 0x95, 0xBC, 0xA2,
  34455. 0xA0, 0x2A, 0x4C, 0xEB, 0xFE, 0x14, 0xA3, 0x1D, 0x38, 0x13, 0x07, 0xB9,
  34456. 0x98, 0x62, 0x88, 0xF1, 0x8F, 0xBC, 0xD7, 0x3F, 0x72, 0xD4, 0x2F, 0x77,
  34457. 0xF2, 0x48, 0x0E, 0x9C, 0xAC, 0xE1, 0x44, 0x88, 0x58, 0x9A, 0x8E, 0x81,
  34458. 0xBD, 0xB8, 0x6E, 0xF4, 0x64, 0x9B, 0x3A, 0xF1, 0x1D, 0x13, 0xE3, 0x51,
  34459. 0xB9, 0xD1, 0x4D, 0xA3, 0xB5, 0x5D, 0x7B, 0x18, 0xBD, 0xDE, 0xAB, 0x1F,
  34460. 0x82, 0x23, 0xAE, 0x6E, 0xB7, 0xE9, 0xEA, 0x54, 0xE6, 0xF5, 0x3E, 0x10,
  34461. 0x80, 0x25, 0x36, 0x83, 0x46, 0xB2, 0x97, 0x8D, 0x3A, 0x06, 0xB6, 0xCC,
  34462. 0x8D, 0xBE, 0xB4, 0xE6, 0x5E, 0xCA, 0x7B
  34463. };
  34464. pt = ca_key_der_2048;
  34465. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  34466. sizeof_ca_key_der_2048));
  34467. pt = client_cert_der_2048;
  34468. ExpectNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  34469. sizeof_client_cert_der_2048));
  34470. pt = ca_cert_der_2048;
  34471. ExpectNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  34472. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  34473. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  34474. #ifndef NO_ASN_TIME
  34475. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  34476. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  34477. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  34478. ExpectIntEQ(notAfter->length, 13);
  34479. ExpectTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  34480. ExpectTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  34481. #endif
  34482. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  34483. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  34484. ExpectIntEQ(derSz, sizeof(expected));
  34485. #ifndef NO_ASN_TIME
  34486. ExpectIntEQ(XMEMCMP(der, expected, derSz), 0);
  34487. #endif
  34488. wolfSSL_X509_free(ca);
  34489. wolfSSL_X509_free(x509);
  34490. wolfSSL_EVP_PKEY_free(priv);
  34491. #ifndef NO_ASN_TIME
  34492. wolfSSL_ASN1_TIME_free(notBefore);
  34493. wolfSSL_ASN1_TIME_free(notAfter);
  34494. #endif
  34495. #endif
  34496. return EXPECT_RESULT();
  34497. }
  34498. static int test_wolfSSL_X509_sign(void)
  34499. {
  34500. EXPECT_DECLS;
  34501. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_ASN_TIME) && \
  34502. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  34503. int ret;
  34504. char *cn = NULL;
  34505. word32 cnSz;
  34506. X509_NAME *name = NULL;
  34507. X509 *x509 = NULL;
  34508. X509 *ca = NULL;
  34509. DecodedCert dCert;
  34510. EVP_PKEY *pub = NULL;
  34511. EVP_PKEY *priv = NULL;
  34512. EVP_MD_CTX *mctx = NULL;
  34513. #if defined(USE_CERT_BUFFERS_1024)
  34514. const unsigned char* rsaPriv = client_key_der_1024;
  34515. const unsigned char* rsaPub = client_keypub_der_1024;
  34516. const unsigned char* certIssuer = client_cert_der_1024;
  34517. long clientKeySz = (long)sizeof_client_key_der_1024;
  34518. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  34519. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  34520. #elif defined(USE_CERT_BUFFERS_2048)
  34521. const unsigned char* rsaPriv = client_key_der_2048;
  34522. const unsigned char* rsaPub = client_keypub_der_2048;
  34523. const unsigned char* certIssuer = client_cert_der_2048;
  34524. long clientKeySz = (long)sizeof_client_key_der_2048;
  34525. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  34526. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  34527. #endif
  34528. byte sn[16];
  34529. int snSz = sizeof(sn);
  34530. /* Set X509_NAME fields */
  34531. ExpectNotNull(name = X509_NAME_new());
  34532. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  34533. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  34534. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  34535. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  34536. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  34537. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  34538. /* Get private and public keys */
  34539. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  34540. clientKeySz));
  34541. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  34542. ExpectNotNull(x509 = X509_new());
  34543. /* Set version 3 */
  34544. ExpectIntNE(X509_set_version(x509, 2L), 0);
  34545. /* Set subject name, add pubkey, and sign certificate */
  34546. ExpectIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  34547. X509_NAME_free(name);
  34548. name = NULL;
  34549. ExpectIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  34550. #ifdef WOLFSSL_ALT_NAMES
  34551. /* Add some subject alt names */
  34552. ExpectIntNE(wolfSSL_X509_add_altname(NULL,
  34553. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  34554. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  34555. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  34556. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  34557. "sphygmomanometer",
  34558. ASN_DNS_TYPE), SSL_SUCCESS);
  34559. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  34560. "supercalifragilisticexpialidocious",
  34561. ASN_DNS_TYPE), SSL_SUCCESS);
  34562. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  34563. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  34564. ASN_DNS_TYPE), SSL_SUCCESS);
  34565. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  34566. {
  34567. unsigned char ip4_type[] = {127,128,0,255};
  34568. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  34569. 0xff, 0xee, 0x99, 0x88,
  34570. 0x77, 0x66, 0x55, 0x44,
  34571. 0x00, 0x33, 0x22, 0x11};
  34572. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  34573. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  34574. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  34575. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  34576. }
  34577. #endif
  34578. #endif /* WOLFSSL_ALT_NAMES */
  34579. /* test valid sign case */
  34580. ExpectIntGT(ret = X509_sign(x509, priv, EVP_sha256()), 0);
  34581. /* test valid X509_sign_ctx case */
  34582. ExpectNotNull(mctx = EVP_MD_CTX_new());
  34583. ExpectIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  34584. ExpectIntGT(X509_sign_ctx(x509, mctx), 0);
  34585. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  34586. ExpectIntEQ(X509_get_ext_count(x509), 1);
  34587. #endif
  34588. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  34589. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  34590. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  34591. #endif
  34592. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  34593. WOLFSSL_SUCCESS);
  34594. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  34595. /* Variation in size depends on ASN.1 encoding when MSB is set.
  34596. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  34597. * with the MSB set. See GenerateInteger in asn.c */
  34598. #ifndef USE_CERT_BUFFERS_1024
  34599. #ifndef WOLFSSL_ALT_NAMES
  34600. /* Valid case - size should be 781-786 with 16 byte serial number */
  34601. ExpectTrue((781 + snSz <= ret) && (ret <= 781 + 5 + snSz));
  34602. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  34603. /* Valid case - size should be 955-960 with 16 byte serial number */
  34604. ExpectTrue((939 + snSz <= ret) && (ret <= 939 + 5 + snSz));
  34605. #else
  34606. /* Valid case - size should be 926-931 with 16 byte serial number */
  34607. ExpectTrue((910 + snSz <= ret) && (ret <= 910 + 5 + snSz));
  34608. #endif
  34609. #else
  34610. #ifndef WOLFSSL_ALT_NAMES
  34611. /* Valid case - size should be 537-542 with 16 byte serial number */
  34612. ExpectTrue((521 + snSz <= ret) && (ret <= 521 + 5 + snSz));
  34613. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  34614. /* Valid case - size should be 695-670 with 16 byte serial number */
  34615. ExpectTrue((679 + snSz <= ret) && (ret <= 679 + 5 + snSz));
  34616. #else
  34617. /* Valid case - size should be 666-671 with 16 byte serial number */
  34618. ExpectTrue((650 + snSz <= ret) && (ret <= 650 + 5 + snSz));
  34619. #endif
  34620. #endif
  34621. /* check that issuer name is as expected after signature */
  34622. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  34623. ExpectIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  34624. ExpectNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  34625. ExpectNotNull(name = X509_get_subject_name(ca));
  34626. cnSz = X509_NAME_get_sz(name);
  34627. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  34628. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  34629. ExpectIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  34630. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  34631. cn = NULL;
  34632. #ifdef WOLFSSL_MULTI_ATTRIB
  34633. /* test adding multiple OU's to the signer */
  34634. ExpectNotNull(name = X509_get_subject_name(ca));
  34635. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  34636. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  34637. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  34638. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  34639. ExpectIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  34640. #endif
  34641. ExpectNotNull(name = X509_get_subject_name(ca));
  34642. ExpectIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  34643. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  34644. ExpectNotNull(name = X509_get_issuer_name(x509));
  34645. cnSz = X509_NAME_get_sz(name);
  34646. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  34647. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  34648. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  34649. ExpectIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  34650. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  34651. cn = NULL;
  34652. FreeDecodedCert(&dCert);
  34653. /* Test invalid parameters */
  34654. ExpectIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  34655. ExpectIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  34656. ExpectIntEQ(X509_sign(x509, priv, NULL), 0);
  34657. ExpectIntEQ(X509_sign_ctx(NULL, mctx), 0);
  34658. EVP_MD_CTX_free(mctx);
  34659. mctx = NULL;
  34660. ExpectNotNull(mctx = EVP_MD_CTX_new());
  34661. ExpectIntEQ(X509_sign_ctx(x509, mctx), 0);
  34662. ExpectIntEQ(X509_sign_ctx(x509, NULL), 0);
  34663. /* test invalid version number */
  34664. #if defined(OPENSSL_ALL)
  34665. ExpectIntNE(X509_set_version(x509, 6L), 0);
  34666. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  34667. /* uses ParseCert which fails on bad version number */
  34668. ExpectIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  34669. #endif
  34670. EVP_MD_CTX_free(mctx);
  34671. EVP_PKEY_free(priv);
  34672. EVP_PKEY_free(pub);
  34673. X509_free(x509);
  34674. X509_free(ca);
  34675. #endif
  34676. return EXPECT_RESULT();
  34677. }
  34678. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  34679. {
  34680. EXPECT_DECLS;
  34681. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  34682. X509* x509 = NULL;
  34683. const X509_ALGOR* alg;
  34684. ExpectNotNull(x509 = X509_new());
  34685. ExpectNull(alg = X509_get0_tbs_sigalg(NULL));
  34686. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  34687. X509_free(x509);
  34688. #endif
  34689. return EXPECT_RESULT();
  34690. }
  34691. static int test_wolfSSL_X509_ALGOR_get0(void)
  34692. {
  34693. EXPECT_DECLS;
  34694. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  34695. !defined(NO_SHA256) && !defined(NO_RSA)
  34696. X509* x509 = NULL;
  34697. const ASN1_OBJECT* obj = NULL;
  34698. const X509_ALGOR* alg = NULL;
  34699. int pptype = 0;
  34700. const void *ppval = NULL;
  34701. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  34702. SSL_FILETYPE_PEM));
  34703. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  34704. /* Invalid case */
  34705. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  34706. ExpectNull(obj);
  34707. /* Valid case */
  34708. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  34709. ExpectNotNull(obj);
  34710. ExpectNull(ppval);
  34711. ExpectIntNE(pptype, 0);
  34712. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  34713. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  34714. X509_free(x509);
  34715. #endif
  34716. return EXPECT_RESULT();
  34717. }
  34718. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  34719. {
  34720. EXPECT_DECLS;
  34721. #if defined(OPENSSL_EXTRA)
  34722. X509_VERIFY_PARAM *paramTo = NULL;
  34723. X509_VERIFY_PARAM *paramFrom = NULL;
  34724. char testIPv4[] = "127.0.0.1";
  34725. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  34726. char testhostName1[] = "foo.hoge.com";
  34727. char testhostName2[] = "foobar.hoge.com";
  34728. ExpectNotNull(paramTo = X509_VERIFY_PARAM_new());
  34729. ExpectNotNull(XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM)));
  34730. ExpectNotNull(paramFrom = X509_VERIFY_PARAM_new());
  34731. ExpectNotNull(XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM)));
  34732. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  34733. (int)XSTRLEN(testhostName1)), 1);
  34734. ExpectIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  34735. (int)XSTRLEN(testhostName1)));
  34736. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  34737. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  34738. ExpectIntEQ(0x01, paramFrom->hostFlags);
  34739. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4), 0);
  34740. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4), 1);
  34741. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  34742. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL), 1);
  34743. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6), 1);
  34744. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  34745. /* null pointer */
  34746. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, paramFrom), 0);
  34747. /* in the case of "from" null, returns success */
  34748. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, NULL), 1);
  34749. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, NULL), 0);
  34750. /* inherit flags test : VPARAM_DEFAULT */
  34751. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  34752. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  34753. (int)XSTRLEN(testhostName1)));
  34754. ExpectIntEQ(0x01, paramTo->hostFlags);
  34755. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  34756. /* inherit flags test : VPARAM OVERWRITE */
  34757. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  34758. (int)XSTRLEN(testhostName2)), 1);
  34759. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  34760. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  34761. if (paramTo != NULL) {
  34762. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  34763. }
  34764. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  34765. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  34766. (int)XSTRLEN(testhostName1)));
  34767. ExpectIntEQ(0x01, paramTo->hostFlags);
  34768. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  34769. /* inherit flags test : VPARAM_RESET_FLAGS */
  34770. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  34771. (int)XSTRLEN(testhostName2)), 1);
  34772. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  34773. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  34774. if (paramTo != NULL) {
  34775. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  34776. }
  34777. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  34778. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  34779. (int)XSTRLEN(testhostName1)));
  34780. ExpectIntEQ(0x01, paramTo->hostFlags);
  34781. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  34782. /* inherit flags test : VPARAM_LOCKED */
  34783. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  34784. (int)XSTRLEN(testhostName2)), 1);
  34785. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  34786. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  34787. if (paramTo != NULL) {
  34788. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  34789. }
  34790. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  34791. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  34792. (int)XSTRLEN(testhostName2)));
  34793. ExpectIntEQ(0x00, paramTo->hostFlags);
  34794. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  34795. /* test for incorrect parameters */
  34796. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL),
  34797. 0);
  34798. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, 0), 0);
  34799. /* inherit flags test : VPARAM_ONCE, not testable yet */
  34800. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL),
  34801. 1);
  34802. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo),
  34803. X509_V_FLAG_CRL_CHECK_ALL);
  34804. ExpectIntEQ(X509_VERIFY_PARAM_clear_flags(paramTo,
  34805. X509_V_FLAG_CRL_CHECK_ALL), 1);
  34806. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo), 0);
  34807. X509_VERIFY_PARAM_free(paramTo);
  34808. X509_VERIFY_PARAM_free(paramFrom);
  34809. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  34810. #endif
  34811. return EXPECT_RESULT();
  34812. }
  34813. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  34814. static int test_wolfSSL_check_domain_verify_count = 0;
  34815. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  34816. WOLFSSL_X509_STORE_CTX* store)
  34817. {
  34818. EXPECT_DECLS;
  34819. ExpectIntEQ(X509_STORE_CTX_get_error(store), 0);
  34820. ExpectIntEQ(preverify, 1);
  34821. ExpectIntGT(++test_wolfSSL_check_domain_verify_count, 0);
  34822. return EXPECT_SUCCESS();
  34823. }
  34824. static int test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  34825. {
  34826. EXPECT_DECLS;
  34827. X509_VERIFY_PARAM *param = NULL;
  34828. ExpectNotNull(param = SSL_get0_param(ssl));
  34829. /* Domain check should only be done on the leaf cert */
  34830. X509_VERIFY_PARAM_set_hostflags(param,
  34831. X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  34832. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(param,
  34833. "wolfSSL Server Chain", 0), 1);
  34834. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  34835. test_wolfSSL_check_domain_verify_cb);
  34836. return EXPECT_RESULT();
  34837. }
  34838. static int test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  34839. {
  34840. EXPECT_DECLS;
  34841. /* Use a cert with different domains in chain */
  34842. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  34843. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  34844. return EXPECT_RESULT();
  34845. }
  34846. static int test_wolfSSL_check_domain(void)
  34847. {
  34848. EXPECT_DECLS;
  34849. test_ssl_cbf func_cb_client;
  34850. test_ssl_cbf func_cb_server;
  34851. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  34852. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  34853. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  34854. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  34855. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  34856. &func_cb_server, NULL), TEST_SUCCESS);
  34857. /* Should have been called once for each cert in sent chain */
  34858. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  34859. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  34860. #else
  34861. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  34862. #endif
  34863. return EXPECT_RESULT();
  34864. }
  34865. #endif /* OPENSSL_EXTRA && HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  34866. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  34867. {
  34868. EXPECT_DECLS;
  34869. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  34870. X509* x509 = NULL;
  34871. X509_PUBKEY* pubKey;
  34872. ExpectNotNull(x509 = X509_new());
  34873. ExpectNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  34874. ExpectNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  34875. X509_free(x509);
  34876. #endif
  34877. return EXPECT_RESULT();
  34878. }
  34879. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  34880. {
  34881. EXPECT_DECLS;
  34882. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  34883. !defined(NO_SHA256) && !defined(NO_RSA)
  34884. X509* x509 = NULL;
  34885. ASN1_OBJECT* obj = NULL;
  34886. const ASN1_OBJECT* pa_oid = NULL;
  34887. X509_PUBKEY* pubKey = NULL;
  34888. X509_PUBKEY* pubKey2 = NULL;
  34889. EVP_PKEY* evpKey = NULL;
  34890. const unsigned char *pk = NULL;
  34891. int ppklen;
  34892. int pptype;
  34893. X509_ALGOR *pa = NULL;
  34894. const void *pval;
  34895. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  34896. SSL_FILETYPE_PEM));
  34897. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  34898. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  34899. ExpectNotNull(pk);
  34900. ExpectNotNull(pa);
  34901. ExpectNotNull(pubKey);
  34902. ExpectIntGT(ppklen, 0);
  34903. ExpectIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  34904. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  34905. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  34906. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  34907. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  34908. ExpectNotNull(pk);
  34909. ExpectNotNull(pa);
  34910. ExpectIntGT(ppklen, 0);
  34911. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  34912. ExpectNotNull(pa_oid);
  34913. ExpectNull(pval);
  34914. ExpectIntEQ(pptype, V_ASN1_NULL);
  34915. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  34916. X509_PUBKEY_free(pubKey2);
  34917. X509_free(x509);
  34918. EVP_PKEY_free(evpKey);
  34919. #endif
  34920. return EXPECT_RESULT();
  34921. }
  34922. static int test_wolfSSL_X509_PUBKEY_EC(void)
  34923. {
  34924. EXPECT_DECLS;
  34925. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  34926. X509* x509 = NULL;
  34927. ASN1_OBJECT* obj = NULL;
  34928. ASN1_OBJECT* poid = NULL;
  34929. const ASN1_OBJECT* pa_oid = NULL;
  34930. X509_PUBKEY* pubKey = NULL;
  34931. X509_PUBKEY* pubKey2 = NULL;
  34932. EVP_PKEY* evpKey = NULL;
  34933. const unsigned char *pk = NULL;
  34934. int ppklen;
  34935. int pptype;
  34936. X509_ALGOR *pa = NULL;
  34937. const void *pval;
  34938. char buf[50];
  34939. ExpectNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  34940. SSL_FILETYPE_PEM));
  34941. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  34942. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  34943. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  34944. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  34945. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  34946. ExpectNotNull(pk);
  34947. ExpectNotNull(pa);
  34948. ExpectIntGT(ppklen, 0);
  34949. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  34950. ExpectNotNull(pa_oid);
  34951. ExpectNotNull(pval);
  34952. ExpectIntEQ(pptype, V_ASN1_OBJECT);
  34953. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  34954. poid = (ASN1_OBJECT *)pval;
  34955. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  34956. ExpectIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  34957. X509_PUBKEY_free(pubKey2);
  34958. X509_free(x509);
  34959. EVP_PKEY_free(evpKey);
  34960. #endif
  34961. return EXPECT_RESULT();
  34962. }
  34963. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  34964. {
  34965. EXPECT_DECLS;
  34966. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  34967. word32 bytes;
  34968. #ifdef USE_CERT_BUFFERS_1024
  34969. byte tmp[ONEK_BUF];
  34970. #elif defined(USE_CERT_BUFFERS_2048)
  34971. byte tmp[TWOK_BUF];
  34972. #else
  34973. byte tmp[TWOK_BUF];
  34974. #endif /* END USE_CERT_BUFFERS_1024 */
  34975. const unsigned char* dsaKeyDer = tmp;
  34976. ASN1_OBJECT* obj = NULL;
  34977. ASN1_STRING* str;
  34978. const ASN1_OBJECT* pa_oid = NULL;
  34979. X509_PUBKEY* pubKey = NULL;
  34980. EVP_PKEY* evpKey = NULL;
  34981. const unsigned char *pk = NULL;
  34982. int ppklen, pptype;
  34983. X509_ALGOR *pa = NULL;
  34984. const void *pval;
  34985. #ifdef USE_CERT_BUFFERS_1024
  34986. XMEMSET(tmp, 0, sizeof(tmp));
  34987. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  34988. bytes = sizeof_dsa_key_der_1024;
  34989. #elif defined(USE_CERT_BUFFERS_2048)
  34990. XMEMSET(tmp, 0, sizeof(tmp));
  34991. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  34992. bytes = sizeof_dsa_key_der_2048;
  34993. #else
  34994. {
  34995. XFILE fp = XBADFILE;
  34996. XMEMSET(tmp, 0, sizeof(tmp));
  34997. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  34998. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  34999. if (fp != XBADFILE)
  35000. XFCLOSE(fp);
  35001. }
  35002. #endif
  35003. /* Initialize pkey with der format dsa key */
  35004. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  35005. ExpectNotNull(pubKey = X509_PUBKEY_new());
  35006. ExpectIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  35007. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  35008. ExpectNotNull(pk);
  35009. ExpectNotNull(pa);
  35010. ExpectIntGT(ppklen, 0);
  35011. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  35012. ExpectNotNull(pa_oid);
  35013. ExpectNotNull(pval);
  35014. ExpectIntEQ(pptype, V_ASN1_SEQUENCE);
  35015. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  35016. str = (ASN1_STRING *)pval;
  35017. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  35018. #ifdef USE_CERT_BUFFERS_1024
  35019. ExpectIntEQ(ASN1_STRING_length(str), 291);
  35020. #else
  35021. ExpectIntEQ(ASN1_STRING_length(str), 549);
  35022. #endif /* END USE_CERT_BUFFERS_1024 */
  35023. X509_PUBKEY_free(pubKey);
  35024. EVP_PKEY_free(evpKey);
  35025. #endif
  35026. return EXPECT_RESULT();
  35027. }
  35028. static int test_wolfSSL_BUF(void)
  35029. {
  35030. EXPECT_DECLS;
  35031. #if defined(OPENSSL_EXTRA)
  35032. BUF_MEM* buf = NULL;
  35033. ExpectNotNull(buf = BUF_MEM_new());
  35034. ExpectIntEQ(BUF_MEM_grow(buf, 10), 10);
  35035. ExpectIntEQ(BUF_MEM_grow(buf, -1), 0);
  35036. BUF_MEM_free(buf);
  35037. #endif
  35038. return EXPECT_RESULT();
  35039. }
  35040. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  35041. static int stub_rand_seed(const void *buf, int num)
  35042. {
  35043. (void)buf;
  35044. (void)num;
  35045. return 123;
  35046. }
  35047. static int stub_rand_bytes(unsigned char *buf, int num)
  35048. {
  35049. (void)buf;
  35050. (void)num;
  35051. return 456;
  35052. }
  35053. static byte* was_stub_rand_cleanup_called(void)
  35054. {
  35055. static byte was_called = 0;
  35056. return &was_called;
  35057. }
  35058. static void stub_rand_cleanup(void)
  35059. {
  35060. byte* was_called = was_stub_rand_cleanup_called();
  35061. *was_called = 1;
  35062. return;
  35063. }
  35064. static byte* was_stub_rand_add_called(void)
  35065. {
  35066. static byte was_called = 0;
  35067. return &was_called;
  35068. }
  35069. static int stub_rand_add(const void *buf, int num, double entropy)
  35070. {
  35071. byte* was_called = was_stub_rand_add_called();
  35072. (void)buf;
  35073. (void)num;
  35074. (void)entropy;
  35075. *was_called = 1;
  35076. return 0;
  35077. }
  35078. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  35079. {
  35080. (void)buf;
  35081. (void)num;
  35082. return 9876;
  35083. }
  35084. static int stub_rand_status(void)
  35085. {
  35086. return 5432;
  35087. }
  35088. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  35089. static int test_wolfSSL_RAND_set_rand_method(void)
  35090. {
  35091. EXPECT_DECLS;
  35092. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  35093. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  35094. unsigned char* buf = NULL;
  35095. int num = 0;
  35096. double entropy = 0;
  35097. int ret;
  35098. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  35099. byte* was_add_called = was_stub_rand_add_called();
  35100. ExpectNotNull(buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  35101. DYNAMIC_TYPE_TMP_BUFFER));
  35102. ExpectIntNE(wolfSSL_RAND_status(), 5432);
  35103. ExpectIntEQ(*was_cleanup_called, 0);
  35104. RAND_cleanup();
  35105. ExpectIntEQ(*was_cleanup_called, 0);
  35106. rand_methods.seed = &stub_rand_seed;
  35107. rand_methods.bytes = &stub_rand_bytes;
  35108. rand_methods.cleanup = &stub_rand_cleanup;
  35109. rand_methods.add = &stub_rand_add;
  35110. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  35111. rand_methods.status = &stub_rand_status;
  35112. ExpectIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  35113. ExpectIntEQ(RAND_seed(buf, num), 123);
  35114. ExpectIntEQ(RAND_bytes(buf, num), 456);
  35115. ExpectIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  35116. ExpectIntEQ(RAND_status(), 5432);
  35117. ExpectIntEQ(*was_add_called, 0);
  35118. /* The function pointer for RAND_add returns int, but RAND_add itself
  35119. * returns void. */
  35120. RAND_add(buf, num, entropy);
  35121. ExpectIntEQ(*was_add_called, 1);
  35122. was_add_called = 0;
  35123. ExpectIntEQ(*was_cleanup_called, 0);
  35124. RAND_cleanup();
  35125. ExpectIntEQ(*was_cleanup_called, 1);
  35126. *was_cleanup_called = 0;
  35127. ret = RAND_set_rand_method(NULL);
  35128. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  35129. ExpectIntNE(RAND_status(), 5432);
  35130. ExpectIntEQ(*was_cleanup_called, 0);
  35131. RAND_cleanup();
  35132. ExpectIntEQ(*was_cleanup_called, 0);
  35133. RAND_set_rand_method(NULL);
  35134. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  35135. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  35136. return EXPECT_RESULT();
  35137. }
  35138. static int test_wolfSSL_RAND_bytes(void)
  35139. {
  35140. EXPECT_DECLS;
  35141. #if defined(OPENSSL_EXTRA)
  35142. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  35143. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  35144. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  35145. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  35146. int max_bufsize;
  35147. byte *my_buf = NULL;
  35148. /* sanity check */
  35149. ExpectIntEQ(RAND_bytes(NULL, 16), 0);
  35150. ExpectIntEQ(RAND_bytes(NULL, 0), 0);
  35151. max_bufsize = size4;
  35152. ExpectNotNull(my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  35153. DYNAMIC_TYPE_TMP_BUFFER));
  35154. ExpectIntEQ(RAND_bytes(my_buf, 0), 1);
  35155. ExpectIntEQ(RAND_bytes(my_buf, -1), 0);
  35156. ExpectNotNull(XMEMSET(my_buf, 0, max_bufsize));
  35157. ExpectIntEQ(RAND_bytes(my_buf, size1), 1);
  35158. ExpectIntEQ(RAND_bytes(my_buf, size2), 1);
  35159. ExpectIntEQ(RAND_bytes(my_buf, size3), 1);
  35160. ExpectIntEQ(RAND_bytes(my_buf, size4), 1);
  35161. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  35162. #endif
  35163. return EXPECT_RESULT();
  35164. }
  35165. static int test_wolfSSL_RAND(void)
  35166. {
  35167. EXPECT_DECLS;
  35168. #if defined(OPENSSL_EXTRA)
  35169. byte seed[16];
  35170. XMEMSET(seed, 0, sizeof(seed));
  35171. /* No global methods set. */
  35172. ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
  35173. ExpectIntEQ(RAND_poll(), 1);
  35174. RAND_cleanup();
  35175. ExpectIntEQ(RAND_egd(NULL), -1);
  35176. #ifndef NO_FILESYSTEM
  35177. {
  35178. char fname[100];
  35179. ExpectNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  35180. ExpectIntEQ(RAND_write_file(NULL), 0);
  35181. }
  35182. #endif
  35183. #endif
  35184. return EXPECT_RESULT();
  35185. }
  35186. static int test_wolfSSL_PKCS8_Compat(void)
  35187. {
  35188. EXPECT_DECLS;
  35189. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC) && \
  35190. !defined(NO_BIO)
  35191. PKCS8_PRIV_KEY_INFO* pt = NULL;
  35192. BIO* bio = NULL;
  35193. XFILE f = XBADFILE;
  35194. int bytes;
  35195. char pkcs8_buffer[512];
  35196. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  35197. EVP_PKEY *pkey = NULL;
  35198. #endif
  35199. /* file from wolfssl/certs/ directory */
  35200. ExpectTrue((f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb")) != XBADFILE);
  35201. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)),
  35202. 0);
  35203. if (f != XBADFILE)
  35204. XFCLOSE(f);
  35205. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  35206. ExpectNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  35207. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  35208. ExpectNotNull(pkey = EVP_PKCS82PKEY(pt));
  35209. ExpectIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  35210. /* gets PKCS8 pointer to pkey */
  35211. ExpectNotNull(EVP_PKEY2PKCS8(pkey));
  35212. EVP_PKEY_free(pkey);
  35213. #endif
  35214. BIO_free(bio);
  35215. PKCS8_PRIV_KEY_INFO_free(pt);
  35216. #endif
  35217. return EXPECT_RESULT();
  35218. }
  35219. static int test_wolfSSL_PKCS8_d2i(void)
  35220. {
  35221. EXPECT_DECLS;
  35222. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  35223. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  35224. * requires a 12 byte password in FIPS mode. This test ends up
  35225. * trying to use an 8 byte password. */
  35226. #ifndef NO_FILESYSTEM
  35227. unsigned char pkcs8_buffer[2048];
  35228. const unsigned char* p;
  35229. int bytes;
  35230. XFILE file = XBADFILE;
  35231. WOLFSSL_EVP_PKEY* pkey = NULL;
  35232. #ifndef NO_BIO
  35233. BIO* bio = NULL;
  35234. #if defined(OPENSSL_ALL) && \
  35235. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  35236. defined(HAVE_ECC)) && \
  35237. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  35238. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  35239. #endif
  35240. #endif
  35241. #ifndef NO_RSA
  35242. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  35243. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  35244. #ifndef NO_DES3
  35245. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  35246. #endif
  35247. #endif /* NO_RSA */
  35248. #ifdef HAVE_ECC
  35249. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  35250. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  35251. #ifndef NO_DES3
  35252. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  35253. #endif
  35254. #endif /* HAVE_ECC */
  35255. #endif /* !NO_FILESYSTEM */
  35256. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  35257. #ifndef NO_RSA
  35258. #ifdef USE_CERT_BUFFERS_1024
  35259. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  35260. int rsaSz = sizeof_server_key_der_1024;
  35261. #else
  35262. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  35263. int rsaSz = sizeof_server_key_der_2048;
  35264. #endif
  35265. #endif
  35266. #ifdef HAVE_ECC
  35267. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  35268. int ecSz = sizeof_ecc_key_der_256;
  35269. #endif
  35270. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  35271. #ifndef NO_FILESYSTEM
  35272. (void)pkcs8_buffer;
  35273. (void)p;
  35274. (void)bytes;
  35275. (void)file;
  35276. #ifndef NO_BIO
  35277. (void)bio;
  35278. #endif
  35279. #endif
  35280. #ifdef OPENSSL_ALL
  35281. #ifndef NO_RSA
  35282. /* Try to auto-detect normal RSA private key */
  35283. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  35284. EVP_PKEY_free(pkey);
  35285. pkey = NULL;
  35286. #endif
  35287. #ifdef HAVE_ECC
  35288. /* Try to auto-detect normal EC private key */
  35289. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  35290. EVP_PKEY_free(pkey);
  35291. pkey = NULL;
  35292. #endif
  35293. #endif /* OPENSSL_ALL */
  35294. #ifndef NO_FILESYSTEM
  35295. #ifndef NO_RSA
  35296. /* Get DER encoded RSA PKCS#8 data. */
  35297. ExpectTrue((file = XFOPEN(rsaDerPkcs8File, "rb")) != XBADFILE);
  35298. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  35299. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35300. file)), 0);
  35301. if (file != XBADFILE) {
  35302. XFCLOSE(file);
  35303. file = XBADFILE;
  35304. }
  35305. p = pkcs8_buffer;
  35306. #ifdef OPENSSL_ALL
  35307. /* Try to decode - auto-detect key type. */
  35308. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  35309. #else
  35310. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  35311. #endif
  35312. /* Get PEM encoded RSA PKCS#8 data. */
  35313. ExpectTrue((file = XFOPEN(rsaPemPkcs8File, "rb")) != XBADFILE);
  35314. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35315. file)), 0);
  35316. if (file != XBADFILE) {
  35317. XFCLOSE(file);
  35318. file = XBADFILE;
  35319. }
  35320. #if defined(OPENSSL_ALL) && \
  35321. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  35322. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35323. /* Write PKCS#8 PEM to BIO. */
  35324. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  35325. NULL), bytes);
  35326. /* Compare file and written data */
  35327. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  35328. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  35329. BIO_free(bio);
  35330. bio = NULL;
  35331. #if !defined(NO_DES3) && !defined(NO_SHA)
  35332. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35333. /* Write Encrypted PKCS#8 PEM to BIO. */
  35334. bytes = 1834;
  35335. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  35336. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  35337. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  35338. (void*)"yassl123"));
  35339. EVP_PKEY_free(evpPkey);
  35340. evpPkey = NULL;
  35341. BIO_free(bio);
  35342. bio = NULL;
  35343. #endif /* !NO_DES3 && !NO_SHA */
  35344. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  35345. EVP_PKEY_free(pkey);
  35346. pkey = NULL;
  35347. /* PKCS#8 encrypted RSA key */
  35348. #ifndef NO_DES3
  35349. ExpectTrue((file = XFOPEN(rsaDerPkcs8EncFile, "rb")) != XBADFILE);
  35350. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  35351. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35352. file)), 0);
  35353. if (file != XBADFILE) {
  35354. XFCLOSE(file);
  35355. file = XBADFILE;
  35356. }
  35357. #if defined(OPENSSL_ALL) && \
  35358. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  35359. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  35360. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  35361. (void*)"yassl123"));
  35362. EVP_PKEY_free(pkey);
  35363. pkey = NULL;
  35364. BIO_free(bio);
  35365. bio = NULL;
  35366. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  35367. #endif /* !NO_DES3 */
  35368. #endif /* NO_RSA */
  35369. #ifdef HAVE_ECC
  35370. /* PKCS#8 encode EC key */
  35371. ExpectTrue((file = XFOPEN(ecDerPkcs8File, "rb")) != XBADFILE);
  35372. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  35373. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35374. file)), 0);
  35375. if (file != XBADFILE) {
  35376. XFCLOSE(file);
  35377. file = XBADFILE;
  35378. }
  35379. p = pkcs8_buffer;
  35380. #ifdef OPENSSL_ALL
  35381. /* Try to decode - auto-detect key type. */
  35382. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  35383. #else
  35384. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  35385. #endif
  35386. /* Get PEM encoded RSA PKCS#8 data. */
  35387. ExpectTrue((file = XFOPEN(ecPemPkcs8File, "rb")) != XBADFILE);
  35388. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  35389. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35390. file)), 0);
  35391. if (file != XBADFILE) {
  35392. XFCLOSE(file);
  35393. file = XBADFILE;
  35394. }
  35395. #if defined(OPENSSL_ALL) && \
  35396. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  35397. defined(HAVE_AES_CBC)
  35398. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35399. /* Write PKCS#8 PEM to BIO. */
  35400. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  35401. NULL), bytes);
  35402. /* Compare file and written data */
  35403. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  35404. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  35405. BIO_free(bio);
  35406. bio = NULL;
  35407. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35408. /* Write Encrypted PKCS#8 PEM to BIO. */
  35409. bytes = 379;
  35410. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  35411. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  35412. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  35413. (void*)"yassl123"));
  35414. EVP_PKEY_free(evpPkey);
  35415. evpPkey = NULL;
  35416. BIO_free(bio);
  35417. bio = NULL;
  35418. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  35419. EVP_PKEY_free(pkey);
  35420. pkey = NULL;
  35421. /* PKCS#8 encrypted EC key */
  35422. #ifndef NO_DES3
  35423. ExpectTrue((file = XFOPEN(ecDerPkcs8EncFile, "rb")) != XBADFILE);
  35424. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  35425. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35426. file)), 0);
  35427. if (file != XBADFILE) {
  35428. XFCLOSE(file);
  35429. file = XBADFILE;
  35430. }
  35431. #if defined(OPENSSL_ALL) && \
  35432. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  35433. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  35434. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  35435. (void*)"yassl123"));
  35436. EVP_PKEY_free(pkey);
  35437. pkey = NULL;
  35438. BIO_free(bio);
  35439. bio = NULL;
  35440. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  35441. #endif /* !NO_DES3 */
  35442. #endif /* HAVE_ECC */
  35443. #endif /* !NO_FILESYSTEM */
  35444. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  35445. return EXPECT_RESULT();
  35446. }
  35447. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  35448. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  35449. #define LOGGING_THREADS 5
  35450. #define ERROR_COUNT 10
  35451. /* copied from logging.c since this is not exposed otherwise */
  35452. #ifndef ERROR_QUEUE_MAX
  35453. #ifdef ERROR_QUEUE_PER_THREAD
  35454. #define ERROR_QUEUE_MAX 16
  35455. #else
  35456. /* this breaks from compat of unlimited error queue size */
  35457. #define ERROR_QUEUE_MAX 100
  35458. #endif
  35459. #endif
  35460. static volatile int loggingThreadsReady;
  35461. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  35462. {
  35463. const char* file;
  35464. int line;
  35465. unsigned long err;
  35466. int errorCount = 0;
  35467. int i;
  35468. (void)args;
  35469. while (!loggingThreadsReady);
  35470. for (i = 0; i < ERROR_COUNT; i++)
  35471. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  35472. while ((err = ERR_get_error_line(&file, &line))) {
  35473. AssertIntEQ(err, 990 + errorCount);
  35474. errorCount++;
  35475. }
  35476. AssertIntEQ(errorCount, ERROR_COUNT);
  35477. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  35478. ERR_clear_error(); /* ERR_get_error_line() does not remove */
  35479. errorCount = 0;
  35480. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  35481. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  35482. while ((err = ERR_get_error_line(&file, &line))) {
  35483. AssertIntEQ(err, 990 + errorCount);
  35484. errorCount++;
  35485. }
  35486. /* test that the 3 errors over the max were dropped */
  35487. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  35488. WOLFSSL_RETURN_FROM_THREAD(0);
  35489. }
  35490. #endif
  35491. static int test_error_queue_per_thread(void)
  35492. {
  35493. int res = TEST_SKIPPED;
  35494. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  35495. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  35496. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  35497. int i;
  35498. ERR_clear_error(); /* clear out any error nodes */
  35499. loggingThreadsReady = 0;
  35500. for (i = 0; i < LOGGING_THREADS; i++)
  35501. start_thread(test_logging, NULL, &loggingThreads[i]);
  35502. loggingThreadsReady = 1;
  35503. for (i = 0; i < LOGGING_THREADS; i++)
  35504. join_thread(loggingThreads[i]);
  35505. res = TEST_SUCCESS;
  35506. #endif
  35507. return res;
  35508. }
  35509. static int test_wolfSSL_ERR_put_error(void)
  35510. {
  35511. EXPECT_DECLS;
  35512. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  35513. defined(DEBUG_WOLFSSL)
  35514. const char* file;
  35515. int line;
  35516. ERR_clear_error(); /* clear out any error nodes */
  35517. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  35518. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  35519. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  35520. ExpectIntEQ(ERR_get_error_line(&file, &line), 1);
  35521. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  35522. ExpectIntEQ(ERR_get_error_line(&file, &line), 2);
  35523. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  35524. ExpectIntEQ(ERR_get_error_line(&file, &line), 3);
  35525. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  35526. ExpectIntEQ(ERR_get_error_line(&file, &line), 4);
  35527. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  35528. ExpectIntEQ(ERR_get_error_line(&file, &line), 5);
  35529. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  35530. ExpectIntEQ(ERR_get_error_line(&file, &line), 6);
  35531. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  35532. ExpectIntEQ(ERR_get_error_line(&file, &line), 7);
  35533. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  35534. ExpectIntEQ(ERR_get_error_line(&file, &line), 8);
  35535. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  35536. ExpectIntEQ(ERR_get_error_line(&file, &line), 9);
  35537. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  35538. ExpectIntEQ(ERR_get_error_line(&file, &line), 10);
  35539. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  35540. ExpectIntEQ(ERR_get_error_line(&file, &line), 11);
  35541. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  35542. ExpectIntEQ(ERR_get_error_line(&file, &line), 12);
  35543. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  35544. ExpectIntEQ(ERR_get_error_line(&file, &line), 13);
  35545. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  35546. ExpectIntEQ(ERR_get_error_line(&file, &line), 14);
  35547. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  35548. ExpectIntEQ(ERR_get_error_line(&file, &line), 15);
  35549. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  35550. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  35551. "this file", 100);
  35552. ExpectIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  35553. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  35554. ExpectIntEQ(line, 100);
  35555. ExpectIntEQ(wolfSSL_ERR_peek_error(),
  35556. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  35557. ExpectIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  35558. #endif
  35559. /* try reading past end of error queue */
  35560. file = NULL;
  35561. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  35562. ExpectNull(file);
  35563. ExpectIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  35564. PEMerr(4,4);
  35565. ExpectIntEQ(ERR_get_error(), 4);
  35566. /* Empty and free up all error nodes */
  35567. ERR_clear_error();
  35568. /* Verify all nodes are cleared */
  35569. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  35570. ERR_clear_error();
  35571. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  35572. #endif
  35573. return EXPECT_RESULT();
  35574. }
  35575. /*
  35576. * This is a regression test for a bug where the peek/get error functions were
  35577. * drawing from the end of the queue rather than the front.
  35578. */
  35579. static int test_wolfSSL_ERR_get_error_order(void)
  35580. {
  35581. EXPECT_DECLS;
  35582. #if defined(WOLFSSL_HAVE_ERROR_QUEUE) && defined(OPENSSL_EXTRA)
  35583. /* Empty the queue. */
  35584. wolfSSL_ERR_clear_error();
  35585. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  35586. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  35587. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  35588. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  35589. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  35590. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  35591. #endif /* WOLFSSL_HAVE_ERROR_QUEUE && OPENSSL_EXTRA */
  35592. return EXPECT_RESULT();
  35593. }
  35594. #ifndef NO_BIO
  35595. static int test_wolfSSL_ERR_print_errors(void)
  35596. {
  35597. EXPECT_DECLS;
  35598. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  35599. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  35600. BIO* bio = NULL;
  35601. char buf[1024];
  35602. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35603. ERR_clear_error(); /* clear out any error nodes */
  35604. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  35605. /* Choosing -600 as an unused errno. */
  35606. ERR_put_error(0,SYS_F_BIND, -600, "asn.c", 100);
  35607. ERR_print_errors(bio);
  35608. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  35609. ExpectIntEQ(XSTRNCMP(
  35610. "error:173:wolfSSL library:Bad function argument:ssl.c:0",
  35611. buf, 55), 0);
  35612. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  35613. ExpectIntEQ(XSTRNCMP(
  35614. "error:600:wolfSSL library:unknown error number:asn.c:100",
  35615. buf, 56), 0);
  35616. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  35617. ExpectIntEQ(buf[0], '\0');
  35618. ExpectIntEQ(ERR_get_error_line(NULL, NULL), 0);
  35619. BIO_free(bio);
  35620. #endif
  35621. return EXPECT_RESULT();
  35622. }
  35623. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  35624. defined(DEBUG_WOLFSSL)
  35625. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  35626. {
  35627. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  35628. return 0;
  35629. }
  35630. #endif
  35631. static int test_wolfSSL_ERR_print_errors_cb(void)
  35632. {
  35633. EXPECT_DECLS;
  35634. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  35635. defined(DEBUG_WOLFSSL)
  35636. BIO* bio = NULL;
  35637. char buf[1024];
  35638. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35639. ERR_clear_error(); /* clear out any error nodes */
  35640. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  35641. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  35642. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  35643. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  35644. ExpectIntEQ(XSTRNCMP(
  35645. "wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  35646. buf, 53), 0);
  35647. ExpectIntEQ(XSTRNCMP(
  35648. "wolfSSL error occurred, error = 275 line:100 file:asn.c",
  35649. buf + 53, 55), 0);
  35650. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  35651. BIO_free(bio);
  35652. #endif
  35653. return EXPECT_RESULT();
  35654. }
  35655. /*
  35656. * Testing WOLFSSL_ERROR_MSG
  35657. */
  35658. static int test_WOLFSSL_ERROR_MSG(void)
  35659. {
  35660. int res = TEST_SKIPPED;
  35661. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  35662. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  35663. const char* msg = TEST_STRING;
  35664. WOLFSSL_ERROR_MSG(msg);
  35665. res = TEST_SUCCESS;
  35666. #endif
  35667. return res;
  35668. } /* End test_WOLFSSL_ERROR_MSG */
  35669. /*
  35670. * Testing wc_ERR_remove_state
  35671. */
  35672. static int test_wc_ERR_remove_state(void)
  35673. {
  35674. int res = TEST_SKIPPED;
  35675. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  35676. wc_ERR_remove_state();
  35677. res = TEST_SUCCESS;
  35678. #endif
  35679. return res;
  35680. } /* End test_wc_ERR_remove_state */
  35681. /*
  35682. * Testing wc_ERR_print_errors_fp
  35683. */
  35684. static int test_wc_ERR_print_errors_fp(void)
  35685. {
  35686. EXPECT_DECLS;
  35687. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  35688. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  35689. long sz;
  35690. XFILE fp = XBADFILE;
  35691. WOLFSSL_ERROR(BAD_FUNC_ARG);
  35692. ExpectTrue((fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar")) !=
  35693. XBADFILE);
  35694. wc_ERR_print_errors_fp(fp);
  35695. #if defined(DEBUG_WOLFSSL)
  35696. ExpectTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  35697. #ifdef NO_ERROR_QUEUE
  35698. ExpectIntEQ(sz = XFTELL(fp), 0);
  35699. #else
  35700. ExpectIntNE(sz = XFTELL(fp), 0);
  35701. #endif
  35702. #endif
  35703. if (fp != XBADFILE)
  35704. XFCLOSE(fp);
  35705. (void)sz;
  35706. #endif
  35707. return EXPECT_RESULT();
  35708. } /* End test_wc_ERR_print_errors_fp */
  35709. #ifdef DEBUG_WOLFSSL
  35710. static void Logging_cb(const int logLevel, const char *const logMessage)
  35711. {
  35712. (void)logLevel;
  35713. (void)logMessage;
  35714. }
  35715. #endif
  35716. /*
  35717. * Testing wolfSSL_GetLoggingCb
  35718. */
  35719. static int test_wolfSSL_GetLoggingCb(void)
  35720. {
  35721. EXPECT_DECLS;
  35722. #ifdef DEBUG_WOLFSSL
  35723. /* Testing without wolfSSL_SetLoggingCb() */
  35724. ExpectNull(wolfSSL_GetLoggingCb());
  35725. /* Testing with wolfSSL_SetLoggingCb() */
  35726. ExpectIntEQ(wolfSSL_SetLoggingCb(Logging_cb), 0);
  35727. ExpectNotNull(wolfSSL_GetLoggingCb());
  35728. ExpectIntEQ(wolfSSL_SetLoggingCb(NULL), 0);
  35729. #endif
  35730. ExpectNull(wolfSSL_GetLoggingCb());
  35731. return EXPECT_RESULT();
  35732. } /* End test_wolfSSL_GetLoggingCb */
  35733. #endif /* !NO_BIO */
  35734. static int test_wolfSSL_MD4(void)
  35735. {
  35736. EXPECT_DECLS;
  35737. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  35738. MD4_CTX md4;
  35739. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  35740. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  35741. "789012345678901234567890";
  35742. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  35743. "\xcc\x05\x36";
  35744. int msgSz = (int)XSTRLEN(msg);
  35745. XMEMSET(out, 0, sizeof(out));
  35746. MD4_Init(&md4);
  35747. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  35748. MD4_Final(out, &md4);
  35749. ExpectIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  35750. #endif
  35751. return EXPECT_RESULT();
  35752. }
  35753. static int test_wolfSSL_MD5(void)
  35754. {
  35755. EXPECT_DECLS;
  35756. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  35757. byte input1[] = "";
  35758. byte input2[] = "message digest";
  35759. byte hash[WC_MD5_DIGEST_SIZE];
  35760. unsigned char output1[] =
  35761. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  35762. unsigned char output2[] =
  35763. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  35764. WOLFSSL_MD5_CTX md5;
  35765. XMEMSET(&md5, 0, sizeof(md5));
  35766. /* Test cases for illegal parameters */
  35767. ExpectIntEQ(MD5_Init(NULL), 0);
  35768. ExpectIntEQ(MD5_Init(&md5), 1);
  35769. ExpectIntEQ(MD5_Update(NULL, input1, 0), 0);
  35770. ExpectIntEQ(MD5_Update(NULL, NULL, 0), 0);
  35771. ExpectIntEQ(MD5_Update(&md5, NULL, 1), 0);
  35772. ExpectIntEQ(MD5_Final(NULL, &md5), 0);
  35773. ExpectIntEQ(MD5_Final(hash, NULL), 0);
  35774. ExpectIntEQ(MD5_Final(NULL, NULL), 0);
  35775. /* Init MD5 CTX */
  35776. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  35777. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input1, XSTRLEN((const char*)&input1)),
  35778. 1);
  35779. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  35780. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  35781. /* Init MD5 CTX */
  35782. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  35783. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input2,
  35784. (int)XSTRLEN((const char*)input2)), 1);
  35785. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  35786. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  35787. #if !defined(NO_OLD_NAMES) && \
  35788. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  35789. ExpectPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  35790. ExpectPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  35791. ExpectPtrNE(MD5(input1, 1, NULL), NULL);
  35792. ExpectPtrNE(MD5(NULL, 0, NULL), NULL);
  35793. ExpectPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash),
  35794. &hash);
  35795. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  35796. ExpectPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash),
  35797. &hash);
  35798. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  35799. {
  35800. byte data[] = "Data to be hashed.";
  35801. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  35802. ExpectNotNull(MD5(data, sizeof(data), NULL));
  35803. ExpectNotNull(MD5(data, sizeof(data), hash));
  35804. ExpectNotNull(MD5(NULL, 0, hash));
  35805. ExpectNull(MD5(NULL, sizeof(data), hash));
  35806. }
  35807. #endif
  35808. #endif
  35809. return EXPECT_RESULT();
  35810. }
  35811. static int test_wolfSSL_MD5_Transform(void)
  35812. {
  35813. EXPECT_DECLS;
  35814. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  35815. byte input1[] = "";
  35816. byte input2[] = "abc";
  35817. byte local[WC_MD5_BLOCK_SIZE];
  35818. word32 sLen = 0;
  35819. #ifdef BIG_ENDIAN_ORDER
  35820. unsigned char output1[] =
  35821. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  35822. unsigned char output2[] =
  35823. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  35824. #else
  35825. unsigned char output1[] =
  35826. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  35827. unsigned char output2[] =
  35828. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  35829. #endif
  35830. union {
  35831. wc_Md5 native;
  35832. MD5_CTX compat;
  35833. } md5;
  35834. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  35835. XMEMSET(&local, 0, sizeof(local));
  35836. /* sanity check */
  35837. ExpectIntEQ(MD5_Transform(NULL, NULL), 0);
  35838. ExpectIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  35839. ExpectIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  35840. ExpectIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  35841. ExpectIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  35842. ExpectIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  35843. /* Init MD5 CTX */
  35844. ExpectIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  35845. /* Do Transform*/
  35846. sLen = (word32)XSTRLEN((char*)input1);
  35847. XMEMCPY(local, input1, sLen);
  35848. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  35849. ExpectIntEQ(XMEMCMP(md5.native.digest, output1, WC_MD5_DIGEST_SIZE), 0);
  35850. /* Init MD5 CTX */
  35851. ExpectIntEQ(MD5_Init(&md5.compat), 1);
  35852. sLen = (word32)XSTRLEN((char*)input2);
  35853. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  35854. XMEMCPY(local, input2, sLen);
  35855. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  35856. ExpectIntEQ(XMEMCMP(md5.native.digest, output2, WC_MD5_DIGEST_SIZE), 0);
  35857. #endif
  35858. return EXPECT_RESULT();
  35859. }
  35860. static int test_wolfSSL_SHA(void)
  35861. {
  35862. EXPECT_DECLS;
  35863. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  35864. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  35865. (!defined(HAVE_FIPS) || \
  35866. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  35867. {
  35868. const unsigned char in[] = "abc";
  35869. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  35870. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  35871. unsigned char out[WC_SHA_DIGEST_SIZE];
  35872. unsigned char* p;
  35873. WOLFSSL_SHA_CTX sha;
  35874. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  35875. ExpectNotNull(SHA1(in, XSTRLEN((char*)in), out));
  35876. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  35877. /* SHA interface test */
  35878. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  35879. ExpectNull(SHA(NULL, XSTRLEN((char*)in), out));
  35880. ExpectNotNull(SHA(in, 0, out));
  35881. ExpectNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  35882. ExpectNotNull(SHA(NULL, 0, out));
  35883. ExpectNotNull(SHA(NULL, 0, NULL));
  35884. ExpectNotNull(SHA(in, XSTRLEN((char*)in), out));
  35885. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  35886. ExpectNotNull(p = SHA(in, XSTRLEN((char*)in), NULL));
  35887. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA_DIGEST_SIZE), 0);
  35888. ExpectIntEQ(wolfSSL_SHA_Init(&sha), 1);
  35889. ExpectIntEQ(wolfSSL_SHA_Update(&sha, in, XSTRLEN((char*)in)), 1);
  35890. ExpectIntEQ(wolfSSL_SHA_Final(out, &sha), 1);
  35891. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  35892. ExpectIntEQ(wolfSSL_SHA1_Init(&sha), 1);
  35893. ExpectIntEQ(wolfSSL_SHA1_Update(&sha, in, XSTRLEN((char*)in)), 1);
  35894. ExpectIntEQ(wolfSSL_SHA1_Final(out, &sha), 1);
  35895. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  35896. }
  35897. #endif
  35898. #if !defined(NO_SHA256)
  35899. {
  35900. const unsigned char in[] = "abc";
  35901. unsigned char expected[] =
  35902. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  35903. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  35904. "\x15\xAD";
  35905. unsigned char out[WC_SHA256_DIGEST_SIZE];
  35906. unsigned char* p;
  35907. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  35908. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  35909. ExpectNotNull(SHA256(in, XSTRLEN((char*)in), out));
  35910. #else
  35911. ExpectNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  35912. #endif
  35913. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  35914. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  35915. ExpectNotNull(p = SHA256(in, XSTRLEN((char*)in), NULL));
  35916. #else
  35917. ExpectNotNull(p = wolfSSL_SHA256(in, XSTRLEN((char*)in), NULL));
  35918. #endif
  35919. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA256_DIGEST_SIZE), 0);
  35920. }
  35921. #endif
  35922. #if defined(WOLFSSL_SHA384)
  35923. {
  35924. const unsigned char in[] = "abc";
  35925. unsigned char expected[] =
  35926. "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  35927. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  35928. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  35929. "\xc8\x25\xa7";
  35930. unsigned char out[WC_SHA384_DIGEST_SIZE];
  35931. unsigned char* p;
  35932. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  35933. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  35934. ExpectNotNull(SHA384(in, XSTRLEN((char*)in), out));
  35935. #else
  35936. ExpectNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  35937. #endif
  35938. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  35939. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  35940. ExpectNotNull(p = SHA384(in, XSTRLEN((char*)in), NULL));
  35941. #else
  35942. ExpectNotNull(p = wolfSSL_SHA384(in, XSTRLEN((char*)in), NULL));
  35943. #endif
  35944. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA384_DIGEST_SIZE), 0);
  35945. }
  35946. #endif
  35947. #if defined(WOLFSSL_SHA512)
  35948. {
  35949. const unsigned char in[] = "abc";
  35950. unsigned char expected[] =
  35951. "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  35952. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  35953. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  35954. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  35955. "\xa5\x4c\xa4\x9f";
  35956. unsigned char out[WC_SHA512_DIGEST_SIZE];
  35957. unsigned char* p;
  35958. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  35959. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  35960. ExpectNotNull(SHA512(in, XSTRLEN((char*)in), out));
  35961. #else
  35962. ExpectNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  35963. #endif
  35964. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  35965. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  35966. ExpectNotNull(p = SHA512(in, XSTRLEN((char*)in), NULL));
  35967. #else
  35968. ExpectNotNull(p = wolfSSL_SHA512(in, XSTRLEN((char*)in), NULL));
  35969. #endif
  35970. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA512_DIGEST_SIZE), 0);
  35971. }
  35972. #endif
  35973. #endif
  35974. return EXPECT_RESULT();
  35975. }
  35976. static int test_wolfSSL_SHA_Transform(void)
  35977. {
  35978. EXPECT_DECLS;
  35979. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  35980. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  35981. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  35982. byte input1[] = "";
  35983. byte input2[] = "abc";
  35984. byte local[WC_SHA_BLOCK_SIZE];
  35985. word32 sLen = 0;
  35986. #ifdef BIG_ENDIAN_ORDER
  35987. unsigned char output1[] =
  35988. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  35989. "\x09\xf7\x3a\x93";
  35990. unsigned char output2[] =
  35991. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  35992. "\x7c\x6e\x08\xb8";
  35993. #else
  35994. unsigned char output1[] =
  35995. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  35996. "\x93\x3a\xf7\x09";
  35997. unsigned char output2[] =
  35998. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  35999. "\xb8\x08\x6e\x7c";
  36000. #endif
  36001. union {
  36002. wc_Sha native;
  36003. SHA_CTX compat;
  36004. } sha;
  36005. union {
  36006. wc_Sha native;
  36007. SHA_CTX compat;
  36008. } sha1;
  36009. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  36010. XMEMSET(&local, 0, sizeof(local));
  36011. /* sanity check */
  36012. ExpectIntEQ(SHA_Transform(NULL, NULL), 0);
  36013. ExpectIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  36014. ExpectIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  36015. ExpectIntEQ(SHA1_Transform(NULL, NULL), 0);
  36016. ExpectIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  36017. ExpectIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  36018. ExpectIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  36019. ExpectIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  36020. ExpectIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  36021. /* Init SHA CTX */
  36022. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  36023. /* Do Transform*/
  36024. sLen = (word32)XSTRLEN((char*)input1);
  36025. XMEMCPY(local, input1, sLen);
  36026. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  36027. ExpectIntEQ(XMEMCMP(sha.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  36028. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  36029. /* Init SHA CTX */
  36030. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  36031. sLen = (word32)XSTRLEN((char*)input2);
  36032. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  36033. XMEMCPY(local, input2, sLen);
  36034. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  36035. ExpectIntEQ(XMEMCMP(sha.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  36036. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  36037. /* SHA1 */
  36038. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  36039. /* Init SHA CTX */
  36040. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  36041. /* Do Transform*/
  36042. sLen = (word32)XSTRLEN((char*)input1);
  36043. XMEMCPY(local, input1, sLen);
  36044. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  36045. ExpectIntEQ(XMEMCMP(sha1.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  36046. ExpectIntEQ(SHA1_Final(local, &sha1.compat), 1); /* frees resources */
  36047. /* Init SHA CTX */
  36048. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  36049. sLen = (word32)XSTRLEN((char*)input2);
  36050. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  36051. XMEMCPY(local, input2, sLen);
  36052. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  36053. ExpectIntEQ(XMEMCMP(sha1.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  36054. ExpectIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  36055. #endif
  36056. #endif
  36057. return EXPECT_RESULT();
  36058. }
  36059. static int test_wolfSSL_SHA224(void)
  36060. {
  36061. EXPECT_DECLS;
  36062. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  36063. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36064. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  36065. unsigned char input[] =
  36066. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  36067. unsigned char output[] =
  36068. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  36069. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  36070. size_t inLen;
  36071. byte hash[WC_SHA224_DIGEST_SIZE];
  36072. unsigned char* p;
  36073. inLen = XSTRLEN((char*)input);
  36074. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  36075. ExpectNull(SHA224(NULL, inLen, hash));
  36076. ExpectNotNull(SHA224(input, 0, hash));
  36077. ExpectNotNull(SHA224(input, inLen, NULL));
  36078. ExpectNotNull(SHA224(NULL, 0, hash));
  36079. ExpectNotNull(SHA224(NULL, 0, NULL));
  36080. ExpectNotNull(SHA224(input, inLen, hash));
  36081. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  36082. ExpectNotNull(p = SHA224(input, inLen, NULL));
  36083. ExpectIntEQ(XMEMCMP(p, output, WC_SHA224_DIGEST_SIZE), 0);
  36084. #endif
  36085. return EXPECT_RESULT();
  36086. }
  36087. static int test_wolfSSL_SHA256(void)
  36088. {
  36089. EXPECT_DECLS;
  36090. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  36091. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  36092. unsigned char input[] =
  36093. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  36094. unsigned char output[] =
  36095. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  36096. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  36097. "\x06\xC1";
  36098. size_t inLen;
  36099. byte hash[WC_SHA256_DIGEST_SIZE];
  36100. inLen = XSTRLEN((char*)input);
  36101. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  36102. ExpectNotNull(SHA256(input, inLen, hash));
  36103. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  36104. #endif
  36105. return EXPECT_RESULT();
  36106. }
  36107. static int test_wolfSSL_SHA256_Transform(void)
  36108. {
  36109. EXPECT_DECLS;
  36110. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  36111. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36112. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  36113. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH) && \
  36114. !defined(WOLFSSL_KCAPI_HASH)
  36115. byte input1[] = "";
  36116. byte input2[] = "abc";
  36117. byte local[WC_SHA256_BLOCK_SIZE];
  36118. word32 sLen = 0;
  36119. #ifdef BIG_ENDIAN_ORDER
  36120. unsigned char output1[] =
  36121. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  36122. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  36123. unsigned char output2[] =
  36124. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  36125. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  36126. #else
  36127. unsigned char output1[] =
  36128. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  36129. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  36130. unsigned char output2[] =
  36131. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  36132. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  36133. #endif
  36134. union {
  36135. wc_Sha256 native;
  36136. SHA256_CTX compat;
  36137. } sha256;
  36138. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  36139. XMEMSET(&local, 0, sizeof(local));
  36140. /* sanity check */
  36141. ExpectIntEQ(SHA256_Transform(NULL, NULL), 0);
  36142. ExpectIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  36143. ExpectIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  36144. ExpectIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  36145. ExpectIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  36146. ExpectIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  36147. /* Init SHA256 CTX */
  36148. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  36149. /* Do Transform*/
  36150. sLen = (word32)XSTRLEN((char*)input1);
  36151. XMEMCPY(local, input1, sLen);
  36152. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  36153. ExpectIntEQ(XMEMCMP(sha256.native.digest, output1, WC_SHA256_DIGEST_SIZE),
  36154. 0);
  36155. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  36156. /* Init SHA256 CTX */
  36157. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  36158. sLen = (word32)XSTRLEN((char*)input2);
  36159. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  36160. XMEMCPY(local, input2, sLen);
  36161. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  36162. ExpectIntEQ(XMEMCMP(sha256.native.digest, output2, WC_SHA256_DIGEST_SIZE),
  36163. 0);
  36164. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  36165. #endif
  36166. #endif
  36167. return EXPECT_RESULT();
  36168. }
  36169. static int test_wolfSSL_SHA512_Transform(void)
  36170. {
  36171. EXPECT_DECLS;
  36172. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  36173. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36174. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  36175. !defined(WOLFSSL_KCAPI_HASH)
  36176. byte input1[] = "";
  36177. byte input2[] = "abc";
  36178. byte local[WC_SHA512_BLOCK_SIZE];
  36179. word32 sLen = 0;
  36180. #ifdef BIG_ENDIAN_ORDER
  36181. unsigned char output1[] =
  36182. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  36183. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  36184. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  36185. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  36186. unsigned char output2[] =
  36187. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  36188. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  36189. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  36190. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  36191. #else
  36192. unsigned char output1[] =
  36193. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  36194. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  36195. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  36196. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  36197. unsigned char output2[] =
  36198. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  36199. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  36200. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  36201. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  36202. #endif
  36203. union {
  36204. wc_Sha512 native;
  36205. SHA512_CTX compat;
  36206. } sha512;
  36207. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  36208. XMEMSET(&local, 0, sizeof(local));
  36209. /* sanity check */
  36210. ExpectIntEQ(SHA512_Transform(NULL, NULL), 0);
  36211. ExpectIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  36212. ExpectIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  36213. ExpectIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  36214. ExpectIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  36215. ExpectIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  36216. /* Init SHA512 CTX */
  36217. ExpectIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  36218. /* Do Transform*/
  36219. sLen = (word32)XSTRLEN((char*)input1);
  36220. XMEMCPY(local, input1, sLen);
  36221. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  36222. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  36223. WC_SHA512_DIGEST_SIZE), 0);
  36224. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  36225. /* Init SHA512 CTX */
  36226. ExpectIntEQ(SHA512_Init(&sha512.compat), 1);
  36227. sLen = (word32)XSTRLEN((char*)input2);
  36228. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  36229. XMEMCPY(local, input2, sLen);
  36230. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  36231. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  36232. WC_SHA512_DIGEST_SIZE), 0);
  36233. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  36234. (void)input1;
  36235. #endif
  36236. #endif
  36237. return EXPECT_RESULT();
  36238. }
  36239. static int test_wolfSSL_SHA512_224_Transform(void)
  36240. {
  36241. EXPECT_DECLS;
  36242. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  36243. !defined(WOLFSSL_NOSHA512_224)
  36244. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36245. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  36246. !defined(WOLFSSL_KCAPI_HASH)
  36247. byte input1[] = "";
  36248. byte input2[] = "abc";
  36249. byte local[WC_SHA512_BLOCK_SIZE];
  36250. word32 sLen = 0;
  36251. unsigned char output1[] =
  36252. "\x94\x24\x66\xd4\x60\x3a\xeb\x23\x1d\xa8\x69\x31\x3c\xd2\xde\x11"
  36253. "\x48\x0f\x4a\x5a\xdf\x3a\x8d\x87\xcf\xcd\xbf\xa5\x03\x21\x50\xf1"
  36254. "\x8a\x0d\x0f\x0d\x3c\x07\xba\x52\xe0\xaa\x3c\xbb\xf1\xd3\x3f\xca"
  36255. "\x12\xa7\x61\xf8\x47\xda\x0d\x1b\x79\xc2\x65\x13\x92\xc1\x9c\xa5";
  36256. unsigned char output2[] =
  36257. "\x51\x28\xe7\x0b\xca\x1e\xbc\x5f\xd7\x34\x0b\x48\x30\xd7\xc2\x75"
  36258. "\x6d\x8d\x48\x2c\x1f\xc7\x9e\x2b\x20\x5e\xbb\x0f\x0e\x4d\xb7\x61"
  36259. "\x31\x76\x33\xa0\xb4\x3d\x5f\x93\xc1\x73\xac\xf7\x21\xff\x69\x17"
  36260. "\xce\x66\xe5\x1e\x31\xe7\xf3\x22\x0f\x0b\x34\xd7\x5a\x57\xeb\xbf";
  36261. union {
  36262. wc_Sha512 native;
  36263. SHA512_CTX compat;
  36264. } sha512;
  36265. #ifdef BIG_ENDIAN_ORDER
  36266. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  36267. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  36268. #endif
  36269. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  36270. XMEMSET(&local, 0, sizeof(local));
  36271. /* sanity check */
  36272. ExpectIntEQ(SHA512_224_Transform(NULL, NULL), 0);
  36273. ExpectIntEQ(SHA512_224_Transform(NULL, (const byte*)&input1), 0);
  36274. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, NULL), 0);
  36275. ExpectIntEQ(wc_Sha512_224Transform(NULL, NULL), BAD_FUNC_ARG);
  36276. ExpectIntEQ(wc_Sha512_224Transform(NULL, (const byte*)&input1),
  36277. BAD_FUNC_ARG);
  36278. ExpectIntEQ(wc_Sha512_224Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  36279. /* Init SHA512 CTX */
  36280. ExpectIntEQ(wolfSSL_SHA512_224_Init(&sha512.compat), 1);
  36281. /* Do Transform*/
  36282. sLen = (word32)XSTRLEN((char*)input1);
  36283. XMEMCPY(local, input1, sLen);
  36284. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  36285. 1);
  36286. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  36287. WC_SHA512_DIGEST_SIZE), 0);
  36288. /* frees resources */
  36289. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  36290. /* Init SHA512 CTX */
  36291. ExpectIntEQ(SHA512_224_Init(&sha512.compat), 1);
  36292. sLen = (word32)XSTRLEN((char*)input2);
  36293. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  36294. XMEMCPY(local, input2, sLen);
  36295. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  36296. 1);
  36297. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  36298. WC_SHA512_DIGEST_SIZE), 0);
  36299. /* frees resources */
  36300. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  36301. #endif
  36302. #endif
  36303. return EXPECT_RESULT();
  36304. }
  36305. static int test_wolfSSL_SHA512_256_Transform(void)
  36306. {
  36307. EXPECT_DECLS;
  36308. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  36309. !defined(WOLFSSL_NOSHA512_256)
  36310. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36311. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  36312. !defined(WOLFSSL_KCAPI_HASH)
  36313. byte input1[] = "";
  36314. byte input2[] = "abc";
  36315. byte local[WC_SHA512_BLOCK_SIZE];
  36316. word32 sLen = 0;
  36317. unsigned char output1[] =
  36318. "\xf8\x37\x37\x5a\xd7\x2e\x56\xec\xe2\x51\xa8\x31\x3a\xa0\x63\x2b"
  36319. "\x7e\x7c\x64\xcc\xd9\xff\x2b\x6b\xeb\xc3\xd4\x4d\x7f\x8a\x3a\xb5"
  36320. "\x61\x85\x0b\x37\x30\x9f\x3b\x08\x5e\x7b\xd3\xbc\x6d\x00\x61\xc0"
  36321. "\x65\x9a\xd7\x73\xda\x40\xbe\xc1\xe5\x2f\xc6\x5d\xb7\x9f\xbe\x60";
  36322. unsigned char output2[] =
  36323. "\x22\xad\xc0\x30\xee\xd4\x6a\xef\x13\xee\x5a\x95\x8b\x1f\xb7\xb6"
  36324. "\xb6\xba\xc0\x44\xb8\x18\x3b\xf0\xf6\x4b\x70\x9f\x03\xba\x64\xa1"
  36325. "\xe1\xe3\x45\x15\x91\x7d\xcb\x0b\x9a\xf0\xd2\x8e\x47\x8b\x37\x78"
  36326. "\x91\x41\xa6\xc4\xb0\x29\x8f\x8b\xdd\x78\x5c\xf2\x73\x3f\x21\x31";
  36327. union {
  36328. wc_Sha512 native;
  36329. SHA512_CTX compat;
  36330. } sha512;
  36331. #ifdef BIG_ENDIAN_ORDER
  36332. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  36333. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  36334. #endif
  36335. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  36336. XMEMSET(&local, 0, sizeof(local));
  36337. /* sanity check */
  36338. ExpectIntEQ(SHA512_256_Transform(NULL, NULL), 0);
  36339. ExpectIntEQ(SHA512_256_Transform(NULL, (const byte*)&input1), 0);
  36340. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, NULL), 0);
  36341. ExpectIntEQ(wc_Sha512_256Transform(NULL, NULL), BAD_FUNC_ARG);
  36342. ExpectIntEQ(wc_Sha512_256Transform(NULL, (const byte*)&input1),
  36343. BAD_FUNC_ARG);
  36344. ExpectIntEQ(wc_Sha512_256Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  36345. /* Init SHA512 CTX */
  36346. ExpectIntEQ(wolfSSL_SHA512_256_Init(&sha512.compat), 1);
  36347. /* Do Transform*/
  36348. sLen = (word32)XSTRLEN((char*)input1);
  36349. XMEMCPY(local, input1, sLen);
  36350. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  36351. 1);
  36352. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  36353. WC_SHA512_DIGEST_SIZE), 0);
  36354. /* frees resources */
  36355. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  36356. /* Init SHA512 CTX */
  36357. ExpectIntEQ(SHA512_256_Init(&sha512.compat), 1);
  36358. sLen = (word32)XSTRLEN((char*)input2);
  36359. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  36360. XMEMCPY(local, input2, sLen);
  36361. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  36362. 1);
  36363. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  36364. WC_SHA512_DIGEST_SIZE), 0);
  36365. /* frees resources */
  36366. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  36367. #endif
  36368. #endif
  36369. return EXPECT_RESULT();
  36370. }
  36371. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  36372. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  36373. * buffer of 64 bytes.
  36374. *
  36375. * returns the size of the digest buffer on success and a negative value on
  36376. * failure.
  36377. */
  36378. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest,
  36379. int* sz)
  36380. {
  36381. EXPECT_DECLS;
  36382. HMAC_CTX ctx1;
  36383. HMAC_CTX ctx2;
  36384. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  36385. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  36386. unsigned char long_key[] =
  36387. "0123456789012345678901234567890123456789"
  36388. "0123456789012345678901234567890123456789"
  36389. "0123456789012345678901234567890123456789"
  36390. "0123456789012345678901234567890123456789";
  36391. unsigned char msg[] = "message to hash";
  36392. unsigned int digestSz = 64;
  36393. int keySz = sizeof(key);
  36394. int long_keySz = sizeof(long_key);
  36395. int msgSz = sizeof(msg);
  36396. unsigned char digest2[64];
  36397. unsigned int digestSz2 = 64;
  36398. HMAC_CTX_init(&ctx1);
  36399. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  36400. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36401. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36402. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36403. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36404. HMAC_CTX_cleanup(&ctx1);
  36405. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36406. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  36407. HMAC_CTX_cleanup(&ctx2);
  36408. ExpectIntEQ(digestSz, digestSz2);
  36409. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36410. /* test HMAC_Init with NULL key */
  36411. /* init after copy */
  36412. HMAC_CTX_init(&ctx1);
  36413. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  36414. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36415. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36416. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  36417. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36418. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36419. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36420. HMAC_CTX_cleanup(&ctx1);
  36421. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  36422. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36423. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36424. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  36425. HMAC_CTX_cleanup(&ctx2);
  36426. ExpectIntEQ(digestSz, digestSz2);
  36427. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36428. /* long key */
  36429. HMAC_CTX_init(&ctx1);
  36430. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type),
  36431. SSL_SUCCESS);
  36432. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36433. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36434. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  36435. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36436. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36437. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36438. HMAC_CTX_cleanup(&ctx1);
  36439. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  36440. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36441. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36442. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  36443. HMAC_CTX_cleanup(&ctx2);
  36444. ExpectIntEQ(digestSz, digestSz2);
  36445. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36446. /* init before copy */
  36447. HMAC_CTX_init(&ctx1);
  36448. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  36449. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36450. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  36451. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36452. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36453. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36454. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36455. HMAC_CTX_cleanup(&ctx1);
  36456. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36457. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36458. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  36459. HMAC_CTX_cleanup(&ctx2);
  36460. ExpectIntEQ(digestSz, digestSz2);
  36461. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36462. *sz = digestSz;
  36463. return EXPECT_RESULT();
  36464. }
  36465. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  36466. static int test_wolfSSL_HMAC_CTX(void)
  36467. {
  36468. EXPECT_DECLS;
  36469. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  36470. unsigned char digest[64];
  36471. int digestSz;
  36472. WOLFSSL_HMAC_CTX* hmac_ctx = NULL;
  36473. WOLFSSL_HMAC_CTX ctx1;
  36474. WOLFSSL_HMAC_CTX ctx2;
  36475. ExpectNotNull(hmac_ctx = wolfSSL_HMAC_CTX_new());
  36476. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(NULL), 1);
  36477. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(hmac_ctx), 1);
  36478. wolfSSL_HMAC_CTX_free(NULL);
  36479. wolfSSL_HMAC_CTX_free(hmac_ctx);
  36480. XMEMSET(&ctx2, 0, sizeof(WOLFSSL_HMAC_CTX));
  36481. ExpectIntEQ(HMAC_CTX_init(NULL), 1);
  36482. ExpectIntEQ(HMAC_CTX_init(&ctx2), 1);
  36483. ExpectIntEQ(HMAC_CTX_copy(NULL, NULL), 0);
  36484. ExpectIntEQ(HMAC_CTX_copy(NULL, &ctx2), 0);
  36485. ExpectIntEQ(HMAC_CTX_copy(&ctx2, NULL), 0);
  36486. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  36487. ((! defined(HAVE_FIPS_VERSION)) || \
  36488. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  36489. /* Copy object that hasn't had a digest set - MD5. */
  36490. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 1);
  36491. #else
  36492. /* Copy object that hasn't had a digest set. */
  36493. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 0);
  36494. #endif
  36495. HMAC_CTX_cleanup(NULL);
  36496. HMAC_CTX_cleanup(&ctx2);
  36497. ExpectNull(HMAC_CTX_get_md(NULL));
  36498. #ifndef NO_SHA
  36499. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha1(), digest, &digestSz)),
  36500. TEST_SUCCESS);
  36501. ExpectIntEQ(digestSz, 20);
  36502. ExpectIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  36503. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  36504. #endif /* !NO_SHA */
  36505. #ifdef WOLFSSL_SHA224
  36506. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha224(), digest, &digestSz)),
  36507. TEST_SUCCESS);
  36508. ExpectIntEQ(digestSz, 28);
  36509. ExpectIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  36510. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  36511. "\x02\x0E", digest, digestSz), 0);
  36512. #endif /* WOLFSSL_SHA224 */
  36513. #ifndef NO_SHA256
  36514. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha256(), digest, &digestSz)),
  36515. TEST_SUCCESS);
  36516. ExpectIntEQ(digestSz, 32);
  36517. ExpectIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  36518. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  36519. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  36520. #endif /* !NO_SHA256 */
  36521. #ifdef WOLFSSL_SHA384
  36522. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha384(), digest, &digestSz)),
  36523. TEST_SUCCESS);
  36524. ExpectIntEQ(digestSz, 48);
  36525. ExpectIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  36526. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  36527. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  36528. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  36529. digest, digestSz), 0);
  36530. #endif /* WOLFSSL_SHA384 */
  36531. #ifdef WOLFSSL_SHA512
  36532. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha512(), digest, &digestSz)),
  36533. TEST_SUCCESS);
  36534. ExpectIntEQ(digestSz, 64);
  36535. ExpectIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  36536. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  36537. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  36538. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  36539. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  36540. digest, digestSz), 0);
  36541. #endif /* WOLFSSL_SHA512 */
  36542. #ifdef WOLFSSL_SHA3
  36543. #ifndef WOLFSSL_NOSHA3_224
  36544. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_224(), digest, &digestSz)),
  36545. TEST_SUCCESS);
  36546. ExpectIntEQ(digestSz, 28);
  36547. ExpectIntEQ(XMEMCMP("\xdc\x53\x25\x3f\xc0\x9d\x2b\x0c\x7f\x59\x11\x17\x08"
  36548. "\x5c\xe8\x43\x31\x01\x5a\xb3\xe3\x08\x37\x71\x26\x0b"
  36549. "\x29\x0f", digest, digestSz), 0);
  36550. #endif
  36551. #ifndef WOLFSSL_NOSHA3_256
  36552. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_256(), digest, &digestSz)),
  36553. TEST_SUCCESS);
  36554. ExpectIntEQ(digestSz, 32);
  36555. ExpectIntEQ(XMEMCMP("\x0f\x00\x89\x82\x15\xce\xd6\x45\x01\x83\xce\xc8\x35"
  36556. "\xab\x71\x07\xc9\xfe\x61\x22\x38\xf9\x09\xad\x35\x65"
  36557. "\x43\x77\x24\xd4\x1e\xf4", digest, digestSz), 0);
  36558. #endif
  36559. #ifndef WOLFSSL_NOSHA3_384
  36560. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_384(), digest, &digestSz)),
  36561. TEST_SUCCESS);
  36562. ExpectIntEQ(digestSz, 48);
  36563. ExpectIntEQ(XMEMCMP("\x0f\x6a\xc0\xfb\xc3\xf2\x80\xb1\xb4\x04\xb6\xc8\x45"
  36564. "\x23\x3b\xb4\xbe\xc6\xea\x85\x07\xca\x8c\x71\xbb\x6e"
  36565. "\x79\xf6\xf9\x2b\x98\xf5\xef\x11\x39\xd4\x5d\xd3\xca"
  36566. "\xc0\xe6\x81\xf7\x73\xf9\x85\x5d\x4f",
  36567. digest, digestSz), 0);
  36568. #endif
  36569. #ifndef WOLFSSL_NOSHA3_512
  36570. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_512(), digest, &digestSz)),
  36571. TEST_SUCCESS);
  36572. ExpectIntEQ(digestSz, 64);
  36573. ExpectIntEQ(XMEMCMP("\x3e\x77\xe3\x59\x42\x89\xed\xc3\xa4\x26\x3d\xa4\x75"
  36574. "\xd2\x84\x8c\xb2\xf3\x25\x04\x47\x61\xce\x1c\x42\x86"
  36575. "\xcd\xf4\x56\xaa\x2f\x84\xb1\x3b\x18\xed\xe6\xd6\x48"
  36576. "\x15\xb0\x29\xc5\x9d\x32\xef\xdd\x3e\x09\xf6\xed\x9e"
  36577. "\x70\xbc\x1c\x63\xf7\x3b\x3e\xe1\xdc\x84\x9c\x1c",
  36578. digest, digestSz), 0);
  36579. #endif
  36580. #endif
  36581. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || \
  36582. HAVE_FIPS_VERSION <= 2)
  36583. ExpectIntEQ((test_HMAC_CTX_helper(EVP_md5(), digest, &digestSz)),
  36584. TEST_SUCCESS);
  36585. ExpectIntEQ(digestSz, 16);
  36586. ExpectIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  36587. "\xE4\x98\xDD", digest, digestSz), 0);
  36588. #endif /* !NO_MD5 */
  36589. #endif
  36590. return EXPECT_RESULT();
  36591. }
  36592. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  36593. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  36594. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  36595. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  36596. {
  36597. EXPECT_DECLS;
  36598. static const unsigned char key[] = "simple test key";
  36599. HMAC_CTX* hmac = NULL;
  36600. ENGINE* e = NULL;
  36601. unsigned char hash[WC_MAX_DIGEST_SIZE];
  36602. unsigned int len;
  36603. ExpectNotNull(hmac = HMAC_CTX_new());
  36604. HMAC_CTX_init(hmac);
  36605. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  36606. ((! defined(HAVE_FIPS_VERSION)) || \
  36607. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  36608. /* Get size on object that hasn't had a digest set - MD5. */
  36609. ExpectIntEQ(HMAC_size(hmac), 16);
  36610. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 1);
  36611. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 1);
  36612. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 1);
  36613. #else
  36614. ExpectIntEQ(HMAC_size(hmac), BAD_FUNC_ARG);
  36615. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 0);
  36616. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 0);
  36617. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 0);
  36618. #endif
  36619. ExpectIntEQ(HMAC_Init_ex(NULL, (void*)key, (int)sizeof(key), md, e), 0);
  36620. ExpectIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e), 1);
  36621. /* re-using test key as data to hash */
  36622. ExpectIntEQ(HMAC_Update(NULL, key, (int)sizeof(key)), 0);
  36623. ExpectIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), 1);
  36624. ExpectIntEQ(HMAC_Update(hmac, key, 0), 1);
  36625. ExpectIntEQ(HMAC_Update(hmac, NULL, 0), 1);
  36626. ExpectIntEQ(HMAC_Update(hmac, NULL, (int)sizeof(key)), 1);
  36627. ExpectIntEQ(HMAC_Final(NULL, NULL, &len), 0);
  36628. ExpectIntEQ(HMAC_Final(hmac, NULL, &len), 0);
  36629. ExpectIntEQ(HMAC_Final(NULL, hash, &len), 0);
  36630. ExpectIntEQ(HMAC_Final(hmac, hash, &len), 1);
  36631. ExpectIntEQ(HMAC_Final(hmac, hash, NULL), 1);
  36632. ExpectIntEQ(len, md_len);
  36633. ExpectIntEQ(HMAC_size(NULL), 0);
  36634. ExpectIntEQ(HMAC_size(hmac), md_len);
  36635. ExpectStrEQ(HMAC_CTX_get_md(hmac), md);
  36636. HMAC_cleanup(NULL);
  36637. HMAC_cleanup(hmac);
  36638. HMAC_CTX_free(hmac);
  36639. len = 0;
  36640. ExpectNull(HMAC(NULL, key, (int)sizeof(key), NULL, 0, hash, &len));
  36641. ExpectNull(HMAC(md, NULL, (int)sizeof(key), NULL, 0, hash, &len));
  36642. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, 0, NULL, &len));
  36643. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  36644. ExpectIntEQ(len, md_len);
  36645. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, NULL));
  36646. /* With data. */
  36647. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, (int)sizeof(key), hash,
  36648. &len));
  36649. /* With NULL data. */
  36650. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, (int)sizeof(key), hash,
  36651. &len));
  36652. /* With zero length data. */
  36653. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, 0, hash, &len));
  36654. return EXPECT_RESULT();
  36655. }
  36656. #endif
  36657. static int test_wolfSSL_HMAC(void)
  36658. {
  36659. EXPECT_DECLS;
  36660. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  36661. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  36662. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  36663. #ifndef NO_SHA256
  36664. ExpectIntEQ(test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE),
  36665. TEST_SUCCESS);
  36666. #endif
  36667. #ifdef WOLFSSL_SHA224
  36668. ExpectIntEQ(test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE),
  36669. TEST_SUCCESS);
  36670. #endif
  36671. #ifdef WOLFSSL_SHA384
  36672. ExpectIntEQ(test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE),
  36673. TEST_SUCCESS);
  36674. #endif
  36675. #ifdef WOLFSSL_SHA512
  36676. ExpectIntEQ(test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE),
  36677. TEST_SUCCESS);
  36678. #endif
  36679. #ifdef WOLFSSL_SHA3
  36680. #ifndef WOLFSSL_NOSHA3_224
  36681. ExpectIntEQ(test_openssl_hmac(EVP_sha3_224(),
  36682. (int)WC_SHA3_224_DIGEST_SIZE), TEST_SUCCESS);
  36683. #endif
  36684. #ifndef WOLFSSL_NOSHA3_256
  36685. ExpectIntEQ(test_openssl_hmac(EVP_sha3_256(),
  36686. (int)WC_SHA3_256_DIGEST_SIZE), TEST_SUCCESS);
  36687. #endif
  36688. #ifndef WOLFSSL_NOSHA3_384
  36689. ExpectIntEQ(test_openssl_hmac(EVP_sha3_384(),
  36690. (int)WC_SHA3_384_DIGEST_SIZE), TEST_SUCCESS);
  36691. #endif
  36692. #ifndef WOLFSSL_NOSHA3_512
  36693. ExpectIntEQ(test_openssl_hmac(EVP_sha3_512(),
  36694. (int)WC_SHA3_512_DIGEST_SIZE), TEST_SUCCESS);
  36695. #endif
  36696. #endif
  36697. #ifndef NO_SHA
  36698. ExpectIntEQ(test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE),
  36699. TEST_SUCCESS);
  36700. #endif
  36701. #endif
  36702. return EXPECT_RESULT();
  36703. }
  36704. static int test_wolfSSL_CMAC(void)
  36705. {
  36706. EXPECT_DECLS;
  36707. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  36708. defined(WOLFSSL_AES_DIRECT)
  36709. int i;
  36710. byte key[AES_256_KEY_SIZE];
  36711. CMAC_CTX* cmacCtx = NULL;
  36712. byte out[AES_BLOCK_SIZE];
  36713. size_t outLen = AES_BLOCK_SIZE;
  36714. for (i=0; i < AES_256_KEY_SIZE; ++i) {
  36715. key[i] = i;
  36716. }
  36717. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  36718. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  36719. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  36720. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  36721. NULL), 1);
  36722. /* re-using test key as data to hash */
  36723. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  36724. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  36725. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 1);
  36726. ExpectIntEQ(outLen, AES_BLOCK_SIZE);
  36727. /* No Update works. */
  36728. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  36729. NULL), 1);
  36730. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  36731. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  36732. NULL), 1);
  36733. /* Test parameters with CMAC_Update. */
  36734. ExpectIntEQ(CMAC_Update(NULL, NULL, 0), 0);
  36735. ExpectIntEQ(CMAC_Update(NULL, key, 0), 0);
  36736. ExpectIntEQ(CMAC_Update(NULL, NULL, AES_128_KEY_SIZE), 0);
  36737. ExpectIntEQ(CMAC_Update(NULL, key, AES_128_KEY_SIZE), 0);
  36738. ExpectIntEQ(CMAC_Update(cmacCtx, key, 0), 1);
  36739. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  36740. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, AES_128_KEY_SIZE), 1);
  36741. /* Test parameters with CMAC_Final. */
  36742. ExpectIntEQ(CMAC_Final(NULL, NULL, NULL), 0);
  36743. ExpectIntEQ(CMAC_Final(NULL, out, NULL), 0);
  36744. ExpectIntEQ(CMAC_Final(NULL, NULL, &outLen), 0);
  36745. ExpectIntEQ(CMAC_Final(NULL, out, &outLen), 0);
  36746. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, NULL), 1);
  36747. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, &outLen), 1);
  36748. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  36749. CMAC_CTX_free(cmacCtx);
  36750. /* Test parameters with CMAC Init. */
  36751. cmacCtx = NULL;
  36752. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  36753. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  36754. ExpectIntEQ(CMAC_Init(NULL, NULL, 0, NULL, NULL), 0);
  36755. ExpectIntEQ(CMAC_Init(NULL, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  36756. NULL), 0);
  36757. ExpectIntEQ(CMAC_Init(cmacCtx, NULL, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  36758. NULL), 0);
  36759. /* give a key too small for the cipher, verify we get failure */
  36760. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_192_cbc(),
  36761. NULL), 0);
  36762. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, NULL, NULL), 0);
  36763. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128)
  36764. /* Only AES-CBC supported. */
  36765. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_gcm(),
  36766. NULL), 0);
  36767. #endif
  36768. CMAC_CTX_free(cmacCtx);
  36769. ExpectNull(CMAC_CTX_get0_cipher_ctx(NULL));
  36770. cmacCtx = NULL;
  36771. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  36772. /* No Init. */
  36773. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 0);
  36774. CMAC_CTX_free(cmacCtx);
  36775. /* Test AES-256-CBC */
  36776. cmacCtx = NULL;
  36777. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  36778. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_256_KEY_SIZE, EVP_aes_256_cbc(),
  36779. NULL), 1);
  36780. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  36781. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  36782. CMAC_CTX_free(cmacCtx);
  36783. /* Test AES-192-CBC */
  36784. cmacCtx = NULL;
  36785. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  36786. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  36787. NULL), 1);
  36788. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  36789. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  36790. CMAC_CTX_free(cmacCtx);
  36791. cmacCtx = NULL;
  36792. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  36793. CMAC_CTX_free(cmacCtx);
  36794. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  36795. return EXPECT_RESULT();
  36796. }
  36797. static int test_wolfSSL_DES(void)
  36798. {
  36799. EXPECT_DECLS;
  36800. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  36801. const_DES_cblock myDes;
  36802. DES_cblock iv;
  36803. DES_key_schedule key;
  36804. word32 i;
  36805. DES_LONG dl;
  36806. unsigned char msg[] = "hello wolfssl";
  36807. unsigned char weakKey[][8] = {
  36808. { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
  36809. { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
  36810. { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
  36811. { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E }
  36812. };
  36813. unsigned char semiWeakKey[][8] = {
  36814. { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
  36815. { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
  36816. { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
  36817. { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
  36818. { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
  36819. { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
  36820. { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
  36821. { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
  36822. { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
  36823. { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
  36824. { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
  36825. { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
  36826. };
  36827. DES_check_key(1);
  36828. DES_set_key(&myDes, &key);
  36829. /* check, check of odd parity */
  36830. XMEMSET(myDes, 4, sizeof(const_DES_cblock));
  36831. myDes[0] = 6; /* set even parity */
  36832. XMEMSET(key, 5, sizeof(DES_key_schedule));
  36833. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -1);
  36834. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  36835. ExpectIntEQ(DES_set_key_checked(NULL, NULL), -2);
  36836. ExpectIntEQ(DES_set_key_checked(&myDes, NULL), -2);
  36837. ExpectIntEQ(DES_set_key_checked(NULL, &key), -2);
  36838. /* set odd parity for success case */
  36839. DES_set_odd_parity(&myDes);
  36840. ExpectIntEQ(DES_check_key_parity(&myDes), 1);
  36841. fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
  36842. myDes[3]);
  36843. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  36844. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  36845. ExpectIntEQ(key[i], myDes[i]);
  36846. }
  36847. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  36848. /* check weak key */
  36849. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  36850. XMEMSET(key, 5, sizeof(DES_key_schedule));
  36851. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -2);
  36852. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  36853. DES_set_key_unchecked(NULL, NULL);
  36854. DES_set_key_unchecked(&myDes, NULL);
  36855. DES_set_key_unchecked(NULL, &key);
  36856. /* compare arrays, should be the same */
  36857. /* now do unchecked copy of a weak key over */
  36858. DES_set_key_unchecked(&myDes, &key);
  36859. /* compare arrays, should be the same */
  36860. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  36861. ExpectIntEQ(key[i], myDes[i]);
  36862. }
  36863. ExpectIntEQ(DES_is_weak_key(&myDes), 1);
  36864. myDes[7] = 2;
  36865. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  36866. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  36867. ExpectIntEQ(DES_is_weak_key(NULL), 1);
  36868. /* Test all weak keys. */
  36869. for (i = 0; i < sizeof(weakKey) / sizeof(*weakKey); i++) {
  36870. ExpectIntEQ(DES_set_key_checked(&weakKey[i], &key), -2);
  36871. }
  36872. /* Test all semi-weak keys. */
  36873. for (i = 0; i < sizeof(semiWeakKey) / sizeof(*semiWeakKey); i++) {
  36874. ExpectIntEQ(DES_set_key_checked(&semiWeakKey[i], &key), -2);
  36875. }
  36876. /* check DES_key_sched API */
  36877. XMEMSET(key, 1, sizeof(DES_key_schedule));
  36878. ExpectIntEQ(DES_key_sched(&myDes, NULL), 0);
  36879. ExpectIntEQ(DES_key_sched(NULL, &key), 0);
  36880. ExpectIntEQ(DES_key_sched(&myDes, &key), 0);
  36881. /* compare arrays, should be the same */
  36882. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  36883. ExpectIntEQ(key[i], myDes[i]);
  36884. }
  36885. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, NULL)), 0);
  36886. ExpectIntEQ((DES_cbc_cksum(msg, NULL, 0, NULL, NULL)), 0);
  36887. ExpectIntEQ((DES_cbc_cksum(NULL, &key, 0, NULL, NULL)), 0);
  36888. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, &myDes, NULL)), 0);
  36889. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, &iv)), 0);
  36890. ExpectIntEQ((DES_cbc_cksum(NULL, &key, sizeof(msg), &myDes, &iv)), 0);
  36891. ExpectIntEQ((DES_cbc_cksum(msg, NULL, sizeof(msg), &myDes, &iv)), 0);
  36892. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), NULL, &iv)), 0);
  36893. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, NULL)), 0);
  36894. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  36895. * DES_cbc_encrypt on the input */
  36896. XMEMSET(iv, 0, sizeof(DES_cblock));
  36897. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  36898. ExpectIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  36899. ExpectIntEQ(dl, 480052723);
  36900. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  36901. return EXPECT_RESULT();
  36902. }
  36903. static int test_wolfSSL_DES_ncbc(void)
  36904. {
  36905. EXPECT_DECLS;
  36906. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  36907. const_DES_cblock myDes;
  36908. DES_cblock iv = {1};
  36909. DES_key_schedule key = {0};
  36910. unsigned char msg[] = "hello wolfssl";
  36911. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  36912. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  36913. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  36914. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  36915. /* partial block test */
  36916. DES_set_key(&key, &myDes);
  36917. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  36918. ExpectIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  36919. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  36920. DES_set_key(&key, &myDes);
  36921. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  36922. *((byte*)&iv) = 1;
  36923. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  36924. ExpectIntEQ(XMEMCMP(msg, pln, 3), 0);
  36925. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  36926. /* full block test */
  36927. DES_set_key(&key, &myDes);
  36928. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  36929. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  36930. *((byte*)&iv) = 1;
  36931. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  36932. ExpectIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  36933. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  36934. DES_set_key(&key, &myDes);
  36935. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  36936. *((byte*)&iv) = 1;
  36937. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  36938. ExpectIntEQ(XMEMCMP(msg, pln, 8), 0);
  36939. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  36940. #endif
  36941. return EXPECT_RESULT();
  36942. }
  36943. static int test_wolfSSL_DES_ecb_encrypt(void)
  36944. {
  36945. EXPECT_DECLS;
  36946. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  36947. WOLFSSL_DES_cblock input1, input2, output1, output2, back1, back2;
  36948. WOLFSSL_DES_key_schedule key;
  36949. XMEMCPY(key, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  36950. XMEMCPY(input1, "Iamhuman", sizeof(WOLFSSL_DES_cblock));
  36951. XMEMCPY(input2, "Whoisit?", sizeof(WOLFSSL_DES_cblock));
  36952. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  36953. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  36954. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  36955. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  36956. wolfSSL_DES_ecb_encrypt(NULL, NULL, NULL, DES_ENCRYPT);
  36957. wolfSSL_DES_ecb_encrypt(&input1, NULL, NULL, DES_ENCRYPT);
  36958. wolfSSL_DES_ecb_encrypt(NULL, &output1, NULL, DES_ENCRYPT);
  36959. wolfSSL_DES_ecb_encrypt(NULL, NULL, &key, DES_ENCRYPT);
  36960. wolfSSL_DES_ecb_encrypt(&input1, &output1, NULL, DES_ENCRYPT);
  36961. wolfSSL_DES_ecb_encrypt(&input1, NULL, &key, DES_ENCRYPT);
  36962. wolfSSL_DES_ecb_encrypt(NULL, &output1, &key, DES_ENCRYPT);
  36963. /* Encrypt messages */
  36964. wolfSSL_DES_ecb_encrypt(&input1, &output1, &key, DES_ENCRYPT);
  36965. wolfSSL_DES_ecb_encrypt(&input2, &output2, &key, DES_ENCRYPT);
  36966. {
  36967. /* Decrypt messages */
  36968. int ret1 = 0;
  36969. int ret2 = 0;
  36970. wolfSSL_DES_ecb_encrypt(&output1, &back1, &key, DES_DECRYPT);
  36971. ExpectIntEQ(ret1 = XMEMCMP((unsigned char *)back1,
  36972. (unsigned char *)input1, sizeof(WOLFSSL_DES_cblock)), 0);
  36973. wolfSSL_DES_ecb_encrypt(&output2, &back2, &key, DES_DECRYPT);
  36974. ExpectIntEQ(ret2 = XMEMCMP((unsigned char *)back2,
  36975. (unsigned char *)input2, sizeof(WOLFSSL_DES_cblock)), 0);
  36976. }
  36977. #endif
  36978. return EXPECT_RESULT();
  36979. }
  36980. static int test_wolfSSL_DES_ede3_cbc_encrypt(void)
  36981. {
  36982. EXPECT_DECLS;
  36983. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  36984. unsigned char input1[8], input2[8];
  36985. unsigned char output1[8], output2[8];
  36986. unsigned char back1[8], back2[8];
  36987. WOLFSSL_DES_cblock iv1, iv2;
  36988. WOLFSSL_DES_key_schedule key1, key2, key3;
  36989. int i;
  36990. XMEMCPY(key1, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  36991. XMEMCPY(key2, "23456781", sizeof(WOLFSSL_DES_key_schedule));
  36992. XMEMCPY(key3, "34567823", sizeof(WOLFSSL_DES_key_schedule));
  36993. XMEMCPY(input1, "Iamhuman", sizeof(input1));
  36994. XMEMCPY(input2, "Whoisit?", sizeof(input2));
  36995. XMEMSET(output1, 0, sizeof(output1));
  36996. XMEMSET(output2, 0, sizeof(output2));
  36997. XMEMSET(back1, 0, sizeof(back1));
  36998. XMEMSET(back2, 0, sizeof(back2));
  36999. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  37000. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  37001. /* Encrypt messages */
  37002. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, 8, &key1, &key2, &key3, &iv1,
  37003. DES_ENCRYPT);
  37004. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, 8, &key1, &key2, &key3, &iv2,
  37005. DES_ENCRYPT);
  37006. {
  37007. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  37008. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  37009. /* Decrypt messages */
  37010. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, 8, &key1, &key2, &key3,
  37011. &iv1, DES_DECRYPT);
  37012. ExpectIntEQ(XMEMCMP(back1, input1, sizeof(input1)), 0);
  37013. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, 8, &key1, &key2, &key3,
  37014. &iv2, DES_DECRYPT);
  37015. ExpectIntEQ(XMEMCMP(back2, input2, sizeof(input2)), 0);
  37016. }
  37017. for (i = 0; i < 8; i++) {
  37018. XMEMSET(output1, 0, sizeof(output1));
  37019. XMEMSET(output2, 0, sizeof(output2));
  37020. XMEMSET(back1, 0, sizeof(back1));
  37021. XMEMSET(back2, 0, sizeof(back2));
  37022. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  37023. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  37024. /* Encrypt partial messages */
  37025. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, i, &key1, &key2, &key3,
  37026. &iv1, DES_ENCRYPT);
  37027. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, i, &key1, &key2, &key3,
  37028. &iv2, DES_ENCRYPT);
  37029. {
  37030. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  37031. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  37032. /* Decrypt messages */
  37033. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, i, &key1, &key2,
  37034. &key3, &iv1, DES_DECRYPT);
  37035. ExpectIntEQ(XMEMCMP(back1, input1, i), 0);
  37036. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, i, &key1, &key2,
  37037. &key3, &iv2, DES_DECRYPT);
  37038. ExpectIntEQ(XMEMCMP(back2, input2, i), 0);
  37039. }
  37040. }
  37041. #endif
  37042. return EXPECT_RESULT();
  37043. }
  37044. static int test_wolfSSL_AES_encrypt(void)
  37045. {
  37046. EXPECT_DECLS;
  37047. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB)
  37048. AES_KEY enc;
  37049. AES_KEY dec;
  37050. const byte msg[] = {
  37051. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  37052. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  37053. };
  37054. const byte exp[] = {
  37055. 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c,
  37056. 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8,
  37057. };
  37058. const byte key[] = {
  37059. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  37060. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  37061. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  37062. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  37063. };
  37064. byte eout[sizeof(msg)];
  37065. byte dout[sizeof(msg)];
  37066. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &enc), 0);
  37067. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &dec), 0);
  37068. wolfSSL_AES_encrypt(NULL, NULL, NULL);
  37069. wolfSSL_AES_encrypt(msg, NULL, NULL);
  37070. wolfSSL_AES_encrypt(NULL, eout, NULL);
  37071. wolfSSL_AES_encrypt(NULL, NULL, &enc);
  37072. wolfSSL_AES_encrypt(msg, eout, NULL);
  37073. wolfSSL_AES_encrypt(msg, NULL, &enc);
  37074. wolfSSL_AES_encrypt(NULL, eout, &enc);
  37075. wolfSSL_AES_decrypt(NULL, NULL, NULL);
  37076. wolfSSL_AES_decrypt(eout, NULL, NULL);
  37077. wolfSSL_AES_decrypt(NULL, dout, NULL);
  37078. wolfSSL_AES_decrypt(NULL, NULL, &dec);
  37079. wolfSSL_AES_decrypt(eout, dout, NULL);
  37080. wolfSSL_AES_decrypt(eout, NULL, &dec);
  37081. wolfSSL_AES_decrypt(NULL, dout, &dec);
  37082. wolfSSL_AES_encrypt(msg, eout, &enc);
  37083. ExpectIntEQ(XMEMCMP(eout, exp, AES_BLOCK_SIZE), 0);
  37084. wolfSSL_AES_decrypt(eout, dout, &dec);
  37085. ExpectIntEQ(XMEMCMP(dout, msg, AES_BLOCK_SIZE), 0);
  37086. #endif
  37087. return EXPECT_RESULT();
  37088. }
  37089. static int test_wolfSSL_AES_ecb_encrypt(void)
  37090. {
  37091. EXPECT_DECLS;
  37092. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB)
  37093. AES_KEY aes;
  37094. const byte msg[] =
  37095. {
  37096. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  37097. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  37098. };
  37099. const byte verify[] =
  37100. {
  37101. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  37102. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  37103. };
  37104. const byte key[] =
  37105. {
  37106. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  37107. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  37108. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  37109. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  37110. };
  37111. byte out[AES_BLOCK_SIZE];
  37112. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  37113. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37114. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  37115. ExpectIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  37116. #ifdef HAVE_AES_DECRYPT
  37117. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  37118. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37119. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  37120. ExpectIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  37121. #endif
  37122. /* test bad arguments */
  37123. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  37124. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  37125. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  37126. #endif
  37127. return EXPECT_RESULT();
  37128. }
  37129. static int test_wolfSSL_AES_cbc_encrypt(void)
  37130. {
  37131. EXPECT_DECLS;
  37132. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA)
  37133. AES_KEY aes;
  37134. AES_KEY* aesN = NULL;
  37135. size_t len = 0;
  37136. size_t lenB = 0;
  37137. int keySz0 = 0;
  37138. int keySzN = -1;
  37139. byte out[AES_BLOCK_SIZE] = {0};
  37140. byte* outN = NULL;
  37141. /* Test vectors retrieved from:
  37142. * <begin URL>
  37143. * https://csrc.nist.gov/
  37144. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  37145. * documents/aes/KAT_AES.zip
  37146. * </end URL>
  37147. */
  37148. const byte* pt128N = NULL;
  37149. byte* key128N = NULL;
  37150. byte* iv128N = NULL;
  37151. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  37152. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  37153. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  37154. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  37155. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  37156. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  37157. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  37158. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  37159. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  37160. len = sizeof(pt128);
  37161. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  37162. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  37163. ExpectIntNE(XMEMCMP(b, g, h), i)
  37164. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  37165. /* Stressing wolfSSL_AES_cbc_encrypt() */
  37166. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  37167. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  37168. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  37169. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  37170. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  37171. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  37172. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  37173. /* Stressing wolfSSL_AES_set_encrypt_key */
  37174. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  37175. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  37176. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  37177. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  37178. /* Stressing wolfSSL_AES_set_decrypt_key */
  37179. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  37180. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  37181. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  37182. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  37183. #ifdef WOLFSSL_AES_128
  37184. /* wolfSSL_AES_cbc_encrypt() 128-bit */
  37185. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37186. RESET_IV(iv128tmp, iv128);
  37187. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  37188. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  37189. ExpectIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  37190. wc_AesFree((Aes*)&aes);
  37191. #ifdef HAVE_AES_DECRYPT
  37192. /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
  37193. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37194. RESET_IV(iv128tmp, iv128);
  37195. len = sizeof(ct128);
  37196. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  37197. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  37198. ExpectIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  37199. wc_AesFree((Aes*)&aes);
  37200. #endif
  37201. #endif /* WOLFSSL_AES_128 */
  37202. #ifdef WOLFSSL_AES_192
  37203. {
  37204. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  37205. * Appendix F.2.3 */
  37206. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  37207. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  37208. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  37209. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  37210. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  37211. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  37212. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  37213. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  37214. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  37215. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  37216. len = sizeof(pt192);
  37217. /* wolfSSL_AES_cbc_encrypt() 192-bit */
  37218. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37219. RESET_IV(iv192tmp, iv192);
  37220. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  37221. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  37222. ExpectIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  37223. wc_AesFree((Aes*)&aes);
  37224. #ifdef HAVE_AES_DECRYPT
  37225. /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
  37226. len = sizeof(ct192);
  37227. RESET_IV(iv192tmp, iv192);
  37228. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37229. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  37230. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  37231. ExpectIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  37232. wc_AesFree((Aes*)&aes);
  37233. #endif
  37234. }
  37235. #endif /* WOLFSSL_AES_192 */
  37236. #ifdef WOLFSSL_AES_256
  37237. {
  37238. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  37239. * Appendix F.2.5 */
  37240. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  37241. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  37242. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  37243. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  37244. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  37245. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  37246. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  37247. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  37248. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  37249. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  37250. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  37251. len = sizeof(pt256);
  37252. /* wolfSSL_AES_cbc_encrypt() 256-bit */
  37253. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37254. RESET_IV(iv256tmp, iv256);
  37255. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37256. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  37257. ExpectIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  37258. wc_AesFree((Aes*)&aes);
  37259. #ifdef HAVE_AES_DECRYPT
  37260. /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
  37261. len = sizeof(ct256);
  37262. RESET_IV(iv256tmp, iv256);
  37263. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37264. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37265. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  37266. ExpectIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  37267. wc_AesFree((Aes*)&aes);
  37268. #endif
  37269. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && \
  37270. !defined(HAVE_SELFTEST)
  37271. {
  37272. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  37273. byte wrapPlain[sizeof(key256)] = { 0 };
  37274. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  37275. /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
  37276. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37277. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  37278. 15), WOLFSSL_FAILURE);
  37279. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  37280. sizeof(key256)), sizeof(wrapCipher));
  37281. wc_AesFree((Aes*)&aes);
  37282. /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
  37283. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37284. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  37285. 23), WOLFSSL_FAILURE);
  37286. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  37287. sizeof(wrapCipher)), sizeof(wrapPlain));
  37288. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  37289. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  37290. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  37291. wc_AesFree((Aes*)&aes);
  37292. /* wolfSSL_AES_wrap_key() 256-bit custom iv */
  37293. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37294. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  37295. sizeof(key256)), sizeof(wrapCipher));
  37296. wc_AesFree((Aes*)&aes);
  37297. /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
  37298. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37299. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  37300. sizeof(wrapCipher)), sizeof(wrapPlain));
  37301. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  37302. wc_AesFree((Aes*)&aes);
  37303. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, NULL, 0), 0);
  37304. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, NULL, NULL, 0), 0);
  37305. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  37306. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, wrapCipher, NULL, 0), 0);
  37307. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, key256, 0), 0);
  37308. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, wrapCipher, key256, 0), 0);
  37309. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256, 0), 0);
  37310. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, NULL, key256, 0), 0);
  37311. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, NULL, 0), 0);
  37312. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, NULL, 0), 0);
  37313. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, NULL, NULL, 0), 0);
  37314. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  37315. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, wrapPlain, NULL, 0), 0);
  37316. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, wrapCipher, 0), 0);
  37317. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, wrapPlain, wrapCipher, 0),
  37318. 0);
  37319. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher, 0),
  37320. 0);
  37321. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, NULL, wrapCipher, 0), 0);
  37322. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapPlain, NULL, 0), 0);
  37323. }
  37324. #endif /* HAVE_AES_KEYWRAP */
  37325. }
  37326. #endif /* WOLFSSL_AES_256 */
  37327. #endif
  37328. return EXPECT_RESULT();
  37329. }
  37330. static int test_wolfSSL_AES_cfb128_encrypt(void)
  37331. {
  37332. EXPECT_DECLS;
  37333. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(WOLFSSL_AES_CFB)
  37334. AES_KEY aesEnc;
  37335. AES_KEY aesDec;
  37336. const byte msg[] = {
  37337. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  37338. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  37339. };
  37340. const byte exp[] = {
  37341. 0x16, 0xc9, 0x90, 0x6c, 0x04, 0x0c, 0xd1, 0x2f,
  37342. 0x84, 0x7b, 0x18, 0xed, 0xed, 0x6a, 0xb5, 0xfd
  37343. };
  37344. const byte key[] = {
  37345. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  37346. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  37347. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  37348. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  37349. };
  37350. const byte ivData[] = {
  37351. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  37352. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  37353. };
  37354. byte out[AES_BLOCK_SIZE];
  37355. byte iv[AES_BLOCK_SIZE];
  37356. word32 i;
  37357. int num;
  37358. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  37359. XMEMCPY(iv, ivData, sizeof(iv));
  37360. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37361. AES_cfb128_encrypt(msg, out, sizeof(msg), &aesEnc, iv, NULL, AES_ENCRYPT);
  37362. ExpectIntEQ(XMEMCMP(out, exp, sizeof(msg)), 0);
  37363. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  37364. #ifdef HAVE_AES_DECRYPT
  37365. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  37366. XMEMCPY(iv, ivData, sizeof(iv));
  37367. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37368. AES_cfb128_encrypt(exp, out, sizeof(msg), &aesDec, iv, NULL, AES_DECRYPT);
  37369. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  37370. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  37371. #endif
  37372. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(msg)); i++) {
  37373. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  37374. XMEMCPY(iv, ivData, sizeof(iv));
  37375. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37376. AES_cfb128_encrypt(msg, out, i, &aesEnc, iv, &num, AES_ENCRYPT);
  37377. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  37378. ExpectIntEQ(XMEMCMP(out, exp, i), 0);
  37379. if (i == 0) {
  37380. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  37381. }
  37382. else {
  37383. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  37384. }
  37385. #ifdef HAVE_AES_DECRYPT
  37386. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  37387. XMEMCPY(iv, ivData, sizeof(iv));
  37388. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37389. AES_cfb128_encrypt(exp, out, i, &aesDec, iv, &num, AES_DECRYPT);
  37390. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  37391. ExpectIntEQ(XMEMCMP(out, msg, i), 0);
  37392. if (i == 0) {
  37393. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  37394. }
  37395. else {
  37396. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  37397. }
  37398. #endif
  37399. }
  37400. if (EXPECT_SUCCESS()) {
  37401. /* test bad arguments */
  37402. AES_cfb128_encrypt(NULL, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  37403. AES_cfb128_encrypt(msg, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  37404. AES_cfb128_encrypt(NULL, out, 0, NULL, NULL, NULL, AES_DECRYPT);
  37405. AES_cfb128_encrypt(NULL, NULL, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  37406. AES_cfb128_encrypt(NULL, NULL, 0, NULL, iv, NULL, AES_DECRYPT);
  37407. AES_cfb128_encrypt(NULL, out, 0, &aesDec, iv, NULL, AES_DECRYPT);
  37408. AES_cfb128_encrypt(msg, NULL, 0, &aesDec, iv, NULL, AES_DECRYPT);
  37409. AES_cfb128_encrypt(msg, out, 0, NULL, iv, NULL, AES_DECRYPT);
  37410. AES_cfb128_encrypt(msg, out, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  37411. }
  37412. #endif
  37413. return EXPECT_RESULT();
  37414. }
  37415. static int test_wolfSSL_CRYPTO_cts128(void)
  37416. {
  37417. EXPECT_DECLS;
  37418. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  37419. defined(HAVE_CTS)
  37420. byte tmp[64]; /* Largest vector size */
  37421. /* Test vectors taken form RFC3962 Appendix B */
  37422. const testVector vects[] = {
  37423. {
  37424. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37425. "\x20",
  37426. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  37427. "\x97",
  37428. 17, 17
  37429. },
  37430. {
  37431. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37432. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  37433. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  37434. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  37435. 31, 31
  37436. },
  37437. {
  37438. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37439. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  37440. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  37441. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  37442. 32, 32
  37443. },
  37444. {
  37445. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37446. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  37447. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  37448. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  37449. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  37450. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  37451. 47, 47
  37452. },
  37453. {
  37454. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37455. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  37456. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  37457. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  37458. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  37459. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  37460. 48, 48
  37461. },
  37462. {
  37463. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37464. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  37465. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  37466. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  37467. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  37468. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  37469. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  37470. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  37471. 64, 64
  37472. }
  37473. };
  37474. byte keyBytes[AES_128_KEY_SIZE] = {
  37475. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  37476. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  37477. };
  37478. size_t i;
  37479. AES_KEY encKey;
  37480. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  37481. XMEMSET(tmp, 0, sizeof(tmp));
  37482. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  37483. AES_KEY decKey;
  37484. ExpectIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  37485. &encKey), 0);
  37486. ExpectIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  37487. &decKey), 0);
  37488. XMEMSET(iv, 0, sizeof(iv));
  37489. ExpectIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  37490. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  37491. vects[i].outLen);
  37492. ExpectIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  37493. XMEMSET(iv, 0, sizeof(iv));
  37494. ExpectIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  37495. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  37496. vects[i].inLen);
  37497. ExpectIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  37498. }
  37499. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  37500. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  37501. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  37502. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  37503. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  37504. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL,
  37505. (cbc128_f)AES_cbc_encrypt), 0);
  37506. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, &encKey, iv,
  37507. (cbc128_f)AES_cbc_encrypt), 0);
  37508. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, &encKey, iv,
  37509. (cbc128_f)AES_cbc_encrypt), 0);
  37510. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, NULL, iv,
  37511. (cbc128_f)AES_cbc_encrypt), 0);
  37512. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, NULL,
  37513. (cbc128_f)AES_cbc_encrypt), 0);
  37514. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  37515. /* Length too small. */
  37516. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 0, &encKey, iv,
  37517. (cbc128_f)AES_cbc_encrypt), 0);
  37518. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  37519. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  37520. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  37521. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  37522. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  37523. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL,
  37524. (cbc128_f)AES_cbc_encrypt), 0);
  37525. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, &encKey, iv,
  37526. (cbc128_f)AES_cbc_encrypt), 0);
  37527. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, &encKey, iv,
  37528. (cbc128_f)AES_cbc_encrypt), 0);
  37529. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, NULL, iv,
  37530. (cbc128_f)AES_cbc_encrypt), 0);
  37531. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, NULL,
  37532. (cbc128_f)AES_cbc_encrypt), 0);
  37533. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  37534. /* Length too small. */
  37535. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 0, &encKey, iv,
  37536. (cbc128_f)AES_cbc_encrypt), 0);
  37537. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  37538. return EXPECT_RESULT();
  37539. }
  37540. static int test_wolfSSL_RC4(void)
  37541. {
  37542. EXPECT_DECLS;
  37543. #if !defined(NO_RC4) && defined(OPENSSL_EXTRA)
  37544. WOLFSSL_RC4_KEY rc4Key;
  37545. unsigned char key[] = {
  37546. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  37547. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  37548. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  37549. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  37550. };
  37551. unsigned char data[] = {
  37552. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  37553. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  37554. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  37555. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  37556. };
  37557. unsigned char enc[sizeof(data)];
  37558. unsigned char dec[sizeof(data)];
  37559. word32 i;
  37560. word32 j;
  37561. wolfSSL_RC4_set_key(NULL, -1, NULL);
  37562. wolfSSL_RC4_set_key(&rc4Key, -1, NULL);
  37563. wolfSSL_RC4_set_key(NULL, 0, NULL);
  37564. wolfSSL_RC4_set_key(NULL, -1, key);
  37565. wolfSSL_RC4_set_key(&rc4Key, 0, NULL);
  37566. wolfSSL_RC4_set_key(&rc4Key, -1, key);
  37567. wolfSSL_RC4_set_key(NULL, 0, key);
  37568. wolfSSL_RC4(NULL, 0, NULL, NULL);
  37569. wolfSSL_RC4(&rc4Key, 0, NULL, NULL);
  37570. wolfSSL_RC4(NULL, 0, data, NULL);
  37571. wolfSSL_RC4(NULL, 0, NULL, enc);
  37572. wolfSSL_RC4(&rc4Key, 0, data, NULL);
  37573. wolfSSL_RC4(&rc4Key, 0, NULL, enc);
  37574. wolfSSL_RC4(NULL, 0, data, enc);
  37575. ExpectIntEQ(1, 1);
  37576. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(key)); i++) {
  37577. for (j = 0; EXPECT_SUCCESS() && (j <= sizeof(data)); j++) {
  37578. XMEMSET(enc, 0, sizeof(enc));
  37579. XMEMSET(dec, 0, sizeof(dec));
  37580. /* Encrypt */
  37581. wolfSSL_RC4_set_key(&rc4Key, i, key);
  37582. wolfSSL_RC4(&rc4Key, j, data, enc);
  37583. /* Decrypt */
  37584. wolfSSL_RC4_set_key(&rc4Key, i, key);
  37585. wolfSSL_RC4(&rc4Key, j, enc, dec);
  37586. ExpectIntEQ(XMEMCMP(dec, data, j), 0);
  37587. }
  37588. }
  37589. #endif
  37590. return EXPECT_RESULT();
  37591. }
  37592. static int test_wolfSSL_OBJ(void)
  37593. {
  37594. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  37595. * mode
  37596. */
  37597. EXPECT_DECLS;
  37598. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  37599. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  37600. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  37601. ASN1_OBJECT *obj = NULL;
  37602. ASN1_OBJECT *obj2 = NULL;
  37603. char buf[50];
  37604. XFILE fp = XBADFILE;
  37605. X509 *x509 = NULL;
  37606. X509_NAME *x509Name = NULL;
  37607. X509_NAME_ENTRY *x509NameEntry = NULL;
  37608. ASN1_OBJECT *asn1Name = NULL;
  37609. int numNames = 0;
  37610. BIO *bio = NULL;
  37611. int nid;
  37612. int i, j;
  37613. const char *f[] = {
  37614. #ifndef NO_RSA
  37615. "./certs/ca-cert.der",
  37616. #endif
  37617. #ifdef HAVE_ECC
  37618. "./certs/ca-ecc-cert.der",
  37619. "./certs/ca-ecc384-cert.der",
  37620. #endif
  37621. NULL};
  37622. ASN1_OBJECT *field_name_obj = NULL;
  37623. int lastpos = -1;
  37624. int tmp = -1;
  37625. ASN1_STRING *asn1 = NULL;
  37626. unsigned char *buf_dyn = NULL;
  37627. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  37628. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  37629. ExpectIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  37630. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  37631. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  37632. ASN1_OBJECT_free(obj);
  37633. obj = NULL;
  37634. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  37635. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256);
  37636. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  37637. #ifdef WOLFSSL_CERT_EXT
  37638. ExpectIntEQ(OBJ_txt2nid(buf), NID_sha256);
  37639. #endif
  37640. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  37641. ExpectNotNull(obj2 = OBJ_dup(obj));
  37642. ExpectIntEQ(OBJ_cmp(obj, obj2), 0);
  37643. ASN1_OBJECT_free(obj);
  37644. obj = NULL;
  37645. ASN1_OBJECT_free(obj2);
  37646. obj2 = NULL;
  37647. for (i = 0; f[i] != NULL; i++)
  37648. {
  37649. ExpectTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  37650. ExpectNotNull(x509 = d2i_X509_fp(fp, NULL));
  37651. if (fp != XBADFILE) {
  37652. XFCLOSE(fp);
  37653. fp = XBADFILE;
  37654. }
  37655. ExpectNotNull(x509Name = X509_get_issuer_name(x509));
  37656. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  37657. /* Get the Common Name by using OBJ_txt2obj */
  37658. ExpectNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  37659. do
  37660. {
  37661. lastpos = tmp;
  37662. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  37663. } while (tmp > -1);
  37664. ExpectIntNE(lastpos, -1);
  37665. ASN1_OBJECT_free(field_name_obj);
  37666. field_name_obj = NULL;
  37667. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  37668. ExpectNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  37669. ExpectIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  37670. /*
  37671. * All Common Names should be www.wolfssl.com
  37672. * This makes testing easier as we can test for the expected value.
  37673. */
  37674. ExpectStrEQ((char*)buf_dyn, "www.wolfssl.com");
  37675. OPENSSL_free(buf_dyn);
  37676. buf_dyn = NULL;
  37677. bio = BIO_new(BIO_s_mem());
  37678. ExpectTrue(bio != NULL);
  37679. for (j = 0; j < numNames; j++)
  37680. {
  37681. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  37682. ExpectNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  37683. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  37684. }
  37685. BIO_free(bio);
  37686. bio = NULL;
  37687. X509_free(x509);
  37688. x509 = NULL;
  37689. }
  37690. #ifdef HAVE_PKCS12
  37691. {
  37692. PKCS12 *p12 = NULL;
  37693. int boolRet;
  37694. EVP_PKEY *pkey = NULL;
  37695. const char *p12_f[] = {
  37696. #if !defined(NO_DES3) && !defined(NO_RSA)
  37697. "./certs/test-servercert.p12",
  37698. #endif
  37699. NULL};
  37700. for (i = 0; p12_f[i] != NULL; i++)
  37701. {
  37702. ExpectTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  37703. ExpectNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  37704. if (fp != XBADFILE) {
  37705. XFCLOSE(fp);
  37706. fp = XBADFILE;
  37707. }
  37708. ExpectTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  37709. &pkey, &x509, NULL)) > 0);
  37710. wc_PKCS12_free(p12);
  37711. p12 = NULL;
  37712. EVP_PKEY_free(pkey);
  37713. x509Name = X509_get_issuer_name(x509);
  37714. ExpectNotNull(x509Name);
  37715. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  37716. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  37717. for (j = 0; j < numNames; j++)
  37718. {
  37719. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  37720. ExpectNotNull(asn1Name =
  37721. X509_NAME_ENTRY_get_object(x509NameEntry));
  37722. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  37723. }
  37724. BIO_free(bio);
  37725. bio = NULL;
  37726. X509_free(x509);
  37727. x509 = NULL;
  37728. }
  37729. }
  37730. #endif /* HAVE_PKCS12 */
  37731. #endif
  37732. return EXPECT_RESULT();
  37733. }
  37734. static int test_wolfSSL_OBJ_cmp(void)
  37735. {
  37736. EXPECT_DECLS;
  37737. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  37738. ASN1_OBJECT *obj = NULL;
  37739. ASN1_OBJECT *obj2 = NULL;
  37740. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  37741. ExpectNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  37742. ExpectIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  37743. ExpectIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  37744. ExpectIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  37745. ExpectIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  37746. ExpectIntEQ(OBJ_cmp(obj, obj), 0);
  37747. ExpectIntEQ(OBJ_cmp(obj2, obj2), 0);
  37748. ASN1_OBJECT_free(obj);
  37749. ASN1_OBJECT_free(obj2);
  37750. #endif
  37751. return EXPECT_RESULT();
  37752. }
  37753. static int test_wolfSSL_OBJ_txt2nid(void)
  37754. {
  37755. EXPECT_DECLS;
  37756. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  37757. defined(WOLFSSL_APACHE_HTTPD)
  37758. int i;
  37759. static const struct {
  37760. const char* sn;
  37761. const char* ln;
  37762. const char* oid;
  37763. int nid;
  37764. } testVals[] = {
  37765. #ifdef WOLFSSL_APACHE_HTTPD
  37766. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  37767. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  37768. NID_id_on_dnsSRV },
  37769. { "msUPN", "Microsoft User Principal Name",
  37770. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  37771. #endif
  37772. { NULL, NULL, NULL, NID_undef }
  37773. };
  37774. /* Invalid cases */
  37775. ExpectIntEQ(OBJ_txt2nid(NULL), NID_undef);
  37776. ExpectIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  37777. /* Valid cases */
  37778. for (i = 0; testVals[i].sn != NULL; i++) {
  37779. ExpectIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  37780. ExpectIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  37781. ExpectIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  37782. }
  37783. #endif
  37784. return EXPECT_RESULT();
  37785. }
  37786. static int test_wolfSSL_OBJ_txt2obj(void)
  37787. {
  37788. EXPECT_DECLS;
  37789. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  37790. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  37791. int i;
  37792. char buf[50];
  37793. ASN1_OBJECT* obj = NULL;
  37794. static const struct {
  37795. const char* oidStr;
  37796. const char* sn;
  37797. const char* ln;
  37798. } objs_list[] = {
  37799. #if defined(WOLFSSL_APACHE_HTTPD)
  37800. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  37801. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  37802. #endif
  37803. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  37804. { NULL, NULL, NULL }
  37805. };
  37806. static const struct {
  37807. const char* numeric;
  37808. const char* name;
  37809. } objs_named[] = {
  37810. /* In dictionary but not in normal list. */
  37811. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  37812. /* Made up OID. */
  37813. { "1.3.5.7", "1.3.5.7" },
  37814. { NULL, NULL }
  37815. };
  37816. ExpectNull(obj = OBJ_txt2obj("Bad name", 0));
  37817. ASN1_OBJECT_free(obj);
  37818. obj = NULL;
  37819. ExpectNull(obj = OBJ_txt2obj(NULL, 0));
  37820. ASN1_OBJECT_free(obj);
  37821. obj = NULL;
  37822. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  37823. /* Test numerical value of oid (oidStr) */
  37824. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  37825. /* Convert object back to text to confirm oid is correct */
  37826. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  37827. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  37828. ASN1_OBJECT_free(obj);
  37829. obj = NULL;
  37830. XMEMSET(buf, 0, sizeof(buf));
  37831. /* Test short name (sn) */
  37832. ExpectNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  37833. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  37834. /* Convert object back to text to confirm oid is correct */
  37835. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  37836. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  37837. ASN1_OBJECT_free(obj);
  37838. obj = NULL;
  37839. XMEMSET(buf, 0, sizeof(buf));
  37840. /* Test long name (ln) - should fail when no_name = 1 */
  37841. ExpectNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  37842. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  37843. /* Convert object back to text to confirm oid is correct */
  37844. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  37845. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  37846. ASN1_OBJECT_free(obj);
  37847. obj = NULL;
  37848. XMEMSET(buf, 0, sizeof(buf));
  37849. }
  37850. for (i = 0; objs_named[i].numeric != NULL; i++) {
  37851. ExpectNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  37852. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  37853. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  37854. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  37855. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  37856. ASN1_OBJECT_free(obj);
  37857. obj = NULL;
  37858. }
  37859. #endif
  37860. return EXPECT_RESULT();
  37861. }
  37862. static int test_wolfSSL_PEM_write_bio_X509(void)
  37863. {
  37864. EXPECT_DECLS;
  37865. #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
  37866. defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
  37867. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
  37868. !defined(NO_FILESYSTEM)
  37869. /* This test contains the hard coded expected
  37870. * lengths. Update if necessary */
  37871. XFILE fp = XBADFILE;
  37872. WOLFSSL_EVP_PKEY *priv = NULL;
  37873. BIO* input = NULL;
  37874. BIO* output = NULL;
  37875. X509* x509a = NULL;
  37876. X509* x509b = NULL;
  37877. ASN1_TIME* notBeforeA = NULL;
  37878. ASN1_TIME* notAfterA = NULL;
  37879. #ifndef NO_ASN_TIME
  37880. ASN1_TIME* notBeforeB = NULL;
  37881. ASN1_TIME* notAfterB = NULL;
  37882. #endif
  37883. int expectedLen;
  37884. ExpectTrue((fp = XFOPEN("certs/server-key.pem", "rb")) != XBADFILE);
  37885. ExpectNotNull(priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL));
  37886. if (fp != XBADFILE) {
  37887. XFCLOSE(fp);
  37888. fp = XBADFILE;
  37889. }
  37890. ExpectNotNull(input = BIO_new_file("certs/test/cert-ext-multiple.pem",
  37891. "rb"));
  37892. ExpectIntEQ(wolfSSL_BIO_get_len(input), 2000);
  37893. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  37894. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  37895. ExpectNotNull(notBeforeA = X509_get_notBefore(x509a));
  37896. ExpectNotNull(notAfterA = X509_get_notAfter(x509a));
  37897. /* write X509 back to PEM BIO; no need to sign as nothing changed. */
  37898. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  37899. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  37900. /* compare length against expected */
  37901. expectedLen = 2000;
  37902. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  37903. #ifndef NO_ASN_TIME
  37904. /* read exported X509 PEM back into struct, sanity check on export,
  37905. * make sure notBefore/notAfter are the same and certs are identical. */
  37906. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  37907. ExpectNotNull(notBeforeB = X509_get_notBefore(x509b));
  37908. ExpectNotNull(notAfterB = X509_get_notAfter(x509b));
  37909. ExpectIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  37910. ExpectIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  37911. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  37912. X509_free(x509b);
  37913. x509b = NULL;
  37914. #endif
  37915. /* Reset output buffer */
  37916. BIO_free(output);
  37917. output = NULL;
  37918. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  37919. /* Test forcing the AKID to be generated just from KeyIdentifier */
  37920. if (EXPECT_SUCCESS() && x509a->authKeyIdSrc != NULL) {
  37921. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  37922. x509a->authKeyId = x509a->authKeyIdSrc;
  37923. x509a->authKeyIdSrc = NULL;
  37924. x509a->authKeyIdSrcSz = 0;
  37925. }
  37926. /* Resign to re-generate the der */
  37927. ExpectIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
  37928. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  37929. /* Check that we generate a smaller output since the AKID will
  37930. * only contain the KeyIdentifier without any additional
  37931. * information */
  37932. /* Here we copy the validity struct from the original */
  37933. expectedLen = 1688;
  37934. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  37935. /* Reset buffers and x509 */
  37936. BIO_free(input);
  37937. input = NULL;
  37938. BIO_free(output);
  37939. output = NULL;
  37940. X509_free(x509a);
  37941. x509a = NULL;
  37942. /* test CA and basicConstSet values are encoded when
  37943. * the cert is a CA */
  37944. ExpectNotNull(input = BIO_new_file("certs/server-cert.pem", "rb"));
  37945. /* read PEM into X509 struct */
  37946. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  37947. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  37948. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  37949. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  37950. /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
  37951. * values are maintained and certs are identical.*/
  37952. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  37953. ExpectIntEQ(x509b->isCa, 1);
  37954. ExpectIntEQ(x509b->basicConstSet, 1);
  37955. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  37956. X509_free(x509a);
  37957. x509a = NULL;
  37958. X509_free(x509b);
  37959. x509b = NULL;
  37960. BIO_free(input);
  37961. input = NULL;
  37962. BIO_free(output);
  37963. output = NULL;
  37964. /* test CA and basicConstSet values are encoded when
  37965. * the cert is not CA */
  37966. ExpectNotNull(input = BIO_new_file("certs/client-uri-cert.pem", "rb"));
  37967. /* read PEM into X509 struct */
  37968. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  37969. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  37970. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  37971. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  37972. /* read exported X509 PEM back into struct, ensure isCa and
  37973. * basicConstSet values are maintained and certs are identical */
  37974. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  37975. ExpectIntEQ(x509b->isCa, 0);
  37976. ExpectIntEQ(x509b->basicConstSet, 1);
  37977. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  37978. wolfSSL_EVP_PKEY_free(priv);
  37979. X509_free(x509a);
  37980. X509_free(x509b);
  37981. BIO_free(input);
  37982. BIO_free(output);
  37983. #endif
  37984. return EXPECT_RESULT();
  37985. }
  37986. static int test_wolfSSL_X509_NAME_ENTRY(void)
  37987. {
  37988. EXPECT_DECLS;
  37989. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  37990. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  37991. X509* x509 = NULL;
  37992. #ifndef NO_BIO
  37993. BIO* bio = NULL;
  37994. #endif
  37995. X509_NAME* nm = NULL;
  37996. X509_NAME_ENTRY* entry = NULL;
  37997. unsigned char cn[] = "another name to add";
  37998. #ifdef OPENSSL_ALL
  37999. int i;
  38000. int names_len = 0;
  38001. #endif
  38002. ExpectNotNull(x509 =
  38003. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  38004. #ifndef NO_BIO
  38005. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38006. ExpectIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  38007. #endif
  38008. #ifdef WOLFSSL_CERT_REQ
  38009. {
  38010. X509_REQ* req = NULL;
  38011. #ifndef NO_BIO
  38012. BIO* bReq = NULL;
  38013. #endif
  38014. ExpectNotNull(req =
  38015. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  38016. #ifndef NO_BIO
  38017. ExpectNotNull(bReq = BIO_new(BIO_s_mem()));
  38018. ExpectIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  38019. BIO_free(bReq);
  38020. #endif
  38021. X509_free(req);
  38022. }
  38023. #endif
  38024. ExpectNotNull(nm = X509_get_subject_name(x509));
  38025. /* Test add entry */
  38026. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  38027. 0x0c, cn, (int)sizeof(cn)));
  38028. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  38029. #ifdef WOLFSSL_CERT_EXT
  38030. ExpectIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  38031. (byte*)"support@wolfssl.com", 19, -1,
  38032. 1), WOLFSSL_SUCCESS);
  38033. #endif
  38034. X509_NAME_ENTRY_free(entry);
  38035. entry = NULL;
  38036. #ifdef WOLFSSL_CERT_REQ
  38037. {
  38038. unsigned char srv_pkcs9p[] = "Server";
  38039. unsigned char fvrtDrnk[] = "tequila";
  38040. unsigned char* der = NULL;
  38041. char* subject = NULL;
  38042. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  38043. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  38044. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_favouriteDrink,
  38045. MBSTRING_ASC, fvrtDrnk, -1, -1, 0), SSL_SUCCESS);
  38046. ExpectIntGT(wolfSSL_i2d_X509_NAME(nm, &der), 0);
  38047. ExpectNotNull(der);
  38048. ExpectNotNull(subject = X509_NAME_oneline(nm, 0, 0));
  38049. ExpectNotNull(XSTRSTR(subject, "favouriteDrink=tequila"));
  38050. ExpectNotNull(XSTRSTR(subject, "contentType=Server"));
  38051. #ifdef DEBUG_WOLFSSL
  38052. if (subject != NULL) {
  38053. fprintf(stderr, "\n\t%s\n", subject);
  38054. }
  38055. #endif
  38056. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  38057. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  38058. }
  38059. #endif
  38060. /* Test add entry by text */
  38061. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  38062. 0x0c, cn, (int)sizeof(cn)));
  38063. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  38064. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  38065. ExpectNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  38066. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  38067. #endif
  38068. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  38069. X509_NAME_ENTRY_free(entry);
  38070. entry = NULL;
  38071. /* Test add entry by NID */
  38072. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  38073. cn, -1, -1, 0), SSL_SUCCESS);
  38074. #ifdef OPENSSL_ALL
  38075. /* stack of name entry */
  38076. ExpectIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  38077. for (i = 0; i < names_len; i++) {
  38078. ExpectNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  38079. }
  38080. #endif
  38081. #ifndef NO_BIO
  38082. BIO_free(bio);
  38083. #endif
  38084. X509_free(x509); /* free's nm */
  38085. #endif
  38086. return EXPECT_RESULT();
  38087. }
  38088. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  38089. static int test_GENERAL_NAME_set0_othername(void) {
  38090. EXPECT_DECLS;
  38091. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38092. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  38093. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  38094. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  38095. defined(WOLFSSL_FPKI)
  38096. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  38097. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  38098. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  38099. const char * cert_fname = "./certs/server-cert.der";
  38100. const char * key_fname = "./certs/server-key.der";
  38101. X509* x509 = NULL;
  38102. GENERAL_NAME* gn = NULL;
  38103. GENERAL_NAMES* gns = NULL;
  38104. ASN1_OBJECT* upn_oid = NULL;
  38105. ASN1_UTF8STRING *utf8str = NULL;
  38106. ASN1_TYPE *value = NULL;
  38107. X509_EXTENSION * ext = NULL;
  38108. byte* pt = NULL;
  38109. byte der[4096];
  38110. int derSz = 0;
  38111. EVP_PKEY* priv = NULL;
  38112. XFILE f = XBADFILE;
  38113. ExpectTrue((f = XFOPEN(cert_fname, "rb")) != XBADFILE);
  38114. ExpectNotNull(x509 = d2i_X509_fp(f, NULL));
  38115. if (f != XBADFILE) {
  38116. XFCLOSE(f);
  38117. f = XBADFILE;
  38118. }
  38119. ExpectNotNull(gn = GENERAL_NAME_new());
  38120. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  38121. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  38122. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  38123. ExpectNotNull(value = ASN1_TYPE_new());
  38124. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  38125. if ((value == NULL) || (value->value.ptr != (char*)utf8str)) {
  38126. wolfSSL_ASN1_STRING_free(utf8str);
  38127. }
  38128. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  38129. if (EXPECT_FAIL()) {
  38130. ASN1_TYPE_free(value);
  38131. }
  38132. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  38133. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  38134. if (EXPECT_FAIL()) {
  38135. GENERAL_NAME_free(gn);
  38136. gn = NULL;
  38137. }
  38138. ExpectNotNull(ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  38139. ExpectIntEQ(X509_add_ext(x509, ext, -1), 1);
  38140. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  38141. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  38142. if (f != XBADFILE) {
  38143. XFCLOSE(f);
  38144. f = XBADFILE;
  38145. }
  38146. pt = der;
  38147. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  38148. (const unsigned char**)&pt, derSz));
  38149. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  38150. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  38151. gns = NULL;
  38152. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  38153. NULL));
  38154. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 3);
  38155. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 2));
  38156. ExpectIntEQ(gn->type, 0);
  38157. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  38158. ASN1_OBJECT_free(upn_oid);
  38159. X509_EXTENSION_free(ext);
  38160. X509_free(x509);
  38161. EVP_PKEY_free(priv);
  38162. #endif
  38163. return EXPECT_RESULT();
  38164. }
  38165. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  38166. static int test_othername_and_SID_ext(void) {
  38167. EXPECT_DECLS;
  38168. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38169. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  38170. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  38171. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  38172. defined(WOLFSSL_FPKI) && defined(WOLFSSL_ASN_TEMPLATE)
  38173. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  38174. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  38175. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  38176. const char* csr_fname = "./certs/csr.signed.der";
  38177. const char* key_fname = "./certs/server-key.der";
  38178. byte der[4096];
  38179. int derSz = 0;
  38180. X509_REQ* x509 = NULL;
  38181. STACK_OF(X509_EXTENSION) *exts = NULL;
  38182. X509_EXTENSION * san_ext = NULL;
  38183. X509_EXTENSION * ext = NULL;
  38184. GENERAL_NAME* gn = NULL;
  38185. GENERAL_NAMES* gns = NULL;
  38186. ASN1_OBJECT* upn_oid = NULL;
  38187. ASN1_UTF8STRING *utf8str = NULL;
  38188. ASN1_TYPE *value = NULL;
  38189. ASN1_STRING *extval = NULL;
  38190. /* SID extension. SID data format explained here:
  38191. * https://blog.qdsecurity.se/2022/05/27/manually-injecting-a-sid-in-a-certificate/
  38192. */
  38193. uint8_t SidExtension[] = {
  38194. 48, 64, 160, 62, 6, 10, 43, 6, 1, 4, 1, 130, 55, 25, 2, 1, 160,
  38195. 48, 4, 46, 83, 45, 49, 45, 53, 45, 50, 49, 45, 50, 56, 52, 51, 57,
  38196. 48, 55, 52, 49, 56, 45, 51, 57, 50, 54, 50, 55, 55, 52, 50, 49, 45,
  38197. 51, 56, 49, 53, 57, 57, 51, 57, 55, 50, 45, 52, 54, 48, 49};
  38198. uint8_t expectedAltName[] = {
  38199. 0x30, 0x27, 0xA0, 0x25, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82,
  38200. 0x37, 0x14, 0x02, 0x03, 0xA0, 0x17, 0x0C, 0x15, 0x6F, 0x74, 0x68, 0x65,
  38201. 0x72, 0x6E, 0x61, 0x6D, 0x65, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  38202. 0x6C, 0x2E, 0x63, 0x6F, 0x6D};
  38203. X509_EXTENSION *sid_ext = NULL;
  38204. ASN1_OBJECT* sid_oid = NULL;
  38205. ASN1_OCTET_STRING *sid_data = NULL;
  38206. EVP_PKEY* priv = NULL;
  38207. XFILE f = XBADFILE;
  38208. byte* pt = NULL;
  38209. BIO* bio = NULL;
  38210. ExpectTrue((f = XFOPEN(csr_fname, "rb")) != XBADFILE);
  38211. ExpectNotNull(x509 = d2i_X509_REQ_fp(f, NULL));
  38212. if (f != XBADFILE) {
  38213. XFCLOSE(f);
  38214. f = XBADFILE;
  38215. }
  38216. ExpectIntEQ(X509_REQ_set_version(x509, 2), 1);
  38217. ExpectNotNull(gn = GENERAL_NAME_new());
  38218. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  38219. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  38220. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  38221. ExpectNotNull(value = ASN1_TYPE_new());
  38222. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  38223. if (EXPECT_FAIL()) {
  38224. ASN1_UTF8STRING_free(utf8str);
  38225. }
  38226. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  38227. if (EXPECT_FAIL()) {
  38228. ASN1_TYPE_free(value);
  38229. GENERAL_NAME_free(gn);
  38230. gn = NULL;
  38231. }
  38232. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  38233. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  38234. if (EXPECT_FAIL()) {
  38235. GENERAL_NAME_free(gn);
  38236. }
  38237. ExpectNotNull(san_ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  38238. ExpectNotNull(sid_oid = OBJ_txt2obj("1.3.6.1.4.1.311.25.2", 1));
  38239. ExpectNotNull(sid_data = ASN1_OCTET_STRING_new());
  38240. ASN1_OCTET_STRING_set(sid_data, SidExtension, sizeof(SidExtension));
  38241. ExpectNotNull(sid_ext = X509_EXTENSION_create_by_OBJ(NULL, sid_oid, 0,
  38242. sid_data));
  38243. ExpectNotNull(exts = sk_X509_EXTENSION_new_null());
  38244. /* Ensure an empty stack doesn't raise an error. */
  38245. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  38246. ExpectIntEQ(sk_X509_EXTENSION_push(exts, san_ext), 1);
  38247. if (EXPECT_FAIL()) {
  38248. X509_EXTENSION_free(san_ext);
  38249. }
  38250. ExpectIntEQ(sk_X509_EXTENSION_push(exts, sid_ext), 2);
  38251. if (EXPECT_FAIL()) {
  38252. X509_EXTENSION_free(sid_ext);
  38253. }
  38254. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  38255. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  38256. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  38257. if (f != XBADFILE)
  38258. XFCLOSE(f);
  38259. pt = der;
  38260. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  38261. (const unsigned char**)&pt, derSz));
  38262. ExpectIntGT(X509_REQ_sign(x509, priv, EVP_sha256()), 0);
  38263. pt = der;
  38264. ExpectIntGT(derSz = i2d_X509_REQ(x509, &pt), 0);
  38265. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  38266. gns = NULL;
  38267. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  38268. exts = NULL;
  38269. ASN1_OBJECT_free(upn_oid);
  38270. ASN1_OBJECT_free(sid_oid);
  38271. ASN1_OCTET_STRING_free(sid_data);
  38272. X509_REQ_free(x509);
  38273. x509 = NULL;
  38274. EVP_PKEY_free(priv);
  38275. /* At this point everything used to generate what is in der is cleaned up.
  38276. * We now read back from der to confirm the extensions were inserted
  38277. * correctly. */
  38278. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
  38279. ExpectNotNull(bio);
  38280. ExpectIntEQ(BIO_write(bio, der, derSz), derSz); /* d2i consumes BIO */
  38281. ExpectNotNull(d2i_X509_REQ_bio(bio, &x509));
  38282. ExpectNotNull(x509);
  38283. BIO_free(bio);
  38284. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  38285. x509));
  38286. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  38287. /* Check the SID extension. */
  38288. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 0));
  38289. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  38290. ExpectIntEQ(extval->length, sizeof(SidExtension));
  38291. ExpectIntEQ(XMEMCMP(SidExtension, extval->data, sizeof(SidExtension)), 0);
  38292. /* Check the AltNames extension. */
  38293. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 1));
  38294. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  38295. ExpectIntEQ(extval->length, sizeof(expectedAltName));
  38296. ExpectIntEQ(XMEMCMP(expectedAltName, extval->data, sizeof(expectedAltName)),
  38297. 0);
  38298. /* Cleanup */
  38299. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  38300. NULL));
  38301. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  38302. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 0));
  38303. ExpectIntEQ(gn->type, 0);
  38304. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  38305. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  38306. X509_REQ_free(x509);
  38307. #endif
  38308. return EXPECT_RESULT();
  38309. }
  38310. static int test_wolfSSL_X509_set_name(void)
  38311. {
  38312. EXPECT_DECLS;
  38313. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38314. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  38315. X509* x509 = NULL;
  38316. X509_NAME* name = NULL;
  38317. ExpectNotNull(name = X509_NAME_new());
  38318. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  38319. (byte*)"wolfssl.com", 11, 0, 1),
  38320. WOLFSSL_SUCCESS);
  38321. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  38322. (byte*)"support@wolfssl.com", 19, -1,
  38323. 1), WOLFSSL_SUCCESS);
  38324. ExpectNotNull(x509 = X509_new());
  38325. ExpectIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  38326. ExpectIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  38327. ExpectIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  38328. ExpectIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  38329. ExpectIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  38330. ExpectIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  38331. ExpectIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  38332. ExpectIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  38333. X509_free(x509);
  38334. X509_NAME_free(name);
  38335. #endif /* OPENSSL_ALL && !NO_CERTS */
  38336. return EXPECT_RESULT();
  38337. }
  38338. static int test_wolfSSL_X509_set_notAfter(void)
  38339. {
  38340. EXPECT_DECLS;
  38341. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  38342. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  38343. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  38344. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  38345. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  38346. /* Generalized time will overflow time_t if not long */
  38347. X509* x = NULL;
  38348. BIO* bio = NULL;
  38349. ASN1_TIME *asn_time = NULL;
  38350. ASN1_TIME *time_check = NULL;
  38351. const int year = 365*24*60*60;
  38352. const int day = 24*60*60;
  38353. const int hour = 60*60;
  38354. const int mini = 60;
  38355. int offset_day;
  38356. unsigned char buf[25];
  38357. time_t t;
  38358. /*
  38359. * Setup asn_time. APACHE HTTPD uses time(NULL)
  38360. */
  38361. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  38362. offset_day = 7;
  38363. /*
  38364. * Free these.
  38365. */
  38366. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  38367. ExpectNotNull(asn_time);
  38368. ExpectNotNull(x = X509_new());
  38369. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38370. /*
  38371. * Tests
  38372. */
  38373. ExpectTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  38374. /* time_check is simply (ANS1_TIME*)x->notAfter */
  38375. ExpectNotNull(time_check = X509_get_notAfter(x));
  38376. /* ANS1_TIME_check validates by checking if argument can be parsed */
  38377. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  38378. /* Convert to human readable format and compare to intended date */
  38379. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  38380. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  38381. ExpectIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  38382. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, NULL));
  38383. ExpectFalse(wolfSSL_X509_set_notAfter(x, NULL));
  38384. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, asn_time));
  38385. /*
  38386. * Cleanup
  38387. */
  38388. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  38389. X509_free(x);
  38390. BIO_free(bio);
  38391. #endif
  38392. return EXPECT_RESULT();
  38393. }
  38394. static int test_wolfSSL_X509_set_notBefore(void)
  38395. {
  38396. EXPECT_DECLS;
  38397. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  38398. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  38399. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  38400. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  38401. X509* x = NULL;
  38402. BIO* bio = NULL;
  38403. ASN1_TIME *asn_time = NULL;
  38404. ASN1_TIME *time_check = NULL;
  38405. const int year = 365*24*60*60;
  38406. const int day = 24*60*60;
  38407. const int hour = 60*60;
  38408. const int mini = 60;
  38409. int offset_day;
  38410. unsigned char buf[25];
  38411. time_t t;
  38412. /*
  38413. * Setup asn_time. APACHE HTTPD uses time(NULL)
  38414. */
  38415. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  38416. offset_day = 7;
  38417. /*
  38418. * Free these.
  38419. */
  38420. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  38421. ExpectNotNull(asn_time);
  38422. ExpectNotNull(x = X509_new());
  38423. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38424. ExpectIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  38425. /*
  38426. * Main Tests
  38427. */
  38428. ExpectTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  38429. /* time_check == (ANS1_TIME*)x->notBefore */
  38430. ExpectNotNull(time_check = X509_get_notBefore(x));
  38431. /* ANS1_TIME_check validates by checking if argument can be parsed */
  38432. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  38433. /* Convert to human readable format and compare to intended date */
  38434. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  38435. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  38436. ExpectIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  38437. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, NULL));
  38438. ExpectFalse(wolfSSL_X509_set_notBefore(x, NULL));
  38439. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, asn_time));
  38440. /*
  38441. * Cleanup
  38442. */
  38443. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  38444. X509_free(x);
  38445. BIO_free(bio);
  38446. #endif
  38447. return EXPECT_RESULT();
  38448. }
  38449. static int test_wolfSSL_X509_set_version(void)
  38450. {
  38451. EXPECT_DECLS;
  38452. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  38453. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  38454. X509* x509 = NULL;
  38455. long v = 2L;
  38456. long maxInt = INT_MAX;
  38457. ExpectNotNull(x509 = X509_new());
  38458. /* These should pass. */
  38459. ExpectTrue(wolfSSL_X509_set_version(x509, v));
  38460. ExpectIntEQ(v, wolfSSL_X509_get_version(x509));
  38461. /* Fail Case: When v(long) is greater than x509->version(int). */
  38462. v = maxInt+1;
  38463. ExpectFalse(wolfSSL_X509_set_version(x509, v));
  38464. ExpectFalse(wolfSSL_X509_set_version(NULL, 2L));
  38465. ExpectFalse(wolfSSL_X509_set_version(NULL, maxInt+1));
  38466. /* Cleanup */
  38467. X509_free(x509);
  38468. #endif
  38469. return EXPECT_RESULT();
  38470. }
  38471. #ifndef NO_BIO
  38472. static int test_wolfSSL_BIO_gets(void)
  38473. {
  38474. EXPECT_DECLS;
  38475. #if defined(OPENSSL_EXTRA)
  38476. BIO* bio = NULL;
  38477. BIO* bio2 = NULL;
  38478. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  38479. char emp[] = "";
  38480. char bio_buffer[20];
  38481. int bufferSz = 20;
  38482. #ifdef OPENSSL_ALL
  38483. BUF_MEM* emp_bm = NULL;
  38484. BUF_MEM* msg_bm = NULL;
  38485. #endif
  38486. /* try with bad args */
  38487. ExpectNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  38488. #ifdef OPENSSL_ALL
  38489. ExpectIntEQ(BIO_set_mem_buf(bio, NULL, BIO_NOCLOSE), BAD_FUNC_ARG);
  38490. #endif
  38491. /* try with real msg */
  38492. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  38493. XMEMSET(bio_buffer, 0, bufferSz);
  38494. ExpectNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  38495. ExpectNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  38496. ExpectNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  38497. ExpectFalse(bio2 != BIO_next(bio));
  38498. /* make buffer filled with no terminating characters */
  38499. XMEMSET(bio_buffer, 1, bufferSz);
  38500. /* BIO_gets reads a line of data */
  38501. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  38502. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  38503. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  38504. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  38505. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  38506. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  38507. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  38508. #ifdef OPENSSL_ALL
  38509. /* test setting the mem_buf manually */
  38510. BIO_free(bio);
  38511. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  38512. ExpectNotNull(emp_bm = BUF_MEM_new());
  38513. ExpectNotNull(msg_bm = BUF_MEM_new());
  38514. ExpectIntEQ(BUF_MEM_grow(msg_bm, sizeof(msg)), sizeof(msg));
  38515. if (EXPECT_SUCCESS()) {
  38516. XFREE(msg_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  38517. msg_bm->data = NULL;
  38518. }
  38519. /* emp size is 1 for terminator */
  38520. ExpectIntEQ(BUF_MEM_grow(emp_bm, sizeof(emp)), sizeof(emp));
  38521. if (EXPECT_SUCCESS()) {
  38522. XFREE(emp_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  38523. emp_bm->data = emp;
  38524. msg_bm->data = msg;
  38525. }
  38526. ExpectIntEQ(BIO_set_mem_buf(bio, emp_bm, BIO_CLOSE), WOLFSSL_SUCCESS);
  38527. /* check reading an empty string */
  38528. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  38529. ExpectStrEQ(emp, bio_buffer);
  38530. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  38531. /* BIO_gets reads a line of data */
  38532. ExpectIntEQ(BIO_set_mem_buf(bio, msg_bm, BIO_NOCLOSE), WOLFSSL_SUCCESS);
  38533. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  38534. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  38535. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  38536. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  38537. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  38538. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  38539. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  38540. if (EXPECT_SUCCESS())
  38541. emp_bm->data = NULL;
  38542. BUF_MEM_free(emp_bm);
  38543. if (EXPECT_SUCCESS())
  38544. msg_bm->data = NULL;
  38545. BUF_MEM_free(msg_bm);
  38546. #endif
  38547. /* check not null terminated string */
  38548. BIO_free(bio);
  38549. bio = NULL;
  38550. msg[0] = 0x33;
  38551. msg[1] = 0x33;
  38552. msg[2] = 0x33;
  38553. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  38554. ExpectIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  38555. ExpectIntEQ(bio_buffer[0], msg[0]);
  38556. ExpectIntEQ(bio_buffer[1], msg[1]);
  38557. ExpectIntNE(bio_buffer[2], msg[2]);
  38558. BIO_free(bio);
  38559. bio = NULL;
  38560. msg[3] = 0x33;
  38561. bio_buffer[3] = 0x33;
  38562. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  38563. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  38564. ExpectIntEQ(bio_buffer[0], msg[0]);
  38565. ExpectIntEQ(bio_buffer[1], msg[1]);
  38566. ExpectIntEQ(bio_buffer[2], msg[2]);
  38567. ExpectIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  38568. /* check reading an empty string */
  38569. BIO_free(bio);
  38570. bio = NULL;
  38571. ExpectNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  38572. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  38573. ExpectStrEQ(emp, bio_buffer);
  38574. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  38575. /* check error cases */
  38576. BIO_free(bio);
  38577. bio = NULL;
  38578. ExpectIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  38579. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38580. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  38581. #if !defined(NO_FILESYSTEM)
  38582. {
  38583. BIO* f_bio = NULL;
  38584. XFILE f = XBADFILE;
  38585. ExpectNotNull(f_bio = BIO_new(BIO_s_file()));
  38586. ExpectIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  38587. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  38588. ExpectIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  38589. if (EXPECT_FAIL() && (f != XBADFILE)) {
  38590. XFCLOSE(f);
  38591. }
  38592. ExpectIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  38593. BIO_free(f_bio);
  38594. f_bio = NULL;
  38595. }
  38596. #endif /* NO_FILESYSTEM */
  38597. BIO_free(bio);
  38598. bio = NULL;
  38599. BIO_free(bio2);
  38600. bio2 = NULL;
  38601. /* try with type BIO */
  38602. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  38603. sizeof(msg));
  38604. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  38605. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  38606. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  38607. ExpectIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  38608. ExpectIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  38609. ExpectIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  38610. ExpectIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  38611. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  38612. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  38613. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  38614. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  38615. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  38616. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  38617. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  38618. BIO_free(bio);
  38619. bio = NULL;
  38620. BIO_free(bio2);
  38621. bio2 = NULL;
  38622. /* check reading an empty string */
  38623. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  38624. ExpectIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  38625. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  38626. ExpectStrEQ(emp, bio_buffer);
  38627. BIO_free(bio);
  38628. bio = NULL;
  38629. #endif
  38630. return EXPECT_RESULT();
  38631. }
  38632. static int test_wolfSSL_BIO_puts(void)
  38633. {
  38634. EXPECT_DECLS;
  38635. #if defined(OPENSSL_EXTRA)
  38636. BIO* bio = NULL;
  38637. char input[] = "hello\0world\n.....ok\n\0";
  38638. char output[128];
  38639. XMEMSET(output, 0, sizeof(output));
  38640. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38641. ExpectIntEQ(BIO_puts(bio, input), 5);
  38642. ExpectIntEQ(BIO_pending(bio), 5);
  38643. ExpectIntEQ(BIO_puts(bio, input + 6), 14);
  38644. ExpectIntEQ(BIO_pending(bio), 19);
  38645. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  38646. ExpectStrEQ(output, "helloworld\n");
  38647. ExpectIntEQ(BIO_pending(bio), 8);
  38648. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  38649. ExpectStrEQ(output, ".....ok\n");
  38650. ExpectIntEQ(BIO_pending(bio), 0);
  38651. ExpectIntEQ(BIO_puts(bio, ""), -1);
  38652. BIO_free(bio);
  38653. #endif
  38654. return EXPECT_RESULT();
  38655. }
  38656. static int test_wolfSSL_BIO_dump(void)
  38657. {
  38658. EXPECT_DECLS;
  38659. #if defined(OPENSSL_EXTRA)
  38660. BIO* bio;
  38661. static const unsigned char data[] = {
  38662. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  38663. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  38664. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  38665. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  38666. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  38667. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  38668. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  38669. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  38670. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  38671. 0xB4
  38672. };
  38673. /* Generated with OpenSSL. */
  38674. static const char expected[] =
  38675. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  38676. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  38677. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  38678. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  38679. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  38680. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  38681. static const char expectedAll[] =
  38682. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  38683. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  38684. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  38685. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  38686. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  38687. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  38688. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  38689. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  38690. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  38691. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  38692. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  38693. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  38694. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  38695. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  38696. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  38697. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  38698. char output[16 * 80];
  38699. int i;
  38700. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38701. /* Example key dumped. */
  38702. ExpectIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  38703. sizeof(expected) - 1);
  38704. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  38705. ExpectIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  38706. /* Try every possible value for a character. */
  38707. for (i = 0; i < 256; i++)
  38708. output[i] = i;
  38709. ExpectIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  38710. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  38711. ExpectIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  38712. BIO_free(bio);
  38713. #endif
  38714. return EXPECT_RESULT();
  38715. }
  38716. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  38717. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  38718. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  38719. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  38720. {
  38721. (void)ssl;
  38722. (void)buf;
  38723. (void)sz;
  38724. (void)ctx;
  38725. return WOLFSSL_CBIO_ERR_WANT_READ;
  38726. }
  38727. #endif
  38728. static int test_wolfSSL_BIO_should_retry(void)
  38729. {
  38730. EXPECT_DECLS;
  38731. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  38732. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  38733. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  38734. tcp_ready ready;
  38735. func_args server_args;
  38736. THREAD_TYPE serverThread;
  38737. SOCKET_T sockfd = 0;
  38738. WOLFSSL_CTX* ctx = NULL;
  38739. WOLFSSL* ssl = NULL;
  38740. char msg[64] = "hello wolfssl!";
  38741. char reply[1024];
  38742. int msgSz = (int)XSTRLEN(msg);
  38743. int ret;
  38744. BIO* bio = NULL;
  38745. XMEMSET(&server_args, 0, sizeof(func_args));
  38746. #ifdef WOLFSSL_TIRTOS
  38747. fdOpenSession(Task_self());
  38748. #endif
  38749. StartTCP();
  38750. InitTcpReady(&ready);
  38751. #if defined(USE_WINDOWS_API)
  38752. /* use RNG to get random port if using windows */
  38753. ready.port = GetRandomPort();
  38754. #endif
  38755. server_args.signal = &ready;
  38756. start_thread(test_server_nofail, &server_args, &serverThread);
  38757. wait_tcp_ready(&server_args);
  38758. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  38759. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  38760. ExpectIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  38761. #endif
  38762. ExpectIntEQ(WOLFSSL_SUCCESS,
  38763. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  38764. ExpectIntEQ(WOLFSSL_SUCCESS,
  38765. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  38766. ExpectIntEQ(WOLFSSL_SUCCESS,
  38767. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  38768. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  38769. /* force retry */
  38770. ExpectNotNull(bio = wolfSSL_BIO_new_ssl(ctx, 1));
  38771. ExpectIntEQ(BIO_get_ssl(bio, &ssl), 1);
  38772. ExpectNotNull(ssl);
  38773. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  38774. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  38775. if (EXPECT_FAIL()) {
  38776. wolfSSL_free(ssl);
  38777. ssl = NULL;
  38778. }
  38779. ExpectIntLE(BIO_write(bio, msg, msgSz), 0);
  38780. ExpectIntNE(BIO_should_retry(bio), 0);
  38781. ExpectIntEQ(BIO_should_read(bio), 0);
  38782. ExpectIntEQ(BIO_should_write(bio), 0);
  38783. /* now perform successful connection */
  38784. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  38785. ExpectIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  38786. ExpectIntNE(BIO_read(bio, reply, sizeof(reply)), 0);
  38787. ret = wolfSSL_get_error(ssl, -1);
  38788. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  38789. ExpectIntNE(BIO_should_retry(bio), 0);
  38790. if (ret == WOLFSSL_ERROR_WANT_READ)
  38791. ExpectIntEQ(BIO_should_read(bio), 1);
  38792. else
  38793. ExpectIntEQ(BIO_should_read(bio), 0);
  38794. if (ret == WOLFSSL_ERROR_WANT_WRITE)
  38795. ExpectIntEQ(BIO_should_write(bio), 1);
  38796. else
  38797. ExpectIntEQ(BIO_should_write(bio), 0);
  38798. }
  38799. else {
  38800. ExpectIntEQ(BIO_should_retry(bio), 0);
  38801. ExpectIntEQ(BIO_should_read(bio), 0);
  38802. ExpectIntEQ(BIO_should_write(bio), 0);
  38803. }
  38804. ExpectIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  38805. XSTRLEN("I hear you fa shizzle!")), 0);
  38806. BIO_free(bio);
  38807. wolfSSL_CTX_free(ctx);
  38808. CloseSocket(sockfd);
  38809. join_thread(serverThread);
  38810. FreeTcpReady(&ready);
  38811. #ifdef WOLFSSL_TIRTOS
  38812. fdOpenSession(Task_self());
  38813. #endif
  38814. #endif
  38815. return EXPECT_RESULT();
  38816. }
  38817. static int test_wolfSSL_BIO_connect(void)
  38818. {
  38819. EXPECT_DECLS;
  38820. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  38821. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  38822. tcp_ready ready;
  38823. func_args server_args;
  38824. THREAD_TYPE serverThread;
  38825. BIO *tcpBio = NULL;
  38826. BIO *sslBio = NULL;
  38827. SSL_CTX* ctx = NULL;
  38828. SSL *ssl = NULL;
  38829. SSL *sslPtr;
  38830. char msg[] = "hello wolfssl!";
  38831. char reply[30];
  38832. char buff[10] = {0};
  38833. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  38834. ExpectIntEQ(WOLFSSL_SUCCESS,
  38835. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  38836. ExpectIntEQ(WOLFSSL_SUCCESS,
  38837. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  38838. ExpectIntEQ(WOLFSSL_SUCCESS,
  38839. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  38840. /* Setup server */
  38841. XMEMSET(&server_args, 0, sizeof(func_args));
  38842. StartTCP();
  38843. InitTcpReady(&ready);
  38844. #if defined(USE_WINDOWS_API)
  38845. /* use RNG to get random port if using windows */
  38846. ready.port = GetRandomPort();
  38847. #endif
  38848. server_args.signal = &ready;
  38849. start_thread(test_server_nofail, &server_args, &serverThread);
  38850. wait_tcp_ready(&server_args);
  38851. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  38852. /* Start the test proper */
  38853. /* Setup the TCP BIO */
  38854. ExpectNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  38855. ExpectIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  38856. /* Setup the SSL object */
  38857. ExpectNotNull(ssl = SSL_new(ctx));
  38858. SSL_set_connect_state(ssl);
  38859. /* Setup the SSL BIO */
  38860. ExpectNotNull(sslBio = BIO_new(BIO_f_ssl()));
  38861. ExpectIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  38862. if (EXPECT_FAIL()) {
  38863. wolfSSL_free(ssl);
  38864. }
  38865. /* Verify that BIO_get_ssl works. */
  38866. ExpectIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  38867. ExpectPtrEq(ssl, sslPtr);
  38868. /* Link BIO's so that sslBio uses tcpBio for IO */
  38869. ExpectPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  38870. /* Do TCP connect */
  38871. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  38872. /* Do TLS handshake */
  38873. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  38874. /* Test writing */
  38875. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  38876. /* Expect length of default wolfSSL reply */
  38877. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  38878. /* Clean it all up */
  38879. BIO_free_all(sslBio);
  38880. /* Server clean up */
  38881. join_thread(serverThread);
  38882. FreeTcpReady(&ready);
  38883. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  38884. * after. */
  38885. XMEMSET(&server_args, 0, sizeof(func_args));
  38886. StartTCP();
  38887. InitTcpReady(&ready);
  38888. #if defined(USE_WINDOWS_API)
  38889. /* use RNG to get random port if using windows */
  38890. ready.port = GetRandomPort();
  38891. #endif
  38892. server_args.signal = &ready;
  38893. start_thread(test_server_nofail, &server_args, &serverThread);
  38894. wait_tcp_ready(&server_args);
  38895. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  38896. ExpectNotNull(sslBio = BIO_new_ssl_connect(ctx));
  38897. ExpectIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  38898. ExpectIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  38899. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  38900. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  38901. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  38902. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  38903. /* Attempt to close the TLS connection gracefully. */
  38904. BIO_ssl_shutdown(sslBio);
  38905. BIO_free_all(sslBio);
  38906. join_thread(serverThread);
  38907. FreeTcpReady(&ready);
  38908. SSL_CTX_free(ctx);
  38909. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  38910. wc_ecc_fp_free(); /* free per thread cache */
  38911. #endif
  38912. #endif
  38913. return EXPECT_RESULT();
  38914. }
  38915. static int test_wolfSSL_BIO_tls(void)
  38916. {
  38917. EXPECT_DECLS;
  38918. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  38919. SSL_CTX* ctx = NULL;
  38920. SSL *ssl = NULL;
  38921. BIO *readBio = NULL;
  38922. BIO *writeBio = NULL;
  38923. int ret;
  38924. int err = 0;
  38925. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  38926. ExpectNotNull(ssl = SSL_new(ctx));
  38927. ExpectNotNull(readBio = BIO_new(BIO_s_mem()));
  38928. ExpectNotNull(writeBio = BIO_new(BIO_s_mem()));
  38929. /* Qt reads data from write-bio,
  38930. * then writes the read data into plain packet.
  38931. * Qt reads data from plain packet,
  38932. * then writes the read data into read-bio.
  38933. */
  38934. SSL_set_bio(ssl, readBio, writeBio);
  38935. do {
  38936. #ifdef WOLFSSL_ASYNC_CRYPT
  38937. if (err == WC_PENDING_E) {
  38938. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  38939. if (ret < 0) { break; } else if (ret == 0) { continue; }
  38940. }
  38941. #endif
  38942. ret = SSL_connect(ssl);
  38943. err = SSL_get_error(ssl, 0);
  38944. } while (err == WC_PENDING_E);
  38945. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  38946. /* in this use case, should return WANT READ
  38947. * so that Qt will read the data from plain packet for next state.
  38948. */
  38949. ExpectIntEQ(err, SSL_ERROR_WANT_READ);
  38950. SSL_free(ssl);
  38951. SSL_CTX_free(ctx);
  38952. #endif
  38953. return EXPECT_RESULT();
  38954. }
  38955. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  38956. defined(HAVE_HTTP_CLIENT)
  38957. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  38958. {
  38959. BIO* clientBio;
  38960. SSL* sslClient;
  38961. SSL_CTX* ctx;
  38962. char connectAddr[20]; /* IP + port */;
  38963. (void)args;
  38964. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  38965. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  38966. AssertIntEQ(BIO_do_connect(clientBio), 1);
  38967. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  38968. AssertNotNull(sslClient = SSL_new(ctx));
  38969. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  38970. SSL_set_bio(sslClient, clientBio, clientBio);
  38971. AssertIntEQ(SSL_connect(sslClient), 1);
  38972. SSL_free(sslClient);
  38973. SSL_CTX_free(ctx);
  38974. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  38975. wc_ecc_fp_free(); /* free per thread cache */
  38976. #endif
  38977. WOLFSSL_RETURN_FROM_THREAD(0);
  38978. }
  38979. #endif
  38980. static int test_wolfSSL_BIO_accept(void)
  38981. {
  38982. EXPECT_DECLS;
  38983. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  38984. defined(HAVE_HTTP_CLIENT)
  38985. BIO* serverBindBio = NULL;
  38986. BIO* serverAcceptBio = NULL;
  38987. SSL* sslServer = NULL;
  38988. SSL_CTX* ctx = NULL;
  38989. func_args args;
  38990. THREAD_TYPE thread;
  38991. char port[10]; /* 10 bytes should be enough to store the string
  38992. * representation of the port */
  38993. ExpectIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  38994. ExpectNotNull(serverBindBio = BIO_new_accept(port));
  38995. /* First BIO_do_accept binds the port */
  38996. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  38997. XMEMSET(&args, 0, sizeof(func_args));
  38998. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  38999. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  39000. /* Let's plug it into SSL to test */
  39001. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  39002. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  39003. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  39004. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  39005. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  39006. ExpectNotNull(sslServer = SSL_new(ctx));
  39007. ExpectNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  39008. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  39009. ExpectIntEQ(SSL_accept(sslServer), 1);
  39010. join_thread(thread);
  39011. BIO_free(serverBindBio);
  39012. SSL_free(sslServer);
  39013. SSL_CTX_free(ctx);
  39014. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  39015. wc_ecc_fp_free(); /* free per thread cache */
  39016. #endif
  39017. #endif
  39018. return EXPECT_RESULT();
  39019. }
  39020. static int test_wolfSSL_BIO_write(void)
  39021. {
  39022. EXPECT_DECLS;
  39023. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  39024. BIO* bio = NULL;
  39025. BIO* bio64 = NULL;
  39026. BIO* bio_mem = NULL;
  39027. BIO* ptr = NULL;
  39028. int sz;
  39029. char msg[] = "conversion test";
  39030. char out[40];
  39031. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  39032. void* bufPtr = NULL;
  39033. BUF_MEM* buf = NULL;
  39034. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  39035. ExpectNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  39036. if (EXPECT_FAIL()) {
  39037. BIO_free(bio64);
  39038. }
  39039. /* now should convert to base64 then write to memory */
  39040. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  39041. BIO_flush(bio);
  39042. /* test BIO chain */
  39043. ExpectIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  39044. ExpectNotNull(buf);
  39045. ExpectIntEQ(buf->length, 25);
  39046. ExpectIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  39047. ExpectPtrEq(buf->data, bufPtr);
  39048. ExpectNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  39049. sz = sizeof(out);
  39050. XMEMSET(out, 0, sz);
  39051. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  39052. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  39053. /* write then read should return the same message */
  39054. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  39055. sz = sizeof(out);
  39056. XMEMSET(out, 0, sz);
  39057. ExpectIntEQ(BIO_read(bio, out, sz), 16);
  39058. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  39059. /* now try encoding with no line ending */
  39060. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  39061. #ifdef HAVE_EX_DATA
  39062. BIO_set_ex_data(bio64, 0, (void*) "data");
  39063. ExpectIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  39064. #endif
  39065. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  39066. BIO_flush(bio);
  39067. sz = sizeof(out);
  39068. XMEMSET(out, 0, sz);
  39069. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  39070. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  39071. BIO_free_all(bio); /* frees bio64 also */
  39072. bio = NULL;
  39073. /* test with more than one bio64 in list */
  39074. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  39075. ExpectNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  39076. if (EXPECT_FAIL()) {
  39077. BIO_free(bio64);
  39078. bio64 = NULL;
  39079. }
  39080. ExpectNotNull(bio_mem = BIO_new(BIO_s_mem()));
  39081. ExpectNotNull(BIO_push(bio64, bio_mem));
  39082. if (EXPECT_FAIL()) {
  39083. BIO_free(bio_mem);
  39084. }
  39085. /* now should convert to base64 when stored and then decode with read */
  39086. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  39087. BIO_flush(bio);
  39088. sz = sizeof(out);
  39089. XMEMSET(out, 0, sz);
  39090. ExpectIntEQ((sz = BIO_read(bio, out, sz)), 16);
  39091. ExpectIntEQ(XMEMCMP(out, msg, sz), 0);
  39092. BIO_clear_flags(bio64, ~0);
  39093. BIO_set_retry_read(bio);
  39094. BIO_free_all(bio); /* frees bio64s also */
  39095. bio = NULL;
  39096. ExpectNotNull(bio = BIO_new_mem_buf(out, 0));
  39097. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  39098. BIO_free(bio);
  39099. #endif
  39100. return EXPECT_RESULT();
  39101. }
  39102. static int test_wolfSSL_BIO_printf(void)
  39103. {
  39104. EXPECT_DECLS;
  39105. #if defined(OPENSSL_ALL)
  39106. BIO* bio = NULL;
  39107. int sz = 7;
  39108. char msg[] = "TLS 1.3 for the world";
  39109. char out[60];
  39110. char expected[] = "TLS 1.3 for the world : sz = 7";
  39111. XMEMSET(out, 0, sizeof(out));
  39112. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39113. ExpectIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  39114. ExpectIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  39115. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  39116. ExpectIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  39117. BIO_free(bio);
  39118. #endif
  39119. return EXPECT_RESULT();
  39120. }
  39121. static int test_wolfSSL_BIO_f_md(void)
  39122. {
  39123. EXPECT_DECLS;
  39124. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  39125. BIO* bio = NULL;
  39126. BIO* mem = NULL;
  39127. char msg[] = "message to hash";
  39128. char out[60];
  39129. EVP_MD_CTX* ctx = NULL;
  39130. const unsigned char testKey[] =
  39131. {
  39132. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  39133. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  39134. 0x0b, 0x0b, 0x0b, 0x0b
  39135. };
  39136. const char testData[] = "Hi There";
  39137. const unsigned char testResult[] =
  39138. {
  39139. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  39140. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  39141. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  39142. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  39143. };
  39144. const unsigned char expectedHash[] =
  39145. {
  39146. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  39147. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  39148. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  39149. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  39150. };
  39151. const unsigned char emptyHash[] =
  39152. {
  39153. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  39154. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  39155. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  39156. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  39157. };
  39158. unsigned char check[sizeof(testResult) + 1];
  39159. size_t checkSz = -1;
  39160. EVP_PKEY* key = NULL;
  39161. XMEMSET(out, 0, sizeof(out));
  39162. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  39163. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  39164. ExpectIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  39165. ExpectIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  39166. /* should not be able to write/read yet since just digest wrapper and no
  39167. * data is passing through the bio */
  39168. ExpectIntEQ(BIO_write(bio, msg, 0), 0);
  39169. ExpectIntEQ(BIO_pending(bio), 0);
  39170. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  39171. ExpectIntEQ(BIO_gets(bio, out, 3), 0);
  39172. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  39173. ExpectIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  39174. BIO_reset(bio);
  39175. /* append BIO mem to bio in order to read/write */
  39176. ExpectNotNull(bio = BIO_push(bio, mem));
  39177. XMEMSET(out, 0, sizeof(out));
  39178. ExpectIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  39179. ExpectIntEQ(BIO_pending(bio), 16);
  39180. /* this just reads the message and does not hash it (gets calls final) */
  39181. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  39182. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  39183. /* create a message digest using BIO */
  39184. XMEMSET(out, 0, sizeof(out));
  39185. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  39186. ExpectIntEQ(BIO_pending(mem), 16);
  39187. ExpectIntEQ(BIO_pending(bio), 16);
  39188. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  39189. ExpectIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  39190. BIO_free(bio);
  39191. bio = NULL;
  39192. BIO_free(mem);
  39193. mem = NULL;
  39194. /* test with HMAC */
  39195. XMEMSET(out, 0, sizeof(out));
  39196. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  39197. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  39198. BIO_get_md_ctx(bio, &ctx);
  39199. ExpectNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, testKey,
  39200. (int)sizeof(testKey)));
  39201. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  39202. ExpectNotNull(bio = BIO_push(bio, mem));
  39203. BIO_write(bio, testData, (int)strlen(testData));
  39204. ExpectIntEQ(EVP_DigestSignFinal(ctx, NULL, &checkSz), 1);
  39205. ExpectIntEQ(EVP_DigestSignFinal(ctx, check, &checkSz), 1);
  39206. ExpectIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  39207. EVP_PKEY_free(key);
  39208. BIO_free(bio);
  39209. BIO_free(mem);
  39210. #endif
  39211. return EXPECT_RESULT();
  39212. }
  39213. static int test_wolfSSL_BIO_up_ref(void)
  39214. {
  39215. EXPECT_DECLS;
  39216. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  39217. BIO* bio = NULL;
  39218. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  39219. ExpectIntEQ(BIO_up_ref(NULL), 0);
  39220. ExpectIntEQ(BIO_up_ref(bio), 1);
  39221. BIO_free(bio);
  39222. ExpectIntEQ(BIO_up_ref(bio), 1);
  39223. BIO_free(bio);
  39224. BIO_free(bio);
  39225. #endif
  39226. return EXPECT_RESULT();
  39227. }
  39228. static int test_wolfSSL_BIO_reset(void)
  39229. {
  39230. EXPECT_DECLS;
  39231. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  39232. BIO* bio = NULL;
  39233. byte buf[16];
  39234. ExpectNotNull(bio = BIO_new_mem_buf("secure your data",
  39235. (word32)XSTRLEN("secure your data")));
  39236. ExpectIntEQ(BIO_read(bio, buf, 6), 6);
  39237. ExpectIntEQ(XMEMCMP(buf, "secure", 6), 0);
  39238. XMEMSET(buf, 0, 16);
  39239. ExpectIntEQ(BIO_read(bio, buf, 16), 10);
  39240. ExpectIntEQ(XMEMCMP(buf, " your data", 10), 0);
  39241. /* You cannot write to MEM BIO with read-only mode. */
  39242. ExpectIntEQ(BIO_write(bio, "WriteToReadonly", 15), 0);
  39243. ExpectIntEQ(BIO_read(bio, buf, 16), -1);
  39244. XMEMSET(buf, 0, 16);
  39245. ExpectIntEQ(BIO_reset(bio), 0);
  39246. ExpectIntEQ(BIO_read(bio, buf, 16), 16);
  39247. ExpectIntEQ(XMEMCMP(buf, "secure your data", 16), 0);
  39248. BIO_free(bio);
  39249. #endif
  39250. return EXPECT_RESULT();
  39251. }
  39252. #endif /* !NO_BIO */
  39253. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  39254. /* test that the callback arg is correct */
  39255. static int certCbArg = 0;
  39256. static int clientCertCb(WOLFSSL* ssl, void* arg)
  39257. {
  39258. if (ssl == NULL || arg != &certCbArg)
  39259. return 0;
  39260. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  39261. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  39262. return 0;
  39263. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  39264. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  39265. return 0;
  39266. return 1;
  39267. }
  39268. static int clientCertSetupCb(WOLFSSL_CTX* ctx)
  39269. {
  39270. SSL_CTX_set_cert_cb(ctx, clientCertCb, &certCbArg);
  39271. return TEST_SUCCESS;
  39272. }
  39273. /**
  39274. * This is only done because test_client_nofail has no way to stop
  39275. * certificate and key loading
  39276. */
  39277. static int clientCertClearCb(WOLFSSL* ssl)
  39278. {
  39279. /* Clear the loaded certs to force the callbacks to set them up */
  39280. SSL_certs_clear(ssl);
  39281. return TEST_SUCCESS;
  39282. }
  39283. static int serverCertCb(WOLFSSL* ssl, void* arg)
  39284. {
  39285. if (ssl == NULL || arg != &certCbArg)
  39286. return 0;
  39287. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  39288. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  39289. return 0;
  39290. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  39291. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  39292. return 0;
  39293. return 1;
  39294. }
  39295. static int serverCertSetupCb(WOLFSSL_CTX* ctx)
  39296. {
  39297. SSL_CTX_set_cert_cb(ctx, serverCertCb, &certCbArg);
  39298. return TEST_SUCCESS;
  39299. }
  39300. /**
  39301. * This is only done because test_server_nofail has no way to stop
  39302. * certificate and key loading
  39303. */
  39304. static int serverCertClearCb(WOLFSSL* ssl)
  39305. {
  39306. /* Clear the loaded certs to force the callbacks to set them up */
  39307. SSL_certs_clear(ssl);
  39308. return TEST_SUCCESS;
  39309. }
  39310. #endif
  39311. static int test_wolfSSL_cert_cb(void)
  39312. {
  39313. EXPECT_DECLS;
  39314. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  39315. test_ssl_cbf func_cb_client;
  39316. test_ssl_cbf func_cb_server;
  39317. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  39318. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  39319. func_cb_client.ctx_ready = clientCertSetupCb;
  39320. func_cb_client.ssl_ready = clientCertClearCb;
  39321. func_cb_server.ctx_ready = serverCertSetupCb;
  39322. func_cb_server.ssl_ready = serverCertClearCb;
  39323. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  39324. &func_cb_server, NULL), TEST_SUCCESS);
  39325. #endif
  39326. return EXPECT_RESULT();
  39327. }
  39328. static int test_wolfSSL_SESSION(void)
  39329. {
  39330. EXPECT_DECLS;
  39331. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  39332. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  39333. !defined(NO_SESSION_CACHE)
  39334. WOLFSSL* ssl = NULL;
  39335. WOLFSSL_CTX* ctx = NULL;
  39336. WOLFSSL_SESSION* sess = NULL;
  39337. WOLFSSL_SESSION* sess_copy = NULL;
  39338. #ifdef OPENSSL_EXTRA
  39339. #ifdef HAVE_EXT_CACHE
  39340. unsigned char* sessDer = NULL;
  39341. unsigned char* ptr = NULL;
  39342. int sz;
  39343. #endif
  39344. const unsigned char context[] = "user app context";
  39345. unsigned int contextSz = (unsigned int)sizeof(context);
  39346. #endif
  39347. int ret, err;
  39348. SOCKET_T sockfd;
  39349. tcp_ready ready;
  39350. func_args server_args;
  39351. THREAD_TYPE serverThread;
  39352. char msg[80];
  39353. const char* sendGET = "GET";
  39354. /* TLS v1.3 requires session tickets */
  39355. /* CHACHA and POLY1305 required for myTicketEncCb */
  39356. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  39357. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  39358. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  39359. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  39360. #else
  39361. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  39362. #endif
  39363. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  39364. WOLFSSL_FILETYPE_PEM));
  39365. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  39366. WOLFSSL_FILETYPE_PEM));
  39367. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  39368. WOLFSSL_SUCCESS);
  39369. #ifdef WOLFSSL_ENCRYPTED_KEYS
  39370. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  39371. #endif
  39372. #ifdef HAVE_SESSION_TICKET
  39373. /* Use session tickets, for ticket tests below */
  39374. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  39375. #endif
  39376. XMEMSET(&server_args, 0, sizeof(func_args));
  39377. #ifdef WOLFSSL_TIRTOS
  39378. fdOpenSession(Task_self());
  39379. #endif
  39380. StartTCP();
  39381. InitTcpReady(&ready);
  39382. #if defined(USE_WINDOWS_API)
  39383. /* use RNG to get random port if using windows */
  39384. ready.port = GetRandomPort();
  39385. #endif
  39386. server_args.signal = &ready;
  39387. start_thread(test_server_nofail, &server_args, &serverThread);
  39388. wait_tcp_ready(&server_args);
  39389. /* client connection */
  39390. ExpectNotNull(ssl = wolfSSL_new(ctx));
  39391. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  39392. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  39393. #ifdef WOLFSSL_ASYNC_CRYPT
  39394. err = 0; /* Reset error */
  39395. #endif
  39396. do {
  39397. #ifdef WOLFSSL_ASYNC_CRYPT
  39398. if (err == WC_PENDING_E) {
  39399. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  39400. if (ret < 0) { break; } else if (ret == 0) { continue; }
  39401. }
  39402. #endif
  39403. ret = wolfSSL_connect(ssl);
  39404. err = wolfSSL_get_error(ssl, 0);
  39405. } while (err == WC_PENDING_E);
  39406. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  39407. #ifdef WOLFSSL_ASYNC_CRYPT
  39408. err = 0; /* Reset error */
  39409. #endif
  39410. do {
  39411. #ifdef WOLFSSL_ASYNC_CRYPT
  39412. if (err == WC_PENDING_E) {
  39413. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  39414. if (ret < 0) { break; } else if (ret == 0) { continue; }
  39415. }
  39416. #endif
  39417. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  39418. err = wolfSSL_get_error(ssl, 0);
  39419. } while (err == WC_PENDING_E);
  39420. ExpectIntEQ(ret, (int)XSTRLEN(sendGET));
  39421. #ifdef WOLFSSL_ASYNC_CRYPT
  39422. err = 0; /* Reset error */
  39423. #endif
  39424. do {
  39425. #ifdef WOLFSSL_ASYNC_CRYPT
  39426. if (err == WC_PENDING_E) {
  39427. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  39428. if (ret < 0) { break; } else if (ret == 0) { continue; }
  39429. }
  39430. #endif
  39431. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  39432. err = wolfSSL_get_error(ssl, 0);
  39433. } while (err == WC_PENDING_E);
  39434. ExpectIntEQ(ret, 23);
  39435. ExpectPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  39436. ExpectPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  39437. #ifdef HAVE_EXT_CACHE
  39438. ExpectPtrEq(sess, sess_copy); /* they should be the same pointer but without
  39439. * HAVE_EXT_CACHE we get new objects each time */
  39440. #endif
  39441. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  39442. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  39443. sess = wolfSSL_get_session(ssl);
  39444. #ifdef OPENSSL_EXTRA
  39445. ExpectIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  39446. ExpectIntEQ(SSL_SESSION_is_resumable(sess), 1);
  39447. ExpectIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  39448. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  39449. #ifdef HAVE_SESSION_TICKET
  39450. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  39451. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  39452. SESSION_TICKET_HINT_DEFAULT);
  39453. #else
  39454. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  39455. #endif
  39456. #else
  39457. (void)sess;
  39458. #endif /* OPENSSL_EXTRA */
  39459. /* Retain copy of the session for later testing */
  39460. ExpectNotNull(sess = wolfSSL_get1_session(ssl));
  39461. wolfSSL_shutdown(ssl);
  39462. wolfSSL_free(ssl); ssl = NULL;
  39463. join_thread(serverThread);
  39464. FreeTcpReady(&ready);
  39465. #ifdef WOLFSSL_TIRTOS
  39466. fdOpenSession(Task_self());
  39467. #endif
  39468. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  39469. {
  39470. X509 *x509 = NULL;
  39471. char buf[30];
  39472. int bufSz = 0;
  39473. ExpectNotNull(x509 = SSL_SESSION_get0_peer(sess));
  39474. ExpectIntGT((bufSz = X509_NAME_get_text_by_NID(
  39475. X509_get_subject_name(x509), NID_organizationalUnitName, buf,
  39476. sizeof(buf))), 0);
  39477. ExpectIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  39478. if (bufSz == 7) {
  39479. ExpectIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  39480. }
  39481. if (bufSz == 16) {
  39482. ExpectIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  39483. }
  39484. }
  39485. #endif
  39486. #ifdef HAVE_EXT_CACHE
  39487. ExpectNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  39488. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  39489. sess_copy = NULL;
  39490. #endif
  39491. #if defined(OPENSSL_EXTRA) && defined(HAVE_EXT_CACHE)
  39492. /* get session from DER and update the timeout */
  39493. ExpectIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  39494. ExpectIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  39495. wolfSSL_SESSION_free(sess); sess = NULL;
  39496. sess = NULL;
  39497. ptr = sessDer;
  39498. ExpectNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  39499. ExpectNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  39500. (const unsigned char**)&ptr, sz));
  39501. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  39502. sessDer = NULL;
  39503. ExpectIntGT(wolfSSL_SESSION_get_time(sess), 0);
  39504. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  39505. #endif
  39506. /* successful set session test */
  39507. ExpectNotNull(ssl = wolfSSL_new(ctx));
  39508. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  39509. #ifdef HAVE_SESSION_TICKET
  39510. /* Test set/get session ticket */
  39511. {
  39512. const char* ticket = "This is a session ticket";
  39513. char buf[64] = {0};
  39514. word32 bufSz = (word32)sizeof(buf);
  39515. ExpectIntEQ(SSL_SUCCESS,
  39516. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  39517. (word32)XSTRLEN(ticket)));
  39518. ExpectIntEQ(SSL_SUCCESS,
  39519. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  39520. ExpectStrEQ(ticket, buf);
  39521. }
  39522. #endif
  39523. #ifdef OPENSSL_EXTRA
  39524. /* session timeout case */
  39525. /* make the session to be expired */
  39526. ExpectIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  39527. XSLEEP_MS(1200);
  39528. /* SSL_set_session should reject specified session but return success
  39529. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  39530. */
  39531. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  39532. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  39533. #else
  39534. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  39535. #endif
  39536. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  39537. #ifdef WOLFSSL_SESSION_ID_CTX
  39538. /* fail case with miss match session context IDs (use compatibility API) */
  39539. ExpectIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  39540. SSL_SUCCESS);
  39541. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  39542. wolfSSL_free(ssl); ssl = NULL;
  39543. ExpectIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  39544. SSL_FAILURE);
  39545. ExpectIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  39546. SSL_SUCCESS);
  39547. ExpectNotNull(ssl = wolfSSL_new(ctx));
  39548. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  39549. #endif
  39550. #endif /* OPENSSL_EXTRA */
  39551. wolfSSL_free(ssl);
  39552. wolfSSL_SESSION_free(sess);
  39553. wolfSSL_CTX_free(ctx);
  39554. #endif
  39555. return EXPECT_RESULT();
  39556. }
  39557. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  39558. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  39559. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  39560. !defined(WOLFSSL_NO_TLS12)
  39561. static WOLFSSL_SESSION* test_wolfSSL_SESSION_expire_sess = NULL;
  39562. static void test_wolfSSL_SESSION_expire_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  39563. {
  39564. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  39565. /* returns previous timeout value */
  39566. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), 500);
  39567. #else
  39568. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), WOLFSSL_SUCCESS);
  39569. #endif
  39570. }
  39571. /* set the session to timeout in a second */
  39572. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready(WOLFSSL* ssl)
  39573. {
  39574. AssertIntEQ(wolfSSL_set_timeout(ssl, 2), 1);
  39575. }
  39576. /* store the client side session from the first successful connection */
  39577. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result(WOLFSSL* ssl)
  39578. {
  39579. AssertPtrNE((test_wolfSSL_SESSION_expire_sess = wolfSSL_get1_session(ssl)),
  39580. NULL); /* ref count 1 */
  39581. }
  39582. /* wait till session is expired then set it in the WOLFSSL struct for use */
  39583. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait(WOLFSSL* ssl)
  39584. {
  39585. AssertIntEQ(wolfSSL_set_timeout(ssl, 1), 1);
  39586. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  39587. WOLFSSL_SUCCESS);
  39588. XSLEEP_MS(2000); /* wait 2 seconds for session to expire */
  39589. }
  39590. /* set expired session in the WOLFSSL struct for use */
  39591. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set(WOLFSSL* ssl)
  39592. {
  39593. XSLEEP_MS(1200); /* wait a second for session to expire */
  39594. /* set the expired session, call to set session fails but continuing on
  39595. after failure should be handled here */
  39596. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL)
  39597. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  39598. WOLFSSL_SUCCESS);
  39599. #else
  39600. AssertIntNE(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  39601. WOLFSSL_SUCCESS);
  39602. #endif
  39603. }
  39604. /* check that the expired session was not reused */
  39605. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse(WOLFSSL* ssl)
  39606. {
  39607. /* since the session has expired it should not have been reused */
  39608. AssertIntEQ(wolfSSL_session_reused(ssl), 0);
  39609. }
  39610. #endif
  39611. static int test_wolfSSL_SESSION_expire_downgrade(void)
  39612. {
  39613. EXPECT_DECLS;
  39614. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  39615. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  39616. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  39617. !defined(WOLFSSL_NO_TLS12)
  39618. WOLFSSL_CTX* ctx = NULL;
  39619. callback_functions server_cbf, client_cbf;
  39620. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  39621. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  39622. /* force server side to use TLS 1.2 */
  39623. server_cbf.ctx = ctx;
  39624. server_cbf.method = wolfTLSv1_2_server_method;
  39625. client_cbf.method = wolfSSLv23_client_method;
  39626. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  39627. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready;
  39628. client_cbf.on_result = test_wolfSSL_SESSION_expire_downgrade_ssl_result;
  39629. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  39630. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  39631. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  39632. /* set the previously created session and wait till expired */
  39633. server_cbf.ctx = ctx;
  39634. client_cbf.method = wolfSSLv23_client_method;
  39635. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  39636. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait;
  39637. client_cbf.on_result =
  39638. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  39639. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  39640. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  39641. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  39642. /* set the previously created expired session */
  39643. server_cbf.ctx = ctx;
  39644. client_cbf.method = wolfSSLv23_client_method;
  39645. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  39646. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set;
  39647. client_cbf.on_result =
  39648. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  39649. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  39650. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  39651. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  39652. wolfSSL_SESSION_free(test_wolfSSL_SESSION_expire_sess);
  39653. wolfSSL_CTX_free(ctx);
  39654. #endif
  39655. return EXPECT_RESULT();
  39656. }
  39657. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  39658. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  39659. static int clientSessRemCountMalloc = 0;
  39660. static int serverSessRemCountMalloc = 0;
  39661. static int clientSessRemCountFree = 0;
  39662. static int serverSessRemCountFree = 0;
  39663. static WOLFSSL_CTX* serverSessCtx = NULL;
  39664. static WOLFSSL_SESSION* serverSess = NULL;
  39665. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  39666. !defined(NO_SESSION_CACHE_REF)
  39667. static WOLFSSL_CTX* clientSessCtx = NULL;
  39668. static WOLFSSL_SESSION* clientSess = NULL;
  39669. #endif
  39670. static int serverSessRemIdx = 3;
  39671. static int sessRemCtx_Server = WOLFSSL_SERVER_END;
  39672. static int sessRemCtx_Client = WOLFSSL_CLIENT_END;
  39673. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  39674. {
  39675. int* side;
  39676. (void)ctx;
  39677. side = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  39678. if (side != NULL) {
  39679. if (*side == WOLFSSL_CLIENT_END)
  39680. clientSessRemCountFree++;
  39681. else
  39682. serverSessRemCountFree++;
  39683. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  39684. }
  39685. }
  39686. static int SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  39687. {
  39688. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  39689. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  39690. !defined(NO_SESSION_CACHE_REF)
  39691. {
  39692. EXPECT_DECLS;
  39693. /* Allow downgrade, set min version, and disable TLS 1.3.
  39694. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  39695. * reference to the session cache. But with WOLFSSL_TLS13 and without
  39696. * HAVE_SESSION_TICKET we won't have a session ID to be able to place
  39697. * the session in the cache. In this case we need to downgrade to
  39698. * previous versions to just use the legacy session ID field. */
  39699. ExpectIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  39700. SSL_SUCCESS);
  39701. ExpectIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  39702. SSL_SUCCESS);
  39703. return EXPECT_RESULT();
  39704. }
  39705. #else
  39706. return TEST_SUCCESS;
  39707. #endif
  39708. }
  39709. static int SessRemSslSetupCb(WOLFSSL* ssl)
  39710. {
  39711. EXPECT_DECLS;
  39712. int* side;
  39713. if (SSL_is_server(ssl)) {
  39714. side = &sessRemCtx_Server;
  39715. serverSessRemCountMalloc++;
  39716. ExpectNotNull(serverSess = SSL_get1_session(ssl));
  39717. ExpectIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  39718. SSL_SUCCESS);
  39719. }
  39720. else {
  39721. side = &sessRemCtx_Client;
  39722. clientSessRemCountMalloc++;
  39723. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  39724. !defined(NO_SESSION_CACHE_REF)
  39725. ExpectNotNull(clientSess = SSL_get1_session(ssl));
  39726. ExpectIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  39727. SSL_SUCCESS);
  39728. #endif
  39729. }
  39730. ExpectIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl),
  39731. serverSessRemIdx, side), SSL_SUCCESS);
  39732. return EXPECT_RESULT();
  39733. }
  39734. #endif
  39735. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  39736. {
  39737. EXPECT_DECLS;
  39738. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  39739. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  39740. /* Check that the remove callback gets called for external data in a
  39741. * session object */
  39742. test_ssl_cbf func_cb;
  39743. XMEMSET(&func_cb, 0, sizeof(func_cb));
  39744. func_cb.ctx_ready = SessRemCtxSetupCb;
  39745. func_cb.on_result = SessRemSslSetupCb;
  39746. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb, &func_cb,
  39747. NULL), TEST_SUCCESS);
  39748. /* Both should have been allocated */
  39749. ExpectIntEQ(clientSessRemCountMalloc, 1);
  39750. ExpectIntEQ(serverSessRemCountMalloc, 1);
  39751. /* This should not be called yet. Session wasn't evicted from cache yet. */
  39752. ExpectIntEQ(clientSessRemCountFree, 0);
  39753. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  39754. !defined(NO_SESSION_CACHE_REF)
  39755. /* Force a cache lookup */
  39756. ExpectNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  39757. /* Force a cache update */
  39758. ExpectNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  39759. /* This should set the timeout to 0 and call the remove callback from within
  39760. * the session cache. */
  39761. ExpectIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  39762. ExpectNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  39763. ExpectIntEQ(clientSessRemCountFree, 1);
  39764. #endif
  39765. /* Server session is in the cache so ex_data isn't free'd with the SSL
  39766. * object */
  39767. ExpectIntEQ(serverSessRemCountFree, 0);
  39768. /* Force a cache lookup */
  39769. ExpectNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  39770. /* Force a cache update */
  39771. ExpectNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  39772. /* This should set the timeout to 0 and call the remove callback from within
  39773. * the session cache. */
  39774. ExpectIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  39775. ExpectNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  39776. ExpectIntEQ(serverSessRemCountFree, 1);
  39777. /* Need to free the references that we kept */
  39778. SSL_CTX_free(serverSessCtx);
  39779. SSL_SESSION_free(serverSess);
  39780. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  39781. !defined(NO_SESSION_CACHE_REF)
  39782. SSL_CTX_free(clientSessCtx);
  39783. SSL_SESSION_free(clientSess);
  39784. #endif
  39785. #endif
  39786. return EXPECT_RESULT();
  39787. }
  39788. static int test_wolfSSL_ticket_keys(void)
  39789. {
  39790. EXPECT_DECLS;
  39791. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  39792. !defined(NO_WOLFSSL_SERVER)
  39793. WOLFSSL_CTX* ctx = NULL;
  39794. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  39795. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  39796. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  39797. WOLFSSL_FAILURE);
  39798. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  39799. WOLFSSL_FAILURE);
  39800. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  39801. WOLFSSL_FAILURE);
  39802. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  39803. WOLFSSL_FAILURE);
  39804. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  39805. WOLFSSL_FAILURE);
  39806. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  39807. WOLFSSL_FAILURE);
  39808. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  39809. WOLFSSL_FAILURE);
  39810. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  39811. WOLFSSL_FAILURE);
  39812. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  39813. WOLFSSL_FAILURE);
  39814. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  39815. WOLFSSL_FAILURE);
  39816. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  39817. WOLFSSL_FAILURE);
  39818. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  39819. WOLFSSL_FAILURE);
  39820. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  39821. WOLFSSL_FAILURE);
  39822. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  39823. WOLFSSL_FAILURE);
  39824. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  39825. WOLFSSL_SUCCESS);
  39826. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  39827. WOLFSSL_SUCCESS);
  39828. wolfSSL_CTX_free(ctx);
  39829. #endif
  39830. return EXPECT_RESULT();
  39831. }
  39832. #ifndef NO_BIO
  39833. static int test_wolfSSL_d2i_PUBKEY(void)
  39834. {
  39835. EXPECT_DECLS;
  39836. #if defined(OPENSSL_EXTRA)
  39837. BIO* bio = NULL;
  39838. EVP_PKEY* pkey = NULL;
  39839. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39840. ExpectNull(d2i_PUBKEY_bio(NULL, NULL));
  39841. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  39842. /* RSA PUBKEY test */
  39843. ExpectIntGT(BIO_write(bio, client_keypub_der_2048,
  39844. sizeof_client_keypub_der_2048), 0);
  39845. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  39846. EVP_PKEY_free(pkey);
  39847. pkey = NULL;
  39848. #endif
  39849. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  39850. /* ECC PUBKEY test */
  39851. ExpectIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  39852. sizeof_ecc_clikeypub_der_256), 0);
  39853. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  39854. EVP_PKEY_free(pkey);
  39855. pkey = NULL;
  39856. #endif
  39857. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  39858. /* DSA PUBKEY test */
  39859. ExpectIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  39860. sizeof_dsa_pub_key_der_2048), 0);
  39861. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  39862. EVP_PKEY_free(pkey);
  39863. pkey = NULL;
  39864. #endif
  39865. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  39866. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  39867. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  39868. (HAVE_FIPS_VERSION > 2))
  39869. /* DH PUBKEY test */
  39870. ExpectIntGT(BIO_write(bio, dh_pub_key_der_2048,
  39871. sizeof_dh_pub_key_der_2048), 0);
  39872. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  39873. EVP_PKEY_free(pkey);
  39874. pkey = NULL;
  39875. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  39876. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  39877. BIO_free(bio);
  39878. (void)pkey;
  39879. #endif
  39880. return EXPECT_RESULT();
  39881. }
  39882. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  39883. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  39884. {
  39885. EXPECT_DECLS;
  39886. BIO* bio = NULL;
  39887. EVP_PKEY* pkey = NULL;
  39888. WOLFSSL_CTX* ctx = NULL;
  39889. #if defined(WOLFSSL_KEY_GEN)
  39890. unsigned char buff[4096];
  39891. unsigned char* bufPtr = buff;
  39892. #endif
  39893. /* test creating new EVP_PKEY with bad arg */
  39894. ExpectNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  39895. /* test loading RSA key using BIO */
  39896. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  39897. {
  39898. XFILE file = XBADFILE;
  39899. const char* fname = "./certs/server-key.der";
  39900. size_t sz;
  39901. byte* buf = NULL;
  39902. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  39903. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  39904. ExpectTrue((sz = XFTELL(file)) != 0);
  39905. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  39906. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  39907. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  39908. if (file != XBADFILE) {
  39909. XFCLOSE(file);
  39910. }
  39911. /* Test using BIO new mem and loading DER private key */
  39912. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  39913. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  39914. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  39915. BIO_free(bio);
  39916. bio = NULL;
  39917. EVP_PKEY_free(pkey);
  39918. pkey = NULL;
  39919. }
  39920. #endif
  39921. /* test loading ECC key using BIO */
  39922. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  39923. {
  39924. XFILE file = XBADFILE;
  39925. const char* fname = "./certs/ecc-key.der";
  39926. size_t sz;
  39927. byte* buf = NULL;
  39928. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  39929. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  39930. ExpectTrue((sz = XFTELL(file)) != 0);
  39931. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  39932. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  39933. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  39934. if (file != XBADFILE)
  39935. XFCLOSE(file);
  39936. /* Test using BIO new mem and loading DER private key */
  39937. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  39938. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  39939. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  39940. BIO_free(bio);
  39941. bio = NULL;
  39942. EVP_PKEY_free(pkey);
  39943. pkey = NULL;
  39944. }
  39945. #endif
  39946. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39947. #ifndef NO_WOLFSSL_SERVER
  39948. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  39949. #else
  39950. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  39951. #endif
  39952. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  39953. !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  39954. {
  39955. RSA* rsa = NULL;
  39956. /* Tests bad parameters */
  39957. ExpectNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  39958. /* RSA not set yet, expecting to fail*/
  39959. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  39960. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  39961. /* set RSA using bio*/
  39962. ExpectIntGT(BIO_write(bio, client_key_der_2048,
  39963. sizeof_client_key_der_2048), 0);
  39964. ExpectNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  39965. ExpectNotNull(rsa);
  39966. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  39967. /* i2d RSAprivate key tests */
  39968. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  39969. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  39970. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  39971. sizeof_client_key_der_2048);
  39972. bufPtr -= sizeof_client_key_der_2048;
  39973. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  39974. sizeof_client_key_der_2048), 0);
  39975. bufPtr = NULL;
  39976. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  39977. sizeof_client_key_der_2048);
  39978. ExpectNotNull(bufPtr);
  39979. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  39980. sizeof_client_key_der_2048), 0);
  39981. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  39982. RSA_free(rsa);
  39983. rsa = RSA_new();
  39984. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  39985. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  39986. RSA_free(rsa);
  39987. }
  39988. #endif /* !HAVE_FAST_RSA && WOLFSSL_KEY_GEN && !NO_RSA && !HAVE_USER_RSA*/
  39989. SSL_CTX_free(ctx);
  39990. ctx = NULL;
  39991. BIO_free(bio);
  39992. bio = NULL;
  39993. return EXPECT_RESULT();
  39994. }
  39995. #endif /* OPENSSL_ALL || (WOLFSSL_ASIO && !NO_RSA) */
  39996. #endif /* !NO_BIO */
  39997. static int test_wolfSSL_sk_GENERAL_NAME(void)
  39998. {
  39999. EXPECT_DECLS;
  40000. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40001. !defined(NO_RSA)
  40002. X509* x509 = NULL;
  40003. GENERAL_NAME* gn = NULL;
  40004. unsigned char buf[4096];
  40005. const unsigned char* bufPt = NULL;
  40006. int bytes = 0;
  40007. int i;
  40008. int j;
  40009. XFILE f = XBADFILE;
  40010. STACK_OF(GENERAL_NAME)* sk = NULL;
  40011. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  40012. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  40013. if (f != XBADFILE)
  40014. XFCLOSE(f);
  40015. for (j = 0; j < 2; ++j) {
  40016. bufPt = buf;
  40017. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  40018. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  40019. NID_subject_alt_name, NULL, NULL));
  40020. ExpectIntEQ(sk_GENERAL_NAME_num(sk), 1);
  40021. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  40022. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  40023. if (gn != NULL) {
  40024. switch (gn->type) {
  40025. case GEN_DNS:
  40026. fprintf(stderr, "found type GEN_DNS\n");
  40027. break;
  40028. case GEN_EMAIL:
  40029. fprintf(stderr, "found type GEN_EMAIL\n");
  40030. break;
  40031. case GEN_URI:
  40032. fprintf(stderr, "found type GEN_URI\n");
  40033. break;
  40034. }
  40035. }
  40036. }
  40037. X509_free(x509);
  40038. x509 = NULL;
  40039. if (j == 0) {
  40040. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  40041. }
  40042. else {
  40043. /*
  40044. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  40045. * it was supposed to. This is a regression test for that bug.
  40046. */
  40047. GENERAL_NAMES_free(sk);
  40048. }
  40049. sk = NULL;
  40050. }
  40051. #endif
  40052. return EXPECT_RESULT();
  40053. }
  40054. static int test_wolfSSL_GENERAL_NAME_print(void)
  40055. {
  40056. EXPECT_DECLS;
  40057. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  40058. X509* x509 = NULL;
  40059. GENERAL_NAME* gn = NULL;
  40060. unsigned char buf[4096];
  40061. const unsigned char* bufPt = NULL;
  40062. int bytes;
  40063. XFILE f = XBADFILE;
  40064. STACK_OF(GENERAL_NAME)* sk = NULL;
  40065. BIO* out = NULL;
  40066. unsigned char outbuf[128];
  40067. X509_EXTENSION* ext = NULL;
  40068. AUTHORITY_INFO_ACCESS* aia = NULL;
  40069. ACCESS_DESCRIPTION* ad = NULL;
  40070. ASN1_IA5STRING *dnsname = NULL;
  40071. const unsigned char v4Addr[] = {192,168,53,1};
  40072. const unsigned char v6Addr[] =
  40073. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  40074. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  40075. const unsigned char email[] =
  40076. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  40077. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  40078. const char* dnsStr = "DNS:example.com";
  40079. const char* uriStr = "URI:http://127.0.0.1:22220";
  40080. const char* v4addStr = "IP Address:192.168.53.1";
  40081. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  40082. const char* emailStr = "email:info@wolfssl.com";
  40083. const char* othrStr = "othername:<unsupported>";
  40084. const char* x400Str = "X400Name:<unsupported>";
  40085. const char* ediStr = "EdiPartyName:<unsupported>";
  40086. /* BIO to output */
  40087. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  40088. /* test for NULL param */
  40089. gn = NULL;
  40090. ExpectIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  40091. ExpectIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  40092. ExpectIntEQ(GENERAL_NAME_print(out, NULL), 0);
  40093. /* test for GEN_DNS */
  40094. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  40095. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  40096. if (f != XBADFILE) {
  40097. XFCLOSE(f);
  40098. f = XBADFILE;
  40099. }
  40100. bufPt = buf;
  40101. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  40102. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  40103. NID_subject_alt_name, NULL, NULL));
  40104. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  40105. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40106. XMEMSET(outbuf, 0, sizeof(outbuf));
  40107. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40108. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  40109. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  40110. gn = NULL;
  40111. sk = NULL;
  40112. X509_free(x509);
  40113. x509 = NULL;
  40114. /* Lets test for setting as well. */
  40115. ExpectNotNull(gn = GENERAL_NAME_new());
  40116. ExpectNotNull(dnsname = ASN1_IA5STRING_new());
  40117. ExpectIntEQ(ASN1_STRING_set(dnsname, "example.com", -1), 1);
  40118. GENERAL_NAME_set0_value(gn, GEN_DNS, dnsname);
  40119. dnsname = NULL;
  40120. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40121. XMEMSET(outbuf, 0, sizeof(outbuf));
  40122. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40123. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  40124. GENERAL_NAME_free(gn);
  40125. /* test for GEN_URI */
  40126. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  40127. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40128. if (f != XBADFILE) {
  40129. XFCLOSE(f);
  40130. f = XBADFILE;
  40131. }
  40132. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  40133. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  40134. ext));
  40135. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0));
  40136. if (ad != NULL) {
  40137. gn = ad->location;
  40138. }
  40139. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40140. gn = NULL;
  40141. XMEMSET(outbuf,0,sizeof(outbuf));
  40142. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40143. ExpectIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  40144. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  40145. aia = NULL;
  40146. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  40147. ExpectNotNull(aia);
  40148. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  40149. aia = NULL;
  40150. X509_free(x509);
  40151. x509 = NULL;
  40152. /* test for GEN_IPADD */
  40153. /* ip v4 address */
  40154. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40155. if (gn != NULL) {
  40156. gn->type = GEN_IPADD;
  40157. if (gn->d.iPAddress != NULL) {
  40158. gn->d.iPAddress->length = sizeof(v4Addr);
  40159. }
  40160. }
  40161. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  40162. sizeof(v4Addr)), 1);
  40163. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40164. XMEMSET(outbuf,0,sizeof(outbuf));
  40165. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40166. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  40167. GENERAL_NAME_free(gn);
  40168. gn = NULL;
  40169. /* ip v6 address */
  40170. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40171. if (gn != NULL) {
  40172. gn->type = GEN_IPADD;
  40173. if (gn->d.iPAddress != NULL) {
  40174. gn->d.iPAddress->length = sizeof(v6Addr);
  40175. }
  40176. }
  40177. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  40178. sizeof(v6Addr)), 1);
  40179. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40180. XMEMSET(outbuf,0,sizeof(outbuf));
  40181. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40182. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  40183. GENERAL_NAME_free(gn);
  40184. gn = NULL;
  40185. /* test for GEN_EMAIL */
  40186. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40187. if (gn != NULL) {
  40188. gn->type = GEN_EMAIL;
  40189. if (gn->d.rfc822Name != NULL) {
  40190. gn->d.rfc822Name->length = sizeof(email);
  40191. }
  40192. }
  40193. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email, sizeof(email)),
  40194. 1);
  40195. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40196. XMEMSET(outbuf,0,sizeof(outbuf));
  40197. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40198. ExpectIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  40199. GENERAL_NAME_free(gn);
  40200. gn = NULL;
  40201. /* test for GEN_OTHERNAME */
  40202. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40203. if (gn != NULL) {
  40204. gn->type = GEN_OTHERNAME;
  40205. }
  40206. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40207. XMEMSET(outbuf,0,sizeof(outbuf));
  40208. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40209. ExpectIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  40210. GENERAL_NAME_free(gn);
  40211. gn = NULL;
  40212. /* test for GEN_X400 */
  40213. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40214. if (gn != NULL) {
  40215. gn->type = GEN_X400;
  40216. }
  40217. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40218. XMEMSET(outbuf,0,sizeof(outbuf));
  40219. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40220. ExpectIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  40221. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  40222. if (gn != NULL) {
  40223. gn->type = GEN_IA5;
  40224. }
  40225. GENERAL_NAME_free(gn);
  40226. gn = NULL;
  40227. /* test for GEN_EDIPARTY */
  40228. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40229. if (gn != NULL) {
  40230. gn->type = GEN_EDIPARTY;
  40231. }
  40232. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40233. XMEMSET(outbuf,0,sizeof(outbuf));
  40234. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40235. ExpectIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  40236. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  40237. if (gn != NULL) {
  40238. gn->type = GEN_IA5;
  40239. }
  40240. GENERAL_NAME_free(gn);
  40241. gn = NULL;
  40242. BIO_free(out);
  40243. #endif /* OPENSSL_ALL */
  40244. return EXPECT_RESULT();
  40245. }
  40246. static int test_wolfSSL_sk_DIST_POINT(void)
  40247. {
  40248. EXPECT_DECLS;
  40249. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40250. !defined(NO_RSA)
  40251. X509* x509 = NULL;
  40252. unsigned char buf[4096];
  40253. const unsigned char* bufPt;
  40254. int bytes;
  40255. int i;
  40256. int j;
  40257. XFILE f = XBADFILE;
  40258. DIST_POINT* dp;
  40259. DIST_POINT_NAME* dpn;
  40260. GENERAL_NAME* gn;
  40261. ASN1_IA5STRING* uri;
  40262. STACK_OF(DIST_POINT)* dps = NULL;
  40263. STACK_OF(GENERAL_NAME)* gns = NULL;
  40264. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  40265. ExpectTrue((f = XFOPEN(cliCertDerCrlDistPoint, "rb")) != XBADFILE);
  40266. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  40267. if (f != XBADFILE)
  40268. XFCLOSE(f);
  40269. bufPt = buf;
  40270. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  40271. ExpectNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  40272. NID_crl_distribution_points, NULL, NULL));
  40273. ExpectIntEQ(sk_DIST_POINT_num(dps), 1);
  40274. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  40275. ExpectNotNull(dp = sk_DIST_POINT_value(dps, i));
  40276. ExpectNotNull(dpn = dp->distpoint);
  40277. /* this should be type 0, fullname */
  40278. ExpectIntEQ(dpn->type, 0);
  40279. ExpectNotNull(gns = dp->distpoint->name.fullname);
  40280. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  40281. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  40282. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, j));
  40283. ExpectIntEQ(gn->type, GEN_URI);
  40284. ExpectNotNull(uri = gn->d.uniformResourceIdentifier);
  40285. ExpectNotNull(uri->data);
  40286. ExpectIntGT(uri->length, 0);
  40287. }
  40288. }
  40289. X509_free(x509);
  40290. CRL_DIST_POINTS_free(dps);
  40291. #endif
  40292. return EXPECT_RESULT();
  40293. }
  40294. static int test_wolfSSL_verify_mode(void)
  40295. {
  40296. EXPECT_DECLS;
  40297. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40298. WOLFSSL* ssl = NULL;
  40299. WOLFSSL_CTX* ctx = NULL;
  40300. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40301. ExpectNotNull(ssl = SSL_new(ctx));
  40302. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  40303. SSL_free(ssl);
  40304. ssl = NULL;
  40305. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  40306. ExpectNotNull(ssl = SSL_new(ctx));
  40307. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  40308. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  40309. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  40310. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  40311. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  40312. SSL_free(ssl);
  40313. ssl = NULL;
  40314. wolfSSL_CTX_set_verify(ctx,
  40315. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  40316. ExpectNotNull(ssl = SSL_new(ctx));
  40317. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  40318. ExpectIntEQ(SSL_get_verify_mode(ssl),
  40319. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  40320. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  40321. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  40322. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  40323. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  40324. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  40325. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  40326. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  40327. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  40328. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  40329. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  40330. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  40331. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  40332. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  40333. #endif
  40334. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  40335. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  40336. SSL_free(ssl);
  40337. SSL_CTX_free(ctx);
  40338. #endif
  40339. return EXPECT_RESULT();
  40340. }
  40341. static int test_wolfSSL_verify_depth(void)
  40342. {
  40343. EXPECT_DECLS;
  40344. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  40345. WOLFSSL* ssl = NULL;
  40346. WOLFSSL_CTX* ctx = NULL;
  40347. long depth;
  40348. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40349. ExpectIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  40350. ExpectNotNull(ssl = SSL_new(ctx));
  40351. ExpectIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  40352. SSL_free(ssl);
  40353. ssl = NULL;
  40354. SSL_CTX_set_verify_depth(ctx, -1);
  40355. ExpectIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  40356. SSL_CTX_set_verify_depth(ctx, 2);
  40357. ExpectIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  40358. ExpectNotNull(ssl = SSL_new(ctx));
  40359. ExpectIntEQ(2, SSL_get_verify_depth(ssl));
  40360. SSL_free(ssl);
  40361. SSL_CTX_free(ctx);
  40362. #endif
  40363. return EXPECT_RESULT();
  40364. }
  40365. static int test_wolfSSL_verify_result(void)
  40366. {
  40367. EXPECT_DECLS;
  40368. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  40369. defined(OPENSSL_ALL)) && !defined(NO_WOLFSSL_CLIENT)
  40370. WOLFSSL* ssl = NULL;
  40371. WOLFSSL_CTX* ctx = NULL;
  40372. long result = 0xDEADBEEF;
  40373. ExpectIntEQ(WOLFSSL_FAILURE, wolfSSL_get_verify_result(ssl));
  40374. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40375. ExpectNotNull(ssl = SSL_new(ctx));
  40376. wolfSSL_set_verify_result(ssl, result);
  40377. ExpectIntEQ(result, wolfSSL_get_verify_result(ssl));
  40378. SSL_free(ssl);
  40379. SSL_CTX_free(ctx);
  40380. #endif
  40381. return EXPECT_RESULT();
  40382. }
  40383. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  40384. static void sslMsgCb(int w, int version, int type, const void* buf,
  40385. size_t sz, SSL* ssl, void* arg)
  40386. {
  40387. int i;
  40388. unsigned char* pt = (unsigned char*)buf;
  40389. fprintf(stderr, "%s %d bytes of version %d , type %d : ",
  40390. (w)?"Writing":"Reading", (int)sz, version, type);
  40391. for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
  40392. fprintf(stderr, "\n");
  40393. (void)ssl;
  40394. (void)arg;
  40395. }
  40396. #endif /* OPENSSL_EXTRA */
  40397. static int test_wolfSSL_msg_callback(void)
  40398. {
  40399. EXPECT_DECLS;
  40400. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  40401. WOLFSSL* ssl = NULL;
  40402. WOLFSSL_CTX* ctx = NULL;
  40403. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40404. ExpectNotNull(ssl = SSL_new(ctx));
  40405. ExpectIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  40406. ExpectIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  40407. ExpectIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  40408. SSL_free(ssl);
  40409. SSL_CTX_free(ctx);
  40410. #endif
  40411. return EXPECT_RESULT();
  40412. }
  40413. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  40414. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  40415. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  40416. static void binary_dump(void *ptr, int size)
  40417. {
  40418. #ifdef WOLFSSL_EVP_PRINT
  40419. int i = 0;
  40420. unsigned char *p = (unsigned char *) ptr;
  40421. fprintf(stderr, "{");
  40422. while ((p != NULL) && (i < size)) {
  40423. if ((i % 8) == 0) {
  40424. fprintf(stderr, "\n");
  40425. fprintf(stderr, " ");
  40426. }
  40427. fprintf(stderr, "0x%02x, ", p[i]);
  40428. i++;
  40429. }
  40430. fprintf(stderr, "\n};\n");
  40431. #else
  40432. (void) ptr;
  40433. (void) size;
  40434. #endif
  40435. }
  40436. static int last_val = 0x0f;
  40437. static int check_result(unsigned char *data, int len)
  40438. {
  40439. int i;
  40440. for ( ; len; ) {
  40441. last_val = (last_val + 1) % 16;
  40442. for (i = 0; i < 16; len--, i++, data++)
  40443. if (*data != last_val) {
  40444. return -1;
  40445. }
  40446. }
  40447. return 0;
  40448. }
  40449. static int r_offset;
  40450. static int w_offset;
  40451. static void init_offset(void)
  40452. {
  40453. r_offset = 0;
  40454. w_offset = 0;
  40455. }
  40456. static void get_record(unsigned char *data, unsigned char *buf, int len)
  40457. {
  40458. XMEMCPY(buf, data+r_offset, len);
  40459. r_offset += len;
  40460. }
  40461. static void set_record(unsigned char *data, unsigned char *buf, int len)
  40462. {
  40463. XMEMCPY(data+w_offset, buf, len);
  40464. w_offset += len;
  40465. }
  40466. static void set_plain(unsigned char *plain, int rec)
  40467. {
  40468. int i, j;
  40469. unsigned char *p = plain;
  40470. #define BLOCKSZ 16
  40471. for (i=0; i<(rec/BLOCKSZ); i++) {
  40472. for (j=0; j<BLOCKSZ; j++)
  40473. *p++ = (i % 16);
  40474. }
  40475. }
  40476. #endif
  40477. static int test_wolfSSL_EVP_Cipher_extra(void)
  40478. {
  40479. EXPECT_DECLS;
  40480. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  40481. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  40482. /* aes128-cbc, keylen=16, ivlen=16 */
  40483. byte aes128_cbc_key[] = {
  40484. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  40485. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  40486. };
  40487. byte aes128_cbc_iv[] = {
  40488. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  40489. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  40490. };
  40491. /* teset data size table */
  40492. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  40493. int test_drive2[] = {8, 3, 8, 512, 0};
  40494. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  40495. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  40496. int test_drive_len[100];
  40497. int ret = 0;
  40498. EVP_CIPHER_CTX *evp = NULL;
  40499. int ilen = 0;
  40500. int klen = 0;
  40501. int i, j;
  40502. const EVP_CIPHER *type;
  40503. byte *iv;
  40504. byte *key;
  40505. int ivlen;
  40506. int keylen;
  40507. #define RECORDS 16
  40508. #define BUFFSZ 512
  40509. byte plain [BUFFSZ * RECORDS];
  40510. byte cipher[BUFFSZ * RECORDS];
  40511. byte inb[BUFFSZ];
  40512. byte outb[BUFFSZ+16];
  40513. int outl = 0;
  40514. int inl;
  40515. iv = aes128_cbc_iv;
  40516. ivlen = sizeof(aes128_cbc_iv);
  40517. key = aes128_cbc_key;
  40518. keylen = sizeof(aes128_cbc_key);
  40519. type = EVP_aes_128_cbc();
  40520. set_plain(plain, BUFFSZ * RECORDS);
  40521. SSL_library_init();
  40522. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  40523. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  40524. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  40525. klen = EVP_CIPHER_CTX_key_length(evp);
  40526. if (klen > 0 && keylen != klen) {
  40527. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  40528. }
  40529. ilen = EVP_CIPHER_CTX_iv_length(evp);
  40530. if (ilen > 0 && ivlen != ilen) {
  40531. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  40532. }
  40533. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  40534. for (j = 0; j<RECORDS; j++)
  40535. {
  40536. inl = BUFFSZ;
  40537. get_record(plain, inb, inl);
  40538. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  40539. set_record(cipher, outb, outl);
  40540. }
  40541. for (i = 0; test_drive[i]; i++) {
  40542. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  40543. init_offset();
  40544. test_drive_len[i] = 0;
  40545. for (j = 0; test_drive[i][j]; j++)
  40546. {
  40547. inl = test_drive[i][j];
  40548. test_drive_len[i] += inl;
  40549. get_record(plain, inb, inl);
  40550. ExpectIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)),
  40551. 0);
  40552. /* output to cipher buffer, so that following Dec test can detect
  40553. if any error */
  40554. set_record(cipher, outb, outl);
  40555. }
  40556. EVP_CipherFinal(evp, outb, &outl);
  40557. if (outl > 0)
  40558. set_record(cipher, outb, outl);
  40559. }
  40560. for (i = 0; test_drive[i]; i++) {
  40561. last_val = 0x0f;
  40562. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  40563. init_offset();
  40564. for (j = 0; test_drive[i][j]; j++) {
  40565. inl = test_drive[i][j];
  40566. get_record(cipher, inb, inl);
  40567. ExpectIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)),
  40568. 0);
  40569. binary_dump(outb, outl);
  40570. ExpectIntEQ((ret = check_result(outb, outl)), 0);
  40571. ExpectFalse(outl > ((inl/16+1)*16) && outl > 16);
  40572. }
  40573. ret = EVP_CipherFinal(evp, outb, &outl);
  40574. binary_dump(outb, outl);
  40575. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  40576. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  40577. ExpectTrue(ret);
  40578. }
  40579. EVP_CIPHER_CTX_free(evp);
  40580. evp = NULL;
  40581. /* Do an extra test to verify correct behavior with empty input. */
  40582. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  40583. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  40584. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  40585. klen = EVP_CIPHER_CTX_key_length(evp);
  40586. if (klen > 0 && keylen != klen) {
  40587. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  40588. }
  40589. ilen = EVP_CIPHER_CTX_iv_length(evp);
  40590. if (ilen > 0 && ivlen != ilen) {
  40591. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  40592. }
  40593. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  40594. /* outl should be set to 0 after passing NULL, 0 for input args. */
  40595. outl = -1;
  40596. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, NULL, 0)), 0);
  40597. ExpectIntEQ(outl, 0);
  40598. EVP_CIPHER_CTX_free(evp);
  40599. #endif /* test_EVP_Cipher */
  40600. return EXPECT_RESULT();
  40601. }
  40602. static int test_wolfSSL_PEM_read_DHparams(void)
  40603. {
  40604. EXPECT_DECLS;
  40605. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  40606. !defined(NO_FILESYSTEM)
  40607. DH* dh = NULL;
  40608. XFILE fp = XBADFILE;
  40609. unsigned char derOut[300];
  40610. unsigned char* derOutBuf = derOut;
  40611. int derOutSz = 0;
  40612. unsigned char derExpected[300];
  40613. int derExpectedSz = 0;
  40614. XMEMSET(derOut, 0, sizeof(derOut));
  40615. XMEMSET(derExpected, 0, sizeof(derExpected));
  40616. /* open DH param file, read into DH struct */
  40617. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  40618. /* bad args */
  40619. ExpectNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  40620. ExpectNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  40621. /* good args */
  40622. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  40623. if (fp != XBADFILE) {
  40624. XFCLOSE(fp);
  40625. fp = XBADFILE;
  40626. }
  40627. /* read in certs/dh2048.der for comparison against exported params */
  40628. ExpectTrue((fp = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  40629. ExpectIntGT(derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected),
  40630. fp), 0);
  40631. if (fp != XBADFILE) {
  40632. XFCLOSE(fp);
  40633. fp = XBADFILE;
  40634. }
  40635. /* export DH back to DER and compare */
  40636. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  40637. ExpectIntEQ(derOutSz, derExpectedSz);
  40638. ExpectIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  40639. DH_free(dh);
  40640. dh = NULL;
  40641. /* Test parsing with X9.42 header */
  40642. ExpectTrue((fp = XFOPEN("./certs/x942dh2048.pem", "rb")) != XBADFILE);
  40643. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  40644. if (fp != XBADFILE)
  40645. XFCLOSE(fp);
  40646. DH_free(dh);
  40647. #endif
  40648. return EXPECT_RESULT();
  40649. }
  40650. static int test_wolfSSL_X509_get_serialNumber(void)
  40651. {
  40652. EXPECT_DECLS;
  40653. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  40654. ASN1_INTEGER* a = NULL;
  40655. BIGNUM* bn = NULL;
  40656. X509* x509 = NULL;
  40657. char *serialHex = NULL;
  40658. byte serial[3];
  40659. int serialSz;
  40660. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  40661. SSL_FILETYPE_PEM));
  40662. ExpectNotNull(a = X509_get_serialNumber(x509));
  40663. /* check on value of ASN1 Integer */
  40664. ExpectNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  40665. a = NULL;
  40666. /* test setting serial number and then retrieving it */
  40667. ExpectNotNull(a = ASN1_INTEGER_new());
  40668. ExpectIntEQ(ASN1_INTEGER_set(a, 3), 1);
  40669. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  40670. serialSz = sizeof(serial);
  40671. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  40672. WOLFSSL_SUCCESS);
  40673. ExpectIntEQ(serialSz, 1);
  40674. ExpectIntEQ(serial[0], 3);
  40675. ASN1_INTEGER_free(a);
  40676. a = NULL;
  40677. /* test setting serial number with 0's in it */
  40678. serial[0] = 0x01;
  40679. serial[1] = 0x00;
  40680. serial[2] = 0x02;
  40681. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  40682. if (a != NULL) {
  40683. a->data[0] = ASN_INTEGER;
  40684. a->data[1] = sizeof(serial);
  40685. XMEMCPY(&a->data[2], serial, sizeof(serial));
  40686. a->length = sizeof(serial) + 2;
  40687. }
  40688. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  40689. XMEMSET(serial, 0, sizeof(serial));
  40690. serialSz = sizeof(serial);
  40691. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  40692. WOLFSSL_SUCCESS);
  40693. ExpectIntEQ(serialSz, 3);
  40694. ExpectIntEQ(serial[0], 0x01);
  40695. ExpectIntEQ(serial[1], 0x00);
  40696. ExpectIntEQ(serial[2], 0x02);
  40697. ASN1_INTEGER_free(a);
  40698. a = NULL;
  40699. X509_free(x509); /* free's a */
  40700. ExpectNotNull(serialHex = BN_bn2hex(bn));
  40701. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  40702. ExpectStrEQ(serialHex, "01");
  40703. #else
  40704. ExpectStrEQ(serialHex, "1");
  40705. #endif
  40706. OPENSSL_free(serialHex);
  40707. ExpectIntEQ(BN_get_word(bn), 1);
  40708. BN_free(bn);
  40709. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  40710. ExpectNotNull(a = ASN1_INTEGER_new());
  40711. if (a != NULL) {
  40712. ExpectNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  40713. DYNAMIC_TYPE_OPENSSL));
  40714. a->isDynamic = 1;
  40715. ASN1_INTEGER_free(a);
  40716. }
  40717. #endif
  40718. return EXPECT_RESULT();
  40719. }
  40720. static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
  40721. {
  40722. EXPECT_DECLS;
  40723. #if defined(OPENSSL_EXTRA)
  40724. ExpectIntEQ(wolfSSL_add_all_algorithms(), WOLFSSL_SUCCESS);
  40725. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(), WOLFSSL_SUCCESS);
  40726. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(), WOLFSSL_SUCCESS);
  40727. #endif
  40728. return EXPECT_RESULT();
  40729. }
  40730. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  40731. {
  40732. EXPECT_DECLS;
  40733. #if defined(OPENSSL_EXTRA)
  40734. #define MAX_HEXSTR_BUFSZ 9
  40735. #define NUM_CASES 5
  40736. struct Output {
  40737. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  40738. long ret;
  40739. };
  40740. int i;
  40741. int j;
  40742. const char* inputs[NUM_CASES] = {
  40743. "aabcd1357e",
  40744. "01:12:23:34:a5:b6:c7:d8:e9",
  40745. ":01:02",
  40746. "012",
  40747. ":ab:ac:d"
  40748. };
  40749. struct Output expectedOutputs[NUM_CASES] = {
  40750. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  40751. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  40752. {{0x01, 0x02}, 2},
  40753. {{0x00}, 0},
  40754. {{0x00}, 0}
  40755. };
  40756. long len = 0;
  40757. unsigned char* returnedBuf = NULL;
  40758. for (i = 0; i < NUM_CASES && EXPECT_SUCCESS(); ++i) {
  40759. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  40760. if (returnedBuf == NULL) {
  40761. ExpectIntEQ(expectedOutputs[i].ret, 0);
  40762. continue;
  40763. }
  40764. ExpectIntEQ(expectedOutputs[i].ret, len);
  40765. for (j = 0; j < len; ++j) {
  40766. ExpectIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  40767. }
  40768. OPENSSL_free(returnedBuf);
  40769. returnedBuf = NULL;
  40770. }
  40771. #endif
  40772. return EXPECT_RESULT();
  40773. }
  40774. static int test_wolfSSL_X509_CA_num(void)
  40775. {
  40776. EXPECT_DECLS;
  40777. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  40778. defined(HAVE_ECC) && !defined(NO_RSA)
  40779. WOLFSSL_X509_STORE *store = NULL;
  40780. WOLFSSL_X509 *x509_1 = NULL;
  40781. WOLFSSL_X509 *x509_2 = NULL;
  40782. int ca_num = 0;
  40783. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  40784. ExpectNotNull(x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile,
  40785. WOLFSSL_FILETYPE_PEM));
  40786. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_1), 1);
  40787. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 1);
  40788. ExpectNotNull(x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile,
  40789. WOLFSSL_FILETYPE_PEM));
  40790. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_2), 1);
  40791. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 2);
  40792. wolfSSL_X509_free(x509_1);
  40793. wolfSSL_X509_free(x509_2);
  40794. wolfSSL_X509_STORE_free(store);
  40795. #endif
  40796. return EXPECT_RESULT();
  40797. }
  40798. static int test_wolfSSL_X509_check_ca(void)
  40799. {
  40800. EXPECT_DECLS;
  40801. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  40802. WOLFSSL_X509 *x509 = NULL;
  40803. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  40804. WOLFSSL_FILETYPE_PEM));
  40805. ExpectIntEQ(wolfSSL_X509_check_ca(x509), 1);
  40806. wolfSSL_X509_free(x509);
  40807. #endif
  40808. return EXPECT_RESULT();
  40809. }
  40810. static int test_wolfSSL_X509_check_ip_asc(void)
  40811. {
  40812. EXPECT_DECLS;
  40813. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  40814. WOLFSSL_X509 *x509 = NULL;
  40815. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  40816. WOLFSSL_FILETYPE_PEM));
  40817. #if 0
  40818. /* TODO: add cert gen for testing positive case */
  40819. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  40820. #endif
  40821. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  40822. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  40823. wolfSSL_X509_free(x509);
  40824. #endif
  40825. return EXPECT_RESULT();
  40826. }
  40827. static int test_wolfSSL_make_cert(void)
  40828. {
  40829. EXPECT_DECLS;
  40830. #if !defined(NO_RSA) && !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && \
  40831. defined(WOLFSSL_CERT_EXT)
  40832. int ret;
  40833. Cert cert;
  40834. CertName name;
  40835. RsaKey key;
  40836. WC_RNG rng;
  40837. byte der[FOURK_BUF];
  40838. word32 idx;
  40839. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  40840. #ifdef OPENSSL_EXTRA
  40841. const unsigned char* pt;
  40842. int certSz;
  40843. X509* x509 = NULL;
  40844. X509_NAME* x509name;
  40845. X509_NAME_ENTRY* entry;
  40846. ASN1_STRING* entryValue;
  40847. #endif
  40848. XMEMSET(&name, 0, sizeof(CertName));
  40849. /* set up cert name */
  40850. XMEMCPY(name.country, "US", sizeof("US"));
  40851. name.countryEnc = CTC_PRINTABLE;
  40852. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  40853. name.stateEnc = CTC_UTF8;
  40854. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  40855. name.localityEnc = CTC_UTF8;
  40856. XMEMCPY(name.sur, "Test", sizeof("Test"));
  40857. name.surEnc = CTC_UTF8;
  40858. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  40859. name.orgEnc = CTC_UTF8;
  40860. XMEMCPY(name.unit, "Development", sizeof("Development"));
  40861. name.unitEnc = CTC_UTF8;
  40862. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  40863. name.commonNameEnc = CTC_UTF8;
  40864. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  40865. name.serialDevEnc = CTC_PRINTABLE;
  40866. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  40867. name.userIdEnc = CTC_PRINTABLE;
  40868. #ifdef WOLFSSL_MULTI_ATTRIB
  40869. #if CTC_MAX_ATTRIB > 2
  40870. {
  40871. NameAttrib* n;
  40872. n = &name.name[0];
  40873. n->id = ASN_DOMAIN_COMPONENT;
  40874. n->type = CTC_UTF8;
  40875. n->sz = sizeof("com");
  40876. XMEMCPY(n->value, "com", sizeof("com"));
  40877. n = &name.name[1];
  40878. n->id = ASN_DOMAIN_COMPONENT;
  40879. n->type = CTC_UTF8;
  40880. n->sz = sizeof("wolfssl");
  40881. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  40882. }
  40883. #endif
  40884. #endif /* WOLFSSL_MULTI_ATTRIB */
  40885. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  40886. #ifndef HAVE_FIPS
  40887. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  40888. #else
  40889. ExpectIntEQ(wc_InitRng(&rng), 0);
  40890. #endif
  40891. /* load test RSA key */
  40892. idx = 0;
  40893. #if defined(USE_CERT_BUFFERS_1024)
  40894. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  40895. sizeof_server_key_der_1024), 0);
  40896. #elif defined(USE_CERT_BUFFERS_2048)
  40897. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  40898. sizeof_server_key_der_2048), 0);
  40899. #else
  40900. /* error case, no RSA key loaded, happens later */
  40901. (void)idx;
  40902. #endif
  40903. XMEMSET(&cert, 0 , sizeof(Cert));
  40904. ExpectIntEQ(wc_InitCert(&cert), 0);
  40905. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  40906. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  40907. cert.serialSz = (int)sizeof(mySerial);
  40908. cert.isCA = 1;
  40909. #ifndef NO_SHA256
  40910. cert.sigType = CTC_SHA256wRSA;
  40911. #else
  40912. cert.sigType = CTC_SHAwRSA;
  40913. #endif
  40914. /* add SKID from the Public Key */
  40915. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  40916. /* add AKID from the Public Key */
  40917. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  40918. ret = 0;
  40919. do {
  40920. #if defined(WOLFSSL_ASYNC_CRYPT)
  40921. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  40922. #endif
  40923. if (ret >= 0) {
  40924. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  40925. }
  40926. } while (ret == WC_PENDING_E);
  40927. ExpectIntGT(ret, 0);
  40928. #ifdef OPENSSL_EXTRA
  40929. /* der holds a certificate with DC's now check X509 parsing of it */
  40930. certSz = ret;
  40931. pt = der;
  40932. ExpectNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  40933. ExpectNotNull(x509name = X509_get_subject_name(x509));
  40934. #ifdef WOLFSSL_MULTI_ATTRIB
  40935. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  40936. -1)), 5);
  40937. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  40938. idx)), 6);
  40939. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  40940. idx)), -1);
  40941. #endif /* WOLFSSL_MULTI_ATTRIB */
  40942. /* compare DN at index 0 */
  40943. ExpectNotNull(entry = X509_NAME_get_entry(x509name, 0));
  40944. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  40945. ExpectIntEQ(ASN1_STRING_length(entryValue), 2);
  40946. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  40947. #ifndef WOLFSSL_MULTI_ATTRIB
  40948. /* compare Serial Number */
  40949. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_serialNumber,
  40950. -1)), 7);
  40951. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  40952. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  40953. ExpectIntEQ(ASN1_STRING_length(entryValue), XSTRLEN("wolfSSL12345"));
  40954. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "wolfSSL12345");
  40955. #endif
  40956. #ifdef WOLFSSL_MULTI_ATTRIB
  40957. /* get first and second DC and compare result */
  40958. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  40959. -1)), 5);
  40960. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  40961. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  40962. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  40963. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  40964. idx)), 6);
  40965. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  40966. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  40967. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  40968. #endif /* WOLFSSL_MULTI_ATTRIB */
  40969. /* try invalid index locations for regression test and sanity check */
  40970. ExpectNull(entry = X509_NAME_get_entry(x509name, 11));
  40971. ExpectNull(entry = X509_NAME_get_entry(x509name, 20));
  40972. X509_free(x509);
  40973. #endif /* OPENSSL_EXTRA */
  40974. wc_FreeRsaKey(&key);
  40975. wc_FreeRng(&rng);
  40976. #endif
  40977. return EXPECT_RESULT();
  40978. }
  40979. static int test_wolfSSL_X509_get_version(void)
  40980. {
  40981. EXPECT_DECLS;
  40982. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  40983. WOLFSSL_X509 *x509 = NULL;
  40984. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  40985. WOLFSSL_FILETYPE_PEM));
  40986. ExpectIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  40987. wolfSSL_X509_free(x509);
  40988. #endif
  40989. return EXPECT_RESULT();
  40990. }
  40991. #if defined(OPENSSL_ALL)
  40992. static int test_wolfSSL_sk_CIPHER_description(void)
  40993. {
  40994. EXPECT_DECLS;
  40995. #if !defined(NO_RSA)
  40996. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  40997. int i;
  40998. int numCiphers = 0;
  40999. const SSL_METHOD *method = NULL;
  41000. const SSL_CIPHER *cipher = NULL;
  41001. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  41002. SSL_CTX *ctx = NULL;
  41003. SSL *ssl = NULL;
  41004. char buf[256];
  41005. char test_str[9] = "0000000";
  41006. const char badStr[] = "unknown";
  41007. const char certPath[] = "./certs/client-cert.pem";
  41008. XMEMSET(buf, 0, sizeof(buf));
  41009. ExpectNotNull(method = TLSv1_2_client_method());
  41010. ExpectNotNull(ctx = SSL_CTX_new(method));
  41011. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  41012. SSL_CTX_set_verify_depth(ctx, 4);
  41013. SSL_CTX_set_options(ctx, flags);
  41014. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  41015. WOLFSSL_SUCCESS);
  41016. ExpectNotNull(ssl = SSL_new(ctx));
  41017. /* SSL_get_ciphers returns a stack of all configured ciphers
  41018. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  41019. */
  41020. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  41021. /* loop through the amount of supportedCiphers */
  41022. numCiphers = sk_num(supportedCiphers);
  41023. for (i = 0; i < numCiphers; ++i) {
  41024. int j;
  41025. /* sk_value increments "sk->data.cipher->cipherOffset".
  41026. * wolfSSL_sk_CIPHER_description sets the description for
  41027. * the cipher based on the provided offset.
  41028. */
  41029. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  41030. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  41031. }
  41032. /* Search cipher description string for "unknown" descriptor */
  41033. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  41034. int k = 0;
  41035. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  41036. test_str[k] = badStr[k];
  41037. j++;
  41038. k++;
  41039. }
  41040. }
  41041. /* Fail if test_str == badStr == "unknown" */
  41042. ExpectStrNE(test_str,badStr);
  41043. }
  41044. SSL_free(ssl);
  41045. SSL_CTX_free(ctx);
  41046. #endif
  41047. return EXPECT_RESULT();
  41048. }
  41049. static int test_wolfSSL_get_ciphers_compat(void)
  41050. {
  41051. EXPECT_DECLS;
  41052. #if !defined(NO_RSA)
  41053. const SSL_METHOD *method = NULL;
  41054. const char certPath[] = "./certs/client-cert.pem";
  41055. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  41056. SSL_CTX *ctx = NULL;
  41057. WOLFSSL *ssl = NULL;
  41058. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  41059. ExpectNotNull(method = SSLv23_client_method());
  41060. ExpectNotNull(ctx = SSL_CTX_new(method));
  41061. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  41062. SSL_CTX_set_verify_depth(ctx, 4);
  41063. SSL_CTX_set_options(ctx, flags);
  41064. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  41065. WOLFSSL_SUCCESS);
  41066. ExpectNotNull(ssl = SSL_new(ctx));
  41067. /* Test Bad NULL input */
  41068. ExpectNull(supportedCiphers = SSL_get_ciphers(NULL));
  41069. /* Test for Good input */
  41070. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  41071. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  41072. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  41073. SSL_free(ssl);
  41074. SSL_CTX_free(ctx);
  41075. #endif
  41076. return EXPECT_RESULT();
  41077. }
  41078. static int test_wolfSSL_X509_PUBKEY_get(void)
  41079. {
  41080. EXPECT_DECLS;
  41081. WOLFSSL_X509_PUBKEY pubkey;
  41082. WOLFSSL_X509_PUBKEY* key;
  41083. WOLFSSL_EVP_PKEY evpkey ;
  41084. WOLFSSL_EVP_PKEY* evpPkey;
  41085. WOLFSSL_EVP_PKEY* retEvpPkey;
  41086. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  41087. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  41088. key = &pubkey;
  41089. evpPkey = &evpkey;
  41090. evpPkey->type = WOLFSSL_SUCCESS;
  41091. key->pkey = evpPkey;
  41092. ExpectNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  41093. ExpectIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  41094. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  41095. key->pkey = NULL;
  41096. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  41097. return EXPECT_RESULT();
  41098. }
  41099. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  41100. {
  41101. EXPECT_DECLS;
  41102. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  41103. DSA *dsa = NULL;
  41104. DSA *setDsa = NULL;
  41105. EVP_PKEY *pkey = NULL;
  41106. EVP_PKEY *set1Pkey = NULL;
  41107. SHA_CTX sha;
  41108. byte signature[DSA_SIG_SIZE];
  41109. byte hash[WC_SHA_DIGEST_SIZE];
  41110. word32 bytes;
  41111. int answer;
  41112. #ifdef USE_CERT_BUFFERS_1024
  41113. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  41114. int dsaKeySz = sizeof_dsa_key_der_1024;
  41115. byte tmp[ONEK_BUF];
  41116. XMEMSET(tmp, 0, sizeof(tmp));
  41117. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  41118. bytes = dsaKeySz;
  41119. #elif defined(USE_CERT_BUFFERS_2048)
  41120. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  41121. int dsaKeySz = sizeof_dsa_key_der_2048;
  41122. byte tmp[TWOK_BUF];
  41123. XMEMSET(tmp, 0, sizeof(tmp));
  41124. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  41125. bytes = dsaKeySz;
  41126. #else
  41127. byte tmp[TWOK_BUF];
  41128. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  41129. int dsaKeySz;
  41130. XFILE fp = XBADFILE;
  41131. XMEMSET(tmp, 0, sizeof(tmp));
  41132. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  41133. ExpectIntGT(dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  41134. if (fp != XBADFILE)
  41135. XFCLOSE(fp);
  41136. #endif /* END USE_CERT_BUFFERS_1024 */
  41137. /* Create hash to later Sign and Verify */
  41138. ExpectIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  41139. ExpectIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  41140. ExpectIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  41141. /* Initialize pkey with der format dsa key */
  41142. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey, &dsaKeyDer,
  41143. (long)dsaKeySz));
  41144. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  41145. /* Should Fail: NULL argument */
  41146. ExpectNull(dsa = EVP_PKEY_get0_DSA(NULL));
  41147. ExpectNull(dsa = EVP_PKEY_get1_DSA(NULL));
  41148. /* Should Pass: Initialized pkey argument */
  41149. ExpectNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  41150. ExpectNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  41151. #ifdef USE_CERT_BUFFERS_1024
  41152. ExpectIntEQ(DSA_bits(dsa), 1024);
  41153. #else
  41154. ExpectIntEQ(DSA_bits(dsa), 2048);
  41155. #endif
  41156. /* Sign */
  41157. ExpectIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  41158. /* Verify. */
  41159. ExpectIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  41160. WOLFSSL_SUCCESS);
  41161. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  41162. /* Should Fail: set1Pkey not initialized */
  41163. ExpectIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  41164. /* Initialize set1Pkey */
  41165. set1Pkey = EVP_PKEY_new();
  41166. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  41167. ExpectIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  41168. WOLFSSL_SUCCESS);
  41169. /* Should Pass: set dsa into set1Pkey */
  41170. ExpectIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  41171. DSA_free(dsa);
  41172. DSA_free(setDsa);
  41173. EVP_PKEY_free(pkey);
  41174. EVP_PKEY_free(set1Pkey);
  41175. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  41176. return EXPECT_RESULT();
  41177. } /* END test_EVP_PKEY_set1_get1_DSA */
  41178. static int test_wolfSSL_DSA_generate_parameters(void)
  41179. {
  41180. EXPECT_DECLS;
  41181. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  41182. !defined(HAVE_FIPS)
  41183. DSA *dsa = NULL;
  41184. ExpectNotNull(dsa = DSA_generate_parameters(2048, NULL, 0, NULL, NULL, NULL,
  41185. NULL));
  41186. DSA_free(dsa);
  41187. #endif
  41188. return EXPECT_RESULT();
  41189. }
  41190. static int test_wolfSSL_DSA_SIG(void)
  41191. {
  41192. EXPECT_DECLS;
  41193. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  41194. !defined(HAVE_FIPS)
  41195. DSA *dsa = NULL;
  41196. DSA *dsa2 = NULL;
  41197. DSA_SIG *sig = NULL;
  41198. const BIGNUM *p = NULL;
  41199. const BIGNUM *q = NULL;
  41200. const BIGNUM *g = NULL;
  41201. const BIGNUM *pub = NULL;
  41202. const BIGNUM *priv = NULL;
  41203. BIGNUM *dup_p = NULL;
  41204. BIGNUM *dup_q = NULL;
  41205. BIGNUM *dup_g = NULL;
  41206. BIGNUM *dup_pub = NULL;
  41207. BIGNUM *dup_priv = NULL;
  41208. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  41209. ExpectNotNull(dsa = DSA_new());
  41210. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048, NULL, 0, NULL, NULL,
  41211. NULL), 1);
  41212. ExpectIntEQ(DSA_generate_key(dsa), 1);
  41213. DSA_get0_pqg(dsa, &p, &q, &g);
  41214. DSA_get0_key(dsa, &pub, &priv);
  41215. ExpectNotNull(dup_p = BN_dup(p));
  41216. ExpectNotNull(dup_q = BN_dup(q));
  41217. ExpectNotNull(dup_g = BN_dup(g));
  41218. ExpectNotNull(dup_pub = BN_dup(pub));
  41219. ExpectNotNull(dup_priv = BN_dup(priv));
  41220. ExpectNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  41221. ExpectNotNull(dsa2 = DSA_new());
  41222. ExpectIntEQ(DSA_set0_pqg(dsa2, dup_p, dup_q, dup_g), 1);
  41223. if (EXPECT_FAIL()) {
  41224. BN_free(dup_p);
  41225. BN_free(dup_q);
  41226. BN_free(dup_g);
  41227. }
  41228. ExpectIntEQ(DSA_set0_key(dsa2, dup_pub, dup_priv), 1);
  41229. if (EXPECT_FAIL()) {
  41230. BN_free(dup_pub);
  41231. BN_free(dup_priv);
  41232. }
  41233. ExpectIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  41234. DSA_free(dsa);
  41235. DSA_free(dsa2);
  41236. DSA_SIG_free(sig);
  41237. #endif
  41238. return EXPECT_RESULT();
  41239. }
  41240. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  41241. {
  41242. EXPECT_DECLS;
  41243. #ifdef HAVE_ECC
  41244. WOLFSSL_EC_KEY* ecKey = NULL;
  41245. WOLFSSL_EC_KEY* ecGet1 = NULL;
  41246. EVP_PKEY* pkey = NULL;
  41247. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  41248. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41249. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  41250. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  41251. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  41252. /* Should fail since ecKey is empty */
  41253. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  41254. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  41255. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  41256. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  41257. ExpectNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  41258. ExpectNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  41259. wolfSSL_EC_KEY_free(ecKey);
  41260. wolfSSL_EC_KEY_free(ecGet1);
  41261. EVP_PKEY_free(pkey);
  41262. #endif /* HAVE_ECC */
  41263. return EXPECT_RESULT();
  41264. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  41265. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  41266. {
  41267. EXPECT_DECLS;
  41268. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  41269. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41270. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  41271. DH *dh = NULL;
  41272. DH *setDh = NULL;
  41273. EVP_PKEY *pkey = NULL;
  41274. XFILE f = XBADFILE;
  41275. unsigned char buf[4096];
  41276. const unsigned char* pt = buf;
  41277. const char* dh2048 = "./certs/dh2048.der";
  41278. long len = 0;
  41279. int code = -1;
  41280. XMEMSET(buf, 0, sizeof(buf));
  41281. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  41282. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  41283. if (f != XBADFILE)
  41284. XFCLOSE(f);
  41285. /* Load dh2048.der into DH with internal format */
  41286. ExpectNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  41287. ExpectIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  41288. ExpectIntEQ(code, 0);
  41289. code = -1;
  41290. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41291. /* Set DH into PKEY */
  41292. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  41293. /* Get DH from PKEY */
  41294. ExpectNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  41295. ExpectIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  41296. ExpectIntEQ(code, 0);
  41297. EVP_PKEY_free(pkey);
  41298. DH_free(setDh);
  41299. DH_free(dh);
  41300. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  41301. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  41302. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  41303. return EXPECT_RESULT();
  41304. } /* END test_EVP_PKEY_set1_get1_DH */
  41305. static int test_wolfSSL_CTX_ctrl(void)
  41306. {
  41307. EXPECT_DECLS;
  41308. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  41309. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  41310. char caFile[] = "./certs/client-ca.pem";
  41311. char clientFile[] = "./certs/client-cert.pem";
  41312. SSL_CTX* ctx = NULL;
  41313. X509* x509 = NULL;
  41314. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  41315. byte buf[6000];
  41316. char file[] = "./certs/dsaparams.pem";
  41317. XFILE f = XBADFILE;
  41318. int bytes;
  41319. BIO* bio = NULL;
  41320. DSA* dsa = NULL;
  41321. DH* dh = NULL;
  41322. #endif
  41323. #ifdef HAVE_ECC
  41324. WOLFSSL_EC_KEY* ecKey = NULL;
  41325. #endif
  41326. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  41327. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  41328. WOLFSSL_FILETYPE_PEM));
  41329. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  41330. if (EXPECT_FAIL()) {
  41331. wolfSSL_X509_free(x509);
  41332. }
  41333. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  41334. WOLFSSL_FILETYPE_PEM));
  41335. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  41336. /* Initialize DH */
  41337. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  41338. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  41339. if (f != XBADFILE)
  41340. XFCLOSE(f);
  41341. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  41342. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  41343. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  41344. #endif
  41345. #ifdef HAVE_ECC
  41346. /* Initialize WOLFSSL_EC_KEY */
  41347. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  41348. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  41349. #endif
  41350. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  41351. /* additional test of getting EVP_PKEY key size from X509
  41352. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  41353. * allowed with user RSA */
  41354. {
  41355. EVP_PKEY* pkey = NULL;
  41356. #if defined(HAVE_ECC)
  41357. X509* ecX509 = NULL;
  41358. #endif /* HAVE_ECC */
  41359. ExpectNotNull(pkey = X509_get_pubkey(x509));
  41360. /* current RSA key is 2048 bit (256 bytes) */
  41361. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  41362. EVP_PKEY_free(pkey);
  41363. pkey = NULL;
  41364. #if defined(HAVE_ECC)
  41365. #if defined(USE_CERT_BUFFERS_256)
  41366. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  41367. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  41368. SSL_FILETYPE_ASN1));
  41369. #else
  41370. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  41371. cliEccCertFile, SSL_FILETYPE_PEM));
  41372. #endif
  41373. ExpectNotNull(pkey = X509_get_pubkey(ecX509));
  41374. /* current ECC key is 256 bit (32 bytes) */
  41375. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  41376. X509_free(ecX509);
  41377. EVP_PKEY_free(pkey);
  41378. #endif /* HAVE_ECC */
  41379. }
  41380. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  41381. /* Tests should fail with passed in NULL pointer */
  41382. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, NULL),
  41383. SSL_FAILURE);
  41384. #if !defined(NO_DH) && !defined(NO_DSA)
  41385. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, NULL),
  41386. SSL_FAILURE);
  41387. #endif
  41388. #ifdef HAVE_ECC
  41389. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, NULL),
  41390. SSL_FAILURE);
  41391. #endif
  41392. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  41393. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  41394. */
  41395. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, x509),
  41396. SSL_SUCCESS);
  41397. if (EXPECT_FAIL()) {
  41398. wolfSSL_X509_free(x509);
  41399. }
  41400. /* Test with SSL_CTRL_OPTIONS
  41401. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  41402. */
  41403. ExpectTrue(wolfSSL_CTX_ctrl(ctx, SSL_CTRL_OPTIONS, SSL_OP_NO_TLSv1,
  41404. NULL) == SSL_OP_NO_TLSv1);
  41405. ExpectTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  41406. /* Test with SSL_CTRL_SET_TMP_DH
  41407. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  41408. */
  41409. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  41410. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, dh),
  41411. SSL_SUCCESS);
  41412. #endif
  41413. /* Test with SSL_CTRL_SET_TMP_ECDH
  41414. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  41415. */
  41416. #ifdef HAVE_ECC
  41417. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, ecKey),
  41418. SSL_SUCCESS);
  41419. #endif
  41420. #ifdef WOLFSSL_ENCRYPTED_KEYS
  41421. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  41422. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  41423. #endif
  41424. /* Test for min/max proto */
  41425. #ifndef WOLFSSL_NO_TLS12
  41426. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  41427. 0, NULL), SSL_SUCCESS);
  41428. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  41429. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  41430. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  41431. #endif
  41432. #ifdef WOLFSSL_TLS13
  41433. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  41434. 0, NULL), SSL_SUCCESS);
  41435. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  41436. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  41437. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  41438. #ifndef WOLFSSL_NO_TLS12
  41439. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  41440. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  41441. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  41442. #endif
  41443. #endif
  41444. /* Cleanup and Pass */
  41445. #if !defined(NO_DH) && !defined(NO_DSA)
  41446. #ifndef NO_BIO
  41447. BIO_free(bio);
  41448. DSA_free(dsa);
  41449. DH_free(dh);
  41450. #endif
  41451. #endif
  41452. #ifdef HAVE_ECC
  41453. wolfSSL_EC_KEY_free(ecKey);
  41454. #endif
  41455. SSL_CTX_free(ctx);
  41456. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  41457. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  41458. return EXPECT_RESULT();
  41459. }
  41460. static int test_wolfSSL_EVP_PKEY_assign(void)
  41461. {
  41462. EXPECT_DECLS;
  41463. #if !defined(NO_RSA) || !defined(NO_DSA) || defined(HAVE_ECC)
  41464. int type;
  41465. WOLFSSL_EVP_PKEY* pkey = NULL;
  41466. #ifndef NO_RSA
  41467. WOLFSSL_RSA* rsa = NULL;
  41468. #endif
  41469. #ifndef NO_DSA
  41470. WOLFSSL_DSA* dsa = NULL;
  41471. #endif
  41472. #ifdef HAVE_ECC
  41473. WOLFSSL_EC_KEY* ecKey = NULL;
  41474. #endif
  41475. #ifndef NO_RSA
  41476. type = EVP_PKEY_RSA;
  41477. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41478. ExpectNotNull(rsa = wolfSSL_RSA_new());
  41479. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, rsa), WOLFSSL_FAILURE);
  41480. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  41481. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, rsa), WOLFSSL_FAILURE);
  41482. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, rsa), WOLFSSL_SUCCESS);
  41483. if (EXPECT_FAIL()) {
  41484. wolfSSL_RSA_free(rsa);
  41485. }
  41486. wolfSSL_EVP_PKEY_free(pkey);
  41487. pkey = NULL;
  41488. #endif /* NO_RSA */
  41489. #ifndef NO_DSA
  41490. type = EVP_PKEY_DSA;
  41491. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41492. ExpectNotNull(dsa = wolfSSL_DSA_new());
  41493. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, dsa), WOLFSSL_FAILURE);
  41494. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  41495. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, dsa), WOLFSSL_FAILURE);
  41496. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, dsa), WOLFSSL_SUCCESS);
  41497. if (EXPECT_FAIL()) {
  41498. wolfSSL_DSA_free(dsa);
  41499. }
  41500. wolfSSL_EVP_PKEY_free(pkey);
  41501. pkey = NULL;
  41502. #endif /* NO_DSA */
  41503. #ifdef HAVE_ECC
  41504. type = EVP_PKEY_EC;
  41505. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41506. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  41507. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, ecKey), WOLFSSL_FAILURE);
  41508. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  41509. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, ecKey), WOLFSSL_FAILURE);
  41510. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_FAILURE);
  41511. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  41512. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_SUCCESS);
  41513. if (EXPECT_FAIL()) {
  41514. wolfSSL_EC_KEY_free(ecKey);
  41515. }
  41516. wolfSSL_EVP_PKEY_free(pkey);
  41517. pkey = NULL;
  41518. #endif /* HAVE_ECC */
  41519. #endif /* !NO_RSA || !NO_DSA || HAVE_ECC */
  41520. return EXPECT_RESULT();
  41521. }
  41522. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  41523. {
  41524. EXPECT_DECLS;
  41525. #if !defined(NO_DH) && \
  41526. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  41527. XFILE f = XBADFILE;
  41528. unsigned char buf[4096];
  41529. const unsigned char* pt = buf;
  41530. const char* params1 = "./certs/dh2048.der";
  41531. long len = 0;
  41532. WOLFSSL_DH* dh = NULL;
  41533. WOLFSSL_EVP_PKEY* pkey = NULL;
  41534. XMEMSET(buf, 0, sizeof(buf));
  41535. /* Load DH parameters DER. */
  41536. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  41537. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  41538. if (f != XBADFILE)
  41539. XFCLOSE(f);
  41540. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  41541. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  41542. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41543. /* Bad cases */
  41544. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  41545. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  41546. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  41547. /* Good case */
  41548. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  41549. if (EXPECT_FAIL()) {
  41550. wolfSSL_DH_free(dh);
  41551. }
  41552. EVP_PKEY_free(pkey);
  41553. #endif
  41554. return EXPECT_RESULT();
  41555. }
  41556. static int test_wolfSSL_EVP_PKEY_base_id(void)
  41557. {
  41558. EXPECT_DECLS;
  41559. WOLFSSL_EVP_PKEY* pkey = NULL;
  41560. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41561. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  41562. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  41563. EVP_PKEY_free(pkey);
  41564. return EXPECT_RESULT();
  41565. }
  41566. static int test_wolfSSL_EVP_PKEY_id(void)
  41567. {
  41568. EXPECT_DECLS;
  41569. WOLFSSL_EVP_PKEY* pkey = NULL;
  41570. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41571. ExpectIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  41572. ExpectIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  41573. EVP_PKEY_free(pkey);
  41574. return EXPECT_RESULT();
  41575. }
  41576. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  41577. {
  41578. EXPECT_DECLS;
  41579. /* ECC check taken from ecc.c. It is the condition that defines ECC256 */
  41580. #if defined(OPENSSL_ALL) && !defined(NO_ECC_SECP) && \
  41581. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  41582. ECC_MIN_KEY_SZ <= 256)
  41583. EVP_PKEY_CTX* ctx = NULL;
  41584. EVP_PKEY* pkey = NULL;
  41585. /* Test error conditions. */
  41586. ExpectIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  41587. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  41588. ExpectIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  41589. #ifndef NO_RSA
  41590. EVP_PKEY_CTX_free(ctx);
  41591. /* Parameter generation for RSA not supported yet. */
  41592. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  41593. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  41594. #endif
  41595. #ifdef HAVE_ECC
  41596. EVP_PKEY_CTX_free(ctx);
  41597. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  41598. ExpectIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  41599. ExpectIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  41600. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  41601. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  41602. ExpectIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  41603. WOLFSSL_SUCCESS);
  41604. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  41605. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  41606. #endif
  41607. EVP_PKEY_CTX_free(ctx);
  41608. EVP_PKEY_free(pkey);
  41609. #endif
  41610. return EXPECT_RESULT();
  41611. }
  41612. static int test_wolfSSL_EVP_PKEY_keygen(void)
  41613. {
  41614. EXPECT_DECLS;
  41615. WOLFSSL_EVP_PKEY* pkey = NULL;
  41616. EVP_PKEY_CTX* ctx = NULL;
  41617. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  41618. WOLFSSL_EVP_PKEY* params = NULL;
  41619. DH* dh = NULL;
  41620. const BIGNUM* pubkey = NULL;
  41621. const BIGNUM* privkey = NULL;
  41622. ASN1_INTEGER* asn1int = NULL;
  41623. unsigned int length = 0;
  41624. byte* derBuffer = NULL;
  41625. #endif
  41626. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41627. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41628. /* Bad cases */
  41629. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  41630. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  41631. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  41632. /* Good case */
  41633. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  41634. EVP_PKEY_CTX_free(ctx);
  41635. ctx = NULL;
  41636. EVP_PKEY_free(pkey);
  41637. pkey = NULL;
  41638. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  41639. /* Test DH keygen */
  41640. {
  41641. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  41642. ExpectNotNull(dh = DH_get_2048_256());
  41643. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  41644. ExpectNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  41645. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  41646. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  41647. DH_free(dh);
  41648. dh = NULL;
  41649. EVP_PKEY_CTX_free(ctx);
  41650. EVP_PKEY_free(params);
  41651. /* try exporting generated key to DER, to verify */
  41652. ExpectNotNull(dh = EVP_PKEY_get1_DH(pkey));
  41653. DH_get0_key(dh, &pubkey, &privkey);
  41654. ExpectNotNull(pubkey);
  41655. ExpectNotNull(privkey);
  41656. ExpectNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  41657. ExpectIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  41658. ASN1_INTEGER_free(asn1int);
  41659. DH_free(dh);
  41660. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41661. EVP_PKEY_free(pkey);
  41662. }
  41663. #endif
  41664. return EXPECT_RESULT();
  41665. }
  41666. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  41667. {
  41668. EXPECT_DECLS;
  41669. WOLFSSL_EVP_PKEY* pkey = NULL;
  41670. EVP_PKEY_CTX *ctx = NULL;
  41671. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41672. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41673. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  41674. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(NULL), WOLFSSL_SUCCESS);
  41675. EVP_PKEY_CTX_free(ctx);
  41676. EVP_PKEY_free(pkey);
  41677. return EXPECT_RESULT();
  41678. }
  41679. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  41680. {
  41681. EXPECT_DECLS;
  41682. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  41683. WOLFSSL_EVP_PKEY* pkey = NULL;
  41684. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41685. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  41686. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(NULL), 0);
  41687. EVP_PKEY_free(pkey);
  41688. #endif
  41689. return EXPECT_RESULT();
  41690. }
  41691. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  41692. {
  41693. EXPECT_DECLS;
  41694. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  41695. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  41696. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  41697. !defined(NO_FILESYSTEM)
  41698. WOLFSSL_EVP_PKEY* params = NULL;
  41699. WOLFSSL_EVP_PKEY* copy = NULL;
  41700. DH* dh = NULL;
  41701. BIGNUM* p1;
  41702. BIGNUM* g1;
  41703. BIGNUM* q1;
  41704. BIGNUM* p2;
  41705. BIGNUM* g2;
  41706. BIGNUM* q2;
  41707. /* create DH with DH_get_2048_256 params */
  41708. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  41709. ExpectNotNull(dh = DH_get_2048_256());
  41710. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  41711. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  41712. (const BIGNUM**)&q1,
  41713. (const BIGNUM**)&g1);
  41714. DH_free(dh);
  41715. dh = NULL;
  41716. /* create DH with random generated DH params */
  41717. ExpectNotNull(copy = wolfSSL_EVP_PKEY_new());
  41718. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  41719. ExpectIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  41720. DH_free(dh);
  41721. dh = NULL;
  41722. ExpectIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  41723. ExpectNotNull(dh = EVP_PKEY_get1_DH(copy));
  41724. ExpectNotNull(dh->p);
  41725. ExpectNotNull(dh->g);
  41726. ExpectNotNull(dh->q);
  41727. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  41728. (const BIGNUM**)&q2,
  41729. (const BIGNUM**)&g2);
  41730. ExpectIntEQ(BN_cmp(p1, p2), 0);
  41731. ExpectIntEQ(BN_cmp(q1, q2), 0);
  41732. ExpectIntEQ(BN_cmp(g1, g2), 0);
  41733. DH_free(dh);
  41734. EVP_PKEY_free(copy);
  41735. EVP_PKEY_free(params);
  41736. #endif
  41737. return EXPECT_RESULT();
  41738. }
  41739. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  41740. {
  41741. EXPECT_DECLS;
  41742. WOLFSSL_EVP_PKEY* pkey = NULL;
  41743. EVP_PKEY_CTX* ctx = NULL;
  41744. int bits = 2048;
  41745. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41746. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41747. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  41748. WOLFSSL_SUCCESS);
  41749. EVP_PKEY_CTX_free(ctx);
  41750. EVP_PKEY_free(pkey);
  41751. return EXPECT_RESULT();
  41752. }
  41753. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  41754. {
  41755. EXPECT_DECLS;
  41756. /* This is large enough to be used for all key sizes */
  41757. byte key[AES_256_KEY_SIZE] = {0};
  41758. byte iv[AES_BLOCK_SIZE] = {0};
  41759. int i;
  41760. int nids[] = {
  41761. #ifdef HAVE_AES_CBC
  41762. NID_aes_128_cbc,
  41763. #endif
  41764. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  41765. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  41766. #ifdef HAVE_AESGCM
  41767. NID_aes_128_gcm,
  41768. #endif
  41769. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  41770. #ifdef WOLFSSL_AES_COUNTER
  41771. NID_aes_128_ctr,
  41772. #endif
  41773. #ifndef NO_DES3
  41774. NID_des_cbc,
  41775. NID_des_ede3_cbc,
  41776. #endif
  41777. };
  41778. int iv_lengths[] = {
  41779. #ifdef HAVE_AES_CBC
  41780. AES_BLOCK_SIZE,
  41781. #endif
  41782. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  41783. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  41784. #ifdef HAVE_AESGCM
  41785. GCM_NONCE_MID_SZ,
  41786. #endif
  41787. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  41788. #ifdef WOLFSSL_AES_COUNTER
  41789. AES_BLOCK_SIZE,
  41790. #endif
  41791. #ifndef NO_DES3
  41792. DES_BLOCK_SIZE,
  41793. DES_BLOCK_SIZE,
  41794. #endif
  41795. };
  41796. int nidsLen = (sizeof(nids)/sizeof(int));
  41797. for (i = 0; i < nidsLen; i++) {
  41798. const EVP_CIPHER* init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  41799. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  41800. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  41801. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  41802. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  41803. EVP_CIPHER_CTX_free(ctx);
  41804. }
  41805. return EXPECT_RESULT();
  41806. }
  41807. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  41808. {
  41809. EXPECT_DECLS;
  41810. byte key[AES_256_KEY_SIZE] = {0};
  41811. byte iv[AES_BLOCK_SIZE] = {0};
  41812. int i;
  41813. int nids[] = {
  41814. #ifdef HAVE_AES_CBC
  41815. NID_aes_128_cbc,
  41816. NID_aes_256_cbc,
  41817. #endif
  41818. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  41819. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  41820. #ifdef HAVE_AESGCM
  41821. NID_aes_128_gcm,
  41822. NID_aes_256_gcm,
  41823. #endif
  41824. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  41825. #ifdef WOLFSSL_AES_COUNTER
  41826. NID_aes_128_ctr,
  41827. NID_aes_256_ctr,
  41828. #endif
  41829. #ifndef NO_DES3
  41830. NID_des_cbc,
  41831. NID_des_ede3_cbc,
  41832. #endif
  41833. };
  41834. int key_lengths[] = {
  41835. #ifdef HAVE_AES_CBC
  41836. AES_128_KEY_SIZE,
  41837. AES_256_KEY_SIZE,
  41838. #endif
  41839. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  41840. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  41841. #ifdef HAVE_AESGCM
  41842. AES_128_KEY_SIZE,
  41843. AES_256_KEY_SIZE,
  41844. #endif
  41845. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  41846. #ifdef WOLFSSL_AES_COUNTER
  41847. AES_128_KEY_SIZE,
  41848. AES_256_KEY_SIZE,
  41849. #endif
  41850. #ifndef NO_DES3
  41851. DES_KEY_SIZE,
  41852. DES3_KEY_SIZE,
  41853. #endif
  41854. };
  41855. int nidsLen = (sizeof(nids)/sizeof(int));
  41856. for (i = 0; i < nidsLen; i++) {
  41857. const EVP_CIPHER *init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  41858. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  41859. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  41860. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  41861. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), key_lengths[i]);
  41862. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, key_lengths[i]),
  41863. WOLFSSL_SUCCESS);
  41864. EVP_CIPHER_CTX_free(ctx);
  41865. }
  41866. return EXPECT_RESULT();
  41867. }
  41868. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  41869. {
  41870. EXPECT_DECLS;
  41871. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  41872. int ivLen, keyLen;
  41873. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  41874. #ifdef HAVE_AESGCM
  41875. byte key[AES_128_KEY_SIZE] = {0};
  41876. byte iv[AES_BLOCK_SIZE] = {0};
  41877. const EVP_CIPHER *init = EVP_aes_128_gcm();
  41878. #else
  41879. byte key[DES3_KEY_SIZE] = {0};
  41880. byte iv[DES_BLOCK_SIZE] = {0};
  41881. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  41882. #endif
  41883. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  41884. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  41885. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  41886. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  41887. /* Bad cases */
  41888. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen),
  41889. WOLFSSL_FAILURE);
  41890. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen),
  41891. WOLFSSL_FAILURE);
  41892. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  41893. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  41894. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen),
  41895. WOLFSSL_FAILURE);
  41896. /* Good case */
  41897. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  41898. EVP_CIPHER_CTX_free(ctx);
  41899. #endif
  41900. return EXPECT_RESULT();
  41901. }
  41902. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  41903. {
  41904. EXPECT_DECLS;
  41905. WOLFSSL_ENGINE* e = NULL;
  41906. int id = 0;
  41907. EVP_PKEY_CTX *ctx = NULL;
  41908. ExpectNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  41909. EVP_PKEY_CTX_free(ctx);
  41910. return EXPECT_RESULT();
  41911. }
  41912. static int test_wolfSSL_EVP_rc4(void)
  41913. {
  41914. EXPECT_DECLS;
  41915. #if !defined(NO_RC4)
  41916. ExpectNotNull(wolfSSL_EVP_rc4());
  41917. #endif
  41918. return EXPECT_RESULT();
  41919. }
  41920. static int test_wolfSSL_EVP_enc_null(void)
  41921. {
  41922. EXPECT_DECLS;
  41923. ExpectNotNull(wolfSSL_EVP_enc_null());
  41924. return EXPECT_RESULT();
  41925. }
  41926. static int test_wolfSSL_EVP_rc2_cbc(void)
  41927. {
  41928. EXPECT_DECLS;
  41929. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  41930. ExpectNull(wolfSSL_EVP_rc2_cbc());
  41931. #endif
  41932. return EXPECT_RESULT();
  41933. }
  41934. static int test_wolfSSL_EVP_mdc2(void)
  41935. {
  41936. EXPECT_DECLS;
  41937. #if !defined(NO_WOLFSSL_STUB)
  41938. ExpectNull(wolfSSL_EVP_mdc2());
  41939. #endif
  41940. return EXPECT_RESULT();
  41941. }
  41942. static int test_wolfSSL_EVP_md4(void)
  41943. {
  41944. EXPECT_DECLS;
  41945. #if !defined(NO_MD4)
  41946. ExpectNotNull(wolfSSL_EVP_md4());
  41947. #endif
  41948. return EXPECT_RESULT();
  41949. }
  41950. static int test_wolfSSL_EVP_aes_256_gcm(void)
  41951. {
  41952. EXPECT_DECLS;
  41953. #ifdef HAVE_AESGCM
  41954. ExpectNotNull(wolfSSL_EVP_aes_256_gcm());
  41955. #endif
  41956. return EXPECT_RESULT();
  41957. }
  41958. static int test_wolfSSL_EVP_aes_192_gcm(void)
  41959. {
  41960. EXPECT_DECLS;
  41961. #ifdef HAVE_AESGCM
  41962. ExpectNotNull(wolfSSL_EVP_aes_192_gcm());
  41963. #endif
  41964. return EXPECT_RESULT();
  41965. }
  41966. static int test_wolfSSL_EVP_aes_256_ccm(void)
  41967. {
  41968. EXPECT_DECLS;
  41969. #ifdef HAVE_AESCCM
  41970. ExpectNotNull(wolfSSL_EVP_aes_256_ccm());
  41971. #endif
  41972. return EXPECT_RESULT();
  41973. }
  41974. static int test_wolfSSL_EVP_aes_192_ccm(void)
  41975. {
  41976. EXPECT_DECLS;
  41977. #ifdef HAVE_AESCCM
  41978. ExpectNotNull(wolfSSL_EVP_aes_192_ccm());
  41979. #endif
  41980. return EXPECT_RESULT();
  41981. }
  41982. static int test_wolfSSL_EVP_aes_128_ccm(void)
  41983. {
  41984. EXPECT_DECLS;
  41985. #ifdef HAVE_AESCCM
  41986. ExpectNotNull(wolfSSL_EVP_aes_128_ccm());
  41987. #endif
  41988. return EXPECT_RESULT();
  41989. }
  41990. static int test_wolfSSL_EVP_ripemd160(void)
  41991. {
  41992. EXPECT_DECLS;
  41993. #if !defined(NO_WOLFSSL_STUB)
  41994. ExpectNull(wolfSSL_EVP_ripemd160());
  41995. #endif
  41996. return EXPECT_RESULT();
  41997. }
  41998. static int test_wolfSSL_EVP_get_digestbynid(void)
  41999. {
  42000. EXPECT_DECLS;
  42001. #ifndef NO_MD5
  42002. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  42003. #endif
  42004. #ifndef NO_SHA
  42005. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  42006. #endif
  42007. #ifndef NO_SHA256
  42008. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
  42009. #endif
  42010. ExpectNull(wolfSSL_EVP_get_digestbynid(0));
  42011. return EXPECT_RESULT();
  42012. }
  42013. static int test_wolfSSL_EVP_MD_nid(void)
  42014. {
  42015. EXPECT_DECLS;
  42016. #ifndef NO_MD5
  42017. ExpectIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  42018. #endif
  42019. #ifndef NO_SHA
  42020. ExpectIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  42021. #endif
  42022. #ifndef NO_SHA256
  42023. ExpectIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  42024. #endif
  42025. ExpectIntEQ(EVP_MD_nid(NULL), NID_undef);
  42026. return EXPECT_RESULT();
  42027. }
  42028. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  42029. {
  42030. EXPECT_DECLS;
  42031. #if defined(HAVE_ECC)
  42032. WOLFSSL_EVP_PKEY* pkey = NULL;
  42033. ExpectNull(EVP_PKEY_get0_EC_KEY(NULL));
  42034. ExpectNotNull(pkey = EVP_PKEY_new());
  42035. ExpectNull(EVP_PKEY_get0_EC_KEY(pkey));
  42036. EVP_PKEY_free(pkey);
  42037. #endif
  42038. return EXPECT_RESULT();
  42039. }
  42040. static int test_wolfSSL_EVP_X_STATE(void)
  42041. {
  42042. EXPECT_DECLS;
  42043. #if !defined(NO_DES3) && !defined(NO_RC4)
  42044. byte key[DES3_KEY_SIZE] = {0};
  42045. byte iv[DES_IV_SIZE] = {0};
  42046. EVP_CIPHER_CTX *ctx = NULL;
  42047. const EVP_CIPHER *init = NULL;
  42048. /* Bad test cases */
  42049. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  42050. ExpectNotNull(init = EVP_des_ede3_cbc());
  42051. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42052. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42053. ExpectNull(wolfSSL_EVP_X_STATE(NULL));
  42054. ExpectNull(wolfSSL_EVP_X_STATE(ctx));
  42055. EVP_CIPHER_CTX_free(ctx);
  42056. ctx = NULL;
  42057. /* Good test case */
  42058. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  42059. ExpectNotNull(init = wolfSSL_EVP_rc4());
  42060. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42061. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42062. ExpectNotNull(wolfSSL_EVP_X_STATE(ctx));
  42063. EVP_CIPHER_CTX_free(ctx);
  42064. #endif
  42065. return EXPECT_RESULT();
  42066. }
  42067. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  42068. {
  42069. EXPECT_DECLS;
  42070. #if !defined(NO_DES3) && !defined(NO_RC4)
  42071. byte key[DES3_KEY_SIZE] = {0};
  42072. byte iv[DES_IV_SIZE] = {0};
  42073. EVP_CIPHER_CTX *ctx = NULL;
  42074. const EVP_CIPHER *init = NULL;
  42075. /* Bad test cases */
  42076. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  42077. ExpectNotNull(init = EVP_des_ede3_cbc());
  42078. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42079. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42080. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  42081. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  42082. EVP_CIPHER_CTX_free(ctx);
  42083. ctx = NULL;
  42084. /* Good test case */
  42085. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  42086. ExpectNotNull(init = wolfSSL_EVP_rc4());
  42087. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42088. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42089. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  42090. EVP_CIPHER_CTX_free(ctx);
  42091. #endif
  42092. return EXPECT_RESULT();
  42093. }
  42094. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  42095. {
  42096. EXPECT_DECLS;
  42097. #if defined(HAVE_AES_CBC) || defined(HAVE_AESGCM) || \
  42098. defined(WOLFSSL_AES_COUNTER) || defined(HAVE_AES_ECB) || \
  42099. defined(WOLFSSL_AES_OFB) || !defined(NO_RC4) || \
  42100. (defined(HAVE_CHACHA) && defined(HAVE_POLY1305))
  42101. #ifdef HAVE_AES_CBC
  42102. #ifdef WOLFSSL_AES_128
  42103. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  42104. #endif
  42105. #ifdef WOLFSSL_AES_192
  42106. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  42107. #endif
  42108. #ifdef WOLFSSL_AES_256
  42109. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  42110. #endif
  42111. #endif
  42112. #ifdef HAVE_AESGCM
  42113. #ifdef WOLFSSL_AES_128
  42114. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  42115. #endif
  42116. #ifdef WOLFSSL_AES_192
  42117. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  42118. #endif
  42119. #ifdef WOLFSSL_AES_256
  42120. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  42121. #endif
  42122. #endif
  42123. #ifdef HAVE_AESCCM
  42124. #ifdef WOLFSSL_AES_128
  42125. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ccm()), 1);
  42126. #endif
  42127. #ifdef WOLFSSL_AES_192
  42128. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ccm()), 1);
  42129. #endif
  42130. #ifdef WOLFSSL_AES_256
  42131. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ccm()), 1);
  42132. #endif
  42133. #endif
  42134. #ifdef WOLFSSL_AES_COUNTER
  42135. #ifdef WOLFSSL_AES_128
  42136. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  42137. #endif
  42138. #ifdef WOLFSSL_AES_192
  42139. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  42140. #endif
  42141. #ifdef WOLFSSL_AES_256
  42142. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  42143. #endif
  42144. #endif
  42145. #ifdef HAVE_AES_ECB
  42146. #ifdef WOLFSSL_AES_128
  42147. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  42148. #endif
  42149. #ifdef WOLFSSL_AES_192
  42150. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  42151. #endif
  42152. #ifdef WOLFSSL_AES_256
  42153. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  42154. #endif
  42155. #endif
  42156. #ifdef WOLFSSL_AES_OFB
  42157. #ifdef WOLFSSL_AES_128
  42158. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  42159. #endif
  42160. #ifdef WOLFSSL_AES_192
  42161. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  42162. #endif
  42163. #ifdef WOLFSSL_AES_256
  42164. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  42165. #endif
  42166. #endif
  42167. #ifndef NO_RC4
  42168. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  42169. #endif
  42170. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  42171. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  42172. #endif
  42173. #endif
  42174. #ifdef WOLFSSL_SM4_ECB
  42175. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ecb()), SM4_BLOCK_SIZE);
  42176. #endif
  42177. #ifdef WOLFSSL_SM4_CBC
  42178. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_cbc()), SM4_BLOCK_SIZE);
  42179. #endif
  42180. #ifdef WOLFSSL_SM4_CTR
  42181. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ctr()), 1);
  42182. #endif
  42183. #ifdef WOLFSSL_SM4_GCM
  42184. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_gcm()), 1);
  42185. #endif
  42186. #ifdef WOLFSSL_SM4_CCM
  42187. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ccm()), 1);
  42188. #endif
  42189. return EXPECT_RESULT();
  42190. }
  42191. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  42192. {
  42193. EXPECT_DECLS;
  42194. int nids[] = {
  42195. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  42196. #ifdef WOLFSSL_AES_128
  42197. NID_aes_128_cbc,
  42198. #endif
  42199. #ifdef WOLFSSL_AES_192
  42200. NID_aes_192_cbc,
  42201. #endif
  42202. #ifdef WOLFSSL_AES_256
  42203. NID_aes_256_cbc,
  42204. #endif
  42205. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  42206. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  42207. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42208. #ifdef HAVE_AESGCM
  42209. #ifdef WOLFSSL_AES_128
  42210. NID_aes_128_gcm,
  42211. #endif
  42212. #ifdef WOLFSSL_AES_192
  42213. NID_aes_192_gcm,
  42214. #endif
  42215. #ifdef WOLFSSL_AES_256
  42216. NID_aes_256_gcm,
  42217. #endif
  42218. #endif /* HAVE_AESGCM */
  42219. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  42220. #ifdef WOLFSSL_AES_COUNTER
  42221. #ifdef WOLFSSL_AES_128
  42222. NID_aes_128_ctr,
  42223. #endif
  42224. #ifdef WOLFSSL_AES_192
  42225. NID_aes_192_ctr,
  42226. #endif
  42227. #ifdef WOLFSSL_AES_256
  42228. NID_aes_256_ctr,
  42229. #endif
  42230. #endif
  42231. #ifndef NO_DES3
  42232. NID_des_cbc,
  42233. NID_des_ede3_cbc,
  42234. #endif
  42235. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  42236. NID_chacha20_poly1305,
  42237. #endif
  42238. };
  42239. int iv_lengths[] = {
  42240. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  42241. #ifdef WOLFSSL_AES_128
  42242. AES_BLOCK_SIZE,
  42243. #endif
  42244. #ifdef WOLFSSL_AES_192
  42245. AES_BLOCK_SIZE,
  42246. #endif
  42247. #ifdef WOLFSSL_AES_256
  42248. AES_BLOCK_SIZE,
  42249. #endif
  42250. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  42251. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  42252. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42253. #ifdef HAVE_AESGCM
  42254. #ifdef WOLFSSL_AES_128
  42255. GCM_NONCE_MID_SZ,
  42256. #endif
  42257. #ifdef WOLFSSL_AES_192
  42258. GCM_NONCE_MID_SZ,
  42259. #endif
  42260. #ifdef WOLFSSL_AES_256
  42261. GCM_NONCE_MID_SZ,
  42262. #endif
  42263. #endif /* HAVE_AESGCM */
  42264. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  42265. #ifdef WOLFSSL_AES_COUNTER
  42266. #ifdef WOLFSSL_AES_128
  42267. AES_BLOCK_SIZE,
  42268. #endif
  42269. #ifdef WOLFSSL_AES_192
  42270. AES_BLOCK_SIZE,
  42271. #endif
  42272. #ifdef WOLFSSL_AES_256
  42273. AES_BLOCK_SIZE,
  42274. #endif
  42275. #endif
  42276. #ifndef NO_DES3
  42277. DES_BLOCK_SIZE,
  42278. DES_BLOCK_SIZE,
  42279. #endif
  42280. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  42281. CHACHA20_POLY1305_AEAD_IV_SIZE,
  42282. #endif
  42283. };
  42284. int i;
  42285. int nidsLen = (sizeof(nids)/sizeof(int));
  42286. for (i = 0; i < nidsLen; i++) {
  42287. const EVP_CIPHER *c = EVP_get_cipherbynid(nids[i]);
  42288. ExpectIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  42289. }
  42290. return EXPECT_RESULT();
  42291. }
  42292. static int test_wolfSSL_EVP_SignInit_ex(void)
  42293. {
  42294. EXPECT_DECLS;
  42295. WOLFSSL_EVP_MD_CTX mdCtx;
  42296. WOLFSSL_ENGINE* e = 0;
  42297. const EVP_MD* md = EVP_sha256();
  42298. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  42299. ExpectIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  42300. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  42301. return EXPECT_RESULT();
  42302. }
  42303. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  42304. {
  42305. EXPECT_DECLS;
  42306. #if !defined(NO_SHA256)
  42307. WOLFSSL_EVP_MD_CTX mdCtx;
  42308. unsigned int s = 0;
  42309. unsigned char md[WC_SHA256_DIGEST_SIZE];
  42310. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  42311. /* Bad Case */
  42312. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  42313. (HAVE_FIPS_VERSION > 2))
  42314. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  42315. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  42316. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  42317. #else
  42318. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  42319. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  42320. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  42321. #endif
  42322. /* Good Case */
  42323. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  42324. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, EVP_sha256()), WOLFSSL_SUCCESS);
  42325. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  42326. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  42327. #endif
  42328. return EXPECT_RESULT();
  42329. }
  42330. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  42331. {
  42332. EXPECT_DECLS;
  42333. #if defined(OPENSSL_EXTRA)
  42334. EVP_PKEY* pkey = NULL;
  42335. EVP_PKEY_CTX* ctx = NULL;
  42336. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42337. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42338. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  42339. /* void */
  42340. EVP_PKEY_CTX_free(ctx);
  42341. #else
  42342. /* int */
  42343. ExpectIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  42344. #endif
  42345. EVP_PKEY_free(pkey);
  42346. #endif
  42347. return EXPECT_RESULT();
  42348. }
  42349. static int test_wolfSSL_EVP_PKEY_param_check(void)
  42350. {
  42351. EXPECT_DECLS;
  42352. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  42353. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  42354. DH *dh = NULL;
  42355. DH *setDh = NULL;
  42356. EVP_PKEY *pkey = NULL;
  42357. EVP_PKEY_CTX* ctx = NULL;
  42358. FILE* f = NULL;
  42359. unsigned char buf[512];
  42360. const unsigned char* pt = buf;
  42361. const char* dh2048 = "./certs/dh2048.der";
  42362. long len = 0;
  42363. int code = -1;
  42364. XMEMSET(buf, 0, sizeof(buf));
  42365. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  42366. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  42367. if (f != XBADFILE)
  42368. XFCLOSE(f);
  42369. /* Load dh2048.der into DH with internal format */
  42370. ExpectNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  42371. ExpectIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  42372. ExpectIntEQ(code, 0);
  42373. code = -1;
  42374. pkey = wolfSSL_EVP_PKEY_new();
  42375. /* Set DH into PKEY */
  42376. ExpectIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  42377. /* create ctx from pkey */
  42378. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42379. ExpectIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  42380. /* TODO: more invalid cases */
  42381. ExpectIntEQ(EVP_PKEY_param_check(NULL), 0);
  42382. EVP_PKEY_CTX_free(ctx);
  42383. EVP_PKEY_free(pkey);
  42384. DH_free(setDh);
  42385. DH_free(dh);
  42386. #endif
  42387. #endif
  42388. return EXPECT_RESULT();
  42389. }
  42390. static int test_wolfSSL_EVP_BytesToKey(void)
  42391. {
  42392. EXPECT_DECLS;
  42393. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  42394. byte key[AES_BLOCK_SIZE] = {0};
  42395. byte iv[AES_BLOCK_SIZE] = {0};
  42396. int count = 0;
  42397. const EVP_MD* md = EVP_sha256();
  42398. const EVP_CIPHER *type;
  42399. const unsigned char *salt = (unsigned char *)"salt1234";
  42400. int sz = 5;
  42401. const byte data[] = {
  42402. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  42403. 0x72,0x6c,0x64
  42404. };
  42405. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  42406. /* Bad cases */
  42407. ExpectIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  42408. 0);
  42409. ExpectIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  42410. 16);
  42411. md = "2";
  42412. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  42413. WOLFSSL_FAILURE);
  42414. /* Good case */
  42415. md = EVP_sha256();
  42416. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  42417. 16);
  42418. #endif
  42419. return EXPECT_RESULT();
  42420. }
  42421. static int test_evp_cipher_aes_gcm(void)
  42422. {
  42423. EXPECT_DECLS;
  42424. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  42425. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  42426. (HAVE_FIPS_VERSION >= 2)))
  42427. /*
  42428. * This test checks data at various points in the encrypt/decrypt process
  42429. * against known values produced using the same test with OpenSSL. This
  42430. * interop testing is critical for verifying the correctness of our
  42431. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  42432. * a flow supported by OpenSSL that uses the control command
  42433. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  42434. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  42435. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  42436. * wolfSSL OpenSSH clients because there was a bug in this flow that
  42437. * happened to "cancel out" if both sides of the connection had the bug.
  42438. */
  42439. enum {
  42440. NUM_ENCRYPTIONS = 3,
  42441. AAD_SIZE = 4
  42442. };
  42443. byte plainText1[] = {
  42444. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  42445. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  42446. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  42447. };
  42448. byte plainText2[] = {
  42449. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  42450. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  42451. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  42452. };
  42453. byte plainText3[] = {
  42454. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  42455. 0x5b, 0x57, 0x10
  42456. };
  42457. byte* plainTexts[NUM_ENCRYPTIONS] = {
  42458. plainText1,
  42459. plainText2,
  42460. plainText3
  42461. };
  42462. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  42463. sizeof(plainText1),
  42464. sizeof(plainText2),
  42465. sizeof(plainText3)
  42466. };
  42467. byte aad1[AAD_SIZE] = {
  42468. 0x00, 0x00, 0x00, 0x01
  42469. };
  42470. byte aad2[AAD_SIZE] = {
  42471. 0x00, 0x00, 0x00, 0x10
  42472. };
  42473. byte aad3[AAD_SIZE] = {
  42474. 0x00, 0x00, 0x01, 0x00
  42475. };
  42476. byte* aads[NUM_ENCRYPTIONS] = {
  42477. aad1,
  42478. aad2,
  42479. aad3
  42480. };
  42481. const byte iv[GCM_NONCE_MID_SZ] = {
  42482. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  42483. };
  42484. byte currentIv[GCM_NONCE_MID_SZ];
  42485. const byte key[] = {
  42486. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  42487. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  42488. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  42489. };
  42490. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  42491. {
  42492. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  42493. 0xEF
  42494. },
  42495. {
  42496. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  42497. 0xF0
  42498. },
  42499. {
  42500. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  42501. 0xF1
  42502. }
  42503. };
  42504. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  42505. {
  42506. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  42507. 0x3D, 0x32, 0x18, 0x34, 0xA9
  42508. },
  42509. {
  42510. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  42511. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  42512. },
  42513. {
  42514. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  42515. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  42516. }
  42517. };
  42518. const byte expCipherText1[] = {
  42519. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  42520. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  42521. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  42522. };
  42523. const byte expCipherText2[] = {
  42524. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  42525. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  42526. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  42527. };
  42528. const byte expCipherText3[] = {
  42529. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  42530. 0x80, 0x5E, 0x6B,
  42531. };
  42532. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  42533. expCipherText1,
  42534. expCipherText2,
  42535. expCipherText3
  42536. };
  42537. byte* cipherText = NULL;
  42538. byte* calcPlainText = NULL;
  42539. byte tag[AES_BLOCK_SIZE];
  42540. EVP_CIPHER_CTX* encCtx = NULL;
  42541. EVP_CIPHER_CTX* decCtx = NULL;
  42542. int i, j, outl;
  42543. /****************************************************/
  42544. for (i = 0; i < 3; ++i) {
  42545. ExpectNotNull(encCtx = EVP_CIPHER_CTX_new());
  42546. ExpectNotNull(decCtx = EVP_CIPHER_CTX_new());
  42547. /* First iteration, set key before IV. */
  42548. if (i == 0) {
  42549. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  42550. SSL_SUCCESS);
  42551. /*
  42552. * The call to EVP_CipherInit below (with NULL key) should clear the
  42553. * authIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  42554. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  42555. * behavior.
  42556. */
  42557. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  42558. (void*)iv), SSL_SUCCESS);
  42559. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  42560. SSL_SUCCESS);
  42561. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  42562. currentIv), SSL_FAILURE);
  42563. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  42564. SSL_SUCCESS);
  42565. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  42566. SSL_SUCCESS);
  42567. }
  42568. /* Second iteration, IV before key. */
  42569. else {
  42570. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  42571. SSL_SUCCESS);
  42572. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  42573. SSL_SUCCESS);
  42574. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  42575. SSL_SUCCESS);
  42576. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  42577. SSL_SUCCESS);
  42578. }
  42579. /*
  42580. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  42581. * been issued first.
  42582. */
  42583. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  42584. currentIv), SSL_FAILURE);
  42585. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  42586. (void*)iv), SSL_SUCCESS);
  42587. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  42588. (void*)iv), SSL_SUCCESS);
  42589. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  42590. /*************** Encrypt ***************/
  42591. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  42592. currentIv), SSL_SUCCESS);
  42593. /* Check current IV against expected. */
  42594. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  42595. /* Add AAD. */
  42596. if (i == 2) {
  42597. /* Test streaming API. */
  42598. ExpectIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  42599. AAD_SIZE), SSL_SUCCESS);
  42600. }
  42601. else {
  42602. ExpectIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  42603. AAD_SIZE);
  42604. }
  42605. ExpectNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  42606. DYNAMIC_TYPE_TMP_BUFFER));
  42607. /* Encrypt plaintext. */
  42608. if (i == 2) {
  42609. ExpectIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  42610. plainTexts[j], plainTextSzs[j]),
  42611. SSL_SUCCESS);
  42612. }
  42613. else {
  42614. ExpectIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  42615. plainTextSzs[j]), plainTextSzs[j]);
  42616. }
  42617. if (i == 2) {
  42618. ExpectIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  42619. SSL_SUCCESS);
  42620. }
  42621. else {
  42622. /*
  42623. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  42624. * akin to calling EVP_CipherFinal.
  42625. */
  42626. ExpectIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  42627. }
  42628. /* Check ciphertext against expected. */
  42629. ExpectIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  42630. 0);
  42631. /* Get and check tag against expected. */
  42632. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  42633. sizeof(tag), tag), SSL_SUCCESS);
  42634. ExpectIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  42635. /*************** Decrypt ***************/
  42636. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  42637. currentIv), SSL_SUCCESS);
  42638. /* Check current IV against expected. */
  42639. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  42640. /* Add AAD. */
  42641. if (i == 2) {
  42642. /* Test streaming API. */
  42643. ExpectIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  42644. AAD_SIZE), SSL_SUCCESS);
  42645. }
  42646. else {
  42647. ExpectIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  42648. AAD_SIZE);
  42649. }
  42650. /* Set expected tag. */
  42651. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  42652. sizeof(tag), tag), SSL_SUCCESS);
  42653. /* Decrypt ciphertext. */
  42654. ExpectNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  42655. DYNAMIC_TYPE_TMP_BUFFER));
  42656. if (i == 2) {
  42657. ExpectIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  42658. cipherText, plainTextSzs[j]),
  42659. SSL_SUCCESS);
  42660. }
  42661. else {
  42662. /* This first EVP_Cipher call will check the tag, too. */
  42663. ExpectIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  42664. plainTextSzs[j]), plainTextSzs[j]);
  42665. }
  42666. if (i == 2) {
  42667. ExpectIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  42668. SSL_SUCCESS);
  42669. }
  42670. else {
  42671. ExpectIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  42672. }
  42673. /* Check plaintext against expected. */
  42674. ExpectIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  42675. 0);
  42676. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42677. cipherText = NULL;
  42678. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42679. calcPlainText = NULL;
  42680. }
  42681. EVP_CIPHER_CTX_free(encCtx);
  42682. encCtx = NULL;
  42683. EVP_CIPHER_CTX_free(decCtx);
  42684. decCtx = NULL;
  42685. }
  42686. #endif
  42687. return EXPECT_RESULT();
  42688. }
  42689. static int test_wolfSSL_OBJ_ln(void)
  42690. {
  42691. EXPECT_DECLS;
  42692. const int nid_set[] = {
  42693. NID_commonName,
  42694. NID_serialNumber,
  42695. NID_countryName,
  42696. NID_localityName,
  42697. NID_stateOrProvinceName,
  42698. NID_organizationName,
  42699. NID_organizationalUnitName,
  42700. NID_domainComponent,
  42701. NID_businessCategory,
  42702. NID_jurisdictionCountryName,
  42703. NID_jurisdictionStateOrProvinceName,
  42704. NID_emailAddress
  42705. };
  42706. const char* ln_set[] = {
  42707. "commonName",
  42708. "serialNumber",
  42709. "countryName",
  42710. "localityName",
  42711. "stateOrProvinceName",
  42712. "organizationName",
  42713. "organizationalUnitName",
  42714. "domainComponent",
  42715. "businessCategory",
  42716. "jurisdictionCountryName",
  42717. "jurisdictionStateOrProvinceName",
  42718. "emailAddress",
  42719. };
  42720. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  42721. ExpectIntEQ(OBJ_ln2nid(NULL), NID_undef);
  42722. #ifdef HAVE_ECC
  42723. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42724. {
  42725. EC_builtin_curve r[27];
  42726. size_t nCurves = sizeof(r) / sizeof(r[0]);
  42727. nCurves = EC_get_builtin_curves(r, nCurves);
  42728. for (i = 0; i < nCurves; i++) {
  42729. /* skip ECC_CURVE_INVALID */
  42730. if (r[i].nid != ECC_CURVE_INVALID) {
  42731. ExpectIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  42732. ExpectStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  42733. }
  42734. }
  42735. }
  42736. #endif
  42737. #endif
  42738. for (i = 0; i < maxIdx; i++) {
  42739. ExpectIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  42740. ExpectStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  42741. }
  42742. return EXPECT_RESULT();
  42743. }
  42744. static int test_wolfSSL_OBJ_sn(void)
  42745. {
  42746. EXPECT_DECLS;
  42747. int i = 0, maxIdx = 7;
  42748. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  42749. NID_stateOrProvinceName,NID_organizationName,
  42750. NID_organizationalUnitName,NID_emailAddress};
  42751. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  42752. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  42753. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  42754. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  42755. WOLFSSL_EMAIL_ADDR};
  42756. ExpectIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  42757. for (i = 0; i < maxIdx; i++) {
  42758. ExpectIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  42759. ExpectStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  42760. }
  42761. return EXPECT_RESULT();
  42762. }
  42763. #if !defined(NO_BIO)
  42764. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  42765. {
  42766. return lh_strhash(s[3]);
  42767. }
  42768. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  42769. {
  42770. return XSTRCMP(a[3], b[3]);
  42771. }
  42772. #endif
  42773. static int test_wolfSSL_TXT_DB(void)
  42774. {
  42775. EXPECT_DECLS;
  42776. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  42777. BIO *bio = NULL;
  42778. TXT_DB *db = NULL;
  42779. const int columns = 6;
  42780. const char *fields[6] = {
  42781. "V",
  42782. "320926161116Z",
  42783. "",
  42784. "12BD",
  42785. "unknown",
  42786. "/CN=rsa doe",
  42787. };
  42788. char** fields_copy = NULL;
  42789. /* Test read */
  42790. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  42791. ExpectIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  42792. ExpectNotNull(db = TXT_DB_read(bio, columns));
  42793. ExpectNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  42794. DYNAMIC_TYPE_OPENSSL));
  42795. if (fields_copy != NULL) {
  42796. XMEMCPY(fields_copy, fields, sizeof(fields));
  42797. }
  42798. ExpectIntEQ(TXT_DB_insert(db, fields_copy), 1);
  42799. if (EXPECT_FAIL()) {
  42800. XFREE(fields_copy, NULL, DYNAMIC_TYPE_OPENSSL);
  42801. }
  42802. BIO_free(bio);
  42803. bio = NULL;
  42804. /* Test write */
  42805. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  42806. ExpectIntEQ(TXT_DB_write(bio, db), 1484);
  42807. BIO_free(bio);
  42808. /* Test index */
  42809. ExpectIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  42810. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  42811. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  42812. fields[3] = "12DA";
  42813. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  42814. fields[3] = "FFFF";
  42815. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  42816. fields[3] = "";
  42817. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  42818. TXT_DB_free(db);
  42819. #endif
  42820. return EXPECT_RESULT();
  42821. }
  42822. static int test_wolfSSL_NCONF(void)
  42823. {
  42824. EXPECT_DECLS;
  42825. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  42826. const char* confFile = "./tests/NCONF_test.cnf";
  42827. CONF* conf = NULL;
  42828. long eline = 0;
  42829. long num = 0;
  42830. ExpectNotNull(conf = NCONF_new(NULL));
  42831. ExpectIntEQ(NCONF_load(conf, confFile, &eline), 1);
  42832. ExpectIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  42833. ExpectIntEQ(num, 1234);
  42834. ExpectIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  42835. ExpectIntEQ(num, 4321);
  42836. ExpectStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  42837. ExpectStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  42838. "./test-dir/file1");
  42839. ExpectStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  42840. "./test-dir/file1:./test-dir/file2:./section1:file2");
  42841. NCONF_free(conf);
  42842. #endif
  42843. return EXPECT_RESULT();
  42844. }
  42845. #endif /* OPENSSL_ALL */
  42846. static int test_wolfSSL_X509V3_EXT_get(void) {
  42847. EXPECT_DECLS;
  42848. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  42849. XFILE f = XBADFILE;
  42850. int numOfExt =0;
  42851. int extNid = 0;
  42852. int i = 0;
  42853. WOLFSSL_X509* x509 = NULL;
  42854. WOLFSSL_X509_EXTENSION* ext = NULL;
  42855. const WOLFSSL_v3_ext_method* method = NULL;
  42856. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  42857. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  42858. if (f != XBADFILE)
  42859. XFCLOSE(f);
  42860. /* wolfSSL_X509V3_EXT_get() return struct and nid test */
  42861. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  42862. for (i = 0; i < numOfExt; i++) {
  42863. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  42864. ExpectIntNE((extNid = ext->obj->nid), NID_undef);
  42865. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  42866. ExpectIntEQ(method->ext_nid, extNid);
  42867. }
  42868. /* wolfSSL_X509V3_EXT_get() NULL argument test */
  42869. ExpectNull(method = wolfSSL_X509V3_EXT_get(NULL));
  42870. wolfSSL_X509_free(x509);
  42871. #endif
  42872. return EXPECT_RESULT();
  42873. }
  42874. static int test_wolfSSL_X509V3_EXT_nconf(void)
  42875. {
  42876. EXPECT_DECLS;
  42877. #ifdef OPENSSL_ALL
  42878. const char *ext_names[] = {
  42879. "subjectKeyIdentifier",
  42880. "authorityKeyIdentifier",
  42881. "subjectAltName",
  42882. "keyUsage",
  42883. "extendedKeyUsage",
  42884. };
  42885. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  42886. int ext_nids[] = {
  42887. NID_subject_key_identifier,
  42888. NID_authority_key_identifier,
  42889. NID_subject_alt_name,
  42890. NID_key_usage,
  42891. NID_ext_key_usage,
  42892. };
  42893. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  42894. const char *ext_values[] = {
  42895. "hash",
  42896. "hash",
  42897. "DNS:example.com, IP:127.0.0.1",
  42898. "digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,"
  42899. "keyAgreement,keyCertSign,cRLSign,encipherOnly,decipherOnly",
  42900. "serverAuth,clientAuth,codeSigning,emailProtection,timeStamping,"
  42901. "OCSPSigning",
  42902. };
  42903. size_t i;
  42904. X509_EXTENSION* ext = NULL;
  42905. X509* x509 = NULL;
  42906. unsigned int keyUsageFlags;
  42907. unsigned int extKeyUsageFlags;
  42908. ExpectNotNull(x509 = X509_new());
  42909. /* keyUsage / extKeyUsage should match string above */
  42910. keyUsageFlags = KU_DIGITAL_SIGNATURE
  42911. | KU_NON_REPUDIATION
  42912. | KU_KEY_ENCIPHERMENT
  42913. | KU_DATA_ENCIPHERMENT
  42914. | KU_KEY_AGREEMENT
  42915. | KU_KEY_CERT_SIGN
  42916. | KU_CRL_SIGN
  42917. | KU_ENCIPHER_ONLY
  42918. | KU_DECIPHER_ONLY;
  42919. extKeyUsageFlags = XKU_SSL_CLIENT
  42920. | XKU_SSL_SERVER
  42921. | XKU_CODE_SIGN
  42922. | XKU_SMIME
  42923. | XKU_TIMESTAMP
  42924. | XKU_OCSP_SIGN;
  42925. for (i = 0; i < ext_names_count; i++) {
  42926. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  42927. ext_values[i]));
  42928. X509_EXTENSION_free(ext);
  42929. ext = NULL;
  42930. }
  42931. for (i = 0; i < ext_nids_count; i++) {
  42932. ExpectNotNull(ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i],
  42933. ext_values[i]));
  42934. X509_EXTENSION_free(ext);
  42935. ext = NULL;
  42936. }
  42937. /* Test adding extension to X509 */
  42938. for (i = 0; i < ext_nids_count; i++) {
  42939. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  42940. ext_values[i]));
  42941. ExpectIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  42942. if (ext_nids[i] == NID_key_usage) {
  42943. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  42944. }
  42945. else if (ext_nids[i] == NID_ext_key_usage) {
  42946. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  42947. }
  42948. X509_EXTENSION_free(ext);
  42949. ext = NULL;
  42950. }
  42951. X509_free(x509);
  42952. #endif
  42953. return EXPECT_RESULT();
  42954. }
  42955. static int test_wolfSSL_X509V3_EXT(void) {
  42956. EXPECT_DECLS;
  42957. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  42958. XFILE f = XBADFILE;
  42959. int numOfExt = 0, nid = 0, i = 0, expected, actual = 0;
  42960. char* str = NULL;
  42961. unsigned char* data = NULL;
  42962. const WOLFSSL_v3_ext_method* method = NULL;
  42963. WOLFSSL_X509* x509 = NULL;
  42964. WOLFSSL_X509_EXTENSION* ext = NULL;
  42965. WOLFSSL_X509_EXTENSION* ext2 = NULL;
  42966. WOLFSSL_ASN1_OBJECT *obj = NULL;
  42967. WOLFSSL_ASN1_OBJECT *adObj = NULL;
  42968. WOLFSSL_ASN1_STRING* asn1str = NULL;
  42969. WOLFSSL_AUTHORITY_KEYID* aKeyId = NULL;
  42970. WOLFSSL_AUTHORITY_INFO_ACCESS* aia = NULL;
  42971. WOLFSSL_BASIC_CONSTRAINTS* bc = NULL;
  42972. WOLFSSL_ACCESS_DESCRIPTION* ad = NULL;
  42973. WOLFSSL_GENERAL_NAME* gn = NULL;
  42974. /* Check NULL argument */
  42975. ExpectNull(wolfSSL_X509V3_EXT_d2i(NULL));
  42976. /* Using OCSP cert with X509V3 extensions */
  42977. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  42978. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  42979. if (f != XBADFILE)
  42980. XFCLOSE(f);
  42981. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  42982. /* Basic Constraints */
  42983. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  42984. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  42985. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  42986. ExpectNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  42987. ExpectIntEQ(bc->ca, 1);
  42988. ExpectNull(bc->pathlen);
  42989. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  42990. bc = NULL;
  42991. i++;
  42992. /* Subject Key Identifier */
  42993. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  42994. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  42995. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  42996. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  42997. ExpectNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  42998. asn1str));
  42999. X509_EXTENSION_free(ext2);
  43000. ext2 = NULL;
  43001. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  43002. ExpectNotNull(method->i2s);
  43003. ExpectNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  43004. wolfSSL_ASN1_STRING_free(asn1str);
  43005. asn1str = NULL;
  43006. if (str != NULL) {
  43007. actual = strcmp(str,
  43008. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  43009. }
  43010. ExpectIntEQ(actual, 0);
  43011. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43012. str = NULL;
  43013. i++;
  43014. /* Authority Key Identifier */
  43015. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43016. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43017. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  43018. ExpectNotNull(aKeyId = (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(
  43019. ext));
  43020. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  43021. ExpectNotNull(asn1str = aKeyId->keyid);
  43022. ExpectNotNull(str = wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method,
  43023. asn1str));
  43024. asn1str = NULL;
  43025. if (str != NULL) {
  43026. actual = strcmp(str,
  43027. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  43028. }
  43029. ExpectIntEQ(actual, 0);
  43030. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43031. str = NULL;
  43032. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  43033. aKeyId = NULL;
  43034. i++;
  43035. /* Key Usage */
  43036. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43037. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43038. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  43039. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  43040. #if defined(WOLFSSL_QT)
  43041. ExpectNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  43042. #else
  43043. ExpectNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  43044. #endif
  43045. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  43046. if (data != NULL) {
  43047. #ifdef BIG_ENDIAN_ORDER
  43048. actual = data[1];
  43049. #else
  43050. actual = data[0];
  43051. #endif
  43052. }
  43053. ExpectIntEQ(actual, expected);
  43054. wolfSSL_ASN1_STRING_free(asn1str);
  43055. asn1str = NULL;
  43056. #if 1
  43057. i++;
  43058. /* Authority Info Access */
  43059. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43060. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43061. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  43062. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  43063. ext));
  43064. #if defined(WOLFSSL_QT)
  43065. ExpectIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  43066. #else
  43067. ExpectIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  43068. #endif
  43069. /* URI entry is an ACCESS_DESCRIPTION type */
  43070. #if defined(WOLFSSL_QT)
  43071. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  43072. #else
  43073. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  43074. #endif
  43075. ExpectNotNull(adObj = ad->method);
  43076. /* Make sure nid is OCSP */
  43077. ExpectIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  43078. /* GENERAL_NAME stores URI as an ASN1_STRING */
  43079. ExpectNotNull(gn = ad->location);
  43080. ExpectIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  43081. ExpectNotNull(asn1str = gn->d.uniformResourceIdentifier);
  43082. ExpectIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  43083. #if defined(WOLFSSL_QT)
  43084. ExpectNotNull(str = (char*)ASN1_STRING_get0_data(asn1str));
  43085. #else
  43086. ExpectNotNull(str = (char*)wolfSSL_ASN1_STRING_data(asn1str));
  43087. #endif
  43088. if (str != NULL) {
  43089. actual = strcmp(str, "http://127.0.0.1:22220");
  43090. }
  43091. ExpectIntEQ(actual, 0);
  43092. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  43093. aia = NULL;
  43094. #else
  43095. (void) aia; (void) ad; (void) adObj; (void) gn;
  43096. #endif
  43097. wolfSSL_X509_free(x509);
  43098. #endif
  43099. return EXPECT_RESULT();
  43100. }
  43101. static int test_wolfSSL_X509_get_extension_flags(void)
  43102. {
  43103. EXPECT_DECLS;
  43104. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  43105. XFILE f = XBADFILE;
  43106. X509* x509 = NULL;
  43107. unsigned int extFlags;
  43108. unsigned int keyUsageFlags;
  43109. unsigned int extKeyUsageFlags;
  43110. /* client-int-cert.pem has the following extension flags. */
  43111. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  43112. /* and the following key usage flags. */
  43113. keyUsageFlags = KU_DIGITAL_SIGNATURE
  43114. | KU_NON_REPUDIATION
  43115. | KU_KEY_ENCIPHERMENT;
  43116. /* and the following extended key usage flags. */
  43117. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  43118. ExpectTrue((f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb")) !=
  43119. XBADFILE);
  43120. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  43121. if (f != XBADFILE) {
  43122. XFCLOSE(f);
  43123. f = XBADFILE;
  43124. }
  43125. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  43126. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  43127. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  43128. X509_free(x509);
  43129. x509 = NULL;
  43130. /* client-cert-ext.pem has the following extension flags. */
  43131. extFlags = EXFLAG_KUSAGE;
  43132. /* and the following key usage flags. */
  43133. keyUsageFlags = KU_DIGITAL_SIGNATURE
  43134. | KU_KEY_CERT_SIGN
  43135. | KU_CRL_SIGN;
  43136. ExpectTrue((f = fopen("./certs/client-cert-ext.pem", "rb")) != XBADFILE);
  43137. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  43138. if (f != XBADFILE)
  43139. XFCLOSE(f);
  43140. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  43141. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  43142. X509_free(x509);
  43143. #endif /* OPENSSL_ALL */
  43144. return EXPECT_RESULT();
  43145. }
  43146. static int test_wolfSSL_X509_get_ext(void)
  43147. {
  43148. EXPECT_DECLS;
  43149. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43150. int ret = 0;
  43151. XFILE f = XBADFILE;
  43152. WOLFSSL_X509* x509 = NULL;
  43153. WOLFSSL_X509_EXTENSION* foundExtension;
  43154. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  43155. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43156. if (f != XBADFILE)
  43157. XFCLOSE(f);
  43158. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  43159. /* wolfSSL_X509_get_ext() valid input */
  43160. ExpectNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  43161. /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
  43162. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  43163. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  43164. /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
  43165. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  43166. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  43167. /* wolfSSL_X509_get_ext() NULL x509, valid idx */
  43168. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  43169. wolfSSL_X509_free(x509);
  43170. #endif
  43171. return EXPECT_RESULT();
  43172. }
  43173. static int test_wolfSSL_X509_get_ext_by_NID(void)
  43174. {
  43175. EXPECT_DECLS;
  43176. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  43177. int rc;
  43178. XFILE f = XBADFILE;
  43179. WOLFSSL_X509* x509 = NULL;
  43180. ASN1_OBJECT* obj = NULL;
  43181. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  43182. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43183. if (f != XBADFILE)
  43184. XFCLOSE(f);
  43185. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  43186. -1), 0);
  43187. /* Start search from last location (should fail) */
  43188. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  43189. rc), -1);
  43190. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  43191. -2), -1);
  43192. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints,
  43193. -1), -1);
  43194. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1), -1);
  43195. /* NID_ext_key_usage, check also its nid and oid */
  43196. ExpectIntGT(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1),
  43197. -1);
  43198. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(
  43199. x509, rc)));
  43200. ExpectIntEQ(obj->nid, NID_ext_key_usage);
  43201. ExpectIntEQ(obj->type, EXT_KEY_USAGE_OID);
  43202. wolfSSL_X509_free(x509);
  43203. #endif
  43204. return EXPECT_RESULT();
  43205. }
  43206. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  43207. {
  43208. EXPECT_DECLS;
  43209. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  43210. int rc;
  43211. XFILE f = XBADFILE;
  43212. WOLFSSL_X509* x509 = NULL;
  43213. WOLFSSL_X509_EXTENSION* ext;
  43214. WOLFSSL_ASN1_STRING* sanString;
  43215. byte* sanDer;
  43216. const byte expectedDer[] = {
  43217. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  43218. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  43219. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  43220. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  43221. if (f != XBADFILE)
  43222. XFCLOSE(f);
  43223. ExpectIntNE(rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1), -1);
  43224. ExpectNotNull(ext = X509_get_ext(x509, rc));
  43225. ExpectNotNull(sanString = X509_EXTENSION_get_data(ext));
  43226. ExpectIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  43227. ExpectNotNull(sanDer = ASN1_STRING_data(sanString));
  43228. ExpectIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  43229. X509_free(x509);
  43230. #endif
  43231. return EXPECT_RESULT();
  43232. }
  43233. static int test_wolfSSL_X509_EXTENSION_new(void)
  43234. {
  43235. EXPECT_DECLS;
  43236. #if defined (OPENSSL_ALL)
  43237. WOLFSSL_X509_EXTENSION* ext = NULL;
  43238. ExpectNotNull(ext = wolfSSL_X509_EXTENSION_new());
  43239. ExpectNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  43240. wolfSSL_X509_EXTENSION_free(ext);
  43241. #endif
  43242. return EXPECT_RESULT();
  43243. }
  43244. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  43245. {
  43246. EXPECT_DECLS;
  43247. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43248. WOLFSSL_X509* x509 = NULL;
  43249. WOLFSSL_X509_EXTENSION* ext = NULL;
  43250. WOLFSSL_ASN1_OBJECT* o = NULL;
  43251. XFILE file = XBADFILE;
  43252. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  43253. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  43254. if (file != XBADFILE)
  43255. XFCLOSE(file);
  43256. /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
  43257. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  43258. ExpectNull(wolfSSL_X509_EXTENSION_get_object(NULL));
  43259. ExpectNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  43260. ExpectIntEQ(o->nid, 128);
  43261. /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
  43262. ExpectNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  43263. wolfSSL_X509_free(x509);
  43264. #endif
  43265. return EXPECT_RESULT();
  43266. }
  43267. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  43268. {
  43269. EXPECT_DECLS;
  43270. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43271. WOLFSSL_X509* x509 = NULL;
  43272. WOLFSSL_X509_EXTENSION* ext;
  43273. WOLFSSL_ASN1_STRING* str;
  43274. XFILE file = XBADFILE;
  43275. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  43276. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  43277. if (file != XBADFILE)
  43278. XFCLOSE(file);
  43279. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  43280. ExpectNull(str = wolfSSL_X509_EXTENSION_get_data(NULL));
  43281. ExpectNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  43282. wolfSSL_X509_free(x509);
  43283. #endif
  43284. return EXPECT_RESULT();
  43285. }
  43286. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  43287. {
  43288. EXPECT_DECLS;
  43289. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43290. WOLFSSL_X509* x509 = NULL;
  43291. WOLFSSL_X509_EXTENSION* ext;
  43292. XFILE file = XBADFILE;
  43293. int crit;
  43294. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  43295. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  43296. if (file != XBADFILE)
  43297. XFCLOSE(file);
  43298. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  43299. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(NULL), BAD_FUNC_ARG);
  43300. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(ext), 0);
  43301. wolfSSL_X509_free(x509);
  43302. #endif
  43303. return EXPECT_RESULT();
  43304. }
  43305. static int test_wolfSSL_X509V3_EXT_print(void)
  43306. {
  43307. EXPECT_DECLS;
  43308. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  43309. !defined(NO_RSA)
  43310. {
  43311. XFILE f = XBADFILE;
  43312. WOLFSSL_X509* x509 = NULL;
  43313. X509_EXTENSION * ext = NULL;
  43314. int loc;
  43315. BIO *bio = NULL;
  43316. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  43317. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43318. if (f != XBADFILE)
  43319. fclose(f);
  43320. ExpectNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  43321. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  43322. NID_basic_constraints, -1), -1);
  43323. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  43324. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  43325. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  43326. NID_subject_key_identifier, -1), -1);
  43327. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  43328. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  43329. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  43330. NID_authority_key_identifier, -1), -1);
  43331. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  43332. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  43333. wolfSSL_BIO_free(bio);
  43334. wolfSSL_X509_free(x509);
  43335. }
  43336. {
  43337. X509 *x509 = NULL;
  43338. BIO *bio = NULL;
  43339. X509_EXTENSION *ext;
  43340. unsigned int i;
  43341. unsigned int idx;
  43342. /* Some NIDs to test with */
  43343. int nids[] = {
  43344. /* NID_key_usage, currently X509_get_ext returns this as a bit
  43345. * string, which messes up X509V3_EXT_print */
  43346. /* NID_ext_key_usage, */
  43347. NID_subject_alt_name,
  43348. };
  43349. int* n;
  43350. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  43351. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  43352. WOLFSSL_FILETYPE_PEM));
  43353. ExpectIntGT(fprintf(stderr, "\nPrinting extension values:\n"), 0);
  43354. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  43355. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  43356. * update the index */
  43357. ExpectIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  43358. ExpectNotNull(ext = X509_get_ext(x509, idx));
  43359. ExpectIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  43360. ExpectIntGT(fprintf(stderr, "\n"), 0);
  43361. }
  43362. BIO_free(bio);
  43363. X509_free(x509);
  43364. }
  43365. #endif
  43366. return EXPECT_RESULT();
  43367. }
  43368. static int test_wolfSSL_X509_cmp(void)
  43369. {
  43370. EXPECT_DECLS;
  43371. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  43372. XFILE file1 = XBADFILE;
  43373. XFILE file2 = XBADFILE;
  43374. WOLFSSL_X509* cert1 = NULL;
  43375. WOLFSSL_X509* cert2 = NULL;
  43376. ExpectTrue((file1 = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  43377. ExpectTrue((file2 = XFOPEN("./certs/3072/client-cert.pem", "rb")) !=
  43378. XBADFILE);
  43379. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  43380. ExpectNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  43381. if (file1 != XBADFILE)
  43382. fclose(file1);
  43383. if (file2 != XBADFILE)
  43384. fclose(file2);
  43385. /* wolfSSL_X509_cmp() testing matching certs */
  43386. ExpectIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  43387. /* wolfSSL_X509_cmp() testing mismatched certs */
  43388. ExpectIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  43389. /* wolfSSL_X509_cmp() testing NULL, valid args */
  43390. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  43391. /* wolfSSL_X509_cmp() testing valid, NULL args */
  43392. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  43393. /* wolfSSL_X509_cmp() testing NULL, NULL args */
  43394. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  43395. wolfSSL_X509_free(cert1);
  43396. wolfSSL_X509_free(cert2);
  43397. #endif
  43398. return EXPECT_RESULT();
  43399. }
  43400. static int test_wolfSSL_EVP_PKEY_up_ref(void)
  43401. {
  43402. EXPECT_DECLS;
  43403. #if defined(OPENSSL_ALL)
  43404. EVP_PKEY* pkey;
  43405. pkey = EVP_PKEY_new();
  43406. ExpectNotNull(pkey);
  43407. ExpectIntEQ(EVP_PKEY_up_ref(NULL), 0);
  43408. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  43409. EVP_PKEY_free(pkey);
  43410. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  43411. EVP_PKEY_free(pkey);
  43412. EVP_PKEY_free(pkey);
  43413. #endif
  43414. return EXPECT_RESULT();
  43415. }
  43416. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  43417. {
  43418. EXPECT_DECLS;
  43419. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  43420. EVP_PKEY* pkey = NULL;
  43421. const unsigned char* p;
  43422. unsigned char *der = NULL;
  43423. unsigned char *tmp = NULL;
  43424. int derLen;
  43425. p = client_keypub_der_2048;
  43426. /* Check that key can be successfully decoded. */
  43427. ExpectNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  43428. sizeof_client_keypub_der_2048));
  43429. /* Check that key can be successfully encoded. */
  43430. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  43431. /* Ensure that the encoded version matches the original. */
  43432. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  43433. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  43434. /* Do same test except with pre-allocated buffer to ensure the der pointer
  43435. * is advanced. */
  43436. tmp = der;
  43437. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  43438. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  43439. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  43440. ExpectTrue(der + derLen == tmp);
  43441. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  43442. EVP_PKEY_free(pkey);
  43443. #endif
  43444. return EXPECT_RESULT();
  43445. }
  43446. static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
  43447. {
  43448. EXPECT_DECLS;
  43449. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(NO_CERTS) && \
  43450. !defined(NO_ASN) && !defined(NO_PWDBASED)
  43451. EVP_PKEY* pkey = NULL;
  43452. const unsigned char* p;
  43453. unsigned char *der = NULL;
  43454. unsigned char *tmp = NULL;
  43455. int derLen;
  43456. unsigned char pub_buf[65];
  43457. const int pub_len = 65;
  43458. BN_CTX* ctx;
  43459. EC_GROUP* curve = NULL;
  43460. EC_KEY* ephemeral_key = NULL;
  43461. const EC_POINT* h;
  43462. /* Generate an x963 key pair and get public part into pub_buf */
  43463. ExpectNotNull(ctx = BN_CTX_new());
  43464. ExpectNotNull(curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  43465. ExpectNotNull(ephemeral_key = EC_KEY_new_by_curve_name(
  43466. NID_X9_62_prime256v1));
  43467. ExpectIntEQ(EC_KEY_generate_key(ephemeral_key), 1);
  43468. ExpectNotNull(h = EC_KEY_get0_public_key(ephemeral_key));
  43469. ExpectIntEQ(pub_len, EC_POINT_point2oct(curve, h,
  43470. POINT_CONVERSION_UNCOMPRESSED, pub_buf, pub_len, ctx));
  43471. /* Prepare the EVP_PKEY */
  43472. ExpectNotNull(pkey = EVP_PKEY_new());
  43473. p = pub_buf;
  43474. /* Check that key can be successfully decoded. */
  43475. ExpectNotNull(wolfSSL_d2i_PublicKey(EVP_PKEY_EC, &pkey, &p,
  43476. pub_len));
  43477. /* Check that key can be successfully encoded. */
  43478. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  43479. /* Ensure that the encoded version matches the original. */
  43480. ExpectIntEQ(derLen, pub_len);
  43481. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  43482. /* Do same test except with pre-allocated buffer to ensure the der pointer
  43483. * is advanced. */
  43484. tmp = der;
  43485. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  43486. ExpectIntEQ(derLen, pub_len);
  43487. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  43488. ExpectTrue(der + derLen == tmp);
  43489. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  43490. EVP_PKEY_free(pkey);
  43491. EC_KEY_free(ephemeral_key);
  43492. EC_GROUP_free(curve);
  43493. #endif
  43494. return EXPECT_RESULT();
  43495. }
  43496. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  43497. {
  43498. EXPECT_DECLS;
  43499. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  43500. DSA* dsa = NULL;
  43501. byte derIn[] = {
  43502. 0x30, 0x82, 0x01, 0x1f, 0x02, 0x81, 0x81, 0x00,
  43503. 0xcd, 0xde, 0x25, 0x68, 0x80, 0x53, 0x0d, 0xe5,
  43504. 0x77, 0xd6, 0xd2, 0x90, 0x39, 0x3f, 0x90, 0xa2,
  43505. 0x3f, 0x33, 0x94, 0x6e, 0xe8, 0x4f, 0x2b, 0x63,
  43506. 0xab, 0x30, 0xab, 0x15, 0xba, 0x11, 0xea, 0x8a,
  43507. 0x5d, 0x8d, 0xcc, 0xb8, 0xd4, 0xa1, 0xd5, 0xc1,
  43508. 0x47, 0x9d, 0x5a, 0x73, 0x6a, 0x62, 0x49, 0xd1,
  43509. 0x06, 0x07, 0x67, 0xf6, 0x2f, 0xa3, 0x39, 0xbd,
  43510. 0x4e, 0x0d, 0xb4, 0xd3, 0x22, 0x23, 0x84, 0xec,
  43511. 0x93, 0x26, 0x5a, 0x49, 0xee, 0x7c, 0x89, 0x48,
  43512. 0x66, 0x4d, 0xe8, 0xe8, 0xd8, 0x50, 0xfb, 0xa5,
  43513. 0x71, 0x9f, 0x22, 0x18, 0xe5, 0xe6, 0x0b, 0x46,
  43514. 0x87, 0x66, 0xee, 0x52, 0x8f, 0x46, 0x4f, 0xb5,
  43515. 0x03, 0xce, 0xed, 0xe3, 0xbe, 0xe5, 0xb5, 0x81,
  43516. 0xd2, 0x59, 0xe9, 0xc0, 0xad, 0x4d, 0xd0, 0x4d,
  43517. 0x26, 0xf7, 0xba, 0x50, 0xe8, 0xc9, 0x8f, 0xfe,
  43518. 0x24, 0x19, 0x3d, 0x2e, 0xa7, 0x52, 0x3c, 0x6d,
  43519. 0x02, 0x15, 0x00, 0xfb, 0x47, 0xfb, 0xec, 0x81,
  43520. 0x20, 0xc8, 0x1c, 0xe9, 0x4a, 0xba, 0x04, 0x6f,
  43521. 0x19, 0x9b, 0x94, 0xee, 0x82, 0x67, 0xd3, 0x02,
  43522. 0x81, 0x81, 0x00, 0x9b, 0x95, 0xbb, 0x85, 0xc5,
  43523. 0x58, 0x4a, 0x32, 0x9c, 0xaa, 0x44, 0x85, 0xd6,
  43524. 0x68, 0xdc, 0x3e, 0x14, 0xf4, 0xce, 0x6d, 0xa3,
  43525. 0x49, 0x38, 0xea, 0xd6, 0x61, 0x48, 0x92, 0x5a,
  43526. 0x40, 0x95, 0x49, 0x38, 0xaa, 0xe1, 0x39, 0x29,
  43527. 0x68, 0x58, 0x47, 0x8a, 0x4b, 0x01, 0xe1, 0x2e,
  43528. 0x8e, 0x6c, 0x63, 0x6f, 0x40, 0xca, 0x50, 0x3f,
  43529. 0x8c, 0x0b, 0x99, 0xe4, 0x72, 0x42, 0xb8, 0xb1,
  43530. 0xc2, 0x26, 0x48, 0xf1, 0x9c, 0x83, 0xc6, 0x37,
  43531. 0x2e, 0x5a, 0xae, 0x11, 0x09, 0xd9, 0xf3, 0xad,
  43532. 0x1f, 0x6f, 0xad, 0xad, 0x50, 0xe3, 0x78, 0x32,
  43533. 0xe6, 0xde, 0x8e, 0xaa, 0xbf, 0xd1, 0x00, 0x9f,
  43534. 0xb3, 0x02, 0x12, 0x19, 0xa2, 0x15, 0xec, 0x14,
  43535. 0x18, 0x5c, 0x0e, 0x26, 0xce, 0xf9, 0xae, 0xcc,
  43536. 0x7b, 0xb5, 0xd1, 0x26, 0xfc, 0x85, 0xfe, 0x14,
  43537. 0x93, 0xb6, 0x9d, 0x7d, 0x76, 0xe3, 0x35, 0x97,
  43538. 0x1e, 0xde, 0xc4
  43539. };
  43540. int derInLen = sizeof(derIn);
  43541. byte* derOut = NULL;
  43542. int derOutLen;
  43543. byte* p = derIn;
  43544. /* Check that params can be successfully decoded. */
  43545. ExpectNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&p, derInLen));
  43546. /* Check that params can be successfully encoded. */
  43547. ExpectIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  43548. /* Ensure that the encoded version matches the original. */
  43549. ExpectIntEQ(derInLen, derOutLen);
  43550. ExpectIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  43551. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  43552. DSA_free(dsa);
  43553. #endif
  43554. return EXPECT_RESULT();
  43555. }
  43556. static int test_wolfSSL_i2d_PrivateKey(void)
  43557. {
  43558. EXPECT_DECLS;
  43559. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && \
  43560. !defined(NO_ASN) && !defined(NO_PWDBASED)
  43561. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  43562. {
  43563. EVP_PKEY* pkey = NULL;
  43564. const unsigned char* server_key =
  43565. (const unsigned char*)server_key_der_2048;
  43566. unsigned char buf[FOURK_BUF];
  43567. unsigned char* pt = NULL;
  43568. int bufSz;
  43569. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  43570. (long)sizeof_server_key_der_2048));
  43571. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  43572. pt = buf;
  43573. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  43574. ExpectIntNE((pt - buf), 0);
  43575. ExpectIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  43576. EVP_PKEY_free(pkey);
  43577. }
  43578. #endif
  43579. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  43580. {
  43581. EVP_PKEY* pkey = NULL;
  43582. const unsigned char* client_key =
  43583. (const unsigned char*)ecc_clikey_der_256;
  43584. unsigned char buf[FOURK_BUF];
  43585. unsigned char* pt = NULL;
  43586. int bufSz;
  43587. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  43588. (long)sizeof_ecc_clikey_der_256)));
  43589. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  43590. pt = buf;
  43591. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  43592. ExpectIntNE((pt - buf), 0);
  43593. ExpectIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  43594. EVP_PKEY_free(pkey);
  43595. }
  43596. #endif
  43597. #endif
  43598. return EXPECT_RESULT();
  43599. }
  43600. static int test_wolfSSL_OCSP_id_get0_info(void)
  43601. {
  43602. EXPECT_DECLS;
  43603. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && \
  43604. defined(HAVE_OCSP) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  43605. X509* cert = NULL;
  43606. X509* issuer = NULL;
  43607. OCSP_CERTID* id = NULL;
  43608. OCSP_CERTID* id2 = NULL;
  43609. ASN1_STRING* name = NULL;
  43610. ASN1_OBJECT* pmd = NULL;
  43611. ASN1_STRING* keyHash = NULL;
  43612. ASN1_INTEGER* serial = NULL;
  43613. ASN1_INTEGER* x509Int;
  43614. ExpectNotNull(cert = wolfSSL_X509_load_certificate_file(svrCertFile,
  43615. SSL_FILETYPE_PEM));
  43616. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caCertFile,
  43617. SSL_FILETYPE_PEM));
  43618. ExpectNotNull(id = OCSP_cert_to_id(NULL, cert, issuer));
  43619. ExpectNotNull(id2 = OCSP_cert_to_id(NULL, cert, issuer));
  43620. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  43621. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  43622. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  43623. ExpectIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  43624. ExpectIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  43625. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  43626. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  43627. ExpectNotNull(serial);
  43628. /* compare serial number to one in cert, should be equal */
  43629. ExpectNotNull(x509Int = X509_get_serialNumber(cert));
  43630. ExpectIntEQ(x509Int->length, serial->length);
  43631. ExpectIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  43632. /* test OCSP_id_cmp */
  43633. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  43634. ExpectIntNE(OCSP_id_cmp(id, NULL), 0);
  43635. ExpectIntNE(OCSP_id_cmp(NULL, id2), 0);
  43636. ExpectIntEQ(OCSP_id_cmp(id, id2), 0);
  43637. if (id != NULL) {
  43638. id->issuerHash[0] = ~id->issuerHash[0];
  43639. }
  43640. ExpectIntNE(OCSP_id_cmp(id, id2), 0);
  43641. OCSP_CERTID_free(id);
  43642. OCSP_CERTID_free(id2);
  43643. X509_free(cert); /* free's x509Int */
  43644. X509_free(issuer);
  43645. #endif
  43646. return EXPECT_RESULT();
  43647. }
  43648. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  43649. {
  43650. EXPECT_DECLS;
  43651. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  43652. WOLFSSL_OCSP_CERTID certId;
  43653. byte* targetBuffer = NULL;
  43654. byte* p;
  43655. /* OCSP CertID bytes taken from PCAP */
  43656. byte rawCertId[] = {
  43657. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  43658. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  43659. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  43660. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  43661. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  43662. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  43663. 0xcf, 0xbc, 0x91
  43664. };
  43665. int ret = 0;
  43666. int i;
  43667. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  43668. certId.rawCertId = rawCertId;
  43669. certId.rawCertIdSize = sizeof(rawCertId);
  43670. ExpectNotNull(targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL,
  43671. DYNAMIC_TYPE_TMP_BUFFER));
  43672. p = targetBuffer;
  43673. /* Function returns the size of the encoded data. */
  43674. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &p), sizeof(rawCertId));
  43675. /* If target buffer is not null, function increments targetBuffer to point
  43676. * just past the end of the encoded data. */
  43677. ExpectPtrEq(p, (targetBuffer + sizeof(rawCertId)));
  43678. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  43679. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  43680. }
  43681. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43682. targetBuffer = NULL;
  43683. /* If target buffer is null, function allocates memory for a buffer and
  43684. * copies the encoded data into it. targetBuffer then points to the start of
  43685. * this newly allocate buffer. */
  43686. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer),
  43687. sizeof(rawCertId));
  43688. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  43689. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  43690. }
  43691. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  43692. #endif
  43693. return EXPECT_RESULT();
  43694. }
  43695. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  43696. {
  43697. EXPECT_DECLS;
  43698. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  43699. WOLFSSL_OCSP_CERTID* certId;
  43700. WOLFSSL_OCSP_CERTID* certIdGood;
  43701. WOLFSSL_OCSP_CERTID* certIdBad;
  43702. const unsigned char* rawCertIdPtr;
  43703. const unsigned char rawCertId[] = {
  43704. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  43705. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  43706. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  43707. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  43708. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  43709. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  43710. 0xcf, 0xbc, 0x91
  43711. };
  43712. rawCertIdPtr = &rawCertId[0];
  43713. /* If the cert ID is NULL the function should allocate it and copy the
  43714. * data to it. */
  43715. certId = NULL;
  43716. ExpectNotNull(certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  43717. sizeof(rawCertId)));
  43718. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  43719. if (certId != NULL) {
  43720. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  43721. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  43722. }
  43723. /* If the cert ID is not NULL the function will just copy the data to it. */
  43724. ExpectNotNull(certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  43725. DYNAMIC_TYPE_TMP_BUFFER));
  43726. ExpectNotNull(certId);
  43727. ExpectNotNull(XMEMSET(certId, 0, sizeof(*certId)));
  43728. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  43729. rawCertIdPtr = &rawCertId[0];
  43730. ExpectNotNull(certIdGood = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  43731. sizeof(rawCertId)));
  43732. ExpectPtrEq(certIdGood, certId);
  43733. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  43734. if (certId != NULL) {
  43735. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  43736. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43737. certId = NULL;
  43738. }
  43739. /* The below tests should fail when passed bad parameters. NULL should
  43740. * always be returned. */
  43741. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr,
  43742. sizeof(rawCertId)));
  43743. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL,
  43744. sizeof(rawCertId)));
  43745. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0));
  43746. #endif
  43747. return EXPECT_RESULT();
  43748. }
  43749. static int test_wolfSSL_OCSP_id_cmp(void)
  43750. {
  43751. EXPECT_DECLS;
  43752. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  43753. OCSP_CERTID id1;
  43754. OCSP_CERTID id2;
  43755. XMEMSET(&id1, 0, sizeof(id1));
  43756. XMEMSET(&id2, 0, sizeof(id2));
  43757. ExpectIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  43758. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  43759. ExpectIntNE(OCSP_id_cmp(&id1, NULL), 0);
  43760. ExpectIntNE(OCSP_id_cmp(NULL, &id2), 0);
  43761. #endif
  43762. return EXPECT_RESULT();
  43763. }
  43764. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  43765. {
  43766. EXPECT_DECLS;
  43767. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  43768. WOLFSSL_OCSP_SINGLERESP single;
  43769. const WOLFSSL_OCSP_CERTID* certId;
  43770. XMEMSET(&single, 0, sizeof(single));
  43771. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  43772. ExpectPtrEq(&single, certId);
  43773. ExpectNull(wolfSSL_OCSP_SINGLERESP_get0_id(NULL));
  43774. #endif
  43775. return EXPECT_RESULT();
  43776. }
  43777. static int test_wolfSSL_OCSP_single_get0_status(void)
  43778. {
  43779. EXPECT_DECLS;
  43780. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  43781. WOLFSSL_OCSP_SINGLERESP single;
  43782. CertStatus certStatus;
  43783. WOLFSSL_ASN1_TIME* thisDate;
  43784. WOLFSSL_ASN1_TIME* nextDate;
  43785. int ret, i;
  43786. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  43787. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  43788. /* Fill the date fields with some dummy data. */
  43789. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  43790. certStatus.thisDateParsed.data[i] = i;
  43791. certStatus.nextDateParsed.data[i] = i;
  43792. }
  43793. certStatus.status = CERT_GOOD;
  43794. single.status = &certStatus;
  43795. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  43796. &nextDate);
  43797. ExpectIntEQ(ret, CERT_GOOD);
  43798. ExpectPtrEq(thisDate, &certStatus.thisDateParsed);
  43799. ExpectPtrEq(nextDate, &certStatus.nextDateParsed);
  43800. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(NULL, NULL, NULL, NULL, NULL),
  43801. CERT_GOOD);
  43802. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, NULL,
  43803. NULL), CERT_GOOD);
  43804. #endif
  43805. return EXPECT_RESULT();
  43806. }
  43807. static int test_wolfSSL_OCSP_resp_count(void)
  43808. {
  43809. EXPECT_DECLS;
  43810. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  43811. WOLFSSL_OCSP_BASICRESP basicResp;
  43812. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  43813. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  43814. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  43815. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  43816. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  43817. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 0);
  43818. basicResp.single = &singleRespOne;
  43819. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 1);
  43820. singleRespOne.next = &singleRespTwo;
  43821. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 2);
  43822. #endif
  43823. return EXPECT_RESULT();
  43824. }
  43825. static int test_wolfSSL_OCSP_resp_get0(void)
  43826. {
  43827. EXPECT_DECLS;
  43828. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  43829. WOLFSSL_OCSP_BASICRESP basicResp;
  43830. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  43831. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  43832. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  43833. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  43834. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  43835. basicResp.single = &singleRespOne;
  43836. singleRespOne.next = &singleRespTwo;
  43837. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 0), &singleRespOne);
  43838. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 1), &singleRespTwo);
  43839. #endif
  43840. return EXPECT_RESULT();
  43841. }
  43842. static int test_wolfSSL_EVP_PKEY_derive(void)
  43843. {
  43844. EXPECT_DECLS;
  43845. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  43846. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  43847. EVP_PKEY_CTX *ctx = NULL;
  43848. unsigned char *skey = NULL;
  43849. size_t skeylen;
  43850. EVP_PKEY *pkey = NULL;
  43851. EVP_PKEY *peerkey = NULL;
  43852. const unsigned char* key;
  43853. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  43854. /* DH */
  43855. key = dh_key_der_2048;
  43856. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  43857. sizeof_dh_key_der_2048)));
  43858. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  43859. key = dh_key_der_2048;
  43860. ExpectNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  43861. sizeof_dh_key_der_2048)));
  43862. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  43863. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43864. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  43865. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  43866. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  43867. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  43868. DYNAMIC_TYPE_OPENSSL));
  43869. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  43870. EVP_PKEY_CTX_free(ctx);
  43871. ctx = NULL;
  43872. EVP_PKEY_free(peerkey);
  43873. peerkey = NULL;
  43874. EVP_PKEY_free(pkey);
  43875. pkey = NULL;
  43876. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  43877. skey = NULL;
  43878. #endif
  43879. #ifdef HAVE_ECC
  43880. /* ECDH */
  43881. key = ecc_clikey_der_256;
  43882. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  43883. sizeof_ecc_clikey_der_256)));
  43884. key = ecc_clikeypub_der_256;
  43885. ExpectNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  43886. sizeof_ecc_clikeypub_der_256)));
  43887. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43888. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  43889. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  43890. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  43891. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  43892. DYNAMIC_TYPE_OPENSSL));
  43893. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  43894. EVP_PKEY_CTX_free(ctx);
  43895. EVP_PKEY_free(peerkey);
  43896. EVP_PKEY_free(pkey);
  43897. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  43898. #endif /* HAVE_ECC */
  43899. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  43900. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  43901. return EXPECT_RESULT();
  43902. }
  43903. static int test_wolfSSL_EVP_PBE_scrypt(void)
  43904. {
  43905. EXPECT_DECLS;
  43906. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  43907. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  43908. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  43909. int ret;
  43910. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  43911. int pwdlen = sizeof(pwd);
  43912. const byte salt[] = {'N','a','C','l'};
  43913. int saltlen = sizeof(salt);
  43914. byte key[80];
  43915. word64 numOvr32 = (word64)INT32_MAX + 1;
  43916. /* expected derived key for N:16, r:1, p:1 */
  43917. const byte expectedKey[] = {
  43918. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  43919. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  43920. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  43921. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  43922. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  43923. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  43924. 0xE7, 0xE9, 0xC0, 0x9A};
  43925. /* N r p mx key keylen */
  43926. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  43927. ExpectIntEQ(ret, 0); /* N must be greater than 1 */
  43928. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  43929. ExpectIntEQ(ret, 0); /* N must be power of 2 */
  43930. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  43931. ExpectIntEQ(ret, 0); /* r must be greater than 0 */
  43932. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  43933. ExpectIntEQ(ret, 0); /* p must be greater than 0 */
  43934. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  43935. ExpectIntEQ(ret, 0); /* keylen must be greater than 0 */
  43936. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  43937. ExpectIntEQ(ret, 0); /* r must be smaller than 9 */
  43938. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  43939. ExpectIntEQ(ret, 1); /* should succeed if key is NULL */
  43940. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  43941. ExpectIntEQ(ret, 1); /* should succeed */
  43942. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  43943. key, 64);
  43944. ExpectIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  43945. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  43946. key, 64);
  43947. ExpectIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  43948. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  43949. ExpectIntEQ(ret, 1); /* should succeed even if salt is NULL */
  43950. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  43951. ExpectIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  43952. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  43953. ExpectIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  43954. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  43955. ExpectIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  43956. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  43957. ExpectIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  43958. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  43959. ExpectIntEQ(ret, 1);
  43960. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  43961. ExpectIntEQ(ret, 0); /* derived key must be the same as expected-key */
  43962. #endif /* !NO_PWDBASED && !NO_SHA256 */
  43963. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  43964. return EXPECT_RESULT();
  43965. }
  43966. static int test_no_op_functions(void)
  43967. {
  43968. EXPECT_DECLS;
  43969. #if defined(OPENSSL_EXTRA)
  43970. /* this makes sure wolfSSL can compile and run these no-op functions */
  43971. SSL_load_error_strings();
  43972. ENGINE_load_builtin_engines();
  43973. OpenSSL_add_all_ciphers();
  43974. ExpectIntEQ(CRYPTO_malloc_init(), 0);
  43975. #endif
  43976. return EXPECT_RESULT();
  43977. }
  43978. static int test_wolfSSL_CRYPTO_memcmp(void)
  43979. {
  43980. EXPECT_DECLS;
  43981. #ifdef OPENSSL_EXTRA
  43982. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  43983. "implementation of TLS/SSL for embedded devices to the cloud.";
  43984. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  43985. "implementation of TLS/SSL for embedded devices to the cloud.";
  43986. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  43987. "implementation of TLS/SSL for embedded devices to the cloud!";
  43988. ExpectIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  43989. ExpectIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  43990. #endif
  43991. return EXPECT_RESULT();
  43992. }
  43993. /*----------------------------------------------------------------------------*
  43994. | wolfCrypt ASN
  43995. *----------------------------------------------------------------------------*/
  43996. static int test_wc_CreateEncryptedPKCS8Key(void)
  43997. {
  43998. EXPECT_DECLS;
  43999. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  44000. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  44001. WC_RNG rng;
  44002. byte* encKey = NULL;
  44003. word32 encKeySz = 0;
  44004. word32 decKeySz = 0;
  44005. const char password[] = "Lorem ipsum dolor sit amet";
  44006. word32 passwordSz = (word32)XSTRLEN(password);
  44007. word32 tradIdx = 0;
  44008. XMEMSET(&rng, 0, sizeof(WC_RNG));
  44009. ExpectIntEQ(wc_InitRng(&rng), 0);
  44010. /* Call with NULL for out buffer to get necessary length. */
  44011. ExpectIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  44012. sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
  44013. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  44014. LENGTH_ONLY_E);
  44015. ExpectNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  44016. DYNAMIC_TYPE_TMP_BUFFER));
  44017. /* Call with the allocated out buffer. */
  44018. ExpectIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  44019. sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
  44020. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  44021. 0);
  44022. /* Decrypt the encrypted PKCS8 key we just made. */
  44023. ExpectIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
  44024. passwordSz)), 0);
  44025. /* encKey now holds the decrypted key (decrypted in place). */
  44026. ExpectIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  44027. /* Check that the decrypted key matches the key prior to encryption. */
  44028. ExpectIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  44029. sizeof_server_key_der_2048), 0);
  44030. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44031. wc_FreeRng(&rng);
  44032. #endif
  44033. return EXPECT_RESULT();
  44034. }
  44035. static int test_wc_GetPkcs8TraditionalOffset(void)
  44036. {
  44037. EXPECT_DECLS;
  44038. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  44039. int length;
  44040. int derSz = 0;
  44041. word32 inOutIdx;
  44042. const char* path = "./certs/server-keyPkcs8.der";
  44043. XFILE file = XBADFILE;
  44044. byte der[2048];
  44045. ExpectTrue((file = XFOPEN(path, "rb")) != XBADFILE);
  44046. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), file), 0);
  44047. if (file != XBADFILE)
  44048. XFCLOSE(file);
  44049. /* valid case */
  44050. inOutIdx = 0;
  44051. ExpectIntGT(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  44052. 0);
  44053. /* inOutIdx > sz */
  44054. inOutIdx = 4000;
  44055. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  44056. BAD_FUNC_ARG);
  44057. /* null input */
  44058. inOutIdx = 0;
  44059. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0),
  44060. BAD_FUNC_ARG);
  44061. /* invalid input, fill buffer with 1's */
  44062. XMEMSET(der, 1, sizeof(der));
  44063. inOutIdx = 0;
  44064. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  44065. ASN_PARSE_E);
  44066. #endif /* NO_ASN */
  44067. return EXPECT_RESULT();
  44068. }
  44069. static int test_wc_SetSubjectRaw(void)
  44070. {
  44071. EXPECT_DECLS;
  44072. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44073. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  44074. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  44075. WOLFSSL_X509* x509 = NULL;
  44076. int peerCertSz;
  44077. const byte* peerCertBuf = NULL;
  44078. Cert forgedCert;
  44079. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  44080. WOLFSSL_FILETYPE_ASN1));
  44081. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  44082. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  44083. ExpectIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  44084. wolfSSL_FreeX509(x509);
  44085. #endif
  44086. return EXPECT_RESULT();
  44087. }
  44088. static int test_wc_GetSubjectRaw(void)
  44089. {
  44090. EXPECT_DECLS;
  44091. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44092. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  44093. Cert cert;
  44094. byte *subjectRaw;
  44095. ExpectIntEQ(0, wc_InitCert(&cert));
  44096. ExpectIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  44097. #endif
  44098. return EXPECT_RESULT();
  44099. }
  44100. static int test_wc_SetIssuerRaw(void)
  44101. {
  44102. EXPECT_DECLS;
  44103. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44104. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  44105. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  44106. WOLFSSL_X509* x509 = NULL;
  44107. int peerCertSz;
  44108. const byte* peerCertBuf;
  44109. Cert forgedCert;
  44110. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  44111. WOLFSSL_FILETYPE_ASN1));
  44112. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  44113. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  44114. ExpectIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  44115. wolfSSL_FreeX509(x509);
  44116. #endif
  44117. return EXPECT_RESULT();
  44118. }
  44119. static int test_wc_SetIssueBuffer(void)
  44120. {
  44121. EXPECT_DECLS;
  44122. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44123. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  44124. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  44125. WOLFSSL_X509* x509 = NULL;
  44126. int peerCertSz;
  44127. const byte* peerCertBuf;
  44128. Cert forgedCert;
  44129. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  44130. WOLFSSL_FILETYPE_ASN1));
  44131. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  44132. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  44133. ExpectIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  44134. wolfSSL_FreeX509(x509);
  44135. #endif
  44136. return EXPECT_RESULT();
  44137. }
  44138. /*
  44139. * Testing wc_SetSubjectKeyId
  44140. */
  44141. static int test_wc_SetSubjectKeyId(void)
  44142. {
  44143. EXPECT_DECLS;
  44144. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44145. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  44146. Cert cert;
  44147. const char* file = "certs/ecc-client-keyPub.pem";
  44148. ExpectIntEQ(0, wc_InitCert(&cert));
  44149. ExpectIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  44150. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  44151. ExpectIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  44152. #endif
  44153. return EXPECT_RESULT();
  44154. } /* END test_wc_SetSubjectKeyId */
  44155. /*
  44156. * Testing wc_SetSubject
  44157. */
  44158. static int test_wc_SetSubject(void)
  44159. {
  44160. EXPECT_DECLS;
  44161. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44162. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  44163. Cert cert;
  44164. const char* file = "./certs/ca-ecc-cert.pem";
  44165. ExpectIntEQ(0, wc_InitCert(&cert));
  44166. ExpectIntEQ(0, wc_SetSubject(&cert, file));
  44167. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  44168. ExpectIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  44169. #endif
  44170. return EXPECT_RESULT();
  44171. } /* END test_wc_SetSubject */
  44172. static int test_CheckCertSignature(void)
  44173. {
  44174. EXPECT_DECLS;
  44175. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  44176. WOLFSSL_CERT_MANAGER* cm = NULL;
  44177. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  44178. XFILE fp = XBADFILE;
  44179. byte cert[4096];
  44180. int certSz;
  44181. #endif
  44182. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  44183. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  44184. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  44185. #ifndef NO_RSA
  44186. #ifdef USE_CERT_BUFFERS_1024
  44187. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  44188. sizeof_server_cert_der_1024, NULL, cm));
  44189. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  44190. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  44191. WOLFSSL_FILETYPE_ASN1));
  44192. ExpectIntEQ(0, CheckCertSignature(server_cert_der_1024,
  44193. sizeof_server_cert_der_1024, NULL, cm));
  44194. #elif defined(USE_CERT_BUFFERS_2048)
  44195. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  44196. sizeof_server_cert_der_2048, NULL, cm));
  44197. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  44198. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  44199. WOLFSSL_FILETYPE_ASN1));
  44200. ExpectIntEQ(0, CheckCertSignature(server_cert_der_2048,
  44201. sizeof_server_cert_der_2048, NULL, cm));
  44202. #endif
  44203. #endif
  44204. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  44205. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  44206. sizeof_serv_ecc_der_256, NULL, cm));
  44207. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  44208. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  44209. WOLFSSL_FILETYPE_ASN1));
  44210. ExpectIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  44211. NULL, cm));
  44212. #endif
  44213. #if !defined(NO_FILESYSTEM)
  44214. wolfSSL_CertManagerFree(cm);
  44215. cm = NULL;
  44216. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  44217. #ifndef NO_RSA
  44218. ExpectTrue((fp = XFOPEN("./certs/server-cert.der", "rb")) != XBADFILE);
  44219. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  44220. if (fp != XBADFILE) {
  44221. XFCLOSE(fp);
  44222. fp = XBADFILE;
  44223. }
  44224. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  44225. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  44226. "./certs/ca-cert.pem", NULL));
  44227. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  44228. #endif
  44229. #ifdef HAVE_ECC
  44230. ExpectTrue((fp = XFOPEN("./certs/server-ecc.der", "rb")) != XBADFILE);
  44231. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  44232. if (fp != XBADFILE) {
  44233. XFCLOSE(fp);
  44234. fp = XBADFILE;
  44235. }
  44236. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  44237. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  44238. "./certs/ca-ecc-cert.pem", NULL));
  44239. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  44240. #endif
  44241. #endif
  44242. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  44243. (void)fp;
  44244. (void)cert;
  44245. (void)certSz;
  44246. #endif
  44247. wolfSSL_CertManagerFree(cm);
  44248. #endif
  44249. return EXPECT_RESULT();
  44250. }
  44251. static int test_wc_ParseCert(void)
  44252. {
  44253. EXPECT_DECLS;
  44254. #if !defined(NO_CERTS) && !defined(NO_RSA)
  44255. DecodedCert decodedCert;
  44256. const byte* rawCert = client_cert_der_2048;
  44257. const int rawCertSize = sizeof_client_cert_der_2048;
  44258. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  44259. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  44260. #ifndef IGNORE_NAME_CONSTRAINTS
  44261. /* check that the subjects emailAddress was not put in the alt name list */
  44262. ExpectNotNull(decodedCert.subjectEmail);
  44263. ExpectNull(decodedCert.altEmailNames);
  44264. #endif
  44265. wc_FreeDecodedCert(&decodedCert);
  44266. #endif
  44267. return EXPECT_RESULT();
  44268. }
  44269. /* Test wc_ParseCert decoding of various encodings and scenarios ensuring that
  44270. * the API safely errors out on badly-formed ASN input.
  44271. * NOTE: Test not compatible with released FIPS implementations!
  44272. */
  44273. static int test_wc_ParseCert_Error(void)
  44274. {
  44275. EXPECT_DECLS;
  44276. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(HAVE_SELFTEST) && \
  44277. (!defined(HAVE_FIPS) || \
  44278. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  44279. DecodedCert decodedCert;
  44280. int i;
  44281. /* Certificate data */
  44282. const byte c0[] = { 0x30, 0x04, 0x30, 0x02, 0x02, 0x80, 0x00, 0x00};
  44283. const byte c1[] = { 0x30, 0x04, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  44284. const byte c2[] = { 0x30, 0x06, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  44285. const byte c3[] = { 0x30, 0x07, 0x30, 0x05, 0x02, 0x80, 0x10, 0x00, 0x00};
  44286. const byte c4[] = { 0x02, 0x80, 0x10, 0x00, 0x00};
  44287. /* Test data */
  44288. const struct testStruct {
  44289. const byte* c;
  44290. const int cSz;
  44291. const int expRet;
  44292. } t[] = {
  44293. {c0, sizeof(c0), ASN_PARSE_E}, /* Invalid bit-string length */
  44294. {c1, sizeof(c1), ASN_PARSE_E}, /* Invalid bit-string length */
  44295. {c2, sizeof(c2), ASN_PARSE_E}, /* Invalid integer length (zero) */
  44296. {c3, sizeof(c3), ASN_PARSE_E}, /* Valid INTEGER, but buffer too short */
  44297. {c4, sizeof(c4), ASN_PARSE_E}, /* Valid INTEGER, but not in bit-string */
  44298. };
  44299. const int tSz = (int)(sizeof(t) / sizeof(struct testStruct));
  44300. for (i = 0; i < tSz; i++) {
  44301. WOLFSSL_MSG_EX("i == %d", i);
  44302. wc_InitDecodedCert(&decodedCert, t[i].c, t[i].cSz, NULL);
  44303. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), t[i].expRet);
  44304. wc_FreeDecodedCert(&decodedCert);
  44305. }
  44306. #endif
  44307. return EXPECT_RESULT();
  44308. }
  44309. static int test_MakeCertWithPathLen(void)
  44310. {
  44311. EXPECT_DECLS;
  44312. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  44313. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  44314. const byte expectedPathLen = 7;
  44315. Cert cert;
  44316. DecodedCert decodedCert;
  44317. byte der[FOURK_BUF];
  44318. int derSize = 0;
  44319. WC_RNG rng;
  44320. ecc_key key;
  44321. int ret;
  44322. XMEMSET(&rng, 0, sizeof(WC_RNG));
  44323. XMEMSET(&key, 0, sizeof(ecc_key));
  44324. XMEMSET(&cert, 0, sizeof(Cert));
  44325. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  44326. ExpectIntEQ(wc_InitRng(&rng), 0);
  44327. ExpectIntEQ(wc_ecc_init(&key), 0);
  44328. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  44329. ExpectIntEQ(wc_InitCert(&cert), 0);
  44330. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  44331. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  44332. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  44333. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  44334. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  44335. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  44336. CTC_NAME_SIZE);
  44337. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  44338. CTC_NAME_SIZE);
  44339. cert.selfSigned = 1;
  44340. cert.isCA = 1;
  44341. cert.pathLen = expectedPathLen;
  44342. cert.pathLenSet = 1;
  44343. cert.sigType = CTC_SHA256wECDSA;
  44344. #ifdef WOLFSSL_CERT_EXT
  44345. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  44346. #endif
  44347. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  44348. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  44349. FOURK_BUF, NULL, &key, &rng), 0);
  44350. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  44351. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  44352. ExpectIntEQ(decodedCert.pathLength, expectedPathLen);
  44353. wc_FreeDecodedCert(&decodedCert);
  44354. ret = wc_ecc_free(&key);
  44355. ExpectIntEQ(ret, 0);
  44356. ret = wc_FreeRng(&rng);
  44357. ExpectIntEQ(ret, 0);
  44358. #endif
  44359. return EXPECT_RESULT();
  44360. }
  44361. static int test_MakeCertWithCaFalse(void)
  44362. {
  44363. EXPECT_DECLS;
  44364. #if defined(WOLFSSL_ALLOW_ENCODING_CA_FALSE) && defined(WOLFSSL_CERT_REQ) && \
  44365. !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  44366. const byte expectedIsCa = 0;
  44367. Cert cert;
  44368. DecodedCert decodedCert;
  44369. byte der[FOURK_BUF];
  44370. int derSize = 0;
  44371. WC_RNG rng;
  44372. ecc_key key;
  44373. int ret;
  44374. XMEMSET(&rng, 0, sizeof(WC_RNG));
  44375. XMEMSET(&key, 0, sizeof(ecc_key));
  44376. XMEMSET(&cert, 0, sizeof(Cert));
  44377. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  44378. ExpectIntEQ(wc_InitRng(&rng), 0);
  44379. ExpectIntEQ(wc_ecc_init(&key), 0);
  44380. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  44381. ExpectIntEQ(wc_InitCert(&cert), 0);
  44382. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  44383. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  44384. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  44385. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  44386. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  44387. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  44388. CTC_NAME_SIZE);
  44389. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  44390. CTC_NAME_SIZE);
  44391. cert.selfSigned = 1;
  44392. cert.isCA = expectedIsCa;
  44393. cert.isCaSet = 1;
  44394. cert.sigType = CTC_SHA256wECDSA;
  44395. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  44396. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  44397. FOURK_BUF, NULL, &key, &rng), 0);
  44398. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  44399. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  44400. ExpectIntEQ(decodedCert.isCA, expectedIsCa);
  44401. wc_FreeDecodedCert(&decodedCert);
  44402. ret = wc_ecc_free(&key);
  44403. ExpectIntEQ(ret, 0);
  44404. ret = wc_FreeRng(&rng);
  44405. ExpectIntEQ(ret, 0);
  44406. #endif
  44407. return EXPECT_RESULT();
  44408. }
  44409. /*----------------------------------------------------------------------------*
  44410. | wolfCrypt ECC
  44411. *----------------------------------------------------------------------------*/
  44412. static int test_wc_ecc_get_curve_size_from_name(void)
  44413. {
  44414. EXPECT_DECLS;
  44415. #ifdef HAVE_ECC
  44416. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  44417. ExpectIntEQ(wc_ecc_get_curve_size_from_name("SECP256R1"), 32);
  44418. #endif
  44419. /* invalid case */
  44420. ExpectIntEQ(wc_ecc_get_curve_size_from_name("BADCURVE"), -1);
  44421. /* NULL input */
  44422. ExpectIntEQ(wc_ecc_get_curve_size_from_name(NULL), BAD_FUNC_ARG);
  44423. #endif /* HAVE_ECC */
  44424. return EXPECT_RESULT();
  44425. }
  44426. static int test_wc_ecc_get_curve_id_from_name(void)
  44427. {
  44428. EXPECT_DECLS;
  44429. #ifdef HAVE_ECC
  44430. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  44431. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"),
  44432. ECC_SECP256R1);
  44433. #endif
  44434. /* invalid case */
  44435. ExpectIntEQ(wc_ecc_get_curve_id_from_name("BADCURVE"), -1);
  44436. /* NULL input */
  44437. ExpectIntEQ(wc_ecc_get_curve_id_from_name(NULL), BAD_FUNC_ARG);
  44438. #endif /* HAVE_ECC */
  44439. return EXPECT_RESULT();
  44440. }
  44441. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  44442. !defined(HAVE_SELFTEST) && \
  44443. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  44444. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  44445. {
  44446. EXPECT_DECLS;
  44447. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  44448. ecc_key* key;
  44449. const ecc_set_type* params = NULL;
  44450. int ret;
  44451. #endif
  44452. WOLFSSL_EC_KEY *ecKey = NULL;
  44453. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  44454. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"), ECC_SECP256R1);
  44455. ExpectNotNull(ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  44456. if (EXPECT_SUCCESS()) {
  44457. ret = EC_KEY_generate_key(ecKey);
  44458. } else
  44459. ret = 0;
  44460. if (ret == 1) {
  44461. /* normal test */
  44462. key = (ecc_key*)ecKey->internal;
  44463. if (key != NULL) {
  44464. params = key->dp;
  44465. }
  44466. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(params),
  44467. ECC_SECP256R1);
  44468. }
  44469. #endif
  44470. /* invalid case, NULL input*/
  44471. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(NULL), BAD_FUNC_ARG);
  44472. wolfSSL_EC_KEY_free(ecKey);
  44473. return EXPECT_RESULT();
  44474. }
  44475. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  44476. static int test_wc_ecc_get_curve_id_from_params(void)
  44477. {
  44478. EXPECT_DECLS;
  44479. #ifdef HAVE_ECC
  44480. const byte prime[] =
  44481. {
  44482. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  44483. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  44484. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  44485. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  44486. };
  44487. const byte primeInvalid[] =
  44488. {
  44489. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  44490. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  44491. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  44492. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  44493. };
  44494. const byte Af[] =
  44495. {
  44496. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  44497. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  44498. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  44499. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  44500. };
  44501. const byte Bf[] =
  44502. {
  44503. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  44504. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  44505. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  44506. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  44507. };
  44508. const byte order[] =
  44509. {
  44510. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  44511. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  44512. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  44513. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  44514. };
  44515. const byte Gx[] =
  44516. {
  44517. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  44518. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  44519. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  44520. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  44521. };
  44522. const byte Gy[] =
  44523. {
  44524. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  44525. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  44526. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  44527. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  44528. };
  44529. int cofactor = 1;
  44530. int fieldSize = 256;
  44531. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  44532. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  44533. prime, sizeof(prime), Af, sizeof(Af), Bf, sizeof(Bf),
  44534. order, sizeof(order), Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor),
  44535. ECC_SECP256R1);
  44536. #endif
  44537. /* invalid case, fieldSize = 0 */
  44538. ExpectIntEQ(wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  44539. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  44540. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  44541. /* invalid case, NULL prime */
  44542. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  44543. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  44544. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), BAD_FUNC_ARG);
  44545. /* invalid case, invalid prime */
  44546. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  44547. primeInvalid, sizeof(primeInvalid),
  44548. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  44549. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  44550. #endif
  44551. return EXPECT_RESULT();
  44552. }
  44553. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  44554. {
  44555. EXPECT_DECLS;
  44556. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  44557. !defined(HAVE_FAST_RSA)
  44558. WOLFSSL_RSA* rsa = NULL;
  44559. WOLFSSL_EVP_PKEY* pkey = NULL;
  44560. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  44561. const char* in = "What is easy to do is easy not to do.";
  44562. size_t inlen = XSTRLEN(in);
  44563. size_t outEncLen = 0;
  44564. byte* outEnc = NULL;
  44565. byte* outDec = NULL;
  44566. size_t outDecLen = 0;
  44567. size_t rsaKeySz = 2048/8; /* Bytes */
  44568. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  44569. byte* inTmp = NULL;
  44570. byte* outEncTmp = NULL;
  44571. byte* outDecTmp = NULL;
  44572. #endif
  44573. ExpectNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  44574. DYNAMIC_TYPE_TMP_BUFFER));
  44575. if (outEnc != NULL) {
  44576. XMEMSET(outEnc, 0, rsaKeySz);
  44577. }
  44578. ExpectNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  44579. DYNAMIC_TYPE_TMP_BUFFER));
  44580. if (outDec != NULL) {
  44581. XMEMSET(outDec, 0, rsaKeySz);
  44582. }
  44583. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  44584. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44585. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  44586. if (EXPECT_FAIL()) {
  44587. RSA_free(rsa);
  44588. }
  44589. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44590. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  44591. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  44592. WOLFSSL_SUCCESS);
  44593. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  44594. since ctx uses it.*/
  44595. ExpectIntEQ(pkey->ref.count, 2);
  44596. EVP_PKEY_free(pkey);
  44597. ExpectIntEQ(pkey->ref.count, 1);
  44598. /* Encrypt data */
  44599. /* Check that we can get the required output buffer length by passing in a
  44600. * NULL output buffer. */
  44601. ExpectIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  44602. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  44603. ExpectIntEQ(rsaKeySz, outEncLen);
  44604. /* Now do the actual encryption. */
  44605. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  44606. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  44607. /* Decrypt data */
  44608. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  44609. /* Check that we can get the required output buffer length by passing in a
  44610. * NULL output buffer. */
  44611. ExpectIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  44612. WOLFSSL_SUCCESS);
  44613. ExpectIntEQ(rsaKeySz, outDecLen);
  44614. /* Now do the actual decryption. */
  44615. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  44616. WOLFSSL_SUCCESS);
  44617. ExpectIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  44618. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  44619. /* The input length must be the same size as the RSA key.*/
  44620. ExpectNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  44621. DYNAMIC_TYPE_TMP_BUFFER));
  44622. if (inTmp != NULL) {
  44623. XMEMSET(inTmp, 9, rsaKeySz);
  44624. }
  44625. ExpectNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  44626. DYNAMIC_TYPE_TMP_BUFFER));
  44627. if (outEncTmp != NULL) {
  44628. XMEMSET(outEncTmp, 0, rsaKeySz);
  44629. }
  44630. ExpectNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  44631. DYNAMIC_TYPE_TMP_BUFFER));
  44632. if (outDecTmp != NULL) {
  44633. XMEMSET(outDecTmp, 0, rsaKeySz);
  44634. }
  44635. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  44636. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  44637. WOLFSSL_SUCCESS);
  44638. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  44639. WOLFSSL_SUCCESS);
  44640. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  44641. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp,
  44642. outEncLen), WOLFSSL_SUCCESS);
  44643. ExpectIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  44644. #endif
  44645. EVP_PKEY_CTX_free(ctx);
  44646. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44647. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44648. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  44649. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44650. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44651. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44652. #endif
  44653. #endif
  44654. return EXPECT_RESULT();
  44655. }
  44656. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  44657. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  44658. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44659. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  44660. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  44661. #endif
  44662. #endif
  44663. #endif
  44664. #if defined(OPENSSL_EXTRA)
  44665. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  44666. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  44667. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  44668. #endif
  44669. #endif
  44670. #endif
  44671. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  44672. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44673. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  44674. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  44675. #endif
  44676. #endif
  44677. #endif
  44678. #ifdef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  44679. static int test_wolfSSL_EVP_PKEY_sign_verify(int keyType)
  44680. {
  44681. EXPECT_DECLS;
  44682. #if defined(OPENSSL_EXTRA)
  44683. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  44684. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  44685. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44686. WOLFSSL_RSA* rsa = NULL;
  44687. #endif
  44688. #endif
  44689. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  44690. WOLFSSL_DSA* dsa = NULL;
  44691. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  44692. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  44693. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44694. WOLFSSL_EC_KEY* ecKey = NULL;
  44695. #endif
  44696. #endif
  44697. WOLFSSL_EVP_PKEY* pkey = NULL;
  44698. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  44699. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  44700. const char* in = "What is easy to do is easy not to do.";
  44701. size_t inlen = XSTRLEN(in);
  44702. byte hash[SHA256_DIGEST_LENGTH] = {0};
  44703. byte zero[SHA256_DIGEST_LENGTH] = {0};
  44704. SHA256_CTX c;
  44705. byte* sig = NULL;
  44706. byte* sigVerify = NULL;
  44707. size_t siglen;
  44708. size_t siglenOnlyLen;
  44709. size_t keySz = 2048/8; /* Bytes */
  44710. ExpectNotNull(sig =
  44711. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  44712. ExpectNotNull(sigVerify =
  44713. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  44714. siglen = keySz;
  44715. ExpectNotNull(XMEMSET(sig, 0, keySz));
  44716. ExpectNotNull(XMEMSET(sigVerify, 0, keySz));
  44717. /* Generate hash */
  44718. SHA256_Init(&c);
  44719. SHA256_Update(&c, in, inlen);
  44720. SHA256_Final(hash, &c);
  44721. #ifdef WOLFSSL_SMALL_STACK_CACHE
  44722. /* workaround for small stack cache case */
  44723. wc_Sha256Free((wc_Sha256*)&c);
  44724. #endif
  44725. /* Generate key */
  44726. ExpectNotNull(pkey = EVP_PKEY_new());
  44727. switch (keyType) {
  44728. case EVP_PKEY_RSA:
  44729. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  44730. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  44731. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44732. {
  44733. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  44734. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  44735. }
  44736. #endif
  44737. #endif
  44738. break;
  44739. case EVP_PKEY_DSA:
  44740. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  44741. ExpectNotNull(dsa = DSA_new());
  44742. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  44743. NULL, 0, NULL, NULL, NULL), 1);
  44744. ExpectIntEQ(DSA_generate_key(dsa), 1);
  44745. ExpectIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  44746. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  44747. break;
  44748. case EVP_PKEY_EC:
  44749. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  44750. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44751. {
  44752. ExpectNotNull(ecKey = EC_KEY_new());
  44753. ExpectIntEQ(EC_KEY_generate_key(ecKey), 1);
  44754. ExpectIntEQ(
  44755. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  44756. if (EXPECT_FAIL()) {
  44757. EC_KEY_free(ecKey);
  44758. }
  44759. }
  44760. #endif
  44761. #endif
  44762. break;
  44763. }
  44764. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44765. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  44766. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  44767. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  44768. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44769. if (keyType == EVP_PKEY_RSA)
  44770. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  44771. WOLFSSL_SUCCESS);
  44772. #endif
  44773. #endif
  44774. /* Check returning only length */
  44775. ExpectIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  44776. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  44777. ExpectIntGT(siglenOnlyLen, 0);
  44778. /* Sign data */
  44779. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  44780. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  44781. ExpectIntGE(siglenOnlyLen, siglen);
  44782. /* Verify signature */
  44783. ExpectNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  44784. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  44785. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  44786. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  44787. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44788. if (keyType == EVP_PKEY_RSA)
  44789. ExpectIntEQ(
  44790. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  44791. WOLFSSL_SUCCESS);
  44792. #endif
  44793. #endif
  44794. ExpectIntEQ(EVP_PKEY_verify(
  44795. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  44796. WOLFSSL_SUCCESS);
  44797. ExpectIntEQ(EVP_PKEY_verify(
  44798. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  44799. WOLFSSL_FAILURE);
  44800. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  44801. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  44802. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44803. if (keyType == EVP_PKEY_RSA) {
  44804. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  44805. /* Try RSA sign/verify with no padding. */
  44806. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  44807. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  44808. WOLFSSL_SUCCESS);
  44809. ExpectIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  44810. siglen), WOLFSSL_SUCCESS);
  44811. ExpectIntGE(siglenOnlyLen, siglen);
  44812. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  44813. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  44814. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  44815. ExpectIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  44816. siglen), WOLFSSL_SUCCESS);
  44817. #endif
  44818. /* Wrong padding schemes. */
  44819. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  44820. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  44821. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  44822. ExpectIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  44823. siglen), WOLFSSL_SUCCESS);
  44824. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  44825. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  44826. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  44827. ExpectIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  44828. siglen), WOLFSSL_SUCCESS);
  44829. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  44830. WOLFSSL_SUCCESS);
  44831. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  44832. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  44833. }
  44834. #endif
  44835. #endif
  44836. /* error cases */
  44837. siglen = keySz; /* Reset because sig size may vary slightly */
  44838. ExpectIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  44839. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  44840. ExpectIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  44841. WOLFSSL_SUCCESS);
  44842. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  44843. WOLFSSL_SUCCESS);
  44844. EVP_PKEY_free(pkey);
  44845. pkey = NULL;
  44846. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  44847. DSA_free(dsa);
  44848. dsa = NULL;
  44849. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  44850. EVP_PKEY_CTX_free(ctx_verify);
  44851. ctx_verify = NULL;
  44852. EVP_PKEY_CTX_free(ctx);
  44853. ctx = NULL;
  44854. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44855. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44856. #endif /* OPENSSL_EXTRA */
  44857. return EXPECT_RESULT();
  44858. }
  44859. #endif
  44860. static int test_wolfSSL_EVP_PKEY_sign_verify_rsa(void)
  44861. {
  44862. EXPECT_DECLS;
  44863. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  44864. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  44865. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44866. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_RSA), TEST_SUCCESS);
  44867. #endif
  44868. #endif
  44869. return EXPECT_RESULT();
  44870. }
  44871. static int test_wolfSSL_EVP_PKEY_sign_verify_dsa(void)
  44872. {
  44873. EXPECT_DECLS;
  44874. #if defined(OPENSSL_EXTRA)
  44875. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  44876. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_DSA), TEST_SUCCESS);
  44877. #endif
  44878. #endif
  44879. return EXPECT_RESULT();
  44880. }
  44881. static int test_wolfSSL_EVP_PKEY_sign_verify_ec(void)
  44882. {
  44883. EXPECT_DECLS;
  44884. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  44885. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44886. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_EC), TEST_SUCCESS);
  44887. #endif
  44888. #endif
  44889. return EXPECT_RESULT();
  44890. }
  44891. static int test_EVP_PKEY_rsa(void)
  44892. {
  44893. EXPECT_DECLS;
  44894. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  44895. WOLFSSL_RSA* rsa = NULL;
  44896. WOLFSSL_EVP_PKEY* pkey = NULL;
  44897. ExpectNotNull(rsa = wolfSSL_RSA_new());
  44898. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44899. ExpectIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  44900. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  44901. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  44902. if (EXPECT_FAIL()) {
  44903. wolfSSL_RSA_free(rsa);
  44904. }
  44905. ExpectPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  44906. wolfSSL_EVP_PKEY_free(pkey);
  44907. #endif
  44908. return EXPECT_RESULT();
  44909. }
  44910. static int test_EVP_PKEY_ec(void)
  44911. {
  44912. EXPECT_DECLS;
  44913. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  44914. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44915. WOLFSSL_EC_KEY* ecKey = NULL;
  44916. WOLFSSL_EVP_PKEY* pkey = NULL;
  44917. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  44918. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44919. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  44920. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  44921. /* Should fail since ecKey is empty */
  44922. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  44923. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  44924. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  44925. if (EXPECT_FAIL()) {
  44926. wolfSSL_EC_KEY_free(ecKey);
  44927. }
  44928. wolfSSL_EVP_PKEY_free(pkey);
  44929. #endif
  44930. #endif
  44931. return EXPECT_RESULT();
  44932. }
  44933. static int test_EVP_PKEY_cmp(void)
  44934. {
  44935. EXPECT_DECLS;
  44936. #if defined(OPENSSL_EXTRA)
  44937. EVP_PKEY *a = NULL;
  44938. EVP_PKEY *b = NULL;
  44939. const unsigned char *in;
  44940. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  44941. in = client_key_der_2048;
  44942. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  44943. &in, (long)sizeof_client_key_der_2048));
  44944. in = client_key_der_2048;
  44945. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  44946. &in, (long)sizeof_client_key_der_2048));
  44947. /* Test success case RSA */
  44948. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  44949. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  44950. #else
  44951. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  44952. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  44953. EVP_PKEY_free(b);
  44954. b = NULL;
  44955. EVP_PKEY_free(a);
  44956. a = NULL;
  44957. #endif
  44958. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  44959. in = ecc_clikey_der_256;
  44960. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  44961. &in, (long)sizeof_ecc_clikey_der_256));
  44962. in = ecc_clikey_der_256;
  44963. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  44964. &in, (long)sizeof_ecc_clikey_der_256));
  44965. /* Test success case ECC */
  44966. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  44967. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  44968. #else
  44969. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  44970. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  44971. EVP_PKEY_free(b);
  44972. b = NULL;
  44973. EVP_PKEY_free(a);
  44974. a = NULL;
  44975. #endif
  44976. /* Test failure cases */
  44977. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  44978. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  44979. in = client_key_der_2048;
  44980. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  44981. &in, (long)sizeof_client_key_der_2048));
  44982. in = ecc_clikey_der_256;
  44983. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  44984. &in, (long)sizeof_ecc_clikey_der_256));
  44985. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  44986. ExpectIntEQ(EVP_PKEY_cmp(a, b), -1);
  44987. #else
  44988. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  44989. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  44990. EVP_PKEY_free(b);
  44991. b = NULL;
  44992. EVP_PKEY_free(a);
  44993. a = NULL;
  44994. #endif
  44995. /* invalid or empty failure cases */
  44996. a = EVP_PKEY_new();
  44997. b = EVP_PKEY_new();
  44998. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  44999. ExpectIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  45000. ExpectIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  45001. ExpectIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  45002. #ifdef NO_RSA
  45003. /* Type check will fail since RSA is the default EVP key type */
  45004. ExpectIntEQ(EVP_PKEY_cmp(a, b), -2);
  45005. #else
  45006. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  45007. #endif
  45008. #else
  45009. ExpectIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  45010. ExpectIntNE(EVP_PKEY_cmp(a, NULL), 0);
  45011. ExpectIntNE(EVP_PKEY_cmp(NULL, b), 0);
  45012. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  45013. #endif
  45014. EVP_PKEY_free(b);
  45015. EVP_PKEY_free(a);
  45016. (void)in;
  45017. #endif
  45018. return EXPECT_RESULT();
  45019. }
  45020. static int test_ERR_load_crypto_strings(void)
  45021. {
  45022. #if defined(OPENSSL_ALL)
  45023. ERR_load_crypto_strings();
  45024. return TEST_SUCCESS;
  45025. #else
  45026. return TEST_SKIPPED;
  45027. #endif
  45028. }
  45029. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  45030. static void free_x509(X509* x)
  45031. {
  45032. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  45033. }
  45034. #endif
  45035. static int test_sk_X509(void)
  45036. {
  45037. EXPECT_DECLS;
  45038. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  45039. {
  45040. STACK_OF(X509)* s = NULL;
  45041. ExpectNotNull(s = sk_X509_new_null());
  45042. ExpectIntEQ(sk_X509_num(s), 0);
  45043. sk_X509_pop_free(s, NULL);
  45044. ExpectNotNull(s = sk_X509_new_null());
  45045. ExpectIntEQ(sk_X509_num(s), 0);
  45046. sk_X509_pop_free(s, NULL);
  45047. ExpectNotNull(s = sk_X509_new_null());
  45048. sk_X509_push(s, (X509*)1);
  45049. ExpectIntEQ(sk_X509_num(s), 1);
  45050. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  45051. sk_X509_push(s, (X509*)2);
  45052. ExpectIntEQ(sk_X509_num(s), 2);
  45053. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  45054. ExpectIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  45055. sk_X509_push(s, (X509*)2);
  45056. sk_X509_pop_free(s, free_x509);
  45057. }
  45058. {
  45059. /* Push a list of 10 X509s onto stack, then verify that
  45060. * value(), push(), shift(), and pop() behave as expected. */
  45061. STACK_OF(X509)* s = NULL;
  45062. X509* xList[10];
  45063. int i = 0;
  45064. const int len = (sizeof(xList) / sizeof(xList[0]));
  45065. for (i = 0; i < len; ++i) {
  45066. xList[i] = NULL;
  45067. ExpectNotNull(xList[i] = X509_new());
  45068. }
  45069. /* test push, pop, and free */
  45070. ExpectNotNull(s = sk_X509_new_null());
  45071. for (i = 0; i < len; ++i) {
  45072. sk_X509_push(s, xList[i]);
  45073. ExpectIntEQ(sk_X509_num(s), i + 1);
  45074. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  45075. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  45076. }
  45077. /* pop returns and removes last pushed on stack, which is index 0
  45078. * in sk_x509_value */
  45079. for (i = 0; i < len; ++i) {
  45080. X509 * x = sk_X509_value(s, 0);
  45081. X509 * y = sk_X509_pop(s);
  45082. X509 * z = xList[len - 1 - i];
  45083. ExpectIntEQ((x == y), 1);
  45084. ExpectIntEQ((x == z), 1);
  45085. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  45086. }
  45087. sk_free(s);
  45088. s = NULL;
  45089. /* test push, shift, and free */
  45090. ExpectNotNull(s = sk_X509_new_null());
  45091. for (i = 0; i < len; ++i) {
  45092. sk_X509_push(s, xList[i]);
  45093. ExpectIntEQ(sk_X509_num(s), i + 1);
  45094. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  45095. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  45096. }
  45097. /* shift returns and removes first pushed on stack, which is index i
  45098. * in sk_x509_value() */
  45099. for (i = 0; i < len; ++i) {
  45100. X509 * x = sk_X509_value(s, len - 1 - i);
  45101. X509 * y = sk_X509_shift(s);
  45102. X509 * z = xList[i];
  45103. ExpectIntEQ((x == y), 1);
  45104. ExpectIntEQ((x == z), 1);
  45105. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  45106. }
  45107. sk_free(s);
  45108. for (i = 0; i < len; ++i)
  45109. X509_free(xList[i]);
  45110. }
  45111. #endif
  45112. return EXPECT_RESULT();
  45113. }
  45114. static int test_sk_X509_CRL(void)
  45115. {
  45116. EXPECT_DECLS;
  45117. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  45118. X509_CRL* crl = NULL;
  45119. XFILE fp = XBADFILE;
  45120. STACK_OF(X509_CRL)* s = NULL;
  45121. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  45122. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  45123. NULL, NULL));
  45124. if (fp != XBADFILE)
  45125. XFCLOSE(fp);
  45126. ExpectNotNull(s = sk_X509_CRL_new());
  45127. ExpectIntEQ(sk_X509_CRL_num(s), 0);
  45128. ExpectIntEQ(sk_X509_CRL_push(s, crl), 1);
  45129. if (EXPECT_FAIL()) {
  45130. X509_CRL_free(crl);
  45131. }
  45132. ExpectIntEQ(sk_X509_CRL_num(s), 1);
  45133. ExpectPtrEq(sk_X509_CRL_value(s, 0), crl);
  45134. sk_X509_CRL_free(s);
  45135. #endif
  45136. return EXPECT_RESULT();
  45137. }
  45138. static int test_X509_get_signature_nid(void)
  45139. {
  45140. EXPECT_DECLS;
  45141. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45142. X509* x509 = NULL;
  45143. ExpectIntEQ(X509_get_signature_nid(NULL), 0);
  45144. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  45145. SSL_FILETYPE_PEM));
  45146. ExpectIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  45147. X509_free(x509);
  45148. #endif
  45149. return EXPECT_RESULT();
  45150. }
  45151. static int test_X509_REQ(void)
  45152. {
  45153. EXPECT_DECLS;
  45154. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  45155. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  45156. X509_NAME* name = NULL;
  45157. #ifndef NO_RSA
  45158. X509_NAME* subject = NULL;
  45159. #endif
  45160. #if !defined(NO_RSA) || defined(HAVE_ECC)
  45161. X509_REQ* req = NULL;
  45162. EVP_PKEY* priv = NULL;
  45163. EVP_PKEY* pub = NULL;
  45164. unsigned char* der = NULL;
  45165. int len;
  45166. #endif
  45167. #ifndef NO_RSA
  45168. EVP_MD_CTX *mctx = NULL;
  45169. EVP_PKEY_CTX *pkctx = NULL;
  45170. #ifdef USE_CERT_BUFFERS_1024
  45171. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  45172. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  45173. #elif defined(USE_CERT_BUFFERS_2048)
  45174. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  45175. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  45176. #endif
  45177. #endif
  45178. #ifdef HAVE_ECC
  45179. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  45180. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  45181. #endif
  45182. ExpectNotNull(name = X509_NAME_new());
  45183. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  45184. (byte*)"wolfssl.com", 11, 0, 1), WOLFSSL_SUCCESS);
  45185. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  45186. (byte*)"support@wolfssl.com", 19, -1, 1), WOLFSSL_SUCCESS);
  45187. #ifndef NO_RSA
  45188. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  45189. (long)sizeof_client_key_der_2048));
  45190. ExpectNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  45191. (long)sizeof_client_keypub_der_2048));
  45192. ExpectNotNull(req = X509_REQ_new());
  45193. ExpectIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  45194. ExpectIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  45195. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  45196. ExpectIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  45197. ExpectIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  45198. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  45199. ExpectIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  45200. ExpectIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  45201. ExpectIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  45202. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  45203. len = i2d_X509_REQ(req, &der);
  45204. DEBUG_WRITE_DER(der, len, "req.der");
  45205. #ifdef USE_CERT_BUFFERS_1024
  45206. ExpectIntEQ(len, 381);
  45207. #else
  45208. ExpectIntEQ(len, 643);
  45209. #endif
  45210. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  45211. der = NULL;
  45212. mctx = EVP_MD_CTX_new();
  45213. ExpectIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv),
  45214. WOLFSSL_SUCCESS);
  45215. ExpectIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  45216. EVP_MD_CTX_free(mctx);
  45217. mctx = NULL;
  45218. X509_REQ_free(NULL);
  45219. X509_REQ_free(req);
  45220. req = NULL;
  45221. /* Test getting the subject from a newly created X509_REQ */
  45222. ExpectNotNull(req = X509_REQ_new());
  45223. ExpectNotNull(subject = X509_REQ_get_subject_name(req));
  45224. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  45225. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  45226. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  45227. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  45228. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  45229. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  45230. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  45231. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  45232. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  45233. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  45234. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  45235. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  45236. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  45237. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  45238. len = i2d_X509_REQ(req, &der);
  45239. DEBUG_WRITE_DER(der, len, "req2.der");
  45240. #ifdef USE_CERT_BUFFERS_1024
  45241. ExpectIntEQ(len, 435);
  45242. #else
  45243. ExpectIntEQ(len, 696);
  45244. #endif
  45245. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  45246. der = NULL;
  45247. EVP_PKEY_free(pub);
  45248. pub = NULL;
  45249. EVP_PKEY_free(priv);
  45250. priv = NULL;
  45251. X509_REQ_free(req);
  45252. req = NULL;
  45253. #endif
  45254. #ifdef HAVE_ECC
  45255. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  45256. sizeof_ecc_clikey_der_256));
  45257. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  45258. sizeof_ecc_clikeypub_der_256));
  45259. ExpectNotNull(req = X509_REQ_new());
  45260. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  45261. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  45262. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  45263. /* Signature is random and may be shorter or longer. */
  45264. ExpectIntGE((len = i2d_X509_REQ(req, &der)), 245);
  45265. ExpectIntLE(len, 253);
  45266. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  45267. X509_REQ_free(req);
  45268. EVP_PKEY_free(pub);
  45269. EVP_PKEY_free(priv);
  45270. #ifdef FP_ECC
  45271. wc_ecc_fp_free();
  45272. #endif
  45273. #endif /* HAVE_ECC */
  45274. X509_NAME_free(name);
  45275. #endif
  45276. return EXPECT_RESULT();
  45277. }
  45278. static int test_wolfssl_PKCS7(void)
  45279. {
  45280. EXPECT_DECLS;
  45281. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  45282. !defined(NO_RSA)
  45283. PKCS7* pkcs7 = NULL;
  45284. byte data[FOURK_BUF];
  45285. word32 len = sizeof(data);
  45286. const byte* p = data;
  45287. byte content[] = "Test data to encode.";
  45288. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  45289. BIO* bio = NULL;
  45290. byte key[sizeof(client_key_der_2048)];
  45291. word32 keySz = (word32)sizeof(key);
  45292. byte* out = NULL;
  45293. #endif
  45294. ExpectIntGT((len = CreatePKCS7SignedData(data, len, content,
  45295. (word32)sizeof(content), 0, 0, 0, RSA_TYPE)), 0);
  45296. ExpectNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  45297. ExpectNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  45298. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  45299. ExpectIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  45300. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  45301. PKCS7_free(pkcs7);
  45302. pkcs7 = NULL;
  45303. /* fail case, without PKCS7_NOVERIFY */
  45304. p = data;
  45305. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  45306. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  45307. 0), WOLFSSL_FAILURE);
  45308. PKCS7_free(pkcs7);
  45309. pkcs7 = NULL;
  45310. /* success case, with PKCS7_NOVERIFY */
  45311. p = data;
  45312. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  45313. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  45314. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  45315. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  45316. /* test i2d */
  45317. XMEMCPY(key, client_key_der_2048, keySz);
  45318. if (pkcs7 != NULL) {
  45319. pkcs7->privateKey = key;
  45320. pkcs7->privateKeySz = (word32)sizeof(key);
  45321. pkcs7->encryptOID = RSAk;
  45322. #ifdef NO_SHA
  45323. pkcs7->hashOID = SHA256h;
  45324. #else
  45325. pkcs7->hashOID = SHAh;
  45326. #endif
  45327. }
  45328. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  45329. ExpectIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  45330. #ifndef NO_ASN_TIME
  45331. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  45332. #else
  45333. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 625);
  45334. #endif
  45335. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45336. BIO_free(bio);
  45337. #endif
  45338. PKCS7_free(NULL);
  45339. PKCS7_free(pkcs7);
  45340. #endif
  45341. return EXPECT_RESULT();
  45342. }
  45343. static int test_wolfSSL_PKCS7_sign(void)
  45344. {
  45345. EXPECT_DECLS;
  45346. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  45347. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45348. PKCS7* p7 = NULL;
  45349. PKCS7* p7Ver = NULL;
  45350. byte* out = NULL;
  45351. byte* tmpPtr = NULL;
  45352. int outLen = 0;
  45353. int flags = 0;
  45354. byte data[] = "Test data to encode.";
  45355. const char* cert = "./certs/server-cert.pem";
  45356. const char* key = "./certs/server-key.pem";
  45357. const char* ca = "./certs/ca-cert.pem";
  45358. WOLFSSL_BIO* certBio = NULL;
  45359. WOLFSSL_BIO* keyBio = NULL;
  45360. WOLFSSL_BIO* caBio = NULL;
  45361. WOLFSSL_BIO* inBio = NULL;
  45362. X509* signCert = NULL;
  45363. EVP_PKEY* signKey = NULL;
  45364. X509* caCert = NULL;
  45365. X509_STORE* store = NULL;
  45366. /* read signer cert/key into BIO */
  45367. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  45368. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  45369. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  45370. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  45371. /* read CA cert into store (for verify) */
  45372. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  45373. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  45374. ExpectNotNull(store = X509_STORE_new());
  45375. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  45376. /* data to be signed into BIO */
  45377. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  45378. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45379. /* PKCS7_sign, bad args: signer NULL */
  45380. ExpectNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  45381. /* PKCS7_sign, bad args: signer key NULL */
  45382. ExpectNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  45383. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  45384. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  45385. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  45386. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  45387. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  45388. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  45389. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  45390. {
  45391. flags = PKCS7_BINARY;
  45392. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45393. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  45394. /* verify with d2i_PKCS7 */
  45395. tmpPtr = out;
  45396. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  45397. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  45398. PKCS7_free(p7Ver);
  45399. p7Ver = NULL;
  45400. /* verify with wc_PKCS7_VerifySignedData */
  45401. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  45402. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  45403. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  45404. /* compare the signer found to expected signer */
  45405. ExpectIntNE(p7Ver->verifyCertSz, 0);
  45406. tmpPtr = NULL;
  45407. ExpectIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  45408. ExpectIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  45409. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  45410. tmpPtr = NULL;
  45411. wc_PKCS7_Free(p7Ver);
  45412. p7Ver = NULL;
  45413. ExpectNotNull(out);
  45414. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45415. out = NULL;
  45416. PKCS7_free(p7);
  45417. p7 = NULL;
  45418. }
  45419. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  45420. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  45421. {
  45422. /* re-populate input BIO, may have been consumed */
  45423. BIO_free(inBio);
  45424. inBio = NULL;
  45425. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  45426. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45427. flags = PKCS7_BINARY | PKCS7_STREAM;
  45428. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45429. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  45430. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  45431. /* PKCS7_final, bad args: PKCS7 null */
  45432. ExpectIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  45433. /* PKCS7_final, bad args: PKCS7 null */
  45434. ExpectIntEQ(PKCS7_final(p7, NULL, 0), 0);
  45435. tmpPtr = out;
  45436. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  45437. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  45438. PKCS7_free(p7Ver);
  45439. p7Ver = NULL;
  45440. ExpectNotNull(out);
  45441. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45442. out = NULL;
  45443. PKCS7_free(p7);
  45444. p7 = NULL;
  45445. }
  45446. /* TEST SUCCESS: Detached, not streaming, not MIME */
  45447. {
  45448. /* re-populate input BIO, may have been consumed */
  45449. BIO_free(inBio);
  45450. inBio = NULL;
  45451. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  45452. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45453. flags = PKCS7_BINARY | PKCS7_DETACHED;
  45454. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45455. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  45456. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  45457. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  45458. if (p7Ver != NULL) {
  45459. p7Ver->content = data;
  45460. p7Ver->contentSz = sizeof(data);
  45461. }
  45462. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  45463. wc_PKCS7_Free(p7Ver);
  45464. p7Ver = NULL;
  45465. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  45466. * yet support detached content */
  45467. tmpPtr = out;
  45468. ExpectNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  45469. PKCS7_free(p7Ver);
  45470. p7Ver = NULL;
  45471. ExpectNotNull(out);
  45472. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45473. out = NULL;
  45474. PKCS7_free(p7);
  45475. p7 = NULL;
  45476. }
  45477. /* TEST SUCCESS: Detached, streaming, not MIME */
  45478. {
  45479. /* re-populate input BIO, may have been consumed */
  45480. BIO_free(inBio);
  45481. inBio = NULL;
  45482. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  45483. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45484. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  45485. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45486. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  45487. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  45488. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  45489. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  45490. if (p7Ver != NULL) {
  45491. p7Ver->content = data;
  45492. p7Ver->contentSz = sizeof(data);
  45493. }
  45494. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  45495. wc_PKCS7_Free(p7Ver);
  45496. p7Ver = NULL;
  45497. ExpectNotNull(out);
  45498. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45499. PKCS7_free(p7);
  45500. p7 = NULL;
  45501. }
  45502. X509_STORE_free(store);
  45503. X509_free(caCert);
  45504. X509_free(signCert);
  45505. EVP_PKEY_free(signKey);
  45506. BIO_free(inBio);
  45507. BIO_free(keyBio);
  45508. BIO_free(certBio);
  45509. BIO_free(caBio);
  45510. #endif
  45511. return EXPECT_RESULT();
  45512. }
  45513. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  45514. {
  45515. EXPECT_DECLS;
  45516. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  45517. PKCS7_SIGNED* pkcs7 = NULL;
  45518. ExpectNotNull(pkcs7 = PKCS7_SIGNED_new());
  45519. ExpectIntEQ(pkcs7->contentOID, SIGNED_DATA);
  45520. PKCS7_SIGNED_free(pkcs7);
  45521. #endif
  45522. return EXPECT_RESULT();
  45523. }
  45524. #ifndef NO_BIO
  45525. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  45526. {
  45527. EXPECT_DECLS;
  45528. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  45529. PKCS7* pkcs7 = NULL;
  45530. BIO* bio = NULL;
  45531. const byte* cert_buf = NULL;
  45532. int ret = 0;
  45533. WC_RNG rng;
  45534. const byte data[] = { /* Hello World */
  45535. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  45536. 0x72,0x6c,0x64
  45537. };
  45538. #ifndef NO_RSA
  45539. #if defined(USE_CERT_BUFFERS_2048)
  45540. byte key[sizeof(client_key_der_2048)];
  45541. byte cert[sizeof(client_cert_der_2048)];
  45542. word32 keySz = (word32)sizeof(key);
  45543. word32 certSz = (word32)sizeof(cert);
  45544. XMEMSET(key, 0, keySz);
  45545. XMEMSET(cert, 0, certSz);
  45546. XMEMCPY(key, client_key_der_2048, keySz);
  45547. XMEMCPY(cert, client_cert_der_2048, certSz);
  45548. #elif defined(USE_CERT_BUFFERS_1024)
  45549. byte key[sizeof_client_key_der_1024];
  45550. byte cert[sizeof(sizeof_client_cert_der_1024)];
  45551. word32 keySz = (word32)sizeof(key);
  45552. word32 certSz = (word32)sizeof(cert);
  45553. XMEMSET(key, 0, keySz);
  45554. XMEMSET(cert, 0, certSz);
  45555. XMEMCPY(key, client_key_der_1024, keySz);
  45556. XMEMCPY(cert, client_cert_der_1024, certSz);
  45557. #else
  45558. unsigned char cert[ONEK_BUF];
  45559. unsigned char key[ONEK_BUF];
  45560. XFILE fp = XBADFILE;
  45561. int certSz;
  45562. int keySz;
  45563. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  45564. XBADFILE);
  45565. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  45566. fp), 0);
  45567. if (fp != XBADFILE) {
  45568. XFCLOSE(fp);
  45569. fp = XBADFILE;
  45570. }
  45571. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  45572. XBADFILE);
  45573. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  45574. 0);
  45575. if (fp != XBADFILE) {
  45576. XFCLOSE(fp);
  45577. fp = XBADFILE;
  45578. }
  45579. #endif
  45580. #elif defined(HAVE_ECC)
  45581. #if defined(USE_CERT_BUFFERS_256)
  45582. unsigned char cert[sizeof(cliecc_cert_der_256)];
  45583. unsigned char key[sizeof(ecc_clikey_der_256)];
  45584. int certSz = (int)sizeof(cert);
  45585. int keySz = (int)sizeof(key);
  45586. XMEMSET(cert, 0, certSz);
  45587. XMEMSET(key, 0, keySz);
  45588. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  45589. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  45590. #else
  45591. unsigned char cert[ONEK_BUF];
  45592. unsigned char key[ONEK_BUF];
  45593. XFILE fp = XBADFILE;
  45594. int certSz, keySz;
  45595. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  45596. XBADFILE);
  45597. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  45598. fp), 0);
  45599. if (fp != XBADFILE) {
  45600. XFCLOSE(fp);
  45601. fp = XBADFILE;
  45602. }
  45603. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  45604. XBADFILE);
  45605. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  45606. 0);
  45607. if (fp != XBADFILE) {
  45608. XFCLOSE(fp);
  45609. fp = XBADFILE;
  45610. }
  45611. #endif
  45612. #else
  45613. #error PKCS7 requires ECC or RSA
  45614. #endif
  45615. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  45616. /* initialize with DER encoded cert */
  45617. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  45618. /* init rng */
  45619. XMEMSET(&rng, 0, sizeof(WC_RNG));
  45620. ExpectIntEQ(wc_InitRng(&rng), 0);
  45621. if (pkcs7 != NULL) {
  45622. pkcs7->rng = &rng;
  45623. pkcs7->content = (byte*)data; /* not used for ex */
  45624. pkcs7->contentSz = (word32)sizeof(data);
  45625. pkcs7->contentOID = SIGNED_DATA;
  45626. pkcs7->privateKey = key;
  45627. pkcs7->privateKeySz = (word32)sizeof(key);
  45628. pkcs7->encryptOID = RSAk;
  45629. #ifdef NO_SHA
  45630. pkcs7->hashOID = SHA256h;
  45631. #else
  45632. pkcs7->hashOID = SHAh;
  45633. #endif
  45634. pkcs7->signedAttribs = NULL;
  45635. pkcs7->signedAttribsSz = 0;
  45636. }
  45637. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  45638. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  45639. ExpectIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  45640. /* Read PKCS#7 PEM from BIO */
  45641. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  45642. ExpectIntGE(ret, 0);
  45643. BIO_free(bio);
  45644. wc_PKCS7_Free(pkcs7);
  45645. wc_FreeRng(&rng);
  45646. #endif
  45647. return EXPECT_RESULT();
  45648. }
  45649. #ifdef HAVE_SMIME
  45650. static int test_wolfSSL_SMIME_read_PKCS7(void)
  45651. {
  45652. EXPECT_DECLS;
  45653. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  45654. !defined(NO_RSA)
  45655. PKCS7* pkcs7 = NULL;
  45656. BIO* bio = NULL;
  45657. BIO* bcont = NULL;
  45658. BIO* out = NULL;
  45659. const byte* outBuf = NULL;
  45660. int outBufLen = 0;
  45661. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  45662. XFILE smimeTestFile = XBADFILE;
  45663. ExpectTrue((smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r")) !=
  45664. XBADFILE);
  45665. /* smime-test.p7s */
  45666. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  45667. ExpectNotNull(bio);
  45668. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  45669. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  45670. ExpectNotNull(pkcs7);
  45671. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  45672. PKCS7_NOVERIFY), SSL_SUCCESS);
  45673. XFCLOSE(smimeTestFile);
  45674. if (bcont) BIO_free(bcont);
  45675. bcont = NULL;
  45676. wolfSSL_PKCS7_free(pkcs7);
  45677. pkcs7 = NULL;
  45678. /* smime-test-multipart.p7s */
  45679. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  45680. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  45681. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  45682. ExpectNotNull(pkcs7);
  45683. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  45684. PKCS7_NOVERIFY), SSL_SUCCESS);
  45685. XFCLOSE(smimeTestFile);
  45686. if (bcont) BIO_free(bcont);
  45687. bcont = NULL;
  45688. wolfSSL_PKCS7_free(pkcs7);
  45689. pkcs7 = NULL;
  45690. /* smime-test-multipart-badsig.p7s */
  45691. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  45692. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  45693. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  45694. ExpectNotNull(pkcs7); /* can read in the unverified smime bundle */
  45695. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  45696. PKCS7_NOVERIFY), SSL_FAILURE);
  45697. XFCLOSE(smimeTestFile);
  45698. if (bcont) BIO_free(bcont);
  45699. bcont = NULL;
  45700. wolfSSL_PKCS7_free(pkcs7);
  45701. pkcs7 = NULL;
  45702. /* smime-test-canon.p7s */
  45703. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  45704. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  45705. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  45706. ExpectNotNull(pkcs7);
  45707. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  45708. PKCS7_NOVERIFY), SSL_SUCCESS);
  45709. XFCLOSE(smimeTestFile);
  45710. if (bcont) BIO_free(bcont);
  45711. bcont = NULL;
  45712. wolfSSL_PKCS7_free(pkcs7);
  45713. pkcs7 = NULL;
  45714. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  45715. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  45716. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  45717. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  45718. ExpectNotNull(pkcs7);
  45719. out = wolfSSL_BIO_new(BIO_s_mem());
  45720. ExpectNotNull(out);
  45721. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  45722. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  45723. ExpectIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  45724. /* Content-Type should not show up at beginning of output buffer */
  45725. ExpectIntGT(outBufLen, XSTRLEN(contTypeText));
  45726. ExpectIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  45727. BIO_free(out);
  45728. BIO_free(bio);
  45729. if (bcont) BIO_free(bcont);
  45730. wolfSSL_PKCS7_free(pkcs7);
  45731. #endif
  45732. return EXPECT_RESULT();
  45733. }
  45734. static int test_wolfSSL_SMIME_write_PKCS7(void)
  45735. {
  45736. EXPECT_DECLS;
  45737. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  45738. PKCS7* p7 = NULL;
  45739. PKCS7* p7Ver = NULL;
  45740. int flags = 0;
  45741. byte data[] = "Test data to encode.";
  45742. const char* cert = "./certs/server-cert.pem";
  45743. const char* key = "./certs/server-key.pem";
  45744. const char* ca = "./certs/ca-cert.pem";
  45745. WOLFSSL_BIO* certBio = NULL;
  45746. WOLFSSL_BIO* keyBio = NULL;
  45747. WOLFSSL_BIO* caBio = NULL;
  45748. WOLFSSL_BIO* inBio = NULL;
  45749. WOLFSSL_BIO* outBio = NULL;
  45750. WOLFSSL_BIO* content = NULL;
  45751. X509* signCert = NULL;
  45752. EVP_PKEY* signKey = NULL;
  45753. X509* caCert = NULL;
  45754. X509_STORE* store = NULL;
  45755. /* read signer cert/key into BIO */
  45756. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  45757. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  45758. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  45759. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  45760. /* read CA cert into store (for verify) */
  45761. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  45762. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  45763. ExpectNotNull(store = X509_STORE_new());
  45764. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  45765. /* generate and verify SMIME: not detached */
  45766. {
  45767. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  45768. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45769. flags = PKCS7_STREAM;
  45770. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45771. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  45772. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  45773. /* bad arg: out NULL */
  45774. ExpectIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  45775. /* bad arg: pkcs7 NULL */
  45776. ExpectIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  45777. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  45778. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  45779. BIO_free(content);
  45780. content = NULL;
  45781. BIO_free(inBio);
  45782. inBio = NULL;
  45783. BIO_free(outBio);
  45784. outBio = NULL;
  45785. PKCS7_free(p7Ver);
  45786. p7Ver = NULL;
  45787. PKCS7_free(p7);
  45788. p7 = NULL;
  45789. }
  45790. /* generate and verify SMIME: not detached, add Content-Type */
  45791. {
  45792. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  45793. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45794. flags = PKCS7_STREAM | PKCS7_TEXT;
  45795. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45796. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  45797. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  45798. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  45799. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  45800. BIO_free(content);
  45801. content = NULL;
  45802. BIO_free(inBio);
  45803. inBio = NULL;
  45804. BIO_free(outBio);
  45805. outBio = NULL;
  45806. PKCS7_free(p7Ver);
  45807. p7Ver = NULL;
  45808. PKCS7_free(p7);
  45809. p7 = NULL;
  45810. }
  45811. /* generate and verify SMIME: detached */
  45812. {
  45813. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  45814. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45815. flags = PKCS7_DETACHED | PKCS7_STREAM;
  45816. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45817. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  45818. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  45819. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  45820. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  45821. BIO_free(content);
  45822. content = NULL;
  45823. BIO_free(inBio);
  45824. inBio = NULL;
  45825. BIO_free(outBio);
  45826. outBio = NULL;
  45827. PKCS7_free(p7Ver);
  45828. p7Ver = NULL;
  45829. PKCS7_free(p7);
  45830. p7 = NULL;
  45831. }
  45832. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  45833. {
  45834. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  45835. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45836. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  45837. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45838. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  45839. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  45840. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  45841. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  45842. BIO_free(content);
  45843. content = NULL;
  45844. BIO_free(inBio);
  45845. inBio = NULL;
  45846. BIO_free(outBio);
  45847. outBio = NULL;
  45848. PKCS7_free(p7Ver);
  45849. p7Ver = NULL;
  45850. PKCS7_free(p7);
  45851. p7 = NULL;
  45852. }
  45853. X509_STORE_free(store);
  45854. X509_free(caCert);
  45855. X509_free(signCert);
  45856. EVP_PKEY_free(signKey);
  45857. BIO_free(keyBio);
  45858. BIO_free(certBio);
  45859. BIO_free(caBio);
  45860. #endif
  45861. return EXPECT_RESULT();
  45862. }
  45863. #endif /* HAVE_SMIME */
  45864. #endif /* !NO_BIO */
  45865. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  45866. * returns 0) */
  45867. static int test_X509_STORE_No_SSL_CTX(void)
  45868. {
  45869. EXPECT_DECLS;
  45870. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  45871. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  45872. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  45873. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  45874. defined(HAVE_CRL) && !defined(NO_RSA)
  45875. X509_STORE * store = NULL;
  45876. X509_STORE_CTX * storeCtx = NULL;
  45877. X509_CRL * crl = NULL;
  45878. X509 * ca = NULL;
  45879. X509 * cert = NULL;
  45880. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  45881. const char srvCert[] = "./certs/server-cert.pem";
  45882. const char caCert[] = "./certs/ca-cert.pem";
  45883. const char caDir[] = "./certs/crl/hash_pem";
  45884. XFILE fp = XBADFILE;
  45885. X509_LOOKUP * lookup = NULL;
  45886. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  45887. /* Set up store with CA */
  45888. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  45889. SSL_FILETYPE_PEM)));
  45890. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  45891. /* Add CRL lookup directory to store
  45892. * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  45893. * of crl.pem */
  45894. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  45895. X509_LOOKUP_hash_dir())));
  45896. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  45897. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  45898. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  45899. SSL_SUCCESS);
  45900. /* Add CRL to store NOT containing the verified certificate, which
  45901. * forces use of the CRL lookup directory */
  45902. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  45903. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  45904. NULL, NULL));
  45905. if (fp != XBADFILE)
  45906. XFCLOSE(fp);
  45907. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  45908. /* Create verification context outside of an SSL session */
  45909. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  45910. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  45911. SSL_FILETYPE_PEM)));
  45912. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  45913. /* Perform verification, which should NOT indicate CRL missing due to the
  45914. * store CM's X509 store pointer being NULL */
  45915. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  45916. X509_CRL_free(crl);
  45917. X509_STORE_free(store);
  45918. X509_STORE_CTX_free(storeCtx);
  45919. X509_free(cert);
  45920. X509_free(ca);
  45921. #endif
  45922. return EXPECT_RESULT();
  45923. }
  45924. /* Test of X509 store use outside of SSL context w/ CRL lookup, but
  45925. * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  45926. static int test_X509_LOOKUP_add_dir(void)
  45927. {
  45928. EXPECT_DECLS;
  45929. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  45930. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  45931. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  45932. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  45933. defined(HAVE_CRL) && !defined(NO_RSA)
  45934. X509_STORE * store = NULL;
  45935. X509_STORE_CTX * storeCtx = NULL;
  45936. X509_CRL * crl = NULL;
  45937. X509 * ca = NULL;
  45938. X509 * cert = NULL;
  45939. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  45940. const char srvCert[] = "./certs/server-cert.pem";
  45941. const char caCert[] = "./certs/ca-cert.pem";
  45942. const char caDir[] = "./certs/crl/hash_der";
  45943. XFILE fp = XBADFILE;
  45944. X509_LOOKUP * lookup = NULL;
  45945. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  45946. /* Set up store with CA */
  45947. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  45948. SSL_FILETYPE_PEM)));
  45949. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  45950. /* Add CRL lookup directory to store.
  45951. * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  45952. * of crl.der */
  45953. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  45954. X509_LOOKUP_hash_dir())));
  45955. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  45956. SSL_SUCCESS);
  45957. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  45958. SSL_SUCCESS);
  45959. /* Add CRL to store NOT containing the verified certificate, which
  45960. * forces use of the CRL lookup directory */
  45961. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  45962. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  45963. NULL, NULL));
  45964. if (fp != XBADFILE) {
  45965. XFCLOSE(fp);
  45966. fp = XBADFILE;
  45967. }
  45968. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  45969. /* Create verification context outside of an SSL session */
  45970. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  45971. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  45972. SSL_FILETYPE_PEM)));
  45973. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  45974. /* Perform verification, which should NOT return CRL missing */
  45975. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  45976. X509_CRL_free(crl);
  45977. crl = NULL;
  45978. X509_STORE_free(store);
  45979. store = NULL;
  45980. X509_STORE_CTX_free(storeCtx);
  45981. storeCtx = NULL;
  45982. X509_free(cert);
  45983. cert = NULL;
  45984. X509_free(ca);
  45985. ca = NULL;
  45986. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  45987. * We should get CRL_MISSING at the end, because the lookup
  45988. * dir has only ASN1 CRLs. */
  45989. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  45990. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  45991. SSL_FILETYPE_PEM)));
  45992. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  45993. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  45994. X509_LOOKUP_hash_dir())));
  45995. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  45996. SSL_SUCCESS);
  45997. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  45998. SSL_SUCCESS);
  45999. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  46000. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  46001. NULL, NULL));
  46002. if (fp != XBADFILE) {
  46003. XFCLOSE(fp);
  46004. fp = XBADFILE;
  46005. }
  46006. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  46007. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  46008. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  46009. SSL_FILETYPE_PEM)));
  46010. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  46011. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  46012. * in dir containing only ASN1/DER. */
  46013. ExpectIntEQ(X509_verify_cert(storeCtx), WOLFSSL_FAILURE);
  46014. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_MISSING);
  46015. X509_CRL_free(crl);
  46016. X509_STORE_free(store);
  46017. X509_STORE_CTX_free(storeCtx);
  46018. X509_free(cert);
  46019. X509_free(ca);
  46020. #endif
  46021. return EXPECT_RESULT();
  46022. }
  46023. /*----------------------------------------------------------------------------*
  46024. | Certificate Failure Checks
  46025. *----------------------------------------------------------------------------*/
  46026. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  46027. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  46028. #if !defined(NO_RSA) || defined(HAVE_ECC)
  46029. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  46030. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  46031. int type)
  46032. {
  46033. int ret;
  46034. WOLFSSL_CERT_MANAGER* cm = NULL;
  46035. switch (type) {
  46036. case TESTING_RSA:
  46037. #ifdef NO_RSA
  46038. fprintf(stderr, "RSA disabled, skipping test\n");
  46039. return ASN_SIG_CONFIRM_E;
  46040. #else
  46041. break;
  46042. #endif
  46043. case TESTING_ECC:
  46044. #ifndef HAVE_ECC
  46045. fprintf(stderr, "ECC disabled, skipping test\n");
  46046. return ASN_SIG_CONFIRM_E;
  46047. #else
  46048. break;
  46049. #endif
  46050. default:
  46051. fprintf(stderr, "Bad function argument\n");
  46052. return BAD_FUNC_ARG;
  46053. }
  46054. cm = wolfSSL_CertManagerNew();
  46055. if (cm == NULL) {
  46056. fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
  46057. return -1;
  46058. }
  46059. #ifndef NO_FILESYSTEM
  46060. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  46061. if (ret != WOLFSSL_SUCCESS) {
  46062. fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
  46063. wolfSSL_CertManagerFree(cm);
  46064. return ret;
  46065. }
  46066. #else
  46067. (void)ca;
  46068. #endif
  46069. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz,
  46070. WOLFSSL_FILETYPE_ASN1);
  46071. /* Let ExpectIntEQ handle return code */
  46072. wolfSSL_CertManagerFree(cm);
  46073. return ret;
  46074. }
  46075. #endif
  46076. #if !defined(NO_FILESYSTEM)
  46077. static int test_RsaSigFailure_cm(void)
  46078. {
  46079. EXPECT_DECLS;
  46080. #ifndef NO_RSA
  46081. const char* ca_cert = "./certs/ca-cert.pem";
  46082. const char* server_cert = "./certs/server-cert.der";
  46083. byte* cert_buf = NULL;
  46084. size_t cert_sz = 0;
  46085. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  46086. if (cert_buf != NULL) {
  46087. /* corrupt DER - invert last byte, which is signature */
  46088. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  46089. /* test bad cert */
  46090. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  46091. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  46092. WOLFSSL_FATAL_ERROR);
  46093. #else
  46094. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  46095. ASN_SIG_CONFIRM_E);
  46096. #endif
  46097. }
  46098. /* load_file() uses malloc. */
  46099. if (cert_buf != NULL) {
  46100. free(cert_buf);
  46101. }
  46102. #endif /* !NO_RSA */
  46103. return EXPECT_RESULT();
  46104. }
  46105. static int test_EccSigFailure_cm(void)
  46106. {
  46107. EXPECT_DECLS;
  46108. #ifdef HAVE_ECC
  46109. /* self-signed ECC cert, so use server cert as CA */
  46110. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  46111. const char* server_cert = "./certs/server-ecc.der";
  46112. byte* cert_buf = NULL;
  46113. size_t cert_sz = 0;
  46114. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  46115. if (cert_buf != NULL) {
  46116. /* corrupt DER - invert last byte, which is signature */
  46117. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  46118. /* test bad cert */
  46119. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  46120. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  46121. WOLFSSL_FATAL_ERROR);
  46122. #else
  46123. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  46124. ASN_SIG_CONFIRM_E);
  46125. #endif
  46126. }
  46127. /* load_file() uses malloc. */
  46128. if (cert_buf != NULL) {
  46129. free(cert_buf);
  46130. }
  46131. #ifdef FP_ECC
  46132. wc_ecc_fp_free();
  46133. #endif
  46134. #endif /* HAVE_ECC */
  46135. return EXPECT_RESULT();
  46136. }
  46137. #endif /* !NO_FILESYSTEM */
  46138. #endif /* NO_CERTS */
  46139. #ifdef WOLFSSL_TLS13
  46140. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  46141. #ifdef WC_SHA384_DIGEST_SIZE
  46142. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  46143. #else
  46144. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  46145. #endif
  46146. #endif
  46147. #ifdef WOLFSSL_EARLY_DATA
  46148. static const char earlyData[] = "Early Data";
  46149. static char earlyDataBuffer[1];
  46150. #endif
  46151. static int test_tls13_apis(void)
  46152. {
  46153. EXPECT_DECLS;
  46154. int ret;
  46155. #ifndef WOLFSSL_NO_TLS12
  46156. #ifndef NO_WOLFSSL_CLIENT
  46157. WOLFSSL_CTX* clientTls12Ctx = NULL;
  46158. WOLFSSL* clientTls12Ssl = NULL;
  46159. #endif
  46160. #ifndef NO_WOLFSSL_SERVER
  46161. WOLFSSL_CTX* serverTls12Ctx = NULL;
  46162. WOLFSSL* serverTls12Ssl = NULL;
  46163. #endif
  46164. #endif
  46165. #ifndef NO_WOLFSSL_CLIENT
  46166. WOLFSSL_CTX* clientCtx = NULL;
  46167. WOLFSSL* clientSsl = NULL;
  46168. #endif
  46169. #ifndef NO_WOLFSSL_SERVER
  46170. WOLFSSL_CTX* serverCtx = NULL;
  46171. WOLFSSL* serverSsl = NULL;
  46172. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  46173. const char* ourCert = svrCertFile;
  46174. const char* ourKey = svrKeyFile;
  46175. #endif
  46176. #endif
  46177. int required;
  46178. #ifdef WOLFSSL_EARLY_DATA
  46179. int outSz;
  46180. #endif
  46181. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  46182. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  46183. #ifdef HAVE_PQC
  46184. WOLFSSL_KYBER_LEVEL1
  46185. #else
  46186. WOLFSSL_ECC_SECP256R1
  46187. #endif
  46188. };
  46189. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  46190. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  46191. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  46192. int numGroups = 2;
  46193. #endif
  46194. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46195. char groupList[] =
  46196. #ifndef NO_ECC_SECP
  46197. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  46198. "P-521:"
  46199. #endif
  46200. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  46201. "P-384:"
  46202. #endif
  46203. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  46204. "P-256"
  46205. #ifdef HAVE_PQC
  46206. ":P256_KYBER_LEVEL1"
  46207. #endif
  46208. #endif
  46209. #endif /* !defined(NO_ECC_SECP) */
  46210. #ifdef HAVE_PQC
  46211. ":KYBER_LEVEL1"
  46212. #endif
  46213. "";
  46214. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  46215. (void)ret;
  46216. #ifndef WOLFSSL_NO_TLS12
  46217. #ifndef NO_WOLFSSL_CLIENT
  46218. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  46219. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  46220. #endif
  46221. #ifndef NO_WOLFSSL_SERVER
  46222. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  46223. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  46224. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  46225. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey,
  46226. WOLFSSL_FILETYPE_PEM);
  46227. #endif
  46228. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  46229. #endif
  46230. #endif
  46231. #ifndef NO_WOLFSSL_CLIENT
  46232. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  46233. clientSsl = wolfSSL_new(clientCtx);
  46234. #endif
  46235. #ifndef NO_WOLFSSL_SERVER
  46236. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  46237. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  46238. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  46239. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  46240. #endif
  46241. serverSsl = wolfSSL_new(serverCtx);
  46242. ExpectNotNull(serverSsl);
  46243. #endif
  46244. #ifdef WOLFSSL_SEND_HRR_COOKIE
  46245. ExpectIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  46246. #ifndef NO_WOLFSSL_CLIENT
  46247. ExpectIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  46248. #endif
  46249. #ifndef NO_WOLFSSL_SERVER
  46250. #ifndef WOLFSSL_NO_TLS12
  46251. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0),
  46252. BAD_FUNC_ARG);
  46253. #endif
  46254. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  46255. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  46256. WOLFSSL_SUCCESS);
  46257. #endif
  46258. #endif
  46259. #ifdef HAVE_SUPPORTED_CURVES
  46260. #ifdef HAVE_ECC
  46261. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  46262. BAD_FUNC_ARG);
  46263. #ifndef NO_WOLFSSL_SERVER
  46264. do {
  46265. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  46266. #ifdef WOLFSSL_ASYNC_CRYPT
  46267. if (ret == WC_PENDING_E)
  46268. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  46269. #endif
  46270. }
  46271. while (ret == WC_PENDING_E);
  46272. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  46273. #endif
  46274. #ifndef NO_WOLFSSL_CLIENT
  46275. #ifndef WOLFSSL_NO_TLS12
  46276. do {
  46277. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  46278. #ifdef WOLFSSL_ASYNC_CRYPT
  46279. if (ret == WC_PENDING_E)
  46280. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  46281. #endif
  46282. }
  46283. while (ret == WC_PENDING_E);
  46284. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  46285. #endif
  46286. do {
  46287. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  46288. #ifdef WOLFSSL_ASYNC_CRYPT
  46289. if (ret == WC_PENDING_E)
  46290. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  46291. #endif
  46292. }
  46293. while (ret == WC_PENDING_E);
  46294. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  46295. #endif
  46296. #elif defined(HAVE_CURVE25519)
  46297. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  46298. #ifndef NO_WOLFSSL_SERVER
  46299. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  46300. WOLFSSL_SUCCESS);
  46301. #endif
  46302. #ifndef NO_WOLFSSL_CLIENT
  46303. #ifndef WOLFSSL_NO_TLS12
  46304. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  46305. WOLFSSL_SUCCESS);
  46306. #endif
  46307. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  46308. WOLFSSL_SUCCESS);
  46309. #endif
  46310. #elif defined(HAVE_CURVE448)
  46311. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  46312. #ifndef NO_WOLFSSL_SERVER
  46313. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  46314. WOLFSSL_SUCCESS);
  46315. #endif
  46316. #ifndef NO_WOLFSSL_CLIENT
  46317. #ifndef WOLFSSL_NO_TLS12
  46318. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  46319. WOLFSSL_SUCCESS);
  46320. #endif
  46321. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  46322. WOLFSSL_SUCCESS);
  46323. #endif
  46324. #else
  46325. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  46326. BAD_FUNC_ARG);
  46327. #ifndef NO_WOLFSSL_CLIENT
  46328. #ifndef WOLFSSL_NO_TLS12
  46329. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  46330. NOT_COMPILED_IN);
  46331. #endif
  46332. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  46333. NOT_COMPILED_IN);
  46334. #endif
  46335. #endif
  46336. #if defined(HAVE_PQC)
  46337. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  46338. #ifndef NO_WOLFSSL_SERVER
  46339. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  46340. WOLFSSL_SUCCESS);
  46341. #endif
  46342. #ifndef NO_WOLFSSL_CLIENT
  46343. #ifndef WOLFSSL_NO_TLS12
  46344. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  46345. BAD_FUNC_ARG);
  46346. #endif
  46347. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  46348. WOLFSSL_SUCCESS);
  46349. #endif
  46350. #endif
  46351. ExpectIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  46352. #ifndef NO_WOLFSSL_SERVER
  46353. ExpectIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  46354. #endif
  46355. #ifndef NO_WOLFSSL_CLIENT
  46356. #ifndef WOLFSSL_NO_TLS12
  46357. ExpectIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  46358. #endif
  46359. ExpectIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  46360. #endif
  46361. #endif /* HAVE_SUPPORTED_CURVES */
  46362. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  46363. #ifndef NO_WOLFSSL_CLIENT
  46364. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  46365. #endif
  46366. #ifndef NO_WOLFSSL_SERVER
  46367. #ifndef WOLFSSL_NO_TLS12
  46368. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  46369. #endif
  46370. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  46371. #endif
  46372. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  46373. #ifndef NO_WOLFSSL_CLIENT
  46374. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  46375. #endif
  46376. #ifndef NO_WOLFSSL_SERVER
  46377. #ifndef WOLFSSL_NO_TLS12
  46378. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  46379. #endif
  46380. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  46381. #endif
  46382. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  46383. #ifndef NO_WOLFSSL_CLIENT
  46384. #ifndef WOLFSSL_NO_TLS12
  46385. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  46386. #endif
  46387. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  46388. #endif
  46389. #ifndef NO_WOLFSSL_SERVER
  46390. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  46391. #endif
  46392. ExpectIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  46393. #ifndef NO_WOLFSSL_CLIENT
  46394. #ifndef WOLFSSL_NO_TLS12
  46395. ExpectIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  46396. #endif
  46397. ExpectIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  46398. #endif
  46399. #ifndef NO_WOLFSSL_SERVER
  46400. ExpectIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  46401. #endif
  46402. ExpectIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  46403. #ifndef NO_WOLFSSL_CLIENT
  46404. #ifndef WOLFSSL_NO_TLS12
  46405. ExpectIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  46406. #endif
  46407. ExpectIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  46408. #endif
  46409. #ifndef NO_WOLFSSL_SERVER
  46410. ExpectIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  46411. #endif
  46412. ExpectIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  46413. ExpectIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  46414. #ifndef NO_WOLFSSL_CLIENT
  46415. #ifndef WOLFSSL_NO_TLS12
  46416. ExpectIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  46417. BAD_FUNC_ARG);
  46418. #endif
  46419. ExpectIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  46420. #endif
  46421. #ifndef NO_WOLFSSL_SERVER
  46422. ExpectIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  46423. #endif
  46424. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  46425. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  46426. #ifndef NO_WOLFSSL_SERVER
  46427. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  46428. #endif
  46429. #ifndef NO_WOLFSSL_CLIENT
  46430. #ifndef WOLFSSL_NO_TLS12
  46431. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  46432. BAD_FUNC_ARG);
  46433. #endif
  46434. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  46435. #endif
  46436. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  46437. #ifndef NO_WOLFSSL_SERVER
  46438. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  46439. #endif
  46440. #ifndef NO_WOLFSSL_CLIENT
  46441. #ifndef WOLFSSL_NO_TLS12
  46442. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  46443. BAD_FUNC_ARG);
  46444. #endif
  46445. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  46446. #endif
  46447. ExpectIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  46448. #ifndef NO_WOLFSSL_CLIENT
  46449. ExpectIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  46450. #endif
  46451. #ifndef NO_WOLFSSL_SERVER
  46452. #ifndef WOLFSSL_NO_TLS12
  46453. ExpectIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  46454. BAD_FUNC_ARG);
  46455. #endif
  46456. ExpectIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  46457. #endif
  46458. #endif
  46459. #ifdef HAVE_ECC
  46460. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  46461. ExpectIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  46462. #ifndef NO_WOLFSSL_SERVER
  46463. ExpectIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  46464. #endif
  46465. #ifndef NO_WOLFSSL_CLIENT
  46466. #ifndef WOLFSSL_NO_TLS12
  46467. ExpectIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  46468. #endif
  46469. ExpectIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  46470. #endif
  46471. #endif
  46472. #ifdef HAVE_SUPPORTED_CURVES
  46473. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  46474. #ifndef NO_WOLFSSL_CLIENT
  46475. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  46476. #endif
  46477. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  46478. #ifndef NO_WOLFSSL_CLIENT
  46479. #ifndef WOLFSSL_NO_TLS12
  46480. ExpectIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  46481. BAD_FUNC_ARG);
  46482. #endif
  46483. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  46484. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  46485. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  46486. WOLFSSL_SUCCESS);
  46487. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  46488. BAD_FUNC_ARG);
  46489. #endif
  46490. #ifndef NO_WOLFSSL_SERVER
  46491. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  46492. WOLFSSL_SUCCESS);
  46493. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  46494. BAD_FUNC_ARG);
  46495. #endif
  46496. ExpectIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  46497. #ifndef NO_WOLFSSL_CLIENT
  46498. ExpectIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  46499. #endif
  46500. ExpectIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  46501. #ifndef NO_WOLFSSL_CLIENT
  46502. #ifndef WOLFSSL_NO_TLS12
  46503. ExpectIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  46504. BAD_FUNC_ARG);
  46505. #endif
  46506. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups,
  46507. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  46508. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  46509. WOLFSSL_SUCCESS);
  46510. ExpectIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  46511. BAD_FUNC_ARG);
  46512. #endif
  46513. #ifndef NO_WOLFSSL_SERVER
  46514. ExpectIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  46515. WOLFSSL_SUCCESS);
  46516. ExpectIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  46517. BAD_FUNC_ARG);
  46518. #endif
  46519. #ifdef OPENSSL_EXTRA
  46520. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  46521. #ifndef NO_WOLFSSL_CLIENT
  46522. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL),
  46523. WOLFSSL_FAILURE);
  46524. #endif
  46525. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList),
  46526. WOLFSSL_FAILURE);
  46527. #ifndef NO_WOLFSSL_CLIENT
  46528. #ifndef WOLFSSL_NO_TLS12
  46529. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  46530. WOLFSSL_FAILURE);
  46531. #endif
  46532. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  46533. WOLFSSL_SUCCESS);
  46534. #endif
  46535. #ifndef NO_WOLFSSL_SERVER
  46536. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  46537. WOLFSSL_SUCCESS);
  46538. #endif
  46539. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  46540. #ifndef NO_WOLFSSL_CLIENT
  46541. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  46542. #endif
  46543. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  46544. #ifndef NO_WOLFSSL_CLIENT
  46545. #ifndef WOLFSSL_NO_TLS12
  46546. ExpectIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  46547. WOLFSSL_FAILURE);
  46548. #endif
  46549. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  46550. WOLFSSL_SUCCESS);
  46551. #endif
  46552. #ifndef NO_WOLFSSL_SERVER
  46553. ExpectIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  46554. WOLFSSL_SUCCESS);
  46555. #endif
  46556. #endif /* OPENSSL_EXTRA */
  46557. #endif /* HAVE_SUPPORTED_CURVES */
  46558. #endif /* HAVE_ECC */
  46559. #ifdef WOLFSSL_EARLY_DATA
  46560. #ifndef OPENSSL_EXTRA
  46561. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  46562. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  46563. #else
  46564. ExpectIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  46565. ExpectIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  46566. #endif
  46567. #ifndef NO_WOLFSSL_CLIENT
  46568. #ifndef OPENSSL_EXTRA
  46569. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  46570. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  46571. #else
  46572. ExpectIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  46573. ExpectIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  46574. #endif
  46575. #endif
  46576. #ifndef NO_WOLFSSL_SERVER
  46577. #ifndef WOLFSSL_NO_TLS12
  46578. #ifndef OPENSSL_EXTRA
  46579. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  46580. BAD_FUNC_ARG);
  46581. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  46582. #else
  46583. ExpectIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  46584. BAD_FUNC_ARG);
  46585. ExpectIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  46586. #endif
  46587. #endif
  46588. #ifndef OPENSSL_EXTRA
  46589. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  46590. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32),
  46591. WOLFSSL_SUCCESS);
  46592. #else
  46593. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  46594. #endif
  46595. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  46596. #else
  46597. ExpectIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  46598. ExpectIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  46599. #endif
  46600. #endif
  46601. #ifndef OPENSSL_EXTRA
  46602. ExpectIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  46603. ExpectIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  46604. #else
  46605. ExpectIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  46606. ExpectIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  46607. #endif
  46608. #ifndef NO_WOLFSSL_CLIENT
  46609. #ifndef OPENSSL_EXTRA
  46610. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  46611. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  46612. #else
  46613. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  46614. #endif
  46615. ExpectIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  46616. #else
  46617. ExpectIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  46618. ExpectIntEQ(SSL_get_max_early_data(clientSsl), 17);
  46619. #endif
  46620. #endif
  46621. #ifndef NO_WOLFSSL_SERVER
  46622. #ifndef WOLFSSL_NO_TLS12
  46623. #ifndef OPENSSL_EXTRA
  46624. ExpectIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  46625. ExpectIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  46626. #else
  46627. ExpectIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  46628. ExpectIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  46629. #endif
  46630. #endif
  46631. #ifndef OPENSSL_EXTRA
  46632. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  46633. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), WOLFSSL_SUCCESS);
  46634. #else
  46635. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  46636. #endif
  46637. ExpectIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  46638. #else
  46639. ExpectIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  46640. ExpectIntEQ(SSL_get_max_early_data(serverSsl), 16);
  46641. #endif
  46642. #endif
  46643. ExpectIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  46644. &outSz), BAD_FUNC_ARG);
  46645. #ifndef NO_WOLFSSL_CLIENT
  46646. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  46647. &outSz), BAD_FUNC_ARG);
  46648. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  46649. BAD_FUNC_ARG);
  46650. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  46651. sizeof(earlyData), NULL), BAD_FUNC_ARG);
  46652. #endif
  46653. #ifndef NO_WOLFSSL_SERVER
  46654. ExpectIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  46655. sizeof(earlyData), &outSz), SIDE_ERROR);
  46656. #endif
  46657. #ifndef NO_WOLFSSL_CLIENT
  46658. #ifndef WOLFSSL_NO_TLS12
  46659. ExpectIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  46660. sizeof(earlyData), &outSz), BAD_FUNC_ARG);
  46661. #endif
  46662. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  46663. sizeof(earlyData), &outSz), WOLFSSL_FATAL_ERROR);
  46664. #endif
  46665. ExpectIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  46666. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  46667. #ifndef NO_WOLFSSL_SERVER
  46668. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  46669. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  46670. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1,
  46671. &outSz), BAD_FUNC_ARG);
  46672. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  46673. sizeof(earlyDataBuffer), NULL), BAD_FUNC_ARG);
  46674. #endif
  46675. #ifndef NO_WOLFSSL_CLIENT
  46676. ExpectIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  46677. sizeof(earlyDataBuffer), &outSz), SIDE_ERROR);
  46678. #endif
  46679. #ifndef NO_WOLFSSL_SERVER
  46680. #ifndef WOLFSSL_NO_TLS12
  46681. ExpectIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  46682. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  46683. #endif
  46684. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  46685. sizeof(earlyDataBuffer), &outSz), WOLFSSL_FATAL_ERROR);
  46686. #endif
  46687. #endif
  46688. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  46689. ExpectIntLT(SSL_get_early_data_status(NULL), 0);
  46690. #endif
  46691. #ifndef NO_WOLFSSL_SERVER
  46692. wolfSSL_free(serverSsl);
  46693. wolfSSL_CTX_free(serverCtx);
  46694. #endif
  46695. #ifndef NO_WOLFSSL_CLIENT
  46696. wolfSSL_free(clientSsl);
  46697. wolfSSL_CTX_free(clientCtx);
  46698. #endif
  46699. #ifndef WOLFSSL_NO_TLS12
  46700. #ifndef NO_WOLFSSL_SERVER
  46701. wolfSSL_free(serverTls12Ssl);
  46702. wolfSSL_CTX_free(serverTls12Ctx);
  46703. #endif
  46704. #ifndef NO_WOLFSSL_CLIENT
  46705. wolfSSL_free(clientTls12Ssl);
  46706. wolfSSL_CTX_free(clientTls12Ctx);
  46707. #endif
  46708. #endif
  46709. return EXPECT_RESULT();
  46710. }
  46711. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  46712. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  46713. defined(BUILD_TLS_AES_256_GCM_SHA384)
  46714. /* Called when writing. */
  46715. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  46716. {
  46717. (void)ssl;
  46718. (void)buf;
  46719. (void)sz;
  46720. (void)ctx;
  46721. /* Force error return from wolfSSL_accept_TLSv13(). */
  46722. return WANT_WRITE;
  46723. }
  46724. /* Called when reading. */
  46725. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  46726. {
  46727. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  46728. int len = (int)msg->length;
  46729. (void)ssl;
  46730. (void)sz;
  46731. /* Pass back as much of message as will fit in buffer. */
  46732. if (len > sz)
  46733. len = sz;
  46734. XMEMCPY(buf, msg->buffer, len);
  46735. /* Move over returned data. */
  46736. msg->buffer += len;
  46737. msg->length -= len;
  46738. /* Amount actually copied. */
  46739. return len;
  46740. }
  46741. #endif
  46742. static int test_tls13_cipher_suites(void)
  46743. {
  46744. EXPECT_DECLS;
  46745. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  46746. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  46747. defined(BUILD_TLS_AES_256_GCM_SHA384)
  46748. WOLFSSL_CTX* ctx = NULL;
  46749. WOLFSSL *ssl = NULL;
  46750. int i;
  46751. byte clientHello[] = {
  46752. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  46753. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  46754. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  46755. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  46756. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  46757. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  46758. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  46759. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  46760. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46761. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  46762. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  46763. 0x13, 0x01,
  46764. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  46765. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  46766. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  46767. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  46768. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  46769. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  46770. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  46771. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  46772. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  46773. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  46774. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  46775. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  46776. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  46777. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  46778. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  46779. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  46780. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  46781. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  46782. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  46783. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  46784. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  46785. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  46786. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  46787. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  46788. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  46789. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  46790. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  46791. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  46792. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  46793. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  46794. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  46795. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  46796. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  46797. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  46798. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  46799. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46800. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46801. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  46802. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  46803. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  46804. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  46805. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  46806. };
  46807. WOLFSSL_BUFFER_INFO msg;
  46808. /* Offset into ClientHello message data of first cipher suite. */
  46809. const int csOff = 78;
  46810. /* Server cipher list. */
  46811. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  46812. /* Suite list with duplicates. */
  46813. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  46814. "TLS13-AES128-GCM-SHA256:"
  46815. "TLS13-AES256-GCM-SHA384:"
  46816. "TLS13-AES256-GCM-SHA384:"
  46817. "TLS13-AES128-GCM-SHA256";
  46818. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  46819. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  46820. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  46821. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  46822. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  46823. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  46824. #endif
  46825. /* Set up wolfSSL context. */
  46826. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  46827. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  46828. WOLFSSL_FILETYPE_PEM));
  46829. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  46830. WOLFSSL_FILETYPE_PEM));
  46831. /* Read from 'msg'. */
  46832. wolfSSL_SetIORecv(ctx, CsRecv);
  46833. /* No where to send to - dummy sender. */
  46834. wolfSSL_SetIOSend(ctx, CsSend);
  46835. /* Test cipher suite list with many copies of a cipher suite. */
  46836. ExpectNotNull(ssl = wolfSSL_new(ctx));
  46837. msg.buffer = clientHello;
  46838. msg.length = (unsigned int)sizeof(clientHello);
  46839. wolfSSL_SetIOReadCtx(ssl, &msg);
  46840. /* Force server to have as many occurrences of same cipher suite as
  46841. * possible. */
  46842. if (ssl != NULL) {
  46843. Suites* suites = (Suites*)WOLFSSL_SUITES(ssl);
  46844. suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  46845. for (i = 0; i < suites->suiteSz; i += 2) {
  46846. suites->suites[i + 0] = TLS13_BYTE;
  46847. suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  46848. }
  46849. }
  46850. /* Test multiple occurrences of same cipher suite. */
  46851. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  46852. wolfSSL_free(ssl);
  46853. ssl = NULL;
  46854. /* Set client order opposite to server order:
  46855. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  46856. clientHello[csOff + 0] = TLS13_BYTE;
  46857. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  46858. clientHello[csOff + 2] = TLS13_BYTE;
  46859. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  46860. /* Test server order negotiation. */
  46861. ExpectNotNull(ssl = wolfSSL_new(ctx));
  46862. msg.buffer = clientHello;
  46863. msg.length = (unsigned int)sizeof(clientHello);
  46864. wolfSSL_SetIOReadCtx(ssl, &msg);
  46865. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  46866. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  46867. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  46868. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  46869. /* Check refined order - server order. */
  46870. ExpectIntEQ(ssl->suites->suiteSz, 4);
  46871. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  46872. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  46873. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  46874. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  46875. wolfSSL_free(ssl);
  46876. ssl = NULL;
  46877. /* Test client order negotiation. */
  46878. ExpectNotNull(ssl = wolfSSL_new(ctx));
  46879. msg.buffer = clientHello;
  46880. msg.length = (unsigned int)sizeof(clientHello);
  46881. wolfSSL_SetIOReadCtx(ssl, &msg);
  46882. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  46883. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  46884. ExpectIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  46885. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  46886. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  46887. /* Check refined order - client order. */
  46888. ExpectIntEQ(ssl->suites->suiteSz, 4);
  46889. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  46890. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  46891. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  46892. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  46893. wolfSSL_free(ssl);
  46894. ssl = NULL;
  46895. /* Check duplicate detection is working. */
  46896. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  46897. ExpectIntEQ(ctx->suites->suiteSz, 4);
  46898. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  46899. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  46900. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  46901. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  46902. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  46903. ExpectIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  46904. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  46905. ExpectIntEQ(ctx->suites->suiteSz, 4);
  46906. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  46907. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  46908. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  46909. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  46910. #endif
  46911. wolfSSL_CTX_free(ctx);
  46912. #endif
  46913. return EXPECT_RESULT();
  46914. }
  46915. #endif
  46916. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  46917. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  46918. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  46919. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  46920. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  46921. const unsigned char* priv, unsigned int privSz,
  46922. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  46923. unsigned char* out, unsigned int* outlen,
  46924. void* ctx)
  46925. {
  46926. int result;
  46927. /* Test fail when context associated with WOLFSSL is NULL */
  46928. if (ctx == NULL) {
  46929. return -1;
  46930. }
  46931. (void)ssl;
  46932. /* return 0 on success */
  46933. PRIVATE_KEY_UNLOCK();
  46934. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  46935. PRIVATE_KEY_LOCK();
  46936. return result;
  46937. }
  46938. static int test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  46939. EXPECT_DECLS;
  46940. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  46941. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  46942. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  46943. WOLFSSL_SUCCESS);
  46944. #endif
  46945. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  46946. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  46947. WOLFSSL_SUCCESS);
  46948. #endif
  46949. return EXPECT_RESULT();
  46950. }
  46951. static int test_dh_ssl_setup(WOLFSSL* ssl)
  46952. {
  46953. EXPECT_DECLS;
  46954. static int dh_test_ctx = 1;
  46955. int ret;
  46956. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  46957. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  46958. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  46959. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  46960. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  46961. }
  46962. return EXPECT_RESULT();
  46963. }
  46964. static int test_dh_ssl_setup_fail(WOLFSSL* ssl)
  46965. {
  46966. EXPECT_DECLS;
  46967. int ret;
  46968. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  46969. ExpectNull(wolfSSL_GetDhAgreeCtx(ssl));
  46970. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  46971. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  46972. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  46973. }
  46974. return EXPECT_RESULT();
  46975. }
  46976. #endif
  46977. static int test_DhCallbacks(void)
  46978. {
  46979. EXPECT_DECLS;
  46980. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  46981. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  46982. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  46983. WOLFSSL_CTX *ctx = NULL;
  46984. WOLFSSL *ssl = NULL;
  46985. int test;
  46986. test_ssl_cbf func_cb_client;
  46987. test_ssl_cbf func_cb_server;
  46988. /* Test that DH callback APIs work. */
  46989. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  46990. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  46991. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  46992. /* load client ca cert */
  46993. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  46994. WOLFSSL_SUCCESS);
  46995. /* test with NULL arguments */
  46996. wolfSSL_SetDhAgreeCtx(NULL, &test);
  46997. ExpectNull(wolfSSL_GetDhAgreeCtx(NULL));
  46998. /* test success case */
  46999. test = 1;
  47000. ExpectNotNull(ssl = wolfSSL_new(ctx));
  47001. wolfSSL_SetDhAgreeCtx(ssl, &test);
  47002. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  47003. wolfSSL_free(ssl);
  47004. wolfSSL_CTX_free(ctx);
  47005. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  47006. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  47007. /* set callbacks to use DH functions */
  47008. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  47009. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  47010. func_cb_client.method = wolfTLSv1_2_client_method;
  47011. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  47012. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  47013. func_cb_server.method = wolfTLSv1_2_server_method;
  47014. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  47015. &func_cb_server, NULL), TEST_SUCCESS);
  47016. /* Test fail */
  47017. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  47018. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  47019. /* set callbacks to use DH functions */
  47020. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  47021. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  47022. func_cb_client.method = wolfTLSv1_2_client_method;
  47023. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  47024. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  47025. func_cb_server.method = wolfTLSv1_2_server_method;
  47026. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  47027. &func_cb_server, NULL), TEST_FAIL);
  47028. #endif
  47029. return EXPECT_RESULT();
  47030. }
  47031. #endif /* HAVE_PK_CALLBACKS */
  47032. #ifdef HAVE_HASHDRBG
  47033. #ifdef TEST_RESEED_INTERVAL
  47034. static int test_wc_RNG_GenerateBlock_Reseed(void)
  47035. {
  47036. EXPECT_DECLS;
  47037. int i;
  47038. WC_RNG rng;
  47039. byte key[32];
  47040. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47041. ExpectIntEQ(wc_InitRng(&rng), 0);
  47042. for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  47043. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  47044. }
  47045. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  47046. return EXPECT_RESULT();
  47047. }
  47048. #endif /* TEST_RESEED_INTERVAL */
  47049. static int test_wc_RNG_GenerateBlock(void)
  47050. {
  47051. EXPECT_DECLS;
  47052. int i;
  47053. WC_RNG rng;
  47054. byte key[32];
  47055. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47056. ExpectIntEQ(wc_InitRng(&rng), 0);
  47057. for (i = 0; i < 10; i++) {
  47058. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  47059. }
  47060. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  47061. return EXPECT_RESULT();
  47062. }
  47063. #endif /* HAVE_HASHDRBG */
  47064. /*
  47065. * Testing get_rand_digit
  47066. */
  47067. static int test_get_rand_digit(void)
  47068. {
  47069. EXPECT_DECLS;
  47070. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  47071. WC_RNG rng;
  47072. mp_digit d;
  47073. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47074. ExpectIntEQ(wc_InitRng(&rng), 0);
  47075. ExpectIntEQ(get_rand_digit(&rng, &d), 0);
  47076. ExpectIntEQ(get_rand_digit(NULL, NULL), BAD_FUNC_ARG);
  47077. ExpectIntEQ(get_rand_digit(NULL, &d), BAD_FUNC_ARG);
  47078. ExpectIntEQ(get_rand_digit(&rng, NULL), BAD_FUNC_ARG);
  47079. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  47080. #endif
  47081. return EXPECT_RESULT();
  47082. } /* End test_get_rand_digit*/
  47083. /*
  47084. * Testing get_digit_count
  47085. */
  47086. static int test_get_digit_count(void)
  47087. {
  47088. EXPECT_DECLS;
  47089. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  47090. mp_int a;
  47091. XMEMSET(&a, 0, sizeof(mp_int));
  47092. ExpectIntEQ(mp_init(&a), 0);
  47093. ExpectIntEQ(get_digit_count(NULL), 0);
  47094. ExpectIntEQ(get_digit_count(&a), 0);
  47095. mp_clear(&a);
  47096. #endif
  47097. return EXPECT_RESULT();
  47098. } /* End test_get_digit_count*/
  47099. /*
  47100. * Testing mp_cond_copy
  47101. */
  47102. static int test_mp_cond_copy(void)
  47103. {
  47104. EXPECT_DECLS;
  47105. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  47106. defined(WOLFSSL_PUBLIC_MP)
  47107. mp_int a;
  47108. mp_int b;
  47109. int copy = 0;
  47110. XMEMSET(&a, 0, sizeof(mp_int));
  47111. XMEMSET(&b, 0, sizeof(mp_int));
  47112. ExpectIntEQ(mp_init(&a), MP_OKAY);
  47113. ExpectIntEQ(mp_init(&b), MP_OKAY);
  47114. ExpectIntEQ(mp_cond_copy(NULL, copy, NULL), BAD_FUNC_ARG);
  47115. ExpectIntEQ(mp_cond_copy(NULL, copy, &b), BAD_FUNC_ARG);
  47116. ExpectIntEQ(mp_cond_copy(&a, copy, NULL), BAD_FUNC_ARG);
  47117. ExpectIntEQ(mp_cond_copy(&a, copy, &b), 0);
  47118. mp_clear(&a);
  47119. mp_clear(&b);
  47120. #endif
  47121. return EXPECT_RESULT();
  47122. } /* End test_mp_cond_copy*/
  47123. /*
  47124. * Testing mp_rand
  47125. */
  47126. static int test_mp_rand(void)
  47127. {
  47128. EXPECT_DECLS;
  47129. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  47130. mp_int a;
  47131. WC_RNG rng;
  47132. int digits = 1;
  47133. XMEMSET(&a, 0, sizeof(mp_int));
  47134. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47135. ExpectIntEQ(mp_init(&a), MP_OKAY);
  47136. ExpectIntEQ(wc_InitRng(&rng), 0);
  47137. ExpectIntEQ(mp_rand(&a, digits, NULL), MISSING_RNG_E);
  47138. ExpectIntEQ(mp_rand(NULL, digits, &rng), BAD_FUNC_ARG);
  47139. ExpectIntEQ(mp_rand(&a, 0, &rng), BAD_FUNC_ARG);
  47140. ExpectIntEQ(mp_rand(&a, digits, &rng), 0);
  47141. mp_clear(&a);
  47142. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  47143. #endif
  47144. return EXPECT_RESULT();
  47145. } /* End test_mp_rand*/
  47146. /*
  47147. * Testing get_digit
  47148. */
  47149. static int test_get_digit(void)
  47150. {
  47151. EXPECT_DECLS;
  47152. #if defined(WOLFSSL_PUBLIC_MP)
  47153. mp_int a;
  47154. int n = 0;
  47155. XMEMSET(&a, 0, sizeof(mp_int));
  47156. ExpectIntEQ(mp_init(&a), MP_OKAY);
  47157. ExpectIntEQ(get_digit(NULL, n), 0);
  47158. ExpectIntEQ(get_digit(&a, n), 0);
  47159. mp_clear(&a);
  47160. #endif
  47161. return EXPECT_RESULT();
  47162. } /* End test_get_digit*/
  47163. /*
  47164. * Testing wc_export_int
  47165. */
  47166. static int test_wc_export_int(void)
  47167. {
  47168. EXPECT_DECLS;
  47169. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  47170. defined(WOLFSSL_PUBLIC_MP)
  47171. mp_int mp;
  47172. byte buf[32];
  47173. word32 keySz = (word32)sizeof(buf);
  47174. word32 len = (word32)sizeof(buf);
  47175. XMEMSET(&mp, 0, sizeof(mp_int));
  47176. ExpectIntEQ(mp_init(&mp), MP_OKAY);
  47177. ExpectIntEQ(mp_set(&mp, 1234), 0);
  47178. ExpectIntEQ(wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  47179. BAD_FUNC_ARG);
  47180. len = sizeof(buf)-1;
  47181. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  47182. BUFFER_E);
  47183. len = sizeof(buf);
  47184. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN), 0);
  47185. len = 4; /* test input too small */
  47186. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), BUFFER_E);
  47187. len = sizeof(buf);
  47188. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), 0);
  47189. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  47190. ExpectIntEQ(len, 5);
  47191. mp_clear(&mp);
  47192. #endif
  47193. return EXPECT_RESULT();
  47194. } /* End test_wc_export_int*/
  47195. static int test_wc_InitRngNonce(void)
  47196. {
  47197. EXPECT_DECLS;
  47198. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  47199. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  47200. HAVE_FIPS_VERSION >= 2))
  47201. WC_RNG rng;
  47202. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  47203. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  47204. word32 nonceSz = sizeof(nonce);
  47205. ExpectIntEQ(wc_InitRngNonce(&rng, nonce, nonceSz), 0);
  47206. ExpectIntEQ(wc_FreeRng(&rng), 0);
  47207. #endif
  47208. return EXPECT_RESULT();
  47209. } /* End test_wc_InitRngNonce*/
  47210. /*
  47211. * Testing wc_InitRngNonce_ex
  47212. */
  47213. static int test_wc_InitRngNonce_ex(void)
  47214. {
  47215. EXPECT_DECLS;
  47216. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  47217. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  47218. HAVE_FIPS_VERSION >= 2))
  47219. WC_RNG rng;
  47220. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  47221. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  47222. word32 nonceSz = sizeof(nonce);
  47223. ExpectIntEQ(wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId),
  47224. 0);
  47225. ExpectIntEQ(wc_FreeRng(&rng), 0);
  47226. #endif
  47227. return EXPECT_RESULT();
  47228. } /* End test_wc_InitRngNonce_ex */
  47229. static int test_wolfSSL_X509_CRL(void)
  47230. {
  47231. EXPECT_DECLS;
  47232. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  47233. X509_CRL *crl = NULL;
  47234. char pem[][100] = {
  47235. "./certs/crl/crl.pem",
  47236. "./certs/crl/crl2.pem",
  47237. "./certs/crl/caEccCrl.pem",
  47238. "./certs/crl/eccCliCRL.pem",
  47239. "./certs/crl/eccSrvCRL.pem",
  47240. ""
  47241. };
  47242. #ifndef NO_BIO
  47243. BIO *bio = NULL;
  47244. #endif
  47245. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  47246. char der[][100] = {
  47247. "./certs/crl/crl.der",
  47248. "./certs/crl/crl2.der",
  47249. ""};
  47250. #endif
  47251. XFILE fp = XBADFILE;
  47252. int i;
  47253. for (i = 0; pem[i][0] != '\0'; i++)
  47254. {
  47255. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  47256. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  47257. NULL, NULL));
  47258. ExpectNotNull(crl);
  47259. X509_CRL_free(crl);
  47260. if (fp != XBADFILE) {
  47261. XFCLOSE(fp);
  47262. fp = XBADFILE;
  47263. }
  47264. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  47265. ExpectNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL,
  47266. NULL));
  47267. if (EXPECT_FAIL()) {
  47268. crl = NULL;
  47269. }
  47270. ExpectNotNull(crl);
  47271. X509_CRL_free(crl);
  47272. crl = NULL;
  47273. if (fp != XBADFILE) {
  47274. XFCLOSE(fp);
  47275. fp = XBADFILE;
  47276. }
  47277. }
  47278. #ifndef NO_BIO
  47279. for (i = 0; pem[i][0] != '\0'; i++)
  47280. {
  47281. ExpectNotNull(bio = BIO_new_file(pem[i], "rb"));
  47282. ExpectNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  47283. X509_CRL_free(crl);
  47284. crl = NULL;
  47285. BIO_free(bio);
  47286. bio = NULL;
  47287. }
  47288. #endif
  47289. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  47290. for (i = 0; der[i][0] != '\0'; i++) {
  47291. ExpectTrue((fp = XFOPEN(der[i], "rb")) != XBADFILE);
  47292. ExpectTrue((fp != XBADFILE));
  47293. ExpectNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  47294. ExpectNotNull(crl);
  47295. X509_CRL_free(crl);
  47296. if (fp != XBADFILE) {
  47297. XFCLOSE(fp);
  47298. fp = XBADFILE;
  47299. }
  47300. fp = XFOPEN(der[i], "rb");
  47301. ExpectTrue((fp != XBADFILE));
  47302. ExpectNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  47303. if (EXPECT_FAIL()) {
  47304. crl = NULL;
  47305. }
  47306. ExpectNotNull(crl);
  47307. X509_CRL_free(crl);
  47308. crl = NULL;
  47309. if (fp != XBADFILE) {
  47310. XFCLOSE(fp);
  47311. fp = XBADFILE;
  47312. }
  47313. }
  47314. #endif
  47315. #endif
  47316. return EXPECT_RESULT();
  47317. }
  47318. static int test_wolfSSL_X509_load_crl_file(void)
  47319. {
  47320. EXPECT_DECLS;
  47321. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  47322. !defined(NO_RSA) && !defined(NO_BIO)
  47323. int i;
  47324. char pem[][100] = {
  47325. "./certs/crl/crl.pem",
  47326. "./certs/crl/crl2.pem",
  47327. "./certs/crl/caEccCrl.pem",
  47328. "./certs/crl/eccCliCRL.pem",
  47329. "./certs/crl/eccSrvCRL.pem",
  47330. ""
  47331. };
  47332. char der[][100] = {
  47333. "./certs/crl/crl.der",
  47334. "./certs/crl/crl2.der",
  47335. ""
  47336. };
  47337. WOLFSSL_X509_STORE* store = NULL;
  47338. WOLFSSL_X509_LOOKUP* lookup = NULL;
  47339. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  47340. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  47341. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  47342. X509_FILETYPE_PEM), 1);
  47343. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  47344. X509_FILETYPE_PEM), 1);
  47345. if (store) {
  47346. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  47347. WOLFSSL_FILETYPE_PEM), 1);
  47348. /* since store hasn't yet known the revoked cert*/
  47349. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  47350. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  47351. }
  47352. for (i = 0; pem[i][0] != '\0'; i++) {
  47353. ExpectIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM),
  47354. 1);
  47355. }
  47356. if (store) {
  47357. /* since store knows crl list */
  47358. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  47359. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  47360. CRL_CERT_REVOKED);
  47361. }
  47362. /* once feeing store */
  47363. X509_STORE_free(store);
  47364. store = NULL;
  47365. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  47366. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  47367. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  47368. X509_FILETYPE_PEM), 1);
  47369. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  47370. X509_FILETYPE_PEM), 1);
  47371. if (store) {
  47372. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  47373. WOLFSSL_FILETYPE_PEM), 1);
  47374. /* since store hasn't yet known the revoked cert*/
  47375. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  47376. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  47377. }
  47378. for (i = 0; der[i][0] != '\0'; i++) {
  47379. ExpectIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1),
  47380. 1);
  47381. }
  47382. if (store) {
  47383. /* since store knows crl list */
  47384. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  47385. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  47386. CRL_CERT_REVOKED);
  47387. }
  47388. /* test for incorrect parameter */
  47389. ExpectIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  47390. ExpectIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  47391. ExpectIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  47392. X509_STORE_free(store);
  47393. store = NULL;
  47394. #endif
  47395. return EXPECT_RESULT();
  47396. }
  47397. static int test_wolfSSL_i2d_X509(void)
  47398. {
  47399. EXPECT_DECLS;
  47400. #if defined(OPENSSL_EXTRA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  47401. const unsigned char* cert_buf = server_cert_der_2048;
  47402. unsigned char* out = NULL;
  47403. unsigned char* tmp = NULL;
  47404. X509* cert = NULL;
  47405. ExpectNotNull(d2i_X509(&cert, &cert_buf, sizeof_server_cert_der_2048));
  47406. /* Pointer should be advanced */
  47407. ExpectPtrGT(cert_buf, server_cert_der_2048);
  47408. ExpectIntGT(i2d_X509(cert, &out), 0);
  47409. ExpectNotNull(out);
  47410. tmp = out;
  47411. ExpectIntGT(i2d_X509(cert, &tmp), 0);
  47412. ExpectPtrGT(tmp, out);
  47413. if (out != NULL)
  47414. XFREE(out, NULL, DYNAMIC_TYPE_OPENSSL);
  47415. X509_free(cert);
  47416. #endif
  47417. return EXPECT_RESULT();
  47418. }
  47419. static int test_wolfSSL_d2i_X509_REQ(void)
  47420. {
  47421. EXPECT_DECLS;
  47422. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  47423. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  47424. !defined(WOLFSSL_SP_MATH)
  47425. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der
  47426. * were generated by libest
  47427. * ./certs/csr.attr.der contains sample attributes
  47428. * ./certs/csr.ext.der contains sample extensions */
  47429. const char* csrFile = "./certs/csr.signed.der";
  47430. const char* csrPopFile = "./certs/csr.attr.der";
  47431. const char* csrExtFile = "./certs/csr.ext.der";
  47432. /* ./certs/csr.dsa.pem is generated using
  47433. * openssl req -newkey dsa:certs/dsaparams.pem \
  47434. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  47435. * -outform PEM
  47436. * with the passphrase "wolfSSL"
  47437. */
  47438. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  47439. const char* csrDsaFile = "./certs/csr.dsa.pem";
  47440. XFILE f = XBADFILE;
  47441. #endif
  47442. BIO* bio = NULL;
  47443. X509* req = NULL;
  47444. EVP_PKEY *pub_key = NULL;
  47445. {
  47446. ExpectNotNull(bio = BIO_new_file(csrFile, "rb"));
  47447. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  47448. /*
  47449. * Extract the public key from the CSR
  47450. */
  47451. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  47452. /*
  47453. * Verify the signature in the CSR
  47454. */
  47455. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  47456. X509_free(req);
  47457. req = NULL;
  47458. BIO_free(bio);
  47459. bio = NULL;
  47460. EVP_PKEY_free(pub_key);
  47461. pub_key = NULL;
  47462. }
  47463. {
  47464. #ifdef OPENSSL_ALL
  47465. X509_ATTRIBUTE* attr;
  47466. ASN1_TYPE *at;
  47467. #endif
  47468. ExpectNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  47469. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  47470. /*
  47471. * Extract the public key from the CSR
  47472. */
  47473. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  47474. /*
  47475. * Verify the signature in the CSR
  47476. */
  47477. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  47478. #ifdef OPENSSL_ALL
  47479. /*
  47480. * Obtain the challenge password from the CSR
  47481. */
  47482. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  47483. -1), 1);
  47484. ExpectNotNull(attr = X509_REQ_get_attr(req, 1));
  47485. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  47486. ExpectNotNull(at->value.asn1_string);
  47487. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string),
  47488. "2xIE+qqp/rhyTXP+");
  47489. ExpectIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  47490. #endif
  47491. X509_free(req);
  47492. req = NULL;
  47493. BIO_free(bio);
  47494. bio = NULL;
  47495. EVP_PKEY_free(pub_key);
  47496. pub_key = NULL;
  47497. }
  47498. {
  47499. #ifdef OPENSSL_ALL
  47500. X509_ATTRIBUTE* attr;
  47501. ASN1_TYPE *at;
  47502. STACK_OF(X509_EXTENSION) *exts = NULL;
  47503. #endif
  47504. ExpectNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  47505. /* This CSR contains an Extension Request attribute so
  47506. * we test extension parsing in a CSR attribute here. */
  47507. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  47508. /*
  47509. * Extract the public key from the CSR
  47510. */
  47511. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  47512. /*
  47513. * Verify the signature in the CSR
  47514. */
  47515. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  47516. #ifdef OPENSSL_ALL
  47517. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  47518. req));
  47519. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  47520. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  47521. /*
  47522. * Obtain the challenge password from the CSR
  47523. */
  47524. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  47525. -1), 0);
  47526. ExpectNotNull(attr = X509_REQ_get_attr(req, 0));
  47527. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  47528. ExpectNotNull(at->value.asn1_string);
  47529. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  47530. ExpectIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  47531. ExpectIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  47532. #endif
  47533. X509_free(req);
  47534. req = NULL;
  47535. BIO_free(bio);
  47536. bio = NULL;
  47537. EVP_PKEY_free(pub_key);
  47538. pub_key = NULL;
  47539. }
  47540. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  47541. {
  47542. ExpectNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  47543. ExpectNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  47544. /*
  47545. * Extract the public key from the CSR
  47546. */
  47547. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  47548. /*
  47549. * Verify the signature in the CSR
  47550. */
  47551. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  47552. X509_free(req);
  47553. req = NULL;
  47554. BIO_free(bio);
  47555. /* Run the same test, but with a file pointer instead of a BIO.
  47556. * (PEM_read_X509_REQ)*/
  47557. ExpectTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  47558. ExpectNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  47559. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  47560. X509_free(req);
  47561. EVP_PKEY_free(pub_key);
  47562. }
  47563. #endif /* !NO_DSA && !HAVE_SELFTEST */
  47564. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  47565. return EXPECT_RESULT();
  47566. }
  47567. static int test_wolfSSL_PEM_read_X509(void)
  47568. {
  47569. EXPECT_DECLS;
  47570. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  47571. !defined(NO_RSA)
  47572. X509 *x509 = NULL;
  47573. XFILE fp = XBADFILE;
  47574. ExpectTrue((fp = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  47575. ExpectNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  47576. X509_free(x509);
  47577. if (fp != XBADFILE)
  47578. XFCLOSE(fp);
  47579. #endif
  47580. return EXPECT_RESULT();
  47581. }
  47582. static int test_wolfSSL_PEM_read(void)
  47583. {
  47584. EXPECT_DECLS;
  47585. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  47586. const char* filename = "./certs/server-keyEnc.pem";
  47587. XFILE fp = XBADFILE;
  47588. char* name = NULL;
  47589. char* header = NULL;
  47590. byte* data = NULL;
  47591. long len;
  47592. EVP_CIPHER_INFO cipher;
  47593. WOLFSSL_BIO* bio = NULL;
  47594. byte* fileData = NULL;
  47595. size_t fileDataSz = 0;
  47596. byte* out;
  47597. ExpectTrue((fp = XFOPEN(filename, "rb")) != XBADFILE);
  47598. /* Fail cases. */
  47599. ExpectIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  47600. ExpectIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  47601. ExpectIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  47602. ExpectIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  47603. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  47604. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  47605. ExpectIntGT(XSTRLEN(header), 0);
  47606. ExpectIntGT(len, 0);
  47607. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  47608. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  47609. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  47610. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  47611. DYNAMIC_TYPE_TMP_BUFFER));
  47612. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  47613. if (fp != XBADFILE) {
  47614. XFCLOSE(fp);
  47615. fp = XBADFILE;
  47616. }
  47617. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  47618. /* Fail cases. */
  47619. ExpectIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  47620. ExpectIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  47621. ExpectIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  47622. ExpectIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  47623. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  47624. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  47625. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  47626. /* Fail cases. */
  47627. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  47628. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  47629. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  47630. #ifndef NO_DES3
  47631. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  47632. #endif
  47633. /* Fail cases. */
  47634. ExpectIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  47635. (void*)"yassl123"), WOLFSSL_FAILURE);
  47636. ExpectIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  47637. (void*)"yassl123"), WOLFSSL_FAILURE);
  47638. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  47639. (void*)"yassl123"), WOLFSSL_FAILURE);
  47640. #if !defined(NO_DES3) && !defined(NO_MD5)
  47641. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  47642. (void*)"yassl123"), WOLFSSL_SUCCESS);
  47643. #endif
  47644. BIO_free(bio);
  47645. bio = NULL;
  47646. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47647. fileData = NULL;
  47648. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47649. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47650. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47651. name = NULL;
  47652. header = NULL;
  47653. data = NULL;
  47654. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  47655. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  47656. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  47657. ExpectIntEQ(XSTRLEN(header), 0);
  47658. ExpectIntGT(len, 0);
  47659. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  47660. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  47661. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  47662. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  47663. DYNAMIC_TYPE_TMP_BUFFER));
  47664. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  47665. if (fp != XBADFILE)
  47666. XFCLOSE(fp);
  47667. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  47668. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  47669. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  47670. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  47671. BIO_free(bio);
  47672. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47673. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47674. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47675. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47676. #endif
  47677. return EXPECT_RESULT();
  47678. }
  47679. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  47680. {
  47681. EXPECT_DECLS;
  47682. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  47683. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  47684. const byte iv[12] = { 0 };
  47685. const byte key[16] = { 0 };
  47686. const byte cleartext[16] = { 0 };
  47687. const byte aad[] = {
  47688. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  47689. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  47690. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  47691. 0x4f
  47692. };
  47693. byte out1Part[16];
  47694. byte outTag1Part[16];
  47695. byte out2Part[16];
  47696. byte outTag2Part[16];
  47697. byte decryptBuf[16];
  47698. int len = 0;
  47699. int tlen;
  47700. EVP_CIPHER_CTX* ctx = NULL;
  47701. /* ENCRYPT */
  47702. /* Send AAD and data in 1 part */
  47703. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  47704. tlen = 0;
  47705. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  47706. 1);
  47707. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  47708. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  47709. ExpectIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  47710. sizeof(cleartext)), 1);
  47711. tlen += len;
  47712. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  47713. tlen += len;
  47714. ExpectIntEQ(tlen, sizeof(cleartext));
  47715. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  47716. outTag1Part), 1);
  47717. EVP_CIPHER_CTX_free(ctx);
  47718. ctx = NULL;
  47719. /* DECRYPT */
  47720. /* Send AAD and data in 1 part */
  47721. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  47722. tlen = 0;
  47723. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  47724. 1);
  47725. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  47726. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  47727. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  47728. sizeof(cleartext)), 1);
  47729. tlen += len;
  47730. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  47731. outTag1Part), 1);
  47732. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  47733. tlen += len;
  47734. ExpectIntEQ(tlen, sizeof(cleartext));
  47735. EVP_CIPHER_CTX_free(ctx);
  47736. ctx = NULL;
  47737. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  47738. /* ENCRYPT */
  47739. /* Send AAD and data in 2 parts */
  47740. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  47741. tlen = 0;
  47742. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  47743. 1);
  47744. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  47745. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  47746. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  47747. 1);
  47748. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  47749. tlen += len;
  47750. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  47751. sizeof(cleartext) - 1), 1);
  47752. tlen += len;
  47753. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  47754. tlen += len;
  47755. ExpectIntEQ(tlen, sizeof(cleartext));
  47756. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  47757. outTag2Part), 1);
  47758. ExpectIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  47759. ExpectIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  47760. EVP_CIPHER_CTX_free(ctx);
  47761. ctx = NULL;
  47762. /* DECRYPT */
  47763. /* Send AAD and data in 2 parts */
  47764. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  47765. tlen = 0;
  47766. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  47767. 1);
  47768. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  47769. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  47770. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  47771. 1);
  47772. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  47773. tlen += len;
  47774. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  47775. sizeof(cleartext) - 1), 1);
  47776. tlen += len;
  47777. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  47778. outTag1Part), 1);
  47779. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  47780. tlen += len;
  47781. ExpectIntEQ(tlen, sizeof(cleartext));
  47782. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  47783. /* Test AAD reuse */
  47784. EVP_CIPHER_CTX_free(ctx);
  47785. #endif
  47786. return EXPECT_RESULT();
  47787. }
  47788. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  47789. {
  47790. EXPECT_DECLS;
  47791. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  47792. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  47793. /* Zero length plain text */
  47794. byte key[] = {
  47795. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47796. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47797. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47798. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  47799. }; /* align */
  47800. byte iv[] = {
  47801. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  47802. }; /* align */
  47803. byte plaintxt[1];
  47804. int ivSz = 12;
  47805. int plaintxtSz = 0;
  47806. unsigned char tag[16];
  47807. unsigned char tag_kat[] = {
  47808. 0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  47809. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b
  47810. };
  47811. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  47812. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  47813. int ciphertxtSz = 0;
  47814. int decryptedtxtSz = 0;
  47815. int len = 0;
  47816. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  47817. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  47818. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  47819. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  47820. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  47821. plaintxtSz));
  47822. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  47823. ciphertxtSz += len;
  47824. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  47825. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  47826. ExpectIntEQ(0, ciphertxtSz);
  47827. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  47828. EVP_CIPHER_CTX_init(de);
  47829. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  47830. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  47831. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  47832. decryptedtxtSz = len;
  47833. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  47834. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  47835. decryptedtxtSz += len;
  47836. ExpectIntEQ(0, decryptedtxtSz);
  47837. EVP_CIPHER_CTX_free(en);
  47838. EVP_CIPHER_CTX_free(de);
  47839. #endif
  47840. return EXPECT_RESULT();
  47841. }
  47842. static int test_wolfssl_EVP_aes_gcm(void)
  47843. {
  47844. EXPECT_DECLS;
  47845. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  47846. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  47847. /* A 256 bit key, AES_128 will use the first 128 bit*/
  47848. byte *key = (byte*)"01234567890123456789012345678901";
  47849. /* A 128 bit IV */
  47850. byte *iv = (byte*)"0123456789012345";
  47851. int ivSz = AES_BLOCK_SIZE;
  47852. /* Message to be encrypted */
  47853. byte *plaintxt = (byte*)"for things to change you have to change";
  47854. /* Additional non-confidential data */
  47855. byte *aad = (byte*)"Don't spend major time on minor things.";
  47856. unsigned char tag[AES_BLOCK_SIZE] = {0};
  47857. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  47858. int aadSz = (int)XSTRLEN((char*)aad);
  47859. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  47860. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  47861. int ciphertxtSz = 0;
  47862. int decryptedtxtSz = 0;
  47863. int len = 0;
  47864. int i = 0;
  47865. EVP_CIPHER_CTX en[2];
  47866. EVP_CIPHER_CTX de[2];
  47867. for (i = 0; i < 2; i++) {
  47868. EVP_CIPHER_CTX_init(&en[i]);
  47869. if (i == 0) {
  47870. /* Default uses 96-bits IV length */
  47871. #ifdef WOLFSSL_AES_128
  47872. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  47873. key, iv));
  47874. #elif defined(WOLFSSL_AES_192)
  47875. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  47876. key, iv));
  47877. #elif defined(WOLFSSL_AES_256)
  47878. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  47879. key, iv));
  47880. #endif
  47881. }
  47882. else {
  47883. #ifdef WOLFSSL_AES_128
  47884. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  47885. NULL, NULL));
  47886. #elif defined(WOLFSSL_AES_192)
  47887. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  47888. NULL, NULL));
  47889. #elif defined(WOLFSSL_AES_256)
  47890. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  47891. NULL, NULL));
  47892. #endif
  47893. /* non-default must to set the IV length first */
  47894. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  47895. ivSz, NULL));
  47896. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  47897. }
  47898. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  47899. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  47900. plaintxtSz));
  47901. ciphertxtSz = len;
  47902. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  47903. ciphertxtSz += len;
  47904. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  47905. AES_BLOCK_SIZE, tag));
  47906. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  47907. EVP_CIPHER_CTX_init(&de[i]);
  47908. if (i == 0) {
  47909. /* Default uses 96-bits IV length */
  47910. #ifdef WOLFSSL_AES_128
  47911. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  47912. key, iv));
  47913. #elif defined(WOLFSSL_AES_192)
  47914. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  47915. key, iv));
  47916. #elif defined(WOLFSSL_AES_256)
  47917. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  47918. key, iv));
  47919. #endif
  47920. }
  47921. else {
  47922. #ifdef WOLFSSL_AES_128
  47923. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  47924. NULL, NULL));
  47925. #elif defined(WOLFSSL_AES_192)
  47926. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  47927. NULL, NULL));
  47928. #elif defined(WOLFSSL_AES_256)
  47929. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  47930. NULL, NULL));
  47931. #endif
  47932. /* non-default must to set the IV length first */
  47933. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  47934. ivSz, NULL));
  47935. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  47936. }
  47937. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  47938. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  47939. ciphertxtSz));
  47940. decryptedtxtSz = len;
  47941. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  47942. AES_BLOCK_SIZE, tag));
  47943. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  47944. decryptedtxtSz += len;
  47945. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  47946. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  47947. /* modify tag*/
  47948. tag[AES_BLOCK_SIZE-1]+=0xBB;
  47949. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  47950. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  47951. AES_BLOCK_SIZE, tag));
  47952. /* fail due to wrong tag */
  47953. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  47954. ciphertxtSz));
  47955. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  47956. ExpectIntEQ(0, len);
  47957. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  47958. }
  47959. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  47960. return EXPECT_RESULT();
  47961. }
  47962. static int test_wolfssl_EVP_aria_gcm(void)
  47963. {
  47964. int res = TEST_SKIPPED;
  47965. #if defined(OPENSSL_EXTRA) && defined(HAVE_ARIA) && \
  47966. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  47967. /* A 256 bit key, AES_128 will use the first 128 bit*/
  47968. byte *key = (byte*)"01234567890123456789012345678901";
  47969. /* A 128 bit IV */
  47970. byte *iv = (byte*)"0123456789012345";
  47971. int ivSz = ARIA_BLOCK_SIZE;
  47972. /* Message to be encrypted */
  47973. const int plaintxtSz = 40;
  47974. byte plaintxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  47975. XMEMCPY(plaintxt,"for things to change you have to change",plaintxtSz);
  47976. /* Additional non-confidential data */
  47977. byte *aad = (byte*)"Don't spend major time on minor things.";
  47978. unsigned char tag[ARIA_BLOCK_SIZE] = {0};
  47979. int aadSz = (int)XSTRLEN((char*)aad);
  47980. byte ciphertxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  47981. byte decryptedtxt[plaintxtSz];
  47982. int ciphertxtSz = 0;
  47983. int decryptedtxtSz = 0;
  47984. int len = 0;
  47985. int i = 0;
  47986. #define TEST_ARIA_GCM_COUNT 6
  47987. EVP_CIPHER_CTX en[TEST_ARIA_GCM_COUNT];
  47988. EVP_CIPHER_CTX de[TEST_ARIA_GCM_COUNT];
  47989. for (i = 0; i < TEST_ARIA_GCM_COUNT; i++) {
  47990. EVP_CIPHER_CTX_init(&en[i]);
  47991. switch (i) {
  47992. case 0:
  47993. /* Default uses 96-bits IV length */
  47994. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, key, iv));
  47995. break;
  47996. case 1:
  47997. /* Default uses 96-bits IV length */
  47998. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, key, iv));
  47999. break;
  48000. case 2:
  48001. /* Default uses 96-bits IV length */
  48002. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, key, iv));
  48003. break;
  48004. case 3:
  48005. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  48006. /* non-default must to set the IV length first */
  48007. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48008. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  48009. break;
  48010. case 4:
  48011. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  48012. /* non-default must to set the IV length first */
  48013. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48014. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  48015. break;
  48016. case 5:
  48017. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  48018. /* non-default must to set the IV length first */
  48019. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48020. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  48021. break;
  48022. }
  48023. XMEMSET(ciphertxt,0,sizeof(ciphertxt));
  48024. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  48025. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  48026. ciphertxtSz = len;
  48027. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  48028. AssertIntNE(0, XMEMCMP(plaintxt, ciphertxt, plaintxtSz));
  48029. ciphertxtSz += len;
  48030. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, ARIA_BLOCK_SIZE, tag));
  48031. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  48032. EVP_CIPHER_CTX_init(&de[i]);
  48033. switch (i) {
  48034. case 0:
  48035. /* Default uses 96-bits IV length */
  48036. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, key, iv));
  48037. break;
  48038. case 1:
  48039. /* Default uses 96-bits IV length */
  48040. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, key, iv));
  48041. break;
  48042. case 2:
  48043. /* Default uses 96-bits IV length */
  48044. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, key, iv));
  48045. break;
  48046. case 3:
  48047. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  48048. /* non-default must to set the IV length first */
  48049. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48050. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48051. break;
  48052. case 4:
  48053. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  48054. /* non-default must to set the IV length first */
  48055. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48056. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48057. break;
  48058. case 5:
  48059. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  48060. /* non-default must to set the IV length first */
  48061. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48062. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48063. break;
  48064. }
  48065. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  48066. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48067. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  48068. decryptedtxtSz = len;
  48069. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  48070. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48071. decryptedtxtSz += len;
  48072. AssertIntEQ(plaintxtSz, decryptedtxtSz);
  48073. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  48074. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  48075. /* modify tag*/
  48076. tag[AES_BLOCK_SIZE-1]+=0xBB;
  48077. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48078. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  48079. /* fail due to wrong tag */
  48080. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  48081. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48082. AssertIntEQ(0, len);
  48083. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  48084. }
  48085. res = TEST_RES_CHECK(1);
  48086. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  48087. return res;
  48088. }
  48089. static int test_wolfssl_EVP_aes_ccm_zeroLen(void)
  48090. {
  48091. EXPECT_DECLS;
  48092. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  48093. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48094. /* Zero length plain text */
  48095. byte key[] = {
  48096. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48097. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48098. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48099. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48100. }; /* align */
  48101. byte iv[] = {
  48102. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48103. }; /* align */
  48104. byte plaintxt[1];
  48105. int ivSz = 12;
  48106. int plaintxtSz = 0;
  48107. unsigned char tag[16];
  48108. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  48109. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  48110. int ciphertxtSz = 0;
  48111. int decryptedtxtSz = 0;
  48112. int len = 0;
  48113. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  48114. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  48115. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_ccm(), NULL, key, iv));
  48116. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  48117. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  48118. plaintxtSz));
  48119. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  48120. ciphertxtSz += len;
  48121. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  48122. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  48123. ExpectIntEQ(0, ciphertxtSz);
  48124. EVP_CIPHER_CTX_init(de);
  48125. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_ccm(), NULL, key, iv));
  48126. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  48127. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  48128. decryptedtxtSz = len;
  48129. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  48130. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  48131. decryptedtxtSz += len;
  48132. ExpectIntEQ(0, decryptedtxtSz);
  48133. EVP_CIPHER_CTX_free(en);
  48134. EVP_CIPHER_CTX_free(de);
  48135. #endif
  48136. return EXPECT_RESULT();
  48137. }
  48138. static int test_wolfssl_EVP_aes_ccm(void)
  48139. {
  48140. EXPECT_DECLS;
  48141. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  48142. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48143. /* A 256 bit key, AES_128 will use the first 128 bit*/
  48144. byte *key = (byte*)"01234567890123456789012345678901";
  48145. /* A 128 bit IV */
  48146. byte *iv = (byte*)"0123456789012";
  48147. int ivSz = (int)XSTRLEN((char*)iv);
  48148. /* Message to be encrypted */
  48149. byte *plaintxt = (byte*)"for things to change you have to change";
  48150. /* Additional non-confidential data */
  48151. byte *aad = (byte*)"Don't spend major time on minor things.";
  48152. unsigned char tag[AES_BLOCK_SIZE] = {0};
  48153. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  48154. int aadSz = (int)XSTRLEN((char*)aad);
  48155. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  48156. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  48157. int ciphertxtSz = 0;
  48158. int decryptedtxtSz = 0;
  48159. int len = 0;
  48160. int i = 0;
  48161. int ret;
  48162. EVP_CIPHER_CTX en[2];
  48163. EVP_CIPHER_CTX de[2];
  48164. for (i = 0; i < 2; i++) {
  48165. EVP_CIPHER_CTX_init(&en[i]);
  48166. if (i == 0) {
  48167. /* Default uses 96-bits IV length */
  48168. #ifdef WOLFSSL_AES_128
  48169. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  48170. key, iv));
  48171. #elif defined(WOLFSSL_AES_192)
  48172. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  48173. key, iv));
  48174. #elif defined(WOLFSSL_AES_256)
  48175. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  48176. key, iv));
  48177. #endif
  48178. }
  48179. else {
  48180. #ifdef WOLFSSL_AES_128
  48181. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  48182. NULL, NULL));
  48183. #elif defined(WOLFSSL_AES_192)
  48184. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  48185. NULL, NULL));
  48186. #elif defined(WOLFSSL_AES_256)
  48187. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  48188. NULL, NULL));
  48189. #endif
  48190. /* non-default must to set the IV length first */
  48191. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  48192. ivSz, NULL));
  48193. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  48194. }
  48195. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  48196. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  48197. plaintxtSz));
  48198. ciphertxtSz = len;
  48199. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  48200. ciphertxtSz += len;
  48201. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  48202. AES_BLOCK_SIZE, tag));
  48203. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]);
  48204. ExpectIntEQ(ret, 1);
  48205. EVP_CIPHER_CTX_init(&de[i]);
  48206. if (i == 0) {
  48207. /* Default uses 96-bits IV length */
  48208. #ifdef WOLFSSL_AES_128
  48209. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  48210. key, iv));
  48211. #elif defined(WOLFSSL_AES_192)
  48212. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  48213. key, iv));
  48214. #elif defined(WOLFSSL_AES_256)
  48215. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  48216. key, iv));
  48217. #endif
  48218. }
  48219. else {
  48220. #ifdef WOLFSSL_AES_128
  48221. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  48222. NULL, NULL));
  48223. #elif defined(WOLFSSL_AES_192)
  48224. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  48225. NULL, NULL));
  48226. #elif defined(WOLFSSL_AES_256)
  48227. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  48228. NULL, NULL));
  48229. #endif
  48230. /* non-default must to set the IV length first */
  48231. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  48232. ivSz, NULL));
  48233. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48234. }
  48235. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48236. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  48237. ciphertxtSz));
  48238. decryptedtxtSz = len;
  48239. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  48240. AES_BLOCK_SIZE, tag));
  48241. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48242. decryptedtxtSz += len;
  48243. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  48244. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  48245. /* modify tag*/
  48246. tag[AES_BLOCK_SIZE-1]+=0xBB;
  48247. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48248. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  48249. AES_BLOCK_SIZE, tag));
  48250. /* fail due to wrong tag */
  48251. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  48252. ciphertxtSz));
  48253. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48254. ExpectIntEQ(0, len);
  48255. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]);
  48256. ExpectIntEQ(ret, 1);
  48257. }
  48258. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESCCM */
  48259. return EXPECT_RESULT();
  48260. }
  48261. static int test_wolfssl_EVP_chacha20_poly1305(void)
  48262. {
  48263. EXPECT_DECLS;
  48264. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  48265. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  48266. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  48267. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  48268. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  48269. byte cipherText[sizeof(plainText)];
  48270. byte decryptedText[sizeof(plainText)];
  48271. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  48272. EVP_CIPHER_CTX* ctx = NULL;
  48273. int outSz;
  48274. /* Encrypt. */
  48275. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48276. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  48277. NULL), WOLFSSL_SUCCESS);
  48278. /* Invalid IV length. */
  48279. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  48280. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  48281. /* Valid IV length. */
  48282. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  48283. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  48284. /* Invalid tag length. */
  48285. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  48286. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  48287. /* Valid tag length. */
  48288. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  48289. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  48290. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48291. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  48292. WOLFSSL_SUCCESS);
  48293. ExpectIntEQ(outSz, sizeof(aad));
  48294. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  48295. sizeof(plainText)), WOLFSSL_SUCCESS);
  48296. ExpectIntEQ(outSz, sizeof(plainText));
  48297. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  48298. ExpectIntEQ(outSz, 0);
  48299. /* Invalid tag length. */
  48300. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  48301. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  48302. /* Valid tag length. */
  48303. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  48304. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  48305. EVP_CIPHER_CTX_free(ctx);
  48306. ctx = NULL;
  48307. /* Decrypt. */
  48308. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48309. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  48310. NULL), WOLFSSL_SUCCESS);
  48311. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  48312. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  48313. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  48314. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  48315. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48316. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  48317. WOLFSSL_SUCCESS);
  48318. ExpectIntEQ(outSz, sizeof(aad));
  48319. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48320. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48321. ExpectIntEQ(outSz, sizeof(cipherText));
  48322. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  48323. WOLFSSL_SUCCESS);
  48324. ExpectIntEQ(outSz, 0);
  48325. EVP_CIPHER_CTX_free(ctx);
  48326. ctx = NULL;
  48327. /* Test partial Inits. CipherInit() allow setting of key and iv
  48328. * in separate calls. */
  48329. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48330. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  48331. key, NULL, 1), WOLFSSL_SUCCESS);
  48332. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  48333. WOLFSSL_SUCCESS);
  48334. ExpectIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  48335. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  48336. ExpectIntEQ(outSz, sizeof(aad));
  48337. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48338. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48339. ExpectIntEQ(outSz, sizeof(cipherText));
  48340. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  48341. WOLFSSL_SUCCESS);
  48342. ExpectIntEQ(outSz, 0);
  48343. EVP_CIPHER_CTX_free(ctx);
  48344. #endif
  48345. return EXPECT_RESULT();
  48346. }
  48347. static int test_wolfssl_EVP_chacha20(void)
  48348. {
  48349. EXPECT_DECLS;
  48350. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  48351. byte key[CHACHA_MAX_KEY_SZ];
  48352. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  48353. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  48354. byte cipherText[sizeof(plainText)];
  48355. byte decryptedText[sizeof(plainText)];
  48356. EVP_CIPHER_CTX* ctx = NULL;
  48357. int outSz;
  48358. XMEMSET(key, 0, sizeof(key));
  48359. XMEMSET(iv, 0, sizeof(iv));
  48360. /* Encrypt. */
  48361. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48362. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  48363. NULL), WOLFSSL_SUCCESS);
  48364. /* Any tag length must fail - not an AEAD cipher. */
  48365. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  48366. 16, NULL), WOLFSSL_FAILURE);
  48367. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48368. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  48369. sizeof(plainText)), WOLFSSL_SUCCESS);
  48370. ExpectIntEQ(outSz, sizeof(plainText));
  48371. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  48372. ExpectIntEQ(outSz, 0);
  48373. EVP_CIPHER_CTX_free(ctx);
  48374. ctx = NULL;
  48375. /* Decrypt. */
  48376. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48377. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  48378. NULL), WOLFSSL_SUCCESS);
  48379. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48380. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48381. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48382. ExpectIntEQ(outSz, sizeof(cipherText));
  48383. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  48384. WOLFSSL_SUCCESS);
  48385. ExpectIntEQ(outSz, 0);
  48386. EVP_CIPHER_CTX_free(ctx);
  48387. ctx = NULL;
  48388. /* Test partial Inits. CipherInit() allow setting of key and iv
  48389. * in separate calls. */
  48390. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48391. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  48392. key, NULL, 1), WOLFSSL_SUCCESS);
  48393. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  48394. WOLFSSL_SUCCESS);
  48395. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48396. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48397. ExpectIntEQ(outSz, sizeof(cipherText));
  48398. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  48399. WOLFSSL_SUCCESS);
  48400. ExpectIntEQ(outSz, 0);
  48401. EVP_CIPHER_CTX_free(ctx);
  48402. #endif
  48403. return EXPECT_RESULT();
  48404. }
  48405. static int test_wolfssl_EVP_sm4_ecb(void)
  48406. {
  48407. int res = TEST_SKIPPED;
  48408. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_ECB)
  48409. EXPECT_DECLS;
  48410. byte key[SM4_KEY_SIZE];
  48411. byte plainText[SM4_BLOCK_SIZE] = {
  48412. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  48413. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  48414. };
  48415. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  48416. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  48417. EVP_CIPHER_CTX* ctx;
  48418. int outSz;
  48419. XMEMSET(key, 0, sizeof(key));
  48420. /* Encrypt. */
  48421. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48422. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  48423. WOLFSSL_SUCCESS);
  48424. /* Any tag length must fail - not an AEAD cipher. */
  48425. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  48426. WOLFSSL_FAILURE);
  48427. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL),
  48428. WOLFSSL_SUCCESS);
  48429. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  48430. sizeof(plainText)), WOLFSSL_SUCCESS);
  48431. ExpectIntEQ(outSz, sizeof(plainText));
  48432. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  48433. WOLFSSL_SUCCESS);
  48434. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  48435. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  48436. EVP_CIPHER_CTX_free(ctx);
  48437. /* Decrypt. */
  48438. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48439. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  48440. WOLFSSL_SUCCESS);
  48441. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, NULL),
  48442. WOLFSSL_SUCCESS);
  48443. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48444. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48445. ExpectIntEQ(outSz, sizeof(plainText));
  48446. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  48447. WOLFSSL_SUCCESS);
  48448. ExpectIntEQ(outSz, 0);
  48449. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  48450. EVP_CIPHER_CTX_free(ctx);
  48451. res = EXPECT_RESULT();
  48452. #endif
  48453. return res;
  48454. }
  48455. static int test_wolfssl_EVP_sm4_cbc(void)
  48456. {
  48457. int res = TEST_SKIPPED;
  48458. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CBC)
  48459. EXPECT_DECLS;
  48460. byte key[SM4_KEY_SIZE];
  48461. byte iv[SM4_BLOCK_SIZE];
  48462. byte plainText[SM4_BLOCK_SIZE] = {
  48463. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  48464. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  48465. };
  48466. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  48467. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  48468. EVP_CIPHER_CTX* ctx;
  48469. int outSz;
  48470. XMEMSET(key, 0, sizeof(key));
  48471. XMEMSET(iv, 0, sizeof(iv));
  48472. /* Encrypt. */
  48473. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48474. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  48475. WOLFSSL_SUCCESS);
  48476. /* Any tag length must fail - not an AEAD cipher. */
  48477. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  48478. WOLFSSL_FAILURE);
  48479. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48480. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  48481. sizeof(plainText)), WOLFSSL_SUCCESS);
  48482. ExpectIntEQ(outSz, sizeof(plainText));
  48483. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  48484. WOLFSSL_SUCCESS);
  48485. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  48486. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  48487. EVP_CIPHER_CTX_free(ctx);
  48488. /* Decrypt. */
  48489. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48490. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  48491. WOLFSSL_SUCCESS);
  48492. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48493. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48494. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48495. ExpectIntEQ(outSz, sizeof(plainText));
  48496. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  48497. WOLFSSL_SUCCESS);
  48498. ExpectIntEQ(outSz, 0);
  48499. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  48500. EVP_CIPHER_CTX_free(ctx);
  48501. /* Test partial Inits. CipherInit() allow setting of key and iv
  48502. * in separate calls. */
  48503. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48504. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_cbc(), key, NULL, 0),
  48505. WOLFSSL_SUCCESS);
  48506. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 0),
  48507. WOLFSSL_SUCCESS);
  48508. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48509. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48510. ExpectIntEQ(outSz, sizeof(plainText));
  48511. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  48512. WOLFSSL_SUCCESS);
  48513. ExpectIntEQ(outSz, 0);
  48514. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  48515. EVP_CIPHER_CTX_free(ctx);
  48516. res = EXPECT_RESULT();
  48517. #endif
  48518. return res;
  48519. }
  48520. static int test_wolfssl_EVP_sm4_ctr(void)
  48521. {
  48522. int res = TEST_SKIPPED;
  48523. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CTR)
  48524. EXPECT_DECLS;
  48525. byte key[SM4_KEY_SIZE];
  48526. byte iv[SM4_BLOCK_SIZE];
  48527. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  48528. byte cipherText[sizeof(plainText)];
  48529. byte decryptedText[sizeof(plainText)];
  48530. EVP_CIPHER_CTX* ctx;
  48531. int outSz;
  48532. XMEMSET(key, 0, sizeof(key));
  48533. XMEMSET(iv, 0, sizeof(iv));
  48534. /* Encrypt. */
  48535. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48536. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  48537. WOLFSSL_SUCCESS);
  48538. /* Any tag length must fail - not an AEAD cipher. */
  48539. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  48540. WOLFSSL_FAILURE);
  48541. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48542. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  48543. sizeof(plainText)), WOLFSSL_SUCCESS);
  48544. ExpectIntEQ(outSz, sizeof(plainText));
  48545. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  48546. ExpectIntEQ(outSz, 0);
  48547. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  48548. EVP_CIPHER_CTX_free(ctx);
  48549. /* Decrypt. */
  48550. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48551. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  48552. WOLFSSL_SUCCESS);
  48553. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48554. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48555. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48556. ExpectIntEQ(outSz, sizeof(cipherText));
  48557. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  48558. WOLFSSL_SUCCESS);
  48559. ExpectIntEQ(outSz, 0);
  48560. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  48561. EVP_CIPHER_CTX_free(ctx);
  48562. /* Test partial Inits. CipherInit() allow setting of key and iv
  48563. * in separate calls. */
  48564. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48565. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_ctr(), key, NULL, 1),
  48566. WOLFSSL_SUCCESS);
  48567. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  48568. WOLFSSL_SUCCESS);
  48569. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48570. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48571. ExpectIntEQ(outSz, sizeof(cipherText));
  48572. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  48573. WOLFSSL_SUCCESS);
  48574. ExpectIntEQ(outSz, 0);
  48575. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  48576. EVP_CIPHER_CTX_free(ctx);
  48577. res = EXPECT_RESULT();
  48578. #endif
  48579. return res;
  48580. }
  48581. static int test_wolfssl_EVP_sm4_gcm_zeroLen(void)
  48582. {
  48583. int res = TEST_SKIPPED;
  48584. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  48585. /* Zero length plain text */
  48586. EXPECT_DECLS;
  48587. byte key[] = {
  48588. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48589. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48590. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48591. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48592. }; /* align */
  48593. byte iv[] = {
  48594. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48595. }; /* align */
  48596. byte plaintxt[1];
  48597. int ivSz = 12;
  48598. int plaintxtSz = 0;
  48599. unsigned char tag[16];
  48600. unsigned char tag_kat[16] = {
  48601. 0x23,0x2f,0x0c,0xfe,0x30,0x8b,0x49,0xea,
  48602. 0x6f,0xc8,0x82,0x29,0xb5,0xdc,0x85,0x8d
  48603. };
  48604. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  48605. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  48606. int ciphertxtSz = 0;
  48607. int decryptedtxtSz = 0;
  48608. int len = 0;
  48609. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  48610. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  48611. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_gcm(), NULL, key, iv));
  48612. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48613. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  48614. plaintxtSz));
  48615. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  48616. ciphertxtSz += len;
  48617. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  48618. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  48619. ExpectIntEQ(0, ciphertxtSz);
  48620. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  48621. EVP_CIPHER_CTX_init(de);
  48622. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_gcm(), NULL, key, iv));
  48623. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48624. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  48625. decryptedtxtSz = len;
  48626. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  48627. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  48628. decryptedtxtSz += len;
  48629. ExpectIntEQ(0, decryptedtxtSz);
  48630. EVP_CIPHER_CTX_free(en);
  48631. EVP_CIPHER_CTX_free(de);
  48632. res = EXPECT_RESULT();
  48633. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  48634. return res;
  48635. }
  48636. static int test_wolfssl_EVP_sm4_gcm(void)
  48637. {
  48638. int res = TEST_SKIPPED;
  48639. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  48640. EXPECT_DECLS;
  48641. byte *key = (byte*)"0123456789012345";
  48642. /* A 128 bit IV */
  48643. byte *iv = (byte*)"0123456789012345";
  48644. int ivSz = SM4_BLOCK_SIZE;
  48645. /* Message to be encrypted */
  48646. byte *plaintxt = (byte*)"for things to change you have to change";
  48647. /* Additional non-confidential data */
  48648. byte *aad = (byte*)"Don't spend major time on minor things.";
  48649. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  48650. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  48651. int aadSz = (int)XSTRLEN((char*)aad);
  48652. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  48653. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  48654. int ciphertxtSz = 0;
  48655. int decryptedtxtSz = 0;
  48656. int len = 0;
  48657. int i = 0;
  48658. EVP_CIPHER_CTX en[2];
  48659. EVP_CIPHER_CTX de[2];
  48660. for (i = 0; i < 2; i++) {
  48661. EVP_CIPHER_CTX_init(&en[i]);
  48662. if (i == 0) {
  48663. /* Default uses 96-bits IV length */
  48664. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, key,
  48665. iv));
  48666. }
  48667. else {
  48668. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, NULL,
  48669. NULL));
  48670. /* non-default must to set the IV length first */
  48671. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  48672. ivSz, NULL));
  48673. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  48674. }
  48675. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  48676. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  48677. plaintxtSz));
  48678. ciphertxtSz = len;
  48679. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  48680. ciphertxtSz += len;
  48681. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  48682. SM4_BLOCK_SIZE, tag));
  48683. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  48684. EVP_CIPHER_CTX_init(&de[i]);
  48685. if (i == 0) {
  48686. /* Default uses 96-bits IV length */
  48687. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, key,
  48688. iv));
  48689. }
  48690. else {
  48691. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, NULL,
  48692. NULL));
  48693. /* non-default must to set the IV length first */
  48694. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  48695. ivSz, NULL));
  48696. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48697. }
  48698. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48699. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  48700. ciphertxtSz));
  48701. decryptedtxtSz = len;
  48702. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  48703. SM4_BLOCK_SIZE, tag));
  48704. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48705. decryptedtxtSz += len;
  48706. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  48707. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  48708. /* modify tag*/
  48709. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  48710. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48711. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  48712. SM4_BLOCK_SIZE, tag));
  48713. /* fail due to wrong tag */
  48714. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  48715. ciphertxtSz));
  48716. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48717. ExpectIntEQ(0, len);
  48718. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  48719. }
  48720. res = EXPECT_RESULT();
  48721. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  48722. return res;
  48723. }
  48724. static int test_wolfssl_EVP_sm4_ccm_zeroLen(void)
  48725. {
  48726. int res = TEST_SKIPPED;
  48727. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  48728. /* Zero length plain text */
  48729. EXPECT_DECLS;
  48730. byte key[] = {
  48731. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48732. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48733. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48734. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48735. }; /* align */
  48736. byte iv[] = {
  48737. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48738. }; /* align */
  48739. byte plaintxt[1];
  48740. int ivSz = 12;
  48741. int plaintxtSz = 0;
  48742. unsigned char tag[16];
  48743. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  48744. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  48745. int ciphertxtSz = 0;
  48746. int decryptedtxtSz = 0;
  48747. int len = 0;
  48748. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  48749. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  48750. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_ccm(), NULL, key, iv));
  48751. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  48752. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  48753. plaintxtSz));
  48754. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  48755. ciphertxtSz += len;
  48756. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  48757. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  48758. ExpectIntEQ(0, ciphertxtSz);
  48759. EVP_CIPHER_CTX_init(de);
  48760. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_ccm(), NULL, key, iv));
  48761. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  48762. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  48763. decryptedtxtSz = len;
  48764. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  48765. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  48766. decryptedtxtSz += len;
  48767. ExpectIntEQ(0, decryptedtxtSz);
  48768. EVP_CIPHER_CTX_free(en);
  48769. EVP_CIPHER_CTX_free(de);
  48770. res = EXPECT_RESULT();
  48771. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  48772. return res;
  48773. }
  48774. static int test_wolfssl_EVP_sm4_ccm(void)
  48775. {
  48776. int res = TEST_SKIPPED;
  48777. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  48778. EXPECT_DECLS;
  48779. byte *key = (byte*)"0123456789012345";
  48780. byte *iv = (byte*)"0123456789012";
  48781. int ivSz = (int)XSTRLEN((char*)iv);
  48782. /* Message to be encrypted */
  48783. byte *plaintxt = (byte*)"for things to change you have to change";
  48784. /* Additional non-confidential data */
  48785. byte *aad = (byte*)"Don't spend major time on minor things.";
  48786. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  48787. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  48788. int aadSz = (int)XSTRLEN((char*)aad);
  48789. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  48790. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  48791. int ciphertxtSz = 0;
  48792. int decryptedtxtSz = 0;
  48793. int len = 0;
  48794. int i = 0;
  48795. EVP_CIPHER_CTX en[2];
  48796. EVP_CIPHER_CTX de[2];
  48797. for (i = 0; i < 2; i++) {
  48798. EVP_CIPHER_CTX_init(&en[i]);
  48799. if (i == 0) {
  48800. /* Default uses 96-bits IV length */
  48801. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, key,
  48802. iv));
  48803. }
  48804. else {
  48805. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, NULL,
  48806. NULL));
  48807. /* non-default must to set the IV length first */
  48808. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  48809. ivSz, NULL));
  48810. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  48811. }
  48812. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  48813. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  48814. plaintxtSz));
  48815. ciphertxtSz = len;
  48816. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  48817. ciphertxtSz += len;
  48818. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  48819. SM4_BLOCK_SIZE, tag));
  48820. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  48821. EVP_CIPHER_CTX_init(&de[i]);
  48822. if (i == 0) {
  48823. /* Default uses 96-bits IV length */
  48824. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, key,
  48825. iv));
  48826. }
  48827. else {
  48828. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, NULL,
  48829. NULL));
  48830. /* non-default must to set the IV length first */
  48831. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  48832. ivSz, NULL));
  48833. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48834. }
  48835. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48836. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  48837. ciphertxtSz));
  48838. decryptedtxtSz = len;
  48839. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  48840. SM4_BLOCK_SIZE, tag));
  48841. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48842. decryptedtxtSz += len;
  48843. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  48844. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  48845. /* modify tag*/
  48846. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  48847. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48848. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  48849. SM4_BLOCK_SIZE, tag));
  48850. /* fail due to wrong tag */
  48851. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  48852. ciphertxtSz));
  48853. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48854. ExpectIntEQ(0, len);
  48855. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  48856. }
  48857. res = EXPECT_RESULT();
  48858. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  48859. return res;
  48860. }
  48861. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  48862. {
  48863. EXPECT_DECLS;
  48864. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  48865. EVP_PKEY_CTX* ctx = NULL;
  48866. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  48867. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  48868. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  48869. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  48870. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  48871. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  48872. byte outKey[34];
  48873. size_t outKeySz = sizeof(outKey);
  48874. /* These expected outputs were gathered by running the same test below using
  48875. * OpenSSL. */
  48876. const byte extractAndExpand[] = {
  48877. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  48878. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  48879. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  48880. };
  48881. const byte extractOnly[] = {
  48882. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  48883. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  48884. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  48885. };
  48886. const byte expandOnly[] = {
  48887. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  48888. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  48889. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  48890. };
  48891. const byte extractAndExpandAddInfo[] = {
  48892. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  48893. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  48894. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  48895. };
  48896. ExpectNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  48897. ExpectIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  48898. /* NULL ctx. */
  48899. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  48900. /* NULL md. */
  48901. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  48902. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  48903. /* NULL ctx. */
  48904. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  48905. WOLFSSL_FAILURE);
  48906. /* NULL salt is ok. */
  48907. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  48908. WOLFSSL_SUCCESS);
  48909. /* Salt length <= 0. */
  48910. /* Length 0 salt is ok. */
  48911. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  48912. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  48913. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  48914. WOLFSSL_SUCCESS);
  48915. /* NULL ctx. */
  48916. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  48917. WOLFSSL_FAILURE);
  48918. /* NULL key. */
  48919. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  48920. WOLFSSL_FAILURE);
  48921. /* Key length <= 0 */
  48922. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  48923. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  48924. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  48925. WOLFSSL_SUCCESS);
  48926. /* NULL ctx. */
  48927. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  48928. WOLFSSL_FAILURE);
  48929. /* NULL info is ok. */
  48930. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  48931. WOLFSSL_SUCCESS);
  48932. /* Info length <= 0 */
  48933. /* Length 0 info is ok. */
  48934. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  48935. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  48936. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  48937. WOLFSSL_SUCCESS);
  48938. /* NULL ctx. */
  48939. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  48940. WOLFSSL_FAILURE);
  48941. /* Extract and expand (default). */
  48942. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  48943. ExpectIntEQ(outKeySz, sizeof(extractAndExpand));
  48944. ExpectIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  48945. /* Extract only. */
  48946. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  48947. WOLFSSL_SUCCESS);
  48948. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  48949. ExpectIntEQ(outKeySz, sizeof(extractOnly));
  48950. ExpectIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  48951. outKeySz = sizeof(outKey);
  48952. /* Expand only. */
  48953. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  48954. WOLFSSL_SUCCESS);
  48955. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  48956. ExpectIntEQ(outKeySz, sizeof(expandOnly));
  48957. ExpectIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  48958. outKeySz = sizeof(outKey);
  48959. /* Extract and expand with appended additional info. */
  48960. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  48961. WOLFSSL_SUCCESS);
  48962. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  48963. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  48964. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  48965. ExpectIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  48966. ExpectIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  48967. EVP_PKEY_CTX_free(ctx);
  48968. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  48969. return EXPECT_RESULT();
  48970. }
  48971. #ifndef NO_BIO
  48972. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  48973. {
  48974. EXPECT_DECLS;
  48975. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  48976. BIO* bio = NULL;
  48977. X509_INFO* info = NULL;
  48978. STACK_OF(X509_INFO)* sk = NULL;
  48979. char* subject = NULL;
  48980. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/"
  48981. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  48982. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/"
  48983. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  48984. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  48985. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  48986. ExpectNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  48987. ExpectIntEQ(sk_X509_INFO_num(sk), 2);
  48988. /* using dereference to maintain testing for Apache port*/
  48989. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  48990. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  48991. 0, 0));
  48992. ExpectIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  48993. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  48994. X509_INFO_free(info);
  48995. info = NULL;
  48996. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  48997. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  48998. 0, 0));
  48999. ExpectIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  49000. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  49001. X509_INFO_free(info);
  49002. ExpectNull(info = sk_X509_INFO_pop(sk));
  49003. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  49004. BIO_free(bio);
  49005. #endif
  49006. return EXPECT_RESULT();
  49007. }
  49008. #endif /* !NO_BIO */
  49009. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  49010. {
  49011. EXPECT_DECLS;
  49012. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  49013. X509 *x509 = NULL;
  49014. X509_NAME* name = NULL;
  49015. int idx = 0;
  49016. X509_NAME_ENTRY *ne = NULL;
  49017. ASN1_OBJECT *object = NULL;
  49018. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  49019. WOLFSSL_FILETYPE_PEM));
  49020. ExpectNotNull(name = X509_get_subject_name(x509));
  49021. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  49022. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  49023. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  49024. X509_free(x509);
  49025. #endif
  49026. return EXPECT_RESULT();
  49027. }
  49028. static int test_wolfSSL_X509_STORE_get1_certs(void)
  49029. {
  49030. EXPECT_DECLS;
  49031. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  49032. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  49033. X509_STORE_CTX *storeCtx = NULL;
  49034. X509_STORE *store = NULL;
  49035. X509 *caX509 = NULL;
  49036. X509 *svrX509 = NULL;
  49037. X509_NAME *subject = NULL;
  49038. WOLF_STACK_OF(WOLFSSL_X509) *certs = NULL;
  49039. ExpectNotNull(caX509 = X509_load_certificate_file(caCertFile,
  49040. SSL_FILETYPE_PEM));
  49041. ExpectNotNull((svrX509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  49042. SSL_FILETYPE_PEM)));
  49043. ExpectNotNull(storeCtx = X509_STORE_CTX_new());
  49044. ExpectNotNull(store = X509_STORE_new());
  49045. ExpectNotNull(subject = X509_get_subject_name(caX509));
  49046. /* Errors */
  49047. ExpectNull(X509_STORE_get1_certs(storeCtx, subject));
  49048. ExpectNull(X509_STORE_get1_certs(NULL, subject));
  49049. ExpectNull(X509_STORE_get1_certs(storeCtx, NULL));
  49050. ExpectIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  49051. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL),
  49052. SSL_SUCCESS);
  49053. /* Should find the cert */
  49054. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  49055. ExpectIntEQ(1, wolfSSL_sk_X509_num(certs));
  49056. sk_X509_pop_free(certs, NULL);
  49057. certs = NULL;
  49058. /* Should not find the cert */
  49059. ExpectNotNull(subject = X509_get_subject_name(svrX509));
  49060. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  49061. ExpectIntEQ(0, wolfSSL_sk_X509_num(certs));
  49062. sk_X509_pop_free(certs, NULL);
  49063. certs = NULL;
  49064. X509_STORE_free(store);
  49065. X509_STORE_CTX_free(storeCtx);
  49066. X509_free(svrX509);
  49067. X509_free(caX509);
  49068. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  49069. return EXPECT_RESULT();
  49070. }
  49071. static int test_wolfSSL_dup_CA_list(void)
  49072. {
  49073. int res = TEST_SKIPPED;
  49074. #if defined(OPENSSL_ALL)
  49075. EXPECT_DECLS;
  49076. STACK_OF(X509_NAME) *originalStack = NULL;
  49077. STACK_OF(X509_NAME) *copyStack = NULL;
  49078. int originalCount = 0;
  49079. int copyCount = 0;
  49080. X509_NAME *name = NULL;
  49081. int i;
  49082. originalStack = sk_X509_NAME_new_null();
  49083. ExpectNotNull(originalStack);
  49084. for (i = 0; i < 3; i++) {
  49085. name = X509_NAME_new();
  49086. ExpectNotNull(name);
  49087. AssertIntEQ(sk_X509_NAME_push(originalStack, name), WOLFSSL_SUCCESS);
  49088. }
  49089. copyStack = SSL_dup_CA_list(originalStack);
  49090. ExpectNotNull(copyStack);
  49091. originalCount = sk_X509_NAME_num(originalStack);
  49092. copyCount = sk_X509_NAME_num(copyStack);
  49093. AssertIntEQ(originalCount, copyCount);
  49094. sk_X509_NAME_pop_free(originalStack, X509_NAME_free);
  49095. sk_X509_NAME_pop_free(copyStack, X509_NAME_free);
  49096. originalStack = NULL;
  49097. copyStack = NULL;
  49098. res = EXPECT_RESULT();
  49099. #endif /* OPENSSL_ALL */
  49100. return res;
  49101. }
  49102. static int test_ForceZero(void)
  49103. {
  49104. EXPECT_DECLS;
  49105. unsigned char data[32];
  49106. unsigned int i, j, len;
  49107. /* Test case with 0 length */
  49108. ForceZero(data, 0);
  49109. /* Test ForceZero */
  49110. for (i = 0; i < sizeof(data); i++) {
  49111. for (len = 1; len < sizeof(data) - i; len++) {
  49112. for (j = 0; j < sizeof(data); j++)
  49113. data[j] = j + 1;
  49114. ForceZero(data + i, len);
  49115. for (j = 0; j < sizeof(data); j++) {
  49116. if (j < i || j >= i + len) {
  49117. ExpectIntNE(data[j], 0x00);
  49118. }
  49119. else {
  49120. ExpectIntEQ(data[j], 0x00);
  49121. }
  49122. }
  49123. }
  49124. }
  49125. return EXPECT_RESULT();
  49126. }
  49127. #ifndef NO_BIO
  49128. static int test_wolfSSL_X509_print(void)
  49129. {
  49130. EXPECT_DECLS;
  49131. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  49132. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(XSNPRINTF)
  49133. X509 *x509 = NULL;
  49134. BIO *bio = NULL;
  49135. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  49136. const X509_ALGOR *cert_sig_alg;
  49137. #endif
  49138. ExpectNotNull(x509 = X509_load_certificate_file(svrCertFile,
  49139. WOLFSSL_FILETYPE_PEM));
  49140. /* print to memory */
  49141. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  49142. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  49143. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  49144. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  49145. /* Will print IP address subject alt name. */
  49146. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  49147. #elif defined(NO_ASN_TIME)
  49148. /* Will print IP address subject alt name but not Validity. */
  49149. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3235);
  49150. #else
  49151. /* Will print IP address subject alt name. */
  49152. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  49153. #endif
  49154. #elif defined(NO_ASN_TIME)
  49155. /* With NO_ASN_TIME defined, X509_print skips printing Validity. */
  49156. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3213);
  49157. #else
  49158. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  49159. #endif
  49160. BIO_free(bio);
  49161. bio = NULL;
  49162. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  49163. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  49164. /* Print signature */
  49165. ExpectNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  49166. ExpectIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  49167. #endif
  49168. /* print to stderr */
  49169. #if !defined(NO_WOLFSSL_DIR)
  49170. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  49171. #endif
  49172. /* print again */
  49173. ExpectIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
  49174. X509_free(x509);
  49175. BIO_free(bio);
  49176. #endif
  49177. return EXPECT_RESULT();
  49178. }
  49179. static int test_wolfSSL_X509_CRL_print(void)
  49180. {
  49181. EXPECT_DECLS;
  49182. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  49183. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  49184. X509_CRL* crl = NULL;
  49185. BIO *bio = NULL;
  49186. XFILE fp = XBADFILE;
  49187. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  49188. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  49189. NULL, NULL));
  49190. if (fp != XBADFILE)
  49191. XFCLOSE(fp);
  49192. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  49193. ExpectIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  49194. X509_CRL_free(crl);
  49195. BIO_free(bio);
  49196. #endif
  49197. return EXPECT_RESULT();
  49198. }
  49199. static int test_wolfSSL_BIO_get_len(void)
  49200. {
  49201. EXPECT_DECLS;
  49202. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  49203. BIO *bio = NULL;
  49204. const char txt[] = "Some example text to push to the BIO.";
  49205. ExpectIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  49206. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  49207. ExpectIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  49208. ExpectIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  49209. BIO_free(bio);
  49210. bio = NULL;
  49211. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  49212. ExpectIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  49213. BIO_free(bio);
  49214. #endif
  49215. return EXPECT_RESULT();
  49216. }
  49217. #endif /* !NO_BIO */
  49218. static int test_wolfSSL_RSA(void)
  49219. {
  49220. EXPECT_DECLS;
  49221. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  49222. defined(WOLFSSL_KEY_GEN)
  49223. RSA* rsa = NULL;
  49224. const BIGNUM *n;
  49225. const BIGNUM *e;
  49226. const BIGNUM *d;
  49227. const BIGNUM *p;
  49228. const BIGNUM *q;
  49229. const BIGNUM *dmp1;
  49230. const BIGNUM *dmq1;
  49231. const BIGNUM *iqmp;
  49232. ExpectNotNull(rsa = RSA_new());
  49233. ExpectIntEQ(RSA_size(NULL), 0);
  49234. ExpectIntEQ(RSA_size(rsa), 0);
  49235. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  49236. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  49237. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  49238. #ifdef WOLFSSL_RSA_KEY_CHECK
  49239. ExpectIntEQ(RSA_check_key(rsa), 0);
  49240. #endif
  49241. RSA_free(rsa);
  49242. rsa = NULL;
  49243. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  49244. ExpectIntEQ(RSA_size(rsa), 256);
  49245. #ifdef WOLFSSL_RSA_KEY_CHECK
  49246. ExpectIntEQ(RSA_check_key(NULL), 0);
  49247. ExpectIntEQ(RSA_check_key(rsa), 1);
  49248. #endif
  49249. /* sanity check */
  49250. ExpectIntEQ(RSA_bits(NULL), 0);
  49251. /* key */
  49252. ExpectIntEQ(RSA_bits(rsa), 2048);
  49253. RSA_get0_key(rsa, &n, &e, &d);
  49254. ExpectPtrEq(rsa->n, n);
  49255. ExpectPtrEq(rsa->e, e);
  49256. ExpectPtrEq(rsa->d, d);
  49257. n = NULL;
  49258. e = NULL;
  49259. d = NULL;
  49260. ExpectNotNull(n = BN_new());
  49261. ExpectNotNull(e = BN_new());
  49262. ExpectNotNull(d = BN_new());
  49263. ExpectIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  49264. if (EXPECT_FAIL()) {
  49265. BN_free((BIGNUM*)n);
  49266. BN_free((BIGNUM*)e);
  49267. BN_free((BIGNUM*)d);
  49268. }
  49269. ExpectPtrEq(rsa->n, n);
  49270. ExpectPtrEq(rsa->e, e);
  49271. ExpectPtrEq(rsa->d, d);
  49272. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  49273. ExpectIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  49274. /* crt_params */
  49275. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  49276. ExpectPtrEq(rsa->dmp1, dmp1);
  49277. ExpectPtrEq(rsa->dmq1, dmq1);
  49278. ExpectPtrEq(rsa->iqmp, iqmp);
  49279. dmp1 = NULL;
  49280. dmq1 = NULL;
  49281. iqmp = NULL;
  49282. ExpectNotNull(dmp1 = BN_new());
  49283. ExpectNotNull(dmq1 = BN_new());
  49284. ExpectNotNull(iqmp = BN_new());
  49285. ExpectIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  49286. (BIGNUM*)iqmp), 1);
  49287. if (EXPECT_FAIL()) {
  49288. BN_free((BIGNUM*)dmp1);
  49289. BN_free((BIGNUM*)dmq1);
  49290. BN_free((BIGNUM*)iqmp);
  49291. }
  49292. ExpectPtrEq(rsa->dmp1, dmp1);
  49293. ExpectPtrEq(rsa->dmq1, dmq1);
  49294. ExpectPtrEq(rsa->iqmp, iqmp);
  49295. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  49296. ExpectIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  49297. (BIGNUM*)iqmp), 0);
  49298. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  49299. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  49300. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  49301. ExpectNull(dmp1);
  49302. ExpectNull(dmq1);
  49303. ExpectNull(iqmp);
  49304. /* factors */
  49305. RSA_get0_factors(rsa, NULL, NULL);
  49306. RSA_get0_factors(rsa, &p, &q);
  49307. ExpectPtrEq(rsa->p, p);
  49308. ExpectPtrEq(rsa->q, q);
  49309. p = NULL;
  49310. q = NULL;
  49311. ExpectNotNull(p = BN_new());
  49312. ExpectNotNull(q = BN_new());
  49313. ExpectIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  49314. if (EXPECT_FAIL()) {
  49315. BN_free((BIGNUM*)p);
  49316. BN_free((BIGNUM*)q);
  49317. }
  49318. ExpectPtrEq(rsa->p, p);
  49319. ExpectPtrEq(rsa->q, q);
  49320. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  49321. ExpectIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  49322. RSA_get0_factors(NULL, NULL, NULL);
  49323. RSA_get0_factors(NULL, &p, &q);
  49324. ExpectNull(p);
  49325. ExpectNull(q);
  49326. ExpectIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  49327. ExpectIntEQ(RSA_bits(rsa), 21);
  49328. RSA_free(rsa);
  49329. rsa = NULL;
  49330. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  49331. ExpectNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  49332. ExpectIntEQ(RSA_size(rsa), 384);
  49333. ExpectIntEQ(RSA_bits(rsa), 3072);
  49334. RSA_free(rsa);
  49335. rsa = NULL;
  49336. #endif
  49337. /* remove for now with odd key size until adjusting rsa key size check with
  49338. wc_MakeRsaKey()
  49339. ExpectNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  49340. RSA_free(rsa);
  49341. rsa = NULL;
  49342. */
  49343. ExpectNull(RSA_generate_key(-1, 3, NULL, NULL));
  49344. ExpectNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  49345. ExpectNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  49346. ExpectNull(RSA_generate_key(2048, 0, NULL, NULL));
  49347. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  49348. {
  49349. byte buff[FOURK_BUF];
  49350. byte der[FOURK_BUF];
  49351. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  49352. XFILE f = XBADFILE;
  49353. int bytes;
  49354. /* test loading encrypted RSA private pem w/o password */
  49355. ExpectTrue((f = XFOPEN(PrivKeyPemFile, "rb")) != XBADFILE);
  49356. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  49357. if (f != XBADFILE)
  49358. XFCLOSE(f);
  49359. XMEMSET(der, 0, sizeof(der));
  49360. /* test that error value is returned with no password */
  49361. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""),
  49362. 0);
  49363. }
  49364. #endif
  49365. #endif
  49366. return EXPECT_RESULT();
  49367. }
  49368. static int test_wolfSSL_RSA_DER(void)
  49369. {
  49370. EXPECT_DECLS;
  49371. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  49372. !defined(NO_RSA) && !defined(HAVE_USER_RSA) && defined(OPENSSL_EXTRA)
  49373. RSA *rsa = NULL;
  49374. int i;
  49375. const unsigned char *buff = NULL;
  49376. unsigned char *newBuff = NULL;
  49377. struct tbl_s
  49378. {
  49379. const unsigned char *der;
  49380. int sz;
  49381. } tbl[] = {
  49382. #ifdef USE_CERT_BUFFERS_1024
  49383. {client_key_der_1024, sizeof_client_key_der_1024},
  49384. {server_key_der_1024, sizeof_server_key_der_1024},
  49385. #endif
  49386. #ifdef USE_CERT_BUFFERS_2048
  49387. {client_key_der_2048, sizeof_client_key_der_2048},
  49388. {server_key_der_2048, sizeof_server_key_der_2048},
  49389. #endif
  49390. {NULL, 0}
  49391. };
  49392. /* Public Key DER */
  49393. struct tbl_s pub[] = {
  49394. #ifdef USE_CERT_BUFFERS_1024
  49395. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  49396. #endif
  49397. #ifdef USE_CERT_BUFFERS_2048
  49398. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  49399. #endif
  49400. {NULL, 0}
  49401. };
  49402. ExpectNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  49403. buff = pub[0].der;
  49404. ExpectNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  49405. ExpectNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  49406. buff = tbl[0].der;
  49407. ExpectNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  49408. ExpectIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  49409. rsa = RSA_new();
  49410. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  49411. RSA_free(rsa);
  49412. rsa = NULL;
  49413. for (i = 0; tbl[i].der != NULL; i++)
  49414. {
  49415. /* Passing in pointer results in pointer moving. */
  49416. buff = tbl[i].der;
  49417. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  49418. ExpectNotNull(rsa);
  49419. RSA_free(rsa);
  49420. rsa = NULL;
  49421. }
  49422. for (i = 0; tbl[i].der != NULL; i++)
  49423. {
  49424. /* Passing in pointer results in pointer moving. */
  49425. buff = tbl[i].der;
  49426. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  49427. ExpectNotNull(rsa);
  49428. RSA_free(rsa);
  49429. rsa = NULL;
  49430. }
  49431. for (i = 0; pub[i].der != NULL; i++)
  49432. {
  49433. buff = pub[i].der;
  49434. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  49435. ExpectNotNull(rsa);
  49436. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  49437. newBuff = NULL;
  49438. ExpectIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  49439. ExpectNotNull(newBuff);
  49440. ExpectIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  49441. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49442. RSA_free(rsa);
  49443. rsa = NULL;
  49444. }
  49445. #endif
  49446. return EXPECT_RESULT();
  49447. }
  49448. static int test_wolfSSL_RSA_print(void)
  49449. {
  49450. EXPECT_DECLS;
  49451. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  49452. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  49453. !defined(HAVE_FAST_RSA) && !defined(NO_BIO) && defined(XFPRINTF)
  49454. BIO *bio = NULL;
  49455. WOLFSSL_RSA* rsa = NULL;
  49456. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  49457. ExpectNotNull(rsa = RSA_new());
  49458. ExpectIntEQ(RSA_print(NULL, rsa, 0), -1);
  49459. ExpectIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  49460. ExpectIntEQ(RSA_print(bio, NULL, 0), -1);
  49461. ExpectIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
  49462. /* Some very large number of indent spaces. */
  49463. ExpectIntEQ(RSA_print(bio, rsa, 128), -1);
  49464. /* RSA is empty. */
  49465. ExpectIntEQ(RSA_print(bio, rsa, 0), 0);
  49466. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
  49467. RSA_free(rsa);
  49468. rsa = NULL;
  49469. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  49470. ExpectIntEQ(RSA_print(bio, rsa, 0), 1);
  49471. ExpectIntEQ(RSA_print(bio, rsa, 4), 1);
  49472. ExpectIntEQ(RSA_print(bio, rsa, -1), 1);
  49473. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
  49474. ExpectIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
  49475. ExpectIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
  49476. BIO_free(bio);
  49477. RSA_free(rsa);
  49478. #endif
  49479. return EXPECT_RESULT();
  49480. }
  49481. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  49482. {
  49483. EXPECT_DECLS;
  49484. #ifndef NO_RSA
  49485. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  49486. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  49487. RSA *rsa = NULL;
  49488. const unsigned char *derBuf = client_key_der_2048;
  49489. unsigned char em[256] = {0}; /* len = 2048/8 */
  49490. /* Random data simulating a hash */
  49491. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  49492. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  49493. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  49494. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  49495. };
  49496. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  49497. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  49498. RSA_PSS_SALTLEN_DIGEST), 0);
  49499. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  49500. RSA_PSS_SALTLEN_DIGEST), 0);
  49501. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  49502. RSA_PSS_SALTLEN_DIGEST), 0);
  49503. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  49504. RSA_PSS_SALTLEN_DIGEST), 0);
  49505. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  49506. ExpectIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  49507. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  49508. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  49509. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  49510. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  49511. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  49512. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  49513. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  49514. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  49515. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  49516. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  49517. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  49518. RSA_PSS_SALTLEN_DIGEST), 1);
  49519. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  49520. RSA_PSS_SALTLEN_DIGEST), 1);
  49521. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  49522. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  49523. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  49524. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  49525. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  49526. RSA_PSS_SALTLEN_MAX), 1);
  49527. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  49528. RSA_PSS_SALTLEN_MAX), 1);
  49529. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  49530. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  49531. RSA_free(rsa);
  49532. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  49533. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  49534. #endif
  49535. return EXPECT_RESULT();
  49536. }
  49537. static int test_wolfSSL_RSA_sign_sha3(void)
  49538. {
  49539. EXPECT_DECLS;
  49540. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  49541. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  49542. RSA* rsa = NULL;
  49543. const unsigned char *derBuf = client_key_der_2048;
  49544. unsigned char sigRet[256] = {0};
  49545. unsigned int sigLen = sizeof(sigRet);
  49546. /* Random data simulating a hash */
  49547. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  49548. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  49549. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  49550. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  49551. };
  49552. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  49553. ExpectIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet, &sigLen,
  49554. rsa), 1);
  49555. RSA_free(rsa);
  49556. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  49557. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  49558. return EXPECT_RESULT();
  49559. }
  49560. static int test_wolfSSL_RSA_get0_key(void)
  49561. {
  49562. EXPECT_DECLS;
  49563. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  49564. RSA *rsa = NULL;
  49565. const BIGNUM* n = NULL;
  49566. const BIGNUM* e = NULL;
  49567. const BIGNUM* d = NULL;
  49568. const unsigned char* der;
  49569. int derSz;
  49570. #ifdef USE_CERT_BUFFERS_1024
  49571. der = client_key_der_1024;
  49572. derSz = sizeof_client_key_der_1024;
  49573. #elif defined(USE_CERT_BUFFERS_2048)
  49574. der = client_key_der_2048;
  49575. derSz = sizeof_client_key_der_2048;
  49576. #else
  49577. der = NULL;
  49578. derSz = 0;
  49579. #endif
  49580. if (der != NULL) {
  49581. RSA_get0_key(NULL, NULL, NULL, NULL);
  49582. RSA_get0_key(rsa, NULL, NULL, NULL);
  49583. RSA_get0_key(NULL, &n, &e, &d);
  49584. ExpectNull(n);
  49585. ExpectNull(e);
  49586. ExpectNull(d);
  49587. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  49588. ExpectNotNull(rsa);
  49589. RSA_get0_key(rsa, NULL, NULL, NULL);
  49590. RSA_get0_key(rsa, &n, NULL, NULL);
  49591. ExpectNotNull(n);
  49592. RSA_get0_key(rsa, NULL, &e, NULL);
  49593. ExpectNotNull(e);
  49594. RSA_get0_key(rsa, NULL, NULL, &d);
  49595. ExpectNotNull(d);
  49596. RSA_get0_key(rsa, &n, &e, &d);
  49597. ExpectNotNull(n);
  49598. ExpectNotNull(e);
  49599. ExpectNotNull(d);
  49600. RSA_free(rsa);
  49601. }
  49602. #endif
  49603. return EXPECT_RESULT();
  49604. }
  49605. static int test_wolfSSL_RSA_meth(void)
  49606. {
  49607. EXPECT_DECLS;
  49608. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  49609. RSA *rsa = NULL;
  49610. RSA_METHOD *rsa_meth = NULL;
  49611. #ifdef WOLFSSL_KEY_GEN
  49612. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  49613. RSA_free(rsa);
  49614. rsa = NULL;
  49615. #else
  49616. ExpectNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  49617. #endif
  49618. ExpectNotNull(RSA_get_default_method());
  49619. wolfSSL_RSA_meth_free(NULL);
  49620. ExpectNull(wolfSSL_RSA_meth_new(NULL, 0));
  49621. ExpectNotNull(rsa_meth = RSA_meth_new("placeholder RSA method",
  49622. RSA_METHOD_FLAG_NO_CHECK));
  49623. #ifndef NO_WOLFSSL_STUB
  49624. ExpectIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  49625. ExpectIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  49626. ExpectIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  49627. ExpectIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  49628. ExpectIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  49629. ExpectIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  49630. ExpectIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  49631. #endif
  49632. ExpectIntEQ(RSA_flags(NULL), 0);
  49633. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  49634. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  49635. ExpectIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  49636. ExpectNotNull(rsa = RSA_new());
  49637. /* No method set. */
  49638. ExpectIntEQ(RSA_flags(rsa), 0);
  49639. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  49640. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  49641. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  49642. ExpectIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  49643. ExpectIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  49644. if (EXPECT_FAIL()) {
  49645. wolfSSL_RSA_meth_free(rsa_meth);
  49646. }
  49647. ExpectNull(RSA_get_method(NULL));
  49648. ExpectPtrEq(RSA_get_method(rsa), rsa_meth);
  49649. ExpectIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  49650. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  49651. ExpectIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  49652. ExpectIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  49653. RSA_METHOD_FLAG_NO_CHECK);
  49654. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  49655. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  49656. ExpectIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  49657. /* rsa_meth is freed here */
  49658. RSA_free(rsa);
  49659. #endif
  49660. return EXPECT_RESULT();
  49661. }
  49662. static int test_wolfSSL_RSA_verify(void)
  49663. {
  49664. EXPECT_DECLS;
  49665. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && \
  49666. !defined(NO_FILESYSTEM)
  49667. #ifndef NO_BIO
  49668. XFILE fp = XBADFILE;
  49669. RSA *pKey = NULL;
  49670. RSA *pubKey = NULL;
  49671. X509 *cert = NULL;
  49672. const char *text = "Hello wolfSSL !";
  49673. unsigned char hash[SHA256_DIGEST_LENGTH];
  49674. unsigned char signature[2048/8];
  49675. unsigned int signatureLength;
  49676. byte *buf = NULL;
  49677. BIO *bio = NULL;
  49678. SHA256_CTX c;
  49679. EVP_PKEY *evpPkey = NULL;
  49680. EVP_PKEY *evpPubkey = NULL;
  49681. size_t sz;
  49682. /* generate hash */
  49683. SHA256_Init(&c);
  49684. SHA256_Update(&c, text, strlen(text));
  49685. SHA256_Final(hash, &c);
  49686. #ifdef WOLFSSL_SMALL_STACK_CACHE
  49687. /* workaround for small stack cache case */
  49688. wc_Sha256Free((wc_Sha256*)&c);
  49689. #endif
  49690. /* read privete key file */
  49691. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  49692. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_END), 0);
  49693. ExpectTrue((sz = XFTELL(fp)) > 0);
  49694. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_SET), 0);
  49695. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  49696. ExpectIntEQ(XFREAD(buf, 1, sz, fp), sz);
  49697. if (fp != XBADFILE) {
  49698. XFCLOSE(fp);
  49699. fp = XBADFILE;
  49700. }
  49701. /* read private key and sign hash data */
  49702. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  49703. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  49704. ExpectNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  49705. ExpectIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  49706. signature, &signatureLength, pKey), SSL_SUCCESS);
  49707. /* read public key and verify signed data */
  49708. ExpectTrue((fp = XFOPEN(svrCertFile,"rb")) != XBADFILE);
  49709. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  49710. if (fp != XBADFILE)
  49711. XFCLOSE(fp);
  49712. ExpectNotNull(evpPubkey = X509_get_pubkey(cert));
  49713. ExpectNotNull(pubKey = EVP_PKEY_get1_RSA(evpPubkey));
  49714. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  49715. signatureLength, pubKey), SSL_SUCCESS);
  49716. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  49717. signatureLength, NULL), SSL_FAILURE);
  49718. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  49719. signatureLength, pubKey), SSL_FAILURE);
  49720. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  49721. signatureLength, pubKey), SSL_FAILURE);
  49722. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  49723. signatureLength, NULL), SSL_FAILURE);
  49724. RSA_free(pKey);
  49725. EVP_PKEY_free(evpPkey);
  49726. RSA_free(pubKey);
  49727. EVP_PKEY_free(evpPubkey);
  49728. X509_free(cert);
  49729. BIO_free(bio);
  49730. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  49731. #endif
  49732. #endif
  49733. return EXPECT_RESULT();
  49734. }
  49735. static int test_wolfSSL_RSA_sign(void)
  49736. {
  49737. EXPECT_DECLS;
  49738. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  49739. RSA *rsa;
  49740. unsigned char hash[SHA256_DIGEST_LENGTH];
  49741. #ifdef USE_CERT_BUFFERS_1024
  49742. const unsigned char* privDer = client_key_der_1024;
  49743. size_t privDerSz = sizeof_client_key_der_1024;
  49744. const unsigned char* pubDer = client_keypub_der_1024;
  49745. size_t pubDerSz = sizeof_client_keypub_der_1024;
  49746. unsigned char signature[1024/8];
  49747. #else
  49748. const unsigned char* privDer = client_key_der_2048;
  49749. size_t privDerSz = sizeof_client_key_der_2048;
  49750. const unsigned char* pubDer = client_keypub_der_2048;
  49751. size_t pubDerSz = sizeof_client_keypub_der_2048;
  49752. unsigned char signature[2048/8];
  49753. #endif
  49754. unsigned int signatureLen;
  49755. const unsigned char* der;
  49756. XMEMSET(hash, 0, sizeof(hash));
  49757. der = privDer;
  49758. rsa = NULL;
  49759. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  49760. /* Invalid parameters. */
  49761. ExpectIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  49762. ExpectIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  49763. &signatureLen, rsa), 0);
  49764. ExpectIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  49765. &signatureLen, rsa), 0);
  49766. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  49767. &signatureLen, rsa), 0);
  49768. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  49769. NULL, rsa), 0);
  49770. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  49771. &signatureLen, NULL), 0);
  49772. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  49773. &signatureLen, rsa), 1);
  49774. RSA_free(rsa);
  49775. der = pubDer;
  49776. rsa = NULL;
  49777. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  49778. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  49779. signatureLen, rsa), 1);
  49780. RSA_free(rsa);
  49781. #endif
  49782. return EXPECT_RESULT();
  49783. }
  49784. static int test_wolfSSL_RSA_sign_ex(void)
  49785. {
  49786. EXPECT_DECLS;
  49787. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  49788. RSA *rsa = NULL;
  49789. unsigned char hash[SHA256_DIGEST_LENGTH];
  49790. #ifdef USE_CERT_BUFFERS_1024
  49791. const unsigned char* privDer = client_key_der_1024;
  49792. size_t privDerSz = sizeof_client_key_der_1024;
  49793. const unsigned char* pubDer = client_keypub_der_1024;
  49794. size_t pubDerSz = sizeof_client_keypub_der_1024;
  49795. unsigned char signature[1024/8];
  49796. #else
  49797. const unsigned char* privDer = client_key_der_2048;
  49798. size_t privDerSz = sizeof_client_key_der_2048;
  49799. const unsigned char* pubDer = client_keypub_der_2048;
  49800. size_t pubDerSz = sizeof_client_keypub_der_2048;
  49801. unsigned char signature[2048/8];
  49802. #endif
  49803. unsigned int signatureLen;
  49804. const unsigned char* der;
  49805. unsigned char encodedHash[51];
  49806. unsigned int encodedHashLen;
  49807. const unsigned char expEncHash[] = {
  49808. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  49809. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  49810. 0x00, 0x04, 0x20,
  49811. /* Hash data */
  49812. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49813. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49814. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49815. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49816. };
  49817. XMEMSET(hash, 0, sizeof(hash));
  49818. ExpectNotNull(rsa = wolfSSL_RSA_new());
  49819. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49820. &signatureLen, rsa, 1), 0);
  49821. wolfSSL_RSA_free(rsa);
  49822. der = privDer;
  49823. rsa = NULL;
  49824. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  49825. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  49826. -1), 0);
  49827. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  49828. signature, &signatureLen, rsa, 1), 0);
  49829. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  49830. &signatureLen, rsa, 1), 0);
  49831. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  49832. &signatureLen, rsa, 1), 0);
  49833. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49834. NULL, rsa, 1), 0);
  49835. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49836. &signatureLen, NULL, 1), 0);
  49837. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49838. &signatureLen, rsa, -1), 0);
  49839. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  49840. &signatureLen, rsa, 0), 0);
  49841. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  49842. &signatureLen, rsa, 0), 0);
  49843. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49844. NULL, rsa, 0), 0);
  49845. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49846. &signatureLen, rsa, 1), 1);
  49847. /* Test returning encoded hash. */
  49848. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  49849. &encodedHashLen, rsa, 0), 1);
  49850. ExpectIntEQ(encodedHashLen, sizeof(expEncHash));
  49851. ExpectIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  49852. RSA_free(rsa);
  49853. der = pubDer;
  49854. rsa = NULL;
  49855. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  49856. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  49857. signatureLen, rsa), 1);
  49858. RSA_free(rsa);
  49859. #endif
  49860. return EXPECT_RESULT();
  49861. }
  49862. static int test_wolfSSL_RSA_public_decrypt(void)
  49863. {
  49864. EXPECT_DECLS;
  49865. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  49866. RSA *rsa;
  49867. unsigned char msg[SHA256_DIGEST_LENGTH];
  49868. #ifdef USE_CERT_BUFFERS_1024
  49869. const unsigned char* pubDer = client_keypub_der_1024;
  49870. size_t pubDerSz = sizeof_client_keypub_der_1024;
  49871. unsigned char decMsg[1024/8];
  49872. const unsigned char encMsg[] = {
  49873. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  49874. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  49875. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  49876. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  49877. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  49878. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  49879. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  49880. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  49881. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  49882. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  49883. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  49884. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  49885. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  49886. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  49887. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  49888. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  49889. };
  49890. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  49891. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  49892. defined(WC_RSA_NO_PADDING)
  49893. const unsigned char encMsgNoPad[] = {
  49894. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  49895. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  49896. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  49897. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  49898. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  49899. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  49900. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  49901. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  49902. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  49903. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  49904. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  49905. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  49906. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  49907. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  49908. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  49909. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  49910. };
  49911. #endif
  49912. #else
  49913. const unsigned char* pubDer = client_keypub_der_2048;
  49914. size_t pubDerSz = sizeof_client_keypub_der_2048;
  49915. unsigned char decMsg[2048/8];
  49916. const unsigned char encMsg[] = {
  49917. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  49918. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  49919. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  49920. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  49921. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  49922. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  49923. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  49924. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  49925. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  49926. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  49927. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  49928. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  49929. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  49930. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  49931. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  49932. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  49933. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  49934. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  49935. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  49936. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  49937. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  49938. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  49939. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  49940. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  49941. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  49942. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  49943. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  49944. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  49945. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  49946. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  49947. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  49948. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  49949. };
  49950. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  49951. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  49952. defined(WC_RSA_NO_PADDING)
  49953. const unsigned char encMsgNoPad[] = {
  49954. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  49955. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  49956. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  49957. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  49958. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  49959. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  49960. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  49961. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  49962. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  49963. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  49964. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  49965. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  49966. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  49967. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  49968. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  49969. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  49970. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  49971. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  49972. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  49973. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  49974. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  49975. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  49976. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  49977. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  49978. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  49979. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  49980. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  49981. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  49982. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  49983. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  49984. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  49985. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  49986. };
  49987. #endif
  49988. #endif
  49989. const unsigned char* der;
  49990. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  49991. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  49992. defined(WC_RSA_NO_PADDING)
  49993. int i;
  49994. #endif
  49995. XMEMSET(msg, 0, sizeof(msg));
  49996. der = pubDer;
  49997. rsa = NULL;
  49998. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  49999. ExpectIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  50000. ExpectIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  50001. RSA_PKCS1_PADDING), -1);
  50002. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  50003. RSA_PKCS1_PADDING), -1);
  50004. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  50005. RSA_PKCS1_PADDING), -1);
  50006. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  50007. RSA_PKCS1_PADDING), -1);
  50008. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  50009. RSA_PKCS1_PSS_PADDING), -1);
  50010. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  50011. RSA_PKCS1_PADDING), 32);
  50012. ExpectIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  50013. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  50014. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  50015. defined(WC_RSA_NO_PADDING)
  50016. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  50017. rsa, RSA_NO_PADDING), sizeof(decMsg));
  50018. /* Zeros before actual data. */
  50019. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  50020. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  50021. }
  50022. /* Check actual data. */
  50023. XMEMSET(msg, 0x01, sizeof(msg));
  50024. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  50025. #endif
  50026. RSA_free(rsa);
  50027. #endif
  50028. return EXPECT_RESULT();
  50029. }
  50030. static int test_wolfSSL_RSA_private_encrypt(void)
  50031. {
  50032. EXPECT_DECLS;
  50033. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  50034. RSA *rsa;
  50035. unsigned char msg[SHA256_DIGEST_LENGTH];
  50036. #ifdef USE_CERT_BUFFERS_1024
  50037. const unsigned char* privDer = client_key_der_1024;
  50038. size_t privDerSz = sizeof_client_key_der_1024;
  50039. unsigned char encMsg[1024/8];
  50040. const unsigned char expEncMsg[] = {
  50041. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  50042. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  50043. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  50044. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  50045. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  50046. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  50047. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  50048. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  50049. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  50050. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  50051. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  50052. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  50053. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  50054. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  50055. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  50056. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  50057. };
  50058. #ifdef WC_RSA_NO_PADDING
  50059. const unsigned char expEncMsgNoPad[] = {
  50060. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  50061. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  50062. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  50063. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  50064. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  50065. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  50066. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  50067. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  50068. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  50069. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  50070. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  50071. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  50072. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  50073. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  50074. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  50075. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  50076. };
  50077. #endif
  50078. #else
  50079. const unsigned char* privDer = client_key_der_2048;
  50080. size_t privDerSz = sizeof_client_key_der_2048;
  50081. unsigned char encMsg[2048/8];
  50082. const unsigned char expEncMsg[] = {
  50083. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  50084. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  50085. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  50086. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  50087. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  50088. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  50089. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  50090. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  50091. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  50092. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  50093. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  50094. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  50095. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  50096. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  50097. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  50098. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  50099. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  50100. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  50101. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  50102. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  50103. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  50104. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  50105. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  50106. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  50107. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  50108. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  50109. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  50110. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  50111. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  50112. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  50113. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  50114. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  50115. };
  50116. #ifdef WC_RSA_NO_PADDING
  50117. const unsigned char expEncMsgNoPad[] = {
  50118. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  50119. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  50120. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  50121. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  50122. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  50123. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  50124. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  50125. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  50126. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  50127. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  50128. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  50129. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  50130. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  50131. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  50132. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  50133. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  50134. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  50135. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  50136. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  50137. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  50138. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  50139. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  50140. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  50141. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  50142. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  50143. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  50144. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  50145. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  50146. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  50147. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  50148. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  50149. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  50150. };
  50151. #endif
  50152. #endif
  50153. const unsigned char* der;
  50154. XMEMSET(msg, 0x00, sizeof(msg));
  50155. der = privDer;
  50156. rsa = NULL;
  50157. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50158. ExpectIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  50159. ExpectIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  50160. -1);
  50161. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  50162. RSA_PKCS1_PADDING), -1);
  50163. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  50164. RSA_PKCS1_PADDING), -1);
  50165. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  50166. RSA_PKCS1_PADDING), -1);
  50167. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  50168. RSA_PKCS1_PSS_PADDING), -1);
  50169. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  50170. RSA_PKCS1_PADDING), sizeof(encMsg));
  50171. ExpectIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  50172. #ifdef WC_RSA_NO_PADDING
  50173. /* Non-zero message. */
  50174. XMEMSET(msg, 0x01, sizeof(msg));
  50175. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  50176. RSA_NO_PADDING), sizeof(encMsg));
  50177. ExpectIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  50178. #endif
  50179. RSA_free(rsa);
  50180. #endif
  50181. return EXPECT_RESULT();
  50182. }
  50183. static int test_wolfSSL_RSA_public_encrypt(void)
  50184. {
  50185. EXPECT_DECLS;
  50186. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  50187. RSA* rsa = NULL;
  50188. const unsigned char msg[2048/8] = { 0 };
  50189. unsigned char encMsg[2048/8];
  50190. ExpectNotNull(rsa = RSA_new());
  50191. ExpectIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  50192. RSA_PKCS1_PADDING), -1);
  50193. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  50194. RSA_PKCS1_PADDING), -1);
  50195. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  50196. RSA_PKCS1_PADDING), -1);
  50197. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  50198. RSA_PKCS1_PADDING), -1);
  50199. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  50200. RSA_PKCS1_PSS_PADDING), -1);
  50201. /* Empty RSA key. */
  50202. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  50203. RSA_PKCS1_PADDING), -1);
  50204. RSA_free(rsa);
  50205. #endif
  50206. return EXPECT_RESULT();
  50207. }
  50208. static int test_wolfSSL_RSA_private_decrypt(void)
  50209. {
  50210. EXPECT_DECLS;
  50211. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  50212. RSA* rsa = NULL;
  50213. unsigned char msg[2048/8];
  50214. const unsigned char encMsg[2048/8] = { 0 };
  50215. ExpectNotNull(rsa = RSA_new());
  50216. ExpectIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  50217. RSA_PKCS1_PADDING), -1);
  50218. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  50219. RSA_PKCS1_PADDING), -1);
  50220. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  50221. RSA_PKCS1_PADDING), -1);
  50222. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  50223. RSA_PKCS1_PADDING), -1);
  50224. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  50225. RSA_PKCS1_PSS_PADDING), -1);
  50226. /* Empty RSA key. */
  50227. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  50228. RSA_PKCS1_PADDING), -1);
  50229. RSA_free(rsa);
  50230. #endif
  50231. return EXPECT_RESULT();
  50232. }
  50233. static int test_wolfSSL_RSA_GenAdd(void)
  50234. {
  50235. EXPECT_DECLS;
  50236. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  50237. RSA *rsa;
  50238. #ifdef USE_CERT_BUFFERS_1024
  50239. const unsigned char* privDer = client_key_der_1024;
  50240. size_t privDerSz = sizeof_client_key_der_1024;
  50241. const unsigned char* pubDer = client_keypub_der_1024;
  50242. size_t pubDerSz = sizeof_client_keypub_der_1024;
  50243. #else
  50244. const unsigned char* privDer = client_key_der_2048;
  50245. size_t privDerSz = sizeof_client_key_der_2048;
  50246. const unsigned char* pubDer = client_keypub_der_2048;
  50247. size_t pubDerSz = sizeof_client_keypub_der_2048;
  50248. #endif
  50249. const unsigned char* der;
  50250. der = privDer;
  50251. rsa = NULL;
  50252. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50253. ExpectIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  50254. #ifndef RSA_LOW_MEM
  50255. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  50256. #else
  50257. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  50258. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  50259. #endif
  50260. RSA_free(rsa);
  50261. der = pubDer;
  50262. rsa = NULL;
  50263. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  50264. /* Need private values. */
  50265. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  50266. RSA_free(rsa);
  50267. #endif
  50268. return EXPECT_RESULT();
  50269. }
  50270. static int test_wolfSSL_RSA_blinding_on(void)
  50271. {
  50272. EXPECT_DECLS;
  50273. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  50274. RSA *rsa;
  50275. WOLFSSL_BN_CTX *bnCtx = NULL;
  50276. #ifdef USE_CERT_BUFFERS_1024
  50277. const unsigned char* privDer = client_key_der_1024;
  50278. size_t privDerSz = sizeof_client_key_der_1024;
  50279. #else
  50280. const unsigned char* privDer = client_key_der_2048;
  50281. size_t privDerSz = sizeof_client_key_der_2048;
  50282. #endif
  50283. const unsigned char* der;
  50284. der = privDer;
  50285. rsa = NULL;
  50286. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50287. ExpectNotNull(bnCtx = wolfSSL_BN_CTX_new());
  50288. /* Does nothing so all parameters are valid. */
  50289. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  50290. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  50291. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  50292. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  50293. wolfSSL_BN_CTX_free(bnCtx);
  50294. RSA_free(rsa);
  50295. #endif
  50296. return EXPECT_RESULT();
  50297. }
  50298. static int test_wolfSSL_RSA_ex_data(void)
  50299. {
  50300. EXPECT_DECLS;
  50301. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  50302. RSA* rsa = NULL;
  50303. unsigned char data[1];
  50304. ExpectNotNull(rsa = RSA_new());
  50305. ExpectNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  50306. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  50307. #ifdef MAX_EX_DATA
  50308. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  50309. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  50310. #endif
  50311. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  50312. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  50313. #ifdef HAVE_EX_DATA
  50314. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  50315. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  50316. ExpectPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  50317. #else
  50318. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  50319. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  50320. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  50321. #endif
  50322. RSA_free(rsa);
  50323. #endif /* !NO_RSA && OPENSSL_EXTRA */
  50324. return EXPECT_RESULT();
  50325. }
  50326. static int test_wolfSSL_RSA_LoadDer(void)
  50327. {
  50328. EXPECT_DECLS;
  50329. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  50330. defined(OPENSSL_EXTRA_X509_SMALL))
  50331. RSA *rsa = NULL;
  50332. #ifdef USE_CERT_BUFFERS_1024
  50333. const unsigned char* privDer = client_key_der_1024;
  50334. size_t privDerSz = sizeof_client_key_der_1024;
  50335. #else
  50336. const unsigned char* privDer = client_key_der_2048;
  50337. size_t privDerSz = sizeof_client_key_der_2048;
  50338. #endif
  50339. ExpectNotNull(rsa = RSA_new());
  50340. ExpectIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  50341. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  50342. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  50343. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  50344. RSA_free(rsa);
  50345. #endif /* !NO_RSA && OPENSSL_EXTRA */
  50346. return EXPECT_RESULT();
  50347. }
  50348. /* Local API. */
  50349. static int test_wolfSSL_RSA_To_Der(void)
  50350. {
  50351. EXPECT_DECLS;
  50352. #ifdef WOLFSSL_TEST_STATIC_BUILD
  50353. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA) && \
  50354. defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  50355. RSA* rsa;
  50356. #ifdef USE_CERT_BUFFERS_1024
  50357. const unsigned char* privDer = client_key_der_1024;
  50358. size_t privDerSz = sizeof_client_key_der_1024;
  50359. const unsigned char* pubDer = client_keypub_der_1024;
  50360. size_t pubDerSz = sizeof_client_keypub_der_1024;
  50361. unsigned char out[sizeof(client_key_der_1024)];
  50362. #else
  50363. const unsigned char* privDer = client_key_der_2048;
  50364. size_t privDerSz = sizeof_client_key_der_2048;
  50365. const unsigned char* pubDer = client_keypub_der_2048;
  50366. size_t pubDerSz = sizeof_client_keypub_der_2048;
  50367. unsigned char out[sizeof(client_key_der_2048)];
  50368. #endif
  50369. const unsigned char* der;
  50370. unsigned char* outDer = NULL;
  50371. der = privDer;
  50372. rsa = NULL;
  50373. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50374. ExpectIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  50375. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  50376. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  50377. outDer = out;
  50378. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  50379. ExpectIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  50380. outDer = NULL;
  50381. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  50382. ExpectNotNull(outDer);
  50383. ExpectIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  50384. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  50385. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  50386. outDer = out;
  50387. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  50388. ExpectIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  50389. RSA_free(rsa);
  50390. ExpectNotNull(rsa = RSA_new());
  50391. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  50392. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  50393. RSA_free(rsa);
  50394. der = pubDer;
  50395. rsa = NULL;
  50396. ExpectNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  50397. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  50398. RSA_free(rsa);
  50399. #endif
  50400. #endif
  50401. return EXPECT_RESULT();
  50402. }
  50403. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  50404. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  50405. {
  50406. EXPECT_DECLS;
  50407. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  50408. XFILE file = XBADFILE;
  50409. const char* fname = "./certs/server-keyPub.pem";
  50410. RSA *rsa = NULL;
  50411. ExpectNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  50412. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  50413. ExpectNotNull(rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL));
  50414. ExpectIntEQ(RSA_size(rsa), 256);
  50415. RSA_free(rsa);
  50416. if (file != XBADFILE)
  50417. XFCLOSE(file);
  50418. #endif
  50419. return EXPECT_RESULT();
  50420. }
  50421. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  50422. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  50423. {
  50424. EXPECT_DECLS;
  50425. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  50426. defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA)
  50427. RSA* rsa = NULL;
  50428. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  50429. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
  50430. /* Valid but stub so returns 0. */
  50431. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
  50432. #endif
  50433. return EXPECT_RESULT();
  50434. }
  50435. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  50436. {
  50437. EXPECT_DECLS;
  50438. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  50439. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  50440. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  50441. RSA* rsa = NULL;
  50442. #ifdef USE_CERT_BUFFERS_1024
  50443. const unsigned char* privDer = client_key_der_1024;
  50444. size_t privDerSz = sizeof_client_key_der_1024;
  50445. #else
  50446. const unsigned char* privDer = client_key_der_2048;
  50447. size_t privDerSz = sizeof_client_key_der_2048;
  50448. #endif
  50449. const unsigned char* der;
  50450. #ifndef NO_AES
  50451. unsigned char passwd[] = "password";
  50452. #endif
  50453. ExpectNotNull(rsa = RSA_new());
  50454. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  50455. NULL, NULL), 0);
  50456. RSA_free(rsa);
  50457. der = privDer;
  50458. rsa = NULL;
  50459. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50460. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  50461. NULL, NULL), 0);
  50462. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
  50463. NULL, NULL), 0);
  50464. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  50465. NULL, NULL), 1);
  50466. #ifndef NO_AES
  50467. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  50468. NULL, 0, NULL, NULL), 1);
  50469. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  50470. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  50471. #endif
  50472. RSA_free(rsa);
  50473. #endif
  50474. return EXPECT_RESULT();
  50475. }
  50476. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  50477. {
  50478. EXPECT_DECLS;
  50479. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  50480. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  50481. defined(WOLFSSL_DER_TO_PEM))
  50482. RSA* rsa = NULL;
  50483. #ifdef USE_CERT_BUFFERS_1024
  50484. const unsigned char* privDer = client_key_der_1024;
  50485. size_t privDerSz = sizeof_client_key_der_1024;
  50486. #else
  50487. const unsigned char* privDer = client_key_der_2048;
  50488. size_t privDerSz = sizeof_client_key_der_2048;
  50489. #endif
  50490. const unsigned char* der;
  50491. #ifndef NO_AES
  50492. unsigned char passwd[] = "password";
  50493. #endif
  50494. unsigned char* pem = NULL;
  50495. int plen;
  50496. ExpectNotNull(rsa = RSA_new());
  50497. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  50498. &plen), 0);
  50499. RSA_free(rsa);
  50500. der = privDer;
  50501. rsa = NULL;
  50502. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50503. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  50504. &plen), 0);
  50505. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  50506. &plen), 0);
  50507. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  50508. NULL), 0);
  50509. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  50510. &plen), 1);
  50511. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  50512. pem = NULL;
  50513. #ifndef NO_AES
  50514. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  50515. NULL, 0, &pem, &plen), 1);
  50516. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  50517. pem = NULL;
  50518. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  50519. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  50520. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  50521. #endif
  50522. RSA_free(rsa);
  50523. #endif
  50524. return EXPECT_RESULT();
  50525. }
  50526. static int test_wolfSSL_DH(void)
  50527. {
  50528. EXPECT_DECLS;
  50529. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  50530. DH *dh = NULL;
  50531. BIGNUM* p;
  50532. BIGNUM* q;
  50533. BIGNUM* g;
  50534. BIGNUM* pub = NULL;
  50535. BIGNUM* priv = NULL;
  50536. #if defined(OPENSSL_ALL)
  50537. #if !defined(HAVE_FIPS) || \
  50538. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  50539. FILE* f = NULL;
  50540. unsigned char buf[268];
  50541. const unsigned char* pt = buf;
  50542. long len = 0;
  50543. dh = NULL;
  50544. XMEMSET(buf, 0, sizeof(buf));
  50545. /* Test 2048 bit parameters */
  50546. ExpectTrue((f = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  50547. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  50548. if (f != XBADFILE)
  50549. XFCLOSE(f);
  50550. ExpectNotNull(dh = d2i_DHparams(NULL, &pt, len));
  50551. ExpectNotNull(dh->p);
  50552. ExpectNotNull(dh->g);
  50553. ExpectTrue(pt == buf);
  50554. ExpectIntEQ(DH_generate_key(dh), 1);
  50555. ExpectIntEQ(DH_generate_key(dh), 1);
  50556. ExpectIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  50557. ExpectNotNull(pub = BN_new());
  50558. ExpectIntEQ(BN_set_word(pub, 1), 1);
  50559. ExpectIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  50560. ExpectIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  50561. ExpectIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  50562. ExpectIntEQ(DH_compute_key(buf, pub, NULL), -1);
  50563. ExpectIntEQ(DH_compute_key(buf, NULL, dh), -1);
  50564. ExpectIntEQ(DH_compute_key(NULL, pub, dh), -1);
  50565. ExpectIntEQ(DH_compute_key(buf, pub, dh), -1);
  50566. BN_free(pub);
  50567. pub = NULL;
  50568. DH_get0_pqg(dh, (const BIGNUM**)&p,
  50569. (const BIGNUM**)&q,
  50570. (const BIGNUM**)&g);
  50571. ExpectPtrEq(p, dh->p);
  50572. ExpectPtrEq(q, dh->q);
  50573. ExpectPtrEq(g, dh->g);
  50574. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  50575. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  50576. ExpectPtrEq(pub, dh->pub_key);
  50577. ExpectPtrEq(priv, dh->priv_key);
  50578. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  50579. ExpectPtrEq(pub, dh->pub_key);
  50580. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  50581. ExpectPtrEq(priv, dh->priv_key);
  50582. pub = NULL;
  50583. priv = NULL;
  50584. ExpectNotNull(pub = BN_new());
  50585. ExpectNotNull(priv = BN_new());
  50586. ExpectIntEQ(DH_set0_key(NULL, pub, priv), 0);
  50587. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  50588. if (EXPECT_FAIL()) {
  50589. BN_free(pub);
  50590. BN_free(priv);
  50591. }
  50592. pub = NULL;
  50593. priv = NULL;
  50594. ExpectNotNull(pub = BN_new());
  50595. ExpectIntEQ(DH_set0_key(dh, pub, NULL), 1);
  50596. if (EXPECT_FAIL()) {
  50597. BN_free(pub);
  50598. }
  50599. ExpectNotNull(priv = BN_new());
  50600. ExpectIntEQ(DH_set0_key(dh, NULL, priv), 1);
  50601. if (EXPECT_FAIL()) {
  50602. BN_free(priv);
  50603. }
  50604. ExpectPtrEq(pub, dh->pub_key);
  50605. ExpectPtrEq(priv, dh->priv_key);
  50606. pub = NULL;
  50607. priv = NULL;
  50608. DH_free(dh);
  50609. dh = NULL;
  50610. ExpectNotNull(dh = DH_new());
  50611. p = NULL;
  50612. ExpectNotNull(p = BN_new());
  50613. ExpectIntEQ(BN_set_word(p, 1), 1);
  50614. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  50615. ExpectNotNull(pub = BN_new());
  50616. ExpectNotNull(priv = BN_new());
  50617. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  50618. if (EXPECT_FAIL()) {
  50619. BN_free(pub);
  50620. BN_free(priv);
  50621. }
  50622. pub = NULL;
  50623. priv = NULL;
  50624. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  50625. BN_free(p);
  50626. p = NULL;
  50627. DH_free(dh);
  50628. dh = NULL;
  50629. #ifdef WOLFSSL_KEY_GEN
  50630. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  50631. ExpectIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  50632. DH_free(dh);
  50633. #endif
  50634. #endif /* !HAVE_FIPS || (HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2) */
  50635. #endif /* OPENSSL_ALL */
  50636. (void)dh;
  50637. (void)p;
  50638. (void)q;
  50639. (void)g;
  50640. (void)pub;
  50641. (void)priv;
  50642. ExpectNotNull(dh = wolfSSL_DH_new());
  50643. /* invalid parameters test */
  50644. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  50645. (const BIGNUM**)&q,
  50646. (const BIGNUM**)&g);
  50647. DH_get0_pqg(dh, NULL,
  50648. (const BIGNUM**)&q,
  50649. (const BIGNUM**)&g);
  50650. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  50651. DH_get0_pqg(dh, NULL, NULL, NULL);
  50652. DH_get0_pqg(dh, (const BIGNUM**)&p,
  50653. (const BIGNUM**)&q,
  50654. (const BIGNUM**)&g);
  50655. ExpectPtrEq(p, NULL);
  50656. ExpectPtrEq(q, NULL);
  50657. ExpectPtrEq(g, NULL);
  50658. DH_free(dh);
  50659. dh = NULL;
  50660. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  50661. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  50662. #if defined(OPENSSL_ALL) || \
  50663. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  50664. dh = wolfSSL_DH_new();
  50665. ExpectNotNull(dh);
  50666. p = wolfSSL_BN_new();
  50667. ExpectNotNull(p);
  50668. ExpectIntEQ(BN_set_word(p, 11), 1);
  50669. g = wolfSSL_BN_new();
  50670. ExpectNotNull(g);
  50671. ExpectIntEQ(BN_set_word(g, 2), 1);
  50672. q = wolfSSL_BN_new();
  50673. ExpectNotNull(q);
  50674. ExpectIntEQ(BN_set_word(q, 5), 1);
  50675. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  50676. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  50677. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  50678. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  50679. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  50680. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  50681. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  50682. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  50683. /* Don't need q. */
  50684. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  50685. if (EXPECT_FAIL()) {
  50686. BN_free(p);
  50687. BN_free(g);
  50688. }
  50689. p = NULL;
  50690. g = NULL;
  50691. /* Setting again will free the p and g. */
  50692. wolfSSL_BN_free(q);
  50693. q = NULL;
  50694. DH_free(dh);
  50695. dh = NULL;
  50696. dh = wolfSSL_DH_new();
  50697. ExpectNotNull(dh);
  50698. p = wolfSSL_BN_new();
  50699. ExpectNotNull(p);
  50700. ExpectIntEQ(BN_set_word(p, 11), 1);
  50701. g = wolfSSL_BN_new();
  50702. ExpectNotNull(g);
  50703. ExpectIntEQ(BN_set_word(g, 2), 1);
  50704. q = wolfSSL_BN_new();
  50705. ExpectNotNull(q);
  50706. ExpectIntEQ(BN_set_word(q, 5), 1);
  50707. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  50708. /* p, q and g are now owned by dh - don't free. */
  50709. if (EXPECT_FAIL()) {
  50710. BN_free(p);
  50711. BN_free(q);
  50712. BN_free(g);
  50713. }
  50714. p = NULL;
  50715. q = NULL;
  50716. g = NULL;
  50717. p = wolfSSL_BN_new();
  50718. ExpectNotNull(p);
  50719. ExpectIntEQ(BN_set_word(p, 11), 1);
  50720. g = wolfSSL_BN_new();
  50721. ExpectNotNull(g);
  50722. ExpectIntEQ(BN_set_word(g, 2), 1);
  50723. q = wolfSSL_BN_new();
  50724. ExpectNotNull(q);
  50725. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  50726. if (EXPECT_FAIL()) {
  50727. BN_free(p);
  50728. }
  50729. p = NULL;
  50730. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  50731. if (EXPECT_FAIL()) {
  50732. BN_free(q);
  50733. }
  50734. q = NULL;
  50735. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  50736. if (EXPECT_FAIL()) {
  50737. BN_free(g);
  50738. }
  50739. g = NULL;
  50740. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  50741. /* p, q and g are now owned by dh - don't free. */
  50742. DH_free(dh);
  50743. dh = NULL;
  50744. ExpectIntEQ(DH_generate_key(NULL), 0);
  50745. ExpectNotNull(dh = DH_new());
  50746. ExpectIntEQ(DH_generate_key(dh), 0);
  50747. p = wolfSSL_BN_new();
  50748. ExpectNotNull(p);
  50749. ExpectIntEQ(BN_set_word(p, 0), 1);
  50750. g = wolfSSL_BN_new();
  50751. ExpectNotNull(g);
  50752. ExpectIntEQ(BN_set_word(g, 2), 1);
  50753. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  50754. if (EXPECT_FAIL()) {
  50755. BN_free(p);
  50756. BN_free(g);
  50757. }
  50758. p = NULL;
  50759. g = NULL;
  50760. ExpectIntEQ(DH_generate_key(dh), 0);
  50761. DH_free(dh);
  50762. dh = NULL;
  50763. #endif
  50764. #endif
  50765. /* Test DH_up_ref() */
  50766. dh = wolfSSL_DH_new();
  50767. ExpectNotNull(dh);
  50768. ExpectIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  50769. ExpectIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  50770. DH_free(dh); /* decrease ref count */
  50771. DH_free(dh); /* free WOLFSSL_DH */
  50772. q = NULL;
  50773. ExpectNull((dh = DH_new_by_nid(NID_sha1)));
  50774. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  50775. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  50776. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  50777. #ifdef HAVE_FFDHE_2048
  50778. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  50779. DH_free(dh);
  50780. q = NULL;
  50781. #endif
  50782. #ifdef HAVE_FFDHE_3072
  50783. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  50784. DH_free(dh);
  50785. q = NULL;
  50786. #endif
  50787. #ifdef HAVE_FFDHE_4096
  50788. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  50789. DH_free(dh);
  50790. q = NULL;
  50791. #endif
  50792. #else
  50793. ExpectNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  50794. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  50795. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  50796. ExpectIntEQ(wolfSSL_DH_size(NULL), -1);
  50797. #endif /* OPENSSL_EXTRA && !NO_DH */
  50798. return EXPECT_RESULT();
  50799. }
  50800. static int test_wolfSSL_DH_dup(void)
  50801. {
  50802. EXPECT_DECLS;
  50803. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  50804. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  50805. defined(OPENSSL_EXTRA)
  50806. DH *dh = NULL;
  50807. DH *dhDup = NULL;
  50808. ExpectNotNull(dh = wolfSSL_DH_new());
  50809. ExpectNull(dhDup = wolfSSL_DH_dup(NULL));
  50810. ExpectNull(dhDup = wolfSSL_DH_dup(dh));
  50811. #if defined(OPENSSL_ALL) || \
  50812. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  50813. {
  50814. WOLFSSL_BIGNUM* p = NULL;
  50815. WOLFSSL_BIGNUM* g = NULL;
  50816. ExpectNotNull(p = wolfSSL_BN_new());
  50817. ExpectNotNull(g = wolfSSL_BN_new());
  50818. ExpectIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  50819. ExpectIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  50820. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  50821. if (EXPECT_FAIL()) {
  50822. wolfSSL_BN_free(p);
  50823. wolfSSL_BN_free(g);
  50824. }
  50825. ExpectNotNull(dhDup = wolfSSL_DH_dup(dh));
  50826. wolfSSL_DH_free(dhDup);
  50827. }
  50828. #endif
  50829. wolfSSL_DH_free(dh);
  50830. #endif
  50831. #endif
  50832. return EXPECT_RESULT();
  50833. }
  50834. static int test_wolfSSL_DH_check(void)
  50835. {
  50836. EXPECT_DECLS;
  50837. #ifdef OPENSSL_ALL
  50838. #ifndef NO_DH
  50839. #ifndef NO_BIO
  50840. #ifndef NO_DSA
  50841. byte buf[6000];
  50842. char file[] = "./certs/dsaparams.pem";
  50843. XFILE f = XBADFILE;
  50844. int bytes;
  50845. BIO* bio = NULL;
  50846. DSA* dsa = NULL;
  50847. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  50848. static const byte dh2048[] = {
  50849. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  50850. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  50851. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  50852. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  50853. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  50854. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  50855. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  50856. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  50857. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  50858. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  50859. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  50860. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  50861. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  50862. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  50863. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  50864. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  50865. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  50866. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  50867. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  50868. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  50869. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  50870. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  50871. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  50872. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  50873. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  50874. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  50875. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  50876. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  50877. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  50878. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  50879. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  50880. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  50881. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  50882. 0x93, 0x02, 0x01, 0x02
  50883. };
  50884. const byte* params;
  50885. #endif
  50886. DH* dh = NULL;
  50887. WOLFSSL_BIGNUM* p = NULL;
  50888. WOLFSSL_BIGNUM* g = NULL;
  50889. WOLFSSL_BIGNUM* pTmp = NULL;
  50890. WOLFSSL_BIGNUM* gTmp = NULL;
  50891. int codes = -1;
  50892. #ifndef NO_DSA
  50893. /* Initialize DH */
  50894. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  50895. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  50896. if (f != XBADFILE)
  50897. XFCLOSE(f);
  50898. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  50899. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  50900. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  50901. ExpectNotNull(dh);
  50902. BIO_free(bio);
  50903. DSA_free(dsa);
  50904. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  50905. params = dh2048;
  50906. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &params,
  50907. (long)sizeof(dh2048)));
  50908. #else
  50909. ExpectNotNull(dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048));
  50910. #endif
  50911. /* Test assumed to be valid dh.
  50912. * Should return WOLFSSL_SUCCESS
  50913. * codes should be 0
  50914. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  50915. */
  50916. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  50917. ExpectIntEQ(codes, 0);
  50918. /* Test NULL dh: expected BAD_FUNC_ARG */
  50919. ExpectIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  50920. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  50921. if (dh != NULL) {
  50922. pTmp = dh->p;
  50923. dh->p = NULL;
  50924. }
  50925. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  50926. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  50927. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  50928. /* set dh->p back to normal so it won't fail on next tests */
  50929. if (dh != NULL) {
  50930. dh->p = pTmp;
  50931. pTmp = NULL;
  50932. }
  50933. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  50934. if (dh != NULL) {
  50935. gTmp = dh->g;
  50936. dh->g = NULL;
  50937. }
  50938. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  50939. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  50940. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  50941. if (dh != NULL) {
  50942. dh->g = gTmp;
  50943. gTmp = NULL;
  50944. }
  50945. /* Cleanup */
  50946. DH_free(dh);
  50947. dh = NULL;
  50948. dh = DH_new();
  50949. ExpectNotNull(dh);
  50950. /* Check empty DH. */
  50951. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  50952. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  50953. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  50954. /* Check non-prime valued p. */
  50955. ExpectNotNull(p = BN_new());
  50956. ExpectIntEQ(BN_set_word(p, 4), 1);
  50957. ExpectNotNull(g = BN_new());
  50958. ExpectIntEQ(BN_set_word(g, 2), 1);
  50959. ExpectIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  50960. if (EXPECT_FAIL()) {
  50961. wolfSSL_BN_free(p);
  50962. wolfSSL_BN_free(g);
  50963. }
  50964. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  50965. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  50966. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  50967. DH_free(dh);
  50968. #endif
  50969. #endif /* !NO_DH && !NO_DSA */
  50970. #endif
  50971. return EXPECT_RESULT();
  50972. }
  50973. static int test_wolfSSL_DH_prime(void)
  50974. {
  50975. EXPECT_DECLS;
  50976. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  50977. WOLFSSL_BIGNUM* bn = NULL;
  50978. #if WOLFSSL_MAX_BN_BITS >= 768
  50979. WOLFSSL_BIGNUM* bn2 = NULL;
  50980. #endif
  50981. bn = wolfSSL_DH_768_prime(NULL);
  50982. #if WOLFSSL_MAX_BN_BITS >= 768
  50983. ExpectNotNull(bn);
  50984. bn2 = wolfSSL_DH_768_prime(bn);
  50985. ExpectNotNull(bn2);
  50986. ExpectTrue(bn == bn2);
  50987. wolfSSL_BN_free(bn);
  50988. bn = NULL;
  50989. #else
  50990. ExpectNull(bn);
  50991. #endif
  50992. bn = wolfSSL_DH_1024_prime(NULL);
  50993. #if WOLFSSL_MAX_BN_BITS >= 1024
  50994. ExpectNotNull(bn);
  50995. wolfSSL_BN_free(bn);
  50996. bn = NULL;
  50997. #else
  50998. ExpectNull(bn);
  50999. #endif
  51000. bn = wolfSSL_DH_2048_prime(NULL);
  51001. #if WOLFSSL_MAX_BN_BITS >= 2048
  51002. ExpectNotNull(bn);
  51003. wolfSSL_BN_free(bn);
  51004. bn = NULL;
  51005. #else
  51006. ExpectNull(bn);
  51007. #endif
  51008. bn = wolfSSL_DH_3072_prime(NULL);
  51009. #if WOLFSSL_MAX_BN_BITS >= 3072
  51010. ExpectNotNull(bn);
  51011. wolfSSL_BN_free(bn);
  51012. bn = NULL;
  51013. #else
  51014. ExpectNull(bn);
  51015. #endif
  51016. bn = wolfSSL_DH_4096_prime(NULL);
  51017. #if WOLFSSL_MAX_BN_BITS >= 4096
  51018. ExpectNotNull(bn);
  51019. wolfSSL_BN_free(bn);
  51020. bn = NULL;
  51021. #else
  51022. ExpectNull(bn);
  51023. #endif
  51024. bn = wolfSSL_DH_6144_prime(NULL);
  51025. #if WOLFSSL_MAX_BN_BITS >= 6144
  51026. ExpectNotNull(bn);
  51027. wolfSSL_BN_free(bn);
  51028. bn = NULL;
  51029. #else
  51030. ExpectNull(bn);
  51031. #endif
  51032. bn = wolfSSL_DH_8192_prime(NULL);
  51033. #if WOLFSSL_MAX_BN_BITS >= 8192
  51034. ExpectNotNull(bn);
  51035. wolfSSL_BN_free(bn);
  51036. bn = NULL;
  51037. #else
  51038. ExpectNull(bn);
  51039. #endif
  51040. #endif
  51041. return EXPECT_RESULT();
  51042. }
  51043. static int test_wolfSSL_DH_1536_prime(void)
  51044. {
  51045. EXPECT_DECLS;
  51046. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  51047. BIGNUM* bn = NULL;
  51048. unsigned char bits[200];
  51049. int sz = 192; /* known binary size */
  51050. const byte expected[] = {
  51051. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  51052. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  51053. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  51054. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  51055. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  51056. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  51057. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  51058. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  51059. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  51060. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  51061. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  51062. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  51063. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  51064. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  51065. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  51066. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  51067. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  51068. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  51069. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  51070. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  51071. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  51072. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  51073. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  51074. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  51075. };
  51076. ExpectNotNull(bn = get_rfc3526_prime_1536(NULL));
  51077. ExpectIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  51078. ExpectIntEQ(0, XMEMCMP(expected, bits, sz));
  51079. BN_free(bn);
  51080. #endif
  51081. return EXPECT_RESULT();
  51082. }
  51083. static int test_wolfSSL_DH_get_2048_256(void)
  51084. {
  51085. EXPECT_DECLS;
  51086. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  51087. WOLFSSL_DH* dh = NULL;
  51088. const WOLFSSL_BIGNUM* pBn;
  51089. const WOLFSSL_BIGNUM* gBn;
  51090. const WOLFSSL_BIGNUM* qBn;
  51091. const byte pExpected[] = {
  51092. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  51093. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  51094. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  51095. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  51096. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  51097. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  51098. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  51099. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  51100. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  51101. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  51102. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  51103. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  51104. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  51105. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  51106. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  51107. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  51108. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  51109. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  51110. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  51111. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  51112. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  51113. 0x1E, 0x1A, 0x15, 0x97
  51114. };
  51115. const byte gExpected[] = {
  51116. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  51117. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  51118. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  51119. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  51120. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  51121. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  51122. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  51123. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  51124. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  51125. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  51126. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  51127. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  51128. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  51129. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  51130. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  51131. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  51132. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  51133. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  51134. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  51135. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  51136. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  51137. 0x6C, 0xC4, 0x16, 0x59
  51138. };
  51139. const byte qExpected[] = {
  51140. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  51141. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  51142. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  51143. };
  51144. int pSz;
  51145. int qSz;
  51146. int gSz;
  51147. byte* pReturned = NULL;
  51148. byte* qReturned = NULL;
  51149. byte* gReturned = NULL;
  51150. ExpectNotNull((dh = wolfSSL_DH_get_2048_256()));
  51151. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  51152. ExpectIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  51153. ExpectNotNull(pReturned = (byte*)XMALLOC(pSz, NULL,
  51154. DYNAMIC_TYPE_TMP_BUFFER));
  51155. ExpectIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  51156. ExpectIntEQ(pSz, sizeof(pExpected));
  51157. ExpectIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  51158. ExpectIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  51159. ExpectNotNull(qReturned = (byte*)XMALLOC(qSz, NULL,
  51160. DYNAMIC_TYPE_TMP_BUFFER));
  51161. ExpectIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  51162. ExpectIntEQ(qSz, sizeof(qExpected));
  51163. ExpectIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  51164. ExpectIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  51165. ExpectNotNull(gReturned = (byte*)XMALLOC(gSz, NULL,
  51166. DYNAMIC_TYPE_TMP_BUFFER));
  51167. ExpectIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  51168. ExpectIntEQ(gSz, sizeof(gExpected));
  51169. ExpectIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  51170. wolfSSL_DH_free(dh);
  51171. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51172. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51173. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51174. #endif
  51175. return EXPECT_RESULT();
  51176. }
  51177. static int test_wolfSSL_PEM_write_DHparams(void)
  51178. {
  51179. EXPECT_DECLS;
  51180. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  51181. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  51182. DH* dh = NULL;
  51183. BIO* bio = NULL;
  51184. XFILE fp = XBADFILE;
  51185. byte pem[2048];
  51186. int pemSz = 0;
  51187. const char expected[] =
  51188. "-----BEGIN DH PARAMETERS-----\n"
  51189. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  51190. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  51191. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  51192. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  51193. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  51194. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  51195. "-----END DH PARAMETERS-----\n";
  51196. const char badPem[] =
  51197. "-----BEGIN DH PARAMETERS-----\n"
  51198. "-----END DH PARAMETERS-----\n";
  51199. const char emptySeqPem[] =
  51200. "-----BEGIN DH PARAMETERS-----\n"
  51201. "MAA=\n"
  51202. "-----END DH PARAMETERS-----\n";
  51203. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  51204. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  51205. if (fp != XBADFILE) {
  51206. XFCLOSE(fp);
  51207. fp = XBADFILE;
  51208. }
  51209. ExpectNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  51210. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  51211. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51212. ExpectIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  51213. (int)sizeof(badPem));
  51214. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51215. BIO_free(bio);
  51216. bio = NULL;
  51217. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  51218. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51219. ExpectIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  51220. (int)sizeof(emptySeqPem));
  51221. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51222. BIO_free(bio);
  51223. bio = NULL;
  51224. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  51225. ExpectIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  51226. ExpectNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51227. BIO_free(bio);
  51228. bio = NULL;
  51229. ExpectNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  51230. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  51231. ExpectIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  51232. DH_free(dh);
  51233. dh = NULL;
  51234. dh = wolfSSL_DH_new();
  51235. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  51236. if (fp != XBADFILE) {
  51237. XFCLOSE(fp);
  51238. fp = XBADFILE;
  51239. }
  51240. wolfSSL_DH_free(dh);
  51241. dh = NULL;
  51242. /* check results */
  51243. XMEMSET(pem, 0, sizeof(pem));
  51244. ExpectTrue((fp = XFOPEN("./test-write-dhparams.pem", "rb")) != XBADFILE);
  51245. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  51246. ExpectIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  51247. if (fp != XBADFILE)
  51248. XFCLOSE(fp);
  51249. #endif
  51250. return EXPECT_RESULT();
  51251. }
  51252. static int test_wolfSSL_d2i_DHparams(void)
  51253. {
  51254. EXPECT_DECLS;
  51255. #ifdef OPENSSL_ALL
  51256. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  51257. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  51258. XFILE f = XBADFILE;
  51259. unsigned char buf[4096];
  51260. const unsigned char* pt = buf;
  51261. #ifdef HAVE_FFDHE_2048
  51262. const char* params1 = "./certs/dh2048.der";
  51263. #endif
  51264. #ifdef HAVE_FFDHE_3072
  51265. const char* params2 = "./certs/dh3072.der";
  51266. #endif
  51267. long len = 0;
  51268. WOLFSSL_DH* dh = NULL;
  51269. XMEMSET(buf, 0, sizeof(buf));
  51270. /* Test 2048 bit parameters */
  51271. #ifdef HAVE_FFDHE_2048
  51272. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  51273. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  51274. if (f != XBADFILE) {
  51275. XFCLOSE(f);
  51276. f = XBADFILE;
  51277. }
  51278. /* Valid case */
  51279. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  51280. ExpectNotNull(dh->p);
  51281. ExpectNotNull(dh->g);
  51282. ExpectTrue(pt == buf);
  51283. ExpectIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  51284. ExpectIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  51285. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  51286. /* Invalid cases */
  51287. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  51288. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  51289. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  51290. DH_free(dh);
  51291. dh = NULL;
  51292. *buf = 0;
  51293. pt = buf;
  51294. #endif /* HAVE_FFDHE_2048 */
  51295. /* Test 3072 bit parameters */
  51296. #ifdef HAVE_FFDHE_3072
  51297. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  51298. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  51299. if (f != XBADFILE) {
  51300. XFCLOSE(f);
  51301. f = XBADFILE;
  51302. }
  51303. /* Valid case */
  51304. ExpectNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
  51305. ExpectNotNull(dh->p);
  51306. ExpectNotNull(dh->g);
  51307. ExpectTrue(pt != buf);
  51308. ExpectIntEQ(DH_generate_key(dh), 1);
  51309. /* Invalid cases */
  51310. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  51311. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  51312. DH_free(dh);
  51313. dh = NULL;
  51314. #endif /* HAVE_FFDHE_3072 */
  51315. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  51316. #endif /* !NO_DH */
  51317. #endif
  51318. return EXPECT_RESULT();
  51319. }
  51320. static int test_wolfSSL_DH_LoadDer(void)
  51321. {
  51322. EXPECT_DECLS;
  51323. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  51324. defined(OPENSSL_EXTRA)
  51325. static const byte dh2048[] = {
  51326. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  51327. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  51328. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  51329. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  51330. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  51331. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  51332. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  51333. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  51334. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  51335. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  51336. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  51337. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  51338. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  51339. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  51340. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  51341. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  51342. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  51343. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  51344. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  51345. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  51346. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  51347. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  51348. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  51349. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  51350. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  51351. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  51352. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  51353. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  51354. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  51355. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  51356. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  51357. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  51358. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  51359. 0x93, 0x02, 0x01, 0x02
  51360. };
  51361. WOLFSSL_DH* dh = NULL;
  51362. ExpectNotNull(dh = wolfSSL_DH_new());
  51363. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  51364. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  51365. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  51366. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  51367. wolfSSL_DH_free(dh);
  51368. #endif
  51369. return EXPECT_RESULT();
  51370. }
  51371. static int test_wolfSSL_i2d_DHparams(void)
  51372. {
  51373. EXPECT_DECLS;
  51374. #ifdef OPENSSL_ALL
  51375. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  51376. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  51377. XFILE f = XBADFILE;
  51378. unsigned char buf[4096];
  51379. const unsigned char* pt;
  51380. unsigned char* pt2;
  51381. #ifdef HAVE_FFDHE_2048
  51382. const char* params1 = "./certs/dh2048.der";
  51383. #endif
  51384. #ifdef HAVE_FFDHE_3072
  51385. const char* params2 = "./certs/dh3072.der";
  51386. #endif
  51387. long len;
  51388. WOLFSSL_DH* dh = NULL;
  51389. /* Test 2048 bit parameters */
  51390. #ifdef HAVE_FFDHE_2048
  51391. pt = buf;
  51392. pt2 = buf;
  51393. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  51394. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  51395. if (f != XBADFILE) {
  51396. XFCLOSE(f);
  51397. f = XBADFILE;
  51398. }
  51399. /* Valid case */
  51400. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  51401. ExpectTrue(pt == buf);
  51402. ExpectIntEQ(DH_generate_key(dh), 1);
  51403. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  51404. /* Invalid case */
  51405. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  51406. /* Return length only */
  51407. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  51408. DH_free(dh);
  51409. *buf = 0;
  51410. #endif
  51411. /* Test 3072 bit parameters */
  51412. #ifdef HAVE_FFDHE_3072
  51413. pt = buf;
  51414. pt2 = buf;
  51415. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  51416. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  51417. if (f != XBADFILE) {
  51418. XFCLOSE(f);
  51419. f = XBADFILE;
  51420. }
  51421. /* Valid case */
  51422. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  51423. ExpectTrue(pt == buf);
  51424. ExpectIntEQ(DH_generate_key(dh), 1);
  51425. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  51426. /* Invalid case */
  51427. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  51428. /* Return length only */
  51429. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  51430. DH_free(dh);
  51431. #endif
  51432. dh = DH_new();
  51433. ExpectNotNull(dh);
  51434. pt2 = buf;
  51435. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  51436. DH_free(dh);
  51437. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  51438. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  51439. #endif
  51440. return EXPECT_RESULT();
  51441. }
  51442. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  51443. /*----------------------------------------------------------------------------*
  51444. | EC
  51445. *----------------------------------------------------------------------------*/
  51446. static int test_wolfSSL_EC_GROUP(void)
  51447. {
  51448. EXPECT_DECLS;
  51449. #ifdef OPENSSL_EXTRA
  51450. EC_GROUP *group = NULL;
  51451. EC_GROUP *group2 = NULL;
  51452. EC_GROUP *group3 = NULL;
  51453. #ifndef HAVE_ECC_BRAINPOOL
  51454. EC_GROUP *group4 = NULL;
  51455. #endif
  51456. WOLFSSL_BIGNUM* order = NULL;
  51457. int group_bits;
  51458. int i;
  51459. static const int knownEccNids[] = {
  51460. NID_X9_62_prime192v1,
  51461. NID_X9_62_prime192v2,
  51462. NID_X9_62_prime192v3,
  51463. NID_X9_62_prime239v1,
  51464. NID_X9_62_prime239v2,
  51465. NID_X9_62_prime239v3,
  51466. NID_X9_62_prime256v1,
  51467. NID_secp112r1,
  51468. NID_secp112r2,
  51469. NID_secp128r1,
  51470. NID_secp128r2,
  51471. NID_secp160r1,
  51472. NID_secp160r2,
  51473. NID_secp224r1,
  51474. NID_secp384r1,
  51475. NID_secp521r1,
  51476. NID_secp160k1,
  51477. NID_secp192k1,
  51478. NID_secp224k1,
  51479. NID_secp256k1,
  51480. NID_brainpoolP160r1,
  51481. NID_brainpoolP192r1,
  51482. NID_brainpoolP224r1,
  51483. NID_brainpoolP256r1,
  51484. NID_brainpoolP320r1,
  51485. NID_brainpoolP384r1,
  51486. NID_brainpoolP512r1,
  51487. };
  51488. int knowEccNidsLen = (int)(sizeof(knownEccNids) / sizeof(*knownEccNids));
  51489. static const int knownEccEnums[] = {
  51490. ECC_SECP192R1,
  51491. ECC_PRIME192V2,
  51492. ECC_PRIME192V3,
  51493. ECC_PRIME239V1,
  51494. ECC_PRIME239V2,
  51495. ECC_PRIME239V3,
  51496. ECC_SECP256R1,
  51497. ECC_SECP112R1,
  51498. ECC_SECP112R2,
  51499. ECC_SECP128R1,
  51500. ECC_SECP128R2,
  51501. ECC_SECP160R1,
  51502. ECC_SECP160R2,
  51503. ECC_SECP224R1,
  51504. ECC_SECP384R1,
  51505. ECC_SECP521R1,
  51506. ECC_SECP160K1,
  51507. ECC_SECP192K1,
  51508. ECC_SECP224K1,
  51509. ECC_SECP256K1,
  51510. ECC_BRAINPOOLP160R1,
  51511. ECC_BRAINPOOLP192R1,
  51512. ECC_BRAINPOOLP224R1,
  51513. ECC_BRAINPOOLP256R1,
  51514. ECC_BRAINPOOLP320R1,
  51515. ECC_BRAINPOOLP384R1,
  51516. ECC_BRAINPOOLP512R1,
  51517. };
  51518. int knowEccEnumsLen = (int)(sizeof(knownEccEnums) / sizeof(*knownEccEnums));
  51519. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  51520. ExpectNotNull(group2 = EC_GROUP_dup(group));
  51521. ExpectNotNull(group3 = wolfSSL_EC_GROUP_new_by_curve_name(NID_secp384r1));
  51522. #ifndef HAVE_ECC_BRAINPOOL
  51523. ExpectNotNull(group4 = wolfSSL_EC_GROUP_new_by_curve_name(
  51524. NID_brainpoolP256r1));
  51525. #endif
  51526. ExpectNull(EC_GROUP_dup(NULL));
  51527. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(NULL), 0);
  51528. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  51529. ExpectIntEQ((group_bits = EC_GROUP_order_bits(NULL)), 0);
  51530. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  51531. #ifndef HAVE_ECC_BRAINPOOL
  51532. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group4)), 0);
  51533. #endif
  51534. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(NULL), 0);
  51535. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(group), 256);
  51536. ExpectNotNull(order = BN_new());
  51537. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, NULL, NULL), 0);
  51538. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, NULL, NULL), 0);
  51539. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, order, NULL), 0);
  51540. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, order, NULL), 1);
  51541. wolfSSL_BN_free(order);
  51542. ExpectNotNull(EC_GROUP_method_of(group));
  51543. ExpectIntEQ(EC_METHOD_get_field_type(NULL), 0);
  51544. ExpectIntEQ(EC_METHOD_get_field_type(EC_GROUP_method_of(group)),
  51545. NID_X9_62_prime_field);
  51546. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, NULL, NULL), -1);
  51547. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, NULL, NULL), -1);
  51548. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, group, NULL), -1);
  51549. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, group3, NULL), 1);
  51550. #ifndef NO_WOLFSSL_STUB
  51551. wolfSSL_EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  51552. #endif
  51553. #ifndef HAVE_ECC_BRAINPOOL
  51554. EC_GROUP_free(group4);
  51555. #endif
  51556. EC_GROUP_free(group3);
  51557. EC_GROUP_free(group2);
  51558. EC_GROUP_free(group);
  51559. for (i = 0; i < knowEccNidsLen; i++) {
  51560. group = NULL;
  51561. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccNids[i]));
  51562. ExpectIntGT(wolfSSL_EC_GROUP_get_degree(group), 0);
  51563. EC_GROUP_free(group);
  51564. }
  51565. for (i = 0; i < knowEccEnumsLen; i++) {
  51566. group = NULL;
  51567. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccEnums[i]));
  51568. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), knownEccNids[i]);
  51569. EC_GROUP_free(group);
  51570. }
  51571. #endif
  51572. return EXPECT_RESULT();
  51573. }
  51574. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  51575. {
  51576. EXPECT_DECLS;
  51577. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  51578. EC_GROUP *group = NULL;
  51579. BIO* bio = NULL;
  51580. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  51581. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  51582. EC_GROUP *ret = NULL;
  51583. static char ec_nc_p384[] = "-----BEGIN EC PARAMETERS-----\n"
  51584. "BgUrgQQAIg==\n"
  51585. "-----END EC PARAMETERS-----";
  51586. #endif
  51587. static char ec_nc_bad_1[] = "-----BEGIN EC PARAMETERS-----\n"
  51588. "MAA=\n"
  51589. "-----END EC PARAMETERS-----";
  51590. static char ec_nc_bad_2[] = "-----BEGIN EC PARAMETERS-----\n"
  51591. "BgA=\n"
  51592. "-----END EC PARAMETERS-----";
  51593. static char ec_nc_bad_3[] = "-----BEGIN EC PARAMETERS-----\n"
  51594. "BgE=\n"
  51595. "-----END EC PARAMETERS-----";
  51596. static char ec_nc_bad_4[] = "-----BEGIN EC PARAMETERS-----\n"
  51597. "BgE*\n"
  51598. "-----END EC PARAMETERS-----";
  51599. /* Test that first parameter, bio, being NULL fails. */
  51600. ExpectNull(PEM_read_bio_ECPKParameters(NULL, NULL, NULL, NULL));
  51601. /* Test that reading named parameters works. */
  51602. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  51603. ExpectIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  51604. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  51605. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  51606. BIO_free(bio);
  51607. bio = NULL;
  51608. EC_GROUP_free(group);
  51609. group = NULL;
  51610. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  51611. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  51612. /* Test that reusing group works. */
  51613. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  51614. sizeof(ec_nc_p384)));
  51615. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  51616. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  51617. BIO_free(bio);
  51618. bio = NULL;
  51619. EC_GROUP_free(group);
  51620. group = NULL;
  51621. /* Test that returning through group works. */
  51622. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  51623. sizeof(ec_nc_p384)));
  51624. ExpectNotNull(ret = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  51625. ExpectIntEQ(group == ret, 1);
  51626. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  51627. BIO_free(bio);
  51628. bio = NULL;
  51629. EC_GROUP_free(group);
  51630. group = NULL;
  51631. #endif
  51632. /* Test 0x30, 0x00 (not and object id) fails. */
  51633. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_1,
  51634. sizeof(ec_nc_bad_1)));
  51635. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  51636. BIO_free(bio);
  51637. bio = NULL;
  51638. /* Test 0x06, 0x00 (empty object id) fails. */
  51639. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_2,
  51640. sizeof(ec_nc_bad_2)));
  51641. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  51642. BIO_free(bio);
  51643. bio = NULL;
  51644. /* Test 0x06, 0x01 (badly formed object id) fails. */
  51645. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_3,
  51646. sizeof(ec_nc_bad_3)));
  51647. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  51648. BIO_free(bio);
  51649. bio = NULL;
  51650. /* Test invalid PEM encoding - invalid character. */
  51651. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_4,
  51652. sizeof(ec_nc_bad_4)));
  51653. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  51654. BIO_free(bio);
  51655. #endif
  51656. return EXPECT_RESULT();
  51657. }
  51658. static int test_wolfSSL_EC_POINT(void)
  51659. {
  51660. EXPECT_DECLS;
  51661. #if !defined(WOLFSSL_SP_MATH) && \
  51662. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  51663. #ifdef OPENSSL_EXTRA
  51664. BN_CTX* ctx = NULL;
  51665. EC_GROUP* group = NULL;
  51666. #ifndef HAVE_ECC_BRAINPOOL
  51667. EC_GROUP* group2 = NULL;
  51668. #endif
  51669. EC_POINT* Gxy = NULL;
  51670. EC_POINT* new_point = NULL;
  51671. EC_POINT* set_point = NULL;
  51672. EC_POINT* infinity = NULL;
  51673. BIGNUM* k = NULL;
  51674. BIGNUM* Gx = NULL;
  51675. BIGNUM* Gy = NULL;
  51676. BIGNUM* Gz = NULL;
  51677. BIGNUM* X = NULL;
  51678. BIGNUM* Y = NULL;
  51679. BIGNUM* set_point_bn = NULL;
  51680. char* hexStr = NULL;
  51681. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD"
  51682. "17AF381913FF7A96314EA47055EA0FD0";
  51683. /* NISTP256R1 Gx/Gy */
  51684. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F2"
  51685. "77037D812DEB33A0F4A13945D898C296";
  51686. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  51687. "2BCE33576B315ECECBB6406837BF51F5";
  51688. #ifndef HAVE_SELFTEST
  51689. EC_POINT *tmp = NULL;
  51690. size_t bin_len;
  51691. unsigned int blen = 0;
  51692. unsigned char* buf = NULL;
  51693. unsigned char bufInf[1] = { 0x00 };
  51694. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F2"
  51695. "77037D812DEB33A0F4A13945D898C296"
  51696. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  51697. "2BCE33576B315ECECBB6406837BF51F5";
  51698. const unsigned char binUncompG[] = {
  51699. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  51700. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  51701. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  51702. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  51703. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  51704. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  51705. };
  51706. const unsigned char binUncompGBad[] = {
  51707. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  51708. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  51709. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  51710. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  51711. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  51712. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  51713. };
  51714. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F2"
  51715. "77037D812DEB33A0F4A13945D898C296";
  51716. #ifdef HAVE_COMP_KEY
  51717. const unsigned char binCompG[] = {
  51718. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  51719. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  51720. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  51721. };
  51722. #endif
  51723. #endif
  51724. ExpectNotNull(ctx = BN_CTX_new());
  51725. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  51726. #ifndef HAVE_ECC_BRAINPOOL
  51727. /* Used to make groups curve_idx == -1. */
  51728. ExpectNotNull(group2 = EC_GROUP_new_by_curve_name(NID_brainpoolP256r1));
  51729. #endif
  51730. ExpectNull(EC_POINT_new(NULL));
  51731. ExpectNotNull(Gxy = EC_POINT_new(group));
  51732. ExpectNotNull(new_point = EC_POINT_new(group));
  51733. ExpectNotNull(set_point = EC_POINT_new(group));
  51734. ExpectNotNull(X = BN_new());
  51735. ExpectNotNull(Y = BN_new());
  51736. ExpectNotNull(set_point_bn = BN_new());
  51737. ExpectNotNull(infinity = EC_POINT_new(group));
  51738. /* load test values */
  51739. ExpectIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  51740. ExpectIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  51741. ExpectIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  51742. ExpectIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  51743. /* populate coordinates for input point */
  51744. if (Gxy != NULL) {
  51745. Gxy->X = Gx;
  51746. Gxy->Y = Gy;
  51747. Gxy->Z = Gz;
  51748. }
  51749. /* Test handling of NULL point. */
  51750. EC_POINT_clear_free(NULL);
  51751. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  51752. NULL, NULL, ctx), 0);
  51753. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  51754. NULL, NULL, ctx), 0);
  51755. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  51756. NULL, NULL, ctx), 0);
  51757. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  51758. X, NULL, ctx), 0);
  51759. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  51760. NULL, Y, ctx), 0);
  51761. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  51762. X, Y, ctx), 0);
  51763. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  51764. X, Y, ctx), 0);
  51765. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  51766. NULL, Y, ctx), 0);
  51767. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  51768. X, NULL, ctx), 0);
  51769. /* Getting point at infinity returns an error. */
  51770. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, infinity,
  51771. X, Y, ctx), 0);
  51772. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  51773. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  51774. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  51775. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, NULL, ctx), 0);
  51776. ExpectIntEQ(EC_POINT_add(group, NULL, NULL, NULL, ctx), 0);
  51777. ExpectIntEQ(EC_POINT_add(NULL, new_point, NULL, NULL, ctx), 0);
  51778. ExpectIntEQ(EC_POINT_add(NULL, NULL, new_point, NULL, ctx), 0);
  51779. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, Gxy, ctx), 0);
  51780. ExpectIntEQ(EC_POINT_add(NULL, new_point, new_point, Gxy, ctx), 0);
  51781. ExpectIntEQ(EC_POINT_add(group, NULL, new_point, Gxy, ctx), 0);
  51782. ExpectIntEQ(EC_POINT_add(group, new_point, NULL, Gxy, ctx), 0);
  51783. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, NULL, ctx), 0);
  51784. ExpectIntEQ(EC_POINT_mul(NULL, NULL, Gx, Gxy, k, ctx), 0);
  51785. ExpectIntEQ(EC_POINT_mul(NULL, new_point, Gx, Gxy, k, ctx), 0);
  51786. ExpectIntEQ(EC_POINT_mul(group, NULL, Gx, Gxy, k, ctx), 0);
  51787. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), 1);
  51788. /* perform point multiplication */
  51789. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), 1);
  51790. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  51791. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  51792. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  51793. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), 1);
  51794. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  51795. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  51796. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  51797. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), 1);
  51798. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  51799. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  51800. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  51801. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, NULL, NULL, ctx), 1);
  51802. ExpectIntEQ(BN_is_zero(new_point->X), 1);
  51803. ExpectIntEQ(BN_is_zero(new_point->Y), 1);
  51804. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  51805. /* Set point to something. */
  51806. ExpectIntEQ(EC_POINT_add(group, new_point, Gxy, Gxy, ctx), 1);
  51807. #else
  51808. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy,
  51809. ctx), 1);
  51810. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  51811. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  51812. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  51813. #endif
  51814. /* check if point X coordinate is zero */
  51815. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  51816. #if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
  51817. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  51818. ExpectIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  51819. #endif
  51820. /* extract the coordinates from point */
  51821. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  51822. ctx), WOLFSSL_SUCCESS);
  51823. /* check if point X coordinate is zero */
  51824. ExpectIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  51825. /* set the same X and Y points in another object */
  51826. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y,
  51827. ctx), WOLFSSL_SUCCESS);
  51828. /* compare points as they should be the same */
  51829. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, NULL, ctx), -1);
  51830. ExpectIntEQ(EC_POINT_cmp(group, NULL, NULL, ctx), -1);
  51831. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, NULL, ctx), -1);
  51832. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, set_point, ctx), -1);
  51833. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, set_point, ctx), -1);
  51834. ExpectIntEQ(EC_POINT_cmp(group, NULL, set_point, ctx), -1);
  51835. ExpectIntEQ(EC_POINT_cmp(group, new_point, NULL, ctx), -1);
  51836. ExpectIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  51837. /* Test copying */
  51838. ExpectIntEQ(EC_POINT_copy(NULL, NULL), 0);
  51839. ExpectIntEQ(EC_POINT_copy(NULL, set_point), 0);
  51840. ExpectIntEQ(EC_POINT_copy(new_point, NULL), 0);
  51841. ExpectIntEQ(EC_POINT_copy(new_point, set_point), 1);
  51842. /* Test inverting */
  51843. ExpectIntEQ(EC_POINT_invert(NULL, NULL, ctx), 0);
  51844. ExpectIntEQ(EC_POINT_invert(NULL, new_point, ctx), 0);
  51845. ExpectIntEQ(EC_POINT_invert(group, NULL, ctx), 0);
  51846. ExpectIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  51847. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  51848. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  51849. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  51850. {
  51851. EC_POINT* orig_point = NULL;
  51852. ExpectNotNull(orig_point = EC_POINT_new(group));
  51853. ExpectIntEQ(EC_POINT_add(group, orig_point, set_point, set_point, NULL),
  51854. 1);
  51855. /* new_point should be set_point inverted so adding it will revert
  51856. * the point back to set_point */
  51857. ExpectIntEQ(EC_POINT_add(group, orig_point, orig_point, new_point,
  51858. NULL), 1);
  51859. ExpectIntEQ(EC_POINT_cmp(group, orig_point, set_point, NULL), 0);
  51860. EC_POINT_free(orig_point);
  51861. }
  51862. #endif
  51863. /* Test getting affine converts from projective. */
  51864. ExpectIntEQ(EC_POINT_copy(set_point, new_point), 1);
  51865. /* Force non-affine coordinates */
  51866. ExpectIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  51867. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  51868. if (new_point != NULL) {
  51869. new_point->inSet = 0;
  51870. }
  51871. /* extract the coordinates from point */
  51872. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  51873. ctx), WOLFSSL_SUCCESS);
  51874. /* check if point ordinates have changed. */
  51875. ExpectIntNE(BN_cmp(X, set_point->X), 0);
  51876. ExpectIntNE(BN_cmp(Y, set_point->Y), 0);
  51877. /* Test check for infinity */
  51878. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  51879. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, NULL), 0);
  51880. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, infinity), 0);
  51881. ExpectIntEQ(EC_POINT_is_at_infinity(group, NULL), 0);
  51882. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 1);
  51883. ExpectIntEQ(EC_POINT_is_at_infinity(group, Gxy), 0);
  51884. #else
  51885. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 0);
  51886. #endif
  51887. ExpectPtrEq(EC_POINT_point2bn(group, set_point,
  51888. POINT_CONVERSION_UNCOMPRESSED, set_point_bn, ctx), set_point_bn);
  51889. /* check bn2hex */
  51890. hexStr = BN_bn2hex(k);
  51891. ExpectStrEQ(hexStr, kTest);
  51892. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  51893. BN_print_fp(stderr, k);
  51894. fprintf(stderr, "\n");
  51895. #endif
  51896. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  51897. hexStr = BN_bn2hex(Gx);
  51898. ExpectStrEQ(hexStr, kGx);
  51899. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  51900. BN_print_fp(stderr, Gx);
  51901. fprintf(stderr, "\n");
  51902. #endif
  51903. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  51904. hexStr = BN_bn2hex(Gy);
  51905. ExpectStrEQ(hexStr, kGy);
  51906. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  51907. BN_print_fp(stderr, Gy);
  51908. fprintf(stderr, "\n");
  51909. #endif
  51910. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  51911. #ifndef HAVE_SELFTEST
  51912. /* Test point to hex */
  51913. ExpectNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  51914. ctx));
  51915. ExpectNull(EC_POINT_point2hex(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  51916. ctx));
  51917. ExpectNull(EC_POINT_point2hex(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  51918. ctx));
  51919. #ifndef HAVE_ECC_BRAINPOOL
  51920. /* Group not supported in wolfCrypt. */
  51921. ExpectNull(EC_POINT_point2hex(group2, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  51922. ctx));
  51923. #endif
  51924. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  51925. ExpectNotNull(hexStr);
  51926. ExpectStrEQ(hexStr, uncompG);
  51927. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  51928. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  51929. ExpectNotNull(hexStr);
  51930. ExpectStrEQ(hexStr, compG);
  51931. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  51932. /* Test point to oct */
  51933. ExpectIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  51934. NULL, 0, ctx), 0);
  51935. ExpectIntEQ(EC_POINT_point2oct(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  51936. NULL, 0, ctx), 0);
  51937. ExpectIntEQ(EC_POINT_point2oct(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  51938. NULL, 0, ctx), 0);
  51939. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  51940. NULL, 0, ctx);
  51941. ExpectIntEQ(bin_len, sizeof(binUncompG));
  51942. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  51943. DYNAMIC_TYPE_ECC));
  51944. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  51945. buf, bin_len, ctx), bin_len);
  51946. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  51947. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  51948. /* Infinity (x=0, y=0) encodes as '0x00'. */
  51949. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  51950. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), 1);
  51951. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  51952. POINT_CONVERSION_UNCOMPRESSED, bufInf, 0, ctx), 0);
  51953. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  51954. POINT_CONVERSION_UNCOMPRESSED, bufInf, 1, ctx), 1);
  51955. ExpectIntEQ(bufInf[0], 0);
  51956. wolfSSL_EC_POINT_dump(NULL, NULL);
  51957. /* Test point i2d */
  51958. ExpectIntEQ(ECPoint_i2d(NULL, NULL, NULL, &blen), 0);
  51959. ExpectIntEQ(ECPoint_i2d(NULL, Gxy, NULL, &blen), 0);
  51960. ExpectIntEQ(ECPoint_i2d(group, NULL, NULL, &blen), 0);
  51961. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, NULL), 0);
  51962. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, &blen), 1);
  51963. ExpectIntEQ(blen, sizeof(binUncompG));
  51964. ExpectNotNull(buf = (unsigned char*)XMALLOC(blen, NULL, DYNAMIC_TYPE_ECC));
  51965. blen -= 1;
  51966. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 0);
  51967. blen += 1;
  51968. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 1);
  51969. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  51970. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  51971. #ifdef HAVE_COMP_KEY
  51972. /* Test point to oct compressed */
  51973. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL,
  51974. 0, ctx);
  51975. ExpectIntEQ(bin_len, sizeof(binCompG));
  51976. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  51977. DYNAMIC_TYPE_ECC));
  51978. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  51979. bin_len, ctx), bin_len);
  51980. ExpectIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  51981. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  51982. #endif
  51983. /* Test point BN */
  51984. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, NULL,
  51985. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  51986. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, Gxy,
  51987. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  51988. ExpectNull(wolfSSL_EC_POINT_point2bn(group, NULL,
  51989. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  51990. ExpectNull(wolfSSL_EC_POINT_point2bn(group, Gxy, 0, NULL, ctx));
  51991. /* Test oct to point */
  51992. ExpectNotNull(tmp = EC_POINT_new(group));
  51993. ExpectIntEQ(EC_POINT_oct2point(NULL, NULL, binUncompG, sizeof(binUncompG),
  51994. ctx), 0);
  51995. ExpectIntEQ(EC_POINT_oct2point(NULL, tmp, binUncompG, sizeof(binUncompG),
  51996. ctx), 0);
  51997. ExpectIntEQ(EC_POINT_oct2point(group, NULL, binUncompG, sizeof(binUncompG),
  51998. ctx), 0);
  51999. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompGBad,
  52000. sizeof(binUncompGBad), ctx), 0);
  52001. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG),
  52002. ctx), 1);
  52003. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  52004. EC_POINT_free(tmp);
  52005. tmp = NULL;
  52006. /* Test setting BN ordinates. */
  52007. ExpectNotNull(tmp = EC_POINT_new(group));
  52008. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  52009. NULL, ctx), 0);
  52010. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, NULL,
  52011. NULL, ctx), 0);
  52012. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, NULL,
  52013. NULL, ctx), 0);
  52014. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, Gx,
  52015. NULL, ctx), 0);
  52016. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  52017. Gy, ctx), 0);
  52018. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, Gx, Gy,
  52019. ctx), 0);
  52020. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, Gx, Gy,
  52021. ctx), 0);
  52022. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, NULL,
  52023. Gy, ctx), 0);
  52024. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx,
  52025. NULL, ctx), 0);
  52026. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx, Gy,
  52027. ctx), 1);
  52028. EC_POINT_free(tmp);
  52029. tmp = NULL;
  52030. /* Test point d2i */
  52031. ExpectNotNull(tmp = EC_POINT_new(group));
  52032. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, NULL), 0);
  52033. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, NULL), 0);
  52034. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, NULL), 0);
  52035. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, tmp), 0);
  52036. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, tmp), 0);
  52037. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, tmp), 0);
  52038. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, NULL), 0);
  52039. ExpectIntEQ(ECPoint_d2i(binUncompGBad, sizeof(binUncompG), group, tmp), 0);
  52040. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, tmp), 1);
  52041. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  52042. EC_POINT_free(tmp);
  52043. tmp = NULL;
  52044. #ifdef HAVE_COMP_KEY
  52045. /* Test oct compressed to point */
  52046. ExpectNotNull(tmp = EC_POINT_new(group));
  52047. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx),
  52048. 1);
  52049. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  52050. EC_POINT_free(tmp);
  52051. tmp = NULL;
  52052. /* Test point d2i - compressed */
  52053. ExpectNotNull(tmp = EC_POINT_new(group));
  52054. ExpectIntEQ(ECPoint_d2i(binCompG, sizeof(binCompG), group, tmp), 1);
  52055. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  52056. EC_POINT_free(tmp);
  52057. tmp = NULL;
  52058. #endif
  52059. #endif
  52060. /* test BN_mod_add */
  52061. ExpectIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  52062. (WOLFSSL_BIGNUM*)BN_value_one(), (WOLFSSL_BIGNUM*)BN_value_one(), NULL),
  52063. 1);
  52064. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  52065. /* cleanup */
  52066. BN_free(X);
  52067. BN_free(Y);
  52068. BN_free(k);
  52069. BN_free(set_point_bn);
  52070. EC_POINT_free(infinity);
  52071. EC_POINT_free(new_point);
  52072. EC_POINT_free(set_point);
  52073. EC_POINT_clear_free(Gxy);
  52074. #ifndef HAVE_ECC_BRAINPOOL
  52075. EC_GROUP_free(group2);
  52076. #endif
  52077. EC_GROUP_free(group);
  52078. BN_CTX_free(ctx);
  52079. #endif
  52080. #endif /* !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  52081. return EXPECT_RESULT();
  52082. }
  52083. static int test_wolfSSL_SPAKE(void)
  52084. {
  52085. EXPECT_DECLS;
  52086. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(WOLFSSL_ATECC508A) \
  52087. && !defined(WOLFSSL_ATECC608A) && !defined(HAVE_SELFTEST) && \
  52088. !defined(WOLFSSL_SP_MATH) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  52089. BIGNUM* x = NULL; /* kdc priv */
  52090. BIGNUM* y = NULL; /* client priv */
  52091. BIGNUM* w = NULL; /* shared value */
  52092. byte M_bytes[] = {
  52093. /* uncompressed */
  52094. 0x04,
  52095. /* x */
  52096. 0x88, 0x6e, 0x2f, 0x97, 0xac, 0xe4, 0x6e, 0x55, 0xba, 0x9d, 0xd7, 0x24,
  52097. 0x25, 0x79, 0xf2, 0x99, 0x3b, 0x64, 0xe1, 0x6e, 0xf3, 0xdc, 0xab, 0x95,
  52098. 0xaf, 0xd4, 0x97, 0x33, 0x3d, 0x8f, 0xa1, 0x2f,
  52099. /* y */
  52100. 0x5f, 0xf3, 0x55, 0x16, 0x3e, 0x43, 0xce, 0x22, 0x4e, 0x0b, 0x0e, 0x65,
  52101. 0xff, 0x02, 0xac, 0x8e, 0x5c, 0x7b, 0xe0, 0x94, 0x19, 0xc7, 0x85, 0xe0,
  52102. 0xca, 0x54, 0x7d, 0x55, 0xa1, 0x2e, 0x2d, 0x20
  52103. };
  52104. EC_POINT* M = NULL; /* shared value */
  52105. byte N_bytes[] = {
  52106. /* uncompressed */
  52107. 0x04,
  52108. /* x */
  52109. 0xd8, 0xbb, 0xd6, 0xc6, 0x39, 0xc6, 0x29, 0x37, 0xb0, 0x4d, 0x99, 0x7f,
  52110. 0x38, 0xc3, 0x77, 0x07, 0x19, 0xc6, 0x29, 0xd7, 0x01, 0x4d, 0x49, 0xa2,
  52111. 0x4b, 0x4f, 0x98, 0xba, 0xa1, 0x29, 0x2b, 0x49,
  52112. /* y */
  52113. 0x07, 0xd6, 0x0a, 0xa6, 0xbf, 0xad, 0xe4, 0x50, 0x08, 0xa6, 0x36, 0x33,
  52114. 0x7f, 0x51, 0x68, 0xc6, 0x4d, 0x9b, 0xd3, 0x60, 0x34, 0x80, 0x8c, 0xd5,
  52115. 0x64, 0x49, 0x0b, 0x1e, 0x65, 0x6e, 0xdb, 0xe7
  52116. };
  52117. EC_POINT* N = NULL; /* shared value */
  52118. EC_POINT* T = NULL; /* kdc pub */
  52119. EC_POINT* tmp1 = NULL; /* kdc pub */
  52120. EC_POINT* tmp2 = NULL; /* kdc pub */
  52121. EC_POINT* S = NULL; /* client pub */
  52122. EC_POINT* client_secret = NULL;
  52123. EC_POINT* kdc_secret = NULL;
  52124. EC_GROUP* group = NULL;
  52125. BN_CTX* bn_ctx = NULL;
  52126. /* Values taken from a test run of Kerberos 5 */
  52127. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  52128. ExpectNotNull(bn_ctx = BN_CTX_new());
  52129. ExpectNotNull(M = EC_POINT_new(group));
  52130. ExpectNotNull(N = EC_POINT_new(group));
  52131. ExpectNotNull(T = EC_POINT_new(group));
  52132. ExpectNotNull(tmp1 = EC_POINT_new(group));
  52133. ExpectNotNull(tmp2 = EC_POINT_new(group));
  52134. ExpectNotNull(S = EC_POINT_new(group));
  52135. ExpectNotNull(client_secret = EC_POINT_new(group));
  52136. ExpectNotNull(kdc_secret = EC_POINT_new(group));
  52137. ExpectIntEQ(BN_hex2bn(&x, "DAC3027CD692B4BDF0EDFE9B7D0E4E7"
  52138. "E5D8768A725EAEEA6FC68EC239A17C0"), 1);
  52139. ExpectIntEQ(BN_hex2bn(&y, "6F6A1D394E26B1655A54B26DCE30D49"
  52140. "90CC47EBE08F809EF3FF7F6AEAABBB5"), 1);
  52141. ExpectIntEQ(BN_hex2bn(&w, "1D992AB8BA851B9BA05353453D81EE9"
  52142. "506AB395478F0AAB647752CF117B36250"), 1);
  52143. ExpectIntEQ(EC_POINT_oct2point(group, M, M_bytes, sizeof(M_bytes), bn_ctx),
  52144. 1);
  52145. ExpectIntEQ(EC_POINT_oct2point(group, N, N_bytes, sizeof(N_bytes), bn_ctx),
  52146. 1);
  52147. /* Function pattern similar to ossl_keygen and ossl_result in krb5 */
  52148. /* kdc */
  52149. /* T=x*P+w*M */
  52150. /* All in one function call */
  52151. ExpectIntEQ(EC_POINT_mul(group, T, x, M, w, bn_ctx), 1);
  52152. /* Spread into separate calls */
  52153. ExpectIntEQ(EC_POINT_mul(group, tmp1, x, NULL, NULL, bn_ctx), 1);
  52154. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, M, w, bn_ctx), 1);
  52155. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  52156. 1);
  52157. ExpectIntEQ(EC_POINT_cmp(group, T, tmp1, bn_ctx), 0);
  52158. /* client */
  52159. /* S=y*P+w*N */
  52160. /* All in one function call */
  52161. ExpectIntEQ(EC_POINT_mul(group, S, y, N, w, bn_ctx), 1);
  52162. /* Spread into separate calls */
  52163. ExpectIntEQ(EC_POINT_mul(group, tmp1, y, NULL, NULL, bn_ctx), 1);
  52164. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, N, w, bn_ctx), 1);
  52165. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  52166. 1);
  52167. ExpectIntEQ(EC_POINT_cmp(group, S, tmp1, bn_ctx), 0);
  52168. /* K=y*(T-w*M) */
  52169. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, M, w, bn_ctx), 1);
  52170. ExpectIntEQ(EC_POINT_invert(group, client_secret, bn_ctx), 1);
  52171. ExpectIntEQ(EC_POINT_add(group, client_secret, T, client_secret, bn_ctx),
  52172. 1);
  52173. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, client_secret, y,
  52174. bn_ctx), 1);
  52175. /* kdc */
  52176. /* K=x*(S-w*N) */
  52177. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, N, w, bn_ctx), 1);
  52178. ExpectIntEQ(EC_POINT_invert(group, kdc_secret, bn_ctx), 1);
  52179. ExpectIntEQ(EC_POINT_add(group, kdc_secret, S, kdc_secret, bn_ctx),
  52180. 1);
  52181. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, kdc_secret, x, bn_ctx),
  52182. 1);
  52183. /* kdc_secret == client_secret */
  52184. ExpectIntEQ(EC_POINT_cmp(group, client_secret, kdc_secret, bn_ctx), 0);
  52185. BN_free(x);
  52186. BN_free(y);
  52187. BN_free(w);
  52188. EC_POINT_free(M);
  52189. EC_POINT_free(N);
  52190. EC_POINT_free(T);
  52191. EC_POINT_free(tmp1);
  52192. EC_POINT_free(tmp2);
  52193. EC_POINT_free(S);
  52194. EC_POINT_free(client_secret);
  52195. EC_POINT_free(kdc_secret);
  52196. EC_GROUP_free(group);
  52197. BN_CTX_free(bn_ctx);
  52198. #endif
  52199. return EXPECT_RESULT();
  52200. }
  52201. static int test_wolfSSL_EC_KEY_generate(void)
  52202. {
  52203. EXPECT_DECLS;
  52204. #ifdef OPENSSL_EXTRA
  52205. WOLFSSL_EC_KEY* key = NULL;
  52206. #ifndef HAVE_ECC_BRAINPOOL
  52207. WOLFSSL_EC_GROUP* group = NULL;
  52208. #endif
  52209. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52210. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(NULL), 0);
  52211. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  52212. wolfSSL_EC_KEY_free(key);
  52213. key = NULL;
  52214. #ifndef HAVE_ECC_BRAINPOOL
  52215. ExpectNotNull(group = wolfSSL_EC_GROUP_new_by_curve_name(
  52216. NID_brainpoolP256r1));
  52217. ExpectNotNull(key = wolfSSL_EC_KEY_new());
  52218. ExpectIntEQ(wolfSSL_EC_KEY_set_group(key, group), 1);
  52219. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 0);
  52220. wolfSSL_EC_KEY_free(key);
  52221. wolfSSL_EC_GROUP_free(group);
  52222. #endif
  52223. #endif
  52224. return EXPECT_RESULT();
  52225. }
  52226. static int test_EC_i2d(void)
  52227. {
  52228. EXPECT_DECLS;
  52229. #if defined(OPENSSL_EXTRA) && !defined(HAVE_FIPS)
  52230. EC_KEY *key = NULL;
  52231. EC_KEY *copy = NULL;
  52232. int len = 0;
  52233. unsigned char *buf = NULL;
  52234. unsigned char *p = NULL;
  52235. const unsigned char *tmp = NULL;
  52236. const unsigned char octBad[] = {
  52237. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  52238. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  52239. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  52240. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  52241. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  52242. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  52243. };
  52244. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52245. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  52246. ExpectIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  52247. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  52248. DYNAMIC_TYPE_TMP_BUFFER));
  52249. p = buf;
  52250. ExpectIntEQ(i2d_EC_PUBKEY(key, &p), len);
  52251. ExpectNull(o2i_ECPublicKey(NULL, NULL, -1));
  52252. ExpectNull(o2i_ECPublicKey(&copy, NULL, -1));
  52253. ExpectNull(o2i_ECPublicKey(&key, NULL, -1));
  52254. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  52255. ExpectNull(o2i_ECPublicKey(NULL, NULL, 0));
  52256. ExpectNull(o2i_ECPublicKey(&key, NULL, 0));
  52257. ExpectNull(o2i_ECPublicKey(&key, &tmp, 0));
  52258. tmp = buf;
  52259. ExpectNull(o2i_ECPublicKey(NULL, &tmp, 0));
  52260. ExpectNull(o2i_ECPublicKey(&copy, &tmp, 0));
  52261. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  52262. ExpectNull(o2i_ECPublicKey(&key, &tmp, -1));
  52263. ExpectIntEQ(i2o_ECPublicKey(NULL, NULL), 0);
  52264. ExpectIntEQ(i2o_ECPublicKey(NULL, &buf), 0);
  52265. tmp = buf;
  52266. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 0));
  52267. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 1));
  52268. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 0));
  52269. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 1));
  52270. ExpectNull(d2i_ECPrivateKey(&key, &tmp, 0));
  52271. ExpectIntEQ(i2d_ECPrivateKey(NULL, &p), 0);
  52272. ExpectIntEQ(i2d_ECPrivateKey(NULL, NULL), 0);
  52273. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer(NULL, NULL, -1), -1);
  52274. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1, 0), -1);
  52275. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, -1, 0), -1);
  52276. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, -1, 0), -1);
  52277. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, 0, 0), -1);
  52278. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1,
  52279. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  52280. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, len,
  52281. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  52282. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, len,
  52283. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  52284. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, -1,
  52285. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  52286. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len, 0), -1);
  52287. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len,
  52288. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  52289. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  52290. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  52291. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  52292. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  52293. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52294. buf = NULL;
  52295. buf = NULL;
  52296. ExpectIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  52297. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  52298. DYNAMIC_TYPE_TMP_BUFFER));
  52299. p = buf;
  52300. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  52301. p = NULL;
  52302. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  52303. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52304. p = NULL;
  52305. /* Bad point is also an invalid private key. */
  52306. tmp = octBad;
  52307. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, sizeof(octBad)));
  52308. tmp = buf;
  52309. ExpectNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  52310. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52311. buf = NULL;
  52312. buf = NULL;
  52313. ExpectIntGT((len = i2o_ECPublicKey(key, NULL)), 0);
  52314. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  52315. DYNAMIC_TYPE_TMP_BUFFER));
  52316. p = buf;
  52317. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  52318. p = NULL;
  52319. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  52320. tmp = buf;
  52321. ExpectNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  52322. tmp = octBad;
  52323. ExpectNull(o2i_ECPublicKey(&key, &tmp, sizeof(octBad)));
  52324. ExpectIntEQ(EC_KEY_check_key(NULL), 0);
  52325. ExpectIntEQ(EC_KEY_check_key(key), 1);
  52326. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  52327. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  52328. EC_KEY_free(key);
  52329. EC_KEY_free(copy);
  52330. #endif
  52331. return EXPECT_RESULT();
  52332. }
  52333. static int test_wolfSSL_EC_curve(void)
  52334. {
  52335. EXPECT_DECLS;
  52336. #if defined(OPENSSL_EXTRA)
  52337. int nid = NID_secp160k1;
  52338. const char* nid_name = NULL;
  52339. ExpectNull(EC_curve_nid2nist(NID_sha256));
  52340. ExpectNotNull(nid_name = EC_curve_nid2nist(nid));
  52341. ExpectIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  52342. ExpectIntEQ(EC_curve_nist2nid("INVALID"), 0);
  52343. ExpectIntEQ(EC_curve_nist2nid(nid_name), nid);
  52344. #endif
  52345. return EXPECT_RESULT();
  52346. }
  52347. static int test_wolfSSL_EC_KEY_dup(void)
  52348. {
  52349. EXPECT_DECLS;
  52350. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  52351. WOLFSSL_EC_KEY* ecKey = NULL;
  52352. WOLFSSL_EC_KEY* dupKey = NULL;
  52353. ecc_key* srcKey = NULL;
  52354. ecc_key* destKey = NULL;
  52355. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  52356. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  52357. /* Valid cases */
  52358. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52359. ExpectIntEQ(EC_KEY_check_key(dupKey), 1);
  52360. /* Compare pubkey */
  52361. if (ecKey != NULL) {
  52362. srcKey = (ecc_key*)ecKey->internal;
  52363. }
  52364. if (dupKey != NULL) {
  52365. destKey = (ecc_key*)dupKey->internal;
  52366. }
  52367. ExpectIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  52368. /* compare EC_GROUP */
  52369. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  52370. /* compare EC_POINT */
  52371. ExpectIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  52372. dupKey->pub_key, NULL), MP_EQ);
  52373. /* compare BIGNUM */
  52374. ExpectIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  52375. wolfSSL_EC_KEY_free(dupKey);
  52376. dupKey = NULL;
  52377. /* Invalid cases */
  52378. /* NULL key */
  52379. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  52380. /* NULL ecc_key */
  52381. if (ecKey != NULL) {
  52382. wc_ecc_free((ecc_key*)ecKey->internal);
  52383. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  52384. ecKey->internal = NULL; /* Set ecc_key to NULL */
  52385. }
  52386. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52387. wolfSSL_EC_KEY_free(ecKey);
  52388. ecKey = NULL;
  52389. wolfSSL_EC_KEY_free(dupKey);
  52390. dupKey = NULL;
  52391. /* NULL Group */
  52392. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  52393. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  52394. if (ecKey != NULL) {
  52395. wolfSSL_EC_GROUP_free(ecKey->group);
  52396. ecKey->group = NULL; /* Set group to NULL */
  52397. }
  52398. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52399. wolfSSL_EC_KEY_free(ecKey);
  52400. ecKey = NULL;
  52401. wolfSSL_EC_KEY_free(dupKey);
  52402. dupKey = NULL;
  52403. /* NULL public key */
  52404. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  52405. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  52406. if (ecKey != NULL) {
  52407. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  52408. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  52409. }
  52410. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52411. if (ecKey != NULL) {
  52412. wolfSSL_EC_POINT_free(ecKey->pub_key);
  52413. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  52414. }
  52415. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52416. wolfSSL_EC_KEY_free(ecKey);
  52417. ecKey = NULL;
  52418. wolfSSL_EC_KEY_free(dupKey);
  52419. dupKey = NULL;
  52420. /* NULL private key */
  52421. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  52422. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  52423. if (ecKey != NULL) {
  52424. wolfSSL_BN_free(ecKey->priv_key);
  52425. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  52426. }
  52427. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52428. wolfSSL_EC_KEY_free(ecKey);
  52429. ecKey = NULL;
  52430. wolfSSL_EC_KEY_free(dupKey);
  52431. dupKey = NULL;
  52432. /* Test EC_KEY_up_ref */
  52433. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  52434. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  52435. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  52436. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  52437. /* reference count doesn't follow duplicate */
  52438. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52439. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  52440. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  52441. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  52442. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  52443. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  52444. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  52445. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  52446. #endif
  52447. return EXPECT_RESULT();
  52448. }
  52449. static int test_wolfSSL_EC_KEY_set_group(void)
  52450. {
  52451. EXPECT_DECLS;
  52452. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  52453. defined(OPENSSL_EXTRA)
  52454. EC_KEY *key = NULL;
  52455. EC_GROUP *group = NULL;
  52456. const EC_GROUP *group2 = NULL;
  52457. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  52458. ExpectNotNull(key = EC_KEY_new());
  52459. ExpectNull(EC_KEY_get0_group(NULL));
  52460. ExpectIntEQ(EC_KEY_set_group(NULL, NULL), 0);
  52461. ExpectIntEQ(EC_KEY_set_group(key, NULL), 0);
  52462. ExpectIntEQ(EC_KEY_set_group(NULL, group), 0);
  52463. ExpectIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  52464. ExpectNotNull(group2 = EC_KEY_get0_group(key));
  52465. ExpectIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  52466. EC_GROUP_free(group);
  52467. EC_KEY_free(key);
  52468. #endif
  52469. return EXPECT_RESULT();
  52470. }
  52471. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  52472. {
  52473. EXPECT_DECLS;
  52474. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  52475. BIO* bio = NULL;
  52476. EC_KEY* key = NULL;
  52477. /* Error condition: NULL key. */
  52478. ExpectIntLT(EC_KEY_get_conv_form(NULL), 0);
  52479. ExpectNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  52480. ExpectNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  52481. /* Conversion form defaults to uncompressed. */
  52482. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  52483. #ifdef HAVE_COMP_KEY
  52484. /* Explicitly set to compressed. */
  52485. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  52486. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  52487. #else
  52488. /* Will still work just won't change anything. */
  52489. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  52490. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  52491. EC_KEY_set_conv_form(key, POINT_CONVERSION_UNCOMPRESSED);
  52492. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  52493. #endif
  52494. EC_KEY_set_conv_form(NULL, POINT_CONVERSION_UNCOMPRESSED);
  52495. BIO_free(bio);
  52496. EC_KEY_free(key);
  52497. #endif
  52498. return EXPECT_RESULT();
  52499. }
  52500. static int test_wolfSSL_EC_KEY_private_key(void)
  52501. {
  52502. EXPECT_DECLS;
  52503. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  52504. WOLFSSL_EC_KEY* key = NULL;
  52505. WOLFSSL_BIGNUM* priv = NULL;
  52506. WOLFSSL_BIGNUM* priv2 = NULL;
  52507. WOLFSSL_BIGNUM* bn;
  52508. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52509. ExpectNotNull(priv = wolfSSL_BN_new());
  52510. ExpectNotNull(priv2 = wolfSSL_BN_new());
  52511. ExpectIntNE(BN_set_word(priv, 2), 0);
  52512. ExpectIntNE(BN_set_word(priv2, 2), 0);
  52513. ExpectNull(wolfSSL_EC_KEY_get0_private_key(NULL));
  52514. /* No private key set. */
  52515. ExpectNull(wolfSSL_EC_KEY_get0_private_key(key));
  52516. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, NULL), 0);
  52517. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, NULL), 0);
  52518. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, priv), 0);
  52519. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv), 1);
  52520. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  52521. ExpectPtrNE(bn, priv);
  52522. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv2), 1);
  52523. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  52524. ExpectPtrNE(bn, priv2);
  52525. wolfSSL_BN_free(priv2);
  52526. wolfSSL_BN_free(priv);
  52527. wolfSSL_EC_KEY_free(key);
  52528. #endif
  52529. return EXPECT_RESULT();
  52530. }
  52531. static int test_wolfSSL_EC_KEY_public_key(void)
  52532. {
  52533. EXPECT_DECLS;
  52534. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  52535. WOLFSSL_EC_KEY* key = NULL;
  52536. WOLFSSL_EC_POINT* pub = NULL;
  52537. WOLFSSL_EC_POINT* point = NULL;
  52538. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52539. ExpectNull(wolfSSL_EC_KEY_get0_public_key(NULL));
  52540. ExpectNotNull(wolfSSL_EC_KEY_get0_public_key(key));
  52541. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  52542. ExpectNotNull(pub = wolfSSL_EC_KEY_get0_public_key(key));
  52543. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, NULL), 0);
  52544. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, NULL), 0);
  52545. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, pub), 0);
  52546. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, pub), 1);
  52547. ExpectNotNull(point = wolfSSL_EC_KEY_get0_public_key(key));
  52548. ExpectPtrEq(point, pub);
  52549. wolfSSL_EC_KEY_free(key);
  52550. #endif
  52551. return EXPECT_RESULT();
  52552. }
  52553. static int test_wolfSSL_EC_KEY_print_fp(void)
  52554. {
  52555. EXPECT_DECLS;
  52556. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  52557. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
  52558. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  52559. !defined(NO_STDIO_FILESYSTEM)
  52560. EC_KEY* key = NULL;
  52561. /* Bad file pointer. */
  52562. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  52563. /* NULL key. */
  52564. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
  52565. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  52566. /* Negative indent. */
  52567. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
  52568. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  52569. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  52570. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  52571. wolfSSL_EC_KEY_free(key);
  52572. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  52573. NID_X9_62_prime256v1)));
  52574. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  52575. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  52576. wolfSSL_EC_KEY_free(key);
  52577. #endif
  52578. return EXPECT_RESULT();
  52579. }
  52580. static int test_wolfSSL_EC_get_builtin_curves(void)
  52581. {
  52582. EXPECT_DECLS;
  52583. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  52584. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  52585. EC_builtin_curve* curves = NULL;
  52586. size_t crv_len = 0;
  52587. size_t i = 0;
  52588. ExpectIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  52589. ExpectNotNull(curves = (EC_builtin_curve*)XMALLOC(
  52590. sizeof(EC_builtin_curve) * crv_len, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  52591. ExpectIntEQ((EC_get_builtin_curves(curves, 0)), crv_len);
  52592. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  52593. for (i = 0; EXPECT_SUCCESS() && (i < crv_len); i++) {
  52594. if (curves[i].comment != NULL) {
  52595. ExpectStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  52596. }
  52597. }
  52598. if (crv_len > 1) {
  52599. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len - 1), crv_len - 1);
  52600. }
  52601. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52602. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  52603. #endif /* OPENSSL_EXTRA || OPENSSL_ALL */
  52604. return EXPECT_RESULT();
  52605. }
  52606. static int test_wolfSSL_ECDSA_SIG(void)
  52607. {
  52608. EXPECT_DECLS;
  52609. #ifdef OPENSSL_EXTRA
  52610. WOLFSSL_ECDSA_SIG* sig = NULL;
  52611. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  52612. WOLFSSL_BIGNUM* r = NULL;
  52613. WOLFSSL_BIGNUM* s = NULL;
  52614. const WOLFSSL_BIGNUM* r2 = NULL;
  52615. const WOLFSSL_BIGNUM* s2 = NULL;
  52616. const unsigned char* cp = NULL;
  52617. unsigned char* p = NULL;
  52618. unsigned char outSig[8];
  52619. unsigned char sigData[8] =
  52620. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  52621. unsigned char sigDataBad[8] =
  52622. { 0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  52623. wolfSSL_ECDSA_SIG_free(NULL);
  52624. ExpectNotNull(sig = wolfSSL_ECDSA_SIG_new());
  52625. ExpectNotNull(r = wolfSSL_BN_new());
  52626. ExpectNotNull(s = wolfSSL_BN_new());
  52627. ExpectIntEQ(wolfSSL_BN_set_word(r, 1), 1);
  52628. ExpectIntEQ(wolfSSL_BN_set_word(s, 1), 1);
  52629. wolfSSL_ECDSA_SIG_get0(NULL, NULL, NULL);
  52630. wolfSSL_ECDSA_SIG_get0(NULL, &r2, NULL);
  52631. wolfSSL_ECDSA_SIG_get0(NULL, NULL, &s2);
  52632. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  52633. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, NULL), 0);
  52634. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, NULL), 0);
  52635. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, NULL), 0);
  52636. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, s), 0);
  52637. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, s), 0);
  52638. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, s), 0);
  52639. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, NULL), 0);
  52640. r2 = NULL;
  52641. s2 = NULL;
  52642. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  52643. ExpectNull(r2);
  52644. ExpectNull(s2);
  52645. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, s), 1);
  52646. if (EXPECT_FAIL()) {
  52647. wolfSSL_BN_free(r);
  52648. wolfSSL_BN_free(s);
  52649. }
  52650. wolfSSL_ECDSA_SIG_get0(sig, &r2, &s2);
  52651. ExpectPtrEq(r2, r);
  52652. ExpectPtrEq(s2, s);
  52653. r2 = NULL;
  52654. wolfSSL_ECDSA_SIG_get0(sig, &r2, NULL);
  52655. ExpectPtrEq(r2, r);
  52656. s2 = NULL;
  52657. wolfSSL_ECDSA_SIG_get0(sig, NULL, &s2);
  52658. ExpectPtrEq(s2, s);
  52659. /* r and s are freed when sig is freed. */
  52660. wolfSSL_ECDSA_SIG_free(sig);
  52661. sig = NULL;
  52662. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData)));
  52663. cp = sigDataBad;
  52664. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigDataBad)));
  52665. cp = sigData;
  52666. ExpectNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  52667. ExpectIntEQ((cp == sigData + 8), 1);
  52668. cp = sigData;
  52669. ExpectNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  52670. ExpectNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  52671. ExpectIntEQ((sig == sig2), 1);
  52672. cp = outSig;
  52673. p = outSig;
  52674. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  52675. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  52676. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  52677. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  52678. ExpectIntEQ((p == outSig + 8), 1);
  52679. ExpectIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  52680. wolfSSL_ECDSA_SIG_free(sig);
  52681. #endif
  52682. return EXPECT_RESULT();
  52683. }
  52684. static int test_ECDSA_size_sign(void)
  52685. {
  52686. EXPECT_DECLS;
  52687. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  52688. EC_KEY* key = NULL;
  52689. ECDSA_SIG* ecdsaSig = NULL;
  52690. int id;
  52691. byte hash[WC_MAX_DIGEST_SIZE];
  52692. byte hash2[WC_MAX_DIGEST_SIZE];
  52693. byte sig[ECC_MAX_SIG_SIZE];
  52694. unsigned int sigSz = sizeof(sig);
  52695. XMEMSET(hash, 123, sizeof(hash));
  52696. XMEMSET(hash2, 234, sizeof(hash2));
  52697. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  52698. ExpectIntEQ(id, ECC_SECP256R1);
  52699. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52700. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  52701. ExpectIntGE(ECDSA_size(NULL), 0);
  52702. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, NULL), 0);
  52703. ExpectIntEQ(ECDSA_sign(0, NULL, sizeof(hash), sig, &sigSz, key), 0);
  52704. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), NULL, &sigSz, key), 0);
  52705. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, NULL), 0);
  52706. ExpectIntEQ(ECDSA_verify(0, NULL, sizeof(hash), sig, sigSz, key), 0);
  52707. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), NULL, sigSz, key), 0);
  52708. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  52709. ExpectIntGE(ECDSA_size(key), sigSz);
  52710. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
  52711. ExpectIntEQ(ECDSA_verify(0, hash2, sizeof(hash2), sig, sigSz, key), 0);
  52712. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), NULL));
  52713. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), key));
  52714. ExpectNull(ECDSA_do_sign(hash, sizeof(hash), NULL));
  52715. ExpectNotNull(ecdsaSig = ECDSA_do_sign(hash, sizeof(hash), key));
  52716. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, NULL), -1);
  52717. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, NULL), -1);
  52718. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, NULL), -1);
  52719. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, key), -1);
  52720. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, key), -1);
  52721. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, key), -1);
  52722. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, NULL), -1);
  52723. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, key), 1);
  52724. ExpectIntEQ(ECDSA_do_verify(hash2, sizeof(hash2), ecdsaSig, key), 0);
  52725. ECDSA_SIG_free(ecdsaSig);
  52726. EC_KEY_free(key);
  52727. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP */
  52728. return EXPECT_RESULT();
  52729. }
  52730. static int test_ECDH_compute_key(void)
  52731. {
  52732. EXPECT_DECLS;
  52733. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  52734. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  52735. EC_KEY* key1 = NULL;
  52736. EC_KEY* key2 = NULL;
  52737. EC_POINT* pub1 = NULL;
  52738. EC_POINT* pub2 = NULL;
  52739. byte secret1[32];
  52740. byte secret2[32];
  52741. int i;
  52742. ExpectNotNull(key1 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52743. ExpectIntEQ(EC_KEY_generate_key(key1), 1);
  52744. ExpectNotNull(pub1 = wolfSSL_EC_KEY_get0_public_key(key1));
  52745. ExpectNotNull(key2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52746. ExpectIntEQ(EC_KEY_generate_key(key2), 1);
  52747. ExpectNotNull(pub2 = wolfSSL_EC_KEY_get0_public_key(key2));
  52748. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, NULL, NULL), 0);
  52749. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, NULL, NULL),
  52750. 0);
  52751. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, NULL, NULL), 0);
  52752. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, key1, NULL), 0);
  52753. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, key1, NULL), 0);
  52754. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, key1, NULL),
  52755. 0);
  52756. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, NULL, NULL),
  52757. 0);
  52758. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1) - 16, pub2, key1,
  52759. NULL), 0);
  52760. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, key1, NULL),
  52761. sizeof(secret1));
  52762. ExpectIntEQ(ECDH_compute_key(secret2, sizeof(secret2), pub1, key2, NULL),
  52763. sizeof(secret2));
  52764. for (i = 0; i < (int)sizeof(secret1); i++) {
  52765. ExpectIntEQ(secret1[i], secret2[i]);
  52766. }
  52767. EC_KEY_free(key2);
  52768. EC_KEY_free(key1);
  52769. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP &&
  52770. * !WOLF_CRYPTO_CB_ONLY_ECC */
  52771. return EXPECT_RESULT();
  52772. }
  52773. #endif /* HAVE_ECC && !OPENSSL_NO_PK */
  52774. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  52775. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  52776. !defined(NO_ASN_TIME)
  52777. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey,
  52778. int expectedDerSz)
  52779. {
  52780. EXPECT_DECLS;
  52781. X509* x509 = NULL;
  52782. BIGNUM* serial_number = NULL;
  52783. X509_NAME* name = NULL;
  52784. time_t epoch_off = 0;
  52785. ASN1_INTEGER* asn1_serial_number;
  52786. long not_before, not_after;
  52787. int derSz;
  52788. ExpectNotNull(x509 = X509_new());
  52789. ExpectIntNE(X509_set_pubkey(x509, pkey), 0);
  52790. ExpectNotNull(serial_number = BN_new());
  52791. ExpectIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  52792. ExpectNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  52793. ExpectNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  52794. /* version 3 */
  52795. ExpectIntNE(X509_set_version(x509, 2L), 0);
  52796. ExpectNotNull(name = X509_NAME_new());
  52797. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  52798. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  52799. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_pkcs9_contentType,
  52800. MBSTRING_UTF8,(unsigned char*)"Server", -1, -1, 0), 0);
  52801. ExpectIntNE(X509_set_subject_name(x509, name), 0);
  52802. ExpectIntNE(X509_set_issuer_name(x509, name), 0);
  52803. not_before = (long)wc_Time(NULL);
  52804. not_after = not_before + (365 * 24 * 60 * 60);
  52805. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before,
  52806. &epoch_off));
  52807. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after,
  52808. &epoch_off));
  52809. ExpectIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  52810. ExpectNotNull(wolfSSL_X509_get_der(x509, &derSz));
  52811. ExpectIntGE(derSz, expectedDerSz);
  52812. BN_free(serial_number);
  52813. X509_NAME_free(name);
  52814. X509_free(x509);
  52815. return EXPECT_RESULT();
  52816. }
  52817. #endif
  52818. static int test_openssl_generate_key_and_cert(void)
  52819. {
  52820. EXPECT_DECLS;
  52821. #if defined(OPENSSL_EXTRA)
  52822. int expectedDerSz;
  52823. EVP_PKEY* pkey = NULL;
  52824. #ifdef HAVE_ECC
  52825. EC_KEY* ec_key = NULL;
  52826. #endif
  52827. #if !defined(NO_RSA)
  52828. int key_length = 2048;
  52829. BIGNUM* exponent = NULL;
  52830. RSA* rsa = NULL;
  52831. ExpectNotNull(pkey = EVP_PKEY_new());
  52832. ExpectNotNull(exponent = BN_new());
  52833. ExpectNotNull(rsa = RSA_new());
  52834. ExpectIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  52835. #ifndef WOLFSSL_KEY_GEN
  52836. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  52837. #if defined(USE_CERT_BUFFERS_1024)
  52838. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  52839. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  52840. key_length = 1024;
  52841. #elif defined(USE_CERT_BUFFERS_2048)
  52842. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  52843. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  52844. #else
  52845. RSA_free(rsa);
  52846. rsa = NULL;
  52847. #endif
  52848. #else
  52849. ExpectIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  52850. ExpectIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  52851. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  52852. ExpectIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  52853. #endif
  52854. if (rsa) {
  52855. ExpectIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  52856. if (EXPECT_FAIL()) {
  52857. RSA_free(rsa);
  52858. }
  52859. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  52860. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  52861. expectedDerSz = 743;
  52862. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey,
  52863. expectedDerSz), TEST_SUCCESS);
  52864. #endif
  52865. }
  52866. EVP_PKEY_free(pkey);
  52867. pkey = NULL;
  52868. BN_free(exponent);
  52869. #endif /* !NO_RSA */
  52870. #ifdef HAVE_ECC
  52871. ExpectNotNull(pkey = EVP_PKEY_new());
  52872. ExpectNotNull(ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52873. #ifndef NO_WOLFSSL_STUB
  52874. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  52875. #endif
  52876. ExpectIntNE(EC_KEY_generate_key(ec_key), 0);
  52877. ExpectIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  52878. if (EXPECT_FAIL()) {
  52879. EC_KEY_free(ec_key);
  52880. }
  52881. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  52882. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  52883. expectedDerSz = 344;
  52884. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey, expectedDerSz),
  52885. TEST_SUCCESS);
  52886. #endif
  52887. EVP_PKEY_free(pkey);
  52888. #endif /* HAVE_ECC */
  52889. (void)pkey;
  52890. (void)expectedDerSz;
  52891. #endif /* OPENSSL_EXTRA */
  52892. return EXPECT_RESULT();
  52893. }
  52894. static int test_stubs_are_stubs(void)
  52895. {
  52896. EXPECT_DECLS;
  52897. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB) && \
  52898. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  52899. WOLFSSL_CTX* ctx = NULL;
  52900. WOLFSSL_CTX* ctxN = NULL;
  52901. #ifndef NO_WOLFSSL_CLIENT
  52902. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  52903. #elif !defined(NO_WOLFSSL_SERVER)
  52904. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  52905. #endif
  52906. #define CHECKZERO_RET(x, y, z) ExpectIntEQ((int) x(y), 0); \
  52907. ExpectIntEQ((int) x(z), 0)
  52908. /* test logic, all stubs return same result regardless of ctx being NULL
  52909. * as there are no sanity checks, it's just a stub! If at some
  52910. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  52911. * if invalid inputs are supplied. Test calling both
  52912. * with and without valid inputs, if a stub functionality remains unchanged.
  52913. */
  52914. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  52915. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  52916. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  52917. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  52918. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  52919. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  52920. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  52921. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  52922. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  52923. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  52924. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  52925. wolfSSL_CTX_free(ctx);
  52926. ctx = NULL;
  52927. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB && (!NO_WOLFSSL_CLIENT ||
  52928. * !NO_WOLFSSL_SERVER) */
  52929. return EXPECT_RESULT();
  52930. }
  52931. static int test_CONF_modules_xxx(void)
  52932. {
  52933. int res = TEST_SKIPPED;
  52934. #if defined(OPENSSL_EXTRA)
  52935. CONF_modules_free();
  52936. CONF_modules_unload(0);
  52937. CONF_modules_unload(1);
  52938. CONF_modules_unload(-1);
  52939. res = TEST_SUCCESS;
  52940. #endif /* OPENSSL_EXTRA */
  52941. return res;
  52942. }
  52943. static int test_CRYPTO_set_dynlock_xxx(void)
  52944. {
  52945. int res = TEST_SKIPPED;
  52946. #if defined(OPENSSL_EXTRA)
  52947. CRYPTO_set_dynlock_create_callback(
  52948. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  52949. CRYPTO_set_dynlock_create_callback(
  52950. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  52951. CRYPTO_set_dynlock_destroy_callback(
  52952. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  52953. CRYPTO_set_dynlock_destroy_callback(
  52954. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  52955. CRYPTO_set_dynlock_lock_callback(
  52956. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  52957. CRYPTO_set_dynlock_lock_callback(
  52958. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  52959. res = TEST_SUCCESS;
  52960. #endif /* OPENSSL_EXTRA */
  52961. return res;
  52962. }
  52963. static int test_CRYPTO_THREADID_xxx(void)
  52964. {
  52965. EXPECT_DECLS;
  52966. #if defined(OPENSSL_EXTRA)
  52967. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  52968. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  52969. ExpectIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  52970. #endif /* OPENSSL_EXTRA */
  52971. return EXPECT_RESULT();
  52972. }
  52973. static int test_ENGINE_cleanup(void)
  52974. {
  52975. int res = TEST_SKIPPED;
  52976. #if defined(OPENSSL_EXTRA)
  52977. ENGINE_cleanup();
  52978. res = TEST_SUCCESS;
  52979. #endif /* OPENSSL_EXTRA */
  52980. return res;
  52981. }
  52982. static int test_wolfSSL_CTX_LoadCRL(void)
  52983. {
  52984. EXPECT_DECLS;
  52985. #if defined(HAVE_CRL) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  52986. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  52987. WOLFSSL_CTX* ctx = NULL;
  52988. WOLFSSL* ssl = NULL;
  52989. const char* badPath = "dummypath";
  52990. const char* validPath = "./certs/crl";
  52991. const char* validFilePath = "./certs/crl/cliCrl.pem";
  52992. const char* issuerCert = "./certs/client-cert.pem";
  52993. int derType = WOLFSSL_FILETYPE_ASN1;
  52994. int pemType = WOLFSSL_FILETYPE_PEM;
  52995. #ifdef HAVE_CRL_MONITOR
  52996. int monitor = WOLFSSL_CRL_MONITOR;
  52997. #else
  52998. int monitor = 0;
  52999. #endif
  53000. WOLFSSL_CERT_MANAGER* cm = NULL;
  53001. #define FAIL_T1(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  53002. BAD_FUNC_ARG)
  53003. #define FAIL_T2(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  53004. NOT_COMPILED_IN)
  53005. #define SUCC_T(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  53006. WOLFSSL_SUCCESS)
  53007. #ifndef NO_WOLFSSL_CLIENT
  53008. #define NEW_CTX(ctx) ExpectNotNull( \
  53009. (ctx) = wolfSSL_CTX_new(wolfSSLv23_client_method()))
  53010. #elif !defined(NO_WOLFSSL_SERVER)
  53011. #define NEW_CTX(ctx) ExpectNotNull( \
  53012. (ctx) = wolfSSL_CTX_new(wolfSSLv23_server_method()))
  53013. #else
  53014. #define NEW_CTX(ctx) return
  53015. #endif
  53016. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  53017. NEW_CTX(ctx);
  53018. #ifndef HAVE_CRL_MONITOR
  53019. FAIL_T2(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, WOLFSSL_CRL_MONITOR);
  53020. wolfSSL_CTX_free(ctx);
  53021. NEW_CTX(ctx);
  53022. #endif
  53023. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  53024. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  53025. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  53026. wolfSSL_CTX_free(ctx);
  53027. ctx = NULL;
  53028. NEW_CTX(ctx);
  53029. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  53030. WOLFSSL_SUCCESS);
  53031. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  53032. wolfSSL_CTX_free(ctx);
  53033. ctx = NULL;
  53034. NEW_CTX(ctx);
  53035. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  53036. WOLFSSL_SUCCESS);
  53037. ExpectNotNull(ssl = wolfSSL_new(ctx));
  53038. ExpectIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  53039. wolfSSL_free(ssl);
  53040. ssl = NULL;
  53041. wolfSSL_CTX_free(ctx);
  53042. ctx = NULL;
  53043. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  53044. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  53045. WOLFSSL_SUCCESS);
  53046. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType),
  53047. WOLFSSL_SUCCESS);
  53048. wolfSSL_CertManagerFree(cm);
  53049. #endif
  53050. return EXPECT_RESULT();
  53051. }
  53052. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  53053. static int test_multiple_crls_same_issuer_ctx_ready(WOLFSSL_CTX* ctx)
  53054. {
  53055. EXPECT_DECLS;
  53056. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  53057. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, "./certs/crl/crl.pem",
  53058. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  53059. return EXPECT_RESULT();
  53060. }
  53061. #endif
  53062. static int test_multiple_crls_same_issuer(void)
  53063. {
  53064. EXPECT_DECLS;
  53065. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  53066. test_ssl_cbf client_cbs, server_cbs;
  53067. struct {
  53068. const char* server_cert;
  53069. const char* server_key;
  53070. } test_params[] = {
  53071. { "./certs/server-cert.pem", "./certs/server-key.pem" },
  53072. { "./certs/server-revoked-cert.pem", "./certs/server-revoked-key.pem" }
  53073. };
  53074. size_t i;
  53075. for (i = 0; i < (sizeof(test_params)/sizeof(*test_params)); i++) {
  53076. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  53077. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  53078. server_cbs.certPemFile = test_params[i].server_cert;
  53079. server_cbs.keyPemFile = test_params[i].server_key;
  53080. client_cbs.crlPemFile = "./certs/crl/extra-crls/general-server-crl.pem";
  53081. client_cbs.ctx_ready = test_multiple_crls_same_issuer_ctx_ready;
  53082. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  53083. &server_cbs, NULL), TEST_FAIL);
  53084. }
  53085. #endif
  53086. return EXPECT_RESULT();
  53087. }
  53088. static int test_SetTmpEC_DHE_Sz(void)
  53089. {
  53090. EXPECT_DECLS;
  53091. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  53092. WOLFSSL_CTX *ctx = NULL;
  53093. WOLFSSL *ssl = NULL;
  53094. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  53095. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  53096. ExpectNotNull(ssl = wolfSSL_new(ctx));
  53097. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  53098. wolfSSL_free(ssl);
  53099. wolfSSL_CTX_free(ctx);
  53100. #endif
  53101. return EXPECT_RESULT();
  53102. }
  53103. static int test_wolfSSL_CTX_get0_privatekey(void)
  53104. {
  53105. EXPECT_DECLS;
  53106. #ifdef OPENSSL_ALL
  53107. WOLFSSL_CTX* ctx = NULL;
  53108. (void)ctx;
  53109. #ifndef NO_RSA
  53110. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  53111. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  53112. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  53113. WOLFSSL_FILETYPE_PEM));
  53114. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  53115. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  53116. WOLFSSL_FILETYPE_PEM));
  53117. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  53118. wolfSSL_CTX_free(ctx);
  53119. ctx = NULL;
  53120. #endif
  53121. #ifdef HAVE_ECC
  53122. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  53123. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  53124. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  53125. WOLFSSL_FILETYPE_PEM));
  53126. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  53127. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  53128. WOLFSSL_FILETYPE_PEM));
  53129. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  53130. wolfSSL_CTX_free(ctx);
  53131. #endif
  53132. #endif
  53133. return EXPECT_RESULT();
  53134. }
  53135. static int test_wolfSSL_dtls_set_mtu(void)
  53136. {
  53137. EXPECT_DECLS;
  53138. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  53139. !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS) && \
  53140. !defined(WOLFSSL_NO_TLS12)
  53141. WOLFSSL_CTX* ctx = NULL;
  53142. WOLFSSL* ssl = NULL;
  53143. const char* testCertFile;
  53144. const char* testKeyFile;
  53145. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  53146. #ifndef NO_RSA
  53147. testCertFile = svrCertFile;
  53148. testKeyFile = svrKeyFile;
  53149. #elif defined(HAVE_ECC)
  53150. testCertFile = eccCertFile;
  53151. testKeyFile = eccKeyFile;
  53152. #endif
  53153. if (testCertFile != NULL && testKeyFile != NULL) {
  53154. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  53155. WOLFSSL_FILETYPE_PEM));
  53156. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  53157. WOLFSSL_FILETYPE_PEM));
  53158. }
  53159. ExpectNotNull(ssl = wolfSSL_new(ctx));
  53160. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  53161. ExpectIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  53162. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  53163. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  53164. ExpectIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  53165. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  53166. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  53167. wolfSSL_free(ssl);
  53168. wolfSSL_CTX_free(ctx);
  53169. #endif
  53170. return EXPECT_RESULT();
  53171. }
  53172. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  53173. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  53174. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  53175. enum HandShakeType hsType, word16 length)
  53176. {
  53177. size_t idx = 0;
  53178. byte* l;
  53179. /* record layer */
  53180. /* handshake type */
  53181. out[idx++] = handshake;
  53182. /* protocol version */
  53183. out[idx++] = 0xfe;
  53184. out[idx++] = 0xfd; /* DTLS 1.2 */
  53185. /* epoch 0 */
  53186. XMEMSET(out + idx, 0, 2);
  53187. idx += 2;
  53188. /* sequence number */
  53189. XMEMSET(out + idx, 0, 6);
  53190. c32toa(seq, out + idx + 2);
  53191. idx += 6;
  53192. /* length in BE */
  53193. if (length)
  53194. c16toa(length, out + idx);
  53195. else
  53196. c16toa(outSz - idx - 2, out + idx);
  53197. idx += 2;
  53198. /* handshake layer */
  53199. /* handshake type */
  53200. out[idx++] = (byte)hsType;
  53201. /* length */
  53202. l = out + idx;
  53203. idx += 3;
  53204. /* message seq */
  53205. c16toa(0, out + idx);
  53206. idx += 2;
  53207. /* frag offset */
  53208. c32to24(0, out + idx);
  53209. idx += 3;
  53210. /* frag length */
  53211. c32to24((word32)outSz - (word32)idx - 3, l);
  53212. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  53213. idx += 3;
  53214. XMEMSET(out + idx, 0, outSz - idx);
  53215. }
  53216. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  53217. {
  53218. byte msg[] = "This is a msg for the client";
  53219. byte reply[40];
  53220. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  53221. reply[sizeof(reply) - 1] = '\0';
  53222. fprintf(stderr, "Client message: %s\n", reply);
  53223. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  53224. }
  53225. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  53226. {
  53227. byte ch[50];
  53228. int fd = wolfSSL_get_fd(ssl);
  53229. byte msg[] = "This is a msg for the server";
  53230. byte reply[40];
  53231. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  53232. /* Server should ignore this datagram */
  53233. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  53234. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  53235. /* Server should ignore this datagram */
  53236. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  53237. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  53238. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  53239. reply[sizeof(reply) - 1] = '\0';
  53240. fprintf(stderr, "Server response: %s\n", reply);
  53241. }
  53242. static int test_wolfSSL_dtls_plaintext(void)
  53243. {
  53244. callback_functions func_cb_client;
  53245. callback_functions func_cb_server;
  53246. size_t i;
  53247. struct test_params {
  53248. method_provider client_meth;
  53249. method_provider server_meth;
  53250. ssl_callback on_result_server;
  53251. ssl_callback on_result_client;
  53252. } params[] = {
  53253. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  53254. test_wolfSSL_dtls_plaintext_server,
  53255. test_wolfSSL_dtls_plaintext_client},
  53256. };
  53257. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  53258. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  53259. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  53260. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  53261. func_cb_server.method = params[i].server_meth;
  53262. func_cb_client.method = params[i].client_meth;
  53263. func_cb_client.on_result = params[i].on_result_client;
  53264. func_cb_server.on_result = params[i].on_result_server;
  53265. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  53266. if (!func_cb_client.return_code)
  53267. return TEST_FAIL;
  53268. if (!func_cb_server.return_code)
  53269. return TEST_FAIL;
  53270. }
  53271. return TEST_RES_CHECK(1);
  53272. }
  53273. #else
  53274. static int test_wolfSSL_dtls_plaintext(void) {
  53275. return TEST_SKIPPED;
  53276. }
  53277. #endif
  53278. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  53279. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  53280. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  53281. {
  53282. byte b[1100]; /* buffer for the messages to send */
  53283. size_t idx = 0;
  53284. size_t seq_offset = 0;
  53285. size_t msg_offset = 0;
  53286. int i;
  53287. int fd = wolfSSL_get_fd(ssl);
  53288. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  53289. word32 seq_number = 100; /* start high so server definitely reads this */
  53290. word16 msg_number = 50; /* start high so server has to buffer this */
  53291. AssertIntEQ(ret, 1);
  53292. /* Now let's start spamming the peer with fragments it needs to store */
  53293. XMEMSET(b, -1, sizeof(b));
  53294. /* record layer */
  53295. /* handshake type */
  53296. b[idx++] = 22;
  53297. /* protocol version */
  53298. b[idx++] = 0xfe;
  53299. b[idx++] = 0xfd; /* DTLS 1.2 */
  53300. /* epoch 0 */
  53301. XMEMSET(b + idx, 0, 2);
  53302. idx += 2;
  53303. /* sequence number */
  53304. XMEMSET(b + idx, 0, 6);
  53305. seq_offset = idx + 2; /* increment only the low 32 bits */
  53306. idx += 6;
  53307. /* static length in BE */
  53308. c16toa(42, b + idx);
  53309. idx += 2;
  53310. /* handshake layer */
  53311. /* cert type */
  53312. b[idx++] = 11;
  53313. /* length */
  53314. c32to24(1000, b + idx);
  53315. idx += 3;
  53316. /* message seq */
  53317. c16toa(0, b + idx);
  53318. msg_offset = idx;
  53319. idx += 2;
  53320. /* frag offset */
  53321. c32to24(500, b + idx);
  53322. idx += 3;
  53323. /* frag length */
  53324. c32to24(30, b + idx);
  53325. idx += 3;
  53326. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  53327. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  53328. seq_number++, msg_number++, i++) {
  53329. struct timespec delay;
  53330. XMEMSET(&delay, 0, sizeof(delay));
  53331. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  53332. c32toa(seq_number, b + seq_offset);
  53333. c16toa(msg_number, b + msg_offset);
  53334. ret = (int)send(fd, b, 55, 0);
  53335. nanosleep(&delay, NULL);
  53336. }
  53337. }
  53338. #ifdef WOLFSSL_DTLS13
  53339. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  53340. {
  53341. const word16 sendCountMax = 100;
  53342. byte b[150]; /* buffer for the messages to send */
  53343. size_t idx = 0;
  53344. size_t msg_offset = 0;
  53345. int fd = wolfSSL_get_fd(ssl);
  53346. word16 msg_number = 10; /* start high so server has to buffer this */
  53347. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  53348. AssertIntEQ(ret, 1);
  53349. /* Now let's start spamming the peer with fragments it needs to store */
  53350. XMEMSET(b, -1, sizeof(b));
  53351. /* handshake type */
  53352. b[idx++] = 11;
  53353. /* length */
  53354. c32to24(10000, b + idx);
  53355. idx += 3;
  53356. /* message_seq */
  53357. msg_offset = idx;
  53358. idx += 2;
  53359. /* fragment_offset */
  53360. c32to24(5000, b + idx);
  53361. idx += 3;
  53362. /* fragment_length */
  53363. c32to24(100, b + idx);
  53364. idx += 3;
  53365. /* fragment contents */
  53366. idx += 100;
  53367. for (; ret > 0 && msg_number < sendCountMax; msg_number++) {
  53368. byte sendBuf[150];
  53369. int sendSz = sizeof(sendBuf);
  53370. struct timespec delay;
  53371. XMEMSET(&delay, 0, sizeof(delay));
  53372. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  53373. c16toa(msg_number, b + msg_offset);
  53374. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  53375. (int)idx, handshake, 0, 0, 0);
  53376. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  53377. nanosleep(&delay, NULL);
  53378. }
  53379. }
  53380. #endif
  53381. static int test_wolfSSL_dtls_fragments(void)
  53382. {
  53383. EXPECT_DECLS;
  53384. callback_functions func_cb_client;
  53385. callback_functions func_cb_server;
  53386. size_t i;
  53387. struct test_params {
  53388. method_provider client_meth;
  53389. method_provider server_meth;
  53390. ssl_callback spammer;
  53391. } params[] = {
  53392. #if !defined(WOLFSSL_NO_TLS12)
  53393. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  53394. test_wolfSSL_dtls12_fragments_spammer},
  53395. #endif
  53396. #ifdef WOLFSSL_DTLS13
  53397. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  53398. test_wolfSSL_dtls13_fragments_spammer},
  53399. #endif
  53400. };
  53401. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  53402. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  53403. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  53404. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  53405. func_cb_server.method = params[i].server_meth;
  53406. func_cb_client.method = params[i].client_meth;
  53407. func_cb_client.ssl_ready = params[i].spammer;
  53408. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  53409. ExpectFalse(func_cb_client.return_code);
  53410. ExpectFalse(func_cb_server.return_code);
  53411. /* The socket should be closed by the server resulting in a
  53412. * socket error, fatal error or reading a close notify alert */
  53413. if (func_cb_client.last_err != SOCKET_ERROR_E &&
  53414. func_cb_client.last_err != WOLFSSL_ERROR_ZERO_RETURN &&
  53415. func_cb_client.last_err != FATAL_ERROR) {
  53416. ExpectIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  53417. }
  53418. /* Check the server returned an error indicating the msg buffer
  53419. * was full */
  53420. ExpectIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  53421. if (EXPECT_FAIL())
  53422. break;
  53423. }
  53424. return EXPECT_RESULT();
  53425. }
  53426. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  53427. {
  53428. int fd, ret;
  53429. byte alert_msg[] = {
  53430. 0x15, /* alert type */
  53431. 0xfe, 0xfd, /* version */
  53432. 0x00, 0x00, /* epoch */
  53433. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  53434. 0x00, 0x02, /* length */
  53435. 0x02, /* level: fatal */
  53436. 0x46 /* protocol version */
  53437. };
  53438. fd = wolfSSL_get_fd(ssl);
  53439. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  53440. AssertIntGT(ret, 0);
  53441. }
  53442. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  53443. {
  53444. callback_functions client_cbs, server_cbs;
  53445. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  53446. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  53447. client_cbs.doUdp = server_cbs.doUdp = 1;
  53448. if (version12) {
  53449. #if !defined(WOLFSSL_NO_TLS12)
  53450. client_cbs.method = wolfDTLSv1_2_client_method;
  53451. server_cbs.method = wolfDTLSv1_2_server_method;
  53452. #else
  53453. return TEST_SKIPPED;
  53454. #endif
  53455. }
  53456. else
  53457. {
  53458. #ifdef WOLFSSL_DTLS13
  53459. client_cbs.method = wolfDTLSv1_3_client_method;
  53460. server_cbs.method = wolfDTLSv1_3_server_method;
  53461. #else
  53462. return TEST_SKIPPED;
  53463. #endif /* WOLFSSL_DTLS13 */
  53464. }
  53465. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  53466. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  53467. if (!client_cbs.return_code)
  53468. return TEST_FAIL;
  53469. if (!server_cbs.return_code)
  53470. return TEST_FAIL;
  53471. return TEST_SUCCESS;
  53472. }
  53473. static int test_wolfSSL_ignore_alert_before_cookie(void)
  53474. {
  53475. int ret;
  53476. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  53477. if (ret != 0)
  53478. return ret;
  53479. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  53480. if (ret != 0)
  53481. return ret;
  53482. return 0;
  53483. }
  53484. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  53485. {
  53486. int ret;
  53487. int fd;
  53488. byte bad_msg[] = {
  53489. 0x17, /* app data */
  53490. 0xaa, 0xfd, /* bad version */
  53491. 0x00, 0x01, /* epoch 1 */
  53492. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  53493. 0x00, 0x26, /* length: 38 bytes */
  53494. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  53495. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  53496. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  53497. 0x03, 0x18, 0x72
  53498. };
  53499. fd = wolfSSL_get_fd(ssl);
  53500. AssertIntGE(fd, 0);
  53501. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  53502. AssertIntEQ(ret, sizeof(bad_msg));
  53503. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  53504. AssertIntEQ(ret, sizeof("badrecordtest"));
  53505. }
  53506. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  53507. {
  53508. byte buf[100];
  53509. int ret;
  53510. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  53511. AssertIntGT(ret, 0);
  53512. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  53513. }
  53514. static int _test_wolfSSL_dtls_bad_record(
  53515. method_provider client_method, method_provider server_method)
  53516. {
  53517. callback_functions client_cbs, server_cbs;
  53518. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  53519. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  53520. client_cbs.doUdp = server_cbs.doUdp = 1;
  53521. client_cbs.method = client_method;
  53522. server_cbs.method = server_method;
  53523. client_cbs.on_result = test_wolfSSL_send_bad_record;
  53524. server_cbs.on_result = test_wolfSSL_read_string;
  53525. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  53526. if (!client_cbs.return_code)
  53527. return TEST_FAIL;
  53528. if (!server_cbs.return_code)
  53529. return TEST_FAIL;
  53530. return TEST_SUCCESS;
  53531. }
  53532. static int test_wolfSSL_dtls_bad_record(void)
  53533. {
  53534. int ret = TEST_SUCCESS;
  53535. #if !defined(WOLFSSL_NO_TLS12)
  53536. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  53537. wolfDTLSv1_2_server_method);
  53538. #endif
  53539. #ifdef WOLFSSL_DTLS13
  53540. if (ret == TEST_SUCCESS) {
  53541. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  53542. wolfDTLSv1_3_server_method);
  53543. }
  53544. #endif /* WOLFSSL_DTLS13 */
  53545. return ret;
  53546. }
  53547. #else
  53548. static int test_wolfSSL_dtls_fragments(void) {
  53549. return TEST_SKIPPED;
  53550. }
  53551. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  53552. return TEST_SKIPPED;
  53553. }
  53554. static int test_wolfSSL_dtls_bad_record(void) {
  53555. return TEST_SKIPPED;
  53556. }
  53557. #endif
  53558. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
  53559. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  53560. defined(HAVE_IO_TESTS_DEPENDENCIES)
  53561. static byte test_AEAD_fail_decryption = 0;
  53562. static byte test_AEAD_seq_num = 0;
  53563. static byte test_AEAD_done = 0;
  53564. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  53565. {
  53566. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  53567. if (ret > 0) {
  53568. if (test_AEAD_fail_decryption) {
  53569. /* Modify the packet to trigger a decryption failure */
  53570. buf[ret/2] ^= 0xFF;
  53571. if (test_AEAD_fail_decryption == 1)
  53572. test_AEAD_fail_decryption = 0;
  53573. }
  53574. }
  53575. (void)ctx;
  53576. return ret;
  53577. }
  53578. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  53579. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  53580. {
  53581. if (sendLimit)
  53582. w64Zero(sendLimit);
  53583. switch (ssl->specs.bulk_cipher_algorithm) {
  53584. case wolfssl_aes_gcm:
  53585. if (sendLimit)
  53586. *sendLimit = AEAD_AES_LIMIT;
  53587. FALL_THROUGH;
  53588. case wolfssl_chacha:
  53589. if (hardLimit)
  53590. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  53591. if (keyUpdateLimit)
  53592. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  53593. break;
  53594. case wolfssl_aes_ccm:
  53595. if (sendLimit)
  53596. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  53597. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  53598. if (hardLimit)
  53599. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  53600. if (keyUpdateLimit)
  53601. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  53602. }
  53603. else {
  53604. if (hardLimit)
  53605. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  53606. if (keyUpdateLimit)
  53607. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  53608. }
  53609. break;
  53610. default:
  53611. fprintf(stderr, "Unrecognized bulk cipher");
  53612. AssertFalse(1);
  53613. break;
  53614. }
  53615. }
  53616. static void test_AEAD_limit_client(WOLFSSL* ssl)
  53617. {
  53618. int ret;
  53619. int i;
  53620. int didReKey = 0;
  53621. char msgBuf[20];
  53622. w64wrapper hardLimit;
  53623. w64wrapper keyUpdateLimit;
  53624. w64wrapper counter;
  53625. w64wrapper sendLimit;
  53626. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  53627. w64Zero(&counter);
  53628. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  53629. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  53630. for (i = 0; i < 10; i++) {
  53631. /* Test some failed decryptions */
  53632. test_AEAD_fail_decryption = 1;
  53633. w64Increment(&counter);
  53634. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  53635. /* Should succeed since decryption failures are dropped */
  53636. AssertIntGT(ret, 0);
  53637. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  53638. }
  53639. test_AEAD_fail_decryption = 1;
  53640. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  53641. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  53642. /* 100 read calls should be enough to complete the key update */
  53643. w64Zero(&counter);
  53644. for (i = 0; i < 100; i++) {
  53645. /* Key update should be sent and negotiated */
  53646. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  53647. AssertIntGT(ret, 0);
  53648. /* Epoch after one key update is 4 */
  53649. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  53650. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  53651. didReKey = 1;
  53652. break;
  53653. }
  53654. }
  53655. AssertTrue(didReKey);
  53656. if (!w64IsZero(sendLimit)) {
  53657. /* Test the sending limit for AEAD ciphers */
  53658. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  53659. test_AEAD_seq_num = 1;
  53660. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  53661. AssertIntGT(ret, 0);
  53662. didReKey = 0;
  53663. w64Zero(&counter);
  53664. /* 100 read calls should be enough to complete the key update */
  53665. for (i = 0; i < 100; i++) {
  53666. /* Key update should be sent and negotiated */
  53667. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  53668. AssertIntGT(ret, 0);
  53669. /* Epoch after another key update is 5 */
  53670. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  53671. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  53672. didReKey = 1;
  53673. break;
  53674. }
  53675. }
  53676. AssertTrue(didReKey);
  53677. }
  53678. test_AEAD_fail_decryption = 2;
  53679. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  53680. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  53681. /* Connection should fail with a DECRYPT_ERROR */
  53682. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  53683. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  53684. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  53685. test_AEAD_done = 1;
  53686. }
  53687. int counter = 0;
  53688. static void test_AEAD_limit_server(WOLFSSL* ssl)
  53689. {
  53690. char msgBuf[] = "Sending data";
  53691. int ret = WOLFSSL_SUCCESS;
  53692. w64wrapper sendLimit;
  53693. SOCKET_T fd = wolfSSL_get_fd(ssl);
  53694. struct timespec delay;
  53695. XMEMSET(&delay, 0, sizeof(delay));
  53696. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  53697. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  53698. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  53699. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  53700. while (!test_AEAD_done && ret > 0) {
  53701. counter++;
  53702. if (test_AEAD_seq_num) {
  53703. /* We need to update the seq number so that we can understand the
  53704. * peer. Otherwise we will incorrectly interpret the seq number. */
  53705. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  53706. AssertNotNull(e);
  53707. e->nextPeerSeqNumber = sendLimit;
  53708. test_AEAD_seq_num = 0;
  53709. }
  53710. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  53711. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  53712. nanosleep(&delay, NULL);
  53713. }
  53714. }
  53715. static int test_wolfSSL_dtls_AEAD_limit(void)
  53716. {
  53717. callback_functions func_cb_client;
  53718. callback_functions func_cb_server;
  53719. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  53720. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  53721. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  53722. func_cb_server.method = wolfDTLSv1_3_server_method;
  53723. func_cb_client.method = wolfDTLSv1_3_client_method;
  53724. func_cb_server.on_result = test_AEAD_limit_server;
  53725. func_cb_client.on_result = test_AEAD_limit_client;
  53726. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  53727. if (!func_cb_client.return_code)
  53728. return TEST_FAIL;
  53729. if (!func_cb_server.return_code)
  53730. return TEST_FAIL;
  53731. return TEST_SUCCESS;
  53732. }
  53733. #else
  53734. static int test_wolfSSL_dtls_AEAD_limit(void)
  53735. {
  53736. return TEST_SKIPPED;
  53737. }
  53738. #endif
  53739. #if defined(WOLFSSL_DTLS) && \
  53740. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  53741. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  53742. {
  53743. int fd, ret;
  53744. byte ch_msg[] = {
  53745. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  53746. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  53747. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  53748. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  53749. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  53750. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  53751. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  53752. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  53753. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  53754. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  53755. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  53756. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  53757. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  53758. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  53759. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  53760. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  53761. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  53762. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  53763. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  53764. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  53765. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  53766. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  53767. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  53768. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  53769. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  53770. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  53771. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  53772. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  53773. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  53774. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  53775. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  53776. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  53777. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  53778. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  53779. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  53780. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  53781. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  53782. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  53783. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  53784. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  53785. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  53786. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  53787. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  53788. 0x31, 0x68, 0x4c
  53789. };
  53790. fd = wolfSSL_get_fd(ssl);
  53791. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  53792. AssertIntGT(ret, 0);
  53793. /* consume the HRR otherwise handshake will fail */
  53794. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  53795. AssertIntGT(ret, 0);
  53796. }
  53797. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  53798. static void test_wolfSSL_dtls_send_ch_with_invalid_cookie(WOLFSSL* ssl)
  53799. {
  53800. int fd, ret;
  53801. byte ch_msh_invalid_cookie[] = {
  53802. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
  53803. 0x4e, 0x01, 0x00, 0x02, 0x42, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02,
  53804. 0x42, 0xfe, 0xfd, 0x69, 0xca, 0x77, 0x60, 0x6f, 0xfc, 0xd1, 0x5b, 0x60,
  53805. 0x5d, 0xf1, 0xa6, 0x5c, 0x44, 0x71, 0xae, 0xca, 0x62, 0x19, 0x0c, 0xb6,
  53806. 0xf7, 0x2c, 0xa6, 0xd5, 0xd2, 0x99, 0x9d, 0x18, 0xae, 0xac, 0x11, 0x00,
  53807. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  53808. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  53809. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  53810. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  53811. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  53812. 0xe2, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  53813. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  53814. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  53815. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x2c, 0x00, 0x45,
  53816. 0x00, 0x43, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53817. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53818. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53819. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53820. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53821. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x2d, 0x00,
  53822. 0x03, 0x02, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x0a, 0x00, 0x19,
  53823. 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00, 0x00, 0x16, 0x00, 0x00,
  53824. 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17, 0x00, 0x41, 0x04, 0x7c,
  53825. 0x5a, 0xc2, 0x5a, 0xfd, 0xcd, 0x2b, 0x08, 0xb2, 0xeb, 0x8e, 0xc0, 0x02,
  53826. 0x03, 0x9d, 0xb1, 0xc1, 0x0d, 0x7b, 0x7f, 0x46, 0x43, 0xdf, 0xf3, 0xee,
  53827. 0x2b, 0x78, 0x0e, 0x29, 0x8c, 0x42, 0x11, 0x2c, 0xde, 0xd7, 0x41, 0x0f,
  53828. 0x28, 0x94, 0x80, 0x41, 0x70, 0xc4, 0x17, 0xfd, 0x6d, 0xfa, 0xee, 0x9a,
  53829. 0xf2, 0xc4, 0x15, 0x4c, 0x5f, 0x54, 0xb6, 0x78, 0x6e, 0xf9, 0x63, 0x27,
  53830. 0x33, 0xb8, 0x7b, 0x01, 0x00, 0x01, 0x00, 0xd4, 0x46, 0x62, 0x9c, 0xbf,
  53831. 0x8f, 0x1b, 0x65, 0x9b, 0xf0, 0x29, 0x64, 0xd8, 0x50, 0x0e, 0x74, 0xf1,
  53832. 0x58, 0x10, 0xc9, 0xd9, 0x82, 0x5b, 0xd9, 0xbe, 0x14, 0xdf, 0xde, 0x86,
  53833. 0xb4, 0x2e, 0x15, 0xee, 0x4f, 0xf6, 0x74, 0x9e, 0x59, 0x11, 0x36, 0x2d,
  53834. 0xb9, 0x67, 0xaa, 0x5a, 0x09, 0x9b, 0x45, 0xf1, 0x01, 0x4c, 0x4e, 0xf6,
  53835. 0xda, 0x6a, 0xae, 0xa7, 0x73, 0x7b, 0x2e, 0xb6, 0x24, 0x89, 0x99, 0xb7,
  53836. 0x52, 0x16, 0x62, 0x0a, 0xab, 0x58, 0xf8, 0x3f, 0x10, 0x5b, 0x83, 0xfd,
  53837. 0x7b, 0x81, 0x77, 0x81, 0x8d, 0xef, 0x24, 0x56, 0x6d, 0xba, 0x49, 0xd4,
  53838. 0x8b, 0xb5, 0xa0, 0xb1, 0xc9, 0x8c, 0x32, 0x95, 0x1c, 0x5e, 0x0a, 0x4b,
  53839. 0xf6, 0x00, 0x50, 0x0a, 0x87, 0x99, 0x59, 0xcf, 0x6f, 0x9d, 0x02, 0xd0,
  53840. 0x1b, 0xa1, 0x96, 0x45, 0x28, 0x76, 0x40, 0x33, 0x28, 0xc9, 0xa1, 0xfd,
  53841. 0x46, 0xab, 0x2c, 0x9e, 0x5e, 0xc6, 0x74, 0x19, 0x9a, 0xf5, 0x9b, 0x51,
  53842. 0x11, 0x4f, 0xc8, 0xb9, 0x99, 0x6b, 0x4e, 0x3e, 0x31, 0x64, 0xb4, 0x92,
  53843. 0xf4, 0x0d, 0x41, 0x4b, 0x2c, 0x65, 0x23, 0xf7, 0x47, 0xe3, 0xa5, 0x2e,
  53844. 0xe4, 0x9c, 0x2b, 0xc9, 0x41, 0x22, 0x83, 0x8a, 0x23, 0xef, 0x29, 0x7e,
  53845. 0x4f, 0x3f, 0xa3, 0xbf, 0x73, 0x2b, 0xd7, 0xcc, 0xc8, 0xc6, 0xe9, 0xbc,
  53846. 0x01, 0xb7, 0x32, 0x63, 0xd4, 0x7e, 0x7f, 0x9a, 0xaf, 0x5f, 0x05, 0x31,
  53847. 0x53, 0xd6, 0x1f, 0xa2, 0xd0, 0xdf, 0x67, 0x56, 0xf1, 0x9c, 0x4a, 0x9d,
  53848. 0x83, 0xb4, 0xef, 0xb3, 0xf2, 0xcc, 0xf1, 0x91, 0x6c, 0x47, 0xc3, 0x8b,
  53849. 0xd0, 0x92, 0x79, 0x3d, 0xa0, 0xc0, 0x3a, 0x57, 0x26, 0x6d, 0x0a, 0xad,
  53850. 0x5f, 0xad, 0xb4, 0x74, 0x48, 0x4a, 0x51, 0xe1, 0xb5, 0x82, 0x0a, 0x4c,
  53851. 0x4f, 0x9d, 0xaf, 0xee, 0x5a, 0xa2, 0x4d, 0x4d, 0x5f, 0xe0, 0x17, 0x00,
  53852. 0x23, 0x00, 0x00
  53853. };
  53854. byte alert_reply[50];
  53855. byte expected_alert_reply[] = {
  53856. 0x15, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  53857. 0x02, 0x02, 0x2f
  53858. };
  53859. fd = wolfSSL_get_fd(ssl);
  53860. ret = (int)send(fd, ch_msh_invalid_cookie, sizeof(ch_msh_invalid_cookie), 0);
  53861. AssertIntGT(ret, 0);
  53862. /* should reply with an illegal_parameter reply */
  53863. ret = (int)recv(fd, alert_reply, sizeof(alert_reply), 0);
  53864. AssertIntEQ(ret, sizeof(expected_alert_reply));
  53865. AssertIntEQ(XMEMCMP(alert_reply, expected_alert_reply, sizeof(expected_alert_reply)), 0);
  53866. }
  53867. #endif
  53868. static word32 test_wolfSSL_dtls_stateless_HashWOLFSSL(const WOLFSSL* ssl)
  53869. {
  53870. #ifndef NO_MD5
  53871. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  53872. #elif !defined(NO_SHA)
  53873. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  53874. #elif !defined(NO_SHA256)
  53875. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  53876. #else
  53877. #error "We need a digest to hash the WOLFSSL object"
  53878. #endif
  53879. byte hashBuf[WC_MAX_DIGEST_SIZE];
  53880. wc_HashAlg hash;
  53881. const TLSX* exts = ssl->extensions;
  53882. WOLFSSL sslCopy; /* Use a copy to omit certain fields */
  53883. HS_Hashes* hsHashes = ssl->hsHashes; /* Is re-allocated in
  53884. * InitHandshakeHashes */
  53885. XMEMCPY(&sslCopy, ssl, sizeof(*ssl));
  53886. XMEMSET(hashBuf, 0, sizeof(hashBuf));
  53887. /* Following fields are not important to compare */
  53888. XMEMSET(sslCopy.buffers.inputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  53889. sslCopy.buffers.inputBuffer.buffer = NULL;
  53890. sslCopy.buffers.inputBuffer.bufferSize = 0;
  53891. sslCopy.buffers.inputBuffer.dynamicFlag = 0;
  53892. sslCopy.buffers.inputBuffer.offset = 0;
  53893. XMEMSET(sslCopy.buffers.outputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  53894. sslCopy.buffers.outputBuffer.buffer = NULL;
  53895. sslCopy.buffers.outputBuffer.bufferSize = 0;
  53896. sslCopy.buffers.outputBuffer.dynamicFlag = 0;
  53897. sslCopy.buffers.outputBuffer.offset = 0;
  53898. sslCopy.error = 0;
  53899. sslCopy.curSize = 0;
  53900. sslCopy.keys.curSeq_lo = 0;
  53901. XMEMSET(&sslCopy.curRL, 0, sizeof(sslCopy.curRL));
  53902. #ifdef WOLFSSL_DTLS13
  53903. XMEMSET(&sslCopy.keys.curSeq, 0, sizeof(sslCopy.keys.curSeq));
  53904. sslCopy.dtls13FastTimeout = 0;
  53905. #endif
  53906. sslCopy.keys.dtls_peer_handshake_number = 0;
  53907. XMEMSET(&sslCopy.alert_history, 0, sizeof(sslCopy.alert_history));
  53908. sslCopy.hsHashes = NULL;
  53909. #ifdef WOLFSSL_ASYNC_IO
  53910. #ifdef WOLFSSL_ASYNC_CRYPT
  53911. sslCopy.asyncDev = NULL;
  53912. #endif
  53913. sslCopy.async = NULL;
  53914. #endif
  53915. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  53916. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)&sslCopy, sizeof(sslCopy)), 0);
  53917. /* hash extension list */
  53918. while (exts != NULL) {
  53919. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)exts, sizeof(*exts)), 0);
  53920. exts = exts->next;
  53921. }
  53922. /* Hash suites */
  53923. if (sslCopy.suites != NULL) {
  53924. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)sslCopy.suites,
  53925. sizeof(struct Suites)), 0);
  53926. }
  53927. /* Hash hsHashes */
  53928. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)hsHashes,
  53929. sizeof(*hsHashes)), 0);
  53930. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  53931. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  53932. return MakeWordFromHash(hashBuf);
  53933. }
  53934. static CallbackIORecv test_wolfSSL_dtls_compare_stateless_cb;
  53935. static int test_wolfSSL_dtls_compare_stateless_cb_call_once;
  53936. static int test_wolfSSL_dtls_compare_stateless_read_cb_once(WOLFSSL *ssl,
  53937. char *buf, int sz, void *ctx)
  53938. {
  53939. if (test_wolfSSL_dtls_compare_stateless_cb_call_once) {
  53940. test_wolfSSL_dtls_compare_stateless_cb_call_once = 0;
  53941. return test_wolfSSL_dtls_compare_stateless_cb(ssl, buf, sz, ctx);
  53942. }
  53943. else {
  53944. return WOLFSSL_CBIO_ERR_WANT_READ;
  53945. }
  53946. }
  53947. static void test_wolfSSL_dtls_compare_stateless(WOLFSSL* ssl)
  53948. {
  53949. /* Compare the ssl object before and after one ClientHello msg */
  53950. SOCKET_T fd = wolfSSL_get_fd(ssl);
  53951. int res;
  53952. int err;
  53953. word32 initHash;
  53954. test_wolfSSL_dtls_compare_stateless_cb = ssl->CBIORecv;
  53955. test_wolfSSL_dtls_compare_stateless_cb_call_once = 1;
  53956. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  53957. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_read_cb_once;
  53958. initHash = test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl);
  53959. (void)initHash;
  53960. res = tcp_select(fd, 5);
  53961. /* We are expecting a msg. A timeout indicates failure. */
  53962. AssertIntEQ(res, TEST_RECV_READY);
  53963. res = wolfSSL_accept(ssl);
  53964. err = wolfSSL_get_error(ssl, res);
  53965. AssertIntEQ(res, WOLFSSL_FATAL_ERROR);
  53966. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  53967. AssertIntEQ(initHash, test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl));
  53968. wolfSSL_dtls_set_using_nonblock(ssl, 0);
  53969. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_cb;
  53970. }
  53971. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  53972. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  53973. {
  53974. int ret;
  53975. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  53976. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  53977. test_wolfSSL_dtls_compare_stateless(ssl);
  53978. }
  53979. #endif
  53980. static int test_wolfSSL_dtls_stateless(void)
  53981. {
  53982. callback_functions client_cbs, server_cbs;
  53983. size_t i;
  53984. struct {
  53985. method_provider client_meth;
  53986. method_provider server_meth;
  53987. ssl_callback client_ssl_ready;
  53988. ssl_callback server_ssl_ready;
  53989. } test_params[] = {
  53990. #if !defined(WOLFSSL_NO_TLS12)
  53991. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  53992. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_compare_stateless},
  53993. #endif
  53994. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  53995. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  53996. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  53997. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  53998. test_wolfSSL_dtls_send_ch_with_invalid_cookie, test_wolfSSL_dtls_enable_hrrcookie},
  53999. #endif
  54000. };
  54001. if (0 == sizeof(test_params)){
  54002. return TEST_SKIPPED;
  54003. }
  54004. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  54005. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54006. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54007. client_cbs.doUdp = server_cbs.doUdp = 1;
  54008. client_cbs.method = test_params[i].client_meth;
  54009. server_cbs.method = test_params[i].server_meth;
  54010. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  54011. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  54012. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  54013. if (!client_cbs.return_code)
  54014. return TEST_FAIL;
  54015. if (!server_cbs.return_code)
  54016. return TEST_FAIL;
  54017. }
  54018. return TEST_SUCCESS;
  54019. }
  54020. #else
  54021. static int test_wolfSSL_dtls_stateless(void)
  54022. {
  54023. return TEST_SKIPPED;
  54024. }
  54025. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  54026. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  54027. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  54028. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  54029. !defined(WOLFSSL_NO_CLIENT_AUTH))
  54030. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  54031. {
  54032. int ret;
  54033. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  54034. fprintf(stderr, "loading cert %s failed\n", certA);
  54035. fprintf(stderr, "Error: (%d): %s\n", ret,
  54036. wolfSSL_ERR_reason_error_string(ret));
  54037. return -1;
  54038. }
  54039. return 0;
  54040. }
  54041. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  54042. {
  54043. int ret;
  54044. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  54045. != WOLFSSL_SUCCESS) {
  54046. fprintf(stderr, "could not verify the cert: %s\n", certA);
  54047. fprintf(stderr, "Error: (%d): %s\n", ret,
  54048. wolfSSL_ERR_reason_error_string(ret));
  54049. return -1;
  54050. }
  54051. else {
  54052. fprintf(stderr, "successfully verified: %s\n", certA);
  54053. }
  54054. return 0;
  54055. }
  54056. #define LOAD_ONE_CA(a, b, c, d) \
  54057. do { \
  54058. (a) = load_ca_into_cm(c, d); \
  54059. if ((a) != 0) \
  54060. return (b); \
  54061. else \
  54062. (b)--; \
  54063. } while(0)
  54064. #define VERIFY_ONE_CERT(a, b, c, d) \
  54065. do { \
  54066. (a) = verify_cert_with_cm(c, d);\
  54067. if ((a) != 0) \
  54068. return (b); \
  54069. else \
  54070. (b)--; \
  54071. } while(0)
  54072. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  54073. {
  54074. int ret;
  54075. int i = -1;
  54076. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  54077. char chainGArr[9][50] = {"certs/ca-cert.pem",
  54078. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  54079. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  54080. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  54081. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  54082. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  54083. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  54084. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  54085. "certs/test-pathlen/chainG-entity.pem"};
  54086. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  54087. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  54088. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  54089. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  54090. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  54091. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  54092. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  54093. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  54094. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  54095. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  54096. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  54097. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  54098. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  54099. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  54100. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  54101. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  54102. /* test validating the entity twice, should have no effect on pathLen since
  54103. * entity/leaf cert */
  54104. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  54105. return ret;
  54106. }
  54107. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  54108. {
  54109. int ret;
  54110. int i = -1;
  54111. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  54112. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  54113. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  54114. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  54115. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  54116. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  54117. */
  54118. char chainHArr[6][50] = {"certs/ca-cert.pem",
  54119. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  54120. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  54121. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  54122. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  54123. "certs/test-pathlen/chainH-entity.pem"};
  54124. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  54125. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  54126. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  54127. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  54128. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  54129. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  54130. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  54131. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  54132. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  54133. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  54134. return ret;
  54135. }
  54136. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  54137. {
  54138. int ret;
  54139. int i = -1;
  54140. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  54141. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  54142. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  54143. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  54144. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  54145. */
  54146. char chainIArr[5][50] = {"certs/ca-cert.pem",
  54147. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  54148. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  54149. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  54150. "certs/test-pathlen/chainI-entity.pem"};
  54151. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  54152. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  54153. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  54154. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  54155. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  54156. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  54157. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  54158. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  54159. return ret;
  54160. }
  54161. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  54162. {
  54163. int ret;
  54164. int i = -1;
  54165. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  54166. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  54167. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  54168. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  54169. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  54170. */
  54171. char chainJArr[6][50] = {"certs/ca-cert.pem",
  54172. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  54173. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  54174. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  54175. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  54176. "certs/test-pathlen/chainJ-entity.pem"};
  54177. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  54178. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  54179. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  54180. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  54181. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  54182. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  54183. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  54184. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  54185. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  54186. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  54187. return ret;
  54188. }
  54189. static int test_various_pathlen_chains(void)
  54190. {
  54191. EXPECT_DECLS;
  54192. WOLFSSL_CERT_MANAGER* cm = NULL;
  54193. /* Test chain G (large chain with varying pathLens) */
  54194. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  54195. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  54196. ExpectIntEQ(test_chainG(cm), -1);
  54197. #else
  54198. ExpectIntEQ(test_chainG(cm), 0);
  54199. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  54200. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  54201. wolfSSL_CertManagerFree(cm);
  54202. /* end test chain G */
  54203. /* Test chain H (5 chain with same pathLens) */
  54204. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  54205. ExpectIntLT(test_chainH(cm), 0);
  54206. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  54207. wolfSSL_CertManagerFree(cm);
  54208. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  54209. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  54210. wolfSSL_CertManagerFree(cm);
  54211. /* end test chain H */
  54212. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  54213. * followed by 2 ICA's, should pass) */
  54214. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  54215. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  54216. ExpectIntEQ(test_chainI(cm), -1);
  54217. #else
  54218. ExpectIntEQ(test_chainI(cm), 0);
  54219. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  54220. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  54221. wolfSSL_CertManagerFree(cm);
  54222. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  54223. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  54224. wolfSSL_CertManagerFree(cm);
  54225. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  54226. * this time followed by 3 ICA's, should fail */
  54227. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  54228. ExpectIntLT(test_chainJ(cm), 0);
  54229. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  54230. wolfSSL_CertManagerFree(cm);
  54231. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  54232. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  54233. wolfSSL_CertManagerFree(cm);
  54234. return EXPECT_RESULT();
  54235. }
  54236. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  54237. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  54238. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  54239. {
  54240. EXPECT_DECLS;
  54241. byte ekm[100] = {0};
  54242. (void)ctx;
  54243. /* Success Cases */
  54244. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54245. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  54246. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54247. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  54248. /* Use some random context */
  54249. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54250. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  54251. /* Failure cases */
  54252. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54253. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  54254. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54255. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  54256. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54257. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  54258. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54259. "extended master secret", XSTR_SIZEOF("extended master secret"),
  54260. NULL, 0, 0), 0);
  54261. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54262. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  54263. return EXPECT_RESULT();
  54264. }
  54265. static int test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  54266. {
  54267. wolfSSL_KeepArrays(ssl);
  54268. return TEST_SUCCESS;
  54269. }
  54270. static int test_export_keying_material(void)
  54271. {
  54272. EXPECT_DECLS;
  54273. test_ssl_cbf serverCb;
  54274. test_ssl_cbf clientCb;
  54275. XMEMSET(&serverCb, 0, sizeof(serverCb));
  54276. XMEMSET(&clientCb, 0, sizeof(clientCb));
  54277. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  54278. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&clientCb,
  54279. &serverCb, test_export_keying_material_cb), TEST_SUCCESS);
  54280. return EXPECT_RESULT();
  54281. }
  54282. #endif /* HAVE_KEYING_MATERIAL */
  54283. static int test_wolfSSL_THREADID_hash(void)
  54284. {
  54285. EXPECT_DECLS;
  54286. #if defined(OPENSSL_EXTRA)
  54287. CRYPTO_THREADID id;
  54288. CRYPTO_THREADID_current(NULL);
  54289. /* Hash result is unsigned long. */
  54290. ExpectTrue(CRYPTO_THREADID_hash(NULL) == 0UL);
  54291. XMEMSET(&id, 0, sizeof(id));
  54292. ExpectTrue(CRYPTO_THREADID_hash(&id) == 0UL);
  54293. #endif /* OPENSSL_EXTRA */
  54294. return EXPECT_RESULT();
  54295. }
  54296. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  54297. {
  54298. EXPECT_DECLS;
  54299. #if defined(OPENSSL_EXTRA)
  54300. WOLFSSL_CTX* ctx = NULL;
  54301. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,0), 1);
  54302. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,1), 1);
  54303. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,0), 1);
  54304. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,1), 1);
  54305. #endif /* OPENSSL_EXTRA */
  54306. return EXPECT_RESULT();
  54307. }
  54308. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  54309. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  54310. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  54311. {
  54312. EXPECT_DECLS;
  54313. callback_functions* callbacks = NULL;
  54314. WOLFSSL_CTX* ctx = NULL;
  54315. WOLFSSL* ssl = NULL;
  54316. SOCKET_T sfd = 0;
  54317. SOCKET_T cfd = 0;
  54318. word16 port;
  54319. char msg[] = "I hear you fa shizzle!";
  54320. int len = (int) XSTRLEN(msg);
  54321. char input[1024];
  54322. int ret;
  54323. int err = 0;
  54324. if (!args)
  54325. WOLFSSL_RETURN_FROM_THREAD(0);
  54326. ((func_args*)args)->return_code = TEST_FAIL;
  54327. callbacks = ((func_args*)args)->callbacks;
  54328. ctx = wolfSSL_CTX_new(callbacks->method());
  54329. #if defined(USE_WINDOWS_API)
  54330. port = ((func_args*)args)->signal->port;
  54331. #else
  54332. /* Let tcp_listen assign port */
  54333. port = 0;
  54334. #endif
  54335. #ifdef WOLFSSL_TIRTOS
  54336. fdOpenSession(Task_self());
  54337. #endif
  54338. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  54339. caCertFile, 0));
  54340. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  54341. svrCertFile, WOLFSSL_FILETYPE_PEM));
  54342. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  54343. svrKeyFile, WOLFSSL_FILETYPE_PEM));
  54344. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  54345. ExpectIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  54346. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  54347. #elif !defined(NO_DH)
  54348. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  54349. #endif
  54350. if (callbacks->ctx_ready)
  54351. callbacks->ctx_ready(ctx);
  54352. ssl = wolfSSL_new(ctx);
  54353. ExpectNotNull(ssl);
  54354. /* listen and accept */
  54355. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  54356. CloseSocket(sfd);
  54357. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  54358. if (callbacks->ssl_ready)
  54359. callbacks->ssl_ready(ssl);
  54360. if (EXPECT_SUCCESS()) {
  54361. do {
  54362. err = 0; /* Reset error */
  54363. ret = wolfSSL_accept(ssl);
  54364. if (ret != WOLFSSL_SUCCESS) {
  54365. err = wolfSSL_get_error(ssl, 0);
  54366. }
  54367. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  54368. }
  54369. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  54370. /* read and write data */
  54371. XMEMSET(input, 0, sizeof(input));
  54372. while (EXPECT_SUCCESS()) {
  54373. ret = wolfSSL_read(ssl, input, sizeof(input));
  54374. if (ret > 0) {
  54375. break;
  54376. }
  54377. else {
  54378. err = wolfSSL_get_error(ssl,ret);
  54379. if (err == WOLFSSL_ERROR_WANT_READ) {
  54380. continue;
  54381. }
  54382. break;
  54383. }
  54384. }
  54385. if (EXPECT_SUCCESS() && (err == WOLFSSL_ERROR_ZERO_RETURN)) {
  54386. do {
  54387. ret = wolfSSL_write(ssl, msg, len);
  54388. if (ret > 0) {
  54389. break;
  54390. }
  54391. } while (ret < 0);
  54392. }
  54393. /* bidirectional shutdown */
  54394. while (EXPECT_SUCCESS()) {
  54395. ret = wolfSSL_shutdown(ssl);
  54396. ExpectIntNE(ret, WOLFSSL_FATAL_ERROR);
  54397. if (ret == WOLFSSL_SUCCESS) {
  54398. break;
  54399. }
  54400. }
  54401. if (EXPECT_SUCCESS()) {
  54402. /* wait for the peer to disconnect the tcp connection */
  54403. do {
  54404. ret = wolfSSL_read(ssl, input, sizeof(input));
  54405. err = wolfSSL_get_error(ssl, ret);
  54406. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  54407. }
  54408. /* detect TCP disconnect */
  54409. ExpectIntLE(ret,WOLFSSL_FAILURE);
  54410. ExpectIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  54411. ((func_args*)args)->return_code = EXPECT_RESULT();
  54412. wolfSSL_free(ssl);
  54413. wolfSSL_CTX_free(ctx);
  54414. CloseSocket(cfd);
  54415. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  54416. wc_ecc_fp_free(); /* free per thread cache */
  54417. #endif
  54418. WOLFSSL_RETURN_FROM_THREAD(0);
  54419. }
  54420. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  54421. {
  54422. EXPECT_DECLS;
  54423. callback_functions* callbacks = NULL;
  54424. WOLFSSL_CTX* ctx = NULL;
  54425. WOLFSSL* ssl = NULL;
  54426. SOCKET_T sfd = 0;
  54427. char msg[] = "hello wolfssl server!";
  54428. int len = (int) XSTRLEN(msg);
  54429. char input[1024];
  54430. int idx;
  54431. int ret, err;
  54432. if (!args)
  54433. WOLFSSL_RETURN_FROM_THREAD(0);
  54434. ((func_args*)args)->return_code = TEST_FAIL;
  54435. callbacks = ((func_args*)args)->callbacks;
  54436. ctx = wolfSSL_CTX_new(callbacks->method());
  54437. #ifdef WOLFSSL_TIRTOS
  54438. fdOpenSession(Task_self());
  54439. #endif
  54440. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  54441. caCertFile, 0));
  54442. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  54443. cliCertFile, WOLFSSL_FILETYPE_PEM));
  54444. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  54445. cliKeyFile, WOLFSSL_FILETYPE_PEM));
  54446. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  54447. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  54448. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  54449. if (EXPECT_SUCCESS()) {
  54450. do {
  54451. err = 0; /* Reset error */
  54452. ret = wolfSSL_connect(ssl);
  54453. if (ret != WOLFSSL_SUCCESS) {
  54454. err = wolfSSL_get_error(ssl, 0);
  54455. }
  54456. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  54457. }
  54458. ExpectIntGE(wolfSSL_write(ssl, msg, len), 0);
  54459. if (EXPECT_SUCCESS()) {
  54460. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  54461. input[idx] = 0;
  54462. }
  54463. }
  54464. if (EXPECT_SUCCESS()) {
  54465. ret = wolfSSL_shutdown(ssl);
  54466. if (ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  54467. ret = wolfSSL_shutdown(ssl);
  54468. }
  54469. }
  54470. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  54471. ((func_args*)args)->return_code = EXPECT_RESULT();
  54472. wolfSSL_free(ssl);
  54473. wolfSSL_CTX_free(ctx);
  54474. CloseSocket(sfd);
  54475. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  54476. wc_ecc_fp_free(); /* free per thread cache */
  54477. #endif
  54478. WOLFSSL_RETURN_FROM_THREAD(0);
  54479. }
  54480. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  54481. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  54482. /* This test is to check wolfSSL_read behaves as same as
  54483. * openSSL when it is called after SSL_shutdown completes.
  54484. */
  54485. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  54486. {
  54487. EXPECT_DECLS;
  54488. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  54489. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  54490. tcp_ready ready;
  54491. func_args client_args;
  54492. func_args server_args;
  54493. THREAD_TYPE serverThread;
  54494. THREAD_TYPE clientThread;
  54495. callback_functions server_cbf;
  54496. callback_functions client_cbf;
  54497. #ifdef WOLFSSL_TIRTOS
  54498. fdOpenSession(Task_self());
  54499. #endif
  54500. StartTCP();
  54501. InitTcpReady(&ready);
  54502. #if defined(USE_WINDOWS_API)
  54503. /* use RNG to get random port if using windows */
  54504. ready.port = GetRandomPort();
  54505. #endif
  54506. XMEMSET(&client_args, 0, sizeof(func_args));
  54507. XMEMSET(&server_args, 0, sizeof(func_args));
  54508. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  54509. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  54510. server_cbf.method = wolfTLSv1_2_server_method;
  54511. client_cbf.method = wolfTLSv1_2_client_method;
  54512. server_args.callbacks = &server_cbf;
  54513. client_args.callbacks = &client_cbf;
  54514. server_args.signal = &ready;
  54515. client_args.signal = &ready;
  54516. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  54517. wait_tcp_ready(&server_args);
  54518. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  54519. join_thread(clientThread);
  54520. join_thread(serverThread);
  54521. ExpectTrue(client_args.return_code);
  54522. ExpectTrue(server_args.return_code);
  54523. FreeTcpReady(&ready);
  54524. #endif
  54525. return EXPECT_RESULT();
  54526. }
  54527. static int test_wolfSSL_CTX_get_min_proto_version(void)
  54528. {
  54529. EXPECT_DECLS;
  54530. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  54531. WOLFSSL_CTX *ctx = NULL;
  54532. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  54533. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  54534. WOLFSSL_SUCCESS);
  54535. #ifdef WOLFSSL_ALLOW_SSLV3
  54536. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  54537. #else
  54538. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  54539. #endif
  54540. wolfSSL_CTX_free(ctx);
  54541. ctx = NULL;
  54542. #ifdef WOLFSSL_ALLOW_TLSV10
  54543. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  54544. #else
  54545. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  54546. #endif
  54547. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION),
  54548. WOLFSSL_SUCCESS);
  54549. #ifdef WOLFSSL_ALLOW_TLSV10
  54550. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  54551. #else
  54552. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  54553. #endif
  54554. wolfSSL_CTX_free(ctx);
  54555. ctx = NULL;
  54556. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  54557. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION),
  54558. WOLFSSL_SUCCESS);
  54559. #ifndef NO_OLD_TLS
  54560. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  54561. #else
  54562. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  54563. #endif
  54564. wolfSSL_CTX_free(ctx);
  54565. ctx = NULL;
  54566. #ifndef WOLFSSL_NO_TLS12
  54567. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  54568. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION),
  54569. WOLFSSL_SUCCESS);
  54570. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  54571. wolfSSL_CTX_free(ctx);
  54572. ctx = NULL;
  54573. #endif
  54574. #ifdef WOLFSSL_TLS13
  54575. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  54576. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION),
  54577. WOLFSSL_SUCCESS);
  54578. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  54579. wolfSSL_CTX_free(ctx);
  54580. ctx = NULL;
  54581. #endif
  54582. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  54583. return EXPECT_RESULT();
  54584. }
  54585. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  54586. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  54587. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  54588. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  54589. static int test_wolfSSL_set_SSL_CTX(void)
  54590. {
  54591. EXPECT_DECLS;
  54592. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
  54593. && !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13) && \
  54594. !defined(NO_RSA)
  54595. WOLFSSL_CTX *ctx1 = NULL;
  54596. WOLFSSL_CTX *ctx2 = NULL;
  54597. WOLFSSL *ssl = NULL;
  54598. const byte *session_id1 = (const byte *)"CTX1";
  54599. const byte *session_id2 = (const byte *)"CTX2";
  54600. ExpectNotNull(ctx1 = wolfSSL_CTX_new(wolfTLS_server_method()));
  54601. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx1, svrCertFile,
  54602. WOLFSSL_FILETYPE_PEM));
  54603. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx1, svrKeyFile,
  54604. WOLFSSL_FILETYPE_PEM));
  54605. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx1, TLS1_2_VERSION),
  54606. WOLFSSL_SUCCESS);
  54607. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx1), TLS1_2_VERSION);
  54608. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx1), TLS1_3_VERSION);
  54609. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx1, session_id1, 4),
  54610. WOLFSSL_SUCCESS);
  54611. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfTLS_server_method()));
  54612. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx2, svrCertFile,
  54613. WOLFSSL_FILETYPE_PEM));
  54614. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx2, svrKeyFile,
  54615. WOLFSSL_FILETYPE_PEM));
  54616. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx2, TLS1_2_VERSION),
  54617. WOLFSSL_SUCCESS);
  54618. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx2, TLS1_2_VERSION),
  54619. WOLFSSL_SUCCESS);
  54620. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx2), TLS1_2_VERSION);
  54621. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx2), TLS1_2_VERSION);
  54622. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx2, session_id2, 4),
  54623. WOLFSSL_SUCCESS);
  54624. #ifdef HAVE_SESSION_TICKET
  54625. ExpectIntEQ((wolfSSL_CTX_get_options(ctx1) & SSL_OP_NO_TICKET), 0);
  54626. wolfSSL_CTX_set_options(ctx2, SSL_OP_NO_TICKET);
  54627. ExpectIntNE((wolfSSL_CTX_get_options(ctx2) & SSL_OP_NO_TICKET), 0);
  54628. #endif
  54629. ExpectNotNull(ssl = wolfSSL_new(ctx2));
  54630. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  54631. #ifdef WOLFSSL_INT_H
  54632. #ifdef WOLFSSL_SESSION_ID_CTX
  54633. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id2, 4), 0);
  54634. #endif
  54635. ExpectTrue(ssl->buffers.certificate == ctx2->certificate);
  54636. ExpectTrue(ssl->buffers.certChain == ctx2->certChain);
  54637. #endif
  54638. #ifdef HAVE_SESSION_TICKET
  54639. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  54640. #endif
  54641. /* Set the ctx1 that has TLSv1.3 as max proto version */
  54642. ExpectNotNull(wolfSSL_set_SSL_CTX(ssl, ctx1));
  54643. /* MUST not change proto versions of ssl */
  54644. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  54645. #ifdef HAVE_SESSION_TICKET
  54646. /* MUST not change */
  54647. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  54648. #endif
  54649. /* MUST change */
  54650. #ifdef WOLFSSL_INT_H
  54651. ExpectTrue(ssl->buffers.certificate == ctx1->certificate);
  54652. ExpectTrue(ssl->buffers.certChain == ctx1->certChain);
  54653. #ifdef WOLFSSL_SESSION_ID_CTX
  54654. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id1, 4), 0);
  54655. #endif
  54656. #endif
  54657. wolfSSL_free(ssl);
  54658. wolfSSL_CTX_free(ctx1);
  54659. wolfSSL_CTX_free(ctx2);
  54660. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  54661. return EXPECT_RESULT();
  54662. }
  54663. #endif /* defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  54664. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  54665. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  54666. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))) */
  54667. static int test_wolfSSL_security_level(void)
  54668. {
  54669. EXPECT_DECLS;
  54670. #if defined(OPENSSL_EXTRA)
  54671. SSL_CTX *ctx = NULL;
  54672. #ifdef WOLFSSL_TLS13
  54673. #ifdef NO_WOLFSSL_SERVER
  54674. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  54675. #else
  54676. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  54677. #endif
  54678. SSL_CTX_set_security_level(NULL, 1);
  54679. SSL_CTX_set_security_level(ctx, 1);
  54680. ExpectIntEQ(SSL_CTX_get_security_level(NULL), 0);
  54681. /* Stub so nothing happens. */
  54682. ExpectIntEQ(SSL_CTX_get_security_level(ctx), 0);
  54683. SSL_CTX_free(ctx);
  54684. #else
  54685. (void)ctx;
  54686. #endif
  54687. #endif
  54688. return EXPECT_RESULT();
  54689. }
  54690. static int test_wolfSSL_SSL_in_init(void)
  54691. {
  54692. EXPECT_DECLS;
  54693. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  54694. SSL_CTX* ctx = NULL;
  54695. SSL* ssl = NULL;
  54696. const char* testCertFile;
  54697. const char* testKeyFile;
  54698. #ifdef WOLFSSL_TLS13
  54699. #ifdef NO_WOLFSSL_SERVER
  54700. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  54701. #else
  54702. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  54703. #endif
  54704. #else
  54705. #ifdef NO_WOLFSSL_SERVER
  54706. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  54707. #else
  54708. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  54709. #endif
  54710. #endif
  54711. #ifndef NO_RSA
  54712. testCertFile = svrCertFile;
  54713. testKeyFile = svrKeyFile;
  54714. #elif defined(HAVE_ECC)
  54715. testCertFile = eccCertFile;
  54716. testKeyFile = eccKeyFile;
  54717. #else
  54718. testCertFile = NULL;
  54719. testKeyFile = NULL;
  54720. #endif
  54721. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  54722. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  54723. SSL_FILETYPE_PEM));
  54724. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  54725. SSL_FILETYPE_PEM));
  54726. }
  54727. ExpectNotNull(ssl = SSL_new(ctx));
  54728. ExpectIntEQ(SSL_in_init(ssl), 1);
  54729. SSL_CTX_free(ctx);
  54730. SSL_free(ssl);
  54731. #endif
  54732. return EXPECT_RESULT();
  54733. }
  54734. static int test_wolfSSL_CTX_set_timeout(void)
  54735. {
  54736. EXPECT_DECLS;
  54737. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  54738. int timeout;
  54739. WOLFSSL_CTX* ctx = NULL;
  54740. (void)timeout;
  54741. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  54742. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  54743. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  54744. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  54745. */
  54746. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  54747. /* giving 0 as timeout value sets default timeout */
  54748. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  54749. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  54750. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  54751. #else
  54752. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  54753. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  54754. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  54755. #endif
  54756. wolfSSL_CTX_free(ctx);
  54757. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  54758. return EXPECT_RESULT();
  54759. }
  54760. static int test_wolfSSL_OpenSSL_version(void)
  54761. {
  54762. EXPECT_DECLS;
  54763. #if defined(OPENSSL_EXTRA)
  54764. const char* ver;
  54765. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  54766. ExpectNotNull(ver = OpenSSL_version(0));
  54767. #else
  54768. ExpectNotNull(ver = OpenSSL_version());
  54769. #endif
  54770. ExpectIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  54771. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  54772. #endif
  54773. return EXPECT_RESULT();
  54774. }
  54775. static int test_CONF_CTX_CMDLINE(void)
  54776. {
  54777. EXPECT_DECLS;
  54778. #if defined(OPENSSL_ALL)
  54779. SSL_CTX* ctx = NULL;
  54780. SSL_CONF_CTX* cctx = NULL;
  54781. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  54782. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  54783. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  54784. /* set flags */
  54785. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  54786. WOLFSSL_CONF_FLAG_CMDLINE);
  54787. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  54788. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  54789. /* cmd invalid command */
  54790. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  54791. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  54792. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  54793. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  54794. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  54795. /* cmd Certificate and Private Key*/
  54796. {
  54797. #if !defined(NO_CERTS) && !defined(NO_RSA)
  54798. const char* ourCert = svrCertFile;
  54799. const char* ourKey = svrKeyFile;
  54800. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  54801. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert), WOLFSSL_SUCCESS);
  54802. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  54803. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  54804. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54805. #endif
  54806. }
  54807. /* cmd curves */
  54808. {
  54809. #if defined(HAVE_ECC)
  54810. const char* curve = "secp256r1";
  54811. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  54812. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  54813. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54814. #endif
  54815. }
  54816. /* cmd CipherString */
  54817. {
  54818. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  54819. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  54820. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  54821. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54822. }
  54823. /* cmd DH parameter */
  54824. {
  54825. #if !defined(NO_DH) && !defined(NO_BIO)
  54826. const char* ourdhcert = "./certs/dh2048.pem";
  54827. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL), -3);
  54828. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert), WOLFSSL_SUCCESS);
  54829. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54830. #endif
  54831. }
  54832. SSL_CTX_free(ctx);
  54833. SSL_CONF_CTX_free(cctx);
  54834. #endif /* OPENSSL_EXTRA */
  54835. return EXPECT_RESULT();
  54836. }
  54837. static int test_CONF_CTX_FILE(void)
  54838. {
  54839. EXPECT_DECLS;
  54840. #if defined(OPENSSL_ALL)
  54841. SSL_CTX* ctx = NULL;
  54842. SSL_CONF_CTX* cctx = NULL;
  54843. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  54844. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  54845. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  54846. /* set flags */
  54847. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  54848. WOLFSSL_CONF_FLAG_FILE);
  54849. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  54850. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  54851. /* sanity check */
  54852. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  54853. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  54854. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  54855. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  54856. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  54857. /* cmd Certificate and Private Key*/
  54858. {
  54859. #if !defined(NO_CERTS) && !defined(NO_RSA)
  54860. const char* ourCert = svrCertFile;
  54861. const char* ourKey = svrKeyFile;
  54862. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  54863. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  54864. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  54865. WOLFSSL_SUCCESS);
  54866. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  54867. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54868. #endif
  54869. }
  54870. /* cmd curves */
  54871. {
  54872. #if defined(HAVE_ECC)
  54873. const char* curve = "secp256r1";
  54874. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  54875. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  54876. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54877. #endif
  54878. }
  54879. /* cmd CipherString */
  54880. {
  54881. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  54882. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  54883. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher),
  54884. WOLFSSL_SUCCESS);
  54885. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54886. }
  54887. /* cmd DH parameter */
  54888. {
  54889. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  54890. const char* ourdhcert = "./certs/dh3072.pem";
  54891. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  54892. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  54893. WOLFSSL_SUCCESS);
  54894. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54895. #endif
  54896. }
  54897. SSL_CTX_free(ctx);
  54898. SSL_CONF_CTX_free(cctx);
  54899. #endif /* OPENSSL_EXTRA */
  54900. return EXPECT_RESULT();
  54901. }
  54902. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  54903. {
  54904. EXPECT_DECLS;
  54905. #ifdef HAVE_EX_DATA
  54906. int idx1, idx2;
  54907. /* test for unsupported class index */
  54908. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  54909. 0,NULL, NULL, NULL, NULL ), -1);
  54910. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(
  54911. WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  54912. 0,NULL, NULL, NULL, NULL ), -1);
  54913. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  54914. 0,NULL, NULL, NULL, NULL ), -1);
  54915. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  54916. 0,NULL, NULL, NULL, NULL ), -1);
  54917. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  54918. 0,NULL, NULL, NULL, NULL ), -1);
  54919. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  54920. 0,NULL, NULL, NULL, NULL ), -1);
  54921. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  54922. 0,NULL, NULL, NULL, NULL ), -1);
  54923. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  54924. 0,NULL, NULL, NULL, NULL ), -1);
  54925. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  54926. 0,NULL, NULL, NULL, NULL ), -1);
  54927. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  54928. 0,NULL, NULL, NULL, NULL ), -1);
  54929. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  54930. 0,NULL, NULL, NULL, NULL ), -1);
  54931. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  54932. 0,NULL, NULL, NULL, NULL ), -1);
  54933. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20,
  54934. 0,NULL, NULL, NULL, NULL ), -1);
  54935. /* test for supported class index */
  54936. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  54937. 0,NULL, NULL, NULL, NULL );
  54938. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  54939. 0,NULL, NULL, NULL, NULL );
  54940. ExpectIntNE(idx1, -1);
  54941. ExpectIntNE(idx2, -1);
  54942. ExpectIntNE(idx1, idx2);
  54943. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  54944. 0,NULL, NULL, NULL, NULL );
  54945. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  54946. 0,NULL, NULL, NULL, NULL );
  54947. ExpectIntNE(idx1, -1);
  54948. ExpectIntNE(idx2, -1);
  54949. ExpectIntNE(idx1, idx2);
  54950. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  54951. 0,NULL, NULL, NULL, NULL );
  54952. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  54953. 0,NULL, NULL, NULL, NULL );
  54954. ExpectIntNE(idx1, -1);
  54955. ExpectIntNE(idx2, -1);
  54956. ExpectIntNE(idx1, idx2);
  54957. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  54958. 0,NULL, NULL, NULL, NULL );
  54959. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  54960. 0,NULL, NULL, NULL, NULL );
  54961. ExpectIntNE(idx1, -1);
  54962. ExpectIntNE(idx2, -1);
  54963. ExpectIntNE(idx1, idx2);
  54964. #endif /* HAVE_EX_DATA */
  54965. return EXPECT_RESULT();
  54966. }
  54967. #if defined(HAVE_EX_DATA) && defined(HAVE_EXT_CACHE) && \
  54968. (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  54969. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  54970. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  54971. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))))
  54972. #define SESSION_NEW_IDX_LONG 0xDEADBEEF
  54973. #define SESSION_NEW_IDX_VAL ((void*)0xAEADAEAD)
  54974. #define SESSION_DUP_IDX_VAL ((void*)0xDEDEDEDE)
  54975. #define SESSION_NEW_IDX_PTR "Testing"
  54976. static void test_wolfSSL_SESSION_get_ex_new_index_new_cb(void* p, void* ptr,
  54977. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  54978. {
  54979. AssertNotNull(p);
  54980. AssertNull(ptr);
  54981. AssertIntEQ(CRYPTO_set_ex_data(a, idx, SESSION_NEW_IDX_VAL), SSL_SUCCESS);
  54982. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  54983. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  54984. }
  54985. static int test_wolfSSL_SESSION_get_ex_new_index_dup_cb(CRYPTO_EX_DATA* out,
  54986. const CRYPTO_EX_DATA* in, void* inPtr, int idx, long argV,
  54987. void* arg)
  54988. {
  54989. EXPECT_DECLS;
  54990. ExpectNotNull(out);
  54991. ExpectNotNull(in);
  54992. ExpectPtrEq(*(void**)inPtr, SESSION_NEW_IDX_VAL);
  54993. ExpectPtrEq(CRYPTO_get_ex_data(in, idx), SESSION_NEW_IDX_VAL);
  54994. ExpectPtrEq(CRYPTO_get_ex_data(out, idx), SESSION_NEW_IDX_VAL);
  54995. ExpectIntEQ(argV, SESSION_NEW_IDX_LONG);
  54996. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  54997. *(void**)inPtr = SESSION_DUP_IDX_VAL;
  54998. if (EXPECT_SUCCESS()) {
  54999. return SSL_SUCCESS;
  55000. }
  55001. else {
  55002. return SSL_FAILURE;
  55003. }
  55004. }
  55005. static int test_wolfSSL_SESSION_get_ex_new_index_free_cb_called = 0;
  55006. static void test_wolfSSL_SESSION_get_ex_new_index_free_cb(void* p, void* ptr,
  55007. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  55008. {
  55009. EXPECT_DECLS;
  55010. ExpectNotNull(p);
  55011. ExpectNull(ptr);
  55012. ExpectPtrNE(CRYPTO_get_ex_data(a, idx), 0);
  55013. ExpectIntEQ(argValue, SESSION_NEW_IDX_LONG);
  55014. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  55015. if (EXPECT_SUCCESS()) {
  55016. test_wolfSSL_SESSION_get_ex_new_index_free_cb_called++;
  55017. }
  55018. }
  55019. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  55020. {
  55021. EXPECT_DECLS;
  55022. int idx = SSL_SESSION_get_ex_new_index(SESSION_NEW_IDX_LONG,
  55023. (void*)SESSION_NEW_IDX_PTR,
  55024. test_wolfSSL_SESSION_get_ex_new_index_new_cb,
  55025. test_wolfSSL_SESSION_get_ex_new_index_dup_cb,
  55026. test_wolfSSL_SESSION_get_ex_new_index_free_cb);
  55027. SSL_SESSION* s = SSL_SESSION_new();
  55028. SSL_SESSION* d = NULL;
  55029. ExpectNotNull(s);
  55030. ExpectPtrEq(SSL_SESSION_get_ex_data(s, idx), SESSION_NEW_IDX_VAL);
  55031. ExpectNotNull(d = SSL_SESSION_dup(s));
  55032. ExpectPtrEq(SSL_SESSION_get_ex_data(d, idx), SESSION_DUP_IDX_VAL);
  55033. SSL_SESSION_free(s);
  55034. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 1);
  55035. SSL_SESSION_free(d);
  55036. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 2);
  55037. crypto_ex_cb_free(crypto_ex_cb_ctx_session);
  55038. crypto_ex_cb_ctx_session = NULL;
  55039. return EXPECT_RESULT();
  55040. }
  55041. #else
  55042. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  55043. {
  55044. return TEST_SKIPPED;
  55045. }
  55046. #endif
  55047. static int test_wolfSSL_set_psk_use_session_callback(void)
  55048. {
  55049. EXPECT_DECLS;
  55050. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  55051. SSL_CTX* ctx = NULL;
  55052. SSL* ssl = NULL;
  55053. const char* testCertFile;
  55054. const char* testKeyFile;
  55055. #ifdef WOLFSSL_TLS13
  55056. #ifdef NO_WOLFSSL_SERVER
  55057. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  55058. #else
  55059. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  55060. #endif
  55061. #else
  55062. #ifdef NO_WOLFSSL_SERVER
  55063. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  55064. #else
  55065. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  55066. #endif
  55067. #endif
  55068. #ifndef NO_RSA
  55069. testCertFile = svrCertFile;
  55070. testKeyFile = svrKeyFile;
  55071. #elif defined(HAVE_ECC)
  55072. testCertFile = eccCertFile;
  55073. testKeyFile = eccKeyFile;
  55074. #else
  55075. testCertFile = NULL;
  55076. testKeyFile = NULL;
  55077. #endif
  55078. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  55079. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  55080. SSL_FILETYPE_PEM));
  55081. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  55082. SSL_FILETYPE_PEM));
  55083. }
  55084. ExpectNotNull(ssl = SSL_new(ctx));
  55085. SSL_set_psk_use_session_callback(ssl, my_psk_use_session_cb);
  55086. SSL_CTX_free(ctx);
  55087. SSL_free(ssl);
  55088. #endif
  55089. return EXPECT_RESULT();
  55090. }
  55091. static int test_wolfSSL_ERR_strings(void)
  55092. {
  55093. EXPECT_DECLS;
  55094. #if !defined(NO_ERROR_STRINGS)
  55095. const char* err1 = "unsupported cipher suite";
  55096. const char* err2 = "wolfSSL PEM routines";
  55097. const char* err = NULL;
  55098. (void)err;
  55099. (void)err1;
  55100. (void)err2;
  55101. #if defined(OPENSSL_EXTRA)
  55102. ExpectNotNull(err = ERR_reason_error_string(UNSUPPORTED_SUITE));
  55103. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  55104. ExpectNotNull(err = ERR_func_error_string(UNSUPPORTED_SUITE));
  55105. ExpectIntEQ((*err == '\0'), 1);
  55106. ExpectNotNull(err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD));
  55107. ExpectIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  55108. #else
  55109. ExpectNotNull(err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE));
  55110. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  55111. ExpectNotNull(err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE));
  55112. ExpectIntEQ((*err == '\0'), 1);
  55113. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  55114. ExpectNotNull(err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2));
  55115. ExpectIntEQ((*err == '\0'), 1);
  55116. #endif
  55117. #endif
  55118. return EXPECT_RESULT();
  55119. }
  55120. static int test_wolfSSL_EVP_shake128(void)
  55121. {
  55122. EXPECT_DECLS;
  55123. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  55124. defined(WOLFSSL_SHAKE128)
  55125. const EVP_MD* md = NULL;
  55126. ExpectNotNull(md = EVP_shake128());
  55127. ExpectIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  55128. #endif
  55129. return EXPECT_RESULT();
  55130. }
  55131. static int test_wolfSSL_EVP_shake256(void)
  55132. {
  55133. EXPECT_DECLS;
  55134. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  55135. defined(WOLFSSL_SHAKE256)
  55136. const EVP_MD* md = NULL;
  55137. ExpectNotNull(md = EVP_shake256());
  55138. ExpectIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  55139. #endif
  55140. return EXPECT_RESULT();
  55141. }
  55142. /*
  55143. * Testing EVP digest API with SM3
  55144. */
  55145. static int test_wolfSSL_EVP_sm3(void)
  55146. {
  55147. int res = TEST_SKIPPED;
  55148. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM3)
  55149. EXPECT_DECLS;
  55150. const EVP_MD* md = NULL;
  55151. EVP_MD_CTX* mdCtx = NULL;
  55152. byte data[WC_SM3_BLOCK_SIZE * 4];
  55153. byte hash[WC_SM3_DIGEST_SIZE];
  55154. byte calcHash[WC_SM3_DIGEST_SIZE];
  55155. byte expHash[WC_SM3_DIGEST_SIZE] = {
  55156. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  55157. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  55158. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  55159. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  55160. };
  55161. word32 chunk;
  55162. word32 i;
  55163. unsigned int sz;
  55164. int ret;
  55165. XMEMSET(data, 0, sizeof(data));
  55166. md = EVP_sm3();
  55167. ExpectTrue(md != NULL);
  55168. ExpectIntEQ(XSTRNCMP(md, "SM3", XSTRLEN("SM3")), 0);
  55169. mdCtx = EVP_MD_CTX_new();
  55170. ExpectTrue(mdCtx != NULL);
  55171. /* Invalid Parameters */
  55172. ExpectIntEQ(EVP_DigestInit(NULL, md), BAD_FUNC_ARG);
  55173. /* Valid Parameters */
  55174. ExpectIntEQ(EVP_DigestInit(mdCtx, md), WOLFSSL_SUCCESS);
  55175. ExpectIntEQ(EVP_DigestUpdate(NULL, NULL, 1), WOLFSSL_FAILURE);
  55176. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 1), WOLFSSL_FAILURE);
  55177. ExpectIntEQ(EVP_DigestUpdate(NULL, data, 1), WOLFSSL_FAILURE);
  55178. /* Valid Parameters */
  55179. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 0), WOLFSSL_SUCCESS);
  55180. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  55181. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  55182. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE),
  55183. WOLFSSL_SUCCESS);
  55184. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE - 2),
  55185. WOLFSSL_SUCCESS);
  55186. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE * 2),
  55187. WOLFSSL_SUCCESS);
  55188. /* Ensure too many bytes for lengths. */
  55189. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_PAD_SIZE),
  55190. WOLFSSL_SUCCESS);
  55191. /* Invalid Parameters */
  55192. ExpectIntEQ(EVP_DigestFinal(NULL, NULL, NULL), WOLFSSL_FAILURE);
  55193. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  55194. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  55195. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  55196. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  55197. /* Valid Parameters */
  55198. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  55199. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  55200. /* Chunk tests. */
  55201. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, sizeof(data)), WOLFSSL_SUCCESS);
  55202. ExpectIntEQ(EVP_DigestFinal(mdCtx, calcHash, &sz), WOLFSSL_SUCCESS);
  55203. ExpectIntEQ(sz, WC_SM3_DIGEST_SIZE);
  55204. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  55205. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  55206. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i, chunk),
  55207. WOLFSSL_SUCCESS);
  55208. }
  55209. if (i < (word32)sizeof(data)) {
  55210. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i,
  55211. (word32)sizeof(data) - i), WOLFSSL_SUCCESS);
  55212. }
  55213. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  55214. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  55215. }
  55216. /* Not testing when the low 32-bit length overflows. */
  55217. ret = EVP_MD_CTX_cleanup(mdCtx);
  55218. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  55219. wolfSSL_EVP_MD_CTX_free(mdCtx);
  55220. res = EXPECT_RESULT();
  55221. #endif
  55222. return res;
  55223. } /* END test_EVP_sm3 */
  55224. static int test_EVP_blake2(void)
  55225. {
  55226. EXPECT_DECLS;
  55227. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  55228. const EVP_MD* md = NULL;
  55229. (void)md;
  55230. #if defined(HAVE_BLAKE2)
  55231. ExpectNotNull(md = EVP_blake2b512());
  55232. ExpectIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  55233. #endif
  55234. #if defined(HAVE_BLAKE2S)
  55235. ExpectNotNull(md = EVP_blake2s256());
  55236. ExpectIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  55237. #endif
  55238. #endif
  55239. return EXPECT_RESULT();
  55240. }
  55241. #if defined(OPENSSL_EXTRA)
  55242. static void list_md_fn(const EVP_MD* m, const char* from,
  55243. const char* to, void* arg)
  55244. {
  55245. const char* mn;
  55246. BIO *bio;
  55247. (void) from;
  55248. (void) to;
  55249. (void) arg;
  55250. (void) mn;
  55251. (void) bio;
  55252. if (!m) {
  55253. /* alias */
  55254. AssertNull(m);
  55255. AssertNotNull(to);
  55256. }
  55257. else {
  55258. AssertNotNull(m);
  55259. AssertNull(to);
  55260. }
  55261. AssertNotNull(from);
  55262. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  55263. mn = EVP_get_digestbyname(from);
  55264. /* print to stderr */
  55265. AssertNotNull(arg);
  55266. bio = BIO_new(BIO_s_file());
  55267. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  55268. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  55269. BIO_free(bio);
  55270. #endif
  55271. }
  55272. #endif
  55273. static int test_EVP_MD_do_all(void)
  55274. {
  55275. int res = TEST_SKIPPED;
  55276. #if defined(OPENSSL_EXTRA)
  55277. EVP_MD_do_all(NULL, stderr);
  55278. EVP_MD_do_all(list_md_fn, stderr);
  55279. res = TEST_SUCCESS;
  55280. #endif
  55281. return res;
  55282. }
  55283. #if defined(OPENSSL_EXTRA)
  55284. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  55285. {
  55286. (void)arg;
  55287. (void)nm;
  55288. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  55289. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  55290. /* print to stderr */
  55291. AssertNotNull(arg);
  55292. BIO *bio = BIO_new(BIO_s_file());
  55293. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  55294. BIO_printf(bio, "%s\n", nm);
  55295. BIO_free(bio);
  55296. #endif
  55297. }
  55298. #endif
  55299. static int test_OBJ_NAME_do_all(void)
  55300. {
  55301. int res = TEST_SKIPPED;
  55302. #if defined(OPENSSL_EXTRA)
  55303. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  55304. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
  55305. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
  55306. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
  55307. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
  55308. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
  55309. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
  55310. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
  55311. OBJ_NAME_do_all(-1, obj_name_t, stderr);
  55312. res = TEST_SUCCESS;
  55313. #endif
  55314. return res;
  55315. }
  55316. static int test_SSL_CIPHER_get_xxx(void)
  55317. {
  55318. EXPECT_DECLS;
  55319. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  55320. !defined(NO_FILESYSTEM)
  55321. const SSL_CIPHER* cipher = NULL;
  55322. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  55323. int i, numCiphers = 0;
  55324. SSL_CTX* ctx = NULL;
  55325. SSL* ssl = NULL;
  55326. const char* testCertFile;
  55327. const char* testKeyFile;
  55328. char buf[256] = {0};
  55329. const char* cipher_id = NULL;
  55330. int expect_nid1 = NID_undef;
  55331. int expect_nid2 = NID_undef;
  55332. int expect_nid3 = NID_undef;
  55333. int expect_nid4 = NID_undef;
  55334. int expect_nid5 = 0;
  55335. const char* cipher_id2 = NULL;
  55336. int expect_nid21 = NID_undef;
  55337. int expect_nid22 = NID_undef;
  55338. int expect_nid23 = NID_undef;
  55339. int expect_nid24 = NID_undef;
  55340. int expect_nid25 = 0;
  55341. (void)cipher;
  55342. (void)supportedCiphers;
  55343. (void)i;
  55344. (void)numCiphers;
  55345. (void)ctx;
  55346. (void)ssl;
  55347. (void)testCertFile;
  55348. (void)testKeyFile;
  55349. #if defined(WOLFSSL_TLS13)
  55350. cipher_id = "TLS13-AES128-GCM-SHA256";
  55351. expect_nid1 = NID_auth_rsa;
  55352. expect_nid2 = NID_aes_128_gcm;
  55353. expect_nid3 = NID_sha256;
  55354. expect_nid4 = NID_kx_any;
  55355. expect_nid5 = 1;
  55356. #if !defined(WOLFSSL_NO_TLS12)
  55357. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  55358. expect_nid21 = NID_auth_rsa;
  55359. expect_nid22 = NID_aes_256_gcm;
  55360. expect_nid23 = NID_sha384;
  55361. expect_nid24 = NID_kx_ecdhe;
  55362. expect_nid25 = 1;
  55363. #endif
  55364. #endif
  55365. #ifdef NO_WOLFSSL_SERVER
  55366. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  55367. #else
  55368. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  55369. #endif
  55370. if (cipher_id) {
  55371. #ifndef NO_RSA
  55372. testCertFile = svrCertFile;
  55373. testKeyFile = svrKeyFile;
  55374. #elif defined(HAVE_ECC)
  55375. testCertFile = eccCertFile;
  55376. testKeyFile = eccKeyFile;
  55377. #else
  55378. testCertFile = NULL;
  55379. testKeyFile = NULL;
  55380. #endif
  55381. if (testCertFile != NULL && testKeyFile != NULL) {
  55382. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  55383. SSL_FILETYPE_PEM));
  55384. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  55385. SSL_FILETYPE_PEM));
  55386. }
  55387. ExpectNotNull(ssl = SSL_new(ctx));
  55388. ExpectIntEQ(SSL_in_init(ssl), 1);
  55389. supportedCiphers = SSL_get_ciphers(ssl);
  55390. numCiphers = sk_num(supportedCiphers);
  55391. for (i = 0; i < numCiphers; ++i) {
  55392. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  55393. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  55394. }
  55395. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  55396. break;
  55397. }
  55398. }
  55399. /* test case for */
  55400. if (i != numCiphers) {
  55401. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  55402. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  55403. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  55404. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  55405. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  55406. }
  55407. if (cipher_id2) {
  55408. for (i = 0; i < numCiphers; ++i) {
  55409. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  55410. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  55411. }
  55412. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  55413. break;
  55414. }
  55415. }
  55416. /* test case for */
  55417. if (i != numCiphers) {
  55418. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  55419. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  55420. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  55421. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  55422. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  55423. }
  55424. }
  55425. }
  55426. SSL_CTX_free(ctx);
  55427. SSL_free(ssl);
  55428. #endif
  55429. return EXPECT_RESULT();
  55430. }
  55431. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  55432. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  55433. int* keyFormat)
  55434. {
  55435. int ret;
  55436. byte* key_buf = NULL;
  55437. size_t key_sz = 0;
  55438. EncryptedInfo encInfo;
  55439. XMEMSET(&encInfo, 0, sizeof(encInfo));
  55440. ret = load_file(privKeyFile, &key_buf, &key_sz);
  55441. if (ret == 0) {
  55442. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  55443. NULL, &encInfo, keyFormat);
  55444. }
  55445. if (key_buf != NULL) {
  55446. free(key_buf); key_buf = NULL;
  55447. }
  55448. (void)encInfo; /* not used in this test */
  55449. #ifdef DEBUG_WOLFSSL
  55450. fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  55451. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  55452. (*pDer)->length);
  55453. #endif
  55454. return ret;
  55455. }
  55456. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  55457. {
  55458. int ret = CRYPTOCB_UNAVAILABLE;
  55459. const char* privKeyFile = (const char*)ctx;
  55460. DerBuffer* pDer = NULL;
  55461. int keyFormat = 0;
  55462. if (info->algo_type == WC_ALGO_TYPE_PK) {
  55463. #ifdef DEBUG_WOLFSSL
  55464. fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  55465. #endif
  55466. #ifndef NO_RSA
  55467. if (info->pk.type == WC_PK_TYPE_RSA) {
  55468. switch (info->pk.rsa.type) {
  55469. case RSA_PUBLIC_ENCRYPT:
  55470. case RSA_PUBLIC_DECRYPT:
  55471. /* perform software based RSA public op */
  55472. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  55473. break;
  55474. case RSA_PRIVATE_ENCRYPT:
  55475. case RSA_PRIVATE_DECRYPT:
  55476. {
  55477. RsaKey key;
  55478. /* perform software based RSA private op */
  55479. #ifdef DEBUG_WOLFSSL
  55480. fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
  55481. #endif
  55482. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  55483. &keyFormat);
  55484. if (ret != 0) {
  55485. return ret;
  55486. }
  55487. ret = wc_InitRsaKey(&key, HEAP_HINT);
  55488. if (ret == 0) {
  55489. word32 keyIdx = 0;
  55490. /* load RSA private key and perform private transform */
  55491. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  55492. &key, pDer->length);
  55493. if (ret == 0) {
  55494. ret = wc_RsaFunction(
  55495. info->pk.rsa.in, info->pk.rsa.inLen,
  55496. info->pk.rsa.out, info->pk.rsa.outLen,
  55497. info->pk.rsa.type, &key, info->pk.rsa.rng);
  55498. }
  55499. else {
  55500. /* if decode fails, then fall-back to software based crypto */
  55501. fprintf(stderr, "test_CryptoCb_Func: RSA private "
  55502. "key decode failed %d, falling back to "
  55503. "software\n", ret);
  55504. ret = CRYPTOCB_UNAVAILABLE;
  55505. }
  55506. wc_FreeRsaKey(&key);
  55507. }
  55508. wc_FreeDer(&pDer); pDer = NULL;
  55509. break;
  55510. }
  55511. }
  55512. #ifdef DEBUG_WOLFSSL
  55513. fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  55514. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  55515. #endif
  55516. }
  55517. #endif /* !NO_RSA */
  55518. #ifdef HAVE_ECC
  55519. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  55520. /* mark this key as ephemeral */
  55521. if (info->pk.eckg.key != NULL) {
  55522. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  55523. sizeof(info->pk.eckg.key->label));
  55524. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  55525. }
  55526. }
  55527. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  55528. ecc_key key;
  55529. /* perform software based ECC sign */
  55530. #ifdef DEBUG_WOLFSSL
  55531. fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
  55532. #endif
  55533. if (info->pk.eccsign.key != NULL &&
  55534. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  55535. /* this is an empheral key */
  55536. #ifdef DEBUG_WOLFSSL
  55537. fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
  55538. "ephemeral key\n");
  55539. #endif
  55540. return CRYPTOCB_UNAVAILABLE;
  55541. }
  55542. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  55543. if (ret != 0) {
  55544. return ret;
  55545. }
  55546. ret = wc_ecc_init(&key);
  55547. if (ret == 0) {
  55548. word32 keyIdx = 0;
  55549. /* load ECC private key and perform private transform */
  55550. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  55551. &key, pDer->length);
  55552. if (ret == 0) {
  55553. ret = wc_ecc_sign_hash(
  55554. info->pk.eccsign.in, info->pk.eccsign.inlen,
  55555. info->pk.eccsign.out, info->pk.eccsign.outlen,
  55556. info->pk.eccsign.rng, &key);
  55557. }
  55558. else {
  55559. /* if decode fails, then fall-back to software based crypto */
  55560. fprintf(stderr, "test_CryptoCb_Func: ECC private key "
  55561. "decode failed %d, falling back to software\n", ret);
  55562. ret = CRYPTOCB_UNAVAILABLE;
  55563. }
  55564. wc_ecc_free(&key);
  55565. }
  55566. wc_FreeDer(&pDer); pDer = NULL;
  55567. #ifdef DEBUG_WOLFSSL
  55568. fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  55569. ret, *info->pk.eccsign.outlen);
  55570. #endif
  55571. }
  55572. #endif /* HAVE_ECC */
  55573. #ifdef HAVE_ED25519
  55574. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  55575. ed25519_key key;
  55576. /* perform software based ED25519 sign */
  55577. #ifdef DEBUG_WOLFSSL
  55578. fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
  55579. #endif
  55580. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  55581. if (ret != 0) {
  55582. return ret;
  55583. }
  55584. ret = wc_ed25519_init(&key);
  55585. if (ret == 0) {
  55586. word32 keyIdx = 0;
  55587. /* load ED25519 private key and perform private transform */
  55588. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  55589. &key, pDer->length);
  55590. if (ret == 0) {
  55591. /* calculate public key */
  55592. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  55593. if (ret == 0) {
  55594. key.pubKeySet = 1;
  55595. ret = wc_ed25519_sign_msg_ex(
  55596. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  55597. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  55598. &key, info->pk.ed25519sign.type,
  55599. info->pk.ed25519sign.context,
  55600. info->pk.ed25519sign.contextLen);
  55601. }
  55602. }
  55603. else {
  55604. /* if decode fails, then fall-back to software based crypto */
  55605. fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
  55606. "decode failed %d, falling back to software\n", ret);
  55607. ret = CRYPTOCB_UNAVAILABLE;
  55608. }
  55609. wc_ed25519_free(&key);
  55610. }
  55611. wc_FreeDer(&pDer); pDer = NULL;
  55612. #ifdef DEBUG_WOLFSSL
  55613. fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  55614. ret, *info->pk.ed25519sign.outLen);
  55615. #endif
  55616. }
  55617. #endif /* HAVE_ED25519 */
  55618. }
  55619. (void)thisDevId;
  55620. (void)keyFormat;
  55621. return ret;
  55622. }
  55623. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  55624. static int test_wc_CryptoCb_TLS(int tlsVer,
  55625. const char* cliCaPemFile, const char* cliCertPemFile,
  55626. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  55627. const char* svrCaPemFile, const char* svrCertPemFile,
  55628. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  55629. {
  55630. EXPECT_DECLS;
  55631. callback_functions client_cbf;
  55632. callback_functions server_cbf;
  55633. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  55634. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  55635. if (tlsVer == WOLFSSL_TLSV1_3) {
  55636. #ifdef WOLFSSL_TLS13
  55637. server_cbf.method = wolfTLSv1_3_server_method;
  55638. client_cbf.method = wolfTLSv1_3_client_method;
  55639. #endif
  55640. }
  55641. else if (tlsVer == WOLFSSL_TLSV1_2) {
  55642. #ifndef WOLFSSL_NO_TLS12
  55643. server_cbf.method = wolfTLSv1_2_server_method;
  55644. client_cbf.method = wolfTLSv1_2_client_method;
  55645. #endif
  55646. }
  55647. else if (tlsVer == WOLFSSL_TLSV1_1) {
  55648. #ifndef NO_OLD_TLS
  55649. server_cbf.method = wolfTLSv1_1_server_method;
  55650. client_cbf.method = wolfTLSv1_1_client_method;
  55651. #endif
  55652. }
  55653. else if (tlsVer == WOLFSSL_TLSV1) {
  55654. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  55655. server_cbf.method = wolfTLSv1_server_method;
  55656. client_cbf.method = wolfTLSv1_client_method;
  55657. #endif
  55658. }
  55659. else if (tlsVer == WOLFSSL_SSLV3) {
  55660. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  55661. defined(WOLFSSL_STATIC_RSA)
  55662. server_cbf.method = wolfSSLv3_server_method;
  55663. client_cbf.method = wolfSSLv3_client_method;
  55664. #endif
  55665. }
  55666. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  55667. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  55668. server_cbf.method = wolfDTLSv1_2_server_method;
  55669. client_cbf.method = wolfDTLSv1_2_client_method;
  55670. #endif
  55671. }
  55672. else if (tlsVer == WOLFSSL_DTLSV1) {
  55673. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  55674. server_cbf.method = wolfDTLSv1_server_method;
  55675. client_cbf.method = wolfDTLSv1_client_method;
  55676. #endif
  55677. }
  55678. if (server_cbf.method == NULL) {
  55679. /* not enabled */
  55680. return TEST_SUCCESS;
  55681. }
  55682. /* Setup the keys for the TLS test */
  55683. client_cbf.certPemFile = cliCertPemFile;
  55684. client_cbf.keyPemFile = cliPubKeyPemFile;
  55685. client_cbf.caPemFile = cliCaPemFile;
  55686. server_cbf.certPemFile = svrCertPemFile;
  55687. server_cbf.keyPemFile = svrPubKeyPemFile;
  55688. server_cbf.caPemFile = svrCaPemFile;
  55689. /* Setup a crypto callback with pointer to private key file for testing */
  55690. client_cbf.devId = 1;
  55691. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  55692. (void*)cliPrivKeyPemFile);
  55693. server_cbf.devId = 2;
  55694. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  55695. (void*)svrPrivKeyPemFile);
  55696. /* Perform TLS server and client test */
  55697. /* First test is at WOLFSSL_CTX level */
  55698. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  55699. /* Check for success */
  55700. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  55701. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  55702. if (EXPECT_SUCCESS()) {
  55703. /* Second test is a WOLFSSL object level */
  55704. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  55705. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  55706. }
  55707. /* Check for success */
  55708. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  55709. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  55710. /* Un register the devId's */
  55711. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  55712. client_cbf.devId = INVALID_DEVID;
  55713. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  55714. server_cbf.devId = INVALID_DEVID;
  55715. return EXPECT_RESULT();
  55716. }
  55717. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  55718. static int test_wc_CryptoCb(void)
  55719. {
  55720. EXPECT_DECLS;
  55721. #ifdef WOLF_CRYPTO_CB
  55722. /* TODO: Add crypto callback API tests */
  55723. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  55724. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  55725. int tlsVer;
  55726. #endif
  55727. #ifndef NO_RSA
  55728. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  55729. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  55730. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  55731. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile),
  55732. TEST_SUCCESS);
  55733. }
  55734. #endif
  55735. #ifdef HAVE_ECC
  55736. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  55737. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  55738. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  55739. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile),
  55740. TEST_SUCCESS);
  55741. }
  55742. #endif
  55743. #ifdef HAVE_ED25519
  55744. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  55745. if (tlsVer == WOLFSSL_DTLSV1) continue;
  55746. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  55747. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  55748. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile),
  55749. TEST_SUCCESS);
  55750. }
  55751. #endif
  55752. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  55753. #endif /* WOLF_CRYPTO_CB */
  55754. return EXPECT_RESULT();
  55755. }
  55756. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  55757. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  55758. static int test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  55759. const char* cliCaPemFile, const char* cliCertPemFile,
  55760. const char* cliPrivKeyPemFile,
  55761. const char* svrCaPemFile, const char* svrCertPemFile,
  55762. const char* svrPrivKeyPemFile,
  55763. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  55764. {
  55765. EXPECT_DECLS;
  55766. callback_functions client_cbf;
  55767. callback_functions server_cbf;
  55768. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  55769. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  55770. if (tlsVer == WOLFSSL_TLSV1_3) {
  55771. #ifdef WOLFSSL_TLS13
  55772. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  55773. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  55774. #endif
  55775. }
  55776. else if (tlsVer == WOLFSSL_TLSV1_2) {
  55777. #ifndef WOLFSSL_NO_TLS12
  55778. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  55779. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  55780. #endif
  55781. }
  55782. else if (tlsVer == WOLFSSL_TLSV1_1) {
  55783. #ifndef NO_OLD_TLS
  55784. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  55785. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  55786. #endif
  55787. }
  55788. else if (tlsVer == WOLFSSL_TLSV1) {
  55789. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  55790. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  55791. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  55792. #endif
  55793. }
  55794. else if (tlsVer == WOLFSSL_SSLV3) {
  55795. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  55796. defined(WOLFSSL_STATIC_RSA)
  55797. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  55798. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  55799. #endif
  55800. }
  55801. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  55802. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  55803. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  55804. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  55805. #endif
  55806. }
  55807. else if (tlsVer == WOLFSSL_DTLSV1) {
  55808. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  55809. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  55810. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  55811. #endif
  55812. }
  55813. if (server_cbf.method_ex == NULL) {
  55814. /* not enabled */
  55815. return TEST_SUCCESS;
  55816. }
  55817. /* Setup the keys for the TLS test */
  55818. client_cbf.certPemFile = cliCertPemFile;
  55819. client_cbf.keyPemFile = cliPrivKeyPemFile;
  55820. client_cbf.caPemFile = cliCaPemFile;
  55821. server_cbf.certPemFile = svrCertPemFile;
  55822. server_cbf.keyPemFile = svrPrivKeyPemFile;
  55823. server_cbf.caPemFile = svrCaPemFile;
  55824. client_cbf.mem = cliMem;
  55825. client_cbf.memSz = cliMemSz;
  55826. server_cbf.mem = svrMem;
  55827. server_cbf.memSz = svrMemSz;
  55828. client_cbf.devId = INVALID_DEVID;
  55829. server_cbf.devId = INVALID_DEVID;
  55830. /* Perform TLS server and client test */
  55831. /* First test is at WOLFSSL_CTX level */
  55832. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  55833. /* Check for success */
  55834. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  55835. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  55836. if (EXPECT_SUCCESS()) {
  55837. /* Second test is a WOLFSSL object level */
  55838. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  55839. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  55840. }
  55841. /* Check for success */
  55842. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  55843. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  55844. return EXPECT_RESULT();
  55845. }
  55846. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  55847. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  55848. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  55849. {
  55850. EXPECT_DECLS;
  55851. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  55852. WOLFSSL_MEM_STATS mem_stats;
  55853. WOLFSSL_MEM_CONN_STATS ssl_stats;
  55854. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  55855. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  55856. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  55857. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  55858. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  55859. #endif
  55860. ExpectNotNull((ssl1 = wolfSSL_new(ctx)));
  55861. ExpectNotNull((ssl2 = wolfSSL_new(ctx)));
  55862. /* this should fail because kMaxCtxClients == 2 */
  55863. ExpectNull((ssl3 = wolfSSL_new(ctx)));
  55864. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  55865. #ifdef DEBUG_WOLFSSL
  55866. wolfSSL_PrintStatsConn(&ssl_stats);
  55867. #endif
  55868. (void)ssl_stats;
  55869. }
  55870. /* display collected statistics */
  55871. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  55872. #ifdef DEBUG_WOLFSSL
  55873. wolfSSL_PrintStats(&mem_stats);
  55874. #endif
  55875. (void)mem_stats;
  55876. }
  55877. wolfSSL_free(ssl1);
  55878. wolfSSL_free(ssl2);
  55879. return EXPECT_RESULT();
  55880. }
  55881. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  55882. static int test_wolfSSL_CTX_StaticMemory(void)
  55883. {
  55884. EXPECT_DECLS;
  55885. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  55886. wolfSSL_method_func method_func;
  55887. WOLFSSL_CTX* ctx;
  55888. const int kMaxCtxClients = 2;
  55889. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  55890. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  55891. int tlsVer;
  55892. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  55893. #endif
  55894. #endif
  55895. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  55896. #ifndef NO_WOLFSSL_SERVER
  55897. #ifndef WOLFSSL_NO_TLS12
  55898. method_func = wolfTLSv1_2_server_method_ex;
  55899. #else
  55900. method_func = wolfTLSv1_3_server_method_ex;
  55901. #endif
  55902. #else
  55903. #ifndef WOLFSSL_NO_TLS12
  55904. method_func = wolfTLSv1_2_client_method_ex;
  55905. #else
  55906. method_func = wolfTLSv1_3_client_method_ex;
  55907. #endif
  55908. #endif
  55909. /* Test creating CTX directly from static memory pool */
  55910. ctx = NULL;
  55911. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, method_func, svrMem,
  55912. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  55913. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  55914. wolfSSL_CTX_free(ctx);
  55915. ctx = NULL;
  55916. /* Test for heap allocated CTX, then assigning static pool to it */
  55917. ExpectNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  55918. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, NULL, svrMem,
  55919. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  55920. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  55921. wolfSSL_CTX_free(ctx);
  55922. /* TLS Level Tests using static memory */
  55923. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  55924. #ifndef NO_RSA
  55925. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  55926. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  55927. svrCertFile, cliCertFile, cliKeyFile,
  55928. cliCertFile, svrCertFile, svrKeyFile,
  55929. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  55930. TEST_SUCCESS);
  55931. }
  55932. #endif
  55933. #ifdef HAVE_ECC
  55934. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  55935. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  55936. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  55937. cliEccCertFile, eccCertFile, eccKeyFile,
  55938. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  55939. TEST_SUCCESS);
  55940. }
  55941. #endif
  55942. #ifdef HAVE_ED25519
  55943. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  55944. if (tlsVer == WOLFSSL_DTLSV1) continue;
  55945. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  55946. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  55947. cliEdCertFile, edCertFile, edKeyFile,
  55948. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  55949. TEST_SUCCESS);
  55950. }
  55951. #endif
  55952. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  55953. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  55954. return EXPECT_RESULT();
  55955. }
  55956. static int test_openssl_FIPS_drbg(void)
  55957. {
  55958. EXPECT_DECLS;
  55959. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  55960. DRBG_CTX* dctx = NULL;
  55961. byte data1[32], data2[32], zeroData[32];
  55962. byte testSeed[16];
  55963. size_t dlen = sizeof(data1);
  55964. int i;
  55965. XMEMSET(data1, 0, dlen);
  55966. XMEMSET(data2, 0, dlen);
  55967. XMEMSET(zeroData, 0, sizeof(zeroData));
  55968. for (i = 0; i < (int)sizeof(testSeed); i++) {
  55969. testSeed[i] = (byte)i;
  55970. }
  55971. ExpectNotNull(dctx = FIPS_get_default_drbg());
  55972. ExpectIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  55973. ExpectIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  55974. WOLFSSL_SUCCESS);
  55975. ExpectIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  55976. ExpectIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  55977. WOLFSSL_SUCCESS);
  55978. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  55979. ExpectIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  55980. WOLFSSL_SUCCESS);
  55981. ExpectIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  55982. WOLFSSL_SUCCESS);
  55983. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  55984. ExpectIntNE(XMEMCMP(data1, data2, dlen), 0);
  55985. ExpectIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  55986. #ifndef HAVE_GLOBAL_RNG
  55987. /* gets freed by wolfSSL_Cleanup() when HAVE_GLOBAL_RNG defined */
  55988. wolfSSL_FIPS_drbg_free(dctx);
  55989. #endif
  55990. #endif
  55991. return EXPECT_RESULT();
  55992. }
  55993. static int test_wolfSSL_FIPS_mode(void)
  55994. {
  55995. EXPECT_DECLS;
  55996. #if defined(OPENSSL_ALL)
  55997. #ifdef HAVE_FIPS
  55998. ExpectIntEQ(wolfSSL_FIPS_mode(), 1);
  55999. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  56000. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  56001. #else
  56002. ExpectIntEQ(wolfSSL_FIPS_mode(), 0);
  56003. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  56004. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  56005. #endif
  56006. #endif
  56007. return EXPECT_RESULT();
  56008. }
  56009. #ifdef WOLFSSL_DTLS
  56010. /* Prints out the current window */
  56011. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  56012. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  56013. int i;
  56014. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  56015. word32 b = w[i];
  56016. int j;
  56017. /* Prints out a 32 bit binary number in big endian order */
  56018. for (j = 0; j < 32; j++, b <<= 1) {
  56019. if (b & (((word32)1) << 31))
  56020. fprintf(stderr, "1");
  56021. else
  56022. fprintf(stderr, "0");
  56023. }
  56024. fprintf(stderr, " ");
  56025. }
  56026. fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
  56027. #else
  56028. (void)h;
  56029. (void)l;
  56030. (void)w;
  56031. #endif
  56032. }
  56033. /* a - cur_hi
  56034. * b - cur_lo
  56035. * c - next_hi
  56036. * d - next_lo
  56037. * e - window
  56038. * f - expected next_hi
  56039. * g - expected next_lo
  56040. * h - expected window[1]
  56041. * i - expected window[0]
  56042. */
  56043. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  56044. ExpectIntEQ(wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)), 1); \
  56045. DUW_TEST_print_window_binary((a), (b), (e)); \
  56046. ExpectIntEQ((c), (f)); \
  56047. ExpectIntEQ((d), (g)); \
  56048. ExpectIntEQ((e)[1], (h)); \
  56049. ExpectIntEQ((e)[0], (i)); \
  56050. } while (0)
  56051. static int test_wolfSSL_DtlsUpdateWindow(void)
  56052. {
  56053. EXPECT_DECLS;
  56054. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  56055. word32 next_lo = 0;
  56056. word16 next_hi = 0;
  56057. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  56058. fprintf(stderr, "\n");
  56059. #endif
  56060. XMEMSET(window, 0, sizeof window);
  56061. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  56062. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  56063. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  56064. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  56065. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  56066. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  56067. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  56068. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  56069. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  56070. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  56071. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  56072. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  56073. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  56074. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  56075. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  56076. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  56077. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  56078. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  56079. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  56080. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  56081. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  56082. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  56083. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  56084. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  56085. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  56086. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  56087. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  56088. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  56089. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  56090. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  56091. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  56092. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  56093. return EXPECT_RESULT();
  56094. }
  56095. #endif /* WOLFSSL_DTLS */
  56096. #ifdef WOLFSSL_DTLS
  56097. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  56098. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  56099. {
  56100. DtlsMsg* cur;
  56101. static byte msg[100];
  56102. static byte msgInit = 0;
  56103. if (!msgInit) {
  56104. int i;
  56105. for (i = 0; i < 100; i++)
  56106. msg[i] = i + 1;
  56107. msgInit = 1;
  56108. }
  56109. /* Sanitize test parameters */
  56110. if (len > sizeof(msg))
  56111. return -1;
  56112. if (f_offset + f_len > sizeof(msg))
  56113. return -1;
  56114. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  56115. if (ssl->dtls_rx_msg_list == NULL)
  56116. return -100;
  56117. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  56118. return -200;
  56119. if (cur->fragBucketListCount != f_count)
  56120. return -300;
  56121. if (cur->ready != ready)
  56122. return -400;
  56123. if (cur->bytesReceived != bytesReceived)
  56124. return -500;
  56125. if (ready) {
  56126. if (cur->fragBucketList != NULL)
  56127. return -600;
  56128. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  56129. return -700;
  56130. }
  56131. else {
  56132. DtlsFragBucket* fb;
  56133. if (cur->fragBucketList == NULL)
  56134. return -800;
  56135. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  56136. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  56137. return -900;
  56138. }
  56139. }
  56140. return 0;
  56141. }
  56142. static int test_wolfSSL_DTLS_fragment_buckets(void)
  56143. {
  56144. EXPECT_DECLS;
  56145. WOLFSSL ssl[1];
  56146. XMEMSET(ssl, 0, sizeof(*ssl));
  56147. ExpectIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  56148. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  56149. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  56150. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  56151. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  56152. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  56153. /* Test all permutations of 3 regions */
  56154. /* 1 2 3 */
  56155. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  56156. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  56157. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  56158. /* 1 3 2 */
  56159. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  56160. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  56161. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  56162. /* 2 1 3 */
  56163. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  56164. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  56165. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  56166. /* 2 3 1 */
  56167. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  56168. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  56169. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  56170. /* 3 1 2 */
  56171. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  56172. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  56173. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  56174. /* 3 2 1 */
  56175. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  56176. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  56177. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  56178. /* Test overlapping regions */
  56179. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  56180. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  56181. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  56182. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  56183. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  56184. /* Test overlapping multiple regions */
  56185. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  56186. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  56187. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  56188. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  56189. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  56190. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  56191. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  56192. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  56193. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  56194. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  56195. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  56196. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  56197. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  56198. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  56199. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  56200. ssl->dtls_rx_msg_list = NULL;
  56201. ssl->dtls_rx_msg_list_sz = 0;
  56202. return EXPECT_RESULT();
  56203. }
  56204. #endif
  56205. #if !defined(NO_FILESYSTEM) && \
  56206. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  56207. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  56208. static int test_wolfSSL_dtls_stateless2(void)
  56209. {
  56210. EXPECT_DECLS;
  56211. WOLFSSL *ssl_c = NULL;
  56212. WOLFSSL *ssl_c2 = NULL;
  56213. WOLFSSL *ssl_s = NULL;
  56214. struct test_memio_ctx test_ctx;
  56215. WOLFSSL_CTX *ctx_c = NULL;
  56216. WOLFSSL_CTX *ctx_s = NULL;
  56217. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56218. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56219. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  56220. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  56221. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  56222. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  56223. /* send CH */
  56224. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  56225. (ssl_c2->error == WANT_READ));
  56226. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  56227. (ssl_s->error == WANT_READ));
  56228. ExpectIntNE(test_ctx.c_len, 0);
  56229. /* consume HRR */
  56230. test_ctx.c_len = 0;
  56231. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  56232. wolfSSL_free(ssl_c2);
  56233. wolfSSL_free(ssl_c);
  56234. wolfSSL_free(ssl_s);
  56235. wolfSSL_CTX_free(ctx_c);
  56236. wolfSSL_CTX_free(ctx_s);
  56237. return EXPECT_RESULT();
  56238. }
  56239. #ifdef HAVE_MAX_FRAGMENT
  56240. static int test_wolfSSL_dtls_stateless_maxfrag(void)
  56241. {
  56242. EXPECT_DECLS;
  56243. WOLFSSL *ssl_c = NULL;
  56244. WOLFSSL *ssl_c2 = NULL;
  56245. WOLFSSL *ssl_s = NULL;
  56246. struct test_memio_ctx test_ctx;
  56247. WOLFSSL_CTX *ctx_c = NULL;
  56248. WOLFSSL_CTX *ctx_s = NULL;
  56249. word16 max_fragment = 0;
  56250. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56251. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56252. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  56253. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  56254. ExpectIntEQ(wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8),
  56255. WOLFSSL_SUCCESS);
  56256. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  56257. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  56258. if (ssl_s != NULL) {
  56259. max_fragment = ssl_s->max_fragment;
  56260. }
  56261. /* send CH */
  56262. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  56263. (ssl_c2->error == WANT_READ));
  56264. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  56265. (ssl_s->error == WANT_READ));
  56266. /* CH without cookie shouldn't change state */
  56267. ExpectIntEQ(ssl_s->max_fragment, max_fragment);
  56268. ExpectIntNE(test_ctx.c_len, 0);
  56269. /* consume HRR from buffer */
  56270. test_ctx.c_len = 0;
  56271. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  56272. wolfSSL_free(ssl_c2);
  56273. wolfSSL_free(ssl_c);
  56274. wolfSSL_free(ssl_s);
  56275. wolfSSL_CTX_free(ctx_c);
  56276. wolfSSL_CTX_free(ctx_s);
  56277. return EXPECT_RESULT();
  56278. }
  56279. #endif /* HAVE_MAX_FRAGMENT */
  56280. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  56281. #define ROUNDS_WITH_HVR 4
  56282. #define ROUNDS_WITHOUT_HVR 2
  56283. #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
  56284. static int buf_is_hvr(const byte *data, int len)
  56285. {
  56286. if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
  56287. return 0;
  56288. return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
  56289. }
  56290. static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
  56291. {
  56292. EXPECT_DECLS;
  56293. struct test_memio_ctx test_ctx;
  56294. WOLFSSL_CTX *ctx_c = NULL;
  56295. WOLFSSL_CTX *ctx_s = NULL;
  56296. WOLFSSL *ssl_c = NULL;
  56297. WOLFSSL *ssl_s = NULL;
  56298. WOLFSSL_SESSION *sess = NULL;
  56299. int round_trips;
  56300. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56301. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  56302. &ssl_s, wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  56303. #ifdef HAVE_SESSION_TICKET
  56304. if (useticket) {
  56305. ExpectIntEQ(wolfSSL_UseSessionTicket(ssl_c), WOLFSSL_SUCCESS);
  56306. }
  56307. #endif
  56308. round_trips = ROUNDS_WITH_HVR;
  56309. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, round_trips,
  56310. &round_trips), 0);
  56311. ExpectIntEQ(round_trips, ROUNDS_WITH_HVR);
  56312. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  56313. wolfSSL_shutdown(ssl_c);
  56314. wolfSSL_shutdown(ssl_s);
  56315. wolfSSL_free(ssl_c);
  56316. ssl_c = NULL;
  56317. wolfSSL_free(ssl_s);
  56318. ssl_s = NULL;
  56319. test_ctx.c_len = test_ctx.s_len = 0;
  56320. /* make resumption invalid */
  56321. if (bad && (sess != NULL)) {
  56322. if (useticket) {
  56323. #ifdef HAVE_SESSION_TICKET
  56324. if (sess->ticket != NULL) {
  56325. sess->ticket[0] = !sess->ticket[0];
  56326. }
  56327. #endif /* HAVE_SESSION_TICKET */
  56328. }
  56329. else {
  56330. sess->sessionID[0] = !sess->sessionID[0];
  56331. }
  56332. }
  56333. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  56334. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  56335. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  56336. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  56337. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  56338. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  56339. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  56340. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  56341. (ssl_c->error == WANT_READ));
  56342. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  56343. (ssl_s->error == WANT_READ));
  56344. ExpectFalse(bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  56345. ExpectFalse(!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  56346. if (!useticket) {
  56347. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips), 0);
  56348. ExpectFalse(bad && round_trips != ROUNDS_WITH_HVR - 1);
  56349. ExpectFalse(!bad && round_trips != ROUNDS_WITHOUT_HVR - 1);
  56350. }
  56351. wolfSSL_SESSION_free(sess);
  56352. wolfSSL_free(ssl_c);
  56353. wolfSSL_free(ssl_s);
  56354. wolfSSL_CTX_free(ctx_c);
  56355. wolfSSL_CTX_free(ctx_s);
  56356. return EXPECT_RESULT();
  56357. }
  56358. static int test_wolfSSL_dtls_stateless_resume(void)
  56359. {
  56360. EXPECT_DECLS;
  56361. #ifdef HAVE_SESSION_TICKET
  56362. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 0), TEST_SUCCESS);
  56363. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 1), TEST_SUCCESS);
  56364. #endif /* HAVE_SESION_TICKET */
  56365. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 0), TEST_SUCCESS);
  56366. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 1), TEST_SUCCESS);
  56367. return EXPECT_RESULT();
  56368. }
  56369. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  56370. #if !defined(NO_OLD_TLS)
  56371. static int test_wolfSSL_dtls_stateless_downgrade(void)
  56372. {
  56373. EXPECT_DECLS;
  56374. WOLFSSL_CTX *ctx_c = NULL;
  56375. WOLFSSL_CTX *ctx_c2 = NULL;
  56376. WOLFSSL_CTX *ctx_s = NULL;
  56377. WOLFSSL *ssl_c = NULL;
  56378. WOLFSSL *ssl_c2 = NULL;
  56379. WOLFSSL *ssl_s = NULL;
  56380. struct test_memio_ctx test_ctx;
  56381. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56382. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56383. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  56384. ExpectIntEQ(wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1),
  56385. WOLFSSL_SUCCESS);
  56386. ExpectNotNull(ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method()));
  56387. wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
  56388. wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
  56389. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c2));
  56390. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  56391. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  56392. /* send CH */
  56393. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  56394. (ssl_c2->error == WANT_READ));
  56395. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  56396. (ssl_s->error == WANT_READ));
  56397. ExpectIntNE(test_ctx.c_len, 0);
  56398. /* consume HRR */
  56399. test_ctx.c_len = 0;
  56400. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  56401. wolfSSL_free(ssl_c2);
  56402. wolfSSL_free(ssl_c);
  56403. wolfSSL_free(ssl_s);
  56404. wolfSSL_CTX_free(ctx_c);
  56405. wolfSSL_CTX_free(ctx_c2);
  56406. wolfSSL_CTX_free(ctx_s);
  56407. return EXPECT_RESULT();
  56408. }
  56409. #endif /* !defined(NO_OLD_TLS) */
  56410. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  56411. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
  56412. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  56413. !defined(NO_OLD_TLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  56414. static int test_WOLFSSL_dtls_version_alert(void)
  56415. {
  56416. EXPECT_DECLS;
  56417. struct test_memio_ctx test_ctx;
  56418. WOLFSSL_CTX *ctx_c = NULL;
  56419. WOLFSSL_CTX *ctx_s = NULL;
  56420. WOLFSSL *ssl_c = NULL;
  56421. WOLFSSL *ssl_s = NULL;
  56422. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56423. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56424. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method), 0);
  56425. /* client hello */
  56426. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  56427. (ssl_c->error == WANT_READ));
  56428. /* hrr */
  56429. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  56430. (ssl_s->error == WANT_READ));
  56431. /* client hello 1 */
  56432. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  56433. (ssl_c->error == WANT_READ));
  56434. /* server hello */
  56435. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  56436. (ssl_s->error == WANT_READ));
  56437. /* should fail */
  56438. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  56439. (ssl_c->error == VERSION_ERROR));
  56440. /* shuould fail */
  56441. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  56442. (ssl_s->error == VERSION_ERROR || ssl_s->error == FATAL_ERROR));
  56443. wolfSSL_free(ssl_c);
  56444. wolfSSL_free(ssl_s);
  56445. wolfSSL_CTX_free(ctx_c);
  56446. wolfSSL_CTX_free(ctx_s);
  56447. return EXPECT_RESULT();
  56448. }
  56449. #else
  56450. static int test_WOLFSSL_dtls_version_alert(void)
  56451. {
  56452. return TEST_SKIPPED;
  56453. }
  56454. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  56455. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  56456. * !defined(NO_OLD_TLS) && !defined(NO_RSA)
  56457. */
  56458. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  56459. && defined(WOLFSSL_TLS13) && \
  56460. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))\
  56461. && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  56462. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  56463. {
  56464. struct test_memio_ctx *test_ctx;
  56465. byte buf[2048];
  56466. int idx, sz;
  56467. word32 tmp;
  56468. int ret;
  56469. idx = 5; /* space for record header */
  56470. buf[idx] = session_ticket; /* type */
  56471. idx++;
  56472. tmp = OPAQUE32_LEN +
  56473. OPAQUE32_LEN +
  56474. OPAQUE8_LEN + nonceLength +
  56475. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  56476. c32to24(tmp, buf + idx);
  56477. idx += OPAQUE24_LEN;
  56478. c32toa((word32)12345, buf+idx); /* lifetime */
  56479. idx += OPAQUE32_LEN;
  56480. c32toa((word32)12345, buf+idx); /* add */
  56481. idx += OPAQUE32_LEN;
  56482. buf[idx] = nonceLength; /* nonce length */
  56483. idx++;
  56484. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  56485. idx += nonceLength;
  56486. tmp = 1; /* ticket len */
  56487. c16toa((word16)tmp, buf+idx);
  56488. idx += 2;
  56489. buf[idx] = 0xFF; /* ticket */
  56490. idx++;
  56491. tmp = 0; /* ext len */
  56492. c16toa((word16)tmp, buf+idx);
  56493. idx += 2;
  56494. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  56495. handshake, 0, 0, 0);
  56496. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  56497. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  56498. return !(ret == sz);
  56499. }
  56500. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  56501. {
  56502. int ret = 0;
  56503. if ((sess == NULL) || (sess->ticketNonce.len != len)) {
  56504. ret = -1;
  56505. }
  56506. else {
  56507. int i;
  56508. for (i = 0; i < len; i++) {
  56509. if (sess->ticketNonce.data[i] != len) {
  56510. ret = -1;
  56511. break;
  56512. }
  56513. }
  56514. }
  56515. return ret;
  56516. }
  56517. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  56518. {
  56519. EXPECT_DECLS;
  56520. char *buf[1024];
  56521. ExpectIntEQ(send_new_session_ticket(ssl_s, len, len), 0);
  56522. ExpectTrue((wolfSSL_recv(ssl_c, buf, 1024, 0) == WOLFSSL_FATAL_ERROR) &&
  56523. (ssl_c->error == WANT_READ));
  56524. ExpectIntEQ(test_ticket_nonce_check(ssl_c->session, len), 0);
  56525. return EXPECT_RESULT();
  56526. }
  56527. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  56528. {
  56529. EXPECT_DECLS;
  56530. WOLFSSL_SESSION *sess = NULL;
  56531. WOLFSSL_SESSION *cached = NULL;
  56532. WOLFSSL_CTX *ctx = ssl_c->ctx;
  56533. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, len), TEST_SUCCESS);
  56534. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  56535. ExpectIntEQ(AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  56536. NULL, ssl_c->options.side, 1,NULL), 0);
  56537. ExpectNotNull(cached = wolfSSL_SESSION_new());
  56538. ExpectIntEQ(wolfSSL_GetSessionFromCache(ssl_c, cached), WOLFSSL_SUCCESS);
  56539. ExpectIntEQ(test_ticket_nonce_check(cached, len), 0);
  56540. wolfSSL_SESSION_free(cached);
  56541. wolfSSL_SESSION_free(sess);
  56542. return EXPECT_RESULT();
  56543. }
  56544. static int test_ticket_nonce_malloc(void)
  56545. {
  56546. EXPECT_DECLS;
  56547. struct test_memio_ctx test_ctx;
  56548. WOLFSSL_CTX *ctx_c = NULL;
  56549. WOLFSSL_CTX *ctx_s = NULL;
  56550. WOLFSSL *ssl_c = NULL;
  56551. WOLFSSL *ssl_s = NULL;
  56552. byte small;
  56553. byte medium;
  56554. byte big;
  56555. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56556. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56557. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  56558. /* will send ticket manually */
  56559. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(ssl_s), 0);
  56560. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  56561. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  56562. while (EXPECT_SUCCESS() && (ssl_c->options.handShakeDone == 0) &&
  56563. (ssl_s->options.handShakeDone == 0)) {
  56564. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_SUCCESS) ||
  56565. (ssl_c->error == WANT_READ));
  56566. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_SUCCESS) ||
  56567. (ssl_s->error == WANT_READ));
  56568. }
  56569. small = TLS13_TICKET_NONCE_STATIC_SZ;
  56570. medium = small + 20 <= 255 ? small + 20 : 255;
  56571. big = medium + 20 <= 255 ? small + 20 : 255;
  56572. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  56573. ExpectPtrEq(ssl_c->session->ticketNonce.data,
  56574. ssl_c->session->ticketNonce.dataStatic);
  56575. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  56576. TEST_SUCCESS);
  56577. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, big), TEST_SUCCESS);
  56578. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  56579. TEST_SUCCESS);
  56580. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  56581. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  56582. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  56583. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, big), TEST_SUCCESS);
  56584. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  56585. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  56586. wolfSSL_free(ssl_c);
  56587. wolfSSL_free(ssl_s);
  56588. wolfSSL_CTX_free(ctx_c);
  56589. wolfSSL_CTX_free(ctx_s);
  56590. return EXPECT_RESULT();
  56591. }
  56592. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  56593. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
  56594. !defined(WOLFSSL_TICKET_DECRYPT_NO_CREATE) && \
  56595. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  56596. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_RSA) && \
  56597. defined(HAVE_ECC) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  56598. static int test_ticket_ret_create(void)
  56599. {
  56600. EXPECT_DECLS;
  56601. WOLFSSL_CTX *ctx_c = NULL;
  56602. WOLFSSL_CTX *ctx_s = NULL;
  56603. WOLFSSL *ssl_c = NULL;
  56604. WOLFSSL *ssl_s = NULL;
  56605. byte ticket[SESSION_TICKET_LEN];
  56606. struct test_memio_ctx test_ctx;
  56607. WOLFSSL_SESSION *sess = NULL;
  56608. word16 ticketLen = 0;
  56609. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56610. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56611. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  56612. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  56613. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  56614. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx_c), WOLFSSL_SUCCESS);
  56615. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  56616. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  56617. ExpectIntLE(sess->ticketLen, SESSION_TICKET_LEN);
  56618. if (sess != NULL) {
  56619. ticketLen = sess->ticketLen;
  56620. XMEMCPY(ticket, sess->ticket, sess->ticketLen);
  56621. }
  56622. wolfSSL_free(ssl_c);
  56623. ssl_c = NULL;
  56624. wolfSSL_free(ssl_s);
  56625. ssl_s = NULL;
  56626. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  56627. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  56628. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  56629. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  56630. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  56631. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  56632. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  56633. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  56634. ExpectIntLE(ssl_c->session->ticketLen, SESSION_TICKET_LEN);
  56635. ExpectIntEQ(ssl_c->session->ticketLen, ticketLen);
  56636. ExpectTrue(XMEMCMP(ssl_c->session->ticket, ticket, ticketLen) != 0);
  56637. wolfSSL_SESSION_free(sess);
  56638. wolfSSL_free(ssl_c);
  56639. wolfSSL_free(ssl_s);
  56640. wolfSSL_CTX_free(ctx_c);
  56641. wolfSSL_CTX_free(ctx_s);
  56642. return EXPECT_RESULT();
  56643. }
  56644. #else
  56645. static int test_ticket_ret_create(void)
  56646. {
  56647. return TEST_SKIPPED;
  56648. }
  56649. #endif
  56650. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && \
  56651. defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  56652. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  56653. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  56654. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  56655. static void test_ticket_and_psk_mixing_on_result(WOLFSSL* ssl)
  56656. {
  56657. int ret;
  56658. WOLFSSL_SESSION* session = NULL;
  56659. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  56660. if (!wolfSSL_is_server(ssl)) {
  56661. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  56662. AssertNotNull(session);
  56663. }
  56664. do {
  56665. ret = wolfSSL_shutdown(ssl);
  56666. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  56667. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  56668. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  56669. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  56670. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  56671. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  56672. #endif
  56673. if (!wolfSSL_is_server(ssl)) {
  56674. /* client */
  56675. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  56676. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  56677. wolfSSL_set_session(ssl, session);
  56678. wolfSSL_SESSION_free(session);
  56679. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  56680. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  56681. }
  56682. else {
  56683. /* server */
  56684. /* Different ciphersuite so that the ticket will be invalidated based on
  56685. * the ciphersuite */
  56686. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"),
  56687. WOLFSSL_SUCCESS);
  56688. wolfSSL_set_psk_server_tls13_callback(ssl, my_psk_server_tls13_cb);
  56689. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  56690. }
  56691. }
  56692. static void test_ticket_and_psk_mixing_ssl_ready(WOLFSSL* ssl)
  56693. {
  56694. AssertIntEQ(wolfSSL_UseSessionTicket(ssl), WOLFSSL_SUCCESS);
  56695. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  56696. WOLFSSL_SUCCESS);
  56697. }
  56698. static int test_ticket_and_psk_mixing(void)
  56699. {
  56700. EXPECT_DECLS;
  56701. /* Test mixing tickets and regular PSK */
  56702. callback_functions client_cbs, server_cbs;
  56703. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56704. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56705. client_cbs.method = wolfTLSv1_3_client_method;
  56706. server_cbs.method = wolfTLSv1_3_server_method;
  56707. client_cbs.ssl_ready = test_ticket_and_psk_mixing_ssl_ready;
  56708. client_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  56709. server_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  56710. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56711. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  56712. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  56713. return EXPECT_RESULT();
  56714. }
  56715. #else
  56716. static int test_ticket_and_psk_mixing(void)
  56717. {
  56718. return TEST_SKIPPED;
  56719. }
  56720. #endif
  56721. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  56722. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  56723. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  56724. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  56725. static int test_prioritize_psk_cb_called = FALSE;
  56726. static unsigned int test_prioritize_psk_cb(WOLFSSL* ssl,
  56727. const char* identity, unsigned char* key, unsigned int key_max_len,
  56728. const char** ciphersuite)
  56729. {
  56730. test_prioritize_psk_cb_called = TRUE;
  56731. return my_psk_server_tls13_cb(ssl, identity, key, key_max_len, ciphersuite);
  56732. }
  56733. static void test_prioritize_psk_on_result(WOLFSSL* ssl)
  56734. {
  56735. int ret;
  56736. WOLFSSL_SESSION* session = NULL;
  56737. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  56738. if (!wolfSSL_is_server(ssl)) {
  56739. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  56740. AssertNotNull(session);
  56741. }
  56742. do {
  56743. ret = wolfSSL_shutdown(ssl);
  56744. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  56745. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  56746. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  56747. /* Previous connection was made with TLS13-AES128-GCM-SHA256. Order is
  56748. * important. */
  56749. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  56750. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  56751. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  56752. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  56753. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  56754. #endif
  56755. if (!wolfSSL_is_server(ssl)) {
  56756. /* client */
  56757. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  56758. wolfSSL_set_session(ssl, session);
  56759. wolfSSL_SESSION_free(session);
  56760. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  56761. }
  56762. else {
  56763. /* server */
  56764. wolfSSL_set_psk_server_tls13_callback(ssl, test_prioritize_psk_cb);
  56765. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  56766. #ifdef WOLFSSL_PRIORITIZE_PSK
  56767. /* The ticket should be first tried with all ciphersuites and chosen */
  56768. AssertFalse(test_prioritize_psk_cb_called);
  56769. #else
  56770. /* Ciphersuites should be tried with each PSK. This triggers the PSK
  56771. * callback that sets this var. */
  56772. AssertTrue(test_prioritize_psk_cb_called);
  56773. #endif
  56774. }
  56775. }
  56776. static void test_prioritize_psk_ssl_ready(WOLFSSL* ssl)
  56777. {
  56778. if (!wolfSSL_is_server(ssl))
  56779. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  56780. WOLFSSL_SUCCESS);
  56781. else
  56782. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  56783. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  56784. }
  56785. static int test_prioritize_psk(void)
  56786. {
  56787. EXPECT_DECLS;
  56788. /* We always send the ticket first. With WOLFSSL_PRIORITIZE_PSK the order
  56789. * of the PSK's will be followed instead of the ciphersuite. */
  56790. callback_functions client_cbs, server_cbs;
  56791. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56792. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56793. client_cbs.method = wolfTLSv1_3_client_method;
  56794. server_cbs.method = wolfTLSv1_3_server_method;
  56795. client_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  56796. server_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  56797. client_cbs.on_result = test_prioritize_psk_on_result;
  56798. server_cbs.on_result = test_prioritize_psk_on_result;
  56799. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56800. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  56801. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  56802. return EXPECT_RESULT();
  56803. }
  56804. #else
  56805. static int test_prioritize_psk(void)
  56806. {
  56807. return TEST_SKIPPED;
  56808. }
  56809. #endif
  56810. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  56811. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  56812. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  56813. !defined(WOLFSSL_NO_TLS12)
  56814. static int test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server(WOLFSSL_CTX* ctx)
  56815. {
  56816. EXPECT_DECLS;
  56817. ExpectTrue(SSL_CTX_set_cipher_list(ctx, "DEFAULT"));
  56818. /* Set TLS 1.3 specific suite */
  56819. ExpectTrue(SSL_CTX_set_ciphersuites(ctx, "TLS13-AES128-GCM-SHA256"));
  56820. return EXPECT_RESULT();
  56821. }
  56822. static int test_wolfSSL_CTX_set_ciphersuites(void)
  56823. {
  56824. EXPECT_DECLS;
  56825. /* Test using SSL_CTX_set_cipher_list and SSL_CTX_set_ciphersuites and then
  56826. * do a 1.2 connection. */
  56827. test_ssl_cbf client_cbs, server_cbs;
  56828. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56829. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56830. client_cbs.method = wolfTLSv1_2_client_method;
  56831. server_cbs.method = wolfTLS_server_method; /* Allow downgrade */
  56832. server_cbs.ctx_ready = test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server;
  56833. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  56834. &server_cbs, NULL), TEST_SUCCESS);
  56835. return EXPECT_RESULT();
  56836. }
  56837. #else
  56838. static int test_wolfSSL_CTX_set_ciphersuites(void)
  56839. {
  56840. return TEST_SKIPPED;
  56841. }
  56842. #endif
  56843. #if defined(HAVE_CRL) && defined(WOLFSSL_CHECK_ALERT_ON_ERR) && \
  56844. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  56845. static int test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready(WOLFSSL_CTX* ctx)
  56846. {
  56847. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  56848. return TEST_SUCCESS;
  56849. }
  56850. static int test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup(WOLFSSL* ssl)
  56851. {
  56852. EXPECT_DECLS;
  56853. WOLFSSL_ALERT_HISTORY h;
  56854. ExpectIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  56855. ExpectIntEQ(h.last_rx.level, alert_fatal);
  56856. ExpectIntEQ(h.last_rx.code, certificate_revoked);
  56857. return EXPECT_RESULT();
  56858. }
  56859. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  56860. {
  56861. EXPECT_DECLS;
  56862. test_ssl_cbf client_cbs, server_cbs;
  56863. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56864. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56865. server_cbs.certPemFile = "./certs/server-revoked-cert.pem";
  56866. server_cbs.keyPemFile = "./certs/server-revoked-key.pem";
  56867. client_cbs.crlPemFile = "./certs/crl/crl.revoked";
  56868. client_cbs.ctx_ready = test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready;
  56869. server_cbs.on_cleanup = test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup;
  56870. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  56871. &server_cbs, NULL), TEST_FAIL);
  56872. return EXPECT_RESULT();
  56873. }
  56874. #else
  56875. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  56876. {
  56877. return TEST_SKIPPED;
  56878. }
  56879. #endif
  56880. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET) \
  56881. && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  56882. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  56883. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  56884. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  56885. static WOLFSSL_CTX* test_TLS_13_ticket_different_ciphers_ctx = NULL;
  56886. static WOLFSSL_SESSION* test_TLS_13_ticket_different_ciphers_session = NULL;
  56887. static int test_TLS_13_ticket_different_ciphers_run = 0;
  56888. static int test_TLS_13_ticket_different_ciphers_ssl_ready(WOLFSSL* ssl)
  56889. {
  56890. EXPECT_DECLS;
  56891. switch (test_TLS_13_ticket_different_ciphers_run) {
  56892. case 0:
  56893. /* First run */
  56894. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  56895. WOLFSSL_SUCCESS);
  56896. if (wolfSSL_is_server(ssl)) {
  56897. ExpectNotNull(test_TLS_13_ticket_different_ciphers_ctx =
  56898. wolfSSL_get_SSL_CTX(ssl));
  56899. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_up_ref(
  56900. test_TLS_13_ticket_different_ciphers_ctx));
  56901. }
  56902. break;
  56903. case 1:
  56904. /* Second run */
  56905. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  56906. "TLS13-AES128-GCM-SHA256"),
  56907. WOLFSSL_SUCCESS);
  56908. if (!wolfSSL_is_server(ssl)) {
  56909. ExpectIntEQ(wolfSSL_set_session(ssl,
  56910. test_TLS_13_ticket_different_ciphers_session),
  56911. WOLFSSL_SUCCESS);
  56912. }
  56913. break;
  56914. default:
  56915. /* Bad state? */
  56916. Fail(("Should not enter here"), ("Should not enter here"));
  56917. }
  56918. return EXPECT_RESULT();
  56919. }
  56920. static int test_TLS_13_ticket_different_ciphers_on_result(WOLFSSL* ssl)
  56921. {
  56922. EXPECT_DECLS;
  56923. switch (test_TLS_13_ticket_different_ciphers_run) {
  56924. case 0:
  56925. /* First run */
  56926. ExpectNotNull(test_TLS_13_ticket_different_ciphers_session =
  56927. wolfSSL_get1_session(ssl));
  56928. break;
  56929. case 1:
  56930. /* Second run */
  56931. ExpectTrue(wolfSSL_session_reused(ssl));
  56932. break;
  56933. default:
  56934. /* Bad state? */
  56935. Fail(("Should not enter here"), ("Should not enter here"));
  56936. }
  56937. return EXPECT_RESULT();
  56938. }
  56939. static int test_TLS_13_ticket_different_ciphers(void)
  56940. {
  56941. EXPECT_DECLS;
  56942. /* Check that we handle the connection when the ticket doesn't match
  56943. * the first ciphersuite. */
  56944. test_ssl_cbf client_cbs, server_cbs;
  56945. struct test_params {
  56946. method_provider client_meth;
  56947. method_provider server_meth;
  56948. int doUdp;
  56949. } params[] = {
  56950. #ifdef WOLFSSL_DTLS13
  56951. /* Test that the stateless code handles sessions correctly */
  56952. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, 1},
  56953. #endif
  56954. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, 0},
  56955. };
  56956. size_t i;
  56957. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  56958. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56959. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56960. test_TLS_13_ticket_different_ciphers_run = 0;
  56961. client_cbs.doUdp = server_cbs.doUdp = params[i].doUdp;
  56962. client_cbs.method = params[i].client_meth;
  56963. server_cbs.method = params[i].server_meth;
  56964. client_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  56965. server_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  56966. client_cbs.on_result = test_TLS_13_ticket_different_ciphers_on_result;
  56967. server_cbs.ticNoInit = 1;
  56968. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  56969. &server_cbs, NULL), TEST_SUCCESS);
  56970. test_TLS_13_ticket_different_ciphers_run++;
  56971. server_cbs.ctx = test_TLS_13_ticket_different_ciphers_ctx;
  56972. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  56973. &server_cbs, NULL), TEST_SUCCESS);
  56974. wolfSSL_SESSION_free(test_TLS_13_ticket_different_ciphers_session);
  56975. test_TLS_13_ticket_different_ciphers_session = NULL;
  56976. wolfSSL_CTX_free(test_TLS_13_ticket_different_ciphers_ctx);
  56977. test_TLS_13_ticket_different_ciphers_ctx = NULL;
  56978. }
  56979. return EXPECT_RESULT();
  56980. }
  56981. #else
  56982. static int test_TLS_13_ticket_different_ciphers(void)
  56983. {
  56984. return TEST_SKIPPED;
  56985. }
  56986. #endif
  56987. #if defined(WOLFSSL_EXTRA_ALERTS) && !defined(WOLFSSL_NO_TLS12) && \
  56988. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  56989. #define TEST_WRONG_CS_CLIENT "DHE-RSA-AES128-SHA"
  56990. /* AKA TLS_DHE_RSA_WITH_AES_128_CBC_SHA */
  56991. byte test_extra_alerts_wrong_cs_sh[] = {
  56992. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0xef,
  56993. 0x0c, 0x30, 0x98, 0xa2, 0xac, 0xfa, 0x68, 0xe9, 0x3e, 0xaa, 0x5c, 0xcf,
  56994. 0xa7, 0x42, 0x72, 0xaf, 0xa0, 0xe8, 0x39, 0x2b, 0x3e, 0x81, 0xa7, 0x7a,
  56995. 0xa5, 0x62, 0x8a, 0x0e, 0x41, 0xba, 0xda, 0x20, 0x18, 0x9f, 0xe1, 0x8c,
  56996. 0x1d, 0xc0, 0x37, 0x9c, 0xf4, 0x90, 0x5d, 0x8d, 0xa0, 0x79, 0xa7, 0x4b,
  56997. 0xa8, 0x79, 0xdf, 0xcd, 0x8d, 0xf5, 0xb5, 0x50, 0x5f, 0xf1, 0xdb, 0x4d,
  56998. 0xbb, 0x07, 0x54, 0x1c,
  56999. 0x00, 0x02, /* TLS_RSA_WITH_NULL_SHA */
  57000. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  57001. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  57002. };
  57003. static int test_extra_alerts_wrong_cs(void)
  57004. {
  57005. EXPECT_DECLS;
  57006. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  57007. struct test_memio_ctx test_ctx;
  57008. WOLFSSL_CTX *ctx_c = NULL;
  57009. WOLFSSL_ALERT_HISTORY h;
  57010. WOLFSSL *ssl_c = NULL;
  57011. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57012. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  57013. wolfTLSv1_2_client_method, NULL), 0);
  57014. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, TEST_WRONG_CS_CLIENT),
  57015. WOLFSSL_SUCCESS);
  57016. /* CH */
  57017. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57018. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57019. WOLFSSL_ERROR_WANT_READ);
  57020. /* consume CH */
  57021. test_ctx.s_len = 0;
  57022. /* inject SH */
  57023. XMEMCPY(test_ctx.c_buff, test_extra_alerts_wrong_cs_sh,
  57024. sizeof(test_extra_alerts_wrong_cs_sh));
  57025. test_ctx.c_len = sizeof(test_extra_alerts_wrong_cs_sh);
  57026. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57027. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57028. WOLFSSL_ERROR_WANT_READ);
  57029. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  57030. ExpectIntEQ(h.last_tx.code, illegal_parameter);
  57031. ExpectIntEQ(h.last_tx.level, alert_fatal);
  57032. wolfSSL_free(ssl_c);
  57033. wolfSSL_CTX_free(ctx_c);
  57034. #endif
  57035. return EXPECT_RESULT();
  57036. }
  57037. #else
  57038. static int test_extra_alerts_wrong_cs(void)
  57039. {
  57040. return TEST_SKIPPED;
  57041. }
  57042. #endif
  57043. #if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_EXTRA_ALERTS) && \
  57044. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_SP_MATH)
  57045. static void test_remove_msg(byte *msg, int tail_len, int *len, int msg_length)
  57046. {
  57047. tail_len -= msg_length;
  57048. XMEMMOVE(msg, msg + msg_length, tail_len);
  57049. *len = *len - msg_length;
  57050. }
  57051. static int test_remove_hs_msg_from_buffer(byte *buf, int *len, byte type,
  57052. byte *found)
  57053. {
  57054. const unsigned int _HANDSHAKE_HEADER_SZ = 4;
  57055. const unsigned int _RECORD_HEADER_SZ = 5;
  57056. const int _change_cipher_hs = 55;
  57057. const int _change_cipher = 20;
  57058. const int _handshake = 22;
  57059. unsigned int tail_len;
  57060. byte *idx, *curr;
  57061. word8 currType;
  57062. word16 rLength;
  57063. word32 hLength;
  57064. idx = buf;
  57065. tail_len = *len;
  57066. *found = 0;
  57067. while (tail_len > _RECORD_HEADER_SZ) {
  57068. curr = idx;
  57069. currType = *idx;
  57070. ato16(idx + 3, &rLength);
  57071. idx += _RECORD_HEADER_SZ;
  57072. tail_len -= _RECORD_HEADER_SZ;
  57073. if (tail_len < rLength)
  57074. return -1;
  57075. if (type == _change_cipher_hs && currType == _change_cipher) {
  57076. if (rLength != 1)
  57077. return -1;
  57078. /* match */
  57079. test_remove_msg(curr, *len - (int)(curr - buf),
  57080. len, _RECORD_HEADER_SZ + 1);
  57081. *found = 1;
  57082. return 0;
  57083. }
  57084. if (currType != _handshake) {
  57085. idx += rLength;
  57086. tail_len -= rLength;
  57087. continue;
  57088. }
  57089. if (rLength < _HANDSHAKE_HEADER_SZ)
  57090. return -1;
  57091. currType = *idx;
  57092. ato24(idx+1, &hLength);
  57093. hLength += _HANDSHAKE_HEADER_SZ;
  57094. if (tail_len < hLength)
  57095. return -1;
  57096. if (currType != type) {
  57097. idx += hLength;
  57098. tail_len -= hLength;
  57099. continue;
  57100. }
  57101. /* match */
  57102. test_remove_msg(curr, *len - (int)(curr - buf), len,
  57103. hLength + _RECORD_HEADER_SZ);
  57104. *found = 1;
  57105. return 0;
  57106. }
  57107. /* not found */
  57108. return 0;
  57109. }
  57110. static int test_remove_hs_message(byte hs_message_type,
  57111. int extra_round, byte alert_type)
  57112. {
  57113. EXPECT_DECLS;
  57114. WOLFSSL_CTX *ctx_c = NULL;
  57115. WOLFSSL_CTX *ctx_s = NULL;
  57116. WOLFSSL *ssl_c = NULL;
  57117. WOLFSSL *ssl_s = NULL;
  57118. struct test_memio_ctx test_ctx;
  57119. WOLFSSL_ALERT_HISTORY h;
  57120. byte found = 0;
  57121. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57122. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57123. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  57124. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57125. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57126. WOLFSSL_ERROR_WANT_READ);
  57127. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  57128. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57129. WOLFSSL_ERROR_WANT_READ);
  57130. if (extra_round) {
  57131. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57132. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57133. WOLFSSL_ERROR_WANT_READ);
  57134. /* this will complete handshake from server side */
  57135. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  57136. }
  57137. ExpectIntEQ(test_remove_hs_msg_from_buffer(test_ctx.c_buff,
  57138. &test_ctx.c_len, hs_message_type, &found), 0);
  57139. if (!found) {
  57140. wolfSSL_free(ssl_c);
  57141. wolfSSL_CTX_free(ctx_c);
  57142. wolfSSL_free(ssl_s);
  57143. wolfSSL_CTX_free(ctx_s);
  57144. return TEST_SKIPPED;
  57145. }
  57146. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57147. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57148. WOLFSSL_ERROR_WANT_READ);
  57149. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  57150. ExpectTrue(alert_type == 0xff || h.last_tx.code == alert_type);
  57151. ExpectIntEQ(h.last_tx.level, alert_fatal);
  57152. wolfSSL_free(ssl_c);
  57153. wolfSSL_CTX_free(ctx_c);
  57154. wolfSSL_free(ssl_s);
  57155. wolfSSL_CTX_free(ctx_s);
  57156. return EXPECT_RESULT();
  57157. }
  57158. static int test_extra_alerts_skip_hs(void)
  57159. {
  57160. EXPECT_DECLS;
  57161. const byte _server_key_exchange = 12;
  57162. const byte _server_hello = 2;
  57163. const byte _certificate = 11;
  57164. /* server_hello */
  57165. ExpectIntNE(test_remove_hs_message(_server_hello, 0,
  57166. unexpected_message), TEST_FAIL);
  57167. ExpectIntNE(test_remove_hs_message(_certificate, 0,
  57168. 0xff), TEST_FAIL);
  57169. ExpectIntNE(test_remove_hs_message(_server_key_exchange, 0,
  57170. unexpected_message), TEST_FAIL);
  57171. return EXPECT_RESULT();
  57172. }
  57173. #else
  57174. static int test_extra_alerts_skip_hs(void)
  57175. {
  57176. return TEST_SKIPPED;
  57177. }
  57178. #endif
  57179. #if !defined(WOLFSSL_NO_TLS12) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  57180. && defined(WOLFSSL_EXTRA_ALERTS) && !defined(NO_PSK) && !defined(NO_DH)
  57181. static unsigned int test_server_psk_cb(WOLFSSL* ssl, const char* id,
  57182. unsigned char* key, unsigned int key_max_len)
  57183. {
  57184. (void)ssl;
  57185. (void)id;
  57186. (void)key_max_len;
  57187. /* zero means error */
  57188. key[0] = 0x10;
  57189. return 1;
  57190. }
  57191. static int test_extra_alerts_bad_psk(void)
  57192. {
  57193. EXPECT_DECLS;
  57194. WOLFSSL_CTX *ctx_c = NULL;
  57195. WOLFSSL_CTX *ctx_s = NULL;
  57196. WOLFSSL *ssl_c = NULL;
  57197. WOLFSSL *ssl_s = NULL;
  57198. struct test_memio_ctx test_ctx;
  57199. WOLFSSL_ALERT_HISTORY h;
  57200. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57201. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57202. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  57203. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, "DHE-PSK-AES128-GCM-SHA256"),
  57204. WOLFSSL_SUCCESS);
  57205. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_s, "DHE-PSK-AES128-GCM-SHA256"),
  57206. WOLFSSL_SUCCESS);
  57207. wolfSSL_set_psk_server_callback(ssl_s, test_server_psk_cb);
  57208. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57209. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57210. WOLFSSL_ERROR_WANT_READ);
  57211. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  57212. ExpectIntEQ( wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  57213. WOLFSSL_ERROR_WANT_READ);
  57214. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57215. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57216. WOLFSSL_ERROR_WANT_READ);
  57217. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  57218. ExpectIntEQ(h.last_tx.code, handshake_failure);
  57219. ExpectIntEQ(h.last_tx.level, alert_fatal);
  57220. wolfSSL_free(ssl_c);
  57221. wolfSSL_CTX_free(ctx_c);
  57222. wolfSSL_free(ssl_s);
  57223. wolfSSL_CTX_free(ctx_s);
  57224. return EXPECT_RESULT();
  57225. }
  57226. #else
  57227. static int test_extra_alerts_bad_psk(void)
  57228. {
  57229. return TEST_SKIPPED;
  57230. }
  57231. #endif
  57232. #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  57233. defined(HAVE_IO_TESTS_DEPENDENCIES)
  57234. static int test_harden_no_secure_renegotiation_io_cb(WOLFSSL *ssl, char *buf,
  57235. int sz, void *ctx)
  57236. {
  57237. static int sentServerHello = FALSE;
  57238. if (!sentServerHello) {
  57239. byte renegExt[] = { 0xFF, 0x01, 0x00, 0x01, 0x00 };
  57240. size_t i;
  57241. if (sz < (int)sizeof(renegExt))
  57242. return WOLFSSL_CBIO_ERR_GENERAL;
  57243. /* Remove SCR from ServerHello */
  57244. for (i = 0; i < sz - sizeof(renegExt); i++) {
  57245. if (XMEMCMP(buf + i, renegExt, sizeof(renegExt)) == 0) {
  57246. /* Found the extension. Change it to something unrecognized. */
  57247. buf[i+1] = 0x11;
  57248. break;
  57249. }
  57250. }
  57251. sentServerHello = TRUE;
  57252. }
  57253. return EmbedSend(ssl, buf, sz, ctx);
  57254. }
  57255. static void test_harden_no_secure_renegotiation_ssl_ready(WOLFSSL* ssl)
  57256. {
  57257. wolfSSL_SSLSetIOSend(ssl, test_harden_no_secure_renegotiation_io_cb);
  57258. }
  57259. static void test_harden_no_secure_renegotiation_on_cleanup(WOLFSSL* ssl)
  57260. {
  57261. WOLFSSL_ALERT_HISTORY h;
  57262. AssertIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  57263. AssertIntEQ(h.last_rx.code, handshake_failure);
  57264. AssertIntEQ(h.last_rx.level, alert_fatal);
  57265. }
  57266. static int test_harden_no_secure_renegotiation(void)
  57267. {
  57268. EXPECT_DECLS;
  57269. callback_functions client_cbs, server_cbs;
  57270. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57271. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57272. client_cbs.method = wolfTLSv1_2_client_method;
  57273. server_cbs.method = wolfTLSv1_2_server_method;
  57274. server_cbs.ssl_ready = test_harden_no_secure_renegotiation_ssl_ready;
  57275. server_cbs.on_cleanup = test_harden_no_secure_renegotiation_on_cleanup;
  57276. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  57277. ExpectIntEQ(client_cbs.return_code, TEST_FAIL);
  57278. ExpectIntEQ(client_cbs.last_err, SECURE_RENEGOTIATION_E);
  57279. ExpectIntEQ(server_cbs.return_code, TEST_FAIL);
  57280. ExpectTrue(server_cbs.last_err == SOCKET_ERROR_E ||
  57281. server_cbs.last_err == FATAL_ERROR);
  57282. return EXPECT_RESULT();
  57283. }
  57284. #else
  57285. static int test_harden_no_secure_renegotiation(void)
  57286. {
  57287. return TEST_SKIPPED;
  57288. }
  57289. #endif
  57290. #if defined(HAVE_OCSP) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  57291. static int test_override_alt_cert_chain_cert_cb(int preverify,
  57292. WOLFSSL_X509_STORE_CTX* store)
  57293. {
  57294. fprintf(stderr, "preverify: %d\n", preverify);
  57295. fprintf(stderr, "store->error: %d\n", store->error);
  57296. fprintf(stderr, "error reason: %s\n", wolfSSL_ERR_reason_error_string(store->error));
  57297. if (store->error == OCSP_INVALID_STATUS) {
  57298. fprintf(stderr, "Overriding OCSP error\n");
  57299. return 1;
  57300. }
  57301. #ifndef WOLFSSL_ALT_CERT_CHAINS
  57302. else if ((store->error == ASN_NO_SIGNER_E ||
  57303. store->error == ASN_SELF_SIGNED_E
  57304. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  57305. defined(HAVE_WEBSERVER)
  57306. || store->error == WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
  57307. #endif
  57308. ) && store->error_depth == store->totalCerts - 1) {
  57309. fprintf(stderr, "Overriding no signer error only for root cert\n");
  57310. return 1;
  57311. }
  57312. #endif
  57313. else
  57314. return preverify;
  57315. }
  57316. static int test_override_alt_cert_chain_ocsp_cb(void* ioCtx, const char* url,
  57317. int urlSz, unsigned char* request, int requestSz,
  57318. unsigned char** response)
  57319. {
  57320. (void)ioCtx;
  57321. (void)url;
  57322. (void)urlSz;
  57323. (void)request;
  57324. (void)requestSz;
  57325. (void)response;
  57326. return -1;
  57327. }
  57328. static int test_override_alt_cert_chain_client_ctx_ready(WOLFSSL_CTX* ctx)
  57329. {
  57330. EXPECT_DECLS;
  57331. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER,
  57332. test_override_alt_cert_chain_cert_cb);
  57333. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  57334. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  57335. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  57336. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  57337. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  57338. WOLFSSL_SUCCESS);
  57339. return EXPECT_RESULT();
  57340. }
  57341. static int test_override_alt_cert_chain_client_ctx_ready2(WOLFSSL_CTX* ctx)
  57342. {
  57343. EXPECT_DECLS;
  57344. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  57345. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  57346. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  57347. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  57348. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  57349. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  57350. WOLFSSL_SUCCESS);
  57351. return EXPECT_RESULT();
  57352. }
  57353. static int test_override_alt_cert_chain_server_ctx_ready(WOLFSSL_CTX* ctx)
  57354. {
  57355. EXPECT_DECLS;
  57356. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  57357. "./certs/intermediate/server-chain-alt.pem"), WOLFSSL_SUCCESS);
  57358. return EXPECT_RESULT();
  57359. }
  57360. static int test_override_alt_cert_chain(void)
  57361. {
  57362. EXPECT_DECLS;
  57363. size_t i;
  57364. struct test_params {
  57365. ctx_cb client_ctx_cb;
  57366. ctx_cb server_ctx_cb;
  57367. int result;
  57368. } params[] = {
  57369. {test_override_alt_cert_chain_client_ctx_ready,
  57370. test_override_alt_cert_chain_server_ctx_ready, TEST_SUCCESS},
  57371. {test_override_alt_cert_chain_client_ctx_ready2,
  57372. test_override_alt_cert_chain_server_ctx_ready, TEST_FAIL},
  57373. };
  57374. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  57375. test_ssl_cbf client_cbs, server_cbs;
  57376. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57377. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57378. fprintf(stderr, "test config: %d\n", (int)i);
  57379. client_cbs.ctx_ready = params[i].client_ctx_cb;
  57380. server_cbs.ctx_ready = params[i].server_ctx_cb;
  57381. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  57382. &server_cbs, NULL), params[i].result);
  57383. ExpectIntEQ(client_cbs.return_code, params[i].result);
  57384. ExpectIntEQ(server_cbs.return_code, params[i].result);
  57385. }
  57386. return EXPECT_RESULT();
  57387. }
  57388. #else
  57389. static int test_override_alt_cert_chain(void)
  57390. {
  57391. return TEST_SKIPPED;
  57392. }
  57393. #endif
  57394. #if defined(HAVE_RPK)
  57395. #define svrRpkCertFile "./certs/rpk/server-cert-rpk.der"
  57396. #define clntRpkCertFile "./certs/rpk/client-cert-rpk.der"
  57397. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  57398. static int MyRpkVerifyCb(int mode, WOLFSSL_X509_STORE_CTX* strctx)
  57399. {
  57400. int ret = WOLFSSL_SUCCESS;
  57401. (void)mode;
  57402. (void)strctx;
  57403. WOLFSSL_ENTER("MyRpkVerifyCb");
  57404. return ret;
  57405. }
  57406. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  57407. static WC_INLINE int test_rpk_memio_setup(
  57408. struct test_memio_ctx *ctx,
  57409. WOLFSSL_CTX **ctx_c,
  57410. WOLFSSL_CTX **ctx_s,
  57411. WOLFSSL **ssl_c,
  57412. WOLFSSL **ssl_s,
  57413. method_provider method_c,
  57414. method_provider method_s,
  57415. const char* certfile_c, int fmt_cc, /* client cert file path and format */
  57416. const char* certfile_s, int fmt_cs, /* server cert file path and format */
  57417. const char* pkey_c, int fmt_kc, /* client private key and format */
  57418. const char* pkey_s, int fmt_ks /* server private key and format */
  57419. )
  57420. {
  57421. int ret;
  57422. if (ctx_c != NULL && *ctx_c == NULL) {
  57423. *ctx_c = wolfSSL_CTX_new(method_c());
  57424. if (*ctx_c == NULL) {
  57425. return -1;
  57426. }
  57427. wolfSSL_CTX_set_verify(*ctx_c, WOLFSSL_VERIFY_PEER, NULL);
  57428. ret = wolfSSL_CTX_load_verify_locations(*ctx_c, caCertFile, 0);
  57429. if (ret != WOLFSSL_SUCCESS) {
  57430. return -1;
  57431. }
  57432. wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
  57433. wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
  57434. ret = wolfSSL_CTX_use_certificate_file(*ctx_c, certfile_c, fmt_cc);
  57435. if (ret != WOLFSSL_SUCCESS) {
  57436. return -1;
  57437. }
  57438. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_c, pkey_c, fmt_kc);
  57439. if (ret != WOLFSSL_SUCCESS) {
  57440. return -1;
  57441. }
  57442. }
  57443. if (ctx_s != NULL && *ctx_s == NULL) {
  57444. *ctx_s = wolfSSL_CTX_new(method_s());
  57445. if (*ctx_s == NULL) {
  57446. return -1;
  57447. }
  57448. wolfSSL_CTX_set_verify(*ctx_s, WOLFSSL_VERIFY_PEER, NULL);
  57449. ret = wolfSSL_CTX_load_verify_locations(*ctx_s, cliCertFile, 0);
  57450. if (ret != WOLFSSL_SUCCESS) {
  57451. return -1;
  57452. }
  57453. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, pkey_s, fmt_ks);
  57454. if (ret != WOLFSSL_SUCCESS) {
  57455. return -1;
  57456. }
  57457. ret = wolfSSL_CTX_use_certificate_file(*ctx_s, certfile_s, fmt_cs);
  57458. if (ret != WOLFSSL_SUCCESS) {
  57459. return -1;
  57460. }
  57461. wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
  57462. wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
  57463. if (ctx->s_ciphers != NULL) {
  57464. ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
  57465. if (ret != WOLFSSL_SUCCESS) {
  57466. return -1;
  57467. }
  57468. }
  57469. }
  57470. if (ctx_c != NULL && ssl_c != NULL) {
  57471. *ssl_c = wolfSSL_new(*ctx_c);
  57472. if (*ssl_c == NULL) {
  57473. return -1;
  57474. }
  57475. wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
  57476. wolfSSL_SetIOReadCtx(*ssl_c, ctx);
  57477. }
  57478. if (ctx_s != NULL && ssl_s != NULL) {
  57479. *ssl_s = wolfSSL_new(*ctx_s);
  57480. if (*ssl_s == NULL) {
  57481. return -1;
  57482. }
  57483. wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
  57484. wolfSSL_SetIOReadCtx(*ssl_s, ctx);
  57485. #if !defined(NO_DH)
  57486. SetDH(*ssl_s);
  57487. #endif
  57488. }
  57489. return 0;
  57490. }
  57491. #endif /* HAVE_RPK */
  57492. static int test_rpk_set_xxx_cert_type(void)
  57493. {
  57494. EXPECT_DECLS;
  57495. #if defined(HAVE_RPK)
  57496. char ctype[MAX_CLIENT_CERT_TYPE_CNT + 1]; /* prepare bigger buffer */
  57497. WOLFSSL_CTX* ctx = NULL;
  57498. WOLFSSL* ssl = NULL;
  57499. int tp;
  57500. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  57501. ExpectNotNull(ctx);
  57502. ssl = wolfSSL_new(ctx);
  57503. ExpectNotNull(ssl);
  57504. /*--------------------------------------------*/
  57505. /* tests for wolfSSL_CTX_set_client_cert_type */
  57506. /*--------------------------------------------*/
  57507. /* illegal parameter test caces */
  57508. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(NULL, ctype,
  57509. MAX_CLIENT_CERT_TYPE_CNT),
  57510. BAD_FUNC_ARG);
  57511. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  57512. sizeof(ctype)),
  57513. BAD_FUNC_ARG);
  57514. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  57515. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  57516. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  57517. MAX_CLIENT_CERT_TYPE_CNT),
  57518. BAD_FUNC_ARG);
  57519. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  57520. ctype[1] = 10; /* set unknown cert type */
  57521. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  57522. MAX_CLIENT_CERT_TYPE_CNT),
  57523. BAD_FUNC_ARG);
  57524. /* pass larger type count */
  57525. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  57526. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  57527. ctype[2] = 1; /* pass unacceptable type count */
  57528. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  57529. MAX_CLIENT_CERT_TYPE_CNT + 1),
  57530. BAD_FUNC_ARG);
  57531. /* should accept NULL for type buffer */
  57532. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, NULL,
  57533. MAX_CLIENT_CERT_TYPE_CNT),
  57534. WOLFSSL_SUCCESS);
  57535. /* should accept zero for type count */
  57536. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  57537. 0),
  57538. WOLFSSL_SUCCESS);
  57539. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  57540. MAX_CLIENT_CERT_TYPE_CNT),
  57541. WOLFSSL_SUCCESS);
  57542. /*--------------------------------------------*/
  57543. /* tests for wolfSSL_CTX_set_server_cert_type */
  57544. /*--------------------------------------------*/
  57545. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(NULL, ctype,
  57546. MAX_SERVER_CERT_TYPE_CNT),
  57547. BAD_FUNC_ARG);
  57548. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  57549. sizeof(ctype)),
  57550. BAD_FUNC_ARG);
  57551. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  57552. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  57553. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  57554. MAX_SERVER_CERT_TYPE_CNT),
  57555. BAD_FUNC_ARG);
  57556. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  57557. ctype[1] = 10; /* set unknown cert type */
  57558. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  57559. MAX_SERVER_CERT_TYPE_CNT),
  57560. BAD_FUNC_ARG);
  57561. /* pass larger type count */
  57562. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  57563. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  57564. ctype[2] = 1; /* pass unacceptable type count */
  57565. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  57566. MAX_SERVER_CERT_TYPE_CNT + 1),
  57567. BAD_FUNC_ARG);
  57568. /* should accept NULL for type buffer */
  57569. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, NULL,
  57570. MAX_SERVER_CERT_TYPE_CNT),
  57571. WOLFSSL_SUCCESS);
  57572. /* should accept zero for type count */
  57573. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  57574. 0),
  57575. WOLFSSL_SUCCESS);
  57576. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  57577. MAX_CLIENT_CERT_TYPE_CNT),
  57578. WOLFSSL_SUCCESS);
  57579. /*--------------------------------------------*/
  57580. /* tests for wolfSSL_set_client_cert_type */
  57581. /*--------------------------------------------*/
  57582. ExpectIntEQ(wolfSSL_set_client_cert_type(NULL, ctype,
  57583. MAX_CLIENT_CERT_TYPE_CNT),
  57584. BAD_FUNC_ARG);
  57585. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  57586. sizeof(ctype)),
  57587. BAD_FUNC_ARG);
  57588. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  57589. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  57590. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  57591. MAX_CLIENT_CERT_TYPE_CNT),
  57592. BAD_FUNC_ARG);
  57593. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  57594. ctype[1] = 10; /* set unknown cert type */
  57595. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  57596. MAX_CLIENT_CERT_TYPE_CNT),
  57597. BAD_FUNC_ARG);
  57598. /* pass larger type count */
  57599. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  57600. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  57601. ctype[2] = 1; /* pass unacceptable type count */
  57602. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  57603. MAX_CLIENT_CERT_TYPE_CNT + 1),
  57604. BAD_FUNC_ARG);
  57605. /* should accept NULL for type buffer */
  57606. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, NULL,
  57607. MAX_CLIENT_CERT_TYPE_CNT),
  57608. WOLFSSL_SUCCESS);
  57609. /* should accept zero for type count */
  57610. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  57611. 0),
  57612. WOLFSSL_SUCCESS);
  57613. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  57614. MAX_CLIENT_CERT_TYPE_CNT),
  57615. WOLFSSL_SUCCESS);
  57616. /*--------------------------------------------*/
  57617. /* tests for wolfSSL_CTX_set_server_cert_type */
  57618. /*--------------------------------------------*/
  57619. ExpectIntEQ(wolfSSL_set_server_cert_type(NULL, ctype,
  57620. MAX_SERVER_CERT_TYPE_CNT),
  57621. BAD_FUNC_ARG);
  57622. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  57623. sizeof(ctype)),
  57624. BAD_FUNC_ARG);
  57625. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  57626. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  57627. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  57628. MAX_SERVER_CERT_TYPE_CNT),
  57629. BAD_FUNC_ARG);
  57630. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  57631. ctype[1] = 10; /* set unknown cert type */
  57632. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  57633. MAX_SERVER_CERT_TYPE_CNT),
  57634. BAD_FUNC_ARG);
  57635. /* pass larger type count */
  57636. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  57637. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  57638. ctype[2] = 1; /* pass unacceptable type count */
  57639. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  57640. MAX_SERVER_CERT_TYPE_CNT + 1),
  57641. BAD_FUNC_ARG);
  57642. /* should accept NULL for type buffer */
  57643. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, NULL,
  57644. MAX_SERVER_CERT_TYPE_CNT),
  57645. WOLFSSL_SUCCESS);
  57646. /* should accept zero for type count */
  57647. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  57648. 0),
  57649. WOLFSSL_SUCCESS);
  57650. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  57651. MAX_SERVER_CERT_TYPE_CNT),
  57652. WOLFSSL_SUCCESS);
  57653. /*------------------------------------------------*/
  57654. /* tests for wolfSSL_get_negotiated_xxx_cert_type */
  57655. /*------------------------------------------------*/
  57656. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(NULL, &tp),
  57657. BAD_FUNC_ARG);
  57658. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl, NULL),
  57659. BAD_FUNC_ARG);
  57660. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(NULL, &tp),
  57661. BAD_FUNC_ARG);
  57662. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl, NULL),
  57663. BAD_FUNC_ARG);
  57664. /* clean up */
  57665. wolfSSL_free(ssl);
  57666. wolfSSL_CTX_free(ctx);
  57667. #endif
  57668. return EXPECT_RESULT();
  57669. }
  57670. static int test_tls13_rpk_handshake(void)
  57671. {
  57672. EXPECT_DECLS;
  57673. #if defined(HAVE_RPK)
  57674. int ret = 0;
  57675. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  57676. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  57677. struct test_memio_ctx test_ctx;
  57678. int err;
  57679. char certType_c[MAX_CLIENT_CERT_TYPE_CNT];
  57680. char certType_s[MAX_CLIENT_CERT_TYPE_CNT];
  57681. int typeCnt_c;
  57682. int typeCnt_s;
  57683. int tp;
  57684. (void)err;
  57685. (void)typeCnt_c;
  57686. (void)typeCnt_s;
  57687. (void)certType_c;
  57688. (void)certType_s;
  57689. /* TLS1.2
  57690. * Both client and server load x509 cert and start handshaking.
  57691. * Check no negotiation occurred.
  57692. */
  57693. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57694. ExpectIntEQ(
  57695. test_rpk_memio_setup(
  57696. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57697. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  57698. cliCertFile, WOLFSSL_FILETYPE_PEM,
  57699. svrCertFile, WOLFSSL_FILETYPE_PEM,
  57700. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  57701. svrKeyFile, WOLFSSL_FILETYPE_PEM)
  57702. , 0);
  57703. /* set client certificate type in client end */
  57704. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  57705. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  57706. typeCnt_c = 2;
  57707. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  57708. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  57709. typeCnt_s = 2;
  57710. /* both clien and server do not call client/server_cert_type APIs,
  57711. * expecting default settings works and no negotiation performed.
  57712. */
  57713. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  57714. return TEST_FAIL;
  57715. /* confirm no negotiation occurred */
  57716. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  57717. WOLFSSL_SUCCESS);
  57718. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57719. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  57720. WOLFSSL_SUCCESS);
  57721. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57722. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  57723. WOLFSSL_SUCCESS);
  57724. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57725. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  57726. WOLFSSL_SUCCESS);
  57727. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57728. wolfSSL_free(ssl_c);
  57729. wolfSSL_CTX_free(ctx_c);
  57730. wolfSSL_free(ssl_s);
  57731. wolfSSL_CTX_free(ctx_s);
  57732. ssl_c = ssl_s = NULL;
  57733. ctx_c = ctx_s = NULL;
  57734. /* Both client and server load x509 cert and start handshaking.
  57735. * Check no negotiation occurred.
  57736. */
  57737. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57738. ExpectIntEQ(
  57739. test_rpk_memio_setup(
  57740. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57741. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  57742. cliCertFile, WOLFSSL_FILETYPE_PEM,
  57743. svrCertFile, WOLFSSL_FILETYPE_PEM,
  57744. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  57745. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  57746. , 0);
  57747. /* set client certificate type in client end */
  57748. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  57749. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  57750. typeCnt_c = 2;
  57751. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  57752. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  57753. typeCnt_s = 2;
  57754. /* both clien and server do not call client/server_cert_type APIs,
  57755. * expecting default settings works and no negotiation performed.
  57756. */
  57757. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  57758. return TEST_FAIL;
  57759. /* confirm no negotiation occurred */
  57760. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  57761. WOLFSSL_SUCCESS);
  57762. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57763. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  57764. WOLFSSL_SUCCESS);
  57765. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57766. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  57767. WOLFSSL_SUCCESS);
  57768. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57769. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  57770. WOLFSSL_SUCCESS);
  57771. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57772. wolfSSL_free(ssl_c);
  57773. wolfSSL_CTX_free(ctx_c);
  57774. wolfSSL_free(ssl_s);
  57775. wolfSSL_CTX_free(ctx_s);
  57776. ssl_c = ssl_s = NULL;
  57777. ctx_c = ctx_s = NULL;
  57778. /* Both client and server load RPK cert and start handshaking.
  57779. * Confirm negotiated cert types match as expected.
  57780. */
  57781. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57782. ExpectIntEQ(
  57783. test_rpk_memio_setup(
  57784. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57785. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  57786. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  57787. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  57788. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  57789. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  57790. , 0);
  57791. /* set client certificate type in client end */
  57792. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  57793. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  57794. typeCnt_c = 2;
  57795. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  57796. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  57797. typeCnt_s = 2;
  57798. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  57799. WOLFSSL_SUCCESS);
  57800. /* set server certificate type in client end */
  57801. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  57802. WOLFSSL_SUCCESS);
  57803. /* set client certificate type in server end */
  57804. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  57805. WOLFSSL_SUCCESS);
  57806. /* set server certificate type in server end */
  57807. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  57808. WOLFSSL_SUCCESS);
  57809. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  57810. return TEST_FAIL;
  57811. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  57812. WOLFSSL_SUCCESS);
  57813. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57814. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  57815. WOLFSSL_SUCCESS);
  57816. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57817. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  57818. WOLFSSL_SUCCESS);
  57819. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57820. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  57821. WOLFSSL_SUCCESS);
  57822. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57823. wolfSSL_free(ssl_c);
  57824. wolfSSL_CTX_free(ctx_c);
  57825. wolfSSL_free(ssl_s);
  57826. wolfSSL_CTX_free(ctx_s);
  57827. ssl_c = ssl_s = NULL;
  57828. ctx_c = ctx_s = NULL;
  57829. /* TLS1.2
  57830. * Both client and server load RPK cert and start handshaking.
  57831. * Confirm negotiated cert types match as expected.
  57832. */
  57833. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57834. ExpectIntEQ(
  57835. test_rpk_memio_setup(
  57836. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57837. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  57838. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  57839. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  57840. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  57841. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  57842. , 0);
  57843. /* set client certificate type in client end */
  57844. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  57845. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  57846. typeCnt_c = 2;
  57847. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  57848. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  57849. typeCnt_s = 2;
  57850. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  57851. WOLFSSL_SUCCESS);
  57852. /* set server certificate type in client end */
  57853. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  57854. WOLFSSL_SUCCESS);
  57855. /* set client certificate type in server end */
  57856. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  57857. WOLFSSL_SUCCESS);
  57858. /* set server certificate type in server end */
  57859. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  57860. WOLFSSL_SUCCESS);
  57861. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  57862. return TEST_FAIL;
  57863. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  57864. WOLFSSL_SUCCESS);
  57865. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57866. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  57867. WOLFSSL_SUCCESS);
  57868. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57869. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  57870. WOLFSSL_SUCCESS);
  57871. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57872. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  57873. WOLFSSL_SUCCESS);
  57874. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57875. wolfSSL_free(ssl_c);
  57876. wolfSSL_CTX_free(ctx_c);
  57877. wolfSSL_free(ssl_s);
  57878. wolfSSL_CTX_free(ctx_s);
  57879. ssl_c = ssl_s = NULL;
  57880. ctx_c = ctx_s = NULL;
  57881. /* Both client and server load x509 cert.
  57882. * Have client call set_client_cert_type with both RPK and x509.
  57883. * This doesn't makes client add client cert type extension to ClientHello,
  57884. * since it does not load RPK cert actually.
  57885. */
  57886. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57887. ExpectIntEQ(
  57888. test_rpk_memio_setup(
  57889. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57890. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  57891. cliCertFile, WOLFSSL_FILETYPE_PEM,
  57892. svrCertFile, WOLFSSL_FILETYPE_PEM,
  57893. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  57894. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  57895. , 0);
  57896. /* set client certificate type in client end */
  57897. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  57898. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  57899. typeCnt_c = 2;
  57900. /* client indicates both RPK and x509 certs are available but loaded RPK
  57901. * cert only. It does not have client add client-cert-type extension in CH.
  57902. */
  57903. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  57904. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  57905. typeCnt_c = 2;
  57906. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  57907. WOLFSSL_SUCCESS);
  57908. /* client indicates both RPK and x509 certs are acceptable */
  57909. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  57910. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  57911. typeCnt_s = 2;
  57912. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  57913. WOLFSSL_SUCCESS);
  57914. /* server indicates both RPK and x509 certs are acceptable */
  57915. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  57916. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  57917. typeCnt_c = 2;
  57918. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  57919. WOLFSSL_SUCCESS);
  57920. /* server should indicate only RPK cert is available */
  57921. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  57922. certType_s[1] = -1;
  57923. typeCnt_s = 1;
  57924. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  57925. WOLFSSL_SUCCESS);
  57926. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  57927. return TEST_FAIL;
  57928. /* Negotiation for client-cert-type should NOT happen. Therefore -1 should
  57929. * be returned as cert type.
  57930. */
  57931. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  57932. WOLFSSL_SUCCESS);
  57933. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57934. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  57935. WOLFSSL_SUCCESS);
  57936. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  57937. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  57938. WOLFSSL_SUCCESS);
  57939. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57940. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  57941. WOLFSSL_SUCCESS);
  57942. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  57943. wolfSSL_free(ssl_c);
  57944. wolfSSL_CTX_free(ctx_c);
  57945. wolfSSL_free(ssl_s);
  57946. wolfSSL_CTX_free(ctx_s);
  57947. ssl_c = ssl_s = NULL;
  57948. ctx_c = ctx_s = NULL;
  57949. /* Have client load RPK cert and have server load x509 cert.
  57950. * Check the negotiation result from both ends.
  57951. */
  57952. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57953. ExpectIntEQ(
  57954. test_rpk_memio_setup(
  57955. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57956. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  57957. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  57958. svrCertFile, WOLFSSL_FILETYPE_PEM,
  57959. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  57960. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  57961. , 0);
  57962. /* have client tell to use RPK cert */
  57963. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  57964. certType_c[1] = -1;
  57965. typeCnt_c = 1;
  57966. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  57967. WOLFSSL_SUCCESS);
  57968. /* have client tell to accept both RPK and x509 cert */
  57969. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  57970. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  57971. typeCnt_s = 2;
  57972. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  57973. WOLFSSL_SUCCESS);
  57974. /* have server accept to both RPK and x509 cert */
  57975. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  57976. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  57977. typeCnt_c = 2;
  57978. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  57979. WOLFSSL_SUCCESS);
  57980. /* does not call wolfSSL_set_server_cert_type intentionally in sesrver
  57981. * end, expecting the default setting works.
  57982. */
  57983. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  57984. return TEST_FAIL;
  57985. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  57986. WOLFSSL_SUCCESS);
  57987. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57988. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  57989. WOLFSSL_SUCCESS);
  57990. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  57991. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  57992. WOLFSSL_SUCCESS);
  57993. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57994. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  57995. WOLFSSL_SUCCESS);
  57996. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  57997. wolfSSL_free(ssl_c);
  57998. wolfSSL_CTX_free(ctx_c);
  57999. wolfSSL_free(ssl_s);
  58000. wolfSSL_CTX_free(ctx_s);
  58001. ssl_c = ssl_s = NULL;
  58002. ctx_c = ctx_s = NULL;
  58003. /* Have both client and server load RPK cert, however, have server
  58004. * indicate its cert type x509.
  58005. * Client is expected to detect the cert type mismatch then to send alert
  58006. * with "unsupported_certificate".
  58007. */
  58008. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58009. ExpectIntEQ(
  58010. test_rpk_memio_setup(
  58011. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58012. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  58013. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  58014. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1, /* server sends RPK cert */
  58015. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  58016. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  58017. , 0);
  58018. /* have client tell to use RPK cert */
  58019. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58020. certType_c[1] = -1;
  58021. typeCnt_c = 1;
  58022. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  58023. WOLFSSL_SUCCESS);
  58024. /* have client tell to accept both RPK and x509 cert */
  58025. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  58026. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  58027. typeCnt_s = 2;
  58028. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  58029. WOLFSSL_SUCCESS);
  58030. /* have server accept to both RPK and x509 cert */
  58031. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  58032. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  58033. typeCnt_c = 2;
  58034. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  58035. WOLFSSL_SUCCESS);
  58036. /* have server tell to use x509 cert intentionally. This will bring
  58037. * certificate type mismatch in client side.
  58038. */
  58039. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  58040. certType_s[1] = -1;
  58041. typeCnt_s = 1;
  58042. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  58043. WOLFSSL_SUCCESS);
  58044. /* expect client detect cert type mismatch then send Alert */
  58045. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  58046. if (ret != -1)
  58047. return TEST_FAIL;
  58048. ExpectIntEQ(wolfSSL_get_error(ssl_c, ret), UNSUPPORTED_CERTIFICATE);
  58049. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  58050. WOLFSSL_SUCCESS);
  58051. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58052. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  58053. WOLFSSL_SUCCESS);
  58054. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58055. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  58056. WOLFSSL_SUCCESS);
  58057. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58058. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  58059. WOLFSSL_SUCCESS);
  58060. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58061. wolfSSL_free(ssl_c);
  58062. wolfSSL_CTX_free(ctx_c);
  58063. wolfSSL_free(ssl_s);
  58064. wolfSSL_CTX_free(ctx_s);
  58065. ssl_c = ssl_s = NULL;
  58066. ctx_c = ctx_s = NULL;
  58067. /* Have client load x509 cert and server load RPK cert,
  58068. * however, have client indicate its cert type RPK.
  58069. * Server is expected to detect the cert type mismatch then to send alert
  58070. * with "unsupported_certificate".
  58071. */
  58072. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58073. ExpectIntEQ(
  58074. test_rpk_memio_setup(
  58075. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58076. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  58077. cliCertFile, WOLFSSL_FILETYPE_PEM,
  58078. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  58079. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  58080. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  58081. , 0);
  58082. /* have client tell to use RPK cert intentionally */
  58083. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58084. certType_c[1] = -1;
  58085. typeCnt_c = 1;
  58086. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  58087. WOLFSSL_SUCCESS);
  58088. /* have client tell to accept both RPK and x509 cert */
  58089. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  58090. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  58091. typeCnt_s = 2;
  58092. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  58093. WOLFSSL_SUCCESS);
  58094. /* have server accept to both RPK and x509 cert */
  58095. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  58096. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  58097. typeCnt_c = 2;
  58098. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  58099. WOLFSSL_SUCCESS);
  58100. /* have server tell to use x509 cert intentionally. This will bring
  58101. * certificate type mismatch in client side.
  58102. */
  58103. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  58104. certType_s[1] = -1;
  58105. typeCnt_s = 1;
  58106. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  58107. WOLFSSL_SUCCESS);
  58108. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  58109. /* expect server detect cert type mismatch then send Alert */
  58110. ExpectIntNE(ret, 0);
  58111. err = wolfSSL_get_error(ssl_c, ret);
  58112. ExpectIntEQ(err, UNSUPPORTED_CERTIFICATE);
  58113. /* client did not load RPK cert actually, so negotiation did not happen */
  58114. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  58115. WOLFSSL_SUCCESS);
  58116. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58117. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  58118. WOLFSSL_SUCCESS);
  58119. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58120. /* client did not load RPK cert actually, so negotiation did not happen */
  58121. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  58122. WOLFSSL_SUCCESS);
  58123. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58124. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  58125. WOLFSSL_SUCCESS);
  58126. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58127. wolfSSL_free(ssl_c);
  58128. wolfSSL_CTX_free(ctx_c);
  58129. wolfSSL_free(ssl_s);
  58130. wolfSSL_CTX_free(ctx_s);
  58131. ssl_c = ssl_s = NULL;
  58132. ctx_c = ctx_s = NULL;
  58133. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  58134. /* Both client and server load RPK cert and set certificate verify
  58135. * callbacks then start handshaking.
  58136. * Confirm both side can refer the peer's cert.
  58137. */
  58138. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58139. ExpectIntEQ(
  58140. test_rpk_memio_setup(
  58141. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58142. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  58143. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  58144. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  58145. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  58146. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  58147. , 0);
  58148. /* set client certificate type in client end */
  58149. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58150. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  58151. typeCnt_c = 2;
  58152. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  58153. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  58154. typeCnt_s = 2;
  58155. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  58156. WOLFSSL_SUCCESS);
  58157. /* set server certificate type in client end */
  58158. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  58159. WOLFSSL_SUCCESS);
  58160. /* set client certificate type in server end */
  58161. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  58162. WOLFSSL_SUCCESS);
  58163. /* set server certificate type in server end */
  58164. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  58165. WOLFSSL_SUCCESS);
  58166. /* set certificate verify callback to both client and server */
  58167. int isServer = 0;
  58168. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  58169. wolfSSL_set_verify(ssl_c, SSL_VERIFY_PEER, MyRpkVerifyCb);
  58170. isServer = 1;
  58171. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  58172. wolfSSL_set_verify(ssl_s, SSL_VERIFY_PEER, MyRpkVerifyCb);
  58173. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  58174. if (ret != 0)
  58175. return TEST_FAIL;
  58176. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  58177. WOLFSSL_SUCCESS);
  58178. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58179. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  58180. WOLFSSL_SUCCESS);
  58181. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58182. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  58183. WOLFSSL_SUCCESS);
  58184. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58185. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  58186. WOLFSSL_SUCCESS);
  58187. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58188. wolfSSL_free(ssl_c);
  58189. wolfSSL_CTX_free(ctx_c);
  58190. wolfSSL_free(ssl_s);
  58191. wolfSSL_CTX_free(ctx_s);
  58192. ssl_c = ssl_s = NULL;
  58193. ctx_c = ctx_s = NULL;
  58194. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  58195. #endif /* HAVE_RPK */
  58196. return EXPECT_RESULT();
  58197. }
  58198. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  58199. static int test_dtls13_bad_epoch_ch(void)
  58200. {
  58201. EXPECT_DECLS;
  58202. WOLFSSL_CTX *ctx_c = NULL;
  58203. WOLFSSL_CTX *ctx_s = NULL;
  58204. WOLFSSL *ssl_c = NULL;
  58205. WOLFSSL *ssl_s = NULL;
  58206. struct test_memio_ctx test_ctx;
  58207. const int EPOCH_OFF = 3;
  58208. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58209. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58210. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  58211. /* disable hrr cookie so we can later check msgsReceived.got_client_hello
  58212. * with just one message */
  58213. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  58214. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58215. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58216. WOLFSSL_ERROR_WANT_READ);
  58217. ExpectIntGE(test_ctx.s_len, EPOCH_OFF + 2);
  58218. /* first CH should use epoch 0x0 */
  58219. ExpectTrue((test_ctx.s_buff[EPOCH_OFF] == 0x0) &&
  58220. (test_ctx.s_buff[EPOCH_OFF + 1] == 0x0));
  58221. /* change epoch to 2 */
  58222. test_ctx.s_buff[EPOCH_OFF + 1] = 0x2;
  58223. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  58224. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  58225. WOLFSSL_ERROR_WANT_READ);
  58226. ExpectIntNE(ssl_s->msgsReceived.got_client_hello, 1);
  58227. /* resend the CH */
  58228. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  58229. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58230. wolfSSL_free(ssl_c);
  58231. wolfSSL_CTX_free(ctx_c);
  58232. wolfSSL_free(ssl_s);
  58233. wolfSSL_CTX_free(ctx_s);
  58234. return EXPECT_RESULT();
  58235. }
  58236. #else
  58237. static int test_dtls13_bad_epoch_ch(void)
  58238. {
  58239. return TEST_SKIPPED;
  58240. }
  58241. #endif
  58242. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_SESSION_CACHE)
  58243. static int test_short_session_id_ssl_ready(WOLFSSL* ssl)
  58244. {
  58245. EXPECT_DECLS;
  58246. WOLFSSL_SESSION *sess = NULL;
  58247. /* Setup the session to avoid errors */
  58248. ssl->session->timeout = -1;
  58249. ssl->session->side = WOLFSSL_CLIENT_END;
  58250. #if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
  58251. defined(HAVE_SESSION_TICKET))
  58252. ssl->session->version = ssl->version;
  58253. #endif
  58254. /* Force a short session ID to be sent */
  58255. ssl->session->sessionIDSz = 4;
  58256. #ifndef NO_SESSION_CACHE_REF
  58257. /* Allow the client cache to be used */
  58258. ssl->session->idLen = 4;
  58259. #endif
  58260. ssl->session->isSetup = 1;
  58261. ExpectNotNull(sess = wolfSSL_get_session(ssl));
  58262. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  58263. return EXPECT_RESULT();
  58264. }
  58265. static int test_short_session_id(void)
  58266. {
  58267. EXPECT_DECLS;
  58268. test_ssl_cbf client_cbf;
  58269. test_ssl_cbf server_cbf;
  58270. size_t i;
  58271. struct {
  58272. method_provider client_meth;
  58273. method_provider server_meth;
  58274. const char* tls_version;
  58275. } params[] = {
  58276. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  58277. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID) && \
  58278. !defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
  58279. /* With WOLFSSL_TLS13_MIDDLEBOX_COMPAT a short ID will result in an error */
  58280. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  58281. #ifdef WOLFSSL_DTLS13
  58282. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  58283. #endif
  58284. #endif
  58285. #ifndef WOLFSSL_NO_TLS12
  58286. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  58287. #ifdef WOLFSSL_DTLS
  58288. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  58289. #endif
  58290. #endif
  58291. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  58292. !defined(NO_DES3))
  58293. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  58294. #ifdef WOLFSSL_DTLS
  58295. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  58296. #endif
  58297. #endif
  58298. };
  58299. fprintf(stderr, "\n");
  58300. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  58301. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  58302. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  58303. fprintf(stderr, "\tTesting short ID with %s\n", params[i].tls_version);
  58304. client_cbf.ssl_ready = test_short_session_id_ssl_ready;
  58305. client_cbf.method = params[i].client_meth;
  58306. server_cbf.method = params[i].server_meth;
  58307. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  58308. &server_cbf, NULL), TEST_SUCCESS);
  58309. }
  58310. return EXPECT_RESULT();
  58311. }
  58312. #else
  58313. static int test_short_session_id(void)
  58314. {
  58315. return TEST_SKIPPED;
  58316. }
  58317. #endif
  58318. #if defined(HAVE_NULL_CIPHER) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) \
  58319. && defined(WOLFSSL_DTLS13)
  58320. static byte* test_find_string(const char *string,
  58321. byte *buf, int buf_size)
  58322. {
  58323. int string_size, i;
  58324. string_size = (int)XSTRLEN(string);
  58325. for (i = 0; i < buf_size - string_size - 1; i++) {
  58326. if (XSTRCMP((char*)&buf[i], string) == 0)
  58327. return &buf[i];
  58328. }
  58329. return NULL;
  58330. }
  58331. static int test_wolfSSL_dtls13_null_cipher(void)
  58332. {
  58333. EXPECT_DECLS;
  58334. WOLFSSL_CTX *ctx_c = NULL;
  58335. WOLFSSL_CTX *ctx_s = NULL;
  58336. WOLFSSL *ssl_c = NULL;
  58337. WOLFSSL *ssl_s = NULL;
  58338. struct test_memio_ctx test_ctx;
  58339. const char *test_str = "test";
  58340. int test_str_size;
  58341. byte buf[255], *ptr = NULL;
  58342. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58343. test_ctx.c_ciphers = test_ctx.s_ciphers = "TLS13-SHA256-SHA256";
  58344. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58345. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  58346. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58347. test_str_size = XSTRLEN("test") + 1;
  58348. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  58349. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  58350. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  58351. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  58352. /* check that the packet was sent cleartext */
  58353. ExpectNotNull(ptr = test_find_string(test_str, test_ctx.s_buff,
  58354. test_ctx.s_len));
  58355. if (ptr != NULL) {
  58356. /* modify the message */
  58357. *ptr = 'H';
  58358. /* bad messages should be ignored in DTLS */
  58359. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), -1);
  58360. ExpectIntEQ(ssl_s->error, WANT_READ);
  58361. }
  58362. wolfSSL_free(ssl_c);
  58363. wolfSSL_free(ssl_s);
  58364. wolfSSL_CTX_free(ctx_c);
  58365. wolfSSL_CTX_free(ctx_s);
  58366. return TEST_SUCCESS;
  58367. }
  58368. #else
  58369. static int test_wolfSSL_dtls13_null_cipher(void)
  58370. {
  58371. return TEST_SKIPPED;
  58372. }
  58373. #endif
  58374. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58375. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  58376. !defined(SINGLE_THREADED) && !defined(NO_RSA)
  58377. static int test_dtls_msg_get_connected_port(int fd, word16 *port)
  58378. {
  58379. SOCKADDR_S peer;
  58380. XSOCKLENT len;
  58381. int ret;
  58382. XMEMSET((byte*)&peer, 0, sizeof(peer));
  58383. len = sizeof(peer);
  58384. ret = getpeername(fd, (SOCKADDR*)&peer, &len);
  58385. if (ret != 0 || len > (XSOCKLENT)sizeof(peer))
  58386. return -1;
  58387. switch (peer.ss_family) {
  58388. #ifdef WOLFSSL_IPV6
  58389. case WOLFSSL_IP6: {
  58390. *port = ntohs(((SOCKADDR_IN6*)&peer)->sin6_port);
  58391. break;
  58392. }
  58393. #endif /* WOLFSSL_IPV6 */
  58394. case WOLFSSL_IP4:
  58395. *port = ntohs(((SOCKADDR_IN*)&peer)->sin_port);
  58396. break;
  58397. default:
  58398. return -1;
  58399. }
  58400. return 0;
  58401. }
  58402. static int test_dtls_msg_from_other_peer_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  58403. {
  58404. char buf[1] = {'t'};
  58405. SOCKADDR_IN_T addr;
  58406. int sock_fd;
  58407. word16 port;
  58408. int err;
  58409. (void)ssl;
  58410. (void)ctx;
  58411. if (ssl == NULL)
  58412. return -1;
  58413. err = test_dtls_msg_get_connected_port(wolfSSL_get_fd(ssl), &port);
  58414. if (err != 0)
  58415. return -1;
  58416. sock_fd = socket(AF_INET_V, SOCK_DGRAM, 0);
  58417. if (sock_fd == -1)
  58418. return -1;
  58419. build_addr(&addr, wolfSSLIP, port, 1, 0);
  58420. /* send a packet to the server. Being another socket, the kernel will ensure
  58421. * the source port will be different. */
  58422. err = (int)sendto(sock_fd, buf, sizeof(buf), 0, (SOCKADDR*)&addr,
  58423. sizeof(addr));
  58424. close(sock_fd);
  58425. if (err == -1)
  58426. return -1;
  58427. return 0;
  58428. }
  58429. /* setup a SSL session but just after the handshake send a packet to the server
  58430. * with a source address different than the one of the connected client. The I/O
  58431. * callback EmbedRecvFrom should just ignore the packet. Sending of the packet
  58432. * is done in test_dtls_msg_from_other_peer_cb */
  58433. static int test_dtls_msg_from_other_peer(void)
  58434. {
  58435. EXPECT_DECLS;
  58436. callback_functions client_cbs;
  58437. callback_functions server_cbs;
  58438. XMEMSET((byte*)&client_cbs, 0, sizeof(client_cbs));
  58439. XMEMSET((byte*)&server_cbs, 0, sizeof(server_cbs));
  58440. client_cbs.method = wolfDTLSv1_2_client_method;
  58441. server_cbs.method = wolfDTLSv1_2_server_method;
  58442. client_cbs.doUdp = 1;
  58443. server_cbs.doUdp = 1;
  58444. test_wolfSSL_client_server_nofail_ex(&client_cbs, &server_cbs,
  58445. test_dtls_msg_from_other_peer_cb);
  58446. ExpectIntEQ(client_cbs.return_code, WOLFSSL_SUCCESS);
  58447. ExpectIntEQ(server_cbs.return_code, WOLFSSL_SUCCESS);
  58448. return EXPECT_RESULT();
  58449. }
  58450. #else
  58451. static int test_dtls_msg_from_other_peer(void)
  58452. {
  58453. return TEST_SKIPPED;
  58454. }
  58455. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58456. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  58457. * !defined(SINGLE_THREADED) && !defined(NO_RSA) */
  58458. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_IPV6) && \
  58459. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  58460. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  58461. static int test_dtls_ipv6_check(void)
  58462. {
  58463. EXPECT_DECLS;
  58464. WOLFSSL_CTX *ctx_c = NULL;
  58465. WOLFSSL_CTX *ctx_s = NULL;
  58466. WOLFSSL *ssl_c = NULL;
  58467. WOLFSSL *ssl_s = NULL;
  58468. SOCKADDR_IN fake_addr6;
  58469. int sockfd = -1;
  58470. ExpectNotNull(ctx_c = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  58471. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  58472. ExpectNotNull(ctx_s = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  58473. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile,
  58474. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  58475. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile,
  58476. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  58477. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  58478. XMEMSET((byte*)&fake_addr6, 0, sizeof(fake_addr6));
  58479. /* mimic a sockaddr_in6 struct, this way we can't test without
  58480. * WOLFSSL_IPV6 */
  58481. fake_addr6.sin_family = WOLFSSL_IP6;
  58482. ExpectIntNE(sockfd = socket(AF_INET, SOCK_DGRAM, 0), -1);
  58483. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  58484. /* can't return error here, as the peer is opaque for wolfssl library at
  58485. * this point */
  58486. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_c, &fake_addr6, sizeof(fake_addr6)),
  58487. WOLFSSL_SUCCESS);
  58488. ExpectIntNE(fcntl(sockfd, F_SETFL, O_NONBLOCK), -1);
  58489. wolfSSL_dtls_set_using_nonblock(ssl_c, 1);
  58490. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58491. ExpectIntEQ(ssl_c->error, SOCKET_ERROR_E);
  58492. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_s, &fake_addr6, sizeof(fake_addr6)),
  58493. WOLFSSL_SUCCESS);
  58494. /* reuse the socket */
  58495. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  58496. wolfSSL_dtls_set_using_nonblock(ssl_s, 1);
  58497. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  58498. ExpectIntEQ(ssl_s->error, SOCKET_ERROR_E);
  58499. if (sockfd != -1)
  58500. close(sockfd);
  58501. wolfSSL_free(ssl_c);
  58502. wolfSSL_CTX_free(ctx_c);
  58503. wolfSSL_free(ssl_s);
  58504. wolfSSL_CTX_free(ctx_s);
  58505. return EXPECT_RESULT();
  58506. }
  58507. #else
  58508. static int test_dtls_ipv6_check(void)
  58509. {
  58510. return TEST_SKIPPED;
  58511. }
  58512. #endif
  58513. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  58514. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  58515. static WOLFSSL_SESSION* test_wolfSSL_SCR_after_resumption_session = NULL;
  58516. static void test_wolfSSL_SCR_after_resumption_ctx_ready(WOLFSSL_CTX* ctx)
  58517. {
  58518. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  58519. }
  58520. static void test_wolfSSL_SCR_after_resumption_on_result(WOLFSSL* ssl)
  58521. {
  58522. if (test_wolfSSL_SCR_after_resumption_session == NULL) {
  58523. test_wolfSSL_SCR_after_resumption_session = wolfSSL_get1_session(ssl);
  58524. AssertNotNull(test_wolfSSL_SCR_after_resumption_session);
  58525. }
  58526. else {
  58527. char testMsg[] = "Message after SCR";
  58528. char msgBuf[sizeof(testMsg)];
  58529. int ret;
  58530. if (!wolfSSL_is_server(ssl)) {
  58531. AssertIntEQ(WOLFSSL_SUCCESS,
  58532. wolfSSL_set_session(ssl,
  58533. test_wolfSSL_SCR_after_resumption_session));
  58534. }
  58535. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  58536. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  58537. sizeof(testMsg));
  58538. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  58539. if (ret != sizeof(msgBuf)) /* Possibly APP_DATA_READY error. Retry. */
  58540. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  58541. AssertIntEQ(ret, sizeof(msgBuf));
  58542. }
  58543. }
  58544. static void test_wolfSSL_SCR_after_resumption_ssl_ready(WOLFSSL* ssl)
  58545. {
  58546. AssertIntEQ(WOLFSSL_SUCCESS,
  58547. wolfSSL_set_session(ssl, test_wolfSSL_SCR_after_resumption_session));
  58548. }
  58549. static int test_wolfSSL_SCR_after_resumption(void)
  58550. {
  58551. EXPECT_DECLS;
  58552. callback_functions func_cb_client;
  58553. callback_functions func_cb_server;
  58554. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  58555. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  58556. func_cb_client.method = wolfTLSv1_2_client_method;
  58557. func_cb_client.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  58558. func_cb_client.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  58559. func_cb_server.method = wolfTLSv1_2_server_method;
  58560. func_cb_server.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  58561. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  58562. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  58563. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  58564. func_cb_client.ssl_ready = test_wolfSSL_SCR_after_resumption_ssl_ready;
  58565. func_cb_server.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  58566. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  58567. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  58568. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  58569. wolfSSL_SESSION_free(test_wolfSSL_SCR_after_resumption_session);
  58570. return EXPECT_RESULT();
  58571. }
  58572. #else
  58573. static int test_wolfSSL_SCR_after_resumption(void)
  58574. {
  58575. return TEST_SKIPPED;
  58576. }
  58577. #endif
  58578. static int test_wolfSSL_configure_args(void)
  58579. {
  58580. EXPECT_DECLS;
  58581. #if defined(LIBWOLFSSL_CONFIGURE_ARGS) && defined(HAVE_WC_INTROSPECTION)
  58582. ExpectNotNull(wolfSSL_configure_args());
  58583. #endif
  58584. return EXPECT_RESULT();
  58585. }
  58586. static int test_dtls_no_extensions(void)
  58587. {
  58588. EXPECT_DECLS;
  58589. #if defined(WOLFSSL_DTLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  58590. !defined(WOLFSSL_NO_TLS12)
  58591. WOLFSSL *ssl_s = NULL;
  58592. WOLFSSL_CTX *ctx_s = NULL;
  58593. struct test_memio_ctx test_ctx;
  58594. const byte chNoExtensions[] = {
  58595. /* Handshake type */
  58596. 0x16,
  58597. /* Version */
  58598. 0xfe, 0xff,
  58599. /* Epoch */
  58600. 0x00, 0x00,
  58601. /* Seq number */
  58602. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  58603. /* Length */
  58604. 0x00, 0x40,
  58605. /* CH type */
  58606. 0x01,
  58607. /* Length */
  58608. 0x00, 0x00, 0x34,
  58609. /* Msg Seq */
  58610. 0x00, 0x00,
  58611. /* Frag offset */
  58612. 0x00, 0x00, 0x00,
  58613. /* Frag length */
  58614. 0x00, 0x00, 0x34,
  58615. /* Version */
  58616. 0xfe, 0xff,
  58617. /* Random */
  58618. 0x62, 0xfe, 0xbc, 0xfe, 0x2b, 0xfe, 0x3f, 0xeb, 0x03, 0xc4, 0xea, 0x37,
  58619. 0xe7, 0x47, 0x7e, 0x8a, 0xd9, 0xbf, 0x77, 0x0f, 0x6c, 0xb6, 0x77, 0x0b,
  58620. 0x03, 0x3f, 0x82, 0x2b, 0x21, 0x64, 0x57, 0x1d,
  58621. /* Session Length */
  58622. 0x00,
  58623. /* Cookie Length */
  58624. 0x00,
  58625. /* CS Length */
  58626. 0x00, 0x0c,
  58627. /* CS */
  58628. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x39, 0x00, 0x33,
  58629. /* Comp Meths Length */
  58630. 0x01,
  58631. /* Comp Meths */
  58632. 0x00
  58633. /* And finally... no extensions */
  58634. };
  58635. int i;
  58636. #ifdef OPENSSL_EXTRA
  58637. int repeats = 2;
  58638. #else
  58639. int repeats = 1;
  58640. #endif
  58641. for (i = 0; i < repeats; i++) {
  58642. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58643. ssl_s = NULL;
  58644. ctx_s = NULL;
  58645. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  58646. NULL, wolfDTLS_server_method), 0);
  58647. XMEMCPY(test_ctx.s_buff, chNoExtensions, sizeof(chNoExtensions));
  58648. test_ctx.s_len = sizeof(chNoExtensions);
  58649. #ifdef OPENSSL_EXTRA
  58650. if (i > 0) {
  58651. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl_s, DTLS1_2_VERSION),
  58652. WOLFSSL_SUCCESS);
  58653. }
  58654. #endif
  58655. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  58656. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  58657. /* Expecting a handshake msg. Either HVR or SH. */
  58658. ExpectIntGT(test_ctx.c_len, 0);
  58659. ExpectIntEQ(test_ctx.c_buff[0], 0x16);
  58660. wolfSSL_free(ssl_s);
  58661. wolfSSL_CTX_free(ctx_s);
  58662. }
  58663. #endif
  58664. return EXPECT_RESULT();
  58665. }
  58666. static int test_TLSX_CA_NAMES_bad_extension(void)
  58667. {
  58668. EXPECT_DECLS;
  58669. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  58670. !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES) && \
  58671. defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA384) && \
  58672. defined(HAVE_NULL_CIPHER)
  58673. /* This test should only fail (with BUFFER_ERROR) when we actually try to
  58674. * parse the CA Names extension. Otherwise it will return other non-related
  58675. * errors. If CA Names will be parsed in more configurations, that should
  58676. * be reflected in the macro guard above. */
  58677. WOLFSSL *ssl_c = NULL;
  58678. WOLFSSL_CTX *ctx_c = NULL;
  58679. struct test_memio_ctx test_ctx;
  58680. /* HRR + SH using TLS_DHE_PSK_WITH_NULL_SHA384 */
  58681. const byte shBadCaNamesExt[] = {
  58682. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  58683. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  58684. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  58685. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  58686. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  58687. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  58688. 0x5c, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x03, 0xcf, 0x21, 0xad, 0x74,
  58689. 0x00, 0x00, 0x83, 0x3f, 0x3b, 0x80, 0x01, 0xac, 0x65, 0x8c, 0x19, 0x2a,
  58690. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x02, 0x00, 0x9e, 0x09, 0x1c, 0xe8,
  58691. 0xa8, 0x09, 0x9c, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  58692. 0x03, 0x3f, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x13, 0x05,
  58693. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x00, 0x09, 0x00, 0x00,
  58694. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x01, 0xff,
  58695. 0xff, 0xff, 0xff, 0xfa, 0x0d, 0x00, 0x00, 0x00, 0xad, 0x02
  58696. };
  58697. const byte shBadCaNamesExt2[] = {
  58698. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  58699. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  58700. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  58701. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  58702. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  58703. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  58704. 0x5e, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x7f, 0xd0, 0x2d, 0xea, 0x6e,
  58705. 0x53, 0xa1, 0x6a, 0xc9, 0xc8, 0x54, 0xef, 0x75, 0xe4, 0xd9, 0xc6, 0x3e,
  58706. 0x74, 0xcb, 0x30, 0x80, 0xcc, 0x83, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x00,
  58707. 0x00, 0xc0, 0x5a, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  58708. 0x03, 0x03, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x53, 0x25,
  58709. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x02, 0x05, 0x00, 0x00,
  58710. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x06, 0x00,
  58711. 0x04, 0x00, 0x03, 0x30, 0x00, 0x13, 0x94, 0x00, 0x06, 0x00, 0x04, 0x02
  58712. };
  58713. int i = 0;
  58714. for (i = 0; i < 2; i++) {
  58715. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58716. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  58717. wolfTLSv1_3_client_method, NULL), 0);
  58718. switch (i) {
  58719. case 0:
  58720. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt,
  58721. sizeof(shBadCaNamesExt));
  58722. test_ctx.c_len = sizeof(shBadCaNamesExt);
  58723. break;
  58724. case 1:
  58725. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt2,
  58726. sizeof(shBadCaNamesExt2));
  58727. test_ctx.c_len = sizeof(shBadCaNamesExt2);
  58728. break;
  58729. }
  58730. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  58731. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), BUFFER_ERROR);
  58732. wolfSSL_free(ssl_c);
  58733. ssl_c = NULL;
  58734. wolfSSL_CTX_free(ctx_c);
  58735. ctx_c = NULL;
  58736. }
  58737. #endif
  58738. return EXPECT_RESULT();
  58739. }
  58740. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58741. defined(HAVE_IO_TESTS_DEPENDENCIES)
  58742. static void test_dtls_1_0_hvr_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  58743. {
  58744. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  58745. WOLFSSL_SUCCESS);
  58746. }
  58747. static int test_dtls_1_0_hvr_downgrade(void)
  58748. {
  58749. EXPECT_DECLS;
  58750. callback_functions func_cb_client;
  58751. callback_functions func_cb_server;
  58752. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  58753. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  58754. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  58755. func_cb_client.method = wolfDTLS_client_method;
  58756. func_cb_server.method = wolfDTLSv1_2_server_method;
  58757. func_cb_client.ctx_ready = test_dtls_1_0_hvr_downgrade_ctx_ready;
  58758. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  58759. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  58760. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  58761. return EXPECT_RESULT();
  58762. }
  58763. #else
  58764. static int test_dtls_1_0_hvr_downgrade(void)
  58765. {
  58766. EXPECT_DECLS;
  58767. return EXPECT_RESULT();
  58768. }
  58769. #endif
  58770. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12) && \
  58771. defined(HAVE_SESSION_TICKET)
  58772. static WOLFSSL_SESSION* test_session_ticket_no_id_session = NULL;
  58773. static void test_session_ticket_no_id_on_result(WOLFSSL* ssl)
  58774. {
  58775. test_session_ticket_no_id_session = wolfSSL_get1_session(ssl);
  58776. AssertNotNull(test_session_ticket_no_id_session);
  58777. }
  58778. static void test_session_ticket_no_id_ctx_ready(WOLFSSL_CTX* ctx)
  58779. {
  58780. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  58781. }
  58782. static void test_session_ticket_no_id_ssl_ready(WOLFSSL* ssl)
  58783. {
  58784. test_session_ticket_no_id_session->sessionIDSz = 0;
  58785. AssertIntEQ(WOLFSSL_SUCCESS,
  58786. wolfSSL_set_session(ssl, test_session_ticket_no_id_session));
  58787. }
  58788. static int test_session_ticket_no_id(void)
  58789. {
  58790. /* We are testing an expired (invalid crypto context in out case since the
  58791. * ctx changes) session ticket being sent with the session ID being 0
  58792. * length. */
  58793. EXPECT_DECLS;
  58794. callback_functions func_cb_client;
  58795. callback_functions func_cb_server;
  58796. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  58797. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  58798. func_cb_client.method = wolfTLSv1_2_client_method;
  58799. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  58800. func_cb_client.on_result = test_session_ticket_no_id_on_result;
  58801. func_cb_server.method = wolfTLSv1_2_server_method;
  58802. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  58803. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  58804. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  58805. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  58806. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  58807. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  58808. func_cb_client.method = wolfTLSv1_2_client_method;
  58809. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  58810. func_cb_client.ssl_ready = test_session_ticket_no_id_ssl_ready;
  58811. func_cb_server.method = wolfTLSv1_2_server_method;
  58812. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  58813. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  58814. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  58815. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  58816. wolfSSL_SESSION_free(test_session_ticket_no_id_session);
  58817. return EXPECT_RESULT();
  58818. }
  58819. #else
  58820. static int test_session_ticket_no_id(void)
  58821. {
  58822. EXPECT_DECLS;
  58823. return EXPECT_RESULT();
  58824. }
  58825. #endif
  58826. static int test_session_ticket_hs_update(void)
  58827. {
  58828. EXPECT_DECLS;
  58829. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  58830. defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  58831. struct test_memio_ctx test_ctx;
  58832. struct test_memio_ctx test_ctx2;
  58833. struct test_memio_ctx test_ctx3;
  58834. WOLFSSL_CTX *ctx_c = NULL;
  58835. WOLFSSL_CTX *ctx_s = NULL;
  58836. WOLFSSL *ssl_c = NULL;
  58837. WOLFSSL *ssl_c2 = NULL;
  58838. WOLFSSL *ssl_c3 = NULL;
  58839. WOLFSSL *ssl_s = NULL;
  58840. WOLFSSL *ssl_s2 = NULL;
  58841. WOLFSSL *ssl_s3 = NULL;
  58842. WOLFSSL_SESSION *sess = NULL;
  58843. byte read_data[1];
  58844. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58845. XMEMSET(&test_ctx2, 0, sizeof(test_ctx2));
  58846. XMEMSET(&test_ctx3, 0, sizeof(test_ctx3));
  58847. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58848. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  58849. /* Generate tickets */
  58850. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58851. wolfSSL_SetLoggingPrefix("client");
  58852. /* Read the ticket msg */
  58853. ExpectIntEQ(wolfSSL_read(ssl_c, read_data, sizeof(read_data)),
  58854. WOLFSSL_FATAL_ERROR);
  58855. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58856. WOLFSSL_ERROR_WANT_READ);
  58857. wolfSSL_SetLoggingPrefix(NULL);
  58858. ExpectIntEQ(test_memio_setup(&test_ctx2, &ctx_c, &ctx_s, &ssl_c2, &ssl_s2,
  58859. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  58860. ExpectIntEQ(test_memio_setup(&test_ctx3, &ctx_c, &ctx_s, &ssl_c3, &ssl_s3,
  58861. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  58862. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  58863. ExpectIntEQ(wolfSSL_set_session(ssl_c2, sess), WOLFSSL_SUCCESS);
  58864. ExpectIntEQ(wolfSSL_set_session(ssl_c3, sess), WOLFSSL_SUCCESS);
  58865. wolfSSL_SetLoggingPrefix("client");
  58866. /* Exchange initial flights for the second connection */
  58867. ExpectIntEQ(wolfSSL_connect(ssl_c2), WOLFSSL_FATAL_ERROR);
  58868. ExpectIntEQ(wolfSSL_get_error(ssl_c2, WOLFSSL_FATAL_ERROR),
  58869. WOLFSSL_ERROR_WANT_READ);
  58870. wolfSSL_SetLoggingPrefix(NULL);
  58871. wolfSSL_SetLoggingPrefix("server");
  58872. ExpectIntEQ(wolfSSL_accept(ssl_s2), WOLFSSL_FATAL_ERROR);
  58873. ExpectIntEQ(wolfSSL_get_error(ssl_s2, WOLFSSL_FATAL_ERROR),
  58874. WOLFSSL_ERROR_WANT_READ);
  58875. wolfSSL_SetLoggingPrefix(NULL);
  58876. /* Complete third connection so that new tickets are exchanged */
  58877. ExpectIntEQ(test_memio_do_handshake(ssl_c3, ssl_s3, 10, NULL), 0);
  58878. /* Read the ticket msg */
  58879. wolfSSL_SetLoggingPrefix("client");
  58880. ExpectIntEQ(wolfSSL_read(ssl_c3, read_data, sizeof(read_data)),
  58881. WOLFSSL_FATAL_ERROR);
  58882. ExpectIntEQ(wolfSSL_get_error(ssl_c3, WOLFSSL_FATAL_ERROR),
  58883. WOLFSSL_ERROR_WANT_READ);
  58884. wolfSSL_SetLoggingPrefix(NULL);
  58885. /* Complete second connection */
  58886. ExpectIntEQ(test_memio_do_handshake(ssl_c2, ssl_s2, 10, NULL), 0);
  58887. ExpectIntEQ(wolfSSL_session_reused(ssl_c2), 1);
  58888. ExpectIntEQ(wolfSSL_session_reused(ssl_c3), 1);
  58889. wolfSSL_free(ssl_c);
  58890. wolfSSL_free(ssl_c2);
  58891. wolfSSL_free(ssl_c3);
  58892. wolfSSL_free(ssl_s);
  58893. wolfSSL_free(ssl_s2);
  58894. wolfSSL_free(ssl_s3);
  58895. wolfSSL_CTX_free(ctx_c);
  58896. wolfSSL_CTX_free(ctx_s);
  58897. wolfSSL_SESSION_free(sess);
  58898. #endif
  58899. return EXPECT_RESULT();
  58900. }
  58901. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58902. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  58903. static void test_dtls_downgrade_scr_server_ctx_ready_server(WOLFSSL_CTX* ctx)
  58904. {
  58905. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  58906. WOLFSSL_SUCCESS);
  58907. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  58908. }
  58909. static void test_dtls_downgrade_scr_server_ctx_ready(WOLFSSL_CTX* ctx)
  58910. {
  58911. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  58912. }
  58913. static void test_dtls_downgrade_scr_server_on_result(WOLFSSL* ssl)
  58914. {
  58915. char testMsg[] = "Message after SCR";
  58916. char msgBuf[sizeof(testMsg)];
  58917. if (wolfSSL_is_server(ssl)) {
  58918. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  58919. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  58920. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  58921. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  58922. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  58923. sizeof(testMsg));
  58924. }
  58925. else {
  58926. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  58927. sizeof(testMsg));
  58928. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  58929. }
  58930. }
  58931. static int test_dtls_downgrade_scr_server(void)
  58932. {
  58933. EXPECT_DECLS;
  58934. callback_functions func_cb_client;
  58935. callback_functions func_cb_server;
  58936. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  58937. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  58938. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  58939. func_cb_client.method = wolfDTLSv1_2_client_method;
  58940. func_cb_server.method = wolfDTLS_server_method;
  58941. func_cb_client.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready;
  58942. func_cb_server.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready_server;
  58943. func_cb_client.on_result = test_dtls_downgrade_scr_server_on_result;
  58944. func_cb_server.on_result = test_dtls_downgrade_scr_server_on_result;
  58945. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  58946. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  58947. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  58948. return EXPECT_RESULT();
  58949. }
  58950. #else
  58951. static int test_dtls_downgrade_scr_server(void)
  58952. {
  58953. EXPECT_DECLS;
  58954. return EXPECT_RESULT();
  58955. }
  58956. #endif
  58957. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58958. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  58959. static void test_dtls_downgrade_scr_ctx_ready(WOLFSSL_CTX* ctx)
  58960. {
  58961. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  58962. WOLFSSL_SUCCESS);
  58963. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  58964. }
  58965. static void test_dtls_downgrade_scr_on_result(WOLFSSL* ssl)
  58966. {
  58967. char testMsg[] = "Message after SCR";
  58968. char msgBuf[sizeof(testMsg)];
  58969. if (wolfSSL_is_server(ssl)) {
  58970. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  58971. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  58972. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  58973. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  58974. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  58975. sizeof(testMsg));
  58976. }
  58977. else {
  58978. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  58979. sizeof(testMsg));
  58980. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  58981. }
  58982. }
  58983. static int test_dtls_downgrade_scr(void)
  58984. {
  58985. EXPECT_DECLS;
  58986. callback_functions func_cb_client;
  58987. callback_functions func_cb_server;
  58988. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  58989. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  58990. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  58991. func_cb_client.method = wolfDTLS_client_method;
  58992. func_cb_server.method = wolfDTLSv1_2_server_method;
  58993. func_cb_client.ctx_ready = test_dtls_downgrade_scr_ctx_ready;
  58994. func_cb_client.on_result = test_dtls_downgrade_scr_on_result;
  58995. func_cb_server.on_result = test_dtls_downgrade_scr_on_result;
  58996. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  58997. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  58998. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  58999. return EXPECT_RESULT();
  59000. }
  59001. #else
  59002. static int test_dtls_downgrade_scr(void)
  59003. {
  59004. EXPECT_DECLS;
  59005. return EXPECT_RESULT();
  59006. }
  59007. #endif
  59008. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  59009. && !defined(WOLFSSL_NO_TLS12)
  59010. static int test_dtls_client_hello_timeout_downgrade_read_cb(WOLFSSL *ssl,
  59011. char *data, int sz, void *ctx)
  59012. {
  59013. static int call_counter = 0;
  59014. call_counter++;
  59015. (void)ssl;
  59016. (void)data;
  59017. (void)sz;
  59018. (void)ctx;
  59019. switch (call_counter) {
  59020. case 1:
  59021. case 2:
  59022. return WOLFSSL_CBIO_ERR_TIMEOUT;
  59023. case 3:
  59024. return WOLFSSL_CBIO_ERR_WANT_READ;
  59025. default:
  59026. AssertIntLE(call_counter, 3);
  59027. return -1;
  59028. }
  59029. }
  59030. #endif
  59031. /* Make sure we don't send acks before getting a server hello */
  59032. static int test_dtls_client_hello_timeout_downgrade(void)
  59033. {
  59034. EXPECT_DECLS;
  59035. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  59036. && !defined(WOLFSSL_NO_TLS12)
  59037. WOLFSSL_CTX *ctx_c = NULL;
  59038. WOLFSSL_CTX *ctx_s = NULL;
  59039. WOLFSSL *ssl_c = NULL;
  59040. WOLFSSL *ssl_s = NULL;
  59041. struct test_memio_ctx test_ctx;
  59042. DtlsRecordLayerHeader* dtlsRH;
  59043. size_t len;
  59044. byte sequence_number[8];
  59045. int i;
  59046. for (i = 0; i < 2; i++) {
  59047. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59048. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59049. wolfDTLS_client_method, wolfDTLSv1_2_server_method), 0);
  59050. if (i == 0) {
  59051. /* First time simulate timeout in IO layer */
  59052. /* CH1 */
  59053. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59054. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59055. /* HVR */
  59056. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59057. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59058. /* CH2 */
  59059. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59060. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59061. /* SH flight */
  59062. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59063. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59064. /* Drop the SH */
  59065. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  59066. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  59067. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  59068. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  59069. (sizeof(DtlsRecordLayerHeader) + len));
  59070. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  59071. /* Read the remainder of the flight */
  59072. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59073. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59074. wolfSSL_SSLSetIORecv(ssl_c,
  59075. test_dtls_client_hello_timeout_downgrade_read_cb);
  59076. /* CH3 */
  59077. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59078. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59079. wolfSSL_SSLSetIORecv(ssl_c, test_memio_read_cb);
  59080. }
  59081. else {
  59082. /* Second time call wolfSSL_dtls_got_timeout */
  59083. /* CH1 */
  59084. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59085. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59086. /* HVR */
  59087. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59088. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59089. /* CH2 */
  59090. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59091. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59092. /* SH flight */
  59093. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59094. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59095. /* Drop the SH */
  59096. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  59097. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  59098. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  59099. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  59100. (sizeof(DtlsRecordLayerHeader) + len));
  59101. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  59102. /* Read the remainder of the flight */
  59103. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59104. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59105. /* Quick timeout should be set as we received at least one msg */
  59106. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 1);
  59107. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  59108. /* Quick timeout should be cleared after a quick timeout */
  59109. /* CH3 */
  59110. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 0);
  59111. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  59112. }
  59113. /* Parse out to make sure we got exactly one ClientHello message */
  59114. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  59115. /* Second ClientHello after HVR */
  59116. sequence_number[7] = 2;
  59117. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.s_buff;
  59118. ExpectIntEQ(dtlsRH->type, handshake);
  59119. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  59120. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  59121. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  59122. sizeof(sequence_number)), 0);
  59123. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  59124. ExpectIntEQ(sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  59125. /* Connection should be able to continue */
  59126. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59127. wolfSSL_free(ssl_c);
  59128. wolfSSL_free(ssl_s);
  59129. wolfSSL_CTX_free(ctx_c);
  59130. wolfSSL_CTX_free(ctx_s);
  59131. ssl_c = NULL;
  59132. ssl_s = NULL;
  59133. ctx_c = NULL;
  59134. ctx_s = NULL;
  59135. if (!EXPECT_SUCCESS())
  59136. break;
  59137. }
  59138. #endif
  59139. return EXPECT_RESULT();
  59140. }
  59141. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  59142. static int test_dtls_client_hello_timeout_read_cb(WOLFSSL *ssl, char *data,
  59143. int sz, void *ctx)
  59144. {
  59145. static int call_counter = 0;
  59146. call_counter++;
  59147. (void)ssl;
  59148. (void)data;
  59149. (void)sz;
  59150. (void)ctx;
  59151. switch (call_counter) {
  59152. case 1:
  59153. return WOLFSSL_CBIO_ERR_TIMEOUT;
  59154. case 2:
  59155. return WOLFSSL_CBIO_ERR_WANT_READ;
  59156. default:
  59157. AssertIntLE(call_counter, 2);
  59158. return -1;
  59159. }
  59160. }
  59161. #endif
  59162. /* Make sure we don't send acks before getting a server hello */
  59163. static int test_dtls_client_hello_timeout(void)
  59164. {
  59165. EXPECT_DECLS;
  59166. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  59167. WOLFSSL *ssl_c = NULL;
  59168. WOLFSSL_CTX *ctx_c = NULL;
  59169. struct test_memio_ctx test_ctx;
  59170. DtlsRecordLayerHeader* dtlsRH;
  59171. size_t idx;
  59172. size_t len;
  59173. byte sequence_number[8];
  59174. int i;
  59175. for (i = 0; i < 2; i++) {
  59176. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59177. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  59178. wolfDTLSv1_3_client_method, NULL), 0);
  59179. if (i == 0) {
  59180. /* First time simulate timeout in IO layer */
  59181. wolfSSL_SSLSetIORecv(ssl_c, test_dtls_client_hello_timeout_read_cb);
  59182. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  59183. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59184. }
  59185. else {
  59186. /* Second time call wolfSSL_dtls_got_timeout */
  59187. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  59188. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59189. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  59190. }
  59191. /* Parse out to make sure we got exactly two ClientHello messages */
  59192. idx = 0;
  59193. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  59194. /* First ClientHello */
  59195. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  59196. ExpectIntEQ(dtlsRH->type, handshake);
  59197. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  59198. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  59199. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  59200. sizeof(sequence_number)), 0);
  59201. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  59202. ExpectIntLT(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  59203. idx += sizeof(DtlsRecordLayerHeader) + len;
  59204. /* Second ClientHello */
  59205. sequence_number[7] = 1;
  59206. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  59207. ExpectIntEQ(dtlsRH->type, handshake);
  59208. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  59209. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  59210. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  59211. sizeof(sequence_number)), 0);
  59212. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  59213. ExpectIntEQ(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  59214. wolfSSL_free(ssl_c);
  59215. wolfSSL_CTX_free(ctx_c);
  59216. ssl_c = NULL;
  59217. ctx_c = NULL;
  59218. if (!EXPECT_SUCCESS())
  59219. break;
  59220. }
  59221. #endif
  59222. return EXPECT_RESULT();
  59223. }
  59224. /* DTLS test when dropping the changed cipher spec message */
  59225. static int test_dtls_dropped_ccs(void)
  59226. {
  59227. EXPECT_DECLS;
  59228. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  59229. && !defined(WOLFSSL_NO_TLS12)
  59230. WOLFSSL_CTX *ctx_c = NULL;
  59231. WOLFSSL_CTX *ctx_s = NULL;
  59232. WOLFSSL *ssl_c = NULL;
  59233. WOLFSSL *ssl_s = NULL;
  59234. struct test_memio_ctx test_ctx;
  59235. DtlsRecordLayerHeader* dtlsRH;
  59236. size_t len;
  59237. byte data[1];
  59238. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59239. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59240. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  59241. /* CH1 */
  59242. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59243. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59244. /* HVR */
  59245. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59246. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59247. /* CH2 */
  59248. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59249. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59250. /* Server first flight */
  59251. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59252. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59253. /* Client flight */
  59254. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59255. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59256. /* Server ccs + finished */
  59257. ExpectIntEQ(wolfSSL_negotiate(ssl_s), 1);
  59258. /* Drop the ccs */
  59259. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  59260. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  59261. ExpectIntEQ(len, 1);
  59262. ExpectIntEQ(dtlsRH->type, change_cipher_spec);
  59263. if (EXPECT_SUCCESS()) {
  59264. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  59265. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  59266. (sizeof(DtlsRecordLayerHeader) + len));
  59267. }
  59268. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  59269. /* Client rtx flight */
  59270. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59271. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59272. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  59273. /* Server ccs + finished rtx */
  59274. ExpectIntEQ(wolfSSL_read(ssl_s, data, sizeof(data)), -1);
  59275. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59276. /* Client processes finished */
  59277. ExpectIntEQ(wolfSSL_negotiate(ssl_c), 1);
  59278. wolfSSL_free(ssl_c);
  59279. wolfSSL_free(ssl_s);
  59280. wolfSSL_CTX_free(ctx_c);
  59281. wolfSSL_CTX_free(ctx_s);
  59282. #endif
  59283. return EXPECT_RESULT();
  59284. }
  59285. /**
  59286. * Make sure we don't send RSA Signature Hash Algorithms in the
  59287. * CertificateRequest when we don't have any such ciphers set.
  59288. * @return EXPECT_RESULT()
  59289. */
  59290. static int test_certreq_sighash_algos(void)
  59291. {
  59292. EXPECT_DECLS;
  59293. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  59294. !defined(WOLFSSL_MAX_STRENGTH) && defined(HAVE_ECC) && \
  59295. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  59296. defined(HAVE_AES_CBC) && !defined(WOLFSSL_NO_TLS12)
  59297. WOLFSSL_CTX *ctx_c = NULL;
  59298. WOLFSSL_CTX *ctx_s = NULL;
  59299. WOLFSSL *ssl_c = NULL;
  59300. WOLFSSL *ssl_s = NULL;
  59301. struct test_memio_ctx test_ctx;
  59302. int idx = 0;
  59303. int maxIdx = 0;
  59304. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59305. test_ctx.c_ciphers = test_ctx.s_ciphers =
  59306. "ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA384";
  59307. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59308. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  59309. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx_c,
  59310. "./certs/ca-ecc-cert.pem", NULL), WOLFSSL_SUCCESS);
  59311. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_PEER, NULL);
  59312. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ssl_s, "./certs/ecc-key.pem",
  59313. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59314. ExpectIntEQ(wolfSSL_use_certificate_file(ssl_s, "./certs/server-ecc.pem",
  59315. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59316. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_FATAL_ERROR);
  59317. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59318. WOLFSSL_ERROR_WANT_READ);
  59319. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_FATAL_ERROR);
  59320. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  59321. WOLFSSL_ERROR_WANT_READ);
  59322. /* Find the CertificateRequest message */
  59323. for (idx = 0; idx < test_ctx.c_len && EXPECT_SUCCESS();) {
  59324. word16 len;
  59325. ExpectIntEQ(test_ctx.c_buff[idx++], handshake);
  59326. ExpectIntEQ(test_ctx.c_buff[idx++], SSLv3_MAJOR);
  59327. ExpectIntEQ(test_ctx.c_buff[idx++], TLSv1_2_MINOR);
  59328. ato16(test_ctx.c_buff + idx, &len);
  59329. idx += OPAQUE16_LEN;
  59330. if (test_ctx.c_buff[idx] == certificate_request) {
  59331. idx++;
  59332. /* length */
  59333. idx += OPAQUE24_LEN;
  59334. /* cert types */
  59335. idx += 1 + test_ctx.c_buff[idx];
  59336. /* Sig algos */
  59337. ato16(test_ctx.c_buff + idx, &len);
  59338. idx += OPAQUE16_LEN;
  59339. maxIdx = idx + (int)len;
  59340. for (; idx < maxIdx && EXPECT_SUCCESS(); idx += OPAQUE16_LEN) {
  59341. if (test_ctx.c_buff[idx+1] == ED25519_SA_MINOR ||
  59342. test_ctx.c_buff[idx+1] == ED448_SA_MINOR)
  59343. ExpectIntEQ(test_ctx.c_buff[idx], NEW_SA_MAJOR);
  59344. else
  59345. ExpectIntEQ(test_ctx.c_buff[idx+1], ecc_dsa_sa_algo);
  59346. }
  59347. break;
  59348. }
  59349. else {
  59350. idx += (int)len;
  59351. }
  59352. }
  59353. ExpectIntLT(idx, test_ctx.c_len);
  59354. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59355. wolfSSL_free(ssl_c);
  59356. wolfSSL_free(ssl_s);
  59357. wolfSSL_CTX_free(ctx_c);
  59358. wolfSSL_CTX_free(ctx_s);
  59359. #endif
  59360. return EXPECT_RESULT();
  59361. }
  59362. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  59363. static int test_revoked_loaded_int_cert_ctx_ready1(WOLFSSL_CTX* ctx)
  59364. {
  59365. EXPECT_DECLS;
  59366. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  59367. myVerifyAction = VERIFY_USE_PREVERFIY;
  59368. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  59369. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  59370. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  59371. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  59372. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  59373. WOLFSSL_SUCCESS);
  59374. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  59375. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  59376. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59377. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  59378. "./certs/crl/ca-int.pem",
  59379. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59380. return EXPECT_RESULT();
  59381. }
  59382. static int test_revoked_loaded_int_cert_ctx_ready2(WOLFSSL_CTX* ctx)
  59383. {
  59384. EXPECT_DECLS;
  59385. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  59386. myVerifyAction = VERIFY_USE_PREVERFIY;
  59387. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  59388. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  59389. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  59390. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  59391. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  59392. "./certs/intermediate/ca-int2-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  59393. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  59394. WOLFSSL_SUCCESS);
  59395. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  59396. "./certs/crl/ca-int2.pem",
  59397. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59398. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  59399. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  59400. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59401. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  59402. "./certs/crl/ca-int.pem",
  59403. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59404. return EXPECT_RESULT();
  59405. }
  59406. #endif
  59407. static int test_revoked_loaded_int_cert(void)
  59408. {
  59409. EXPECT_DECLS;
  59410. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  59411. test_ssl_cbf client_cbf;
  59412. test_ssl_cbf server_cbf;
  59413. struct {
  59414. const char* certPemFile;
  59415. const char* keyPemFile;
  59416. ctx_cb client_ctx_ready;
  59417. } test_params[] = {
  59418. {"./certs/intermediate/ca-int2-cert.pem",
  59419. "./certs/intermediate/ca-int2-key.pem",
  59420. test_revoked_loaded_int_cert_ctx_ready1},
  59421. {"./certs/intermediate/server-chain.pem",
  59422. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  59423. {"./certs/intermediate/server-chain-short.pem",
  59424. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  59425. };
  59426. size_t i;
  59427. printf("\n");
  59428. for (i = 0; i < XELEM_CNT(test_params); i++) {
  59429. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  59430. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  59431. printf("\tTesting with %s...\n", test_params[i].certPemFile);
  59432. server_cbf.certPemFile = test_params[i].certPemFile;
  59433. server_cbf.keyPemFile = test_params[i].keyPemFile;
  59434. client_cbf.ctx_ready = test_params[i].client_ctx_ready;
  59435. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  59436. &server_cbf, NULL), TEST_FAIL);
  59437. #ifndef WOLFSSL_HAPROXY
  59438. ExpectIntEQ(client_cbf.last_err, CRL_CERT_REVOKED);
  59439. #else
  59440. ExpectIntEQ(client_cbf.last_err, WOLFSSL_X509_V_ERR_CERT_REVOKED);
  59441. #endif
  59442. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  59443. if (!EXPECT_SUCCESS())
  59444. break;
  59445. printf("\t%s passed\n", test_params[i].certPemFile);
  59446. }
  59447. #endif
  59448. return EXPECT_RESULT();
  59449. }
  59450. static int test_dtls13_frag_ch_pq(void)
  59451. {
  59452. EXPECT_DECLS;
  59453. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  59454. && defined(WOLFSSL_DTLS_CH_FRAG) && defined(HAVE_LIBOQS)
  59455. WOLFSSL_CTX *ctx_c = NULL;
  59456. WOLFSSL_CTX *ctx_s = NULL;
  59457. WOLFSSL *ssl_c = NULL;
  59458. WOLFSSL *ssl_s = NULL;
  59459. struct test_memio_ctx test_ctx;
  59460. const char *test_str = "test";
  59461. int test_str_size;
  59462. byte buf[255];
  59463. int group = WOLFSSL_KYBER_LEVEL5;
  59464. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59465. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59466. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  59467. /* Add in a large post-quantum key share to make the CH long. */
  59468. ExpectIntEQ(wolfSSL_set_groups(ssl_c, &group, 1), WOLFSSL_SUCCESS);
  59469. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, group), WOLFSSL_SUCCESS);
  59470. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  59471. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59472. ExpectStrEQ(wolfSSL_get_curve_name(ssl_c), "KYBER_LEVEL5");
  59473. ExpectStrEQ(wolfSSL_get_curve_name(ssl_s), "KYBER_LEVEL5");
  59474. test_str_size = XSTRLEN("test") + 1;
  59475. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  59476. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  59477. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  59478. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  59479. wolfSSL_free(ssl_c);
  59480. wolfSSL_free(ssl_s);
  59481. wolfSSL_CTX_free(ctx_c);
  59482. wolfSSL_CTX_free(ctx_s);
  59483. #endif
  59484. return EXPECT_RESULT();
  59485. }
  59486. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  59487. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  59488. static int test_dtls_frag_ch_count_records(byte* b, int len)
  59489. {
  59490. DtlsRecordLayerHeader* dtlsRH;
  59491. int records = 0;
  59492. size_t recordLen;
  59493. while (len > 0) {
  59494. records++;
  59495. dtlsRH = (DtlsRecordLayerHeader*)b;
  59496. recordLen = (dtlsRH->length[0] << 8) | dtlsRH->length[1];
  59497. b += sizeof(DtlsRecordLayerHeader) + recordLen;
  59498. len -= sizeof(DtlsRecordLayerHeader) + recordLen;
  59499. }
  59500. return records;
  59501. }
  59502. #endif
  59503. static int test_dtls_frag_ch(void)
  59504. {
  59505. EXPECT_DECLS;
  59506. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  59507. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  59508. WOLFSSL_CTX *ctx_c = NULL;
  59509. WOLFSSL_CTX *ctx_s = NULL;
  59510. WOLFSSL *ssl_c = NULL;
  59511. WOLFSSL *ssl_s = NULL;
  59512. struct test_memio_ctx test_ctx;
  59513. static unsigned int DUMMY_MTU = 256;
  59514. unsigned char four_frag_CH[] = {
  59515. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59516. 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59517. 0xce, 0xfe, 0xfd, 0xf3, 0x94, 0x01, 0x33, 0x2c, 0xcf, 0x2c, 0x47, 0xb1,
  59518. 0xe5, 0xa1, 0x7b, 0x19, 0x3e, 0xac, 0x68, 0xdd, 0xe6, 0x17, 0x6b, 0x85,
  59519. 0xad, 0x5f, 0xfc, 0x7f, 0x6e, 0xf0, 0xb9, 0xe0, 0x2e, 0xca, 0x47, 0x00,
  59520. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  59521. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  59522. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  59523. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  59524. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x02,
  59525. 0x7c, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  59526. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  59527. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  59528. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  59529. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  59530. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x02, 0x39, 0x02, 0x37, 0x00, 0x17,
  59531. 0x00, 0x41, 0x04, 0x94, 0xdf, 0x36, 0xd7, 0xb3, 0x90, 0x6d, 0x01, 0xa1,
  59532. 0xe6, 0xed, 0x67, 0xf4, 0xd9, 0x9d, 0x2c, 0xac, 0x57, 0x74, 0xff, 0x19,
  59533. 0xbe, 0x5a, 0xc9, 0x30, 0x11, 0xb7, 0x2b, 0x59, 0x47, 0x80, 0x7c, 0xa9,
  59534. 0xb7, 0x31, 0x8c, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59535. 0x00, 0x01, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00,
  59536. 0xce, 0x00, 0x00, 0xce, 0x9e, 0x13, 0x74, 0x3b, 0x86, 0xba, 0x69, 0x1f,
  59537. 0x12, 0xf7, 0xcd, 0x78, 0x53, 0xe8, 0x50, 0x4d, 0x71, 0x3f, 0x4b, 0x4e,
  59538. 0xeb, 0x3e, 0xe5, 0x43, 0x54, 0x78, 0x17, 0x6d, 0x00, 0x18, 0x00, 0x61,
  59539. 0x04, 0xd1, 0x99, 0x66, 0x4f, 0xda, 0xc7, 0x12, 0x3b, 0xff, 0xb2, 0xd6,
  59540. 0x2f, 0x35, 0xb6, 0x17, 0x1f, 0xb3, 0xd0, 0xb6, 0x52, 0xff, 0x97, 0x8b,
  59541. 0x01, 0xe8, 0xd9, 0x68, 0x71, 0x40, 0x02, 0xd5, 0x68, 0x3a, 0x58, 0xb2,
  59542. 0x5d, 0xee, 0xa4, 0xe9, 0x5f, 0xf4, 0xaf, 0x3e, 0x30, 0x9c, 0x3e, 0x2b,
  59543. 0xda, 0x61, 0x43, 0x99, 0x02, 0x35, 0x33, 0x9f, 0xcf, 0xb5, 0xd3, 0x28,
  59544. 0x19, 0x9d, 0x1c, 0xbe, 0x69, 0x07, 0x9e, 0xfc, 0xe4, 0x8e, 0xcd, 0x86,
  59545. 0x4a, 0x1b, 0xf0, 0xfc, 0x17, 0x94, 0x66, 0x53, 0xda, 0x24, 0x5e, 0xaf,
  59546. 0xce, 0xec, 0x62, 0x4c, 0x06, 0xb4, 0x52, 0x94, 0xb1, 0x4a, 0x7a, 0x8c,
  59547. 0x4f, 0x00, 0x19, 0x00, 0x85, 0x04, 0x00, 0x27, 0xeb, 0x99, 0x49, 0x7f,
  59548. 0xcb, 0x2c, 0x46, 0x54, 0x2d, 0x93, 0x5d, 0x25, 0x92, 0x58, 0x5e, 0x06,
  59549. 0xc3, 0x7c, 0xfb, 0x9a, 0xa7, 0xec, 0xcd, 0x9f, 0xe1, 0x6b, 0x2d, 0x78,
  59550. 0xf5, 0x16, 0xa9, 0x20, 0x52, 0x48, 0x19, 0x0f, 0x1a, 0xd0, 0xce, 0xd8,
  59551. 0x68, 0xb1, 0x4e, 0x7f, 0x33, 0x03, 0x7d, 0x0c, 0x39, 0xdb, 0x9c, 0x4b,
  59552. 0xf4, 0xe7, 0xc2, 0xf5, 0xdd, 0x51, 0x9b, 0x03, 0xa8, 0x53, 0x2b, 0xe6,
  59553. 0x00, 0x15, 0x4b, 0xff, 0xd2, 0xa0, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00,
  59554. 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00,
  59555. 0x00, 0x00, 0x01, 0x9c, 0x00, 0x00, 0xce, 0x58, 0x30, 0x10, 0x3d, 0x46,
  59556. 0xcc, 0xca, 0x1a, 0x44, 0xc8, 0x58, 0x9b, 0x27, 0x17, 0x67, 0x31, 0x96,
  59557. 0x8a, 0x66, 0x39, 0xf4, 0xcc, 0xc1, 0x9f, 0x12, 0x1f, 0x01, 0x30, 0x50,
  59558. 0x16, 0xd6, 0x89, 0x97, 0xa3, 0x66, 0xd7, 0x99, 0x50, 0x09, 0x6e, 0x80,
  59559. 0x87, 0xe4, 0xa2, 0x88, 0xae, 0xb4, 0x23, 0x57, 0x2f, 0x12, 0x60, 0xe7,
  59560. 0x7d, 0x44, 0x2d, 0xad, 0xbe, 0xe9, 0x0d, 0x01, 0x00, 0x01, 0x00, 0xd5,
  59561. 0xdd, 0x62, 0xee, 0xf3, 0x0e, 0xd9, 0x30, 0x0e, 0x38, 0xf3, 0x48, 0xf4,
  59562. 0xc9, 0x8f, 0x8c, 0x20, 0xf7, 0xd3, 0xa8, 0xb3, 0x87, 0x3c, 0x98, 0x5d,
  59563. 0x70, 0xc5, 0x03, 0x76, 0xb7, 0xd5, 0x0b, 0x7b, 0x23, 0x97, 0x6b, 0xe3,
  59564. 0xb5, 0x18, 0xeb, 0x64, 0x55, 0x18, 0xb2, 0x8a, 0x90, 0x1a, 0x8f, 0x0e,
  59565. 0x15, 0xda, 0xb1, 0x8e, 0x7f, 0xee, 0x1f, 0xe0, 0x3b, 0xb9, 0xed, 0xfc,
  59566. 0x4e, 0x3f, 0x78, 0x16, 0x39, 0x95, 0x5f, 0xb7, 0xcb, 0x65, 0x55, 0x72,
  59567. 0x7b, 0x7d, 0x86, 0x2f, 0x8a, 0xe5, 0xee, 0xf7, 0x57, 0x40, 0xf3, 0xc4,
  59568. 0x96, 0x4f, 0x11, 0x4d, 0x85, 0xf9, 0x56, 0xfa, 0x3d, 0xf0, 0xc9, 0xa4,
  59569. 0xec, 0x1e, 0xaa, 0x47, 0x90, 0x53, 0xdf, 0xe1, 0xb7, 0x78, 0x18, 0xeb,
  59570. 0xdd, 0x0d, 0x89, 0xb7, 0xf6, 0x15, 0x0e, 0x55, 0x12, 0xb3, 0x23, 0x17,
  59571. 0x0b, 0x59, 0x6f, 0x83, 0x05, 0x6b, 0xa6, 0xf8, 0x6c, 0x3a, 0x9b, 0x1b,
  59572. 0x50, 0x93, 0x51, 0xea, 0x95, 0x2d, 0x99, 0x96, 0x38, 0x16, 0xfe, 0xfd,
  59573. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x7e, 0x01, 0x00,
  59574. 0x02, 0xdc, 0x00, 0x00, 0x00, 0x02, 0x6a, 0x00, 0x00, 0x72, 0x2d, 0x66,
  59575. 0x3e, 0xf2, 0x36, 0x5a, 0xf2, 0x23, 0x8f, 0x28, 0x09, 0xa9, 0x55, 0x8c,
  59576. 0x8f, 0xc0, 0x0d, 0x61, 0x98, 0x33, 0x56, 0x87, 0x7a, 0xfd, 0xa7, 0x50,
  59577. 0x71, 0x84, 0x2e, 0x41, 0x58, 0x00, 0x87, 0xd9, 0x27, 0xe5, 0x7b, 0xf4,
  59578. 0x6d, 0x84, 0x4e, 0x2e, 0x0c, 0x80, 0x0c, 0xf3, 0x8a, 0x02, 0x4b, 0x99,
  59579. 0x3a, 0x1f, 0x9f, 0x18, 0x7d, 0x1c, 0xec, 0xad, 0x60, 0x54, 0xa6, 0xa3,
  59580. 0x2c, 0x82, 0x5e, 0xf8, 0x8f, 0xae, 0xe1, 0xc4, 0x82, 0x7e, 0x43, 0x43,
  59581. 0xc5, 0x99, 0x49, 0x05, 0xd3, 0xf6, 0xdf, 0xa1, 0xb5, 0x2d, 0x0c, 0x13,
  59582. 0x2f, 0x1e, 0xb6, 0x28, 0x7c, 0x5c, 0xa1, 0x02, 0x6b, 0x8d, 0xa3, 0xeb,
  59583. 0xd4, 0x58, 0xe6, 0xa0, 0x7e, 0x6b, 0xaa, 0x09, 0x43, 0x67, 0x71, 0x87,
  59584. 0xa5, 0xcb, 0x68, 0xf3
  59585. };
  59586. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59587. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59588. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  59589. /* Fragment msgs */
  59590. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_c, DUMMY_MTU), WOLFSSL_SUCCESS);
  59591. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_s, DUMMY_MTU), WOLFSSL_SUCCESS);
  59592. /* Add in some key shares to make the CH long */
  59593. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP256R1),
  59594. WOLFSSL_SUCCESS);
  59595. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP384R1),
  59596. WOLFSSL_SUCCESS);
  59597. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP521R1),
  59598. WOLFSSL_SUCCESS);
  59599. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_FFDHE_2048),
  59600. WOLFSSL_SUCCESS);
  59601. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  59602. /* Reject fragmented first CH */
  59603. ExpectIntEQ(test_dtls_frag_ch_count_records(four_frag_CH,
  59604. sizeof(four_frag_CH)), 4);
  59605. XMEMCPY(test_ctx.s_buff, four_frag_CH, sizeof(four_frag_CH));
  59606. test_ctx.s_len = sizeof(four_frag_CH);
  59607. while (test_ctx.s_len > 0 && EXPECT_SUCCESS()) {
  59608. int s_len = test_ctx.s_len;
  59609. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59610. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59611. /* Fail if we didn't advance the buffer to avoid infinite loops */
  59612. ExpectIntLT(test_ctx.s_len, s_len);
  59613. }
  59614. /* Expect all fragments read */
  59615. ExpectIntEQ(test_ctx.s_len, 0);
  59616. /* Expect quietly dropping fragmented first CH */
  59617. ExpectIntEQ(test_ctx.c_len, 0);
  59618. /* CH1 */
  59619. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59620. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59621. /* Count records. Expect 1 unfragmented CH */
  59622. ExpectIntEQ(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  59623. test_ctx.s_len), 1);
  59624. /* HRR */
  59625. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59626. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59627. /* CH2 */
  59628. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59629. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59630. /* Count records. Expect fragmented CH */
  59631. ExpectIntGT(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  59632. test_ctx.s_len), 1);
  59633. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59634. wolfSSL_free(ssl_c);
  59635. wolfSSL_free(ssl_s);
  59636. wolfSSL_CTX_free(ctx_c);
  59637. wolfSSL_CTX_free(ctx_s);
  59638. ssl_c = ssl_s = NULL;
  59639. ctx_c = ctx_s = NULL;
  59640. #endif
  59641. return EXPECT_RESULT();
  59642. }
  59643. static int test_dtls_empty_keyshare_with_cookie(void)
  59644. {
  59645. EXPECT_DECLS;
  59646. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  59647. WOLFSSL_CTX *ctx_s = NULL;
  59648. WOLFSSL *ssl_s = NULL;
  59649. struct test_memio_ctx test_ctx;
  59650. unsigned char ch_empty_keyshare_with_cookie[] = {
  59651. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  59652. 0x12, 0x01, 0x00, 0x01, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
  59653. 0x06, 0xfe, 0xfd, 0xfb, 0x8c, 0x9b, 0x28, 0xae, 0x50, 0x1c, 0x4d, 0xf3,
  59654. 0xb8, 0xcf, 0x4d, 0xd8, 0x7e, 0x93, 0x13, 0x7b, 0x9e, 0xd9, 0xeb, 0xe9,
  59655. 0x13, 0x4b, 0x0d, 0x7f, 0x2e, 0x43, 0x62, 0x8c, 0xe4, 0x57, 0x79, 0x00,
  59656. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  59657. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  59658. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  59659. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  59660. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x00,
  59661. 0xa6, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x2c, 0x00, 0x47,
  59662. 0x00, 0x45, 0x20, 0xee, 0x4b, 0x17, 0x70, 0x63, 0xa0, 0x4c, 0x82, 0xbf,
  59663. 0x43, 0x01, 0x7d, 0x8d, 0xc1, 0x1b, 0x4e, 0x9b, 0xa0, 0x3c, 0x53, 0x1f,
  59664. 0xb7, 0xd1, 0x10, 0x81, 0xa8, 0xdf, 0xdf, 0x8c, 0x7f, 0xf3, 0x11, 0x13,
  59665. 0x01, 0x02, 0x3d, 0x3b, 0x7d, 0x14, 0x2c, 0x31, 0xb3, 0x60, 0x72, 0x4d,
  59666. 0xe5, 0x1a, 0xb2, 0xa3, 0x61, 0x77, 0x73, 0x03, 0x40, 0x0e, 0x5f, 0xc5,
  59667. 0x61, 0x38, 0x43, 0x56, 0x21, 0x4a, 0x95, 0xd5, 0x35, 0xa8, 0x0d, 0x00,
  59668. 0x0d, 0x00, 0x2a, 0x00, 0x28, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02,
  59669. 0x03, 0xfe, 0x0b, 0xfe, 0x0e, 0xfe, 0xa0, 0xfe, 0xa3, 0xfe, 0xa5, 0x08,
  59670. 0x06, 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06,
  59671. 0x01, 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00,
  59672. 0x18, 0x00, 0x16, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01,
  59673. 0x00, 0x02, 0x3a, 0x02, 0x3c, 0x02, 0x3d, 0x2f, 0x3a, 0x2f, 0x3c, 0x2f,
  59674. 0x3d, 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x00, 0x02, 0x00, 0x00
  59675. };
  59676. DtlsRecordLayerHeader* dtlsRH;
  59677. byte sequence_number[8];
  59678. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  59679. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59680. XMEMCPY(test_ctx.s_buff, ch_empty_keyshare_with_cookie,
  59681. sizeof(ch_empty_keyshare_with_cookie));
  59682. test_ctx.s_len = sizeof(ch_empty_keyshare_with_cookie);
  59683. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  59684. NULL, wolfDTLSv1_3_server_method), 0);
  59685. /* CH1 */
  59686. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59687. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59688. /* Expect an alert. A plaintext alert should be exactly 15 bytes. */
  59689. ExpectIntEQ(test_ctx.c_len, 15);
  59690. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  59691. ExpectIntEQ(dtlsRH->type, alert);
  59692. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  59693. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  59694. sequence_number[7] = 1;
  59695. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  59696. sizeof(sequence_number)), 0);
  59697. ExpectIntEQ(dtlsRH->length[0], 0);
  59698. ExpectIntEQ(dtlsRH->length[1], 2);
  59699. ExpectIntEQ(test_ctx.c_buff[13], alert_fatal);
  59700. ExpectIntEQ(test_ctx.c_buff[14], illegal_parameter);
  59701. wolfSSL_free(ssl_s);
  59702. wolfSSL_CTX_free(ctx_s);
  59703. #endif
  59704. return EXPECT_RESULT();
  59705. }
  59706. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  59707. defined(HAVE_LIBOQS)
  59708. static void test_tls13_pq_groups_ctx_ready(WOLFSSL_CTX* ctx)
  59709. {
  59710. int group = WOLFSSL_KYBER_LEVEL5;
  59711. AssertIntEQ(wolfSSL_CTX_set_groups(ctx, &group, 1), WOLFSSL_SUCCESS);
  59712. }
  59713. static void test_tls13_pq_groups_on_result(WOLFSSL* ssl)
  59714. {
  59715. AssertStrEQ(wolfSSL_get_curve_name(ssl), "KYBER_LEVEL5");
  59716. }
  59717. #endif
  59718. static int test_tls13_pq_groups(void)
  59719. {
  59720. EXPECT_DECLS;
  59721. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  59722. defined(HAVE_LIBOQS)
  59723. callback_functions func_cb_client;
  59724. callback_functions func_cb_server;
  59725. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  59726. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  59727. func_cb_client.method = wolfTLSv1_3_client_method;
  59728. func_cb_server.method = wolfTLSv1_3_server_method;
  59729. func_cb_client.ctx_ready = test_tls13_pq_groups_ctx_ready;
  59730. func_cb_client.on_result = test_tls13_pq_groups_on_result;
  59731. func_cb_server.on_result = test_tls13_pq_groups_on_result;
  59732. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  59733. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  59734. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  59735. #endif
  59736. return EXPECT_RESULT();
  59737. }
  59738. static int test_dtls13_early_data(void)
  59739. {
  59740. EXPECT_DECLS;
  59741. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) && \
  59742. defined(WOLFSSL_EARLY_DATA) && defined(HAVE_SESSION_TICKET)
  59743. struct test_memio_ctx test_ctx;
  59744. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  59745. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  59746. WOLFSSL_SESSION *sess = NULL;
  59747. int written = 0;
  59748. int read = 0;
  59749. char msg[] = "This is early data";
  59750. char msg2[] = "This is client data";
  59751. char msg3[] = "This is server data";
  59752. char msg4[] = "This is server immediate data";
  59753. char msgBuf[50];
  59754. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59755. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59756. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  59757. /* Get a ticket so that we can do 0-RTT on the next connection */
  59758. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59759. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  59760. wolfSSL_free(ssl_c);
  59761. ssl_c = NULL;
  59762. wolfSSL_free(ssl_s);
  59763. ssl_s = NULL;
  59764. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59765. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59766. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  59767. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  59768. #ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME
  59769. ExpectIntEQ(wolfSSL_dtls13_no_hrr_on_resume(ssl_s, 1), WOLFSSL_SUCCESS);
  59770. #else
  59771. /* Let's test this but we generally don't recommend turning off the
  59772. * cookie exchange */
  59773. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  59774. #endif
  59775. /* Test 0-RTT data */
  59776. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  59777. &written), sizeof(msg));
  59778. ExpectIntEQ(written, sizeof(msg));
  59779. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  59780. &read), sizeof(msg));
  59781. ExpectIntEQ(read, sizeof(msg));
  59782. ExpectStrEQ(msg, msgBuf);
  59783. /* Test 0.5-RTT data */
  59784. ExpectIntEQ(wolfSSL_write(ssl_s, msg4, sizeof(msg4)), sizeof(msg4));
  59785. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  59786. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), APP_DATA_READY);
  59787. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  59788. ExpectStrEQ(msg4, msgBuf);
  59789. /* Complete handshake */
  59790. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  59791. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59792. /* Use wolfSSL_is_init_finished to check if handshake is complete. Normally
  59793. * a user would loop until it is true but here we control both sides so we
  59794. * just assert the expected value. wolfSSL_read_early_data does not provide
  59795. * handshake status to us with non-blocking IO and we can't use
  59796. * wolfSSL_accept as TLS layer may return ZERO_RETURN due to early data
  59797. * parsing logic. */
  59798. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  59799. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  59800. &read), -1);
  59801. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59802. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59803. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  59804. /* Test bi-directional write */
  59805. ExpectIntEQ(wolfSSL_write(ssl_c, msg2, sizeof(msg2)), sizeof(msg2));
  59806. ExpectIntEQ(wolfSSL_read(ssl_s, msgBuf, sizeof(msgBuf)), sizeof(msg2));
  59807. ExpectStrEQ(msg2, msgBuf);
  59808. ExpectIntEQ(wolfSSL_write(ssl_s, msg3, sizeof(msg3)), sizeof(msg3));
  59809. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg3));
  59810. ExpectStrEQ(msg3, msgBuf);
  59811. ExpectTrue(wolfSSL_session_reused(ssl_c));
  59812. ExpectTrue(wolfSSL_session_reused(ssl_s));
  59813. #endif
  59814. return EXPECT_RESULT();
  59815. }
  59816. /*----------------------------------------------------------------------------*
  59817. | Main
  59818. *----------------------------------------------------------------------------*/
  59819. typedef int (*TEST_FUNC)(void);
  59820. typedef struct {
  59821. const char *name;
  59822. TEST_FUNC func;
  59823. byte run:1;
  59824. byte fail:1;
  59825. } TEST_CASE;
  59826. #define TEST_DECL(func) { #func, func, 0, 0 }
  59827. int testAll = 1;
  59828. TEST_CASE testCases[] = {
  59829. TEST_DECL(test_fileAccess),
  59830. /*********************************
  59831. * wolfcrypt
  59832. *********************************/
  59833. TEST_DECL(test_ForceZero),
  59834. TEST_DECL(test_wolfCrypt_Init),
  59835. /* Locking with Compat Mutex */
  59836. TEST_DECL(test_wc_SetMutexCb),
  59837. TEST_DECL(test_wc_LockMutex_ex),
  59838. /* Digests */
  59839. TEST_DECL(test_wc_InitMd5),
  59840. TEST_DECL(test_wc_Md5Update),
  59841. TEST_DECL(test_wc_Md5Final),
  59842. TEST_DECL(test_wc_InitSha),
  59843. TEST_DECL(test_wc_ShaUpdate),
  59844. TEST_DECL(test_wc_ShaFinal),
  59845. TEST_DECL(test_wc_InitSha256),
  59846. TEST_DECL(test_wc_Sha256Update),
  59847. TEST_DECL(test_wc_Sha256Final),
  59848. TEST_DECL(test_wc_Sha256FinalRaw),
  59849. TEST_DECL(test_wc_Sha256GetFlags),
  59850. TEST_DECL(test_wc_Sha256Free),
  59851. TEST_DECL(test_wc_Sha256GetHash),
  59852. TEST_DECL(test_wc_Sha256Copy),
  59853. TEST_DECL(test_wc_InitSha224),
  59854. TEST_DECL(test_wc_Sha224Update),
  59855. TEST_DECL(test_wc_Sha224Final),
  59856. TEST_DECL(test_wc_Sha224SetFlags),
  59857. TEST_DECL(test_wc_Sha224GetFlags),
  59858. TEST_DECL(test_wc_Sha224Free),
  59859. TEST_DECL(test_wc_Sha224GetHash),
  59860. TEST_DECL(test_wc_Sha224Copy),
  59861. TEST_DECL(test_wc_InitSha512),
  59862. TEST_DECL(test_wc_Sha512Update),
  59863. TEST_DECL(test_wc_Sha512Final),
  59864. TEST_DECL(test_wc_Sha512GetFlags),
  59865. TEST_DECL(test_wc_Sha512FinalRaw),
  59866. TEST_DECL(test_wc_Sha512Free),
  59867. TEST_DECL(test_wc_Sha512GetHash),
  59868. TEST_DECL(test_wc_Sha512Copy),
  59869. TEST_DECL(test_wc_InitSha512_224),
  59870. TEST_DECL(test_wc_Sha512_224Update),
  59871. TEST_DECL(test_wc_Sha512_224Final),
  59872. TEST_DECL(test_wc_Sha512_224GetFlags),
  59873. TEST_DECL(test_wc_Sha512_224FinalRaw),
  59874. TEST_DECL(test_wc_Sha512_224Free),
  59875. TEST_DECL(test_wc_Sha512_224GetHash),
  59876. TEST_DECL(test_wc_Sha512_224Copy),
  59877. TEST_DECL(test_wc_InitSha512_256),
  59878. TEST_DECL(test_wc_Sha512_256Update),
  59879. TEST_DECL(test_wc_Sha512_256Final),
  59880. TEST_DECL(test_wc_Sha512_256GetFlags),
  59881. TEST_DECL(test_wc_Sha512_256FinalRaw),
  59882. TEST_DECL(test_wc_Sha512_256Free),
  59883. TEST_DECL(test_wc_Sha512_256GetHash),
  59884. TEST_DECL(test_wc_Sha512_256Copy),
  59885. TEST_DECL(test_wc_InitSha384),
  59886. TEST_DECL(test_wc_Sha384Update),
  59887. TEST_DECL(test_wc_Sha384Final),
  59888. TEST_DECL(test_wc_Sha384GetFlags),
  59889. TEST_DECL(test_wc_Sha384FinalRaw),
  59890. TEST_DECL(test_wc_Sha384Free),
  59891. TEST_DECL(test_wc_Sha384GetHash),
  59892. TEST_DECL(test_wc_Sha384Copy),
  59893. TEST_DECL(test_wc_InitBlake2b),
  59894. TEST_DECL(test_wc_InitBlake2b_WithKey),
  59895. TEST_DECL(test_wc_InitBlake2s_WithKey),
  59896. TEST_DECL(test_wc_InitRipeMd),
  59897. TEST_DECL(test_wc_RipeMdUpdate),
  59898. TEST_DECL(test_wc_RipeMdFinal),
  59899. TEST_DECL(test_wc_InitSha3),
  59900. TEST_DECL(testing_wc_Sha3_Update),
  59901. TEST_DECL(test_wc_Sha3_224_Final),
  59902. TEST_DECL(test_wc_Sha3_256_Final),
  59903. TEST_DECL(test_wc_Sha3_384_Final),
  59904. TEST_DECL(test_wc_Sha3_512_Final),
  59905. TEST_DECL(test_wc_Sha3_224_Copy),
  59906. TEST_DECL(test_wc_Sha3_256_Copy),
  59907. TEST_DECL(test_wc_Sha3_384_Copy),
  59908. TEST_DECL(test_wc_Sha3_512_Copy),
  59909. TEST_DECL(test_wc_Sha3_GetFlags),
  59910. TEST_DECL(test_wc_InitShake256),
  59911. TEST_DECL(testing_wc_Shake256_Update),
  59912. TEST_DECL(test_wc_Shake256_Final),
  59913. TEST_DECL(test_wc_Shake256_Copy),
  59914. TEST_DECL(test_wc_Shake256Hash),
  59915. /* SM3 Digest */
  59916. TEST_DECL(test_wc_InitSm3Free),
  59917. TEST_DECL(test_wc_Sm3UpdateFinal),
  59918. TEST_DECL(test_wc_Sm3GetHash),
  59919. TEST_DECL(test_wc_Sm3Copy),
  59920. TEST_DECL(test_wc_Sm3FinalRaw),
  59921. TEST_DECL(test_wc_Sm3GetSetFlags),
  59922. TEST_DECL(test_wc_Sm3Hash),
  59923. TEST_DECL(test_wc_HashInit),
  59924. TEST_DECL(test_wc_HashSetFlags),
  59925. TEST_DECL(test_wc_HashGetFlags),
  59926. /* HMAC */
  59927. TEST_DECL(test_wc_Md5HmacSetKey),
  59928. TEST_DECL(test_wc_Md5HmacUpdate),
  59929. TEST_DECL(test_wc_Md5HmacFinal),
  59930. TEST_DECL(test_wc_ShaHmacSetKey),
  59931. TEST_DECL(test_wc_ShaHmacUpdate),
  59932. TEST_DECL(test_wc_ShaHmacFinal),
  59933. TEST_DECL(test_wc_Sha224HmacSetKey),
  59934. TEST_DECL(test_wc_Sha224HmacUpdate),
  59935. TEST_DECL(test_wc_Sha224HmacFinal),
  59936. TEST_DECL(test_wc_Sha256HmacSetKey),
  59937. TEST_DECL(test_wc_Sha256HmacUpdate),
  59938. TEST_DECL(test_wc_Sha256HmacFinal),
  59939. TEST_DECL(test_wc_Sha384HmacSetKey),
  59940. TEST_DECL(test_wc_Sha384HmacUpdate),
  59941. TEST_DECL(test_wc_Sha384HmacFinal),
  59942. /* CMAC */
  59943. TEST_DECL(test_wc_InitCmac),
  59944. TEST_DECL(test_wc_CmacUpdate),
  59945. TEST_DECL(test_wc_CmacFinal),
  59946. TEST_DECL(test_wc_AesCmacGenerate),
  59947. /* Cipher */
  59948. TEST_DECL(test_wc_AesGcmStream),
  59949. TEST_DECL(test_wc_Des3_SetIV),
  59950. TEST_DECL(test_wc_Des3_SetKey),
  59951. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  59952. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  59953. TEST_DECL(test_wc_Des3_EcbEncrypt),
  59954. TEST_DECL(test_wc_Chacha_SetKey),
  59955. TEST_DECL(test_wc_Chacha_Process),
  59956. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  59957. TEST_DECL(test_wc_Poly1305SetKey),
  59958. TEST_DECL(test_wc_CamelliaSetKey),
  59959. TEST_DECL(test_wc_CamelliaSetIV),
  59960. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  59961. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  59962. TEST_DECL(test_wc_Arc4SetKey),
  59963. TEST_DECL(test_wc_Arc4Process),
  59964. TEST_DECL(test_wc_Rc2SetKey),
  59965. TEST_DECL(test_wc_Rc2SetIV),
  59966. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  59967. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  59968. /* AES cipher and GMAC. */
  59969. TEST_DECL(test_wc_AesSetKey),
  59970. TEST_DECL(test_wc_AesSetIV),
  59971. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  59972. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  59973. TEST_DECL(test_wc_AesGcmSetKey),
  59974. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  59975. TEST_DECL(test_wc_AesGcmMixedEncDecLongIV),
  59976. TEST_DECL(test_wc_GmacSetKey),
  59977. TEST_DECL(test_wc_GmacUpdate),
  59978. TEST_DECL(test_wc_AesCcmSetKey),
  59979. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  59980. #if defined(WOLFSSL_AES_EAX) && \
  59981. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  59982. TEST_DECL(test_wc_AesEaxVectors),
  59983. TEST_DECL(test_wc_AesEaxEncryptAuth),
  59984. TEST_DECL(test_wc_AesEaxDecryptAuth),
  59985. #endif /* WOLFSSL_AES_EAX */
  59986. /* SM4 cipher */
  59987. TEST_DECL(test_wc_Sm4),
  59988. TEST_DECL(test_wc_Sm4Ecb),
  59989. TEST_DECL(test_wc_Sm4Cbc),
  59990. TEST_DECL(test_wc_Sm4Ctr),
  59991. TEST_DECL(test_wc_Sm4Gcm),
  59992. TEST_DECL(test_wc_Sm4Ccm),
  59993. /* RNG tests */
  59994. #ifdef HAVE_HASHDRBG
  59995. #ifdef TEST_RESEED_INTERVAL
  59996. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  59997. #endif
  59998. TEST_DECL(test_wc_RNG_GenerateBlock),
  59999. #endif
  60000. TEST_DECL(test_get_rand_digit),
  60001. TEST_DECL(test_wc_InitRngNonce),
  60002. TEST_DECL(test_wc_InitRngNonce_ex),
  60003. /* MP API tests */
  60004. TEST_DECL(test_get_digit_count),
  60005. TEST_DECL(test_mp_cond_copy),
  60006. TEST_DECL(test_mp_rand),
  60007. TEST_DECL(test_get_digit),
  60008. TEST_DECL(test_wc_export_int),
  60009. /* RSA */
  60010. TEST_DECL(test_wc_InitRsaKey),
  60011. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  60012. TEST_DECL(test_wc_RsaPublicKeyDecode),
  60013. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  60014. TEST_DECL(test_wc_MakeRsaKey),
  60015. TEST_DECL(test_wc_CheckProbablePrime),
  60016. TEST_DECL(test_wc_RsaPSS_Verify),
  60017. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  60018. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  60019. TEST_DECL(test_wc_RsaKeyToDer),
  60020. TEST_DECL(test_wc_RsaKeyToPublicDer),
  60021. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  60022. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  60023. TEST_DECL(test_wc_RsaEncryptSize),
  60024. TEST_DECL(test_wc_RsaSSL_SignVerify),
  60025. TEST_DECL(test_wc_RsaFlattenPublicKey),
  60026. TEST_DECL(test_RsaDecryptBoundsCheck),
  60027. /* DSA */
  60028. TEST_DECL(test_wc_InitDsaKey),
  60029. TEST_DECL(test_wc_DsaSignVerify),
  60030. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  60031. TEST_DECL(test_wc_MakeDsaKey),
  60032. TEST_DECL(test_wc_DsaKeyToDer),
  60033. TEST_DECL(test_wc_DsaKeyToPublicDer),
  60034. TEST_DECL(test_wc_DsaImportParamsRaw),
  60035. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  60036. TEST_DECL(test_wc_DsaExportParamsRaw),
  60037. TEST_DECL(test_wc_DsaExportKeyRaw),
  60038. /* DH */
  60039. TEST_DECL(test_wc_DhPublicKeyDecode),
  60040. /* wolfCrypt ECC tests */
  60041. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  60042. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  60043. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  60044. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  60045. !defined(HAVE_SELFTEST) && \
  60046. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  60047. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  60048. #endif
  60049. TEST_DECL(test_wc_ecc_make_key),
  60050. TEST_DECL(test_wc_ecc_init),
  60051. TEST_DECL(test_wc_ecc_check_key),
  60052. TEST_DECL(test_wc_ecc_get_generator),
  60053. TEST_DECL(test_wc_ecc_size),
  60054. TEST_DECL(test_wc_ecc_params),
  60055. TEST_DECL(test_wc_ecc_signVerify_hash),
  60056. TEST_DECL(test_wc_ecc_shared_secret),
  60057. TEST_DECL(test_wc_ecc_export_x963),
  60058. TEST_DECL(test_wc_ecc_export_x963_ex),
  60059. TEST_DECL(test_wc_ecc_import_x963),
  60060. TEST_DECL(test_wc_ecc_import_private_key),
  60061. TEST_DECL(test_wc_ecc_export_private_only),
  60062. TEST_DECL(test_wc_ecc_rs_to_sig),
  60063. TEST_DECL(test_wc_ecc_import_raw),
  60064. TEST_DECL(test_wc_ecc_import_unsigned),
  60065. TEST_DECL(test_wc_ecc_sig_size),
  60066. TEST_DECL(test_wc_ecc_ctx_new),
  60067. TEST_DECL(test_wc_ecc_ctx_reset),
  60068. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  60069. TEST_DECL(test_wc_ecc_ctx_set_info),
  60070. TEST_DECL(test_wc_ecc_encryptDecrypt),
  60071. TEST_DECL(test_wc_ecc_del_point),
  60072. TEST_DECL(test_wc_ecc_pointFns),
  60073. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  60074. TEST_DECL(test_wc_ecc_verify_hash_ex),
  60075. TEST_DECL(test_wc_ecc_mulmod),
  60076. TEST_DECL(test_wc_ecc_is_valid_idx),
  60077. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  60078. TEST_DECL(test_wc_ecc_sig_size_calc),
  60079. TEST_DECL(test_wc_EccPrivateKeyToDer),
  60080. /* SM2 elliptic curve */
  60081. TEST_DECL(test_wc_ecc_sm2_make_key),
  60082. TEST_DECL(test_wc_ecc_sm2_shared_secret),
  60083. TEST_DECL(test_wc_ecc_sm2_create_digest),
  60084. TEST_DECL(test_wc_ecc_sm2_verify_hash_ex),
  60085. TEST_DECL(test_wc_ecc_sm2_verify_hash),
  60086. TEST_DECL(test_wc_ecc_sm2_sign_hash_ex),
  60087. TEST_DECL(test_wc_ecc_sm2_sign_hash),
  60088. /* Curve25519 */
  60089. TEST_DECL(test_wc_curve25519_init),
  60090. TEST_DECL(test_wc_curve25519_size),
  60091. TEST_DECL(test_wc_curve25519_export_key_raw),
  60092. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  60093. TEST_DECL(test_wc_curve25519_make_key),
  60094. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  60095. TEST_DECL(test_wc_curve25519_make_pub),
  60096. TEST_DECL(test_wc_curve25519_export_public_ex),
  60097. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  60098. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  60099. TEST_DECL(test_wc_curve25519_import_private),
  60100. /* ED25519 */
  60101. TEST_DECL(test_wc_ed25519_make_key),
  60102. TEST_DECL(test_wc_ed25519_init),
  60103. TEST_DECL(test_wc_ed25519_sign_msg),
  60104. TEST_DECL(test_wc_ed25519_import_public),
  60105. TEST_DECL(test_wc_ed25519_import_private_key),
  60106. TEST_DECL(test_wc_ed25519_export),
  60107. TEST_DECL(test_wc_ed25519_size),
  60108. TEST_DECL(test_wc_ed25519_exportKey),
  60109. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  60110. TEST_DECL(test_wc_Ed25519KeyToDer),
  60111. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  60112. /* Curve448 */
  60113. TEST_DECL(test_wc_curve448_make_key),
  60114. TEST_DECL(test_wc_curve448_shared_secret_ex),
  60115. TEST_DECL(test_wc_curve448_export_public_ex),
  60116. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  60117. TEST_DECL(test_wc_curve448_export_key_raw),
  60118. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  60119. TEST_DECL(test_wc_curve448_import_private),
  60120. TEST_DECL(test_wc_curve448_init),
  60121. TEST_DECL(test_wc_curve448_size),
  60122. /* Ed448 */
  60123. TEST_DECL(test_wc_ed448_make_key),
  60124. TEST_DECL(test_wc_ed448_init),
  60125. TEST_DECL(test_wc_ed448_sign_msg),
  60126. TEST_DECL(test_wc_ed448_import_public),
  60127. TEST_DECL(test_wc_ed448_import_private_key),
  60128. TEST_DECL(test_wc_ed448_export),
  60129. TEST_DECL(test_wc_ed448_size),
  60130. TEST_DECL(test_wc_ed448_exportKey),
  60131. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  60132. TEST_DECL(test_wc_Ed448KeyToDer),
  60133. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  60134. /* Signature API */
  60135. TEST_DECL(test_wc_SignatureGetSize_ecc),
  60136. TEST_DECL(test_wc_SignatureGetSize_rsa),
  60137. /* PEM and DER APIs. */
  60138. TEST_DECL(test_wc_PemToDer),
  60139. TEST_DECL(test_wc_AllocDer),
  60140. TEST_DECL(test_wc_CertPemToDer),
  60141. TEST_DECL(test_wc_KeyPemToDer),
  60142. TEST_DECL(test_wc_PubKeyPemToDer),
  60143. TEST_DECL(test_wc_PemPubKeyToDer),
  60144. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  60145. TEST_DECL(test_wc_CheckCertSigPubKey),
  60146. /* wolfCrypt ASN tests */
  60147. TEST_DECL(test_ToTraditional),
  60148. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  60149. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  60150. /* Certificate */
  60151. TEST_DECL(test_wc_SetSubjectRaw),
  60152. TEST_DECL(test_wc_GetSubjectRaw),
  60153. TEST_DECL(test_wc_SetIssuerRaw),
  60154. TEST_DECL(test_wc_SetIssueBuffer),
  60155. TEST_DECL(test_wc_SetSubjectKeyId),
  60156. TEST_DECL(test_wc_SetSubject),
  60157. TEST_DECL(test_CheckCertSignature),
  60158. TEST_DECL(test_wc_ParseCert),
  60159. TEST_DECL(test_wc_ParseCert_Error),
  60160. TEST_DECL(test_MakeCertWithPathLen),
  60161. TEST_DECL(test_MakeCertWithCaFalse),
  60162. TEST_DECL(test_wc_SetKeyUsage),
  60163. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  60164. TEST_DECL(test_wc_SetSubjectBuffer),
  60165. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  60166. /* wolfcrypt PKCS#7 */
  60167. TEST_DECL(test_wc_PKCS7_New),
  60168. TEST_DECL(test_wc_PKCS7_Init),
  60169. TEST_DECL(test_wc_PKCS7_InitWithCert),
  60170. TEST_DECL(test_wc_PKCS7_EncodeData),
  60171. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  60172. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  60173. TEST_DECL(test_wc_PKCS7_VerifySignedData_RSA),
  60174. TEST_DECL(test_wc_PKCS7_VerifySignedData_ECC),
  60175. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  60176. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  60177. TEST_DECL(test_wc_PKCS7_Degenerate),
  60178. TEST_DECL(test_wc_PKCS7_BER),
  60179. TEST_DECL(test_wc_PKCS7_signed_enveloped),
  60180. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  60181. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  60182. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  60183. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  60184. /* wolfCrypt PKCS#12 */
  60185. TEST_DECL(test_wc_i2d_PKCS12),
  60186. /*
  60187. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  60188. * avoid memory leaks.
  60189. */
  60190. TEST_DECL(test_wolfCrypt_Cleanup),
  60191. TEST_DECL(test_wolfSSL_Init),
  60192. /*********************************
  60193. * OpenSSL compatibility API tests
  60194. *********************************/
  60195. /* If at some point a stub get implemented this test should fail indicating
  60196. * a need to implement a new test case
  60197. */
  60198. TEST_DECL(test_stubs_are_stubs),
  60199. /* ASN.1 compatibility API tests */
  60200. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  60201. TEST_DECL(test_wolfSSL_ASN1_INTEGER),
  60202. TEST_DECL(test_wolfSSL_ASN1_INTEGER_cmp),
  60203. TEST_DECL(test_wolfSSL_ASN1_INTEGER_BN),
  60204. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  60205. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  60206. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  60207. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  60208. TEST_DECL(test_wolfSSL_ASN1_OBJECT),
  60209. TEST_DECL(test_wolfSSL_ASN1_get_object),
  60210. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  60211. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  60212. TEST_DECL(test_wolfSSL_sk_ASN1_OBJECT),
  60213. TEST_DECL(test_wolfSSL_ASN1_STRING),
  60214. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  60215. TEST_DECL(test_wolfSSL_i2s_ASN1_STRING),
  60216. TEST_DECL(test_wolfSSL_ASN1_STRING_canon),
  60217. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  60218. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  60219. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  60220. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  60221. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_print),
  60222. TEST_DECL(test_wolfSSL_ASN1_TIME),
  60223. TEST_DECL(test_wolfSSL_ASN1_TIME_to_string),
  60224. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  60225. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  60226. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  60227. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  60228. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  60229. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  60230. TEST_DECL(test_wolfSSL_ASN1_TYPE),
  60231. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  60232. TEST_DECL(test_wolfSSL_lhash),
  60233. TEST_DECL(test_wolfSSL_certs),
  60234. TEST_DECL(test_wolfSSL_private_keys),
  60235. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  60236. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  60237. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  60238. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  60239. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  60240. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  60241. #ifndef NO_BIO
  60242. TEST_DECL(test_wolfSSL_BIO),
  60243. TEST_DECL(test_wolfSSL_PEM_read_bio),
  60244. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  60245. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  60246. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  60247. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  60248. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  60249. #endif
  60250. /* EVP API testing */
  60251. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  60252. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  60253. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  60254. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  60255. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  60256. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  60257. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  60258. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  60259. TEST_DECL(test_wolfSSL_EVP_shake128),
  60260. TEST_DECL(test_wolfSSL_EVP_shake256),
  60261. TEST_DECL(test_wolfSSL_EVP_sm3),
  60262. TEST_DECL(test_EVP_blake2),
  60263. #ifdef OPENSSL_ALL
  60264. TEST_DECL(test_wolfSSL_EVP_md4),
  60265. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  60266. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  60267. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  60268. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  60269. #endif
  60270. TEST_DECL(test_EVP_MD_do_all),
  60271. TEST_DECL(test_wolfSSL_EVP_MD_size),
  60272. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  60273. TEST_DECL(test_wolfSSL_EVP_Digest),
  60274. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  60275. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  60276. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  60277. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  60278. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  60279. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  60280. TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
  60281. TEST_DECL(test_wolfssl_EVP_aes_ccm),
  60282. TEST_DECL(test_wolfssl_EVP_aes_ccm_zeroLen),
  60283. TEST_DECL(test_wolfssl_EVP_chacha20),
  60284. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  60285. TEST_DECL(test_wolfssl_EVP_sm4_ecb),
  60286. TEST_DECL(test_wolfssl_EVP_sm4_cbc),
  60287. TEST_DECL(test_wolfssl_EVP_sm4_ctr),
  60288. TEST_DECL(test_wolfssl_EVP_sm4_gcm_zeroLen),
  60289. TEST_DECL(test_wolfssl_EVP_sm4_gcm),
  60290. TEST_DECL(test_wolfssl_EVP_sm4_ccm_zeroLen),
  60291. TEST_DECL(test_wolfssl_EVP_sm4_ccm),
  60292. #ifdef OPENSSL_ALL
  60293. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  60294. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  60295. TEST_DECL(test_wolfSSL_EVP_aes_256_ccm),
  60296. TEST_DECL(test_wolfSSL_EVP_aes_192_ccm),
  60297. TEST_DECL(test_wolfSSL_EVP_aes_128_ccm),
  60298. TEST_DECL(test_wolfSSL_EVP_rc4),
  60299. TEST_DECL(test_wolfSSL_EVP_enc_null),
  60300. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  60301. TEST_DECL(test_wolfSSL_EVP_mdc2),
  60302. TEST_DECL(test_evp_cipher_aes_gcm),
  60303. #endif
  60304. TEST_DECL(test_wolfssl_EVP_aria_gcm),
  60305. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  60306. #ifdef OPENSSL_EXTRA
  60307. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  60308. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  60309. #endif
  60310. #ifdef OPENSSL_ALL
  60311. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  60312. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  60313. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  60314. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  60315. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  60316. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  60317. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  60318. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  60319. #endif
  60320. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  60321. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  60322. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  60323. TEST_DECL(test_wolfSSL_EVP_PKEY_up_ref),
  60324. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  60325. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  60326. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  60327. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey_ecc),
  60328. #ifndef NO_BIO
  60329. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  60330. #endif
  60331. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  60332. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  60333. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  60334. #ifndef NO_BIO
  60335. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  60336. #endif /* !NO_BIO */
  60337. #endif
  60338. #ifdef OPENSSL_ALL
  60339. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  60340. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  60341. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  60342. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  60343. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  60344. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  60345. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  60346. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  60347. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  60348. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  60349. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  60350. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  60351. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  60352. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  60353. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  60354. #endif
  60355. TEST_DECL(test_EVP_PKEY_rsa),
  60356. TEST_DECL(test_EVP_PKEY_ec),
  60357. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  60358. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_rsa),
  60359. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_dsa),
  60360. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_ec),
  60361. TEST_DECL(test_EVP_PKEY_cmp),
  60362. #ifdef OPENSSL_ALL
  60363. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  60364. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  60365. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  60366. #endif
  60367. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  60368. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  60369. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  60370. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  60371. #endif
  60372. #ifndef NO_BIO
  60373. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  60374. TEST_DECL(test_wolfSSL_GetLoggingCb),
  60375. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  60376. TEST_DECL(test_wc_ERR_remove_state),
  60377. TEST_DECL(test_wc_ERR_print_errors_fp),
  60378. #endif
  60379. TEST_DECL(test_wolfSSL_configure_args),
  60380. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  60381. TEST_DECL(test_wolfSSL_set1_curves_list),
  60382. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  60383. TEST_DECL(test_wolfSSL_OtherName),
  60384. TEST_DECL(test_wolfSSL_FPKI),
  60385. TEST_DECL(test_wolfSSL_URI),
  60386. TEST_DECL(test_wolfSSL_TBS),
  60387. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  60388. TEST_DECL(test_X509_STORE_untrusted),
  60389. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  60390. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  60391. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  60392. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  60393. TEST_DECL(test_wolfSSL_X509_Name_canon),
  60394. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  60395. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  60396. TEST_DECL(test_wolfSSL_X509_NID),
  60397. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  60398. TEST_DECL(test_wolfSSL_get0_param),
  60399. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  60400. TEST_DECL(test_wolfSSL_set1_host),
  60401. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  60402. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  60403. TEST_DECL(test_wolfSSL_X509_STORE),
  60404. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  60405. TEST_DECL(test_X509_STORE_get0_objects),
  60406. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  60407. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  60408. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  60409. TEST_DECL(test_wolfSSL_X509_cmp_time),
  60410. TEST_DECL(test_wolfSSL_X509_time_adj),
  60411. /* X509 tests */
  60412. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  60413. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  60414. TEST_DECL(test_wolfSSL_X509_check_host),
  60415. TEST_DECL(test_wolfSSL_X509_check_email),
  60416. TEST_DECL(test_wolfSSL_X509_check_private_key),
  60417. TEST_DECL(test_wolfSSL_X509),
  60418. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  60419. TEST_DECL(test_wolfSSL_X509_sign),
  60420. TEST_DECL(test_wolfSSL_X509_sign2),
  60421. TEST_DECL(test_wolfSSL_X509_verify),
  60422. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  60423. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  60424. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  60425. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  60426. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  60427. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  60428. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  60429. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  60430. TEST_DECL(test_wolfSSL_X509_NAME),
  60431. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  60432. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  60433. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  60434. TEST_DECL(test_wolfSSL_X509_set_name),
  60435. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  60436. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  60437. TEST_DECL(test_wolfSSL_X509_set_version),
  60438. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  60439. TEST_DECL(test_wolfSSL_X509_CRL),
  60440. TEST_DECL(test_wolfSSL_i2d_X509),
  60441. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  60442. TEST_DECL(test_wolfSSL_PEM_read_X509),
  60443. TEST_DECL(test_wolfSSL_X509_check_ca),
  60444. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  60445. TEST_DECL(test_wolfSSL_make_cert),
  60446. #ifndef NO_BIO
  60447. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  60448. TEST_DECL(test_wolfSSL_X509_INFO),
  60449. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  60450. #endif
  60451. #ifdef OPENSSL_ALL
  60452. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  60453. #endif
  60454. TEST_DECL(test_wolfSSL_X509_CA_num),
  60455. TEST_DECL(test_wolfSSL_X509_get_version),
  60456. #ifndef NO_BIO
  60457. TEST_DECL(test_wolfSSL_X509_print),
  60458. TEST_DECL(test_wolfSSL_X509_CRL_print),
  60459. #endif
  60460. TEST_DECL(test_X509_get_signature_nid),
  60461. /* X509 extension testing. */
  60462. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  60463. TEST_DECL(test_wolfSSL_X509_get_ext),
  60464. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  60465. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  60466. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  60467. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  60468. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  60469. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  60470. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  60471. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  60472. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  60473. TEST_DECL(test_wolfSSL_X509V3_EXT),
  60474. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  60475. TEST_DECL(test_wolfSSL_X509_cmp),
  60476. TEST_DECL(test_GENERAL_NAME_set0_othername),
  60477. TEST_DECL(test_othername_and_SID_ext),
  60478. TEST_DECL(test_wolfSSL_dup_CA_list),
  60479. /* OpenSSL sk_X509 API test */
  60480. TEST_DECL(test_sk_X509),
  60481. /* OpenSSL sk_X509_CRL API test */
  60482. TEST_DECL(test_sk_X509_CRL),
  60483. /* OpenSSL X509 REQ API test */
  60484. TEST_DECL(test_X509_REQ),
  60485. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  60486. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  60487. TEST_DECL(test_X509_LOOKUP_add_dir),
  60488. /* RAND compatibility API */
  60489. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  60490. TEST_DECL(test_wolfSSL_RAND_bytes),
  60491. TEST_DECL(test_wolfSSL_RAND),
  60492. /* BN compatibility API */
  60493. TEST_DECL(test_wolfSSL_BN_CTX),
  60494. TEST_DECL(test_wolfSSL_BN),
  60495. TEST_DECL(test_wolfSSL_BN_init),
  60496. TEST_DECL(test_wolfSSL_BN_enc_dec),
  60497. TEST_DECL(test_wolfSSL_BN_word),
  60498. TEST_DECL(test_wolfSSL_BN_bits),
  60499. TEST_DECL(test_wolfSSL_BN_shift),
  60500. TEST_DECL(test_wolfSSL_BN_math),
  60501. TEST_DECL(test_wolfSSL_BN_math_mod),
  60502. TEST_DECL(test_wolfSSL_BN_math_other),
  60503. TEST_DECL(test_wolfSSL_BN_rand),
  60504. TEST_DECL(test_wolfSSL_BN_prime),
  60505. /* OpenSSL PKCS5 API test */
  60506. TEST_DECL(test_wolfSSL_PKCS5),
  60507. /* OpenSSL PKCS8 API test */
  60508. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  60509. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  60510. /* OpenSSL PKCS7 API test */
  60511. TEST_DECL(test_wolfssl_PKCS7),
  60512. TEST_DECL(test_wolfSSL_PKCS7_certs),
  60513. TEST_DECL(test_wolfSSL_PKCS7_sign),
  60514. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  60515. #ifndef NO_BIO
  60516. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  60517. #ifdef HAVE_SMIME
  60518. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  60519. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  60520. #endif /* HAVE_SMIME */
  60521. #endif /* !NO_BIO */
  60522. /* OpenSSL PKCS12 API test */
  60523. TEST_DECL(test_wolfSSL_PKCS12),
  60524. /* Can't memory test as callbacks use Assert. */
  60525. TEST_DECL(test_error_queue_per_thread),
  60526. TEST_DECL(test_wolfSSL_ERR_put_error),
  60527. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  60528. #ifndef NO_BIO
  60529. TEST_DECL(test_wolfSSL_ERR_print_errors),
  60530. #endif
  60531. TEST_DECL(test_OBJ_NAME_do_all),
  60532. TEST_DECL(test_wolfSSL_OBJ),
  60533. TEST_DECL(test_wolfSSL_OBJ_cmp),
  60534. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  60535. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  60536. #ifdef OPENSSL_ALL
  60537. TEST_DECL(test_wolfSSL_OBJ_ln),
  60538. TEST_DECL(test_wolfSSL_OBJ_sn),
  60539. #endif
  60540. #ifndef NO_BIO
  60541. TEST_DECL(test_wolfSSL_BIO_gets),
  60542. TEST_DECL(test_wolfSSL_BIO_puts),
  60543. TEST_DECL(test_wolfSSL_BIO_dump),
  60544. /* Can't memory test as server hangs. */
  60545. TEST_DECL(test_wolfSSL_BIO_should_retry),
  60546. TEST_DECL(test_wolfSSL_BIO_write),
  60547. TEST_DECL(test_wolfSSL_BIO_printf),
  60548. TEST_DECL(test_wolfSSL_BIO_f_md),
  60549. TEST_DECL(test_wolfSSL_BIO_up_ref),
  60550. TEST_DECL(test_wolfSSL_BIO_reset),
  60551. TEST_DECL(test_wolfSSL_BIO_get_len),
  60552. #endif
  60553. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  60554. TEST_DECL(test_wolfSSL_check_domain),
  60555. #endif
  60556. TEST_DECL(test_wolfSSL_cert_cb),
  60557. /* Can't memory test as tcp_connect aborts. */
  60558. TEST_DECL(test_wolfSSL_SESSION),
  60559. TEST_DECL(test_wolfSSL_SESSION_expire_downgrade),
  60560. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  60561. TEST_DECL(test_wolfSSL_ticket_keys),
  60562. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  60563. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  60564. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  60565. TEST_DECL(test_wolfSSL_verify_mode),
  60566. TEST_DECL(test_wolfSSL_verify_depth),
  60567. TEST_DECL(test_wolfSSL_verify_result),
  60568. TEST_DECL(test_wolfSSL_msg_callback),
  60569. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  60570. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  60571. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  60572. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  60573. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  60574. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  60575. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  60576. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  60577. TEST_DECL(test_wolfSSL_PEM_read),
  60578. TEST_DECL(test_wolfSSL_OpenSSL_version),
  60579. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  60580. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  60581. TEST_DECL(test_CONF_modules_xxx),
  60582. #ifdef OPENSSL_ALL
  60583. TEST_DECL(test_wolfSSL_TXT_DB),
  60584. TEST_DECL(test_wolfSSL_NCONF),
  60585. #endif
  60586. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  60587. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  60588. TEST_DECL(test_wolfSSL_SESSION_get_ex_new_index),
  60589. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  60590. TEST_DECL(test_CRYPTO_THREADID_xxx),
  60591. TEST_DECL(test_ENGINE_cleanup),
  60592. /* test the no op functions for compatibility */
  60593. TEST_DECL(test_no_op_functions),
  60594. /* OpenSSL error API tests */
  60595. TEST_DECL(test_ERR_load_crypto_strings),
  60596. #ifdef OPENSSL_ALL
  60597. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  60598. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  60599. TEST_DECL(test_wolfSSL_CTX_ctrl),
  60600. #endif /* OPENSSL_ALL */
  60601. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  60602. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  60603. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  60604. /*********************************
  60605. * Crypto API tests
  60606. *********************************/
  60607. TEST_DECL(test_wolfSSL_MD4),
  60608. TEST_DECL(test_wolfSSL_MD5),
  60609. TEST_DECL(test_wolfSSL_MD5_Transform),
  60610. TEST_DECL(test_wolfSSL_SHA),
  60611. TEST_DECL(test_wolfSSL_SHA_Transform),
  60612. TEST_DECL(test_wolfSSL_SHA224),
  60613. TEST_DECL(test_wolfSSL_SHA256),
  60614. TEST_DECL(test_wolfSSL_SHA256_Transform),
  60615. TEST_DECL(test_wolfSSL_SHA512_Transform),
  60616. TEST_DECL(test_wolfSSL_SHA512_224_Transform),
  60617. TEST_DECL(test_wolfSSL_SHA512_256_Transform),
  60618. TEST_DECL(test_wolfSSL_HMAC_CTX),
  60619. TEST_DECL(test_wolfSSL_HMAC),
  60620. TEST_DECL(test_wolfSSL_CMAC),
  60621. TEST_DECL(test_wolfSSL_DES),
  60622. TEST_DECL(test_wolfSSL_DES_ncbc),
  60623. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  60624. TEST_DECL(test_wolfSSL_DES_ede3_cbc_encrypt),
  60625. TEST_DECL(test_wolfSSL_AES_encrypt),
  60626. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  60627. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  60628. TEST_DECL(test_wolfSSL_AES_cfb128_encrypt),
  60629. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  60630. TEST_DECL(test_wolfSSL_RC4),
  60631. TEST_DECL(test_wolfSSL_RSA),
  60632. TEST_DECL(test_wolfSSL_RSA_DER),
  60633. TEST_DECL(test_wolfSSL_RSA_print),
  60634. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  60635. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  60636. TEST_DECL(test_wolfSSL_RSA_get0_key),
  60637. TEST_DECL(test_wolfSSL_RSA_meth),
  60638. TEST_DECL(test_wolfSSL_RSA_verify),
  60639. TEST_DECL(test_wolfSSL_RSA_sign),
  60640. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  60641. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  60642. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  60643. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  60644. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  60645. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  60646. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  60647. TEST_DECL(test_wolfSSL_RSA_ex_data),
  60648. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  60649. TEST_DECL(test_wolfSSL_RSA_To_Der),
  60650. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  60651. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  60652. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  60653. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  60654. TEST_DECL(test_wolfSSL_DH),
  60655. TEST_DECL(test_wolfSSL_DH_dup),
  60656. TEST_DECL(test_wolfSSL_DH_check),
  60657. TEST_DECL(test_wolfSSL_DH_prime),
  60658. TEST_DECL(test_wolfSSL_DH_1536_prime),
  60659. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  60660. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  60661. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  60662. TEST_DECL(test_wolfSSL_d2i_DHparams),
  60663. TEST_DECL(test_wolfSSL_DH_LoadDer),
  60664. TEST_DECL(test_wolfSSL_i2d_DHparams),
  60665. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  60666. TEST_DECL(test_wolfSSL_EC_GROUP),
  60667. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  60668. TEST_DECL(test_wolfSSL_EC_POINT),
  60669. TEST_DECL(test_wolfSSL_SPAKE),
  60670. TEST_DECL(test_wolfSSL_EC_KEY_generate),
  60671. TEST_DECL(test_EC_i2d),
  60672. TEST_DECL(test_wolfSSL_EC_curve),
  60673. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  60674. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  60675. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  60676. TEST_DECL(test_wolfSSL_EC_KEY_private_key),
  60677. TEST_DECL(test_wolfSSL_EC_KEY_public_key),
  60678. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  60679. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  60680. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  60681. TEST_DECL(test_ECDSA_size_sign),
  60682. TEST_DECL(test_ECDH_compute_key),
  60683. #endif
  60684. #ifdef OPENSSL_EXTRA
  60685. TEST_DECL(test_ED25519),
  60686. TEST_DECL(test_ED448),
  60687. #endif
  60688. TEST_DECL(test_DSA_do_sign_verify),
  60689. #ifdef OPENSSL_ALL
  60690. TEST_DECL(test_wolfSSL_DSA_generate_parameters),
  60691. TEST_DECL(test_wolfSSL_DSA_SIG),
  60692. #endif
  60693. TEST_DECL(test_openssl_generate_key_and_cert),
  60694. TEST_DECL(test_wolfSSL_FIPS_mode),
  60695. TEST_DECL(test_openssl_FIPS_drbg),
  60696. /*********************************
  60697. * CertManager API tests
  60698. *********************************/
  60699. TEST_DECL(test_wolfSSL_CertManagerAPI),
  60700. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  60701. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer_ex),
  60702. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  60703. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  60704. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  60705. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  60706. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  60707. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  60708. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  60709. TEST_DECL(test_wolfSSL_CertManagerCRL),
  60710. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  60711. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  60712. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  60713. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  60714. !defined(WOLFSSL_NO_CLIENT_AUTH))
  60715. TEST_DECL(test_various_pathlen_chains),
  60716. #endif
  60717. /*********************************
  60718. * SSL/TLS API tests
  60719. *********************************/
  60720. TEST_DECL(test_wolfSSL_Method_Allocators),
  60721. #ifndef NO_WOLFSSL_SERVER
  60722. TEST_DECL(test_wolfSSL_CTX_new),
  60723. #endif
  60724. TEST_DECL(test_server_wolfSSL_new),
  60725. TEST_DECL(test_client_wolfSSL_new),
  60726. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  60727. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  60728. TEST_DECL(test_for_double_Free),
  60729. #endif
  60730. TEST_DECL(test_wolfSSL_set_options),
  60731. #ifdef WOLFSSL_TLS13
  60732. /* TLS v1.3 API tests */
  60733. TEST_DECL(test_tls13_apis),
  60734. TEST_DECL(test_tls13_cipher_suites),
  60735. #endif
  60736. TEST_DECL(test_wolfSSL_tmp_dh),
  60737. TEST_DECL(test_wolfSSL_ctrl),
  60738. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  60739. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  60740. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  60741. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  60742. TEST_DECL(test_wolfSSL_set_SSL_CTX),
  60743. #endif
  60744. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  60745. TEST_DECL(test_wolfSSL_security_level),
  60746. TEST_DECL(test_wolfSSL_SSL_in_init),
  60747. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  60748. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  60749. TEST_DECL(test_CONF_CTX_FILE),
  60750. TEST_DECL(test_CONF_CTX_CMDLINE),
  60751. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  60752. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  60753. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  60754. /* Bad certificate signature tests */
  60755. TEST_DECL(test_EccSigFailure_cm),
  60756. TEST_DECL(test_RsaSigFailure_cm),
  60757. #endif /* NO_CERTS */
  60758. /* PKCS8 testing */
  60759. TEST_DECL(test_wolfSSL_no_password_cb),
  60760. TEST_DECL(test_wolfSSL_PKCS8),
  60761. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  60762. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  60763. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  60764. TEST_DECL(test_wolfSSL_get_finished),
  60765. /* Uses Assert in handshake callback. */
  60766. TEST_DECL(test_wolfSSL_CTX_add_session),
  60767. /* Large number of memory allocations. */
  60768. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls13),
  60769. /* Large number of memory allocations. */
  60770. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls13),
  60771. /* Large number of memory allocations. */
  60772. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls12),
  60773. /* Large number of memory allocations. */
  60774. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls12),
  60775. /* Large number of memory allocations. */
  60776. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls11),
  60777. /* Large number of memory allocations. */
  60778. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls1),
  60779. #endif
  60780. TEST_DECL(test_SSL_CIPHER_get_xxx),
  60781. TEST_DECL(test_wolfSSL_ERR_strings),
  60782. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  60783. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  60784. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  60785. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  60786. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  60787. /* Large number of memory allocations. */
  60788. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  60789. TEST_DECL(test_wolfSSL_CertRsaPss),
  60790. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  60791. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  60792. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  60793. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  60794. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  60795. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  60796. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  60797. TEST_DECL(test_multiple_crls_same_issuer),
  60798. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  60799. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  60800. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  60801. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  60802. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  60803. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  60804. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  60805. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  60806. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  60807. TEST_DECL(test_SetTmpEC_DHE_Sz),
  60808. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  60809. #ifdef WOLFSSL_DTLS
  60810. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  60811. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  60812. #endif
  60813. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  60814. /* Uses Assert in handshake callback. */
  60815. TEST_DECL(test_wolfSSL_dtls_plaintext),
  60816. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  60817. defined(HAVE_IO_TESTS_DEPENDENCIES)
  60818. TEST_DECL(test_wolfSSL_read_write),
  60819. /* Can't memory test as server hangs if client fails before second connect.
  60820. */
  60821. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  60822. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_1),
  60823. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_2),
  60824. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_3),
  60825. TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
  60826. /* Can't memory test as server hangs. */
  60827. TEST_DECL(test_wolfSSL_dtls_export),
  60828. /* Uses Assert in handshake callback. */
  60829. TEST_DECL(test_wolfSSL_tls_export),
  60830. #endif
  60831. TEST_DECL(test_wolfSSL_SetMinVersion),
  60832. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  60833. /* wolfSSL handshake APIs. */
  60834. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  60835. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  60836. TEST_DECL(test_wolfSSL_BUF),
  60837. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  60838. /* Can't memory test as server hangs. */
  60839. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  60840. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  60841. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  60842. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  60843. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  60844. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  60845. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  60846. /* Can't memory test as server hangs. */
  60847. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  60848. TEST_DECL(test_wolfSSL_Tls13_postauth),
  60849. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  60850. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  60851. TEST_DECL(test_wolfSSL_CTX_set_max_proto_version),
  60852. TEST_DECL(test_wolfSSL_THREADID_hash),
  60853. /* TLS extensions tests */
  60854. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  60855. #ifdef HAVE_SNI
  60856. TEST_DECL(test_wolfSSL_UseSNI_params),
  60857. /* Uses Assert in handshake callback. */
  60858. TEST_DECL(test_wolfSSL_UseSNI_connection),
  60859. TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
  60860. #endif /* HAVE_SNI */
  60861. #endif
  60862. TEST_DECL(test_wolfSSL_UseTrustedCA),
  60863. TEST_DECL(test_wolfSSL_UseMaxFragment),
  60864. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  60865. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  60866. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  60867. /* Uses Assert in handshake callback. */
  60868. TEST_DECL(test_wolfSSL_UseALPN_connection),
  60869. TEST_DECL(test_wolfSSL_UseALPN_params),
  60870. #endif
  60871. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  60872. /* Uses Assert in handshake callback. */
  60873. TEST_DECL(test_wolfSSL_set_alpn_protos),
  60874. #endif
  60875. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  60876. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  60877. TEST_DECL(test_wolfSSL_SCR_Reconnect),
  60878. TEST_DECL(test_tls_ext_duplicate),
  60879. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  60880. defined(HAVE_IO_TESTS_DEPENDENCIES)
  60881. TEST_DECL(test_wolfSSL_Tls13_ECH_params),
  60882. /* Uses Assert in handshake callback. */
  60883. TEST_DECL(test_wolfSSL_Tls13_ECH),
  60884. #endif
  60885. TEST_DECL(test_wolfSSL_X509_TLS_version_test_1),
  60886. TEST_DECL(test_wolfSSL_X509_TLS_version_test_2),
  60887. /* OCSP Stapling */
  60888. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  60889. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  60890. /* Multicast */
  60891. TEST_DECL(test_wolfSSL_mcast),
  60892. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  60893. TEST_DECL(test_wolfSSL_msgCb),
  60894. TEST_DECL(test_wolfSSL_either_side),
  60895. TEST_DECL(test_wolfSSL_DTLS_either_side),
  60896. /* Uses Assert in handshake callback. */
  60897. TEST_DECL(test_wolfSSL_dtls_fragments),
  60898. /* Uses Assert in handshake callback. */
  60899. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  60900. /* Uses Assert in handshake callback. */
  60901. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  60902. /* Uses Assert in handshake callback. */
  60903. TEST_DECL(test_wolfSSL_dtls_bad_record),
  60904. /* Uses Assert in handshake callback. */
  60905. TEST_DECL(test_wolfSSL_dtls_stateless),
  60906. TEST_DECL(test_generate_cookie),
  60907. #ifndef NO_BIO
  60908. /* Can't memory test as server hangs. */
  60909. TEST_DECL(test_wolfSSL_BIO_connect),
  60910. /* Can't memory test as server Asserts in thread. */
  60911. TEST_DECL(test_wolfSSL_BIO_accept),
  60912. TEST_DECL(test_wolfSSL_BIO_tls),
  60913. #endif
  60914. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  60915. TEST_DECL(test_DhCallbacks),
  60916. #endif
  60917. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  60918. TEST_DECL(test_export_keying_material),
  60919. #endif
  60920. /* Can't memory test as client/server Asserts in thread. */
  60921. TEST_DECL(test_ticket_and_psk_mixing),
  60922. /* Can't memory test as client/server Asserts in thread. */
  60923. TEST_DECL(test_prioritize_psk),
  60924. /* Can't memory test as client/server hangs. */
  60925. TEST_DECL(test_wc_CryptoCb),
  60926. /* Can't memory test as client/server hangs. */
  60927. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  60928. #if !defined(NO_FILESYSTEM) && \
  60929. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  60930. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  60931. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  60932. TEST_DECL(test_wolfSSL_dtls_stateless_resume),
  60933. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  60934. #ifdef HAVE_MAX_FRAGMENT
  60935. TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
  60936. #endif /* HAVE_MAX_FRAGMENT */
  60937. #ifndef NO_RSA
  60938. TEST_DECL(test_wolfSSL_dtls_stateless2),
  60939. #if !defined(NO_OLD_TLS)
  60940. TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
  60941. #endif /* !defined(NO_OLD_TLS) */
  60942. #endif /* ! NO_RSA */
  60943. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  60944. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
  60945. TEST_DECL(test_wolfSSL_CTX_set_ciphersuites),
  60946. TEST_DECL(test_wolfSSL_CRL_CERT_REVOKED_alert),
  60947. TEST_DECL(test_TLS_13_ticket_different_ciphers),
  60948. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  60949. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  60950. && defined(WOLFSSL_TLS13) && \
  60951. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  60952. TEST_DECL(test_ticket_nonce_malloc),
  60953. #endif
  60954. TEST_DECL(test_ticket_ret_create),
  60955. TEST_DECL(test_extra_alerts_wrong_cs),
  60956. TEST_DECL(test_extra_alerts_skip_hs),
  60957. TEST_DECL(test_extra_alerts_bad_psk),
  60958. /* Can't memory test as client/server Asserts. */
  60959. TEST_DECL(test_harden_no_secure_renegotiation),
  60960. TEST_DECL(test_override_alt_cert_chain),
  60961. TEST_DECL(test_rpk_set_xxx_cert_type),
  60962. TEST_DECL(test_tls13_rpk_handshake),
  60963. TEST_DECL(test_dtls13_bad_epoch_ch),
  60964. TEST_DECL(test_short_session_id),
  60965. TEST_DECL(test_wolfSSL_dtls13_null_cipher),
  60966. /* Can't memory test as client/server hangs. */
  60967. TEST_DECL(test_dtls_msg_from_other_peer),
  60968. TEST_DECL(test_dtls_ipv6_check),
  60969. TEST_DECL(test_wolfSSL_SCR_after_resumption),
  60970. TEST_DECL(test_dtls_no_extensions),
  60971. TEST_DECL(test_TLSX_CA_NAMES_bad_extension),
  60972. TEST_DECL(test_dtls_1_0_hvr_downgrade),
  60973. TEST_DECL(test_session_ticket_no_id),
  60974. TEST_DECL(test_session_ticket_hs_update),
  60975. TEST_DECL(test_dtls_downgrade_scr_server),
  60976. TEST_DECL(test_dtls_downgrade_scr),
  60977. TEST_DECL(test_dtls_client_hello_timeout_downgrade),
  60978. TEST_DECL(test_dtls_client_hello_timeout),
  60979. TEST_DECL(test_dtls_dropped_ccs),
  60980. TEST_DECL(test_certreq_sighash_algos),
  60981. TEST_DECL(test_revoked_loaded_int_cert),
  60982. TEST_DECL(test_dtls_frag_ch),
  60983. TEST_DECL(test_dtls13_frag_ch_pq),
  60984. TEST_DECL(test_dtls_empty_keyshare_with_cookie),
  60985. TEST_DECL(test_tls13_pq_groups),
  60986. TEST_DECL(test_dtls13_early_data),
  60987. /* This test needs to stay at the end to clean up any caches allocated. */
  60988. TEST_DECL(test_wolfSSL_Cleanup)
  60989. };
  60990. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  60991. static void TestSetup(void)
  60992. {
  60993. /* Stub, for now. Add common test setup code here. */
  60994. }
  60995. static void TestCleanup(void)
  60996. {
  60997. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  60998. /* Clear any errors added to the error queue during the test run. */
  60999. wolfSSL_ERR_clear_error();
  61000. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  61001. }
  61002. /* Print out all API test cases with numeric identifier.
  61003. */
  61004. void ApiTest_PrintTestCases(void)
  61005. {
  61006. int i;
  61007. printf("All Test Cases:\n");
  61008. for (i = 0; i < TEST_CASE_CNT; i++) {
  61009. printf("%3d: %s\n", i + 1, testCases[i].name);
  61010. }
  61011. }
  61012. /* Add test case with index to the list to run.
  61013. *
  61014. * @param [in] idx Index of test case to run starting at 1.
  61015. * @return 0 on success.
  61016. * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
  61017. */
  61018. int ApiTest_RunIdx(int idx)
  61019. {
  61020. if (idx < 1 || idx > TEST_CASE_CNT) {
  61021. printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
  61022. return BAD_FUNC_ARG;
  61023. }
  61024. testAll = 0;
  61025. testCases[idx-1].run = 1;
  61026. return 0;
  61027. }
  61028. /* Add test case with name to the list to run.
  61029. *
  61030. * @param [in] name Name of test case to run.
  61031. * @return 0 on success.
  61032. * @return BAD_FUNC_ARG when name is not a known test case name.
  61033. */
  61034. int ApiTest_RunName(char* name)
  61035. {
  61036. int i;
  61037. for (i = 0; i < TEST_CASE_CNT; i++) {
  61038. if (XSTRCMP(testCases[i].name, name) == 0) {
  61039. testAll = 0;
  61040. testCases[i].run = 1;
  61041. return 0;
  61042. }
  61043. }
  61044. printf("Test case name not found: %s\n", name);
  61045. printf("Use --list to see all test case names.\n");
  61046. return BAD_FUNC_ARG;
  61047. }
  61048. /* Converts the result code to a string.
  61049. *
  61050. * @param [in] res Test result code.
  61051. * @return String describing test result.
  61052. */
  61053. static const char* apitest_res_string(int res)
  61054. {
  61055. const char* str = "invalid result";
  61056. switch (res) {
  61057. case TEST_SUCCESS:
  61058. str = "passed";
  61059. break;
  61060. case TEST_FAIL:
  61061. str = "failed";
  61062. break;
  61063. case TEST_SKIPPED:
  61064. str = "skipped";
  61065. break;
  61066. }
  61067. return str;
  61068. }
  61069. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  61070. static double gettime_secs(void)
  61071. #if defined(_MSC_VER) && defined(_WIN32)
  61072. {
  61073. /* there's no gettimeofday for Windows, so we'll use system time */
  61074. #define EPOCH_DIFF 11644473600LL
  61075. FILETIME currentFileTime;
  61076. GetSystemTimePreciseAsFileTime(&currentFileTime);
  61077. ULARGE_INTEGER uli = { 0, 0 };
  61078. uli.LowPart = currentFileTime.dwLowDateTime;
  61079. uli.HighPart = currentFileTime.dwHighDateTime;
  61080. /* Convert to seconds since Unix epoch */
  61081. return (double)((uli.QuadPart - (EPOCH_DIFF * 10000000)) / 10000000.0);
  61082. }
  61083. #else
  61084. {
  61085. struct timeval tv;
  61086. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  61087. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
  61088. }
  61089. #endif
  61090. #endif
  61091. int ApiTest(void)
  61092. {
  61093. int i;
  61094. int ret;
  61095. int res = 0;
  61096. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  61097. double timeDiff;
  61098. #endif
  61099. printf(" Begin API Tests\n");
  61100. fflush(stdout);
  61101. /* we must perform init and cleanup if not all tests are running */
  61102. if (!testAll) {
  61103. #ifdef WOLFCRYPT_ONLY
  61104. if (wolfCrypt_Init() != 0) {
  61105. printf("wolfCrypt Initialization failed\n");
  61106. res = 1;
  61107. }
  61108. #else
  61109. if (wolfSSL_Init() != WOLFSSL_SUCCESS) {
  61110. printf("wolfSSL Initialization failed\n");
  61111. res = 1;
  61112. }
  61113. #endif
  61114. }
  61115. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  61116. if (res == 0) {
  61117. if (create_tmp_dir(tmpDirName, sizeof(tmpDirName) - 1) == NULL) {
  61118. printf("failed to create tmp dir\n");
  61119. res = 1;
  61120. }
  61121. else {
  61122. tmpDirNameSet = 1;
  61123. }
  61124. }
  61125. #endif
  61126. if (res == 0) {
  61127. for (i = 0; i < TEST_CASE_CNT; ++i) {
  61128. EXPECT_DECLS;
  61129. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  61130. currentTestName = testCases[i].name;
  61131. #endif
  61132. /* When not testing all cases then skip if not marked for running.
  61133. */
  61134. if (!testAll && !testCases[i].run) {
  61135. continue;
  61136. }
  61137. TestSetup();
  61138. printf(" %3d: %-52s:", i + 1, testCases[i].name);
  61139. fflush(stdout);
  61140. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  61141. timeDiff = gettime_secs();
  61142. #endif
  61143. ret = testCases[i].func();
  61144. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  61145. timeDiff = gettime_secs() - timeDiff;
  61146. #endif
  61147. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  61148. if (ret != TEST_SKIPPED) {
  61149. printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
  61150. }
  61151. else
  61152. #endif
  61153. {
  61154. printf(" %s\n", apitest_res_string(ret));
  61155. }
  61156. fflush(stdout);
  61157. /* if return code is < 0 and not skipped then assert error */
  61158. Expect((ret > 0 || ret == TEST_SKIPPED),
  61159. ("Test failed\n"),
  61160. ("ret %d", ret));
  61161. testCases[i].fail = ((ret <= 0) && (ret != TEST_SKIPPED));
  61162. res |= ((ret <= 0) && (ret != TEST_SKIPPED));
  61163. TestCleanup();
  61164. }
  61165. }
  61166. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  61167. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  61168. wc_ecc_fp_free(); /* free per thread cache */
  61169. #endif
  61170. if (!testAll) {
  61171. #ifdef WOLFCRYPT_ONLY
  61172. wolfCrypt_Cleanup();
  61173. #else
  61174. wolfSSL_Cleanup();
  61175. #endif
  61176. }
  61177. (void)testDevId;
  61178. if (res != 0) {
  61179. printf("\nFAILURES:\n");
  61180. for (i = 0; i < TEST_CASE_CNT; ++i) {
  61181. if (testCases[i].fail) {
  61182. printf(" %3d: %s\n", i + 1, testCases[i].name);
  61183. }
  61184. }
  61185. printf("\n");
  61186. fflush(stdout);
  61187. }
  61188. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  61189. if (tmpDirNameSet) {
  61190. printf("\nBinary dumps of the memio streams can be found in the\n"
  61191. "%s directory. This can be imported into\n"
  61192. "Wireshark by transforming the file with\n"
  61193. "\tod -Ax -tx1 -v stream.dump > stream.dump.hex\n"
  61194. "And then loading test_output.dump.hex into Wireshark using\n"
  61195. "the \"Import from Hex Dump...\" option and selecting the\n"
  61196. "TCP encapsulation option.\n", tmpDirName);
  61197. }
  61198. #endif
  61199. printf(" End API Tests\n");
  61200. fflush(stdout);
  61201. return res;
  61202. }