api.c 2.5 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581445824458344584445854458644587445884458944590445914459244593445944459544596445974459844599446004460144602446034460444605446064460744608446094461044611446124461344614446154461644617446184461944620446214462244623446244462544626446274462844629446304463144632446334463444635446364463744638446394464044641446424464344644446454464644647446484464944650446514465244653446544465544656446574465844659446604466144662446634466444665446664466744668446694467044671446724467344674446754467644677446784467944680446814468244683446844468544686446874468844689446904469144692446934469444695446964469744698446994470044701447024470344704447054470644707447084470944710447114471244713447144471544716447174471844719447204472144722447234472444725447264472744728447294473044731447324473344734447354473644737447384473944740447414474244743447444474544746447474474844749447504475144752447534475444755447564475744758447594476044761447624476344764447654476644767447684476944770447714477244773447744477544776447774477844779447804478144782447834478444785447864478744788447894479044791447924479344794447954479644797447984479944800448014480244803448044480544806448074480844809448104481144812448134481444815448164481744818448194482044821448224482344824448254482644827448284482944830448314483244833448344483544836448374483844839448404484144842448434484444845448464484744848448494485044851448524485344854448554485644857448584485944860448614486244863448644486544866448674486844869448704487144872448734487444875448764487744878448794488044881448824488344884448854488644887448884488944890448914489244893448944489544896448974489844899449004490144902449034490444905449064490744908449094491044911449124491344914449154491644917449184491944920449214492244923449244492544926449274492844929449304493144932449334493444935449364493744938449394494044941449424494344944449454494644947449484494944950449514495244953449544495544956449574495844959449604496144962449634496444965449664496744968449694497044971449724497344974449754497644977449784497944980449814498244983449844498544986449874498844989449904499144992449934499444995449964499744998449994500045001450024500345004450054500645007450084500945010450114501245013450144501545016450174501845019450204502145022450234502445025450264502745028450294503045031450324503345034450354503645037450384503945040450414504245043450444504545046450474504845049450504505145052450534505445055450564505745058450594506045061450624506345064450654506645067450684506945070450714507245073450744507545076450774507845079450804508145082450834508445085450864508745088450894509045091450924509345094450954509645097450984509945100451014510245103451044510545106451074510845109451104511145112451134511445115451164511745118451194512045121451224512345124451254512645127451284512945130451314513245133451344513545136451374513845139451404514145142451434514445145451464514745148451494515045151451524515345154451554515645157451584515945160451614516245163451644516545166451674516845169451704517145172451734517445175451764517745178451794518045181451824518345184451854518645187451884518945190451914519245193451944519545196451974519845199452004520145202452034520445205452064520745208452094521045211452124521345214452154521645217452184521945220452214522245223452244522545226452274522845229452304523145232452334523445235452364523745238452394524045241452424524345244452454524645247452484524945250452514525245253452544525545256452574525845259452604526145262452634526445265452664526745268452694527045271452724527345274452754527645277452784527945280452814528245283452844528545286452874528845289452904529145292452934529445295452964529745298452994530045301453024530345304453054530645307453084530945310453114531245313453144531545316453174531845319453204532145322453234532445325453264532745328453294533045331453324533345334453354533645337453384533945340453414534245343453444534545346453474534845349453504535145352453534535445355453564535745358453594536045361453624536345364453654536645367453684536945370453714537245373453744537545376453774537845379453804538145382453834538445385453864538745388453894539045391453924539345394453954539645397453984539945400454014540245403454044540545406454074540845409454104541145412454134541445415454164541745418454194542045421454224542345424454254542645427454284542945430454314543245433454344543545436454374543845439454404544145442454434544445445454464544745448454494545045451454524545345454454554545645457454584545945460454614546245463454644546545466454674546845469454704547145472454734547445475454764547745478454794548045481454824548345484454854548645487454884548945490454914549245493454944549545496454974549845499455004550145502455034550445505455064550745508455094551045511455124551345514455154551645517455184551945520455214552245523455244552545526455274552845529455304553145532455334553445535455364553745538455394554045541455424554345544455454554645547455484554945550455514555245553455544555545556455574555845559455604556145562455634556445565455664556745568455694557045571455724557345574455754557645577455784557945580455814558245583455844558545586455874558845589455904559145592455934559445595455964559745598455994560045601456024560345604456054560645607456084560945610456114561245613456144561545616456174561845619456204562145622456234562445625456264562745628456294563045631456324563345634456354563645637456384563945640456414564245643456444564545646456474564845649456504565145652456534565445655456564565745658456594566045661456624566345664456654566645667456684566945670456714567245673456744567545676456774567845679456804568145682456834568445685456864568745688456894569045691456924569345694456954569645697456984569945700457014570245703457044570545706457074570845709457104571145712457134571445715457164571745718457194572045721457224572345724457254572645727457284572945730457314573245733457344573545736457374573845739457404574145742457434574445745457464574745748457494575045751457524575345754457554575645757457584575945760457614576245763457644576545766457674576845769457704577145772457734577445775457764577745778457794578045781457824578345784457854578645787457884578945790457914579245793457944579545796457974579845799458004580145802458034580445805458064580745808458094581045811458124581345814458154581645817458184581945820458214582245823458244582545826458274582845829458304583145832458334583445835458364583745838458394584045841458424584345844458454584645847458484584945850458514585245853458544585545856458574585845859458604586145862458634586445865458664586745868458694587045871458724587345874458754587645877458784587945880458814588245883458844588545886458874588845889458904589145892458934589445895458964589745898458994590045901459024590345904459054590645907459084590945910459114591245913459144591545916459174591845919459204592145922459234592445925459264592745928459294593045931459324593345934459354593645937459384593945940459414594245943459444594545946459474594845949459504595145952459534595445955459564595745958459594596045961459624596345964459654596645967459684596945970459714597245973459744597545976459774597845979459804598145982459834598445985459864598745988459894599045991459924599345994459954599645997459984599946000460014600246003460044600546006460074600846009460104601146012460134601446015460164601746018460194602046021460224602346024460254602646027460284602946030460314603246033460344603546036460374603846039460404604146042460434604446045460464604746048460494605046051460524605346054460554605646057460584605946060460614606246063460644606546066460674606846069460704607146072460734607446075460764607746078460794608046081460824608346084460854608646087460884608946090460914609246093460944609546096460974609846099461004610146102461034610446105461064610746108461094611046111461124611346114461154611646117461184611946120461214612246123461244612546126461274612846129461304613146132461334613446135461364613746138461394614046141461424614346144461454614646147461484614946150461514615246153461544615546156461574615846159461604616146162461634616446165461664616746168461694617046171461724617346174461754617646177461784617946180461814618246183461844618546186461874618846189461904619146192461934619446195461964619746198461994620046201462024620346204462054620646207462084620946210462114621246213462144621546216462174621846219462204622146222462234622446225462264622746228462294623046231462324623346234462354623646237462384623946240462414624246243462444624546246462474624846249462504625146252462534625446255462564625746258462594626046261462624626346264462654626646267462684626946270462714627246273462744627546276462774627846279462804628146282462834628446285462864628746288462894629046291462924629346294462954629646297462984629946300463014630246303463044630546306463074630846309463104631146312463134631446315463164631746318463194632046321463224632346324463254632646327463284632946330463314633246333463344633546336463374633846339463404634146342463434634446345463464634746348463494635046351463524635346354463554635646357463584635946360463614636246363463644636546366463674636846369463704637146372463734637446375463764637746378463794638046381463824638346384463854638646387463884638946390463914639246393463944639546396463974639846399464004640146402464034640446405464064640746408464094641046411464124641346414464154641646417464184641946420464214642246423464244642546426464274642846429464304643146432464334643446435464364643746438464394644046441464424644346444464454644646447464484644946450464514645246453464544645546456464574645846459464604646146462464634646446465464664646746468464694647046471464724647346474464754647646477464784647946480464814648246483464844648546486464874648846489464904649146492464934649446495464964649746498464994650046501465024650346504465054650646507465084650946510465114651246513465144651546516465174651846519465204652146522465234652446525465264652746528465294653046531465324653346534465354653646537465384653946540465414654246543465444654546546465474654846549465504655146552465534655446555465564655746558465594656046561465624656346564465654656646567465684656946570465714657246573465744657546576465774657846579465804658146582465834658446585465864658746588465894659046591465924659346594465954659646597465984659946600466014660246603466044660546606466074660846609466104661146612466134661446615466164661746618466194662046621466224662346624466254662646627466284662946630466314663246633466344663546636466374663846639466404664146642466434664446645466464664746648466494665046651466524665346654466554665646657466584665946660466614666246663466644666546666466674666846669466704667146672466734667446675466764667746678466794668046681466824668346684466854668646687466884668946690466914669246693466944669546696466974669846699467004670146702467034670446705467064670746708467094671046711467124671346714467154671646717467184671946720467214672246723467244672546726467274672846729467304673146732467334673446735467364673746738467394674046741467424674346744467454674646747467484674946750467514675246753467544675546756467574675846759467604676146762467634676446765467664676746768467694677046771467724677346774467754677646777467784677946780467814678246783467844678546786467874678846789467904679146792467934679446795467964679746798467994680046801468024680346804468054680646807468084680946810468114681246813468144681546816468174681846819468204682146822468234682446825468264682746828468294683046831468324683346834468354683646837468384683946840468414684246843468444684546846468474684846849468504685146852468534685446855468564685746858468594686046861468624686346864468654686646867468684686946870468714687246873468744687546876468774687846879468804688146882468834688446885468864688746888468894689046891468924689346894468954689646897468984689946900469014690246903469044690546906469074690846909469104691146912469134691446915469164691746918469194692046921469224692346924469254692646927469284692946930469314693246933469344693546936469374693846939469404694146942469434694446945469464694746948469494695046951469524695346954469554695646957469584695946960469614696246963469644696546966469674696846969469704697146972469734697446975469764697746978469794698046981469824698346984469854698646987469884698946990469914699246993469944699546996469974699846999470004700147002470034700447005470064700747008470094701047011470124701347014470154701647017470184701947020470214702247023470244702547026470274702847029470304703147032470334703447035470364703747038470394704047041470424704347044470454704647047470484704947050470514705247053470544705547056470574705847059470604706147062470634706447065470664706747068470694707047071470724707347074470754707647077470784707947080470814708247083470844708547086470874708847089470904709147092470934709447095470964709747098470994710047101471024710347104471054710647107471084710947110471114711247113471144711547116471174711847119471204712147122471234712447125471264712747128471294713047131471324713347134471354713647137471384713947140471414714247143471444714547146471474714847149471504715147152471534715447155471564715747158471594716047161471624716347164471654716647167471684716947170471714717247173471744717547176471774717847179471804718147182471834718447185471864718747188471894719047191471924719347194471954719647197471984719947200472014720247203472044720547206472074720847209472104721147212472134721447215472164721747218472194722047221472224722347224472254722647227472284722947230472314723247233472344723547236472374723847239472404724147242472434724447245472464724747248472494725047251472524725347254472554725647257472584725947260472614726247263472644726547266472674726847269472704727147272472734727447275472764727747278472794728047281472824728347284472854728647287472884728947290472914729247293472944729547296472974729847299473004730147302473034730447305473064730747308473094731047311473124731347314473154731647317473184731947320473214732247323473244732547326473274732847329473304733147332473334733447335473364733747338473394734047341473424734347344473454734647347473484734947350473514735247353473544735547356473574735847359473604736147362473634736447365473664736747368473694737047371473724737347374473754737647377473784737947380473814738247383473844738547386473874738847389473904739147392473934739447395473964739747398473994740047401474024740347404474054740647407474084740947410474114741247413474144741547416474174741847419474204742147422474234742447425474264742747428474294743047431474324743347434474354743647437474384743947440474414744247443474444744547446474474744847449474504745147452474534745447455474564745747458474594746047461474624746347464474654746647467474684746947470474714747247473474744747547476474774747847479474804748147482474834748447485474864748747488474894749047491474924749347494474954749647497474984749947500475014750247503475044750547506475074750847509475104751147512475134751447515475164751747518475194752047521475224752347524475254752647527475284752947530475314753247533475344753547536475374753847539475404754147542475434754447545475464754747548475494755047551475524755347554475554755647557475584755947560475614756247563475644756547566475674756847569475704757147572475734757447575475764757747578475794758047581475824758347584475854758647587475884758947590475914759247593475944759547596475974759847599476004760147602476034760447605476064760747608476094761047611476124761347614476154761647617476184761947620476214762247623476244762547626476274762847629476304763147632476334763447635476364763747638476394764047641476424764347644476454764647647476484764947650476514765247653476544765547656476574765847659476604766147662476634766447665476664766747668476694767047671476724767347674476754767647677476784767947680476814768247683476844768547686476874768847689476904769147692476934769447695476964769747698476994770047701477024770347704477054770647707477084770947710477114771247713477144771547716477174771847719477204772147722477234772447725477264772747728477294773047731477324773347734477354773647737477384773947740477414774247743477444774547746477474774847749477504775147752477534775447755477564775747758477594776047761477624776347764477654776647767477684776947770477714777247773477744777547776477774777847779477804778147782477834778447785477864778747788477894779047791477924779347794477954779647797477984779947800478014780247803478044780547806478074780847809478104781147812478134781447815478164781747818478194782047821478224782347824478254782647827478284782947830478314783247833478344783547836478374783847839478404784147842478434784447845478464784747848478494785047851478524785347854478554785647857478584785947860478614786247863478644786547866478674786847869478704787147872478734787447875478764787747878478794788047881478824788347884478854788647887478884788947890478914789247893478944789547896478974789847899479004790147902479034790447905479064790747908479094791047911479124791347914479154791647917479184791947920479214792247923479244792547926479274792847929479304793147932479334793447935479364793747938479394794047941479424794347944479454794647947479484794947950479514795247953479544795547956479574795847959479604796147962479634796447965479664796747968479694797047971479724797347974479754797647977479784797947980479814798247983479844798547986479874798847989479904799147992479934799447995479964799747998479994800048001480024800348004480054800648007480084800948010480114801248013480144801548016480174801848019480204802148022480234802448025480264802748028480294803048031480324803348034480354803648037480384803948040480414804248043480444804548046480474804848049480504805148052480534805448055480564805748058480594806048061480624806348064480654806648067480684806948070480714807248073480744807548076480774807848079480804808148082480834808448085480864808748088480894809048091480924809348094480954809648097480984809948100481014810248103481044810548106481074810848109481104811148112481134811448115481164811748118481194812048121481224812348124481254812648127481284812948130481314813248133481344813548136481374813848139481404814148142481434814448145481464814748148481494815048151481524815348154481554815648157481584815948160481614816248163481644816548166481674816848169481704817148172481734817448175481764817748178481794818048181481824818348184481854818648187481884818948190481914819248193481944819548196481974819848199482004820148202482034820448205482064820748208482094821048211482124821348214482154821648217482184821948220482214822248223482244822548226482274822848229482304823148232482334823448235482364823748238482394824048241482424824348244482454824648247482484824948250482514825248253482544825548256482574825848259482604826148262482634826448265482664826748268482694827048271482724827348274482754827648277482784827948280482814828248283482844828548286482874828848289482904829148292482934829448295482964829748298482994830048301483024830348304483054830648307483084830948310483114831248313483144831548316483174831848319483204832148322483234832448325483264832748328483294833048331483324833348334483354833648337483384833948340483414834248343483444834548346483474834848349483504835148352483534835448355483564835748358483594836048361483624836348364483654836648367483684836948370483714837248373483744837548376483774837848379483804838148382483834838448385483864838748388483894839048391483924839348394483954839648397483984839948400484014840248403484044840548406484074840848409484104841148412484134841448415484164841748418484194842048421484224842348424484254842648427484284842948430484314843248433484344843548436484374843848439484404844148442484434844448445484464844748448484494845048451484524845348454484554845648457484584845948460484614846248463484644846548466484674846848469484704847148472484734847448475484764847748478484794848048481484824848348484484854848648487484884848948490484914849248493484944849548496484974849848499485004850148502485034850448505485064850748508485094851048511485124851348514485154851648517485184851948520485214852248523485244852548526485274852848529485304853148532485334853448535485364853748538485394854048541485424854348544485454854648547485484854948550485514855248553485544855548556485574855848559485604856148562485634856448565485664856748568485694857048571485724857348574485754857648577485784857948580485814858248583485844858548586485874858848589485904859148592485934859448595485964859748598485994860048601486024860348604486054860648607486084860948610486114861248613486144861548616486174861848619486204862148622486234862448625486264862748628486294863048631486324863348634486354863648637486384863948640486414864248643486444864548646486474864848649486504865148652486534865448655486564865748658486594866048661486624866348664486654866648667486684866948670486714867248673486744867548676486774867848679486804868148682486834868448685486864868748688486894869048691486924869348694486954869648697486984869948700487014870248703487044870548706487074870848709487104871148712487134871448715487164871748718487194872048721487224872348724487254872648727487284872948730487314873248733487344873548736487374873848739487404874148742487434874448745487464874748748487494875048751487524875348754487554875648757487584875948760487614876248763487644876548766487674876848769487704877148772487734877448775487764877748778487794878048781487824878348784487854878648787487884878948790487914879248793487944879548796487974879848799488004880148802488034880448805488064880748808488094881048811488124881348814488154881648817488184881948820488214882248823488244882548826488274882848829488304883148832488334883448835488364883748838488394884048841488424884348844488454884648847488484884948850488514885248853488544885548856488574885848859488604886148862488634886448865488664886748868488694887048871488724887348874488754887648877488784887948880488814888248883488844888548886488874888848889488904889148892488934889448895488964889748898488994890048901489024890348904489054890648907489084890948910489114891248913489144891548916489174891848919489204892148922489234892448925489264892748928489294893048931489324893348934489354893648937489384893948940489414894248943489444894548946489474894848949489504895148952489534895448955489564895748958489594896048961489624896348964489654896648967489684896948970489714897248973489744897548976489774897848979489804898148982489834898448985489864898748988489894899048991489924899348994489954899648997489984899949000490014900249003490044900549006490074900849009490104901149012490134901449015490164901749018490194902049021490224902349024490254902649027490284902949030490314903249033490344903549036490374903849039490404904149042490434904449045490464904749048490494905049051490524905349054490554905649057490584905949060490614906249063490644906549066490674906849069490704907149072490734907449075490764907749078490794908049081490824908349084490854908649087490884908949090490914909249093490944909549096490974909849099491004910149102491034910449105491064910749108491094911049111491124911349114491154911649117491184911949120491214912249123491244912549126491274912849129491304913149132491334913449135491364913749138491394914049141491424914349144491454914649147491484914949150491514915249153491544915549156491574915849159491604916149162491634916449165491664916749168491694917049171491724917349174491754917649177491784917949180491814918249183491844918549186491874918849189491904919149192491934919449195491964919749198491994920049201492024920349204492054920649207492084920949210492114921249213492144921549216492174921849219492204922149222492234922449225492264922749228492294923049231492324923349234492354923649237492384923949240492414924249243492444924549246492474924849249492504925149252492534925449255492564925749258492594926049261492624926349264492654926649267492684926949270492714927249273492744927549276492774927849279492804928149282492834928449285492864928749288492894929049291492924929349294492954929649297492984929949300493014930249303493044930549306493074930849309493104931149312493134931449315493164931749318493194932049321493224932349324493254932649327493284932949330493314933249333493344933549336493374933849339493404934149342493434934449345493464934749348493494935049351493524935349354493554935649357493584935949360493614936249363493644936549366493674936849369493704937149372493734937449375493764937749378493794938049381493824938349384493854938649387493884938949390493914939249393493944939549396493974939849399494004940149402494034940449405494064940749408494094941049411494124941349414494154941649417494184941949420494214942249423494244942549426494274942849429494304943149432494334943449435494364943749438494394944049441494424944349444494454944649447494484944949450494514945249453494544945549456494574945849459494604946149462494634946449465494664946749468494694947049471494724947349474494754947649477494784947949480494814948249483494844948549486494874948849489494904949149492494934949449495494964949749498494994950049501495024950349504495054950649507495084950949510495114951249513495144951549516495174951849519495204952149522495234952449525495264952749528495294953049531495324953349534495354953649537495384953949540495414954249543495444954549546495474954849549495504955149552495534955449555495564955749558495594956049561495624956349564495654956649567495684956949570495714957249573495744957549576495774957849579495804958149582495834958449585495864958749588495894959049591495924959349594495954959649597495984959949600496014960249603496044960549606496074960849609496104961149612496134961449615496164961749618496194962049621496224962349624496254962649627496284962949630496314963249633496344963549636496374963849639496404964149642496434964449645496464964749648496494965049651496524965349654496554965649657496584965949660496614966249663496644966549666496674966849669496704967149672496734967449675496764967749678496794968049681496824968349684496854968649687496884968949690496914969249693496944969549696496974969849699497004970149702497034970449705497064970749708497094971049711497124971349714497154971649717497184971949720497214972249723497244972549726497274972849729497304973149732497334973449735497364973749738497394974049741497424974349744497454974649747497484974949750497514975249753497544975549756497574975849759497604976149762497634976449765497664976749768497694977049771497724977349774497754977649777497784977949780497814978249783497844978549786497874978849789497904979149792497934979449795497964979749798497994980049801498024980349804498054980649807498084980949810498114981249813498144981549816498174981849819498204982149822498234982449825498264982749828498294983049831498324983349834498354983649837498384983949840498414984249843498444984549846498474984849849498504985149852498534985449855498564985749858498594986049861498624986349864498654986649867498684986949870498714987249873498744987549876498774987849879498804988149882498834988449885498864988749888498894989049891498924989349894498954989649897498984989949900499014990249903499044990549906499074990849909499104991149912499134991449915499164991749918499194992049921499224992349924499254992649927499284992949930499314993249933499344993549936499374993849939499404994149942499434994449945499464994749948499494995049951499524995349954499554995649957499584995949960499614996249963499644996549966499674996849969499704997149972499734997449975499764997749978499794998049981499824998349984499854998649987499884998949990499914999249993499944999549996499974999849999500005000150002500035000450005500065000750008500095001050011500125001350014500155001650017500185001950020500215002250023500245002550026500275002850029500305003150032500335003450035500365003750038500395004050041500425004350044500455004650047500485004950050500515005250053500545005550056500575005850059500605006150062500635006450065500665006750068500695007050071500725007350074500755007650077500785007950080500815008250083500845008550086500875008850089500905009150092500935009450095500965009750098500995010050101501025010350104501055010650107501085010950110501115011250113501145011550116501175011850119501205012150122501235012450125501265012750128501295013050131501325013350134501355013650137501385013950140501415014250143501445014550146501475014850149501505015150152501535015450155501565015750158501595016050161501625016350164501655016650167501685016950170501715017250173501745017550176501775017850179501805018150182501835018450185501865018750188501895019050191501925019350194501955019650197501985019950200502015020250203502045020550206502075020850209502105021150212502135021450215502165021750218502195022050221502225022350224502255022650227502285022950230502315023250233502345023550236502375023850239502405024150242502435024450245502465024750248502495025050251502525025350254502555025650257502585025950260502615026250263502645026550266502675026850269502705027150272502735027450275502765027750278502795028050281502825028350284502855028650287502885028950290502915029250293502945029550296502975029850299503005030150302503035030450305503065030750308503095031050311503125031350314503155031650317503185031950320503215032250323503245032550326503275032850329503305033150332503335033450335503365033750338503395034050341503425034350344503455034650347503485034950350503515035250353503545035550356503575035850359503605036150362503635036450365503665036750368503695037050371503725037350374503755037650377503785037950380503815038250383503845038550386503875038850389503905039150392503935039450395503965039750398503995040050401504025040350404504055040650407504085040950410504115041250413504145041550416504175041850419504205042150422504235042450425504265042750428504295043050431504325043350434504355043650437504385043950440504415044250443504445044550446504475044850449504505045150452504535045450455504565045750458504595046050461504625046350464504655046650467504685046950470504715047250473504745047550476504775047850479504805048150482504835048450485504865048750488504895049050491504925049350494504955049650497504985049950500505015050250503505045050550506505075050850509505105051150512505135051450515505165051750518505195052050521505225052350524505255052650527505285052950530505315053250533505345053550536505375053850539505405054150542505435054450545505465054750548505495055050551505525055350554505555055650557505585055950560505615056250563505645056550566505675056850569505705057150572505735057450575505765057750578505795058050581505825058350584505855058650587505885058950590505915059250593505945059550596505975059850599506005060150602506035060450605506065060750608506095061050611506125061350614506155061650617506185061950620506215062250623506245062550626506275062850629506305063150632506335063450635506365063750638506395064050641506425064350644506455064650647506485064950650506515065250653506545065550656506575065850659506605066150662506635066450665506665066750668506695067050671506725067350674506755067650677506785067950680506815068250683506845068550686506875068850689506905069150692506935069450695506965069750698506995070050701507025070350704507055070650707507085070950710507115071250713507145071550716507175071850719507205072150722507235072450725507265072750728507295073050731507325073350734507355073650737507385073950740507415074250743507445074550746507475074850749507505075150752507535075450755507565075750758507595076050761507625076350764507655076650767507685076950770507715077250773507745077550776507775077850779507805078150782507835078450785507865078750788507895079050791507925079350794507955079650797507985079950800508015080250803508045080550806508075080850809508105081150812508135081450815508165081750818508195082050821508225082350824508255082650827508285082950830508315083250833508345083550836508375083850839508405084150842508435084450845508465084750848508495085050851508525085350854508555085650857508585085950860508615086250863508645086550866508675086850869508705087150872508735087450875508765087750878508795088050881508825088350884508855088650887508885088950890508915089250893508945089550896508975089850899509005090150902509035090450905509065090750908509095091050911509125091350914509155091650917509185091950920509215092250923509245092550926509275092850929509305093150932509335093450935509365093750938509395094050941509425094350944509455094650947509485094950950509515095250953509545095550956509575095850959509605096150962509635096450965509665096750968509695097050971509725097350974509755097650977509785097950980509815098250983509845098550986509875098850989509905099150992509935099450995509965099750998509995100051001510025100351004510055100651007510085100951010510115101251013510145101551016510175101851019510205102151022510235102451025510265102751028510295103051031510325103351034510355103651037510385103951040510415104251043510445104551046510475104851049510505105151052510535105451055510565105751058510595106051061510625106351064510655106651067510685106951070510715107251073510745107551076510775107851079510805108151082510835108451085510865108751088510895109051091510925109351094510955109651097510985109951100511015110251103511045110551106511075110851109511105111151112511135111451115511165111751118511195112051121511225112351124511255112651127511285112951130511315113251133511345113551136511375113851139511405114151142511435114451145511465114751148511495115051151511525115351154511555115651157511585115951160511615116251163511645116551166511675116851169511705117151172511735117451175511765117751178511795118051181511825118351184511855118651187511885118951190511915119251193511945119551196511975119851199512005120151202512035120451205512065120751208512095121051211512125121351214512155121651217512185121951220512215122251223512245122551226512275122851229512305123151232512335123451235512365123751238512395124051241512425124351244512455124651247512485124951250512515125251253512545125551256512575125851259512605126151262512635126451265512665126751268512695127051271512725127351274512755127651277512785127951280512815128251283512845128551286512875128851289512905129151292512935129451295512965129751298512995130051301513025130351304513055130651307513085130951310513115131251313513145131551316513175131851319513205132151322513235132451325513265132751328513295133051331513325133351334513355133651337513385133951340513415134251343513445134551346513475134851349513505135151352513535135451355513565135751358513595136051361513625136351364513655136651367513685136951370513715137251373513745137551376513775137851379513805138151382513835138451385513865138751388513895139051391513925139351394513955139651397513985139951400514015140251403514045140551406514075140851409514105141151412514135141451415514165141751418514195142051421514225142351424514255142651427514285142951430514315143251433514345143551436514375143851439514405144151442514435144451445514465144751448514495145051451514525145351454514555145651457514585145951460514615146251463514645146551466514675146851469514705147151472514735147451475514765147751478514795148051481514825148351484514855148651487514885148951490514915149251493514945149551496514975149851499515005150151502515035150451505515065150751508515095151051511515125151351514515155151651517515185151951520515215152251523515245152551526515275152851529515305153151532515335153451535515365153751538515395154051541515425154351544515455154651547515485154951550515515155251553515545155551556515575155851559515605156151562515635156451565515665156751568515695157051571515725157351574515755157651577515785157951580515815158251583515845158551586515875158851589515905159151592515935159451595515965159751598515995160051601516025160351604516055160651607516085160951610516115161251613516145161551616516175161851619516205162151622516235162451625516265162751628516295163051631516325163351634516355163651637516385163951640516415164251643516445164551646516475164851649516505165151652516535165451655516565165751658516595166051661516625166351664516655166651667516685166951670516715167251673516745167551676516775167851679516805168151682516835168451685516865168751688516895169051691516925169351694516955169651697516985169951700517015170251703517045170551706517075170851709517105171151712517135171451715517165171751718517195172051721517225172351724517255172651727517285172951730517315173251733517345173551736517375173851739517405174151742517435174451745517465174751748517495175051751517525175351754517555175651757517585175951760517615176251763517645176551766517675176851769517705177151772517735177451775517765177751778517795178051781517825178351784517855178651787517885178951790517915179251793517945179551796517975179851799518005180151802518035180451805518065180751808518095181051811518125181351814518155181651817518185181951820518215182251823518245182551826518275182851829518305183151832518335183451835518365183751838518395184051841518425184351844518455184651847518485184951850518515185251853518545185551856518575185851859518605186151862518635186451865518665186751868518695187051871518725187351874518755187651877518785187951880518815188251883518845188551886518875188851889518905189151892518935189451895518965189751898518995190051901519025190351904519055190651907519085190951910519115191251913519145191551916519175191851919519205192151922519235192451925519265192751928519295193051931519325193351934519355193651937519385193951940519415194251943519445194551946519475194851949519505195151952519535195451955519565195751958519595196051961519625196351964519655196651967519685196951970519715197251973519745197551976519775197851979519805198151982519835198451985519865198751988519895199051991519925199351994519955199651997519985199952000520015200252003520045200552006520075200852009520105201152012520135201452015520165201752018520195202052021520225202352024520255202652027520285202952030520315203252033520345203552036520375203852039520405204152042520435204452045520465204752048520495205052051520525205352054520555205652057520585205952060520615206252063520645206552066520675206852069520705207152072520735207452075520765207752078520795208052081520825208352084520855208652087520885208952090520915209252093520945209552096520975209852099521005210152102521035210452105521065210752108521095211052111521125211352114521155211652117521185211952120521215212252123521245212552126521275212852129521305213152132521335213452135521365213752138521395214052141521425214352144521455214652147521485214952150521515215252153521545215552156521575215852159521605216152162521635216452165521665216752168521695217052171521725217352174521755217652177521785217952180521815218252183521845218552186521875218852189521905219152192521935219452195521965219752198521995220052201522025220352204522055220652207522085220952210522115221252213522145221552216522175221852219522205222152222522235222452225522265222752228522295223052231522325223352234522355223652237522385223952240522415224252243522445224552246522475224852249522505225152252522535225452255522565225752258522595226052261522625226352264522655226652267522685226952270522715227252273522745227552276522775227852279522805228152282522835228452285522865228752288522895229052291522925229352294522955229652297522985229952300523015230252303523045230552306523075230852309523105231152312523135231452315523165231752318523195232052321523225232352324523255232652327523285232952330523315233252333523345233552336523375233852339523405234152342523435234452345523465234752348523495235052351523525235352354523555235652357523585235952360523615236252363523645236552366523675236852369523705237152372523735237452375523765237752378523795238052381523825238352384523855238652387523885238952390523915239252393523945239552396523975239852399524005240152402524035240452405524065240752408524095241052411524125241352414524155241652417524185241952420524215242252423524245242552426524275242852429524305243152432524335243452435524365243752438524395244052441524425244352444524455244652447524485244952450524515245252453524545245552456524575245852459524605246152462524635246452465524665246752468524695247052471524725247352474524755247652477524785247952480524815248252483524845248552486524875248852489524905249152492524935249452495524965249752498524995250052501525025250352504525055250652507525085250952510525115251252513525145251552516525175251852519525205252152522525235252452525525265252752528525295253052531525325253352534525355253652537525385253952540525415254252543525445254552546525475254852549525505255152552525535255452555525565255752558525595256052561525625256352564525655256652567525685256952570525715257252573525745257552576525775257852579525805258152582525835258452585525865258752588525895259052591525925259352594525955259652597525985259952600526015260252603526045260552606526075260852609526105261152612526135261452615526165261752618526195262052621526225262352624526255262652627526285262952630526315263252633526345263552636526375263852639526405264152642526435264452645526465264752648526495265052651526525265352654526555265652657526585265952660526615266252663526645266552666526675266852669526705267152672526735267452675526765267752678526795268052681526825268352684526855268652687526885268952690526915269252693526945269552696526975269852699527005270152702527035270452705527065270752708527095271052711527125271352714527155271652717527185271952720527215272252723527245272552726527275272852729527305273152732527335273452735527365273752738527395274052741527425274352744527455274652747527485274952750527515275252753527545275552756527575275852759527605276152762527635276452765527665276752768527695277052771527725277352774527755277652777527785277952780527815278252783527845278552786527875278852789527905279152792527935279452795527965279752798527995280052801528025280352804528055280652807528085280952810528115281252813528145281552816528175281852819528205282152822528235282452825528265282752828528295283052831528325283352834528355283652837528385283952840528415284252843528445284552846528475284852849528505285152852528535285452855528565285752858528595286052861528625286352864528655286652867528685286952870528715287252873528745287552876528775287852879528805288152882528835288452885528865288752888528895289052891528925289352894528955289652897528985289952900529015290252903529045290552906529075290852909529105291152912529135291452915529165291752918529195292052921529225292352924529255292652927529285292952930529315293252933529345293552936529375293852939529405294152942529435294452945529465294752948529495295052951529525295352954529555295652957529585295952960529615296252963529645296552966529675296852969529705297152972529735297452975529765297752978529795298052981529825298352984529855298652987529885298952990529915299252993529945299552996529975299852999530005300153002530035300453005530065300753008530095301053011530125301353014530155301653017530185301953020530215302253023530245302553026530275302853029530305303153032530335303453035530365303753038530395304053041530425304353044530455304653047530485304953050530515305253053530545305553056530575305853059530605306153062530635306453065530665306753068530695307053071530725307353074530755307653077530785307953080530815308253083530845308553086530875308853089530905309153092530935309453095530965309753098530995310053101531025310353104531055310653107531085310953110531115311253113531145311553116531175311853119531205312153122531235312453125531265312753128531295313053131531325313353134531355313653137531385313953140531415314253143531445314553146531475314853149531505315153152531535315453155531565315753158531595316053161531625316353164531655316653167531685316953170531715317253173531745317553176531775317853179531805318153182531835318453185531865318753188531895319053191531925319353194531955319653197531985319953200532015320253203532045320553206532075320853209532105321153212532135321453215532165321753218532195322053221532225322353224532255322653227532285322953230532315323253233532345323553236532375323853239532405324153242532435324453245532465324753248532495325053251532525325353254532555325653257532585325953260532615326253263532645326553266532675326853269532705327153272532735327453275532765327753278532795328053281532825328353284532855328653287532885328953290532915329253293532945329553296532975329853299533005330153302533035330453305533065330753308533095331053311533125331353314533155331653317533185331953320533215332253323533245332553326533275332853329533305333153332533335333453335533365333753338533395334053341533425334353344533455334653347533485334953350533515335253353533545335553356533575335853359533605336153362533635336453365533665336753368533695337053371533725337353374533755337653377533785337953380533815338253383533845338553386533875338853389533905339153392533935339453395533965339753398533995340053401534025340353404534055340653407534085340953410534115341253413534145341553416534175341853419534205342153422534235342453425534265342753428534295343053431534325343353434534355343653437534385343953440534415344253443534445344553446534475344853449534505345153452534535345453455534565345753458534595346053461534625346353464534655346653467534685346953470534715347253473534745347553476534775347853479534805348153482534835348453485534865348753488534895349053491534925349353494534955349653497534985349953500535015350253503535045350553506535075350853509535105351153512535135351453515535165351753518535195352053521535225352353524535255352653527535285352953530535315353253533535345353553536535375353853539535405354153542535435354453545535465354753548535495355053551535525355353554535555355653557535585355953560535615356253563535645356553566535675356853569535705357153572535735357453575535765357753578535795358053581535825358353584535855358653587535885358953590535915359253593535945359553596535975359853599536005360153602536035360453605536065360753608536095361053611536125361353614536155361653617536185361953620536215362253623536245362553626536275362853629536305363153632536335363453635536365363753638536395364053641536425364353644536455364653647536485364953650536515365253653536545365553656536575365853659536605366153662536635366453665536665366753668536695367053671536725367353674536755367653677536785367953680536815368253683536845368553686536875368853689536905369153692536935369453695536965369753698536995370053701537025370353704537055370653707537085370953710537115371253713537145371553716537175371853719537205372153722537235372453725537265372753728537295373053731537325373353734537355373653737537385373953740537415374253743537445374553746537475374853749537505375153752537535375453755537565375753758537595376053761537625376353764537655376653767537685376953770537715377253773537745377553776537775377853779537805378153782537835378453785537865378753788537895379053791537925379353794537955379653797537985379953800538015380253803538045380553806538075380853809538105381153812538135381453815538165381753818538195382053821538225382353824538255382653827538285382953830538315383253833538345383553836538375383853839538405384153842538435384453845538465384753848538495385053851538525385353854538555385653857538585385953860538615386253863538645386553866538675386853869538705387153872538735387453875538765387753878538795388053881538825388353884538855388653887538885388953890538915389253893538945389553896538975389853899539005390153902539035390453905539065390753908539095391053911539125391353914539155391653917539185391953920539215392253923539245392553926539275392853929539305393153932539335393453935539365393753938539395394053941539425394353944539455394653947539485394953950539515395253953539545395553956539575395853959539605396153962539635396453965539665396753968539695397053971539725397353974539755397653977539785397953980539815398253983539845398553986539875398853989539905399153992539935399453995539965399753998539995400054001540025400354004540055400654007540085400954010540115401254013540145401554016540175401854019540205402154022540235402454025540265402754028540295403054031540325403354034540355403654037540385403954040540415404254043540445404554046540475404854049540505405154052540535405454055540565405754058540595406054061540625406354064540655406654067540685406954070540715407254073540745407554076540775407854079540805408154082540835408454085540865408754088540895409054091540925409354094540955409654097540985409954100541015410254103541045410554106541075410854109541105411154112541135411454115541165411754118541195412054121541225412354124541255412654127541285412954130541315413254133541345413554136541375413854139541405414154142541435414454145541465414754148541495415054151541525415354154541555415654157541585415954160541615416254163541645416554166541675416854169541705417154172541735417454175541765417754178541795418054181541825418354184541855418654187541885418954190541915419254193541945419554196541975419854199542005420154202542035420454205542065420754208542095421054211542125421354214542155421654217542185421954220542215422254223542245422554226542275422854229542305423154232542335423454235542365423754238542395424054241542425424354244542455424654247542485424954250542515425254253542545425554256542575425854259542605426154262542635426454265542665426754268542695427054271542725427354274542755427654277542785427954280542815428254283542845428554286542875428854289542905429154292542935429454295542965429754298542995430054301543025430354304543055430654307543085430954310543115431254313543145431554316543175431854319543205432154322543235432454325543265432754328543295433054331543325433354334543355433654337543385433954340543415434254343543445434554346543475434854349543505435154352543535435454355543565435754358543595436054361543625436354364543655436654367543685436954370543715437254373543745437554376543775437854379543805438154382543835438454385543865438754388543895439054391543925439354394543955439654397543985439954400544015440254403544045440554406544075440854409544105441154412544135441454415544165441754418544195442054421544225442354424544255442654427544285442954430544315443254433544345443554436544375443854439544405444154442544435444454445544465444754448544495445054451544525445354454544555445654457544585445954460544615446254463544645446554466544675446854469544705447154472544735447454475544765447754478544795448054481544825448354484544855448654487544885448954490544915449254493544945449554496544975449854499545005450154502545035450454505545065450754508545095451054511545125451354514545155451654517545185451954520545215452254523545245452554526545275452854529545305453154532545335453454535545365453754538545395454054541545425454354544545455454654547545485454954550545515455254553545545455554556545575455854559545605456154562545635456454565545665456754568545695457054571545725457354574545755457654577545785457954580545815458254583545845458554586545875458854589545905459154592545935459454595545965459754598545995460054601546025460354604546055460654607546085460954610546115461254613546145461554616546175461854619546205462154622546235462454625546265462754628546295463054631546325463354634546355463654637546385463954640546415464254643546445464554646546475464854649546505465154652546535465454655546565465754658546595466054661546625466354664546655466654667546685466954670546715467254673546745467554676546775467854679546805468154682546835468454685546865468754688546895469054691546925469354694546955469654697546985469954700547015470254703547045470554706547075470854709547105471154712547135471454715547165471754718547195472054721547225472354724547255472654727547285472954730547315473254733547345473554736547375473854739547405474154742547435474454745547465474754748547495475054751547525475354754547555475654757547585475954760547615476254763547645476554766547675476854769547705477154772547735477454775547765477754778547795478054781547825478354784547855478654787547885478954790547915479254793547945479554796547975479854799548005480154802548035480454805548065480754808548095481054811548125481354814548155481654817548185481954820548215482254823548245482554826548275482854829548305483154832548335483454835548365483754838548395484054841548425484354844548455484654847548485484954850548515485254853548545485554856548575485854859548605486154862548635486454865548665486754868548695487054871548725487354874548755487654877548785487954880548815488254883548845488554886548875488854889548905489154892548935489454895548965489754898548995490054901549025490354904549055490654907549085490954910549115491254913549145491554916549175491854919549205492154922549235492454925549265492754928549295493054931549325493354934549355493654937549385493954940549415494254943549445494554946549475494854949549505495154952549535495454955549565495754958549595496054961549625496354964549655496654967549685496954970549715497254973549745497554976549775497854979549805498154982549835498454985549865498754988549895499054991549925499354994549955499654997549985499955000550015500255003550045500555006550075500855009550105501155012550135501455015550165501755018550195502055021550225502355024550255502655027550285502955030550315503255033550345503555036550375503855039550405504155042550435504455045550465504755048550495505055051550525505355054550555505655057550585505955060550615506255063550645506555066550675506855069550705507155072550735507455075550765507755078550795508055081550825508355084550855508655087550885508955090550915509255093550945509555096550975509855099551005510155102551035510455105551065510755108551095511055111551125511355114551155511655117551185511955120551215512255123551245512555126551275512855129551305513155132551335513455135551365513755138551395514055141551425514355144551455514655147551485514955150551515515255153551545515555156551575515855159551605516155162551635516455165551665516755168551695517055171551725517355174551755517655177551785517955180551815518255183551845518555186551875518855189551905519155192551935519455195551965519755198551995520055201552025520355204552055520655207552085520955210552115521255213552145521555216552175521855219552205522155222552235522455225552265522755228552295523055231552325523355234552355523655237552385523955240552415524255243552445524555246552475524855249552505525155252552535525455255552565525755258552595526055261552625526355264552655526655267552685526955270552715527255273552745527555276552775527855279552805528155282552835528455285552865528755288552895529055291552925529355294552955529655297552985529955300553015530255303553045530555306553075530855309553105531155312553135531455315553165531755318553195532055321553225532355324553255532655327553285532955330553315533255333553345533555336553375533855339553405534155342553435534455345553465534755348553495535055351553525535355354553555535655357553585535955360553615536255363553645536555366553675536855369553705537155372553735537455375553765537755378553795538055381553825538355384553855538655387553885538955390553915539255393553945539555396553975539855399554005540155402554035540455405554065540755408554095541055411554125541355414554155541655417554185541955420554215542255423554245542555426554275542855429554305543155432554335543455435554365543755438554395544055441554425544355444554455544655447554485544955450554515545255453554545545555456554575545855459554605546155462554635546455465554665546755468554695547055471554725547355474554755547655477554785547955480554815548255483554845548555486554875548855489554905549155492554935549455495554965549755498554995550055501555025550355504555055550655507555085550955510555115551255513555145551555516555175551855519555205552155522555235552455525555265552755528555295553055531555325553355534555355553655537555385553955540555415554255543555445554555546555475554855549555505555155552555535555455555555565555755558555595556055561555625556355564555655556655567555685556955570555715557255573555745557555576555775557855579555805558155582555835558455585555865558755588555895559055591555925559355594555955559655597555985559955600556015560255603556045560555606556075560855609556105561155612556135561455615556165561755618556195562055621556225562355624556255562655627556285562955630556315563255633556345563555636556375563855639556405564155642556435564455645556465564755648556495565055651556525565355654556555565655657556585565955660556615566255663556645566555666556675566855669556705567155672556735567455675556765567755678556795568055681556825568355684556855568655687556885568955690556915569255693556945569555696556975569855699557005570155702557035570455705557065570755708557095571055711557125571355714557155571655717557185571955720557215572255723557245572555726557275572855729557305573155732557335573455735557365573755738557395574055741557425574355744557455574655747557485574955750557515575255753557545575555756557575575855759557605576155762557635576455765557665576755768557695577055771557725577355774557755577655777557785577955780557815578255783557845578555786557875578855789557905579155792557935579455795557965579755798557995580055801558025580355804558055580655807558085580955810558115581255813558145581555816558175581855819558205582155822558235582455825558265582755828558295583055831558325583355834558355583655837558385583955840558415584255843558445584555846558475584855849558505585155852558535585455855558565585755858558595586055861558625586355864558655586655867558685586955870558715587255873558745587555876558775587855879558805588155882558835588455885558865588755888558895589055891558925589355894558955589655897558985589955900559015590255903559045590555906559075590855909559105591155912559135591455915559165591755918559195592055921559225592355924559255592655927559285592955930559315593255933559345593555936559375593855939559405594155942559435594455945559465594755948559495595055951559525595355954559555595655957559585595955960559615596255963559645596555966559675596855969559705597155972559735597455975559765597755978559795598055981559825598355984559855598655987559885598955990559915599255993559945599555996559975599855999560005600156002560035600456005560065600756008560095601056011560125601356014560155601656017560185601956020560215602256023560245602556026560275602856029560305603156032560335603456035560365603756038560395604056041560425604356044560455604656047560485604956050560515605256053560545605556056560575605856059560605606156062560635606456065560665606756068560695607056071560725607356074560755607656077560785607956080560815608256083560845608556086560875608856089560905609156092560935609456095560965609756098560995610056101561025610356104561055610656107561085610956110561115611256113561145611556116561175611856119561205612156122561235612456125561265612756128561295613056131561325613356134561355613656137561385613956140561415614256143561445614556146561475614856149561505615156152561535615456155561565615756158561595616056161561625616356164561655616656167561685616956170561715617256173561745617556176561775617856179561805618156182561835618456185561865618756188561895619056191561925619356194561955619656197561985619956200562015620256203562045620556206562075620856209562105621156212562135621456215562165621756218562195622056221562225622356224562255622656227562285622956230562315623256233562345623556236562375623856239562405624156242562435624456245562465624756248562495625056251562525625356254562555625656257562585625956260562615626256263562645626556266562675626856269562705627156272562735627456275562765627756278562795628056281562825628356284562855628656287562885628956290562915629256293562945629556296562975629856299563005630156302563035630456305563065630756308563095631056311563125631356314563155631656317563185631956320563215632256323563245632556326563275632856329563305633156332563335633456335563365633756338563395634056341563425634356344563455634656347563485634956350563515635256353563545635556356563575635856359563605636156362563635636456365563665636756368563695637056371563725637356374563755637656377563785637956380563815638256383563845638556386563875638856389563905639156392563935639456395563965639756398563995640056401564025640356404564055640656407564085640956410564115641256413564145641556416564175641856419564205642156422564235642456425564265642756428564295643056431564325643356434564355643656437564385643956440564415644256443564445644556446564475644856449564505645156452564535645456455564565645756458564595646056461564625646356464564655646656467564685646956470564715647256473564745647556476564775647856479564805648156482564835648456485564865648756488564895649056491564925649356494564955649656497564985649956500565015650256503565045650556506565075650856509565105651156512565135651456515565165651756518565195652056521565225652356524565255652656527565285652956530565315653256533565345653556536565375653856539565405654156542565435654456545565465654756548565495655056551565525655356554565555655656557565585655956560565615656256563565645656556566565675656856569565705657156572565735657456575565765657756578565795658056581565825658356584565855658656587565885658956590565915659256593565945659556596565975659856599566005660156602566035660456605566065660756608566095661056611566125661356614566155661656617566185661956620566215662256623566245662556626566275662856629566305663156632566335663456635566365663756638566395664056641566425664356644566455664656647566485664956650566515665256653566545665556656566575665856659566605666156662566635666456665566665666756668566695667056671566725667356674566755667656677566785667956680566815668256683566845668556686566875668856689566905669156692566935669456695566965669756698566995670056701567025670356704567055670656707567085670956710567115671256713567145671556716567175671856719567205672156722567235672456725567265672756728567295673056731567325673356734567355673656737567385673956740567415674256743567445674556746567475674856749567505675156752567535675456755567565675756758567595676056761567625676356764567655676656767567685676956770567715677256773567745677556776567775677856779567805678156782567835678456785567865678756788567895679056791567925679356794567955679656797567985679956800568015680256803568045680556806568075680856809568105681156812568135681456815568165681756818568195682056821568225682356824568255682656827568285682956830568315683256833568345683556836568375683856839568405684156842568435684456845568465684756848568495685056851568525685356854568555685656857568585685956860568615686256863568645686556866568675686856869568705687156872568735687456875568765687756878568795688056881568825688356884568855688656887568885688956890568915689256893568945689556896568975689856899569005690156902569035690456905569065690756908569095691056911569125691356914569155691656917569185691956920569215692256923569245692556926569275692856929569305693156932569335693456935569365693756938569395694056941569425694356944569455694656947569485694956950569515695256953569545695556956569575695856959569605696156962569635696456965569665696756968569695697056971569725697356974569755697656977569785697956980569815698256983569845698556986569875698856989569905699156992569935699456995569965699756998569995700057001570025700357004570055700657007570085700957010570115701257013570145701557016570175701857019570205702157022570235702457025570265702757028570295703057031570325703357034570355703657037570385703957040570415704257043570445704557046570475704857049570505705157052570535705457055570565705757058570595706057061570625706357064570655706657067570685706957070570715707257073570745707557076570775707857079570805708157082570835708457085570865708757088570895709057091570925709357094570955709657097570985709957100571015710257103571045710557106571075710857109571105711157112571135711457115571165711757118571195712057121571225712357124571255712657127571285712957130571315713257133571345713557136571375713857139571405714157142571435714457145571465714757148571495715057151571525715357154571555715657157571585715957160571615716257163571645716557166571675716857169571705717157172571735717457175571765717757178571795718057181571825718357184571855718657187571885718957190571915719257193571945719557196571975719857199572005720157202572035720457205572065720757208572095721057211572125721357214572155721657217572185721957220572215722257223572245722557226572275722857229572305723157232572335723457235572365723757238572395724057241572425724357244572455724657247572485724957250572515725257253572545725557256572575725857259572605726157262572635726457265572665726757268572695727057271572725727357274572755727657277572785727957280572815728257283572845728557286572875728857289572905729157292572935729457295572965729757298572995730057301573025730357304573055730657307573085730957310573115731257313573145731557316573175731857319573205732157322573235732457325573265732757328573295733057331573325733357334573355733657337573385733957340573415734257343573445734557346573475734857349573505735157352573535735457355573565735757358573595736057361573625736357364573655736657367573685736957370573715737257373573745737557376573775737857379573805738157382573835738457385573865738757388573895739057391573925739357394573955739657397573985739957400574015740257403574045740557406574075740857409574105741157412574135741457415574165741757418574195742057421574225742357424574255742657427574285742957430574315743257433574345743557436574375743857439574405744157442574435744457445574465744757448574495745057451574525745357454574555745657457574585745957460574615746257463574645746557466574675746857469574705747157472574735747457475574765747757478574795748057481574825748357484574855748657487574885748957490574915749257493574945749557496574975749857499575005750157502575035750457505575065750757508575095751057511575125751357514575155751657517575185751957520575215752257523575245752557526575275752857529575305753157532575335753457535575365753757538575395754057541575425754357544575455754657547575485754957550575515755257553575545755557556575575755857559575605756157562575635756457565575665756757568575695757057571575725757357574575755757657577575785757957580575815758257583575845758557586575875758857589575905759157592575935759457595575965759757598575995760057601576025760357604576055760657607576085760957610576115761257613576145761557616576175761857619576205762157622576235762457625576265762757628576295763057631576325763357634576355763657637576385763957640576415764257643576445764557646576475764857649576505765157652576535765457655576565765757658576595766057661576625766357664576655766657667576685766957670576715767257673576745767557676576775767857679576805768157682576835768457685576865768757688576895769057691576925769357694576955769657697576985769957700577015770257703577045770557706577075770857709577105771157712577135771457715577165771757718577195772057721577225772357724577255772657727577285772957730577315773257733577345773557736577375773857739577405774157742577435774457745577465774757748577495775057751577525775357754577555775657757577585775957760577615776257763577645776557766577675776857769577705777157772577735777457775577765777757778577795778057781577825778357784577855778657787577885778957790577915779257793577945779557796577975779857799578005780157802578035780457805578065780757808578095781057811578125781357814578155781657817578185781957820578215782257823578245782557826578275782857829578305783157832578335783457835578365783757838578395784057841578425784357844578455784657847578485784957850578515785257853578545785557856578575785857859578605786157862578635786457865578665786757868578695787057871578725787357874578755787657877578785787957880578815788257883578845788557886578875788857889578905789157892578935789457895578965789757898578995790057901579025790357904579055790657907579085790957910579115791257913579145791557916579175791857919579205792157922579235792457925579265792757928579295793057931579325793357934579355793657937579385793957940579415794257943579445794557946579475794857949579505795157952579535795457955579565795757958579595796057961579625796357964579655796657967579685796957970579715797257973579745797557976579775797857979579805798157982579835798457985579865798757988579895799057991579925799357994579955799657997579985799958000580015800258003580045800558006580075800858009580105801158012580135801458015580165801758018580195802058021580225802358024580255802658027580285802958030580315803258033580345803558036580375803858039580405804158042580435804458045580465804758048580495805058051580525805358054580555805658057580585805958060580615806258063580645806558066580675806858069580705807158072580735807458075580765807758078580795808058081580825808358084580855808658087580885808958090580915809258093580945809558096580975809858099581005810158102581035810458105581065810758108581095811058111581125811358114581155811658117581185811958120581215812258123581245812558126581275812858129581305813158132581335813458135581365813758138581395814058141581425814358144581455814658147581485814958150581515815258153581545815558156581575815858159581605816158162581635816458165581665816758168581695817058171581725817358174581755817658177581785817958180581815818258183581845818558186581875818858189581905819158192581935819458195581965819758198581995820058201582025820358204582055820658207582085820958210582115821258213582145821558216582175821858219582205822158222582235822458225582265822758228582295823058231582325823358234582355823658237582385823958240582415824258243582445824558246582475824858249582505825158252582535825458255582565825758258582595826058261582625826358264582655826658267582685826958270582715827258273582745827558276582775827858279582805828158282582835828458285582865828758288582895829058291582925829358294582955829658297582985829958300583015830258303583045830558306583075830858309583105831158312583135831458315583165831758318583195832058321583225832358324583255832658327583285832958330583315833258333583345833558336583375833858339583405834158342583435834458345583465834758348583495835058351583525835358354583555835658357583585835958360583615836258363583645836558366583675836858369583705837158372583735837458375583765837758378583795838058381583825838358384583855838658387583885838958390583915839258393583945839558396583975839858399584005840158402584035840458405584065840758408584095841058411584125841358414584155841658417584185841958420584215842258423584245842558426584275842858429584305843158432584335843458435584365843758438584395844058441584425844358444584455844658447584485844958450584515845258453584545845558456584575845858459584605846158462584635846458465584665846758468584695847058471584725847358474584755847658477584785847958480584815848258483584845848558486584875848858489584905849158492584935849458495584965849758498584995850058501585025850358504585055850658507585085850958510585115851258513585145851558516585175851858519585205852158522585235852458525585265852758528585295853058531585325853358534585355853658537585385853958540585415854258543585445854558546585475854858549585505855158552585535855458555585565855758558585595856058561585625856358564585655856658567585685856958570585715857258573585745857558576585775857858579585805858158582585835858458585585865858758588585895859058591585925859358594585955859658597585985859958600586015860258603586045860558606586075860858609586105861158612586135861458615586165861758618586195862058621586225862358624586255862658627586285862958630586315863258633586345863558636586375863858639586405864158642586435864458645586465864758648586495865058651586525865358654586555865658657586585865958660586615866258663586645866558666586675866858669586705867158672586735867458675586765867758678586795868058681586825868358684586855868658687586885868958690586915869258693586945869558696586975869858699587005870158702587035870458705587065870758708587095871058711587125871358714587155871658717587185871958720587215872258723587245872558726587275872858729587305873158732587335873458735587365873758738587395874058741587425874358744587455874658747587485874958750587515875258753587545875558756587575875858759587605876158762587635876458765587665876758768587695877058771587725877358774587755877658777587785877958780587815878258783587845878558786587875878858789587905879158792587935879458795587965879758798587995880058801588025880358804588055880658807588085880958810588115881258813588145881558816588175881858819588205882158822588235882458825588265882758828588295883058831588325883358834588355883658837588385883958840588415884258843588445884558846588475884858849588505885158852588535885458855588565885758858588595886058861588625886358864588655886658867588685886958870588715887258873588745887558876588775887858879588805888158882588835888458885588865888758888588895889058891588925889358894588955889658897588985889958900589015890258903589045890558906589075890858909589105891158912589135891458915589165891758918589195892058921589225892358924589255892658927589285892958930589315893258933589345893558936589375893858939589405894158942589435894458945589465894758948589495895058951589525895358954589555895658957589585895958960589615896258963589645896558966589675896858969589705897158972589735897458975589765897758978589795898058981589825898358984589855898658987589885898958990589915899258993589945899558996589975899858999590005900159002590035900459005590065900759008590095901059011590125901359014590155901659017590185901959020590215902259023590245902559026590275902859029590305903159032590335903459035590365903759038590395904059041590425904359044590455904659047590485904959050590515905259053590545905559056590575905859059590605906159062590635906459065590665906759068590695907059071590725907359074590755907659077590785907959080590815908259083590845908559086590875908859089590905909159092590935909459095590965909759098590995910059101591025910359104591055910659107591085910959110591115911259113591145911559116591175911859119591205912159122591235912459125591265912759128591295913059131591325913359134591355913659137591385913959140591415914259143591445914559146591475914859149591505915159152591535915459155591565915759158591595916059161591625916359164591655916659167591685916959170591715917259173591745917559176591775917859179591805918159182591835918459185591865918759188591895919059191591925919359194591955919659197591985919959200592015920259203592045920559206592075920859209592105921159212592135921459215592165921759218592195922059221592225922359224592255922659227592285922959230592315923259233592345923559236592375923859239592405924159242592435924459245592465924759248592495925059251592525925359254592555925659257592585925959260592615926259263592645926559266592675926859269592705927159272592735927459275592765927759278592795928059281592825928359284592855928659287592885928959290592915929259293592945929559296592975929859299593005930159302593035930459305593065930759308593095931059311593125931359314593155931659317593185931959320593215932259323593245932559326593275932859329593305933159332593335933459335593365933759338593395934059341593425934359344593455934659347593485934959350593515935259353593545935559356593575935859359593605936159362593635936459365593665936759368593695937059371593725937359374593755937659377593785937959380593815938259383593845938559386593875938859389593905939159392593935939459395593965939759398593995940059401594025940359404594055940659407594085940959410594115941259413594145941559416594175941859419594205942159422594235942459425594265942759428594295943059431594325943359434594355943659437594385943959440594415944259443594445944559446594475944859449594505945159452594535945459455594565945759458594595946059461594625946359464594655946659467594685946959470594715947259473594745947559476594775947859479594805948159482594835948459485594865948759488594895949059491594925949359494594955949659497594985949959500595015950259503595045950559506595075950859509595105951159512595135951459515595165951759518595195952059521595225952359524595255952659527595285952959530595315953259533595345953559536595375953859539595405954159542595435954459545595465954759548595495955059551595525955359554595555955659557595585955959560595615956259563595645956559566595675956859569595705957159572595735957459575595765957759578595795958059581595825958359584595855958659587595885958959590595915959259593595945959559596595975959859599596005960159602596035960459605596065960759608596095961059611596125961359614596155961659617596185961959620596215962259623596245962559626596275962859629596305963159632596335963459635596365963759638596395964059641596425964359644596455964659647596485964959650596515965259653596545965559656596575965859659596605966159662596635966459665596665966759668596695967059671596725967359674596755967659677596785967959680596815968259683596845968559686596875968859689596905969159692596935969459695596965969759698596995970059701597025970359704597055970659707597085970959710597115971259713597145971559716597175971859719597205972159722597235972459725597265972759728597295973059731597325973359734597355973659737597385973959740597415974259743597445974559746597475974859749597505975159752597535975459755597565975759758597595976059761597625976359764597655976659767597685976959770597715977259773597745977559776597775977859779597805978159782597835978459785597865978759788597895979059791597925979359794597955979659797597985979959800598015980259803598045980559806598075980859809598105981159812598135981459815598165981759818598195982059821598225982359824598255982659827598285982959830598315983259833598345983559836598375983859839598405984159842598435984459845598465984759848598495985059851598525985359854598555985659857598585985959860598615986259863598645986559866598675986859869598705987159872598735987459875598765987759878598795988059881598825988359884598855988659887598885988959890598915989259893598945989559896598975989859899599005990159902599035990459905599065990759908599095991059911599125991359914599155991659917599185991959920599215992259923599245992559926599275992859929599305993159932599335993459935599365993759938599395994059941599425994359944599455994659947599485994959950599515995259953599545995559956599575995859959599605996159962599635996459965599665996759968599695997059971599725997359974599755997659977599785997959980599815998259983599845998559986599875998859989599905999159992599935999459995599965999759998599996000060001600026000360004600056000660007600086000960010600116001260013600146001560016600176001860019600206002160022600236002460025600266002760028600296003060031600326003360034600356003660037600386003960040600416004260043600446004560046600476004860049600506005160052600536005460055600566005760058600596006060061600626006360064600656006660067600686006960070600716007260073600746007560076600776007860079600806008160082600836008460085600866008760088600896009060091600926009360094600956009660097600986009960100601016010260103601046010560106601076010860109601106011160112601136011460115601166011760118601196012060121601226012360124601256012660127601286012960130601316013260133601346013560136601376013860139601406014160142601436014460145601466014760148601496015060151601526015360154601556015660157601586015960160601616016260163601646016560166601676016860169601706017160172601736017460175601766017760178601796018060181601826018360184601856018660187601886018960190601916019260193601946019560196601976019860199602006020160202602036020460205602066020760208602096021060211602126021360214602156021660217602186021960220602216022260223602246022560226602276022860229602306023160232602336023460235602366023760238602396024060241602426024360244602456024660247602486024960250602516025260253602546025560256602576025860259602606026160262602636026460265602666026760268602696027060271602726027360274602756027660277602786027960280602816028260283602846028560286602876028860289602906029160292602936029460295602966029760298602996030060301603026030360304603056030660307603086030960310603116031260313603146031560316603176031860319603206032160322603236032460325603266032760328603296033060331603326033360334603356033660337603386033960340603416034260343603446034560346603476034860349603506035160352603536035460355603566035760358603596036060361603626036360364603656036660367603686036960370603716037260373603746037560376603776037860379603806038160382603836038460385603866038760388603896039060391603926039360394603956039660397603986039960400604016040260403604046040560406604076040860409604106041160412604136041460415604166041760418604196042060421604226042360424604256042660427604286042960430604316043260433604346043560436604376043860439604406044160442604436044460445604466044760448604496045060451604526045360454604556045660457604586045960460604616046260463604646046560466604676046860469604706047160472604736047460475604766047760478604796048060481604826048360484604856048660487604886048960490604916049260493604946049560496604976049860499605006050160502605036050460505605066050760508605096051060511605126051360514605156051660517605186051960520605216052260523605246052560526605276052860529605306053160532605336053460535605366053760538605396054060541605426054360544605456054660547605486054960550605516055260553605546055560556605576055860559605606056160562605636056460565605666056760568605696057060571605726057360574605756057660577605786057960580605816058260583605846058560586605876058860589605906059160592605936059460595605966059760598605996060060601606026060360604606056060660607606086060960610606116061260613606146061560616606176061860619606206062160622606236062460625606266062760628606296063060631606326063360634606356063660637606386063960640606416064260643606446064560646606476064860649606506065160652606536065460655606566065760658606596066060661606626066360664606656066660667606686066960670606716067260673606746067560676606776067860679606806068160682606836068460685606866068760688606896069060691606926069360694606956069660697606986069960700607016070260703607046070560706607076070860709607106071160712607136071460715607166071760718607196072060721607226072360724607256072660727607286072960730607316073260733607346073560736607376073860739607406074160742607436074460745607466074760748607496075060751607526075360754607556075660757607586075960760607616076260763607646076560766607676076860769607706077160772607736077460775607766077760778607796078060781607826078360784607856078660787607886078960790607916079260793607946079560796607976079860799608006080160802608036080460805608066080760808608096081060811608126081360814608156081660817608186081960820608216082260823608246082560826608276082860829608306083160832608336083460835608366083760838608396084060841608426084360844608456084660847608486084960850608516085260853608546085560856608576085860859608606086160862608636086460865608666086760868608696087060871608726087360874608756087660877608786087960880608816088260883608846088560886608876088860889608906089160892608936089460895608966089760898608996090060901609026090360904609056090660907609086090960910609116091260913609146091560916609176091860919609206092160922609236092460925609266092760928609296093060931609326093360934609356093660937609386093960940609416094260943609446094560946609476094860949609506095160952609536095460955609566095760958609596096060961609626096360964609656096660967609686096960970609716097260973609746097560976609776097860979609806098160982609836098460985609866098760988609896099060991609926099360994609956099660997609986099961000610016100261003610046100561006610076100861009610106101161012610136101461015610166101761018610196102061021610226102361024610256102661027610286102961030610316103261033610346103561036610376103861039610406104161042610436104461045610466104761048610496105061051610526105361054610556105661057610586105961060610616106261063610646106561066610676106861069610706107161072610736107461075610766107761078610796108061081610826108361084610856108661087610886108961090610916109261093610946109561096610976109861099611006110161102611036110461105611066110761108611096111061111611126111361114611156111661117611186111961120611216112261123611246112561126611276112861129611306113161132611336113461135611366113761138611396114061141611426114361144611456114661147611486114961150611516115261153611546115561156611576115861159611606116161162611636116461165611666116761168611696117061171611726117361174611756117661177611786117961180611816118261183611846118561186611876118861189611906119161192611936119461195611966119761198611996120061201612026120361204612056120661207612086120961210612116121261213612146121561216612176121861219612206122161222612236122461225612266122761228612296123061231612326123361234612356123661237612386123961240612416124261243612446124561246612476124861249612506125161252612536125461255612566125761258612596126061261612626126361264612656126661267612686126961270612716127261273612746127561276612776127861279612806128161282612836128461285612866128761288612896129061291612926129361294612956129661297612986129961300613016130261303613046130561306613076130861309613106131161312613136131461315613166131761318613196132061321613226132361324613256132661327613286132961330613316133261333613346133561336613376133861339613406134161342613436134461345613466134761348613496135061351613526135361354613556135661357613586135961360613616136261363613646136561366613676136861369613706137161372613736137461375613766137761378613796138061381613826138361384613856138661387613886138961390613916139261393613946139561396613976139861399614006140161402614036140461405614066140761408614096141061411614126141361414614156141661417614186141961420614216142261423614246142561426614276142861429614306143161432614336143461435614366143761438614396144061441614426144361444614456144661447614486144961450614516145261453614546145561456614576145861459614606146161462614636146461465614666146761468614696147061471614726147361474614756147661477614786147961480614816148261483614846148561486614876148861489614906149161492614936149461495614966149761498614996150061501615026150361504615056150661507615086150961510615116151261513615146151561516615176151861519615206152161522615236152461525615266152761528615296153061531615326153361534615356153661537615386153961540615416154261543615446154561546615476154861549615506155161552615536155461555615566155761558615596156061561615626156361564615656156661567615686156961570615716157261573615746157561576615776157861579615806158161582615836158461585615866158761588615896159061591615926159361594615956159661597615986159961600616016160261603616046160561606616076160861609616106161161612616136161461615616166161761618616196162061621616226162361624616256162661627616286162961630616316163261633616346163561636616376163861639616406164161642616436164461645616466164761648616496165061651616526165361654616556165661657616586165961660616616166261663616646166561666616676166861669616706167161672616736167461675616766167761678616796168061681616826168361684616856168661687616886168961690616916169261693616946169561696616976169861699617006170161702617036170461705617066170761708617096171061711617126171361714617156171661717617186171961720617216172261723617246172561726617276172861729617306173161732617336173461735617366173761738617396174061741617426174361744617456174661747617486174961750617516175261753617546175561756617576175861759617606176161762617636176461765617666176761768617696177061771617726177361774617756177661777617786177961780617816178261783617846178561786617876178861789617906179161792617936179461795617966179761798617996180061801618026180361804618056180661807618086180961810618116181261813618146181561816618176181861819618206182161822618236182461825618266182761828618296183061831618326183361834618356183661837618386183961840618416184261843618446184561846618476184861849618506185161852618536185461855618566185761858618596186061861618626186361864618656186661867618686186961870618716187261873618746187561876618776187861879618806188161882618836188461885618866188761888618896189061891618926189361894618956189661897618986189961900619016190261903619046190561906619076190861909619106191161912619136191461915619166191761918619196192061921619226192361924619256192661927619286192961930619316193261933619346193561936619376193861939619406194161942619436194461945619466194761948619496195061951619526195361954619556195661957619586195961960619616196261963619646196561966619676196861969619706197161972619736197461975619766197761978619796198061981619826198361984619856198661987619886198961990619916199261993619946199561996619976199861999620006200162002620036200462005620066200762008620096201062011620126201362014620156201662017620186201962020620216202262023620246202562026620276202862029620306203162032620336203462035620366203762038620396204062041620426204362044620456204662047620486204962050620516205262053620546205562056620576205862059620606206162062620636206462065620666206762068620696207062071620726207362074620756207662077620786207962080620816208262083620846208562086620876208862089620906209162092620936209462095620966209762098620996210062101621026210362104621056210662107621086210962110621116211262113621146211562116621176211862119621206212162122621236212462125621266212762128621296213062131621326213362134621356213662137621386213962140621416214262143621446214562146621476214862149621506215162152621536215462155621566215762158621596216062161621626216362164621656216662167621686216962170621716217262173621746217562176621776217862179621806218162182621836218462185621866218762188621896219062191621926219362194621956219662197621986219962200622016220262203622046220562206622076220862209622106221162212622136221462215622166221762218622196222062221622226222362224622256222662227622286222962230622316223262233622346223562236622376223862239622406224162242622436224462245622466224762248622496225062251622526225362254622556225662257622586225962260622616226262263622646226562266622676226862269622706227162272622736227462275622766227762278622796228062281622826228362284622856228662287622886228962290622916229262293622946229562296622976229862299623006230162302623036230462305623066230762308623096231062311623126231362314623156231662317623186231962320623216232262323623246232562326623276232862329623306233162332623336233462335623366233762338623396234062341623426234362344623456234662347623486234962350623516235262353623546235562356623576235862359623606236162362623636236462365623666236762368623696237062371623726237362374623756237662377623786237962380623816238262383623846238562386623876238862389623906239162392623936239462395623966239762398623996240062401624026240362404624056240662407624086240962410624116241262413624146241562416624176241862419624206242162422624236242462425624266242762428624296243062431624326243362434624356243662437624386243962440624416244262443624446244562446624476244862449624506245162452624536245462455624566245762458624596246062461624626246362464624656246662467624686246962470624716247262473624746247562476624776247862479624806248162482624836248462485624866248762488624896249062491624926249362494624956249662497624986249962500625016250262503625046250562506625076250862509625106251162512625136251462515625166251762518625196252062521625226252362524625256252662527625286252962530625316253262533625346253562536625376253862539625406254162542625436254462545625466254762548625496255062551625526255362554625556255662557625586255962560625616256262563625646256562566625676256862569625706257162572625736257462575625766257762578625796258062581625826258362584625856258662587625886258962590625916259262593625946259562596625976259862599626006260162602626036260462605626066260762608626096261062611626126261362614626156261662617626186261962620626216262262623626246262562626626276262862629626306263162632626336263462635626366263762638626396264062641626426264362644626456264662647626486264962650626516265262653626546265562656626576265862659626606266162662626636266462665626666266762668626696267062671626726267362674626756267662677626786267962680626816268262683626846268562686626876268862689626906269162692626936269462695626966269762698626996270062701627026270362704627056270662707627086270962710627116271262713627146271562716627176271862719627206272162722627236272462725627266272762728627296273062731627326273362734627356273662737627386273962740627416274262743627446274562746627476274862749627506275162752627536275462755627566275762758627596276062761627626276362764627656276662767627686276962770627716277262773627746277562776627776277862779627806278162782627836278462785627866278762788627896279062791627926279362794627956279662797627986279962800628016280262803628046280562806628076280862809628106281162812628136281462815628166281762818628196282062821628226282362824628256282662827628286282962830628316283262833628346283562836628376283862839628406284162842628436284462845628466284762848628496285062851628526285362854628556285662857628586285962860628616286262863628646286562866628676286862869628706287162872628736287462875628766287762878628796288062881628826288362884628856288662887628886288962890628916289262893628946289562896628976289862899629006290162902629036290462905629066290762908629096291062911629126291362914629156291662917629186291962920629216292262923629246292562926629276292862929629306293162932629336293462935629366293762938629396294062941629426294362944629456294662947629486294962950629516295262953629546295562956629576295862959629606296162962629636296462965629666296762968629696297062971629726297362974629756297662977629786297962980629816298262983629846298562986629876298862989629906299162992629936299462995629966299762998629996300063001630026300363004630056300663007630086300963010630116301263013630146301563016630176301863019630206302163022630236302463025630266302763028630296303063031630326303363034630356303663037630386303963040630416304263043630446304563046630476304863049630506305163052630536305463055630566305763058630596306063061630626306363064630656306663067630686306963070630716307263073630746307563076630776307863079630806308163082630836308463085630866308763088630896309063091630926309363094630956309663097630986309963100631016310263103631046310563106631076310863109631106311163112631136311463115631166311763118631196312063121631226312363124631256312663127631286312963130631316313263133631346313563136631376313863139631406314163142631436314463145631466314763148631496315063151631526315363154631556315663157631586315963160631616316263163631646316563166631676316863169631706317163172631736317463175631766317763178631796318063181631826318363184631856318663187631886318963190631916319263193631946319563196631976319863199632006320163202632036320463205632066320763208632096321063211632126321363214632156321663217632186321963220632216322263223632246322563226632276322863229632306323163232632336323463235632366323763238632396324063241632426324363244632456324663247632486324963250632516325263253632546325563256632576325863259632606326163262632636326463265632666326763268632696327063271632726327363274632756327663277632786327963280632816328263283632846328563286632876328863289632906329163292632936329463295632966329763298632996330063301633026330363304633056330663307633086330963310633116331263313633146331563316633176331863319633206332163322633236332463325633266332763328633296333063331633326333363334633356333663337633386333963340633416334263343633446334563346633476334863349633506335163352633536335463355633566335763358633596336063361633626336363364633656336663367633686336963370633716337263373633746337563376633776337863379633806338163382633836338463385633866338763388633896339063391633926339363394633956339663397633986339963400634016340263403634046340563406634076340863409634106341163412634136341463415634166341763418634196342063421634226342363424634256342663427634286342963430634316343263433634346343563436634376343863439634406344163442634436344463445634466344763448634496345063451634526345363454634556345663457634586345963460634616346263463634646346563466634676346863469634706347163472634736347463475634766347763478634796348063481634826348363484634856348663487634886348963490634916349263493634946349563496634976349863499635006350163502635036350463505635066350763508635096351063511635126351363514635156351663517635186351963520635216352263523635246352563526635276352863529635306353163532635336353463535635366353763538635396354063541635426354363544635456354663547635486354963550635516355263553635546355563556635576355863559635606356163562635636356463565635666356763568635696357063571635726357363574635756357663577635786357963580635816358263583635846358563586635876358863589635906359163592635936359463595635966359763598635996360063601636026360363604636056360663607636086360963610636116361263613636146361563616636176361863619636206362163622636236362463625636266362763628636296363063631636326363363634636356363663637636386363963640636416364263643636446364563646636476364863649636506365163652636536365463655636566365763658636596366063661636626366363664636656366663667636686366963670636716367263673636746367563676636776367863679636806368163682636836368463685636866368763688636896369063691636926369363694636956369663697636986369963700637016370263703637046370563706637076370863709637106371163712637136371463715637166371763718637196372063721637226372363724637256372663727637286372963730637316373263733637346373563736637376373863739637406374163742637436374463745637466374763748637496375063751637526375363754637556375663757637586375963760637616376263763637646376563766637676376863769637706377163772637736377463775637766377763778637796378063781637826378363784637856378663787637886378963790637916379263793637946379563796637976379863799638006380163802638036380463805638066380763808638096381063811638126381363814638156381663817638186381963820638216382263823638246382563826638276382863829638306383163832638336383463835638366383763838638396384063841638426384363844638456384663847638486384963850638516385263853638546385563856638576385863859638606386163862638636386463865638666386763868638696387063871638726387363874638756387663877638786387963880638816388263883638846388563886638876388863889638906389163892638936389463895638966389763898638996390063901639026390363904639056390663907639086390963910639116391263913639146391563916639176391863919639206392163922639236392463925639266392763928639296393063931639326393363934639356393663937639386393963940639416394263943639446394563946639476394863949639506395163952639536395463955639566395763958639596396063961639626396363964639656396663967639686396963970639716397263973639746397563976639776397863979639806398163982639836398463985639866398763988639896399063991639926399363994639956399663997639986399964000640016400264003640046400564006640076400864009640106401164012640136401464015640166401764018640196402064021640226402364024640256402664027640286402964030640316403264033640346403564036640376403864039640406404164042640436404464045640466404764048640496405064051640526405364054640556405664057640586405964060640616406264063640646406564066640676406864069640706407164072640736407464075640766407764078640796408064081640826408364084640856408664087640886408964090640916409264093640946409564096640976409864099641006410164102641036410464105641066410764108641096411064111641126411364114641156411664117641186411964120641216412264123641246412564126641276412864129641306413164132641336413464135641366413764138641396414064141641426414364144641456414664147641486414964150641516415264153641546415564156641576415864159641606416164162641636416464165641666416764168641696417064171641726417364174641756417664177641786417964180641816418264183641846418564186641876418864189641906419164192641936419464195641966419764198641996420064201642026420364204642056420664207642086420964210642116421264213642146421564216642176421864219642206422164222642236422464225642266422764228642296423064231642326423364234642356423664237642386423964240642416424264243642446424564246642476424864249642506425164252642536425464255642566425764258642596426064261642626426364264642656426664267642686426964270642716427264273642746427564276642776427864279642806428164282642836428464285642866428764288642896429064291642926429364294642956429664297642986429964300643016430264303643046430564306643076430864309643106431164312643136431464315643166431764318643196432064321643226432364324643256432664327643286432964330643316433264333643346433564336643376433864339643406434164342643436434464345643466434764348643496435064351643526435364354643556435664357643586435964360643616436264363643646436564366643676436864369643706437164372643736437464375643766437764378643796438064381643826438364384643856438664387643886438964390643916439264393643946439564396643976439864399644006440164402644036440464405644066440764408644096441064411644126441364414644156441664417644186441964420644216442264423644246442564426644276442864429644306443164432644336443464435644366443764438644396444064441644426444364444644456444664447644486444964450644516445264453644546445564456644576445864459644606446164462644636446464465644666446764468644696447064471644726447364474644756447664477644786447964480644816448264483644846448564486644876448864489644906449164492644936449464495644966449764498644996450064501645026450364504645056450664507645086450964510645116451264513645146451564516645176451864519645206452164522645236452464525645266452764528645296453064531645326453364534645356453664537645386453964540645416454264543645446454564546645476454864549645506455164552645536455464555645566455764558645596456064561645626456364564645656456664567645686456964570645716457264573645746457564576645776457864579645806458164582645836458464585645866458764588645896459064591645926459364594645956459664597645986459964600646016460264603646046460564606646076460864609646106461164612646136461464615646166461764618646196462064621646226462364624646256462664627646286462964630646316463264633646346463564636646376463864639646406464164642646436464464645646466464764648646496465064651646526465364654646556465664657646586465964660646616466264663646646466564666646676466864669646706467164672646736467464675646766467764678646796468064681646826468364684646856468664687646886468964690646916469264693646946469564696646976469864699647006470164702647036470464705647066470764708647096471064711647126471364714647156471664717647186471964720647216472264723647246472564726647276472864729647306473164732647336473464735647366473764738647396474064741647426474364744647456474664747647486474964750647516475264753647546475564756647576475864759647606476164762647636476464765647666476764768647696477064771647726477364774647756477664777647786477964780647816478264783647846478564786647876478864789647906479164792647936479464795647966479764798647996480064801648026480364804648056480664807648086480964810648116481264813648146481564816648176481864819648206482164822648236482464825648266482764828648296483064831648326483364834648356483664837648386483964840648416484264843648446484564846648476484864849648506485164852648536485464855648566485764858648596486064861648626486364864648656486664867648686486964870648716487264873648746487564876648776487864879648806488164882648836488464885648866488764888648896489064891648926489364894648956489664897648986489964900649016490264903649046490564906649076490864909649106491164912649136491464915649166491764918649196492064921649226492364924649256492664927649286492964930649316493264933649346493564936649376493864939649406494164942649436494464945649466494764948649496495064951649526495364954649556495664957649586495964960649616496264963649646496564966649676496864969649706497164972649736497464975649766497764978649796498064981649826498364984649856498664987649886498964990649916499264993649946499564996649976499864999650006500165002650036500465005650066500765008650096501065011650126501365014650156501665017650186501965020650216502265023650246502565026650276502865029650306503165032650336503465035650366503765038650396504065041650426504365044650456504665047650486504965050650516505265053650546505565056650576505865059650606506165062650636506465065650666506765068650696507065071650726507365074650756507665077650786507965080650816508265083650846508565086650876508865089650906509165092650936509465095650966509765098650996510065101651026510365104651056510665107651086510965110651116511265113651146511565116651176511865119651206512165122651236512465125651266512765128651296513065131651326513365134651356513665137651386513965140651416514265143651446514565146651476514865149651506515165152651536515465155651566515765158651596516065161651626516365164651656516665167651686516965170651716517265173651746517565176651776517865179651806518165182651836518465185651866518765188651896519065191651926519365194651956519665197651986519965200652016520265203652046520565206652076520865209652106521165212652136521465215652166521765218652196522065221652226522365224652256522665227652286522965230652316523265233652346523565236652376523865239652406524165242652436524465245652466524765248652496525065251652526525365254652556525665257652586525965260652616526265263652646526565266652676526865269652706527165272652736527465275652766527765278652796528065281652826528365284652856528665287652886528965290652916529265293652946529565296652976529865299653006530165302653036530465305653066530765308653096531065311653126531365314653156531665317653186531965320653216532265323653246532565326653276532865329653306533165332653336533465335653366533765338653396534065341653426534365344653456534665347653486534965350653516535265353653546535565356653576535865359653606536165362653636536465365653666536765368653696537065371653726537365374653756537665377653786537965380653816538265383653846538565386653876538865389653906539165392653936539465395653966539765398653996540065401654026540365404654056540665407654086540965410654116541265413654146541565416654176541865419654206542165422654236542465425654266542765428654296543065431654326543365434654356543665437654386543965440654416544265443654446544565446654476544865449654506545165452654536545465455654566545765458654596546065461654626546365464654656546665467654686546965470654716547265473654746547565476654776547865479654806548165482654836548465485654866548765488654896549065491654926549365494654956549665497654986549965500655016550265503655046550565506655076550865509655106551165512655136551465515655166551765518655196552065521655226552365524655256552665527655286552965530655316553265533655346553565536655376553865539655406554165542655436554465545655466554765548655496555065551655526555365554655556555665557655586555965560655616556265563655646556565566655676556865569655706557165572655736557465575655766557765578655796558065581655826558365584655856558665587655886558965590655916559265593655946559565596655976559865599656006560165602656036560465605656066560765608656096561065611656126561365614656156561665617656186561965620656216562265623656246562565626656276562865629656306563165632656336563465635656366563765638656396564065641656426564365644656456564665647656486564965650656516565265653656546565565656656576565865659656606566165662656636566465665656666566765668656696567065671656726567365674656756567665677656786567965680656816568265683656846568565686656876568865689656906569165692656936569465695656966569765698656996570065701657026570365704657056570665707657086570965710657116571265713657146571565716657176571865719657206572165722657236572465725657266572765728657296573065731657326573365734657356573665737657386573965740657416574265743657446574565746657476574865749657506575165752657536575465755657566575765758657596576065761657626576365764657656576665767657686576965770657716577265773657746577565776657776577865779657806578165782657836578465785657866578765788657896579065791657926579365794657956579665797657986579965800658016580265803658046580565806658076580865809658106581165812658136581465815658166581765818658196582065821658226582365824658256582665827658286582965830658316583265833658346583565836658376583865839658406584165842658436584465845658466584765848658496585065851658526585365854658556585665857658586585965860658616586265863658646586565866658676586865869658706587165872658736587465875658766587765878658796588065881658826588365884658856588665887658886588965890658916589265893658946589565896658976589865899659006590165902659036590465905659066590765908659096591065911659126591365914659156591665917659186591965920659216592265923659246592565926659276592865929659306593165932659336593465935659366593765938659396594065941659426594365944659456594665947659486594965950659516595265953659546595565956659576595865959659606596165962659636596465965659666596765968659696597065971659726597365974659756597665977659786597965980659816598265983659846598565986659876598865989659906599165992659936599465995659966599765998659996600066001660026600366004660056600666007660086600966010660116601266013660146601566016660176601866019660206602166022660236602466025660266602766028660296603066031660326603366034660356603666037660386603966040660416604266043660446604566046660476604866049660506605166052660536605466055660566605766058660596606066061660626606366064660656606666067660686606966070660716607266073660746607566076660776607866079660806608166082660836608466085660866608766088660896609066091660926609366094660956609666097660986609966100661016610266103661046610566106661076610866109661106611166112661136611466115661166611766118661196612066121661226612366124661256612666127661286612966130661316613266133661346613566136661376613866139661406614166142661436614466145661466614766148661496615066151661526615366154661556615666157661586615966160661616616266163661646616566166661676616866169661706617166172661736617466175661766617766178661796618066181661826618366184661856618666187661886618966190661916619266193661946619566196661976619866199662006620166202662036620466205662066620766208662096621066211662126621366214662156621666217662186621966220662216622266223662246622566226662276622866229662306623166232662336623466235662366623766238662396624066241662426624366244662456624666247662486624966250662516625266253662546625566256662576625866259662606626166262662636626466265662666626766268662696627066271662726627366274662756627666277662786627966280662816628266283662846628566286662876628866289662906629166292662936629466295662966629766298662996630066301663026630366304663056630666307663086630966310663116631266313663146631566316663176631866319663206632166322663236632466325663266632766328663296633066331663326633366334663356633666337663386633966340663416634266343663446634566346663476634866349663506635166352663536635466355663566635766358663596636066361663626636366364663656636666367663686636966370663716637266373663746637566376663776637866379663806638166382663836638466385663866638766388663896639066391663926639366394663956639666397663986639966400664016640266403664046640566406664076640866409664106641166412664136641466415664166641766418664196642066421664226642366424664256642666427664286642966430664316643266433664346643566436664376643866439664406644166442664436644466445664466644766448664496645066451664526645366454664556645666457664586645966460664616646266463664646646566466664676646866469664706647166472664736647466475664766647766478664796648066481664826648366484664856648666487664886648966490664916649266493664946649566496664976649866499665006650166502665036650466505665066650766508665096651066511665126651366514665156651666517665186651966520665216652266523665246652566526665276652866529665306653166532665336653466535665366653766538665396654066541665426654366544665456654666547665486654966550665516655266553665546655566556665576655866559665606656166562665636656466565665666656766568665696657066571665726657366574665756657666577665786657966580665816658266583665846658566586665876658866589665906659166592665936659466595665966659766598665996660066601666026660366604666056660666607666086660966610666116661266613666146661566616666176661866619666206662166622666236662466625666266662766628666296663066631666326663366634666356663666637666386663966640666416664266643666446664566646666476664866649666506665166652666536665466655666566665766658666596666066661666626666366664666656666666667666686666966670666716667266673666746667566676666776667866679666806668166682666836668466685666866668766688666896669066691666926669366694666956669666697666986669966700667016670266703667046670566706667076670866709667106671166712667136671466715667166671766718667196672066721667226672366724667256672666727667286672966730667316673266733667346673566736667376673866739667406674166742667436674466745667466674766748667496675066751667526675366754667556675666757667586675966760667616676266763667646676566766667676676866769667706677166772667736677466775667766677766778667796678066781667826678366784667856678666787667886678966790667916679266793667946679566796667976679866799668006680166802668036680466805668066680766808668096681066811668126681366814668156681666817668186681966820668216682266823668246682566826668276682866829668306683166832668336683466835668366683766838668396684066841668426684366844668456684666847668486684966850668516685266853668546685566856668576685866859668606686166862668636686466865668666686766868668696687066871668726687366874668756687666877668786687966880668816688266883668846688566886668876688866889668906689166892668936689466895668966689766898668996690066901669026690366904669056690666907669086690966910669116691266913669146691566916669176691866919669206692166922669236692466925669266692766928669296693066931669326693366934669356693666937669386693966940669416694266943669446694566946669476694866949669506695166952669536695466955669566695766958669596696066961669626696366964669656696666967669686696966970669716697266973669746697566976669776697866979669806698166982669836698466985669866698766988669896699066991669926699366994669956699666997669986699967000670016700267003670046700567006670076700867009670106701167012670136701467015670166701767018670196702067021670226702367024670256702667027670286702967030670316703267033670346703567036670376703867039670406704167042670436704467045670466704767048670496705067051670526705367054670556705667057670586705967060670616706267063670646706567066670676706867069670706707167072670736707467075670766707767078670796708067081670826708367084670856708667087670886708967090670916709267093670946709567096670976709867099671006710167102671036710467105671066710767108671096711067111671126711367114671156711667117671186711967120671216712267123671246712567126671276712867129671306713167132671336713467135671366713767138671396714067141671426714367144671456714667147671486714967150671516715267153671546715567156671576715867159671606716167162671636716467165671666716767168671696717067171671726717367174671756717667177671786717967180671816718267183671846718567186671876718867189671906719167192671936719467195671966719767198671996720067201672026720367204672056720667207672086720967210672116721267213672146721567216672176721867219672206722167222672236722467225672266722767228672296723067231672326723367234672356723667237672386723967240672416724267243672446724567246672476724867249672506725167252672536725467255672566725767258672596726067261672626726367264672656726667267672686726967270672716727267273672746727567276672776727867279672806728167282672836728467285672866728767288672896729067291672926729367294672956729667297672986729967300673016730267303673046730567306673076730867309673106731167312673136731467315673166731767318673196732067321673226732367324673256732667327673286732967330673316733267333673346733567336673376733867339673406734167342673436734467345673466734767348673496735067351673526735367354673556735667357673586735967360673616736267363673646736567366673676736867369673706737167372673736737467375673766737767378673796738067381673826738367384673856738667387673886738967390673916739267393673946739567396673976739867399674006740167402674036740467405674066740767408674096741067411674126741367414674156741667417674186741967420674216742267423674246742567426674276742867429674306743167432674336743467435674366743767438674396744067441674426744367444674456744667447674486744967450674516745267453674546745567456674576745867459674606746167462674636746467465674666746767468674696747067471674726747367474674756747667477674786747967480674816748267483674846748567486674876748867489674906749167492674936749467495674966749767498674996750067501675026750367504675056750667507675086750967510675116751267513675146751567516675176751867519675206752167522675236752467525675266752767528675296753067531675326753367534675356753667537675386753967540675416754267543675446754567546675476754867549675506755167552675536755467555675566755767558675596756067561675626756367564675656756667567675686756967570675716757267573675746757567576675776757867579675806758167582675836758467585675866758767588675896759067591675926759367594675956759667597675986759967600676016760267603676046760567606676076760867609676106761167612676136761467615676166761767618676196762067621676226762367624676256762667627676286762967630676316763267633676346763567636676376763867639676406764167642676436764467645676466764767648676496765067651676526765367654676556765667657676586765967660676616766267663676646766567666676676766867669676706767167672676736767467675676766767767678676796768067681676826768367684676856768667687676886768967690676916769267693676946769567696676976769867699677006770167702677036770467705677066770767708677096771067711677126771367714677156771667717677186771967720677216772267723677246772567726677276772867729677306773167732677336773467735677366773767738677396774067741677426774367744677456774667747677486774967750677516775267753677546775567756677576775867759677606776167762677636776467765677666776767768677696777067771677726777367774677756777667777677786777967780677816778267783677846778567786677876778867789677906779167792677936779467795677966779767798677996780067801678026780367804678056780667807678086780967810678116781267813678146781567816678176781867819678206782167822678236782467825678266782767828678296783067831678326783367834678356783667837678386783967840678416784267843678446784567846678476784867849678506785167852678536785467855678566785767858678596786067861678626786367864678656786667867678686786967870678716787267873678746787567876678776787867879678806788167882678836788467885678866788767888678896789067891678926789367894678956789667897678986789967900679016790267903679046790567906679076790867909679106791167912679136791467915679166791767918679196792067921679226792367924679256792667927679286792967930679316793267933679346793567936679376793867939679406794167942679436794467945679466794767948679496795067951679526795367954679556795667957679586795967960679616796267963679646796567966679676796867969679706797167972679736797467975679766797767978679796798067981679826798367984679856798667987679886798967990679916799267993679946799567996679976799867999680006800168002680036800468005680066800768008680096801068011680126801368014680156801668017680186801968020680216802268023680246802568026680276802868029680306803168032680336803468035680366803768038680396804068041680426804368044680456804668047680486804968050680516805268053680546805568056680576805868059680606806168062680636806468065680666806768068680696807068071680726807368074680756807668077680786807968080680816808268083680846808568086680876808868089680906809168092680936809468095680966809768098680996810068101681026810368104681056810668107681086810968110681116811268113681146811568116681176811868119681206812168122681236812468125681266812768128681296813068131681326813368134681356813668137681386813968140681416814268143681446814568146681476814868149681506815168152681536815468155681566815768158681596816068161681626816368164681656816668167681686816968170681716817268173681746817568176681776817868179681806818168182681836818468185681866818768188681896819068191681926819368194681956819668197681986819968200682016820268203682046820568206682076820868209682106821168212682136821468215682166821768218682196822068221682226822368224682256822668227682286822968230682316823268233682346823568236682376823868239682406824168242682436824468245682466824768248682496825068251682526825368254682556825668257682586825968260682616826268263682646826568266682676826868269682706827168272682736827468275682766827768278682796828068281682826828368284682856828668287682886828968290682916829268293682946829568296682976829868299683006830168302683036830468305683066830768308683096831068311683126831368314683156831668317683186831968320683216832268323683246832568326683276832868329683306833168332683336833468335683366833768338683396834068341683426834368344683456834668347683486834968350683516835268353683546835568356683576835868359683606836168362683636836468365683666836768368683696837068371683726837368374683756837668377683786837968380683816838268383683846838568386683876838868389683906839168392683936839468395683966839768398683996840068401684026840368404684056840668407684086840968410684116841268413684146841568416684176841868419684206842168422684236842468425684266842768428684296843068431684326843368434684356843668437684386843968440684416844268443684446844568446684476844868449684506845168452684536845468455684566845768458684596846068461684626846368464684656846668467684686846968470684716847268473684746847568476684776847868479684806848168482684836848468485684866848768488684896849068491684926849368494684956849668497684986849968500685016850268503685046850568506685076850868509685106851168512685136851468515685166851768518685196852068521685226852368524685256852668527685286852968530685316853268533685346853568536685376853868539685406854168542685436854468545685466854768548685496855068551685526855368554685556855668557685586855968560685616856268563685646856568566685676856868569685706857168572685736857468575685766857768578685796858068581685826858368584685856858668587685886858968590685916859268593685946859568596685976859868599686006860168602686036860468605686066860768608686096861068611686126861368614686156861668617686186861968620686216862268623686246862568626686276862868629686306863168632686336863468635686366863768638686396864068641686426864368644686456864668647686486864968650686516865268653686546865568656686576865868659686606866168662686636866468665686666866768668686696867068671686726867368674686756867668677686786867968680686816868268683686846868568686686876868868689686906869168692686936869468695686966869768698686996870068701687026870368704687056870668707687086870968710687116871268713687146871568716687176871868719687206872168722687236872468725687266872768728687296873068731687326873368734687356873668737687386873968740687416874268743687446874568746687476874868749687506875168752687536875468755687566875768758687596876068761687626876368764687656876668767687686876968770687716877268773687746877568776687776877868779687806878168782687836878468785687866878768788687896879068791687926879368794687956879668797687986879968800688016880268803688046880568806688076880868809688106881168812688136881468815688166881768818688196882068821688226882368824688256882668827688286882968830688316883268833688346883568836688376883868839688406884168842688436884468845688466884768848688496885068851688526885368854688556885668857688586885968860688616886268863688646886568866688676886868869688706887168872688736887468875688766887768878688796888068881688826888368884688856888668887688886888968890688916889268893688946889568896688976889868899689006890168902689036890468905689066890768908689096891068911689126891368914689156891668917689186891968920689216892268923689246892568926689276892868929689306893168932689336893468935689366893768938689396894068941689426894368944689456894668947689486894968950689516895268953689546895568956689576895868959689606896168962689636896468965689666896768968689696897068971689726897368974689756897668977689786897968980689816898268983689846898568986689876898868989689906899168992689936899468995689966899768998689996900069001690026900369004690056900669007690086900969010690116901269013690146901569016690176901869019690206902169022690236902469025690266902769028690296903069031690326903369034690356903669037690386903969040690416904269043690446904569046690476904869049690506905169052690536905469055690566905769058690596906069061690626906369064690656906669067690686906969070690716907269073690746907569076690776907869079690806908169082690836908469085690866908769088690896909069091690926909369094690956909669097690986909969100691016910269103691046910569106691076910869109691106911169112691136911469115691166911769118691196912069121691226912369124691256912669127691286912969130691316913269133691346913569136691376913869139691406914169142691436914469145691466914769148691496915069151691526915369154691556915669157691586915969160691616916269163691646916569166691676916869169691706917169172691736917469175691766917769178691796918069181691826918369184691856918669187691886918969190691916919269193691946919569196691976919869199692006920169202692036920469205692066920769208692096921069211692126921369214692156921669217692186921969220692216922269223692246922569226692276922869229692306923169232692336923469235692366923769238692396924069241692426924369244692456924669247692486924969250692516925269253692546925569256692576925869259692606926169262692636926469265692666926769268692696927069271692726927369274692756927669277692786927969280692816928269283692846928569286692876928869289692906929169292692936929469295692966929769298692996930069301693026930369304693056930669307693086930969310693116931269313693146931569316693176931869319693206932169322693236932469325693266932769328693296933069331693326933369334693356933669337693386933969340693416934269343693446934569346693476934869349693506935169352693536935469355693566935769358693596936069361693626936369364693656936669367693686936969370693716937269373693746937569376693776937869379693806938169382693836938469385693866938769388693896939069391693926939369394693956939669397693986939969400694016940269403694046940569406694076940869409694106941169412694136941469415694166941769418694196942069421694226942369424694256942669427694286942969430694316943269433694346943569436694376943869439694406944169442694436944469445694466944769448694496945069451694526945369454694556945669457694586945969460694616946269463694646946569466694676946869469694706947169472694736947469475694766947769478694796948069481694826948369484694856948669487694886948969490694916949269493694946949569496694976949869499695006950169502695036950469505695066950769508695096951069511695126951369514695156951669517695186951969520695216952269523695246952569526695276952869529695306953169532695336953469535695366953769538695396954069541695426954369544695456954669547695486954969550695516955269553695546955569556695576955869559695606956169562695636956469565695666956769568695696957069571695726957369574695756957669577695786957969580695816958269583695846958569586695876958869589695906959169592695936959469595695966959769598695996960069601696026960369604696056960669607696086960969610696116961269613696146961569616696176961869619696206962169622696236962469625696266962769628696296963069631696326963369634696356963669637696386963969640696416964269643696446964569646696476964869649696506965169652696536965469655696566965769658696596966069661696626966369664696656966669667696686966969670696716967269673696746967569676696776967869679696806968169682696836968469685696866968769688696896969069691696926969369694696956969669697696986969969700697016970269703697046970569706697076970869709697106971169712697136971469715697166971769718697196972069721697226972369724697256972669727697286972969730697316973269733697346973569736697376973869739697406974169742697436974469745697466974769748697496975069751697526975369754697556975669757697586975969760697616976269763697646976569766697676976869769697706977169772697736977469775697766977769778697796978069781697826978369784697856978669787697886978969790697916979269793697946979569796697976979869799698006980169802698036980469805698066980769808698096981069811698126981369814698156981669817698186981969820698216982269823698246982569826698276982869829698306983169832698336983469835698366983769838698396984069841698426984369844698456984669847698486984969850698516985269853698546985569856698576985869859698606986169862698636986469865698666986769868698696987069871698726987369874698756987669877698786987969880698816988269883698846988569886698876988869889698906989169892698936989469895698966989769898698996990069901699026990369904699056990669907699086990969910699116991269913699146991569916699176991869919699206992169922699236992469925699266992769928699296993069931699326993369934699356993669937699386993969940699416994269943699446994569946699476994869949699506995169952699536995469955699566995769958699596996069961699626996369964699656996669967699686996969970699716997269973699746997569976699776997869979699806998169982699836998469985699866998769988699896999069991699926999369994699956999669997699986999970000700017000270003700047000570006700077000870009700107001170012700137001470015700167001770018700197002070021700227002370024700257002670027700287002970030700317003270033700347003570036700377003870039700407004170042700437004470045700467004770048700497005070051700527005370054700557005670057700587005970060700617006270063700647006570066700677006870069700707007170072700737007470075700767007770078700797008070081700827008370084700857008670087700887008970090700917009270093700947009570096700977009870099701007010170102701037010470105701067010770108701097011070111701127011370114701157011670117701187011970120701217012270123701247012570126701277012870129701307013170132701337013470135701367013770138701397014070141701427014370144701457014670147701487014970150701517015270153701547015570156701577015870159701607016170162701637016470165701667016770168701697017070171701727017370174701757017670177701787017970180701817018270183701847018570186701877018870189701907019170192701937019470195701967019770198701997020070201702027020370204702057020670207702087020970210702117021270213702147021570216702177021870219702207022170222702237022470225702267022770228702297023070231702327023370234702357023670237702387023970240702417024270243702447024570246702477024870249702507025170252702537025470255702567025770258702597026070261702627026370264702657026670267702687026970270702717027270273702747027570276702777027870279702807028170282702837028470285702867028770288702897029070291702927029370294702957029670297702987029970300703017030270303703047030570306703077030870309703107031170312703137031470315703167031770318703197032070321703227032370324703257032670327703287032970330703317033270333703347033570336703377033870339703407034170342703437034470345703467034770348703497035070351703527035370354703557035670357703587035970360703617036270363703647036570366703677036870369703707037170372703737037470375703767037770378703797038070381703827038370384703857038670387703887038970390703917039270393703947039570396703977039870399704007040170402704037040470405704067040770408704097041070411704127041370414704157041670417704187041970420704217042270423704247042570426704277042870429704307043170432704337043470435704367043770438704397044070441704427044370444704457044670447704487044970450704517045270453704547045570456704577045870459704607046170462704637046470465704667046770468704697047070471704727047370474704757047670477704787047970480704817048270483704847048570486704877048870489704907049170492704937049470495704967049770498704997050070501705027050370504705057050670507705087050970510705117051270513705147051570516705177051870519705207052170522705237052470525705267052770528705297053070531705327053370534705357053670537705387053970540705417054270543705447054570546705477054870549705507055170552705537055470555705567055770558705597056070561705627056370564705657056670567705687056970570705717057270573705747057570576705777057870579705807058170582705837058470585705867058770588705897059070591705927059370594705957059670597705987059970600706017060270603706047060570606706077060870609706107061170612706137061470615706167061770618706197062070621706227062370624706257062670627706287062970630706317063270633706347063570636706377063870639706407064170642706437064470645706467064770648706497065070651706527065370654706557065670657706587065970660706617066270663706647066570666706677066870669706707067170672706737067470675706767067770678706797068070681706827068370684706857068670687706887068970690706917069270693706947069570696706977069870699707007070170702707037070470705707067070770708707097071070711707127071370714707157071670717707187071970720707217072270723707247072570726707277072870729707307073170732707337073470735707367073770738707397074070741707427074370744707457074670747707487074970750707517075270753707547075570756707577075870759707607076170762707637076470765707667076770768707697077070771707727077370774707757077670777707787077970780707817078270783707847078570786707877078870789707907079170792707937079470795707967079770798707997080070801708027080370804708057080670807708087080970810708117081270813708147081570816708177081870819708207082170822708237082470825708267082770828708297083070831708327083370834708357083670837708387083970840708417084270843708447084570846708477084870849708507085170852708537085470855708567085770858708597086070861708627086370864708657086670867708687086970870708717087270873708747087570876708777087870879708807088170882708837088470885708867088770888708897089070891708927089370894708957089670897708987089970900709017090270903709047090570906709077090870909709107091170912709137091470915709167091770918709197092070921709227092370924709257092670927709287092970930709317093270933709347093570936709377093870939709407094170942709437094470945709467094770948709497095070951709527095370954709557095670957709587095970960709617096270963709647096570966709677096870969709707097170972709737097470975709767097770978709797098070981709827098370984709857098670987709887098970990709917099270993709947099570996709977099870999710007100171002710037100471005710067100771008710097101071011710127101371014710157101671017710187101971020710217102271023710247102571026710277102871029710307103171032710337103471035710367103771038710397104071041710427104371044710457104671047710487104971050710517105271053710547105571056710577105871059710607106171062710637106471065710667106771068710697107071071710727107371074710757107671077710787107971080710817108271083710847108571086710877108871089710907109171092710937109471095710967109771098710997110071101711027110371104711057110671107711087110971110711117111271113711147111571116711177111871119711207112171122711237112471125711267112771128711297113071131711327113371134711357113671137711387113971140711417114271143711447114571146711477114871149711507115171152711537115471155711567115771158711597116071161711627116371164711657116671167711687116971170711717117271173711747117571176711777117871179711807118171182711837118471185711867118771188711897119071191711927119371194711957119671197711987119971200712017120271203712047120571206712077120871209712107121171212712137121471215712167121771218712197122071221712227122371224712257122671227712287122971230712317123271233712347123571236712377123871239712407124171242712437124471245712467124771248712497125071251712527125371254712557125671257712587125971260712617126271263712647126571266712677126871269712707127171272712737127471275712767127771278712797128071281712827128371284712857128671287712887128971290712917129271293712947129571296712977129871299713007130171302713037130471305713067130771308713097131071311713127131371314713157131671317713187131971320713217132271323713247132571326713277132871329713307133171332713337133471335713367133771338713397134071341713427134371344713457134671347713487134971350713517135271353713547135571356713577135871359713607136171362713637136471365713667136771368713697137071371713727137371374713757137671377713787137971380713817138271383713847138571386713877138871389713907139171392713937139471395713967139771398713997140071401714027140371404714057140671407714087140971410714117141271413714147141571416714177141871419714207142171422714237142471425714267142771428714297143071431714327143371434714357143671437714387143971440714417144271443714447144571446714477144871449714507145171452714537145471455714567145771458714597146071461714627146371464714657146671467714687146971470714717147271473714747147571476714777147871479714807148171482714837148471485714867148771488714897149071491714927149371494714957149671497714987149971500715017150271503715047150571506715077150871509715107151171512715137151471515715167151771518715197152071521715227152371524715257152671527715287152971530715317153271533715347153571536715377153871539715407154171542715437154471545715467154771548715497155071551715527155371554715557155671557715587155971560715617156271563715647156571566715677156871569715707157171572715737157471575715767157771578715797158071581715827158371584715857158671587715887158971590715917159271593715947159571596715977159871599716007160171602716037160471605716067160771608716097161071611716127161371614716157161671617716187161971620716217162271623716247162571626716277162871629716307163171632716337163471635716367163771638716397164071641716427164371644716457164671647716487164971650716517165271653716547165571656716577165871659716607166171662716637166471665716667166771668716697167071671716727167371674716757167671677716787167971680716817168271683716847168571686716877168871689716907169171692716937169471695716967169771698716997170071701717027170371704717057170671707717087170971710717117171271713717147171571716717177171871719717207172171722717237172471725717267172771728717297173071731717327173371734717357173671737717387173971740717417174271743717447174571746717477174871749717507175171752717537175471755717567175771758717597176071761717627176371764717657176671767717687176971770717717177271773717747177571776717777177871779717807178171782717837178471785717867178771788717897179071791717927179371794717957179671797717987179971800718017180271803718047180571806718077180871809718107181171812718137181471815718167181771818718197182071821718227182371824718257182671827718287182971830718317183271833718347183571836718377183871839718407184171842718437184471845718467184771848718497185071851718527185371854718557185671857718587185971860718617186271863718647186571866718677186871869718707187171872718737187471875718767187771878718797188071881718827188371884718857188671887718887188971890718917189271893718947189571896718977189871899719007190171902719037190471905719067190771908719097191071911719127191371914719157191671917719187191971920719217192271923719247192571926719277192871929719307193171932719337193471935719367193771938719397194071941719427194371944719457194671947719487194971950719517195271953719547195571956719577195871959719607196171962719637196471965719667196771968719697197071971719727197371974719757197671977719787197971980719817198271983719847198571986719877198871989719907199171992719937199471995719967199771998719997200072001720027200372004720057200672007720087200972010720117201272013720147201572016720177201872019720207202172022720237202472025720267202772028720297203072031720327203372034720357203672037720387203972040720417204272043720447204572046720477204872049720507205172052720537205472055720567205772058720597206072061720627206372064720657206672067720687206972070720717207272073720747207572076720777207872079720807208172082720837208472085720867208772088720897209072091720927209372094720957209672097720987209972100721017210272103721047210572106721077210872109721107211172112721137211472115721167211772118721197212072121721227212372124721257212672127721287212972130721317213272133721347213572136721377213872139721407214172142721437214472145721467214772148721497215072151721527215372154721557215672157721587215972160721617216272163721647216572166721677216872169721707217172172721737217472175721767217772178721797218072181721827218372184721857218672187721887218972190721917219272193721947219572196721977219872199722007220172202722037220472205722067220772208722097221072211722127221372214722157221672217722187221972220722217222272223722247222572226722277222872229722307223172232722337223472235722367223772238722397224072241722427224372244722457224672247722487224972250722517225272253722547225572256722577225872259722607226172262722637226472265722667226772268722697227072271722727227372274722757227672277722787227972280722817228272283722847228572286722877228872289722907229172292722937229472295722967229772298722997230072301723027230372304723057230672307723087230972310723117231272313723147231572316723177231872319723207232172322723237232472325723267232772328723297233072331723327233372334723357233672337723387233972340723417234272343723447234572346723477234872349723507235172352723537235472355723567235772358723597236072361723627236372364723657236672367723687236972370723717237272373723747237572376723777237872379723807238172382723837238472385723867238772388723897239072391723927239372394723957239672397723987239972400724017240272403724047240572406724077240872409724107241172412724137241472415724167241772418724197242072421724227242372424724257242672427724287242972430724317243272433724347243572436724377243872439724407244172442724437244472445724467244772448724497245072451724527245372454724557245672457724587245972460724617246272463724647246572466724677246872469724707247172472724737247472475724767247772478724797248072481724827248372484724857248672487724887248972490724917249272493724947249572496724977249872499725007250172502725037250472505725067250772508725097251072511725127251372514725157251672517725187251972520725217252272523725247252572526725277252872529725307253172532725337253472535725367253772538725397254072541725427254372544725457254672547725487254972550725517255272553725547255572556725577255872559725607256172562725637256472565725667256772568725697257072571725727257372574725757257672577725787257972580725817258272583725847258572586725877258872589725907259172592725937259472595725967259772598725997260072601726027260372604726057260672607726087260972610726117261272613726147261572616726177261872619726207262172622726237262472625726267262772628726297263072631726327263372634726357263672637726387263972640726417264272643726447264572646726477264872649726507265172652726537265472655726567265772658726597266072661726627266372664726657266672667726687266972670726717267272673726747267572676726777267872679726807268172682726837268472685726867268772688726897269072691726927269372694726957269672697726987269972700727017270272703727047270572706727077270872709727107271172712727137271472715727167271772718727197272072721727227272372724727257272672727727287272972730727317273272733727347273572736727377273872739727407274172742727437274472745727467274772748727497275072751727527275372754727557275672757727587275972760727617276272763727647276572766727677276872769727707277172772727737277472775727767277772778727797278072781727827278372784727857278672787727887278972790727917279272793727947279572796727977279872799728007280172802728037280472805728067280772808728097281072811728127281372814728157281672817728187281972820728217282272823728247282572826728277282872829728307283172832728337283472835728367283772838728397284072841728427284372844728457284672847728487284972850728517285272853728547285572856728577285872859728607286172862728637286472865728667286772868728697287072871728727287372874728757287672877728787287972880728817288272883728847288572886728877288872889728907289172892728937289472895728967289772898728997290072901729027290372904729057290672907729087290972910729117291272913729147291572916729177291872919729207292172922729237292472925729267292772928729297293072931729327293372934729357293672937729387293972940729417294272943729447294572946729477294872949729507295172952729537295472955729567295772958729597296072961729627296372964729657296672967729687296972970729717297272973729747297572976729777297872979729807298172982729837298472985729867298772988729897299072991729927299372994729957299672997729987299973000730017300273003730047300573006730077300873009730107301173012730137301473015730167301773018730197302073021730227302373024730257302673027730287302973030730317303273033730347303573036730377303873039730407304173042730437304473045730467304773048730497305073051730527305373054730557305673057730587305973060730617306273063730647306573066730677306873069730707307173072730737307473075730767307773078730797308073081730827308373084730857308673087730887308973090730917309273093730947309573096730977309873099731007310173102731037310473105731067310773108731097311073111731127311373114731157311673117731187311973120731217312273123731247312573126731277312873129731307313173132731337313473135731367313773138731397314073141731427314373144731457314673147731487314973150731517315273153731547315573156731577315873159731607316173162731637316473165731667316773168731697317073171731727317373174731757317673177731787317973180731817318273183731847318573186731877318873189731907319173192731937319473195731967319773198731997320073201732027320373204
  1. /* api.c API unit tests
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* For AES-CBC, input lengths can optionally be validated to be a
  22. * multiple of the block size, by defining WOLFSSL_AES_CBC_LENGTH_CHECKS,
  23. * also available via the configure option --enable-aescbc-length-checks.
  24. */
  25. /*----------------------------------------------------------------------------*
  26. | Includes
  27. *----------------------------------------------------------------------------*/
  28. #ifdef HAVE_CONFIG_H
  29. #include <config.h>
  30. #endif
  31. #include <wolfssl/wolfcrypt/settings.h>
  32. #undef TEST_OPENSSL_COEXIST /* can't use this option with this example */
  33. #ifndef FOURK_BUF
  34. #define FOURK_BUF 4096
  35. #endif
  36. #ifndef TWOK_BUF
  37. #define TWOK_BUF 2048
  38. #endif
  39. #ifndef ONEK_BUF
  40. #define ONEK_BUF 1024
  41. #endif
  42. #if defined(WOLFSSL_STATIC_MEMORY)
  43. #include <wolfssl/wolfcrypt/memory.h>
  44. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  45. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) || \
  46. defined(SESSION_CERTS)
  47. #ifdef OPENSSL_EXTRA
  48. #define TEST_TLS_STATIC_MEMSZ (400000)
  49. #else
  50. #define TEST_TLS_STATIC_MEMSZ (320000)
  51. #endif
  52. #else
  53. #define TEST_TLS_STATIC_MEMSZ (80000)
  54. #endif
  55. #endif
  56. #endif /* WOLFSSL_STATIC_MEMORY */
  57. #ifndef HEAP_HINT
  58. #define HEAP_HINT NULL
  59. #endif /* WOLFSSL_STAIC_MEMORY */
  60. #ifdef WOLFSSL_ASNC_CRYPT
  61. #include <wolfssl/wolfcrypt/async.h>
  62. #endif
  63. #ifdef HAVE_ECC
  64. #include <wolfssl/wolfcrypt/ecc.h> /* wc_ecc_fp_free */
  65. #ifndef ECC_ASN963_MAX_BUF_SZ
  66. #define ECC_ASN963_MAX_BUF_SZ 133
  67. #endif
  68. #ifndef ECC_PRIV_KEY_BUF
  69. #define ECC_PRIV_KEY_BUF 66 /* For non user defined curves. */
  70. #endif
  71. /* ecc key sizes: 14, 16, 20, 24, 28, 30, 32, 40, 48, 64 */
  72. /* logic to choose right key ECC size */
  73. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  74. #define KEY14 14
  75. #else
  76. #define KEY14 32
  77. #endif
  78. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  79. #define KEY16 16
  80. #else
  81. #define KEY16 32
  82. #endif
  83. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  84. #define KEY20 20
  85. #else
  86. #define KEY20 32
  87. #endif
  88. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  89. #define KEY24 24
  90. #else
  91. #define KEY24 32
  92. #endif
  93. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  94. #define KEY28 28
  95. #else
  96. #define KEY28 32
  97. #endif
  98. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  99. #define KEY30 30
  100. #else
  101. #define KEY30 32
  102. #endif
  103. #define KEY32 32
  104. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  105. #define KEY40 40
  106. #else
  107. #define KEY40 32
  108. #endif
  109. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  110. #define KEY48 48
  111. #else
  112. #define KEY48 32
  113. #endif
  114. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  115. #define KEY64 64
  116. #else
  117. #define KEY64 32
  118. #endif
  119. #if !defined(HAVE_COMP_KEY)
  120. #if !defined(NOCOMP)
  121. #define NOCOMP 0
  122. #endif
  123. #else
  124. #if !defined(COMP)
  125. #define COMP 1
  126. #endif
  127. #endif
  128. #if !defined(DER_SZ)
  129. #define DER_SZ(ks) ((ks) * 2 + 1)
  130. #endif
  131. #ifdef WOLFSSL_SM2
  132. #include <wolfssl/wolfcrypt/sm2.h>
  133. #endif
  134. #endif
  135. #ifndef NO_ASN
  136. #include <wolfssl/wolfcrypt/asn_public.h>
  137. #endif
  138. #include <wolfssl/error-ssl.h>
  139. #include <stdlib.h>
  140. #include <wolfssl/ssl.h> /* compatibility layer */
  141. #include <wolfssl/test.h>
  142. #include <tests/unit.h>
  143. #include "examples/server/server.h"
  144. /* for testing compatibility layer callbacks */
  145. #ifndef NO_MD5
  146. #include <wolfssl/wolfcrypt/md5.h>
  147. #endif
  148. #ifndef NO_SHA
  149. #include <wolfssl/wolfcrypt/sha.h>
  150. #endif
  151. #ifndef NO_SHA256
  152. #include <wolfssl/wolfcrypt/sha256.h>
  153. #endif
  154. #ifdef WOLFSSL_SHA512
  155. #include <wolfssl/wolfcrypt/sha512.h>
  156. #endif
  157. #ifdef WOLFSSL_SHA384
  158. #include <wolfssl/wolfcrypt/sha512.h>
  159. #endif
  160. #ifdef WOLFSSL_SHA3
  161. #include <wolfssl/wolfcrypt/sha3.h>
  162. #ifndef HEAP_HINT
  163. #define HEAP_HINT NULL
  164. #endif
  165. #endif
  166. #ifdef WOLFSSL_SM3
  167. #include <wolfssl/wolfcrypt/sm3.h>
  168. #endif
  169. #ifndef NO_AES
  170. #include <wolfssl/wolfcrypt/aes.h>
  171. #ifdef HAVE_AES_DECRYPT
  172. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  173. #endif
  174. #endif
  175. #ifdef WOLFSSL_SM4
  176. #include <wolfssl/wolfcrypt/sm4.h>
  177. #endif
  178. #ifdef WOLFSSL_RIPEMD
  179. #include <wolfssl/wolfcrypt/ripemd.h>
  180. #endif
  181. #ifndef NO_DES3
  182. #include <wolfssl/wolfcrypt/des3.h>
  183. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  184. #endif
  185. #ifdef WC_RC2
  186. #include <wolfssl/wolfcrypt/rc2.h>
  187. #endif
  188. #ifndef NO_HMAC
  189. #include <wolfssl/wolfcrypt/hmac.h>
  190. #endif
  191. #ifdef HAVE_CHACHA
  192. #include <wolfssl/wolfcrypt/chacha.h>
  193. #endif
  194. #ifdef HAVE_POLY1305
  195. #include <wolfssl/wolfcrypt/poly1305.h>
  196. #endif
  197. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  198. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  199. #endif
  200. #ifdef HAVE_CAMELLIA
  201. #include <wolfssl/wolfcrypt/camellia.h>
  202. #endif
  203. #ifndef NO_RC4
  204. #include <wolfssl/wolfcrypt/arc4.h>
  205. #endif
  206. #ifdef HAVE_BLAKE2
  207. #include <wolfssl/wolfcrypt/blake2.h>
  208. #endif
  209. #include <wolfssl/wolfcrypt/hash.h>
  210. #ifndef NO_RSA
  211. #include <wolfssl/wolfcrypt/rsa.h>
  212. #define FOURK_BUF 4096
  213. #define GEN_BUF 294
  214. #endif
  215. #ifndef NO_SIG_WRAPPER
  216. #include <wolfssl/wolfcrypt/signature.h>
  217. #endif
  218. #ifdef HAVE_AESCCM
  219. #include <wolfssl/wolfcrypt/aes.h>
  220. #endif
  221. #ifdef HAVE_PKCS7
  222. #include <wolfssl/wolfcrypt/pkcs7.h>
  223. #include <wolfssl/wolfcrypt/asn.h>
  224. #ifdef HAVE_LIBZ
  225. #include <wolfssl/wolfcrypt/compress.h>
  226. #endif
  227. #endif
  228. #ifdef WOLFSSL_SMALL_CERT_VERIFY
  229. #include <wolfssl/wolfcrypt/asn.h>
  230. #endif
  231. #ifndef NO_DSA
  232. #include <wolfssl/wolfcrypt/dsa.h>
  233. #ifndef ONEK_BUF
  234. #define ONEK_BUF 1024
  235. #endif
  236. #ifndef TWOK_BUF
  237. #define TWOK_BUF 2048
  238. #endif
  239. #ifndef FOURK_BUF
  240. #define FOURK_BUF 4096
  241. #endif
  242. #ifndef DSA_SIG_SIZE
  243. #define DSA_SIG_SIZE 40
  244. #endif
  245. #ifndef MAX_DSA_PARAM_SIZE
  246. #define MAX_DSA_PARAM_SIZE 256
  247. #endif
  248. #endif
  249. #ifdef WOLFSSL_CMAC
  250. #include <wolfssl/wolfcrypt/cmac.h>
  251. #endif
  252. #ifdef HAVE_ED25519
  253. #include <wolfssl/wolfcrypt/ed25519.h>
  254. #endif
  255. #ifdef HAVE_CURVE25519
  256. #include <wolfssl/wolfcrypt/curve25519.h>
  257. #endif
  258. #ifdef HAVE_ED448
  259. #include <wolfssl/wolfcrypt/ed448.h>
  260. #endif
  261. #ifdef HAVE_CURVE448
  262. #include <wolfssl/wolfcrypt/curve448.h>
  263. #endif
  264. #ifdef HAVE_PKCS12
  265. #include <wolfssl/wolfcrypt/pkcs12.h>
  266. #endif
  267. #include <wolfssl/wolfcrypt/logging.h>
  268. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || defined(OPENSSL_ALL))
  269. #include <wolfssl/openssl/ssl.h>
  270. #ifndef NO_ASN
  271. /* for ASN_COMMON_NAME DN_tags enum */
  272. #include <wolfssl/wolfcrypt/asn.h>
  273. #endif
  274. #ifdef HAVE_OCSP
  275. #include <wolfssl/openssl/ocsp.h>
  276. #endif
  277. #endif
  278. #ifdef OPENSSL_EXTRA
  279. #include <wolfssl/openssl/cmac.h>
  280. #include <wolfssl/openssl/x509v3.h>
  281. #include <wolfssl/openssl/asn1.h>
  282. #include <wolfssl/openssl/crypto.h>
  283. #include <wolfssl/openssl/pkcs12.h>
  284. #include <wolfssl/openssl/evp.h>
  285. #include <wolfssl/openssl/dh.h>
  286. #include <wolfssl/openssl/bn.h>
  287. #include <wolfssl/openssl/buffer.h>
  288. #include <wolfssl/openssl/pem.h>
  289. #include <wolfssl/openssl/ec.h>
  290. #include <wolfssl/openssl/ecdh.h>
  291. #include <wolfssl/openssl/engine.h>
  292. #include <wolfssl/openssl/hmac.h>
  293. #include <wolfssl/openssl/objects.h>
  294. #include <wolfssl/openssl/rand.h>
  295. #include <wolfssl/openssl/modes.h>
  296. #include <wolfssl/openssl/fips_rand.h>
  297. #include <wolfssl/openssl/kdf.h>
  298. #ifdef OPENSSL_ALL
  299. #include <wolfssl/openssl/txt_db.h>
  300. #include <wolfssl/openssl/lhash.h>
  301. #endif
  302. #ifndef NO_AES
  303. #include <wolfssl/openssl/aes.h>
  304. #endif
  305. #ifndef NO_DES3
  306. #include <wolfssl/openssl/des.h>
  307. #endif
  308. #ifndef NO_RC4
  309. #include <wolfssl/openssl/rc4.h>
  310. #endif
  311. #ifdef HAVE_ECC
  312. #include <wolfssl/openssl/ecdsa.h>
  313. #endif
  314. #ifdef HAVE_PKCS7
  315. #include <wolfssl/openssl/pkcs7.h>
  316. #endif
  317. #ifdef HAVE_CURVE25519
  318. #include <wolfssl/openssl/ec25519.h>
  319. #endif
  320. #ifdef HAVE_ED25519
  321. #include <wolfssl/openssl/ed25519.h>
  322. #endif
  323. #ifdef HAVE_CURVE448
  324. #include <wolfssl/openssl/ec448.h>
  325. #endif
  326. #ifdef HAVE_ED448
  327. #include <wolfssl/openssl/ed448.h>
  328. #endif
  329. #endif /* OPENSSL_EXTRA */
  330. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  331. && !defined(NO_SHA256) && !defined(RC_NO_RNG)
  332. #include <wolfssl/wolfcrypt/srp.h>
  333. #endif
  334. #if (defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)) || \
  335. defined(HAVE_SESSION_TICKET) || (defined(OPENSSL_EXTRA) && \
  336. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)) || \
  337. defined(WOLFSSL_TEST_STATIC_BUILD) || defined(WOLFSSL_DTLS) || \
  338. defined(HAVE_ECH) || defined(HAVE_EX_DATA) || !defined(NO_SESSION_CACHE) \
  339. || !defined(WOLFSSL_NO_TLS12) || defined(WOLFSSL_TLS13)
  340. /* for testing SSL_get_peer_cert_chain, or SESSION_TICKET_HINT_DEFAULT,
  341. * for setting authKeyIdSrc in WOLFSSL_X509, or testing DTLS sequence
  342. * number tracking */
  343. #include "wolfssl/internal.h"
  344. #endif
  345. /* force enable test buffers */
  346. #ifndef USE_CERT_BUFFERS_2048
  347. #define USE_CERT_BUFFERS_2048
  348. #endif
  349. #ifndef USE_CERT_BUFFERS_256
  350. #define USE_CERT_BUFFERS_256
  351. #endif
  352. #include <wolfssl/certs_test.h>
  353. #include "tests/utils.h"
  354. /* include misc.c here regardless of NO_INLINE, because misc.c implementations
  355. * have default (hidden) visibility, and in the absence of visibility, it's
  356. * benign to mask out the library implementation.
  357. */
  358. #define WOLFSSL_MISC_INCLUDED
  359. #include <wolfcrypt/src/misc.c>
  360. #ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  361. /* FIPS build has replaced ecc.h. */
  362. #define wc_ecc_key_get_priv(key) (&((key)->k))
  363. #define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  364. #endif
  365. typedef struct testVector {
  366. const char* input;
  367. const char* output;
  368. size_t inLen;
  369. size_t outLen;
  370. } testVector;
  371. #if defined(HAVE_PKCS7)
  372. typedef struct {
  373. const byte* content;
  374. word32 contentSz;
  375. int contentOID;
  376. int encryptOID;
  377. int keyWrapOID;
  378. int keyAgreeOID;
  379. byte* cert;
  380. size_t certSz;
  381. byte* privateKey;
  382. word32 privateKeySz;
  383. } pkcs7EnvelopedVector;
  384. #ifndef NO_PKCS7_ENCRYPTED_DATA
  385. typedef struct {
  386. const byte* content;
  387. word32 contentSz;
  388. int contentOID;
  389. int encryptOID;
  390. byte* encryptionKey;
  391. word32 encryptionKeySz;
  392. } pkcs7EncryptedVector;
  393. #endif
  394. #endif /* HAVE_PKCS7 */
  395. typedef int (*ctx_cb)(WOLFSSL_CTX* ctx);
  396. typedef int (*ssl_cb)(WOLFSSL* ssl);
  397. typedef int (*test_cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  398. typedef int (*hs_cb)(WOLFSSL_CTX **ctx, WOLFSSL **ssl);
  399. typedef struct test_ssl_cbf {
  400. method_provider method;
  401. ctx_cb ctx_ready;
  402. ssl_cb ssl_ready;
  403. ssl_cb on_result;
  404. ctx_cb on_ctx_cleanup;
  405. ssl_cb on_cleanup;
  406. hs_cb on_handshake;
  407. WOLFSSL_CTX* ctx;
  408. const char* caPemFile;
  409. const char* certPemFile;
  410. const char* keyPemFile;
  411. const char* crlPemFile;
  412. #ifdef WOLFSSL_STATIC_MEMORY
  413. byte* mem;
  414. word32 memSz;
  415. wolfSSL_method_func method_ex;
  416. #endif
  417. int devId;
  418. int return_code;
  419. int last_err;
  420. unsigned char isSharedCtx:1;
  421. unsigned char loadToSSL:1;
  422. unsigned char ticNoInit:1;
  423. unsigned char doUdp:1;
  424. } test_ssl_cbf;
  425. #define TEST_SSL_MEMIO_BUF_SZ (64 * 1024)
  426. typedef struct test_ssl_memio_ctx {
  427. WOLFSSL_CTX* s_ctx;
  428. WOLFSSL_CTX* c_ctx;
  429. WOLFSSL* s_ssl;
  430. WOLFSSL* c_ssl;
  431. const char* c_ciphers;
  432. const char* s_ciphers;
  433. char* c_msg;
  434. int c_msglen;
  435. char* s_msg;
  436. int s_msglen;
  437. test_ssl_cbf s_cb;
  438. test_ssl_cbf c_cb;
  439. byte c_buff[TEST_SSL_MEMIO_BUF_SZ];
  440. int c_len;
  441. byte s_buff[TEST_SSL_MEMIO_BUF_SZ];
  442. int s_len;
  443. } test_ssl_memio_ctx;
  444. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  445. test_ssl_cbf* server_cb, test_cbType client_on_handshake);
  446. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  447. const char* currentTestName;
  448. char tmpDirName[16];
  449. int tmpDirNameSet = 0;
  450. #endif
  451. /*----------------------------------------------------------------------------*
  452. | Constants
  453. *----------------------------------------------------------------------------*/
  454. /* Test result constants and macros. */
  455. /* Test succeeded. */
  456. #define TEST_SUCCESS (1)
  457. /* Test failed. */
  458. #define TEST_FAIL (0)
  459. /* Test skipped - not run. */
  460. #define TEST_SKIPPED (-7777)
  461. /* Returns the result based on whether check is true.
  462. *
  463. * @param [in] check Condition for success.
  464. * @return When condition is true: TEST_SUCCESS.
  465. * @return When condition is false: TEST_FAIL.
  466. */
  467. #ifdef DEBUG_WOLFSSL_VERBOSE
  468. #define XSTRINGIFY(s) STRINGIFY(s)
  469. #define STRINGIFY(s) #s
  470. #define TEST_RES_CHECK(check) ({ \
  471. int _ret = (check) ? TEST_SUCCESS : TEST_FAIL; \
  472. if (_ret == TEST_FAIL) { \
  473. fprintf(stderr, " check \"%s\" at %d ", \
  474. XSTRINGIFY(check), __LINE__); \
  475. } \
  476. _ret; })
  477. #else
  478. #define TEST_RES_CHECK(check) \
  479. ((check) ? TEST_SUCCESS : TEST_FAIL)
  480. #endif /* DEBUG_WOLFSSL_VERBOSE */
  481. #define TEST_STRING "Everyone gets Friday off."
  482. #define TEST_STRING_SZ 25
  483. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  484. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  485. #define TEST_RSA_BITS 1024
  486. #else
  487. #define TEST_RSA_BITS 2048
  488. #endif
  489. #define TEST_RSA_BYTES (TEST_RSA_BITS/8)
  490. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  491. (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
  492. static const char* bogusFile =
  493. #ifdef _WIN32
  494. "NUL"
  495. #else
  496. "/dev/null"
  497. #endif
  498. ;
  499. #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
  500. enum {
  501. TESTING_RSA = 1,
  502. TESTING_ECC = 2
  503. };
  504. #ifdef WOLFSSL_QNX_CAAM
  505. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  506. static int testDevId = WOLFSSL_CAAM_DEVID;
  507. #else
  508. static int testDevId = INVALID_DEVID;
  509. #endif
  510. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  511. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  512. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  513. #define HAVE_IO_TESTS_DEPENDENCIES
  514. #endif
  515. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  516. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  517. #define HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  518. #endif
  519. /*----------------------------------------------------------------------------*
  520. | BIO with fixed read/write size
  521. *----------------------------------------------------------------------------*/
  522. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  523. static int wolfssl_bio_s_fixed_mem_write(WOLFSSL_BIO* bio, const char* data,
  524. int len)
  525. {
  526. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  527. len = 0;
  528. }
  529. else {
  530. if (bio->wrSz - bio->wrIdx < len) {
  531. len = bio->wrSz - bio->wrIdx;
  532. }
  533. XMEMCPY((char*)bio->ptr + bio->wrIdx, data, len);
  534. bio->wrIdx += len;
  535. }
  536. return len;
  537. }
  538. static int wolfssl_bio_s_fixed_mem_read(WOLFSSL_BIO* bio, char* data, int len)
  539. {
  540. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  541. len = 0;
  542. }
  543. else {
  544. if (bio->wrSz - bio->rdIdx < len) {
  545. len = bio->wrSz - bio->rdIdx;
  546. }
  547. XMEMCPY(data, (char*)bio->ptr + bio->rdIdx, len);
  548. bio->rdIdx += len;
  549. }
  550. return len;
  551. }
  552. static WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_fixed_mem(void)
  553. {
  554. static WOLFSSL_BIO_METHOD meth;
  555. meth.type = WOLFSSL_BIO_BIO;
  556. XMEMCPY(meth.name, "Fixed Memory Size", 18);
  557. meth.writeCb = wolfssl_bio_s_fixed_mem_write;
  558. meth.readCb = wolfssl_bio_s_fixed_mem_read;
  559. return &meth;
  560. }
  561. #endif
  562. /*----------------------------------------------------------------------------*
  563. | Setup
  564. *----------------------------------------------------------------------------*/
  565. static int test_wolfSSL_Init(void)
  566. {
  567. EXPECT_DECLS;
  568. ExpectIntEQ(wolfSSL_Init(), WOLFSSL_SUCCESS);
  569. return EXPECT_RESULT();
  570. }
  571. static int test_wolfSSL_Cleanup(void)
  572. {
  573. EXPECT_DECLS;
  574. ExpectIntEQ(wolfSSL_Cleanup(), WOLFSSL_SUCCESS);
  575. return EXPECT_RESULT();
  576. }
  577. /* Initialize the wolfCrypt state.
  578. * POST: 0 success.
  579. */
  580. static int test_wolfCrypt_Init(void)
  581. {
  582. EXPECT_DECLS;
  583. ExpectIntEQ(wolfCrypt_Init(), 0);
  584. return EXPECT_RESULT();
  585. } /* END test_wolfCrypt_Init */
  586. static int test_wolfCrypt_Cleanup(void)
  587. {
  588. EXPECT_DECLS;
  589. ExpectIntEQ(wolfCrypt_Cleanup(), 0);
  590. return EXPECT_RESULT();
  591. }
  592. static int test_wc_LoadStaticMemory_ex(void)
  593. {
  594. EXPECT_DECLS;
  595. #ifdef WOLFSSL_STATIC_MEMORY
  596. byte staticMemory[440000];
  597. word32 sizeList[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_BUCKETS };
  598. word32 distList[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_DIST };
  599. WOLFSSL_HEAP_HINT* heap;
  600. /* Pass in zero everything. */
  601. ExpectIntEQ(wc_LoadStaticMemory_ex(NULL, 0, NULL, NULL, NULL, 0, 0, 0),
  602. BAD_FUNC_ARG);
  603. /* Set the heap pointer to NULL. */
  604. ExpectIntEQ(wc_LoadStaticMemory_ex(NULL,
  605. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  606. staticMemory, (word32)sizeof(staticMemory),
  607. 0, 1),
  608. BAD_FUNC_ARG);
  609. /* Set other pointer values to NULL one at a time. */
  610. heap = NULL;
  611. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  612. WOLFMEM_DEF_BUCKETS, NULL, distList,
  613. staticMemory, (word32)sizeof(staticMemory),
  614. 0, 1),
  615. BAD_FUNC_ARG);
  616. heap = NULL;
  617. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  618. WOLFMEM_DEF_BUCKETS, sizeList, NULL,
  619. staticMemory, (word32)sizeof(staticMemory),
  620. 0, 1),
  621. BAD_FUNC_ARG);
  622. heap = NULL;
  623. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  624. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  625. NULL, (word32)sizeof(staticMemory),
  626. 0, 1),
  627. BAD_FUNC_ARG);
  628. /* Set the size of the static buffer to 0. */
  629. heap = NULL;
  630. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  631. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  632. staticMemory, 0,
  633. 0, 1),
  634. BUFFER_E);
  635. /* Set the size of the static buffer to one less than minimum allowed. */
  636. heap = NULL;
  637. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  638. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  639. staticMemory,
  640. (word32)(sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT)) - 1,
  641. 0, 1),
  642. BUFFER_E);
  643. /* Set the number of buckets to 1 too many allowed. */
  644. heap = NULL;
  645. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  646. WOLFMEM_MAX_BUCKETS+1, sizeList, distList,
  647. staticMemory, (word32)sizeof(staticMemory),
  648. 0, 1),
  649. BAD_FUNC_ARG);
  650. /* Set the size of the static buffer to exactly the minimum size. */
  651. heap = NULL;
  652. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  653. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  654. staticMemory,
  655. (word32)(sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT)),
  656. 0, 1),
  657. 0);
  658. wc_UnloadStaticMemory(heap);
  659. /* Success case. */
  660. heap = NULL;
  661. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  662. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  663. staticMemory, (word32)sizeof(staticMemory),
  664. 0, 1),
  665. 0);
  666. wc_UnloadStaticMemory(heap);
  667. #endif /* WOLFSSL_STATIC_MEMORY */
  668. return EXPECT_RESULT();
  669. }
  670. /*----------------------------------------------------------------------------*
  671. | Platform dependent function test
  672. *----------------------------------------------------------------------------*/
  673. static int test_fileAccess(void)
  674. {
  675. EXPECT_DECLS;
  676. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  677. const char *fname[] = {
  678. svrCertFile, svrKeyFile, caCertFile,
  679. eccCertFile, eccKeyFile, eccRsaCertFile,
  680. cliCertFile, cliCertDerFile, cliKeyFile,
  681. dhParamFile,
  682. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  683. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  684. NULL
  685. };
  686. const char derfile[] = "./certs/server-cert.der";
  687. XFILE f = XBADFILE;
  688. size_t sz;
  689. byte *buff = NULL;
  690. int i;
  691. ExpectTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  692. for (i=0; EXPECT_SUCCESS() && fname[i] != NULL ; i++) {
  693. ExpectTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  694. XFCLOSE(f);
  695. }
  696. ExpectTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  697. ExpectTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  698. ExpectIntGE(sz = (size_t) XFTELL(f), sizeof_server_cert_der_2048);
  699. ExpectTrue(XFSEEK(f, 0, XSEEK_SET) == 0);
  700. ExpectTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL);
  701. ExpectTrue(XFREAD(buff, 1, sz, f) == sz);
  702. ExpectIntEQ(XMEMCMP(server_cert_der_2048, buff, sz), 0);
  703. XFREE(buff, NULL, DYNAMIC_TYPE_FILE);
  704. XFCLOSE(f);
  705. #endif
  706. return EXPECT_RESULT();
  707. }
  708. /*----------------------------------------------------------------------------*
  709. | Method Allocators
  710. *----------------------------------------------------------------------------*/
  711. static int test_wolfSSL_Method_Allocators(void)
  712. {
  713. EXPECT_DECLS;
  714. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  715. do { \
  716. WOLFSSL_METHOD *method = NULL; \
  717. condition(method = allocator()); \
  718. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  719. } while (0)
  720. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  721. TEST_METHOD_ALLOCATOR(a, ExpectNotNull)
  722. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  723. TEST_METHOD_ALLOCATOR(a, ExpectNull)
  724. #ifndef NO_OLD_TLS
  725. #ifdef WOLFSSL_ALLOW_SSLV3
  726. #ifndef NO_WOLFSSL_SERVER
  727. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  728. #endif
  729. #ifndef NO_WOLFSSL_CLIENT
  730. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  731. #endif
  732. #endif
  733. #ifdef WOLFSSL_ALLOW_TLSV10
  734. #ifndef NO_WOLFSSL_SERVER
  735. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  736. #endif
  737. #ifndef NO_WOLFSSL_CLIENT
  738. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  739. #endif
  740. #endif
  741. #ifndef NO_WOLFSSL_SERVER
  742. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  743. #endif
  744. #ifndef NO_WOLFSSL_CLIENT
  745. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  746. #endif
  747. #endif /* !NO_OLD_TLS */
  748. #ifndef WOLFSSL_NO_TLS12
  749. #ifndef NO_WOLFSSL_SERVER
  750. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  751. #endif
  752. #ifndef NO_WOLFSSL_CLIENT
  753. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  754. #endif
  755. #endif /* !WOLFSSL_NO_TLS12 */
  756. #ifdef WOLFSSL_TLS13
  757. #ifndef NO_WOLFSSL_SERVER
  758. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  759. #endif
  760. #ifndef NO_WOLFSSL_CLIENT
  761. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  762. #endif
  763. #endif /* WOLFSSL_TLS13 */
  764. #ifndef NO_WOLFSSL_SERVER
  765. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  766. #endif
  767. #ifndef NO_WOLFSSL_CLIENT
  768. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  769. #endif
  770. #ifdef WOLFSSL_DTLS
  771. #ifndef NO_OLD_TLS
  772. #ifndef NO_WOLFSSL_SERVER
  773. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  774. #endif
  775. #ifndef NO_WOLFSSL_CLIENT
  776. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  777. #endif
  778. #endif
  779. #ifndef WOLFSSL_NO_TLS12
  780. #ifndef NO_WOLFSSL_SERVER
  781. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  782. #endif
  783. #ifndef NO_WOLFSSL_CLIENT
  784. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  785. #endif
  786. #endif
  787. #endif /* WOLFSSL_DTLS */
  788. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  789. /* Stubs */
  790. #ifndef NO_WOLFSSL_SERVER
  791. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  792. #endif
  793. #ifndef NO_WOLFSSL_CLIENT
  794. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  795. #endif
  796. #endif
  797. /* Test Either Method (client or server) */
  798. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  799. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  800. #ifndef NO_OLD_TLS
  801. #ifdef WOLFSSL_ALLOW_TLSV10
  802. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  803. #endif
  804. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  805. #endif /* !NO_OLD_TLS */
  806. #ifndef WOLFSSL_NO_TLS12
  807. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  808. #endif /* !WOLFSSL_NO_TLS12 */
  809. #ifdef WOLFSSL_TLS13
  810. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  811. #endif /* WOLFSSL_TLS13 */
  812. #ifdef WOLFSSL_DTLS
  813. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  814. #ifndef NO_OLD_TLS
  815. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  816. #endif /* !NO_OLD_TLS */
  817. #ifndef WOLFSSL_NO_TLS12
  818. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  819. #endif /* !WOLFSSL_NO_TLS12 */
  820. #endif /* WOLFSSL_DTLS */
  821. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  822. return EXPECT_RESULT();
  823. }
  824. #if defined(WOLFSSL_DUAL_ALG_CERTS) && !defined(NO_FILESYSTEM)
  825. /*----------------------------------------------------------------------------*
  826. | Dual algorithm Certificate Tests
  827. *----------------------------------------------------------------------------*/
  828. #define LARGE_TEMP_SZ 4096
  829. /* To better understand this, please see the X9.146 example in wolfssl-examples
  830. * repo. */
  831. static int do_dual_alg_root_certgen(byte **out, char *caKeyFile,
  832. char *sapkiFile, char *altPrivFile)
  833. {
  834. EXPECT_DECLS;
  835. FILE* file = NULL;
  836. Cert newCert;
  837. DecodedCert preTBS;
  838. byte caKeyBuf[LARGE_TEMP_SZ];
  839. word32 caKeySz = LARGE_TEMP_SZ;
  840. byte sapkiBuf[LARGE_TEMP_SZ];
  841. word32 sapkiSz = LARGE_TEMP_SZ;
  842. byte altPrivBuf[LARGE_TEMP_SZ];
  843. word32 altPrivSz = LARGE_TEMP_SZ;
  844. byte altSigAlgBuf[LARGE_TEMP_SZ];
  845. word32 altSigAlgSz = LARGE_TEMP_SZ;
  846. byte scratchBuf[LARGE_TEMP_SZ];
  847. word32 scratchSz = LARGE_TEMP_SZ;
  848. byte preTbsBuf[LARGE_TEMP_SZ];
  849. word32 preTbsSz = LARGE_TEMP_SZ;
  850. byte altSigValBuf[LARGE_TEMP_SZ];
  851. word32 altSigValSz = LARGE_TEMP_SZ;
  852. byte *outBuf = NULL;
  853. word32 outSz = LARGE_TEMP_SZ;
  854. WC_RNG rng;
  855. RsaKey caKey;
  856. ecc_key altCaKey;
  857. word32 idx = 0;
  858. ExpectNotNull(outBuf = (byte*)XMALLOC(outSz, NULL,
  859. DYNAMIC_TYPE_TMP_BUFFER));
  860. ExpectIntEQ(wc_InitRng(&rng), 0);
  861. XMEMSET(caKeyBuf, 0, caKeySz);
  862. ExpectNotNull(file = fopen(caKeyFile, "rb"));
  863. ExpectIntGT(caKeySz = (word32)fread(caKeyBuf, 1, caKeySz, file), 0);
  864. if (file) {
  865. fclose(file);
  866. file = NULL;
  867. }
  868. ExpectIntEQ(wc_InitRsaKey_ex(&caKey, NULL, INVALID_DEVID), 0);
  869. idx = 0;
  870. ExpectIntEQ(wc_RsaPrivateKeyDecode(caKeyBuf, &idx, &caKey, caKeySz),
  871. 0);
  872. XMEMSET(sapkiBuf, 0, sapkiSz);
  873. ExpectNotNull(file = fopen(sapkiFile, "rb"));
  874. ExpectIntGT(sapkiSz = (word32)fread(sapkiBuf, 1, sapkiSz, file), 0);
  875. if (file) {
  876. fclose(file);
  877. file = NULL;
  878. }
  879. XMEMSET(altPrivBuf, 0, altPrivSz);
  880. ExpectNotNull(file = fopen(altPrivFile, "rb"));
  881. ExpectIntGT(altPrivSz = (word32)fread(altPrivBuf, 1, altPrivSz, file), 0);
  882. if (file) {
  883. fclose(file);
  884. file = NULL;
  885. }
  886. wc_ecc_init(&altCaKey);
  887. idx = 0;
  888. ExpectIntEQ(wc_EccPrivateKeyDecode(altPrivBuf, &idx, &altCaKey,
  889. (word32)altPrivSz), 0);
  890. XMEMSET(altSigAlgBuf, 0, altSigAlgSz);
  891. ExpectIntGT(altSigAlgSz = SetAlgoID(CTC_SHA256wECDSA, altSigAlgBuf,
  892. oidSigType, 0), 0);
  893. wc_InitCert(&newCert);
  894. strncpy(newCert.subject.country, "US", CTC_NAME_SIZE);
  895. strncpy(newCert.subject.state, "MT", CTC_NAME_SIZE);
  896. strncpy(newCert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  897. strncpy(newCert.subject.org, "wolfSSL", CTC_NAME_SIZE);
  898. strncpy(newCert.subject.unit, "Engineering", CTC_NAME_SIZE);
  899. strncpy(newCert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
  900. strncpy(newCert.subject.email, "root@wolfssl.com", CTC_NAME_SIZE);
  901. newCert.sigType = CTC_SHA256wRSA;
  902. newCert.isCA = 1;
  903. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "1.2.3.4.5",
  904. (const byte *)"This is NOT a critical extension", 32), 0);
  905. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.72", sapkiBuf,
  906. sapkiSz), 0);
  907. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.73", altSigAlgBuf,
  908. altSigAlgSz), 0);
  909. XMEMSET(scratchBuf, 0, scratchSz);
  910. ExpectIntGT(scratchSz = wc_MakeSelfCert(&newCert, scratchBuf, scratchSz,
  911. &caKey, &rng), 0);
  912. wc_InitDecodedCert(&preTBS, scratchBuf, scratchSz, 0);
  913. ExpectIntEQ(wc_ParseCert(&preTBS, CERT_TYPE, NO_VERIFY, NULL), 0);
  914. XMEMSET(preTbsBuf, 0, preTbsSz);
  915. ExpectIntGT(preTbsSz = wc_GeneratePreTBS(&preTBS, preTbsBuf, preTbsSz), 0);
  916. XMEMSET(altSigValBuf, 0, altSigValSz);
  917. ExpectIntGT(altSigValSz = wc_MakeSigWithBitStr(altSigValBuf, altSigValSz,
  918. CTC_SHA256wECDSA, preTbsBuf, preTbsSz, ECC_TYPE, &altCaKey,
  919. &rng), 0);
  920. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.74", altSigValBuf,
  921. altSigValSz), 0);
  922. /* Finally, generate the new certificate. */
  923. XMEMSET(outBuf, 0, outSz);
  924. ExpectIntGT(outSz = wc_MakeSelfCert(&newCert, outBuf, outSz, &caKey, &rng),
  925. 0);
  926. *out = outBuf;
  927. wc_FreeRsaKey(&caKey);
  928. wc_FreeRng(&rng);
  929. wc_FreeDecodedCert(&preTBS);
  930. return outSz;
  931. }
  932. static int do_dual_alg_server_certgen(byte **out, char *caKeyFile,
  933. char *sapkiFile, char *altPrivFile,
  934. char *serverKeyFile,
  935. byte *caCertBuf, int caCertSz)
  936. {
  937. EXPECT_DECLS;
  938. FILE* file = NULL;
  939. Cert newCert;
  940. DecodedCert preTBS;
  941. byte serverKeyBuf[LARGE_TEMP_SZ];
  942. word32 serverKeySz = LARGE_TEMP_SZ;
  943. byte caKeyBuf[LARGE_TEMP_SZ];
  944. word32 caKeySz = LARGE_TEMP_SZ;
  945. byte sapkiBuf[LARGE_TEMP_SZ];
  946. word32 sapkiSz = LARGE_TEMP_SZ;
  947. byte altPrivBuf[LARGE_TEMP_SZ];
  948. word32 altPrivSz = LARGE_TEMP_SZ;
  949. byte altSigAlgBuf[LARGE_TEMP_SZ];
  950. word32 altSigAlgSz = LARGE_TEMP_SZ;
  951. byte scratchBuf[LARGE_TEMP_SZ];
  952. word32 scratchSz = LARGE_TEMP_SZ;
  953. byte preTbsBuf[LARGE_TEMP_SZ];
  954. word32 preTbsSz = LARGE_TEMP_SZ;
  955. byte altSigValBuf[LARGE_TEMP_SZ];
  956. word32 altSigValSz = LARGE_TEMP_SZ;
  957. byte *outBuf = NULL;
  958. word32 outSz = LARGE_TEMP_SZ;
  959. WC_RNG rng;
  960. RsaKey caKey;
  961. RsaKey serverKey;
  962. ecc_key altCaKey;
  963. word32 idx = 0;
  964. ExpectNotNull(outBuf = (byte*)XMALLOC(outSz, NULL,
  965. DYNAMIC_TYPE_TMP_BUFFER));
  966. ExpectIntEQ(wc_InitRng(&rng), 0);
  967. XMEMSET(serverKeyBuf, 0, serverKeySz);
  968. ExpectNotNull(file = fopen(serverKeyFile, "rb"));
  969. ExpectIntGT(serverKeySz = (word32)fread(serverKeyBuf, 1, serverKeySz, file),
  970. 0);
  971. if (file) {
  972. fclose(file);
  973. file = NULL;
  974. }
  975. ExpectIntEQ(wc_InitRsaKey_ex(&serverKey, NULL, INVALID_DEVID), 0);
  976. idx = 0;
  977. ExpectIntEQ(wc_RsaPrivateKeyDecode(serverKeyBuf, &idx, &serverKey,
  978. (word32)serverKeySz), 0);
  979. XMEMSET(caKeyBuf, 0, caKeySz);
  980. ExpectNotNull(file = fopen(caKeyFile, "rb"));
  981. ExpectIntGT(caKeySz = (word32)fread(caKeyBuf, 1, caKeySz, file), 0);
  982. if (file) {
  983. fclose(file);
  984. file = NULL;
  985. }
  986. ExpectIntEQ(wc_InitRsaKey_ex(&caKey, NULL, INVALID_DEVID), 0);
  987. idx = 0;
  988. ExpectIntEQ(wc_RsaPrivateKeyDecode(caKeyBuf, &idx, &caKey,
  989. (word32)caKeySz), 0);
  990. XMEMSET(sapkiBuf, 0, sapkiSz);
  991. ExpectNotNull(file = fopen(sapkiFile, "rb"));
  992. ExpectIntGT(sapkiSz = (word32)fread(sapkiBuf, 1, sapkiSz, file), 0);
  993. if (file) {
  994. fclose(file);
  995. file = NULL;
  996. }
  997. XMEMSET(altPrivBuf, 0, altPrivSz);
  998. ExpectNotNull(file = fopen(altPrivFile, "rb"));
  999. ExpectIntGT(altPrivSz = (word32)fread(altPrivBuf, 1, altPrivSz, file), 0);
  1000. if (file) {
  1001. fclose(file);
  1002. file = NULL;
  1003. }
  1004. wc_ecc_init(&altCaKey);
  1005. idx = 0;
  1006. ExpectIntEQ(wc_EccPrivateKeyDecode(altPrivBuf, &idx, &altCaKey,
  1007. (word32)altPrivSz), 0);
  1008. XMEMSET(altSigAlgBuf, 0, altSigAlgSz);
  1009. ExpectIntGT(altSigAlgSz = SetAlgoID(CTC_SHA256wECDSA, altSigAlgBuf,
  1010. oidSigType, 0), 0);
  1011. wc_InitCert(&newCert);
  1012. strncpy(newCert.subject.country, "US", CTC_NAME_SIZE);
  1013. strncpy(newCert.subject.state, "MT", CTC_NAME_SIZE);
  1014. strncpy(newCert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  1015. strncpy(newCert.subject.org, "wolfSSL", CTC_NAME_SIZE);
  1016. strncpy(newCert.subject.unit, "Engineering", CTC_NAME_SIZE);
  1017. strncpy(newCert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
  1018. strncpy(newCert.subject.email, "server@wolfssl.com", CTC_NAME_SIZE);
  1019. newCert.sigType = CTC_SHA256wRSA;
  1020. newCert.isCA = 0;
  1021. ExpectIntEQ(wc_SetIssuerBuffer(&newCert, caCertBuf, caCertSz), 0);
  1022. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "1.2.3.4.5",
  1023. (const byte *)"This is NOT a critical extension", 32), 0);
  1024. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.72", sapkiBuf,
  1025. sapkiSz), 0);
  1026. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.73", altSigAlgBuf,
  1027. altSigAlgSz), 0);
  1028. XMEMSET(scratchBuf, 0, scratchSz);
  1029. ExpectIntGT(wc_MakeCert(&newCert, scratchBuf, scratchSz, &serverKey, NULL,
  1030. &rng), 0);
  1031. ExpectIntGT(scratchSz = wc_SignCert(newCert.bodySz, newCert.sigType,
  1032. scratchBuf, scratchSz, &caKey, NULL, &rng), 0);
  1033. wc_InitDecodedCert(&preTBS, scratchBuf, scratchSz, 0);
  1034. ExpectIntEQ(wc_ParseCert(&preTBS, CERT_TYPE, NO_VERIFY, NULL), 0);
  1035. XMEMSET(preTbsBuf, 0, preTbsSz);
  1036. ExpectIntGT(preTbsSz = wc_GeneratePreTBS(&preTBS, preTbsBuf, preTbsSz), 0);
  1037. XMEMSET(altSigValBuf, 0, altSigValSz);
  1038. ExpectIntGT(altSigValSz = wc_MakeSigWithBitStr(altSigValBuf, altSigValSz,
  1039. CTC_SHA256wECDSA, preTbsBuf, preTbsSz, ECC_TYPE, &altCaKey,
  1040. &rng), 0);
  1041. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.74",
  1042. altSigValBuf, altSigValSz), 0);
  1043. /* Finally, generate the new certificate. */
  1044. XMEMSET(outBuf, 0, outSz);
  1045. ExpectIntGT(wc_MakeCert(&newCert, outBuf, outSz, &serverKey, NULL, &rng),
  1046. 0);
  1047. ExpectIntGT(outSz = wc_SignCert(newCert.bodySz, newCert.sigType, outBuf,
  1048. outSz, &caKey, NULL, &rng), 0);
  1049. *out = outBuf;
  1050. wc_FreeRsaKey(&caKey);
  1051. wc_FreeRsaKey(&serverKey);
  1052. wc_FreeRng(&rng);
  1053. wc_FreeDecodedCert(&preTBS);
  1054. return outSz;
  1055. }
  1056. static int do_dual_alg_tls13_connection(byte *caCert, word32 caCertSz,
  1057. byte *serverCert, word32 serverCertSz,
  1058. byte *serverKey, word32 serverKeySz,
  1059. int negative_test)
  1060. {
  1061. EXPECT_DECLS;
  1062. WOLFSSL_CTX *ctx_c = NULL;
  1063. WOLFSSL_CTX *ctx_s = NULL;
  1064. WOLFSSL *ssl_c = NULL;
  1065. WOLFSSL *ssl_s = NULL;
  1066. struct test_memio_ctx test_ctx;
  1067. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  1068. ExpectIntEQ(test_memio_setup_ex(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  1069. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  1070. caCert, caCertSz, serverCert, serverCertSz,
  1071. serverKey, serverKeySz), 0);
  1072. if (negative_test) {
  1073. ExpectTrue(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0);
  1074. }
  1075. else {
  1076. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  1077. }
  1078. wolfSSL_free(ssl_c);
  1079. wolfSSL_free(ssl_s);
  1080. wolfSSL_CTX_free(ctx_c);
  1081. wolfSSL_CTX_free(ctx_s);
  1082. return EXPECT_RESULT();
  1083. }
  1084. static int extCount = 0;
  1085. static int myUnknownExtCallback(const word16* oid, word32 oidSz, int crit,
  1086. const unsigned char* der, word32 derSz)
  1087. {
  1088. (void) oid;
  1089. (void) oidSz;
  1090. (void) crit;
  1091. (void) der;
  1092. (void) derSz;
  1093. extCount ++;
  1094. /* Accept all extensions. This is only a test. Normally we would be much more
  1095. * careful about critical extensions. */
  1096. return 1;
  1097. }
  1098. static int test_dual_alg_support(void)
  1099. {
  1100. EXPECT_DECLS;
  1101. /* Root CA and server keys will be the same. This is only appropriate for
  1102. * testing. */
  1103. char keyFile[] = "./certs/ca-key.der";
  1104. char sapkiFile[] = "./certs/ecc-keyPub.der";
  1105. char altPrivFile[] = "./certs/ecc-key.der";
  1106. char wrongPrivFile[] = "./certs/ecc-client-key.der";
  1107. byte *serverKey = NULL;
  1108. size_t serverKeySz = 0;
  1109. byte *root = NULL;
  1110. int rootSz = 0;
  1111. byte *server = NULL;
  1112. int serverSz = 0;
  1113. WOLFSSL_CERT_MANAGER* cm = NULL;
  1114. ExpectIntEQ(load_file(keyFile, &serverKey, &serverKeySz), 0);
  1115. /* Base normal case. */
  1116. rootSz = do_dual_alg_root_certgen(&root, keyFile, sapkiFile, altPrivFile);
  1117. ExpectNotNull(root);
  1118. ExpectIntGT(rootSz, 0);
  1119. serverSz = do_dual_alg_server_certgen(&server, keyFile, sapkiFile,
  1120. altPrivFile, keyFile, root, rootSz);
  1121. ExpectNotNull(server);
  1122. ExpectIntGT(serverSz, 0);
  1123. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1124. server, serverSz, serverKey, (word32)serverKeySz, 0),
  1125. TEST_SUCCESS);
  1126. XFREE(root, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1127. XFREE(server, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1128. /* Now we try a negative case. Note that we use wrongPrivFile to generate
  1129. * the alternative signature and then set negative_test to true for the
  1130. * call to do_dual_alg_tls13_connection(). Its expecting a failed connection
  1131. * because the signature won't verify. The exception is if
  1132. * WOLFSSL_TRUST_PEER_CERT is defined. In that case, no verfication happens
  1133. * and this is no longer a negative test. */
  1134. rootSz = do_dual_alg_root_certgen(&root, keyFile, sapkiFile, wrongPrivFile);
  1135. ExpectNotNull(root);
  1136. ExpectIntGT(rootSz, 0);
  1137. serverSz = do_dual_alg_server_certgen(&server, keyFile, sapkiFile,
  1138. wrongPrivFile, keyFile, root, rootSz);
  1139. ExpectNotNull(server);
  1140. ExpectIntGT(serverSz, 0);
  1141. #ifdef WOLFSSL_TRUST_PEER_CERT
  1142. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1143. server, serverSz, serverKey, (word32)serverKeySz, 0),
  1144. TEST_SUCCESS);
  1145. #else
  1146. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1147. server, serverSz, serverKey, (word32)serverKeySz, 1),
  1148. TEST_SUCCESS);
  1149. #endif
  1150. /* Lets see if CertManager can find the new extensions */
  1151. extCount = 0;
  1152. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1153. wolfSSL_CertManagerSetUnknownExtCallback(cm, myUnknownExtCallback);
  1154. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, root, rootSz,
  1155. SSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1156. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server, serverSz,
  1157. SSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1158. /* There is only 1 unknown extension (1.2.3.4.5). The other ones are known
  1159. * because they are for the dual alg extensions. */
  1160. ExpectIntEQ(extCount, 1);
  1161. wolfSSL_CertManagerFree(cm);
  1162. XFREE(root, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1163. XFREE(server, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1164. free(serverKey);
  1165. return EXPECT_RESULT();
  1166. }
  1167. #else
  1168. static int test_dual_alg_support(void)
  1169. {
  1170. return TEST_SKIPPED;
  1171. }
  1172. #endif /* WOLFSSL_DUAL_ALG_CERTS && !NO_FILESYSTEM */
  1173. /*----------------------------------------------------------------------------*
  1174. | Context
  1175. *----------------------------------------------------------------------------*/
  1176. #ifndef NO_WOLFSSL_SERVER
  1177. static int test_wolfSSL_CTX_new(void)
  1178. {
  1179. EXPECT_DECLS;
  1180. WOLFSSL_CTX *ctx;
  1181. WOLFSSL_METHOD* method;
  1182. ExpectNull(ctx = wolfSSL_CTX_new(NULL));
  1183. ExpectNotNull(method = wolfSSLv23_server_method());
  1184. ExpectNotNull(ctx = wolfSSL_CTX_new(method));
  1185. wolfSSL_CTX_free(ctx);
  1186. return EXPECT_RESULT();
  1187. }
  1188. #endif
  1189. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  1190. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  1191. static int test_for_double_Free(void)
  1192. {
  1193. EXPECT_DECLS;
  1194. WOLFSSL_CTX* ctx = NULL;
  1195. WOLFSSL* ssl = NULL;
  1196. int skipTest = 0;
  1197. const char* testCertFile;
  1198. const char* testKeyFile;
  1199. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  1200. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  1201. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  1202. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  1203. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  1204. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  1205. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  1206. "NULL-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  1207. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  1208. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  1209. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  1210. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  1211. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  1212. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  1213. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  1214. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  1215. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  1216. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  1217. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  1218. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  1219. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  1220. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  1221. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  1222. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  1223. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  1224. "LY1305-OLD:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  1225. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  1226. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  1227. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  1228. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  1229. /* OpenVPN uses a "blacklist" method to specify which ciphers NOT to use */
  1230. #ifdef OPENSSL_EXTRA
  1231. char openvpnCiphers[] = "DEFAULT:!EXP:!LOW:!MEDIUM:!kDH:!kECDH:!DSS:!PSK:"
  1232. "!SRP:!kRSA:!aNULL:!eNULL";
  1233. #endif
  1234. #ifndef NO_RSA
  1235. testCertFile = svrCertFile;
  1236. testKeyFile = svrKeyFile;
  1237. #elif defined(HAVE_ECC)
  1238. testCertFile = eccCertFile;
  1239. testKeyFile = eccKeyFile;
  1240. #else
  1241. skipTest = 1;
  1242. #endif
  1243. if (skipTest != 1) {
  1244. #ifndef NO_WOLFSSL_SERVER
  1245. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1246. #else
  1247. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1248. #endif
  1249. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1250. WOLFSSL_FILETYPE_PEM));
  1251. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1252. WOLFSSL_FILETYPE_PEM));
  1253. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1254. /* First test freeing SSL, then CTX */
  1255. wolfSSL_free(ssl);
  1256. ssl = NULL;
  1257. wolfSSL_CTX_free(ctx);
  1258. ctx = NULL;
  1259. #ifndef NO_WOLFSSL_CLIENT
  1260. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1261. #else
  1262. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1263. #endif
  1264. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1265. WOLFSSL_FILETYPE_PEM));
  1266. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1267. WOLFSSL_FILETYPE_PEM));
  1268. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1269. /* Next test freeing CTX then SSL */
  1270. wolfSSL_CTX_free(ctx);
  1271. ctx = NULL;
  1272. wolfSSL_free(ssl);
  1273. ssl = NULL;
  1274. #ifndef NO_WOLFSSL_SERVER
  1275. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1276. #else
  1277. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1278. #endif
  1279. /* Test setting ciphers at ctx level */
  1280. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1281. WOLFSSL_FILETYPE_PEM));
  1282. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1283. WOLFSSL_FILETYPE_PEM));
  1284. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  1285. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  1286. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  1287. /* only update TLSv13 suites */
  1288. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  1289. #endif
  1290. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  1291. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  1292. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  1293. /* only update pre-TLSv13 suites */
  1294. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx,
  1295. "ECDHE-RSA-AES128-GCM-SHA256"));
  1296. #endif
  1297. #ifdef OPENSSL_EXTRA
  1298. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, openvpnCiphers));
  1299. #endif
  1300. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1301. wolfSSL_CTX_free(ctx);
  1302. ctx = NULL;
  1303. wolfSSL_free(ssl);
  1304. ssl = NULL;
  1305. #ifndef NO_WOLFSSL_CLIENT
  1306. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1307. #else
  1308. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1309. #endif
  1310. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1311. WOLFSSL_FILETYPE_PEM));
  1312. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1313. WOLFSSL_FILETYPE_PEM));
  1314. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1315. /* test setting ciphers at SSL level */
  1316. ExpectTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  1317. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  1318. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  1319. /* only update TLSv13 suites */
  1320. ExpectTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  1321. #endif
  1322. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  1323. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  1324. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  1325. /* only update pre-TLSv13 suites */
  1326. ExpectTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  1327. #endif
  1328. wolfSSL_CTX_free(ctx);
  1329. ctx = NULL;
  1330. wolfSSL_free(ssl);
  1331. ssl = NULL;
  1332. }
  1333. return EXPECT_RESULT();
  1334. }
  1335. #endif
  1336. static int test_wolfSSL_CTX_set_cipher_list_bytes(void)
  1337. {
  1338. EXPECT_DECLS;
  1339. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)) && \
  1340. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  1341. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  1342. const char* testCertFile;
  1343. const char* testKeyFile;
  1344. WOLFSSL_CTX* ctx = NULL;
  1345. WOLFSSL* ssl = NULL;
  1346. const byte cipherList[] =
  1347. {
  1348. /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x16,
  1349. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x39,
  1350. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x33,
  1351. /* TLS_DH_anon_WITH_AES_128_CBC_SHA */ 0xC0, 0x34,
  1352. /* TLS_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x35,
  1353. /* TLS_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x2F,
  1354. /* TLS_RSA_WITH_NULL_MD5 */ 0xC0, 0x01,
  1355. /* TLS_RSA_WITH_NULL_SHA */ 0xC0, 0x02,
  1356. /* TLS_PSK_WITH_AES_256_CBC_SHA */ 0xC0, 0x8d,
  1357. /* TLS_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0xae,
  1358. /* TLS_PSK_WITH_AES_256_CBC_SHA384 */ 0xC0, 0xaf,
  1359. /* TLS_PSK_WITH_AES_128_CBC_SHA */ 0xC0, 0x8c,
  1360. /* TLS_PSK_WITH_NULL_SHA256 */ 0xC0, 0xb0,
  1361. /* TLS_PSK_WITH_NULL_SHA384 */ 0xC0, 0xb1,
  1362. /* TLS_PSK_WITH_NULL_SHA */ 0xC0, 0x2c,
  1363. /* SSL_RSA_WITH_RC4_128_SHA */ 0xC0, 0x05,
  1364. /* SSL_RSA_WITH_RC4_128_MD5 */ 0xC0, 0x04,
  1365. /* SSL_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0A,
  1366. /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
  1367. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x14,
  1368. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x13,
  1369. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0A,
  1370. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x09,
  1371. /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */ 0xC0, 0x11,
  1372. /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x07,
  1373. /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x12,
  1374. /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x08,
  1375. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x27,
  1376. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256*/ 0xC0, 0x23,
  1377. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x28,
  1378. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384*/ 0xC0, 0x24,
  1379. /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */ 0xC0, 0x06,
  1380. /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */ 0xC0, 0x3a,
  1381. /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x37,
  1382. /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
  1383. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0F,
  1384. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x0E,
  1385. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x05,
  1386. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x04,
  1387. /* TLS_ECDH_RSA_WITH_RC4_128_SHA */ 0xC0, 0x0C,
  1388. /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x02,
  1389. /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0D,
  1390. /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x03,
  1391. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x29,
  1392. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x25,
  1393. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x2A,
  1394. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x26,
  1395. /* WDM_WITH_NULL_SHA256 */ 0x00, 0xFE, /* wolfSSL DTLS Multicast */
  1396. /* SHA256 */
  1397. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x6b,
  1398. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x67,
  1399. /* TLS_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x3d,
  1400. /* TLS_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x3c,
  1401. /* TLS_RSA_WITH_NULL_SHA256 */ 0x00, 0x3b,
  1402. /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */ 0x00, 0xb2,
  1403. /* TLS_DHE_PSK_WITH_NULL_SHA256 */ 0x00, 0xb4,
  1404. /* SHA384 */
  1405. /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */ 0x00, 0xb3,
  1406. /* TLS_DHE_PSK_WITH_NULL_SHA384 */ 0x00, 0xb5,
  1407. /* AES-GCM */
  1408. /* TLS_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9c,
  1409. /* TLS_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9d,
  1410. /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9e,
  1411. /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9f,
  1412. /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa7,
  1413. /* TLS_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xa8,
  1414. /* TLS_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa9,
  1415. /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xaa,
  1416. /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xab,
  1417. /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
  1418. /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2b,
  1419. /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2c,
  1420. /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2d,
  1421. /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2e,
  1422. /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2f,
  1423. /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x30,
  1424. /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x31,
  1425. /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x32,
  1426. /* AES-CCM, first byte is 0xC0 but isn't ECC,
  1427. * also, in some of the other AES-CCM suites
  1428. * there will be second byte number conflicts
  1429. * with non-ECC AES-GCM */
  1430. /* TLS_RSA_WITH_AES_128_CCM_8 */ 0xC0, 0xa0,
  1431. /* TLS_RSA_WITH_AES_256_CCM_8 */ 0xC0, 0xa1,
  1432. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */ 0xC0, 0xac,
  1433. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */ 0xC0, 0xae,
  1434. /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */ 0xC0, 0xaf,
  1435. /* TLS_PSK_WITH_AES_128_CCM */ 0xC0, 0xa4,
  1436. /* TLS_PSK_WITH_AES_256_CCM */ 0xC0, 0xa5,
  1437. /* TLS_PSK_WITH_AES_128_CCM_8 */ 0xC0, 0xa8,
  1438. /* TLS_PSK_WITH_AES_256_CCM_8 */ 0xC0, 0xa9,
  1439. /* TLS_DHE_PSK_WITH_AES_128_CCM */ 0xC0, 0xa6,
  1440. /* TLS_DHE_PSK_WITH_AES_256_CCM */ 0xC0, 0xa7,
  1441. /* Camellia */
  1442. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x41,
  1443. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x84,
  1444. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xba,
  1445. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc0,
  1446. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x45,
  1447. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x88,
  1448. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xbe,
  1449. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc4,
  1450. /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
  1451. /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa8,
  1452. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa9,
  1453. /* TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xaa,
  1454. /* TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xac,
  1455. /* TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xab,
  1456. /* TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xad,
  1457. /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
  1458. /* TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x13,
  1459. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x14,
  1460. /* TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x15,
  1461. /* ECDHE_PSK RFC8442, first byte is 0xD0 (ECDHE_PSK_BYTE) */
  1462. /* TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 */ 0xD0, 0x01,
  1463. /* TLS v1.3 cipher suites */
  1464. /* TLS_AES_128_GCM_SHA256 */ 0x13, 0x01,
  1465. /* TLS_AES_256_GCM_SHA384 */ 0x13, 0x02,
  1466. /* TLS_CHACHA20_POLY1305_SHA256 */ 0x13, 0x03,
  1467. /* TLS_AES_128_CCM_SHA256 */ 0x13, 0x04,
  1468. /* TLS_AES_128_CCM_8_SHA256 */ 0x13, 0x05,
  1469. /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
  1470. /* TLS_SHA256_SHA256 */ 0xC0, 0xB4,
  1471. /* TLS_SHA384_SHA384 */ 0xC0, 0xB5
  1472. };
  1473. #ifndef NO_RSA
  1474. testCertFile = svrCertFile;
  1475. testKeyFile = svrKeyFile;
  1476. #elif defined(HAVE_ECC)
  1477. testCertFile = eccCertFile;
  1478. testKeyFile = eccKeyFile;
  1479. #endif
  1480. #ifndef NO_WOLFSSL_SERVER
  1481. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1482. #else
  1483. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1484. #endif
  1485. ExpectTrue(wolfSSL_CTX_set_cipher_list_bytes(ctx, &cipherList[0U],
  1486. sizeof(cipherList)));
  1487. wolfSSL_CTX_free(ctx);
  1488. ctx = NULL;
  1489. #ifndef NO_WOLFSSL_SERVER
  1490. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1491. #else
  1492. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1493. #endif
  1494. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1495. WOLFSSL_FILETYPE_PEM));
  1496. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1497. WOLFSSL_FILETYPE_PEM));
  1498. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1499. ExpectTrue(wolfSSL_set_cipher_list_bytes(ssl, &cipherList[0U],
  1500. sizeof(cipherList)));
  1501. wolfSSL_free(ssl);
  1502. wolfSSL_CTX_free(ctx);
  1503. #endif /* (OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES) &&
  1504. (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && (!NO_RSA || HAVE_ECC) */
  1505. return EXPECT_RESULT();
  1506. }
  1507. static int test_wolfSSL_CTX_use_certificate_file(void)
  1508. {
  1509. EXPECT_DECLS;
  1510. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1511. WOLFSSL_CTX *ctx = NULL;
  1512. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1513. /* invalid context */
  1514. ExpectFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  1515. WOLFSSL_FILETYPE_PEM));
  1516. /* invalid cert file */
  1517. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  1518. WOLFSSL_FILETYPE_PEM));
  1519. /* invalid cert type */
  1520. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  1521. #ifdef NO_RSA
  1522. /* rsa needed */
  1523. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1524. WOLFSSL_FILETYPE_PEM));
  1525. #else
  1526. /* success */
  1527. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1528. WOLFSSL_FILETYPE_PEM));
  1529. #endif
  1530. wolfSSL_CTX_free(ctx);
  1531. #endif
  1532. return EXPECT_RESULT();
  1533. }
  1534. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  1535. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  1536. {
  1537. EXPECT_DECLS;
  1538. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  1539. WOLFSSL_CTX* ctx = NULL;
  1540. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1541. ExpectIntEQ(SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  1542. server_cert_der_2048), WOLFSSL_SUCCESS);
  1543. wolfSSL_CTX_free(ctx);
  1544. #endif
  1545. return EXPECT_RESULT();
  1546. }
  1547. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  1548. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  1549. * context using buffer.
  1550. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  1551. * --enable-testcert flag.
  1552. */
  1553. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  1554. {
  1555. EXPECT_DECLS;
  1556. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  1557. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  1558. WOLFSSL_CTX* ctx = NULL;
  1559. int ret;
  1560. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1561. ExpectIntEQ(ret = wolfSSL_CTX_use_certificate_buffer(ctx,
  1562. server_cert_der_2048, sizeof_server_cert_der_2048,
  1563. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1564. wolfSSL_CTX_free(ctx);
  1565. #endif
  1566. return EXPECT_RESULT();
  1567. } /* END test_wolfSSL_CTX_use_certificate_buffer */
  1568. static int test_wolfSSL_CTX_use_PrivateKey_file(void)
  1569. {
  1570. EXPECT_DECLS;
  1571. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1572. WOLFSSL_CTX *ctx = NULL;
  1573. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1574. /* invalid context */
  1575. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  1576. WOLFSSL_FILETYPE_PEM));
  1577. /* invalid key file */
  1578. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  1579. WOLFSSL_FILETYPE_PEM));
  1580. /* invalid key type */
  1581. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  1582. /* success */
  1583. #ifdef NO_RSA
  1584. /* rsa needed */
  1585. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1586. WOLFSSL_FILETYPE_PEM));
  1587. #else
  1588. /* success */
  1589. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1590. WOLFSSL_FILETYPE_PEM));
  1591. #endif
  1592. wolfSSL_CTX_free(ctx);
  1593. #endif
  1594. return EXPECT_RESULT();
  1595. }
  1596. /* test both file and buffer versions along with unloading trusted peer certs */
  1597. static int test_wolfSSL_CTX_trust_peer_cert(void)
  1598. {
  1599. EXPECT_DECLS;
  1600. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1601. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_RSA)
  1602. WOLFSSL_CTX *ctx = NULL;
  1603. WOLFSSL* ssl = NULL;
  1604. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1605. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1606. #if !defined(NO_FILESYSTEM)
  1607. /* invalid file */
  1608. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  1609. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1610. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  1611. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1612. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1613. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1614. /* success */
  1615. ExpectIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1616. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1617. /* unload cert */
  1618. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1619. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1620. /* invalid file */
  1621. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
  1622. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1623. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
  1624. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1625. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1626. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1627. /* success */
  1628. ExpectIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1629. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1630. #ifdef WOLFSSL_LOCAL_X509_STORE
  1631. /* unload cert */
  1632. ExpectIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1633. ExpectIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
  1634. #endif
  1635. #endif
  1636. /* Test of loading certs from buffers */
  1637. /* invalid buffer */
  1638. ExpectIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  1639. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1640. /* success */
  1641. #ifdef USE_CERT_BUFFERS_1024
  1642. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  1643. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1644. #endif
  1645. #ifdef USE_CERT_BUFFERS_2048
  1646. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  1647. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1648. #endif
  1649. /* unload cert */
  1650. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1651. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1652. wolfSSL_free(ssl);
  1653. wolfSSL_CTX_free(ctx);
  1654. #endif
  1655. return EXPECT_RESULT();
  1656. }
  1657. static int test_wolfSSL_CTX_load_verify_locations(void)
  1658. {
  1659. EXPECT_DECLS;
  1660. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  1661. WOLFSSL_CTX *ctx = NULL;
  1662. #ifndef NO_RSA
  1663. WOLFSSL_CERT_MANAGER* cm = NULL;
  1664. #ifdef PERSIST_CERT_CACHE
  1665. int cacheSz = 0;
  1666. unsigned char* cache = NULL;
  1667. int used = 0;
  1668. #ifndef NO_FILESYSTEM
  1669. const char* cacheFile = "./tests/cert_cache.tmp";
  1670. #endif
  1671. int i;
  1672. int t;
  1673. int* p;
  1674. #endif
  1675. #endif
  1676. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1677. const char* load_certs_path = "./certs/external";
  1678. const char* load_no_certs_path = "./examples";
  1679. const char* load_expired_path = "./certs/test/expired";
  1680. #endif
  1681. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1682. /* invalid arguments */
  1683. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL),
  1684. WOLFSSL_FAILURE);
  1685. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL),
  1686. WOLFSSL_FAILURE);
  1687. /* invalid ca file */
  1688. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  1689. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  1690. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  1691. ((defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)) && \
  1692. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  1693. /* invalid path */
  1694. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  1695. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  1696. #endif
  1697. #if defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)
  1698. /* test ignoring the invalid path */
  1699. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, bogusFile,
  1700. WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR), WOLFSSL_SUCCESS);
  1701. #endif
  1702. /* load ca cert */
  1703. #ifdef NO_RSA
  1704. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1705. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  1706. #else /* Skip the following test without RSA certs. */
  1707. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1708. WOLFSSL_SUCCESS);
  1709. #ifdef PERSIST_CERT_CACHE
  1710. /* Get cert cache size */
  1711. ExpectIntGT(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), 0);
  1712. ExpectNotNull(cache = (byte*)XMALLOC(cacheSz, NULL,
  1713. DYNAMIC_TYPE_TMP_BUFFER));
  1714. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, NULL),
  1715. BAD_FUNC_ARG);
  1716. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, -1, NULL),
  1717. BAD_FUNC_ARG);
  1718. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, -1, NULL),
  1719. BAD_FUNC_ARG);
  1720. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, cacheSz, NULL),
  1721. BAD_FUNC_ARG);
  1722. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, &used),
  1723. BAD_FUNC_ARG);
  1724. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, cacheSz, &used),
  1725. BAD_FUNC_ARG);
  1726. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, cacheSz, &used),
  1727. BAD_FUNC_ARG);
  1728. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, -1, &used),
  1729. BAD_FUNC_ARG);
  1730. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, NULL),
  1731. BAD_FUNC_ARG);
  1732. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz - 10, &used),
  1733. BUFFER_E);
  1734. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, &used), 1);
  1735. ExpectIntEQ(cacheSz, used);
  1736. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, -1),
  1737. BAD_FUNC_ARG);
  1738. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, -1),
  1739. BAD_FUNC_ARG);
  1740. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, -1),
  1741. BAD_FUNC_ARG);
  1742. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, cacheSz),
  1743. BAD_FUNC_ARG);
  1744. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, cacheSz),
  1745. BAD_FUNC_ARG);
  1746. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, cacheSz),
  1747. BAD_FUNC_ARG);
  1748. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, -1),
  1749. BAD_FUNC_ARG);
  1750. /* Smaller than header. */
  1751. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, 1), BUFFER_E);
  1752. for (i = 1; i < cacheSz; i++) {
  1753. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz - i),
  1754. BUFFER_E);
  1755. }
  1756. if (EXPECT_SUCCESS()) {
  1757. /* Modify header for bad results! */
  1758. p = (int*)cache;
  1759. /* version */
  1760. t = p[0]; p[0] = 0xff;
  1761. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1762. CACHE_MATCH_ERROR);
  1763. p[0] = t; p++;
  1764. /* rows */
  1765. t = p[0]; p[0] = 0xff;
  1766. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1767. CACHE_MATCH_ERROR);
  1768. p[0] = t; p++;
  1769. /* columns[0] */
  1770. t = p[0]; p[0] = -1;
  1771. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1772. PARSE_ERROR);
  1773. p[0] = t; p += CA_TABLE_SIZE;
  1774. /* signerSz*/
  1775. t = p[0]; p[0] = 0xff;
  1776. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1777. CACHE_MATCH_ERROR);
  1778. p[0] = t;
  1779. }
  1780. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz), 1);
  1781. ExpectIntEQ(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), used);
  1782. #ifndef NO_FILESYSTEM
  1783. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1784. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1785. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1786. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, cacheFile), 1);
  1787. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1788. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1789. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1790. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "no-file"),
  1791. WOLFSSL_BAD_FILE);
  1792. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, cacheFile), 1);
  1793. /* File contents is not a cache. */
  1794. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "./certs/ca-cert.pem"),
  1795. CACHE_MATCH_ERROR);
  1796. #endif
  1797. XFREE(cache, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1798. #endif
  1799. /* Test unloading CA's */
  1800. ExpectIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  1801. #ifdef PERSIST_CERT_CACHE
  1802. /* Verify no certs (result is less than cacheSz) */
  1803. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1804. #endif
  1805. /* load ca cert again */
  1806. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1807. WOLFSSL_SUCCESS);
  1808. /* Test getting CERT_MANAGER */
  1809. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  1810. /* Test unloading CA's using CM */
  1811. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  1812. #ifdef PERSIST_CERT_CACHE
  1813. /* Verify no certs (result is less than cacheSz) */
  1814. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1815. #endif
  1816. #endif
  1817. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1818. /* Test loading CA certificates using a path */
  1819. #ifdef NO_RSA
  1820. /* failure here okay since certs in external directory are RSA */
  1821. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1822. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1823. #else
  1824. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1825. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1826. #endif
  1827. /* Test loading path with no files */
  1828. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1829. load_no_certs_path, WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  1830. /* Test loading expired CA certificates */
  1831. #ifdef NO_RSA
  1832. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1833. load_expired_path,
  1834. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1835. WOLFSSL_SUCCESS);
  1836. #else
  1837. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1838. load_expired_path,
  1839. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1840. WOLFSSL_SUCCESS);
  1841. #endif
  1842. /* Test loading CA certificates and ignoring all errors */
  1843. #ifdef NO_RSA
  1844. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1845. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  1846. #else
  1847. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1848. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  1849. #endif
  1850. #endif
  1851. wolfSSL_CTX_free(ctx);
  1852. #endif
  1853. return EXPECT_RESULT();
  1854. }
  1855. static int test_wolfSSL_CTX_load_system_CA_certs(void)
  1856. {
  1857. int res = TEST_SKIPPED;
  1858. #if defined(WOLFSSL_SYS_CA_CERTS) && !defined(NO_WOLFSSL_CLIENT) && \
  1859. (!defined(NO_RSA) || defined(HAVE_ECC))
  1860. WOLFSSL_CTX* ctx;
  1861. byte dirValid = 0;
  1862. int ret = 0;
  1863. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1864. if (ctx == NULL) {
  1865. fprintf(stderr, "wolfSSL_CTX_new failed.\n");
  1866. ret = -1;
  1867. }
  1868. if (ret == 0) {
  1869. #if defined(USE_WINDOWS_API) || defined(__APPLE__)
  1870. dirValid = 1;
  1871. #else
  1872. word32 numDirs;
  1873. const char** caDirs = wolfSSL_get_system_CA_dirs(&numDirs);
  1874. if (caDirs == NULL || numDirs == 0) {
  1875. fprintf(stderr, "wolfSSL_get_system_CA_dirs failed.\n");
  1876. ret = -1;
  1877. }
  1878. else {
  1879. ReadDirCtx dirCtx;
  1880. word32 i;
  1881. for (i = 0; i < numDirs; ++i) {
  1882. if (wc_ReadDirFirst(&dirCtx, caDirs[i], NULL) == 0) {
  1883. /* Directory isn't empty. */
  1884. dirValid = 1;
  1885. wc_ReadDirClose(&dirCtx);
  1886. break;
  1887. }
  1888. }
  1889. }
  1890. #endif
  1891. }
  1892. /*
  1893. * If the directory isn't empty, we should be able to load CA
  1894. * certs from it. On Windows/Mac, we assume the CA cert stores are
  1895. * usable.
  1896. */
  1897. if (ret == 0 && dirValid && wolfSSL_CTX_load_system_CA_certs(ctx) !=
  1898. WOLFSSL_SUCCESS) {
  1899. fprintf(stderr, "wolfSSL_CTX_load_system_CA_certs failed.\n");
  1900. ret = -1;
  1901. }
  1902. #ifdef OPENSSL_EXTRA
  1903. if (ret == 0 &&
  1904. wolfSSL_CTX_set_default_verify_paths(ctx) != WOLFSSL_SUCCESS) {
  1905. fprintf(stderr, "wolfSSL_CTX_set_default_verify_paths failed.\n");
  1906. ret = -1;
  1907. }
  1908. #endif /* OPENSSL_EXTRA */
  1909. wolfSSL_CTX_free(ctx);
  1910. res = TEST_RES_CHECK(ret == 0);
  1911. #endif /* WOLFSSL_SYS_CA_CERTS && !NO_WOLFSSL_CLIENT */
  1912. return res;
  1913. }
  1914. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1915. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz,
  1916. int file_type)
  1917. {
  1918. int ret;
  1919. WOLFSSL_CERT_MANAGER* cm;
  1920. cm = wolfSSL_CertManagerNew();
  1921. if (cm == NULL) {
  1922. fprintf(stderr, "test_cm_load_ca failed\n");
  1923. return -1;
  1924. }
  1925. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  1926. wolfSSL_CertManagerFree(cm);
  1927. return ret;
  1928. }
  1929. static int test_cm_load_ca_file(const char* ca_cert_file)
  1930. {
  1931. int ret = 0;
  1932. byte* cert_buf = NULL;
  1933. size_t cert_sz = 0;
  1934. #if defined(WOLFSSL_PEM_TO_DER)
  1935. DerBuffer* pDer = NULL;
  1936. #endif
  1937. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1938. if (ret == 0) {
  1939. /* normal test */
  1940. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  1941. if (ret == WOLFSSL_SUCCESS) {
  1942. /* test including null terminator in length */
  1943. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1944. if (tmp == NULL) {
  1945. ret = MEMORY_E;
  1946. }
  1947. else {
  1948. cert_buf = tmp;
  1949. cert_buf[cert_sz] = '\0';
  1950. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  1951. WOLFSSL_FILETYPE_PEM);
  1952. }
  1953. }
  1954. #if defined(WOLFSSL_PEM_TO_DER)
  1955. if (ret == WOLFSSL_SUCCESS) {
  1956. /* test loading DER */
  1957. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1958. if (ret == 0 && pDer != NULL) {
  1959. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  1960. WOLFSSL_FILETYPE_ASN1);
  1961. wc_FreeDer(&pDer);
  1962. }
  1963. }
  1964. #endif
  1965. }
  1966. free(cert_buf);
  1967. return ret;
  1968. }
  1969. static int test_cm_load_ca_buffer_ex(const byte* cert_buf, size_t cert_sz,
  1970. int file_type, word32 flags)
  1971. {
  1972. int ret;
  1973. WOLFSSL_CERT_MANAGER* cm;
  1974. cm = wolfSSL_CertManagerNew();
  1975. if (cm == NULL) {
  1976. fprintf(stderr, "test_cm_load_ca failed\n");
  1977. return -1;
  1978. }
  1979. ret = wolfSSL_CertManagerLoadCABuffer_ex(cm, cert_buf, cert_sz, file_type,
  1980. 0, flags);
  1981. wolfSSL_CertManagerFree(cm);
  1982. return ret;
  1983. }
  1984. static int test_cm_load_ca_file_ex(const char* ca_cert_file, word32 flags)
  1985. {
  1986. int ret = 0;
  1987. byte* cert_buf = NULL;
  1988. size_t cert_sz = 0;
  1989. #if defined(WOLFSSL_PEM_TO_DER)
  1990. DerBuffer* pDer = NULL;
  1991. #endif
  1992. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1993. if (ret == 0) {
  1994. /* normal test */
  1995. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz,
  1996. WOLFSSL_FILETYPE_PEM, flags);
  1997. if (ret == WOLFSSL_SUCCESS) {
  1998. /* test including null terminator in length */
  1999. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  2000. if (tmp == NULL) {
  2001. ret = MEMORY_E;
  2002. }
  2003. else {
  2004. cert_buf = tmp;
  2005. cert_buf[cert_sz] = '\0';
  2006. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz+1,
  2007. WOLFSSL_FILETYPE_PEM, flags);
  2008. }
  2009. }
  2010. #if defined(WOLFSSL_PEM_TO_DER)
  2011. if (ret == WOLFSSL_SUCCESS) {
  2012. /* test loading DER */
  2013. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  2014. if (ret == 0 && pDer != NULL) {
  2015. ret = test_cm_load_ca_buffer_ex(pDer->buffer, pDer->length,
  2016. WOLFSSL_FILETYPE_ASN1, flags);
  2017. wc_FreeDer(&pDer);
  2018. }
  2019. }
  2020. #endif
  2021. }
  2022. free(cert_buf);
  2023. return ret;
  2024. }
  2025. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  2026. static int test_wolfSSL_CertManagerAPI(void)
  2027. {
  2028. EXPECT_DECLS;
  2029. #ifndef NO_CERTS
  2030. WOLFSSL_CERT_MANAGER* cm = NULL;
  2031. unsigned char c;
  2032. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2033. wolfSSL_CertManagerFree(NULL);
  2034. ExpectIntEQ(wolfSSL_CertManager_up_ref(NULL), 0);
  2035. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(NULL), BAD_FUNC_ARG);
  2036. #ifdef WOLFSSL_TRUST_PEER_CERT
  2037. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(NULL), BAD_FUNC_ARG);
  2038. #endif
  2039. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer_ex(NULL, &c, 1,
  2040. WOLFSSL_FILETYPE_ASN1, 0, 0), WOLFSSL_FATAL_ERROR);
  2041. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  2042. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, -1,
  2043. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2044. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, -1,
  2045. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2046. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, -1,
  2047. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2048. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, 1,
  2049. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2050. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, 1,
  2051. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2052. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, 1,
  2053. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2054. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, -1,
  2055. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2056. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, 1, -1),
  2057. WOLFSSL_BAD_FILETYPE);
  2058. #endif
  2059. #if !defined(NO_FILESYSTEM)
  2060. {
  2061. const char* ca_cert = "./certs/ca-cert.pem";
  2062. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  2063. const char* ca_cert_der = "./certs/ca-cert.der";
  2064. #endif
  2065. const char* ca_path = "./certs";
  2066. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  2067. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, NULL, -1),
  2068. BAD_FUNC_ARG);
  2069. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  2070. BAD_FUNC_ARG);
  2071. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, ca_cert,
  2072. WOLFSSL_FILETYPE_PEM), BAD_FUNC_ARG);
  2073. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert, -1),
  2074. WOLFSSL_BAD_FILETYPE);
  2075. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, "no-file",
  2076. WOLFSSL_FILETYPE_ASN1), WOLFSSL_BAD_FILE);
  2077. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert_der,
  2078. WOLFSSL_FILETYPE_PEM), ASN_NO_PEM_HEADER);
  2079. #endif
  2080. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, NULL),
  2081. WOLFSSL_FATAL_ERROR);
  2082. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, NULL),
  2083. WOLFSSL_FATAL_ERROR);
  2084. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, ca_path),
  2085. WOLFSSL_FATAL_ERROR);
  2086. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, ca_path),
  2087. WOLFSSL_FATAL_ERROR);
  2088. }
  2089. #endif
  2090. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  2091. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), 1);
  2092. #elif !defined(HAVE_CRL)
  2093. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), NOT_COMPILED_IN);
  2094. #endif
  2095. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(NULL), BAD_FUNC_ARG);
  2096. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(cm), 1);
  2097. #ifdef HAVE_CRL
  2098. /* Test APIs when CRL is disabled. */
  2099. #ifdef HAVE_CRL_IO
  2100. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  2101. #endif
  2102. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  2103. sizeof_server_cert_der_2048), 1);
  2104. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  2105. #endif
  2106. /* OCSP */
  2107. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(NULL, 0), BAD_FUNC_ARG);
  2108. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(NULL), BAD_FUNC_ARG);
  2109. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(NULL), BAD_FUNC_ARG);
  2110. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(NULL), BAD_FUNC_ARG);
  2111. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  2112. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  2113. #if !defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  2114. !defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2115. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), NOT_COMPILED_IN);
  2116. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), NOT_COMPILED_IN);
  2117. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), NOT_COMPILED_IN);
  2118. #endif
  2119. #ifdef HAVE_OCSP
  2120. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, -1), BAD_FUNC_ARG);
  2121. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, -1), BAD_FUNC_ARG);
  2122. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, -1), BAD_FUNC_ARG);
  2123. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, 1), BAD_FUNC_ARG);
  2124. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, 1), BAD_FUNC_ARG);
  2125. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, 1), BAD_FUNC_ARG);
  2126. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, -1), BAD_FUNC_ARG);
  2127. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, NULL, 0,
  2128. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2129. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, NULL, 1,
  2130. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2131. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, &c, 1,
  2132. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2133. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, NULL),
  2134. BAD_FUNC_ARG);
  2135. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, ""),
  2136. BAD_FUNC_ARG);
  2137. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, NULL), 1);
  2138. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(NULL, NULL, NULL, NULL),
  2139. BAD_FUNC_ARG);
  2140. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(cm, NULL, NULL, NULL), 1);
  2141. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(cm), 1);
  2142. /* Test APIs when OCSP is disabled. */
  2143. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, &c, 1,
  2144. NULL, NULL, NULL, NULL), 1);
  2145. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, 1), 1);
  2146. #endif
  2147. ExpectIntEQ(wolfSSL_CertManager_up_ref(cm), 1);
  2148. if (EXPECT_SUCCESS()) {
  2149. wolfSSL_CertManagerFree(cm);
  2150. }
  2151. wolfSSL_CertManagerFree(cm);
  2152. cm = NULL;
  2153. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2154. #ifdef HAVE_OCSP
  2155. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, WOLFSSL_OCSP_URL_OVERRIDE |
  2156. WOLFSSL_OCSP_CHECKALL), 1);
  2157. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2158. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2159. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2160. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2161. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), 1);
  2162. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2163. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), 1);
  2164. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), 1);
  2165. #endif
  2166. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  2167. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  2168. #endif
  2169. #ifdef WOLFSSL_TRUST_PEER_CERT
  2170. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(cm), 1);
  2171. #endif
  2172. wolfSSL_CertManagerFree(cm);
  2173. #endif
  2174. return EXPECT_RESULT();
  2175. }
  2176. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  2177. {
  2178. EXPECT_DECLS;
  2179. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2180. const char* ca_cert = "./certs/ca-cert.pem";
  2181. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2182. int ret;
  2183. ExpectIntLE(ret = test_cm_load_ca_file(ca_cert), 1);
  2184. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2185. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2186. #elif defined(NO_RSA)
  2187. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2188. #else
  2189. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2190. #endif
  2191. ExpectIntLE(ret = test_cm_load_ca_file(ca_expired_cert), 1);
  2192. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2193. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2194. #elif defined(NO_RSA)
  2195. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2196. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  2197. !defined(NO_ASN_TIME)
  2198. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  2199. #else
  2200. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2201. #endif
  2202. #endif
  2203. return EXPECT_RESULT();
  2204. }
  2205. static int test_wolfSSL_CertManagerLoadCABuffer_ex(void)
  2206. {
  2207. EXPECT_DECLS;
  2208. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2209. const char* ca_cert = "./certs/ca-cert.pem";
  2210. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2211. int ret;
  2212. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_cert, WOLFSSL_LOAD_FLAG_NONE),
  2213. 1);
  2214. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2215. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2216. #elif defined(NO_RSA)
  2217. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2218. #else
  2219. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2220. #endif
  2221. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_expired_cert,
  2222. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), 1);
  2223. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2224. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2225. #elif defined(NO_RSA)
  2226. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2227. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  2228. !defined(NO_ASN_TIME) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  2229. defined(OPENSSL_COMPATIBLE_DEFAULTS)
  2230. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  2231. #else
  2232. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2233. #endif
  2234. #endif
  2235. return EXPECT_RESULT();
  2236. }
  2237. static int test_wolfSSL_CertManagerGetCerts(void)
  2238. {
  2239. EXPECT_DECLS;
  2240. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  2241. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  2242. defined(WOLFSSL_SIGNER_DER_CERT)
  2243. WOLFSSL_CERT_MANAGER* cm = NULL;
  2244. WOLFSSL_STACK* sk = NULL;
  2245. X509* x509 = NULL;
  2246. X509* cert1 = NULL;
  2247. FILE* file1 = NULL;
  2248. #ifdef DEBUG_WOLFSSL_VERBOSE
  2249. WOLFSSL_BIO* bio = NULL;
  2250. #endif
  2251. int i = 0;
  2252. int ret = 0;
  2253. const byte* der = NULL;
  2254. int derSz = 0;
  2255. ExpectNotNull(file1 = fopen("./certs/ca-cert.pem", "rb"));
  2256. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  2257. if (file1 != NULL) {
  2258. fclose(file1);
  2259. }
  2260. ExpectNull(sk = wolfSSL_CertManagerGetCerts(NULL));
  2261. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2262. ExpectNull(sk = wolfSSL_CertManagerGetCerts(cm));
  2263. ExpectNotNull(der = wolfSSL_X509_get_der(cert1, &derSz));
  2264. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  2265. /* Check that ASN_SELF_SIGNED_E is returned for a self-signed cert for QT
  2266. * and full OpenSSL compatibility */
  2267. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2268. WOLFSSL_FILETYPE_ASN1), ASN_SELF_SIGNED_E);
  2269. #else
  2270. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2271. WOLFSSL_FILETYPE_ASN1), ASN_NO_SIGNER_E);
  2272. #endif
  2273. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  2274. "./certs/ca-cert.pem", NULL));
  2275. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  2276. for (i = 0; EXPECT_SUCCESS() && i < sk_X509_num(sk); i++) {
  2277. ExpectNotNull(x509 = sk_X509_value(sk, i));
  2278. ExpectIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  2279. #ifdef DEBUG_WOLFSSL_VERBOSE
  2280. bio = BIO_new(wolfSSL_BIO_s_file());
  2281. if (bio != NULL) {
  2282. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  2283. X509_print(bio, x509);
  2284. BIO_free(bio);
  2285. }
  2286. #endif /* DEBUG_WOLFSSL_VERBOSE */
  2287. }
  2288. wolfSSL_X509_free(cert1);
  2289. sk_X509_pop_free(sk, NULL);
  2290. wolfSSL_CertManagerFree(cm);
  2291. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  2292. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  2293. defined(WOLFSSL_SIGNER_DER_CERT) */
  2294. return EXPECT_RESULT();
  2295. }
  2296. static int test_wolfSSL_CertManagerSetVerify(void)
  2297. {
  2298. EXPECT_DECLS;
  2299. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2300. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2301. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  2302. WOLFSSL_CERT_MANAGER* cm = NULL;
  2303. int tmp = myVerifyAction;
  2304. const char* ca_cert = "./certs/ca-cert.pem";
  2305. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  2306. wolfSSL_CertManagerSetVerify(NULL, NULL);
  2307. wolfSSL_CertManagerSetVerify(NULL, myVerify);
  2308. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2309. wolfSSL_CertManagerSetVerify(cm, myVerify);
  2310. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2311. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL), -1);
  2312. #else
  2313. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL),
  2314. WOLFSSL_SUCCESS);
  2315. #endif
  2316. /* Use the test CB that always accepts certs */
  2317. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  2318. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, expiredCert,
  2319. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  2320. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  2321. {
  2322. const char* verifyCert = "./certs/server-cert.der";
  2323. /* Use the test CB that always fails certs */
  2324. myVerifyAction = VERIFY_FORCE_FAIL;
  2325. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, verifyCert,
  2326. WOLFSSL_FILETYPE_ASN1), VERIFY_CERT_ERROR);
  2327. }
  2328. #endif
  2329. wolfSSL_CertManagerFree(cm);
  2330. myVerifyAction = tmp;
  2331. #endif
  2332. return EXPECT_RESULT();
  2333. }
  2334. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  2335. defined(DEBUG_UNIT_TEST_CERTS)
  2336. /* Used when debugging name constraint tests. Not static to allow use in
  2337. * multiple locations with complex define guards. */
  2338. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  2339. {
  2340. BIO* out = BIO_new_file(fileName, "wb");
  2341. if (out != NULL) {
  2342. PEM_write_bio_X509(out, x509);
  2343. BIO_free(out);
  2344. }
  2345. }
  2346. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  2347. {
  2348. BIO* out = BIO_new_file(fileName, "wb");
  2349. if (out != NULL) {
  2350. BIO_write(out, der, derSz);
  2351. BIO_free(out);
  2352. }
  2353. }
  2354. #else
  2355. #define DEBUG_WRITE_CERT_X509(x509, fileName) WC_DO_NOTHING
  2356. #define DEBUG_WRITE_DER(der, derSz, fileName) WC_DO_NOTHING
  2357. #endif
  2358. static int test_wolfSSL_CertManagerNameConstraint(void)
  2359. {
  2360. EXPECT_DECLS;
  2361. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2362. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2363. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2364. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2365. !defined(NO_SHA256)
  2366. WOLFSSL_CERT_MANAGER* cm = NULL;
  2367. WOLFSSL_EVP_PKEY *priv = NULL;
  2368. WOLFSSL_X509_NAME* name = NULL;
  2369. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  2370. const char* server_cert = "./certs/test/server-goodcn.pem";
  2371. int i = 0;
  2372. static const byte extNameConsOid[] = {85, 29, 30};
  2373. RsaKey key;
  2374. WC_RNG rng;
  2375. byte *der = NULL;
  2376. int derSz = 0;
  2377. word32 idx = 0;
  2378. byte *pt;
  2379. WOLFSSL_X509 *x509 = NULL;
  2380. WOLFSSL_X509 *ca = NULL;
  2381. wc_InitRng(&rng);
  2382. /* load in CA private key for signing */
  2383. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
  2384. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  2385. sizeof_server_key_der_2048), 0);
  2386. /* get ca certificate then alter it */
  2387. ExpectNotNull(der =
  2388. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  2389. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  2390. WOLFSSL_FILETYPE_ASN1));
  2391. ExpectNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  2392. if (EXPECT_SUCCESS() && (der != NULL)) {
  2393. XMEMCPY(der, pt, derSz);
  2394. /* find the name constraint extension and alter it */
  2395. pt = der;
  2396. for (i = 0; i < derSz - 3; i++) {
  2397. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  2398. pt += 3;
  2399. break;
  2400. }
  2401. pt++;
  2402. }
  2403. ExpectIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  2404. /* go to the length value and set it to 0 */
  2405. while (i < derSz && *pt != 0x81) {
  2406. pt++;
  2407. i++;
  2408. }
  2409. ExpectIntNE(i, derSz); /* did not place to alter */
  2410. pt++;
  2411. *pt = 0x00;
  2412. }
  2413. /* resign the altered certificate */
  2414. ExpectIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  2415. FOURK_BUF, &key, NULL, &rng)), 0);
  2416. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2417. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2418. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  2419. wolfSSL_CertManagerFree(cm);
  2420. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2421. wolfSSL_X509_free(x509);
  2422. wc_FreeRsaKey(&key);
  2423. wc_FreeRng(&rng);
  2424. /* add email alt name to satisfy constraint */
  2425. pt = (byte*)server_key_der_2048;
  2426. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2427. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2428. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2429. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2430. WOLFSSL_FILETYPE_ASN1));
  2431. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2432. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2433. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2434. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2435. /* Good cert test with proper alt email name */
  2436. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2437. WOLFSSL_FILETYPE_PEM));
  2438. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2439. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2440. name = NULL;
  2441. ExpectNotNull(name = X509_NAME_new());
  2442. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2443. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2444. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2445. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2446. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2447. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2448. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2449. X509_NAME_free(name);
  2450. name = NULL;
  2451. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2452. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2453. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2454. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2455. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2456. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2457. wolfSSL_X509_free(x509);
  2458. x509 = NULL;
  2459. /* Cert with bad alt name list */
  2460. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2461. WOLFSSL_FILETYPE_PEM));
  2462. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2463. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2464. name = NULL;
  2465. ExpectNotNull(name = X509_NAME_new());
  2466. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2467. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2468. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2469. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2470. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2471. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2472. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2473. X509_NAME_free(name);
  2474. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2475. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2476. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2477. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2478. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2479. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2480. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2481. wolfSSL_CertManagerFree(cm);
  2482. wolfSSL_X509_free(x509);
  2483. wolfSSL_X509_free(ca);
  2484. wolfSSL_EVP_PKEY_free(priv);
  2485. #endif
  2486. return EXPECT_RESULT();
  2487. }
  2488. static int test_wolfSSL_CertManagerNameConstraint2(void)
  2489. {
  2490. EXPECT_DECLS;
  2491. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2492. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2493. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2494. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  2495. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  2496. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  2497. const char* server_cert = "./certs/server-cert.pem";
  2498. WOLFSSL_CERT_MANAGER* cm = NULL;
  2499. WOLFSSL_X509 *x509 = NULL;
  2500. WOLFSSL_X509 *ca = NULL;
  2501. const unsigned char *der = NULL;
  2502. const unsigned char *pt;
  2503. WOLFSSL_EVP_PKEY *priv = NULL;
  2504. WOLFSSL_X509_NAME* name = NULL;
  2505. int derSz = 0;
  2506. /* C=US*/
  2507. char altName[] = {
  2508. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2509. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  2510. };
  2511. /* C=ID */
  2512. char altNameFail[] = {
  2513. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2514. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  2515. };
  2516. /* C=US ST=California*/
  2517. char altNameExc[] = {
  2518. 0x30, 0x22,
  2519. 0x31, 0x0B,
  2520. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  2521. 0x31, 0x13,
  2522. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  2523. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  2524. };
  2525. /* load in CA private key for signing */
  2526. pt = ca_key_der_2048;
  2527. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  2528. sizeof_ca_key_der_2048));
  2529. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2530. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2531. WOLFSSL_FILETYPE_ASN1));
  2532. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2533. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2534. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2535. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2536. WOLFSSL_FILETYPE_PEM));
  2537. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2538. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2539. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2540. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2541. #else
  2542. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2543. #endif
  2544. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2545. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2546. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2547. /* add in matching DIR alt name and resign */
  2548. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2549. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2550. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2551. #else
  2552. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2553. #endif
  2554. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2555. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2556. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2557. wolfSSL_X509_free(x509);
  2558. x509 = NULL;
  2559. /* check verify fail */
  2560. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2561. WOLFSSL_FILETYPE_PEM));
  2562. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2563. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2564. /* add in miss matching DIR alt name and resign */
  2565. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2566. ASN_DIR_TYPE);
  2567. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2568. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2569. #else
  2570. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2571. #endif
  2572. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2573. #ifndef WOLFSSL_NO_ASN_STRICT
  2574. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2575. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2576. #else
  2577. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2578. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2579. #endif
  2580. /* check that it still fails if one bad altname and one good altname is in
  2581. * the certificate */
  2582. wolfSSL_X509_free(x509);
  2583. x509 = NULL;
  2584. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2585. WOLFSSL_FILETYPE_PEM));
  2586. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2587. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2588. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2589. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2590. ASN_DIR_TYPE);
  2591. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2592. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2593. #else
  2594. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2595. #endif
  2596. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2597. #ifndef WOLFSSL_NO_ASN_STRICT
  2598. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2599. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2600. #else
  2601. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2602. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2603. #endif
  2604. /* check it fails with switching position of bad altname */
  2605. wolfSSL_X509_free(x509);
  2606. x509 = NULL;
  2607. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2608. WOLFSSL_FILETYPE_PEM));
  2609. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2610. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2611. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2612. ASN_DIR_TYPE);
  2613. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2614. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2615. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2616. #else
  2617. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2618. #endif
  2619. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2620. #ifndef WOLFSSL_NO_ASN_STRICT
  2621. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2622. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2623. #else
  2624. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2625. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2626. #endif
  2627. wolfSSL_CertManagerFree(cm);
  2628. wolfSSL_X509_free(x509);
  2629. x509 = NULL;
  2630. wolfSSL_X509_free(ca);
  2631. ca = NULL;
  2632. /* now test with excluded name constraint */
  2633. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2634. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  2635. WOLFSSL_FILETYPE_ASN1));
  2636. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2637. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2638. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2639. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2640. WOLFSSL_FILETYPE_PEM));
  2641. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  2642. ASN_DIR_TYPE);
  2643. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2644. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2645. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2646. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2647. #else
  2648. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2649. #endif
  2650. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2651. #ifndef WOLFSSL_NO_ASN_STRICT
  2652. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2653. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2654. #else
  2655. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2656. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2657. #endif
  2658. wolfSSL_CertManagerFree(cm);
  2659. wolfSSL_X509_free(x509);
  2660. wolfSSL_X509_free(ca);
  2661. wolfSSL_EVP_PKEY_free(priv);
  2662. #endif
  2663. return EXPECT_RESULT();
  2664. }
  2665. static int test_wolfSSL_CertManagerNameConstraint3(void)
  2666. {
  2667. EXPECT_DECLS;
  2668. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2669. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2670. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2671. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2672. !defined(NO_SHA256)
  2673. WOLFSSL_CERT_MANAGER* cm = NULL;
  2674. WOLFSSL_EVP_PKEY *priv = NULL;
  2675. WOLFSSL_X509_NAME* name = NULL;
  2676. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  2677. const char* server_cert = "./certs/test/server-goodcn.pem";
  2678. byte *der = NULL;
  2679. int derSz = 0;
  2680. byte *pt;
  2681. WOLFSSL_X509 *x509 = NULL;
  2682. WOLFSSL_X509 *ca = NULL;
  2683. pt = (byte*)server_key_der_2048;
  2684. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2685. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2686. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2687. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2688. WOLFSSL_FILETYPE_ASN1));
  2689. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2690. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2691. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2692. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2693. /* check satisfying .wolfssl.com constraint passes */
  2694. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2695. WOLFSSL_FILETYPE_PEM));
  2696. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2697. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2698. name = NULL;
  2699. ExpectNotNull(name = X509_NAME_new());
  2700. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2701. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2702. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2703. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2704. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2705. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2706. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2707. X509_NAME_free(name);
  2708. name = NULL;
  2709. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2710. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2711. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2712. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2713. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2714. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2715. wolfSSL_X509_free(x509);
  2716. x509 = NULL;
  2717. /* check satisfying .random.com constraint passes */
  2718. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2719. WOLFSSL_FILETYPE_PEM));
  2720. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2721. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2722. name = NULL;
  2723. ExpectNotNull(name = X509_NAME_new());
  2724. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2725. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2726. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2727. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2728. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2729. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  2730. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2731. X509_NAME_free(name);
  2732. name = NULL;
  2733. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  2734. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2735. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2736. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2737. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2738. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2739. wolfSSL_X509_free(x509);
  2740. x509 = NULL;
  2741. /* check fail case when neither constraint is matched */
  2742. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2743. WOLFSSL_FILETYPE_PEM));
  2744. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2745. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2746. name = NULL;
  2747. ExpectNotNull(name = X509_NAME_new());
  2748. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2749. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2750. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2751. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2752. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2753. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  2754. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2755. X509_NAME_free(name);
  2756. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2757. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2758. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2759. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2760. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2761. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2762. wolfSSL_CertManagerFree(cm);
  2763. wolfSSL_X509_free(x509);
  2764. wolfSSL_X509_free(ca);
  2765. wolfSSL_EVP_PKEY_free(priv);
  2766. #endif
  2767. return EXPECT_RESULT();
  2768. }
  2769. static int test_wolfSSL_CertManagerNameConstraint4(void)
  2770. {
  2771. EXPECT_DECLS;
  2772. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2773. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2774. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2775. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2776. !defined(NO_SHA256)
  2777. WOLFSSL_CERT_MANAGER* cm = NULL;
  2778. WOLFSSL_EVP_PKEY *priv = NULL;
  2779. WOLFSSL_X509_NAME* name = NULL;
  2780. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  2781. const char* server_cert = "./certs/test/server-goodcn.pem";
  2782. byte *der = NULL;
  2783. int derSz;
  2784. byte *pt;
  2785. WOLFSSL_X509 *x509 = NULL;
  2786. WOLFSSL_X509 *ca = NULL;
  2787. pt = (byte*)server_key_der_2048;
  2788. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2789. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2790. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2791. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2792. WOLFSSL_FILETYPE_ASN1));
  2793. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2794. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2795. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2796. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2797. /* check satisfying wolfssl.com constraint passes */
  2798. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2799. WOLFSSL_FILETYPE_PEM));
  2800. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2801. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2802. name = NULL;
  2803. ExpectNotNull(name = X509_NAME_new());
  2804. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2805. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2806. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2807. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2808. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2809. X509_NAME_free(name);
  2810. name = NULL;
  2811. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2812. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2813. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2814. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2815. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2816. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2817. wolfSSL_X509_free(x509);
  2818. x509 = NULL;
  2819. /* check satisfying example.com constraint passes */
  2820. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2821. WOLFSSL_FILETYPE_PEM));
  2822. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2823. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2824. name = NULL;
  2825. ExpectNotNull(name = X509_NAME_new());
  2826. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2827. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2828. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2829. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  2830. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2831. X509_NAME_free(name);
  2832. name = NULL;
  2833. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  2834. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2835. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2836. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2837. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2838. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2839. wolfSSL_X509_free(x509);
  2840. x509 = NULL;
  2841. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  2842. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2843. WOLFSSL_FILETYPE_PEM));
  2844. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2845. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2846. name = NULL;
  2847. ExpectNotNull(name = X509_NAME_new());
  2848. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2849. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2850. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2851. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2852. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2853. X509_NAME_free(name);
  2854. name = NULL;
  2855. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2856. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2857. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  2858. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2859. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
  2860. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2861. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2862. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2863. wolfSSL_X509_free(x509);
  2864. x509 = NULL;
  2865. /* check fail when one DNS in the list is bad */
  2866. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2867. WOLFSSL_FILETYPE_PEM));
  2868. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2869. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2870. name = NULL;
  2871. ExpectNotNull(name = X509_NAME_new());
  2872. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2873. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2874. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2875. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2876. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2877. X509_NAME_free(name);
  2878. name = NULL;
  2879. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2880. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  2881. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2882. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2883. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  2884. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2885. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2886. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2887. wolfSSL_X509_free(x509);
  2888. x509 = NULL;
  2889. /* check fail case when neither constraint is matched */
  2890. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2891. WOLFSSL_FILETYPE_PEM));
  2892. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2893. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2894. name = NULL;
  2895. ExpectNotNull(name = X509_NAME_new());
  2896. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2897. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2898. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2899. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  2900. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2901. X509_NAME_free(name);
  2902. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  2903. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2904. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2905. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2906. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2907. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2908. wolfSSL_CertManagerFree(cm);
  2909. wolfSSL_X509_free(x509);
  2910. wolfSSL_X509_free(ca);
  2911. wolfSSL_EVP_PKEY_free(priv);
  2912. #endif
  2913. return EXPECT_RESULT();
  2914. }
  2915. static int test_wolfSSL_CertManagerNameConstraint5(void)
  2916. {
  2917. EXPECT_DECLS;
  2918. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2919. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2920. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2921. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2922. !defined(NO_SHA256)
  2923. WOLFSSL_CERT_MANAGER* cm = NULL;
  2924. WOLFSSL_EVP_PKEY *priv = NULL;
  2925. WOLFSSL_X509_NAME* name = NULL;
  2926. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  2927. const char* server_cert = "./certs/test/server-goodcn.pem";
  2928. byte *der = NULL;
  2929. int derSz;
  2930. byte *pt;
  2931. WOLFSSL_X509 *x509 = NULL;
  2932. WOLFSSL_X509 *ca = NULL;
  2933. pt = (byte*)server_key_der_2048;
  2934. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2935. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2936. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2937. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2938. WOLFSSL_FILETYPE_ASN1));
  2939. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2940. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2941. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2942. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2943. /* check satisfying wolfssl.com constraint passes */
  2944. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2945. WOLFSSL_FILETYPE_PEM));
  2946. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2947. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2948. name = NULL;
  2949. ExpectNotNull(name = X509_NAME_new());
  2950. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2951. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2952. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2953. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  2954. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2955. X509_NAME_free(name);
  2956. name = NULL;
  2957. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  2958. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  2959. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2960. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2961. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2962. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2963. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2964. wolfSSL_X509_free(x509);
  2965. x509 = NULL;
  2966. /* fail with DNS check because of common name */
  2967. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2968. WOLFSSL_FILETYPE_PEM));
  2969. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2970. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2971. name = NULL;
  2972. ExpectNotNull(name = X509_NAME_new());
  2973. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2974. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2975. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2976. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2977. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2978. X509_NAME_free(name);
  2979. name = NULL;
  2980. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2981. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  2982. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2983. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  2984. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2985. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2986. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2987. wolfSSL_X509_free(x509);
  2988. x509 = NULL;
  2989. /* fail on permitted DNS name constraint */
  2990. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2991. WOLFSSL_FILETYPE_PEM));
  2992. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2993. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2994. name = NULL;
  2995. ExpectNotNull(name = X509_NAME_new());
  2996. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2997. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2998. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2999. X509_NAME_free(name);
  3000. name = NULL;
  3001. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  3002. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  3003. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  3004. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3005. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  3006. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3007. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3008. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  3009. wolfSSL_X509_free(x509);
  3010. x509 = NULL;
  3011. /* fail on permitted email name constraint */
  3012. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3013. WOLFSSL_FILETYPE_PEM));
  3014. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3015. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3016. name = NULL;
  3017. ExpectNotNull(name = X509_NAME_new());
  3018. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3019. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3020. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3021. X509_NAME_free(name);
  3022. name = NULL;
  3023. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  3024. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  3025. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  3026. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3027. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  3028. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3029. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3030. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  3031. wolfSSL_X509_free(x509);
  3032. x509 = NULL;
  3033. /* success with empty email name */
  3034. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3035. WOLFSSL_FILETYPE_PEM));
  3036. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3037. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3038. name = NULL;
  3039. ExpectNotNull(name = X509_NAME_new());
  3040. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3041. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3042. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3043. X509_NAME_free(name);
  3044. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  3045. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3046. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  3047. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3048. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3049. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3050. wolfSSL_X509_free(x509);
  3051. wolfSSL_CertManagerFree(cm);
  3052. wolfSSL_X509_free(ca);
  3053. wolfSSL_EVP_PKEY_free(priv);
  3054. #endif
  3055. return EXPECT_RESULT();
  3056. }
  3057. static int test_wolfSSL_CertManagerCRL(void)
  3058. {
  3059. EXPECT_DECLS;
  3060. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  3061. !defined(NO_RSA)
  3062. const char* ca_cert = "./certs/ca-cert.pem";
  3063. const char* crl1 = "./certs/crl/crl.pem";
  3064. const char* crl2 = "./certs/crl/crl2.pem";
  3065. #ifdef WC_RSA_PSS
  3066. const char* crl_rsapss = "./certs/crl/crl_rsapss.pem";
  3067. const char* ca_rsapss = "./certs/rsapss/ca-rsapss.pem";
  3068. #endif
  3069. const unsigned char crl_buff[] = {
  3070. 0x30, 0x82, 0x02, 0x04, 0x30, 0x81, 0xed, 0x02,
  3071. 0x01, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  3072. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  3073. 0x00, 0x30, 0x81, 0x94, 0x31, 0x0b, 0x30, 0x09,
  3074. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  3075. 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  3076. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74,
  3077. 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06,
  3078. 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  3079. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x11, 0x30,
  3080. 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x08,
  3081. 0x53, 0x61, 0x77, 0x74, 0x6f, 0x6f, 0x74, 0x68,
  3082. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04,
  3083. 0x0b, 0x0c, 0x0a, 0x43, 0x6f, 0x6e, 0x73, 0x75,
  3084. 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  3085. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  3086. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66,
  3087. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x31,
  3088. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  3089. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10,
  3090. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  3091. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  3092. 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, 0x36,
  3093. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x17,
  3094. 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, 0x32,
  3095. 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, 0x14,
  3096. 0x30, 0x12, 0x02, 0x01, 0x02, 0x17, 0x0d, 0x32,
  3097. 0x32, 0x31, 0x32, 0x31, 0x36, 0x32, 0x31, 0x31,
  3098. 0x37, 0x35, 0x30, 0x5a, 0xa0, 0x0e, 0x30, 0x0c,
  3099. 0x30, 0x0a, 0x06, 0x03, 0x55, 0x1d, 0x14, 0x04,
  3100. 0x03, 0x02, 0x01, 0x02, 0x30, 0x0d, 0x06, 0x09,
  3101. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  3102. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00,
  3103. 0x39, 0x44, 0xff, 0x39, 0xf4, 0x04, 0x45, 0x79,
  3104. 0x7e, 0x73, 0xe2, 0x42, 0x48, 0xdb, 0x85, 0x66,
  3105. 0xfd, 0x99, 0x76, 0x94, 0x7c, 0xb5, 0x79, 0x5d,
  3106. 0x15, 0x71, 0x36, 0xa9, 0x87, 0xf0, 0x73, 0x05,
  3107. 0x50, 0x08, 0x6b, 0x1c, 0x6e, 0xde, 0x96, 0x45,
  3108. 0x31, 0xc3, 0xc0, 0xba, 0xba, 0xf5, 0x08, 0x1d,
  3109. 0x05, 0x4a, 0x52, 0x39, 0xe9, 0x03, 0xef, 0x59,
  3110. 0xc8, 0x1d, 0x4a, 0xf2, 0x86, 0x05, 0x99, 0x7b,
  3111. 0x4b, 0x74, 0xf6, 0xd3, 0x75, 0x8d, 0xb2, 0x57,
  3112. 0xba, 0xac, 0xa7, 0x11, 0x14, 0xd6, 0x6c, 0x71,
  3113. 0xc4, 0x4c, 0x1c, 0x68, 0xbc, 0x49, 0x78, 0xf0,
  3114. 0xc9, 0x52, 0x8a, 0xe7, 0x8b, 0x54, 0xe6, 0x20,
  3115. 0x58, 0x20, 0x60, 0x66, 0xf5, 0x14, 0xd8, 0xcb,
  3116. 0xff, 0xe0, 0xa0, 0x45, 0xbc, 0xb4, 0x81, 0xad,
  3117. 0x1d, 0xbc, 0xcf, 0xf8, 0x8e, 0xa8, 0x87, 0x24,
  3118. 0x55, 0x99, 0xd9, 0xce, 0x47, 0xf7, 0x5b, 0x4a,
  3119. 0x33, 0x6d, 0xdb, 0xbf, 0x93, 0x64, 0x1a, 0xa6,
  3120. 0x46, 0x5f, 0x27, 0xdc, 0xd8, 0xd4, 0xf9, 0xc2,
  3121. 0x42, 0x2a, 0x7e, 0xb2, 0x7c, 0xdd, 0x98, 0x77,
  3122. 0xf5, 0x88, 0x7d, 0x15, 0x25, 0x08, 0xbc, 0xe0,
  3123. 0xd0, 0x8d, 0xf4, 0xc3, 0xc3, 0x04, 0x41, 0xa4,
  3124. 0xd1, 0xb1, 0x39, 0x4a, 0x6b, 0x2c, 0xb5, 0x2e,
  3125. 0x9a, 0x65, 0x43, 0x0d, 0x0e, 0x73, 0xf4, 0x06,
  3126. 0xe1, 0xb3, 0x49, 0x34, 0x94, 0xb0, 0xb7, 0xff,
  3127. 0xc0, 0x27, 0xc1, 0xb5, 0xea, 0x06, 0xf7, 0x71,
  3128. 0x71, 0x97, 0xbb, 0xbc, 0xc7, 0x1a, 0x9f, 0xeb,
  3129. 0xf6, 0x3d, 0xa5, 0x7b, 0x55, 0xa7, 0xbf, 0xdd,
  3130. 0xd7, 0xee, 0x97, 0xb8, 0x9d, 0xdc, 0xcd, 0xe3,
  3131. 0x06, 0xdb, 0x9a, 0x2c, 0x60, 0xbf, 0x70, 0x84,
  3132. 0xfa, 0x6b, 0x8d, 0x70, 0x7d, 0xde, 0xe8, 0xb7,
  3133. 0xab, 0xb0, 0x38, 0x68, 0x6c, 0xc0, 0xb1, 0xe1,
  3134. 0xba, 0x45, 0xe0, 0xd7, 0x12, 0x3d, 0x71, 0x5b
  3135. };
  3136. WOLFSSL_CERT_MANAGER* cm = NULL;
  3137. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3138. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(NULL, 0), BAD_FUNC_ARG);
  3139. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  3140. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECK), 1);
  3141. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm,
  3142. WOLFSSL_CRL_CHECK | WOLFSSL_CRL_CHECKALL), 1);
  3143. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 16), 1);
  3144. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  3145. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, -1), BAD_FUNC_ARG);
  3146. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, -1), BAD_FUNC_ARG);
  3147. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, -1),
  3148. BAD_FUNC_ARG);
  3149. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, 1), BAD_FUNC_ARG);
  3150. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, 1),
  3151. BAD_FUNC_ARG);
  3152. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, 1), BAD_FUNC_ARG);
  3153. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048, -1),
  3154. BAD_FUNC_ARG);
  3155. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  3156. sizeof_server_cert_der_2048), ASN_NO_SIGNER_E);
  3157. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(NULL, NULL), BAD_FUNC_ARG);
  3158. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(cm, NULL), 1);
  3159. #ifdef HAVE_CRL_IO
  3160. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(NULL, NULL), BAD_FUNC_ARG);
  3161. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  3162. #endif
  3163. #ifndef NO_FILESYSTEM
  3164. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(NULL, NULL, WOLFSSL_FILETYPE_ASN1,
  3165. 0), BAD_FUNC_ARG);
  3166. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, NULL, WOLFSSL_FILETYPE_ASN1,
  3167. 0), BAD_FUNC_ARG);
  3168. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  3169. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, "./certs/crl", -1, 0), 1);
  3170. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(NULL, NULL,
  3171. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3172. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  3173. BAD_FUNC_ARG);
  3174. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  3175. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, "./certs/crl/crl.pem", -1),
  3176. ASN_PARSE_E);
  3177. #endif
  3178. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, -1,
  3179. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3180. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, -1,
  3181. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3182. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, -1,
  3183. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3184. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, 1,
  3185. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3186. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, 1,
  3187. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3188. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, 1,
  3189. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3190. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, -1,
  3191. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3192. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(NULL), BAD_FUNC_ARG);
  3193. DoExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  3194. ExpectIntEQ(WOLFSSL_SUCCESS,
  3195. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3196. ExpectIntEQ(WOLFSSL_SUCCESS,
  3197. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  3198. ExpectIntEQ(WOLFSSL_SUCCESS,
  3199. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  3200. wolfSSL_CertManagerFreeCRL(cm);
  3201. ExpectIntEQ(WOLFSSL_SUCCESS,
  3202. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  3203. ExpectIntEQ(WOLFSSL_SUCCESS,
  3204. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3205. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  3206. sizeof_server_cert_der_2048), CRL_MISSING);
  3207. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server_cert_der_2048,
  3208. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1), CRL_MISSING);
  3209. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, sizeof(crl_buff),
  3210. WOLFSSL_FILETYPE_ASN1), 1);
  3211. #if !defined(NO_FILESYSTEM) && defined(WC_RSA_PSS)
  3212. /* loading should fail without the CA set */
  3213. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_rsapss,
  3214. WOLFSSL_FILETYPE_PEM), ASN_CRL_NO_SIGNER_E);
  3215. /* now successfully load the RSA-PSS crl once loading in it's CA */
  3216. ExpectIntEQ(WOLFSSL_SUCCESS,
  3217. wolfSSL_CertManagerLoadCA(cm, ca_rsapss, NULL));
  3218. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_rsapss,
  3219. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3220. #endif
  3221. wolfSSL_CertManagerFree(cm);
  3222. #endif
  3223. return EXPECT_RESULT();
  3224. }
  3225. static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
  3226. {
  3227. EXPECT_DECLS;
  3228. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA)
  3229. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  3230. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  3231. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  3232. defined(HAVE_LIGHTY)
  3233. WOLFSSL_CERT_MANAGER* cm = NULL;
  3234. /* Raw OCSP response bytes captured using the following setup:
  3235. * - Run responder with
  3236. * openssl ocsp -port 9999 -ndays 9999
  3237. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  3238. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  3239. * -rkey certs/ocsp/ocsp-responder-key.pem
  3240. * -CA certs/ocsp/intermediate1-ca-cert.pem
  3241. * - Run client with
  3242. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  3243. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  3244. * -cert certs/ocsp/server1-cert.pem
  3245. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  3246. * - Select the response packet in Wireshark, and export it using
  3247. * "File->Export Packet Dissection->As "C" Arrays". Select "Selected
  3248. * packets only". After importing into the editor, remove the initial
  3249. * ~148 bytes of header, ending with the Content-Length and the \r\n\r\n.
  3250. */
  3251. static const byte response[] = {
  3252. 0x30, 0x82, 0x07, 0x40, /* ....0..@ */
  3253. 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, /* ......90 */
  3254. 0x82, 0x07, 0x35, 0x06, 0x09, 0x2b, 0x06, 0x01, /* ..5..+.. */
  3255. 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, /* ...0.... */
  3256. 0x07, 0x26, 0x30, 0x82, 0x07, 0x22, 0x30, 0x82, /* .&0.."0. */
  3257. 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, /* .@...0.. */
  3258. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3259. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  3260. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  3261. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  3262. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  3263. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  3264. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  3265. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  3266. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  3267. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  3268. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  3269. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, /* ring1.0. */
  3270. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, /* ..U....w */
  3271. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, /* olfSSL O */
  3272. 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, /* CSP Resp */
  3273. 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, /* onder1.0 */
  3274. 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, /* ...*.H.. */
  3275. 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, /* ......in */
  3276. 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, /* fo@wolfs */
  3277. 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, /* sl.com.. */
  3278. 0x32, 0x30, 0x32, 0x33, 0x31, 0x31, 0x30, 0x38, /* 20231108 */
  3279. 0x30, 0x30, 0x32, 0x36, 0x33, 0x37, 0x5a, 0x30, /* 002637Z0 */
  3280. 0x64, 0x30, 0x62, 0x30, 0x3a, 0x30, 0x09, 0x06, /* d0b0:0.. */
  3281. 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, /* .+...... */
  3282. 0x04, 0x14, 0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, /* ..qM.#@Y */
  3283. 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, /* ...7C.1. */
  3284. 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04, 0x04, 0x14, /* ..C..... */
  3285. 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, /* ..:.,... */
  3286. 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, /* ..L.*.q. */
  3287. 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, /* dD...... */
  3288. 0x00, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x33, 0x31, /* ...20231 */
  3289. 0x31, 0x30, 0x38, 0x30, 0x30, 0x32, 0x36, 0x33, /* 10800263 */
  3290. 0x37, 0x5a, 0xa0, 0x11, 0x18, 0x0f, 0x32, 0x30, /* 7Z....20 */
  3291. 0x35, 0x31, 0x30, 0x33, 0x32, 0x35, 0x30, 0x30, /* 51032500 */
  3292. 0x32, 0x36, 0x33, 0x37, 0x5a, 0xa1, 0x23, 0x30, /* 2637Z.#0 */
  3293. 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, /* !0...+.. */
  3294. 0x05, 0x05, 0x07, 0x30, 0x01, 0x02, 0x04, 0x12, /* ...0.... */
  3295. 0x04, 0x10, 0xdb, 0xbc, 0x2a, 0x76, 0xa0, 0xb4, /* ....*v.. */
  3296. 0x1e, 0x5d, 0xf6, 0x2b, 0x8e, 0x38, 0x62, 0xdb, /* .].+.8b. */
  3297. 0x90, 0xed, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, /* ..0...*. */
  3298. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, /* H....... */
  3299. 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x87, 0xde, /* ........ */
  3300. 0xfb, 0xf9, 0x3a, 0x90, 0x1f, 0x90, 0xde, 0xcf, /* ..:..... */
  3301. 0xfe, 0xad, 0x64, 0x19, 0x34, 0x17, 0xf8, 0x15, /* ..d.4... */
  3302. 0x01, 0x22, 0x5f, 0x67, 0x41, 0xa4, 0x18, 0xf7, /* ."_gA... */
  3303. 0x16, 0xb7, 0xc9, 0xf3, 0xe1, 0x9f, 0xcd, 0x40, /* .......@ */
  3304. 0x56, 0x77, 0x6e, 0x6a, 0xfb, 0x92, 0x6a, 0x6f, /* Vwnj..jo */
  3305. 0x28, 0x3e, 0x22, 0x48, 0xa1, 0xc2, 0xd8, 0x1d, /* (>"H.... */
  3306. 0xc7, 0xe6, 0x78, 0x7f, 0xb6, 0x09, 0xfe, 0x2c, /* ..x...., */
  3307. 0xb5, 0xef, 0x29, 0x7c, 0xc5, 0x51, 0x16, 0x7b, /* ..)|.Q.{ */
  3308. 0x8f, 0xfb, 0x44, 0xa8, 0xcd, 0xf5, 0x5c, 0x0f, /* ..D...\. */
  3309. 0x46, 0x0e, 0xb1, 0xa4, 0xeb, 0x5b, 0xf5, 0x86, /* F....[.. */
  3310. 0x11, 0x0f, 0xcd, 0xe2, 0xe5, 0x3c, 0x91, 0x72, /* .....<.r */
  3311. 0x0d, 0x6a, 0xcb, 0x95, 0x99, 0x39, 0x91, 0x48, /* .j...9.H */
  3312. 0x65, 0x97, 0xb9, 0x78, 0xb5, 0x88, 0x7f, 0x76, /* e..x...v */
  3313. 0xa1, 0x43, 0x2f, 0xf6, 0x1f, 0x49, 0xb7, 0x08, /* .C/..I.. */
  3314. 0x36, 0xe4, 0x2e, 0x34, 0x25, 0xda, 0x16, 0x74, /* 6..4%..t */
  3315. 0x47, 0x62, 0x56, 0xff, 0x2f, 0x02, 0x03, 0x44, /* GbV./..D */
  3316. 0x89, 0x04, 0xe7, 0xb8, 0xde, 0x0a, 0x35, 0x43, /* ......5C */
  3317. 0xae, 0xd7, 0x54, 0xbe, 0xc3, 0x7c, 0x95, 0xa5, /* ..T..|.. */
  3318. 0xc8, 0xe0, 0x2e, 0x52, 0xb6, 0xea, 0x99, 0x45, /* ...R...E */
  3319. 0xfd, 0xda, 0x4b, 0xd5, 0x79, 0x07, 0x64, 0xca, /* ..K.y.d. */
  3320. 0x64, 0xba, 0x52, 0x12, 0x62, 0x8c, 0x08, 0x9a, /* d.R.b... */
  3321. 0x32, 0xeb, 0x85, 0x65, 0x05, 0x39, 0x07, 0x5d, /* 2..e.9.] */
  3322. 0x39, 0x4a, 0xcf, 0xa5, 0x30, 0xf6, 0xd1, 0xf7, /* 9J..0... */
  3323. 0x29, 0xaa, 0x23, 0x42, 0xc6, 0x85, 0x16, 0x7f, /* ).#B.... */
  3324. 0x64, 0x16, 0xb1, 0xb0, 0x5d, 0xcd, 0x88, 0x2d, /* d...]..- */
  3325. 0x06, 0xb0, 0xa9, 0xdf, 0xa3, 0x9f, 0x25, 0x41, /* ......%A */
  3326. 0x89, 0x9a, 0x19, 0xe1, 0xaa, 0xcd, 0xdf, 0x51, /* .......Q */
  3327. 0xcb, 0xa9, 0xc3, 0x7e, 0x27, 0xbc, 0x7d, 0x9b, /* ...~'.}. */
  3328. 0x6f, 0x4d, 0x79, 0x87, 0x09, 0x3f, 0xac, 0xd2, /* oMy..?.. */
  3329. 0x4a, 0x3b, 0xbe, 0xf8, 0x7a, 0xa4, 0x93, 0x45, /* J;..z..E */
  3330. 0x11, 0x64, 0x40, 0xc5, 0x03, 0xc9, 0x24, 0x5b, /* .d@...$[ */
  3331. 0xe9, 0x6d, 0xfc, 0x94, 0x08, 0xbe, 0xa0, 0x82, /* .m...... */
  3332. 0x04, 0xc6, 0x30, 0x82, 0x04, 0xc2, 0x30, 0x82, /* ..0...0. */
  3333. 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, /* ..0..... */
  3334. 0x02, 0x01, 0x02, 0x02, 0x01, 0x04, 0x30, 0x0d, /* ......0. */
  3335. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, /* ..*.H... */
  3336. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x81, 0x97, /* .....0.. */
  3337. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3338. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  3339. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  3340. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  3341. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  3342. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  3343. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  3344. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  3345. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  3346. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  3347. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  3348. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30, 0x16, /* ring1.0. */
  3349. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, /* ..U....w */
  3350. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x72, /* olfSSL r */
  3351. 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, /* oot CA1. */
  3352. 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, /* 0...*.H. */
  3353. 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, /* .......i */
  3354. 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, /* nfo@wolf */
  3355. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, /* ssl.com0 */
  3356. 0x1e, 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, /* ...22121 */
  3357. 0x36, 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, /* 6211750Z */
  3358. 0x17, 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, /* ..250911 */
  3359. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, /* 211750Z0 */
  3360. 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, /* ..1.0... */
  3361. 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, /* U....US1 */
  3362. 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, /* .0...U.. */
  3363. 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, /* ..Washin */
  3364. 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, /* gton1.0. */
  3365. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, /* ..U....S */
  3366. 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, /* eattle1. */
  3367. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, /* 0...U... */
  3368. 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  3369. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3370. 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, /* ...Engin */
  3371. 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, /* eering1. */
  3372. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, /* 0...U... */
  3373. 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  3374. 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, /* OCSP Re */
  3375. 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, /* sponder1 */
  3376. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, /* .0...*.H */
  3377. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, /* ........ */
  3378. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, /* info@wol */
  3379. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, /* fssl.com */
  3380. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, /* 0.."0... */
  3381. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  3382. 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, /* ........ */
  3383. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, /* 0....... */
  3384. 0x00, 0xb8, 0xba, 0x23, 0xb4, 0xf6, 0xc3, 0x7b, /* ...#...{ */
  3385. 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, /* .....a.. */
  3386. 0x1e, 0x63, 0xb9, 0x85, 0x23, 0x34, 0x50, 0x6d, /* .c..#4Pm */
  3387. 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, /* .|.....u */
  3388. 0x5c, 0x2d, 0xf7, 0x63, 0x88, 0xd1, 0x07, 0x7a, /* \-.c...z */
  3389. 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, /* ..E5+... */
  3390. 0x22, 0xb4, 0x94, 0x41, 0x38, 0xe2, 0x9d, 0x74, /* "..A8..t */
  3391. 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, /* ..0".Q.. */
  3392. 0xca, 0x3f, 0x46, 0x2b, 0xfe, 0xe5, 0x5a, 0x3f, /* .?F+..Z? */
  3393. 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, /* Atgu.... */
  3394. 0xc3, 0xee, 0x42, 0xf8, 0x8d, 0xeb, 0x92, 0x95, /* ..B..... */
  3395. 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, /* ..e.C... */
  3396. 0x16, 0x80, 0x90, 0xce, 0x24, 0x35, 0x21, 0xc4, /* ....$5!. */
  3397. 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, /* U.ZQ..-. */
  3398. 0x0a, 0x5a, 0x4f, 0x4a, 0x73, 0x31, 0x50, 0xee, /* .ZOJs1P. */
  3399. 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, /* J..9...H */
  3400. 0x87, 0xb1, 0x99, 0xe2, 0x10, 0xa7, 0x06, 0x72, /* .......r */
  3401. 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, /* g.\..... */
  3402. 0x76, 0xf8, 0xe0, 0x4a, 0xec, 0xbc, 0x93, 0xf4, /* v..J.... */
  3403. 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, /* fL(q..f. */
  3404. 0xb4, 0x90, 0x30, 0xbb, 0x17, 0xb0, 0xfe, 0x97, /* ..0..... */
  3405. 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, /* ....]... */
  3406. 0x19, 0x12, 0x3c, 0xab, 0x82, 0x71, 0x78, 0xff, /* ..<..qx. */
  3407. 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, /* .?2..q.. */
  3408. 0x8c, 0x27, 0xac, 0x11, 0xb8, 0xd8, 0x43, 0x49, /* .'....CI */
  3409. 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, /* ..p..... */
  3410. 0x24, 0x87, 0x17, 0x3b, 0xd8, 0x04, 0x65, 0x6c, /* $..;..el */
  3411. 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, /* .vP..... */
  3412. 0x73, 0x68, 0x26, 0x14, 0x87, 0x95, 0xc3, 0x5f, /* sh&...._ */
  3413. 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, /* na...... */
  3414. 0x0a, 0x8b, 0x98, 0xf3, 0xe3, 0xff, 0x4e, 0x44, /* ......ND */
  3415. 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, /* .et|qTe. */
  3416. 0x39, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x82, /* 9....... */
  3417. 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, /* ..0...0. */
  3418. 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, /* ..U....0 */
  3419. 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, /* .0...U.. */
  3420. 0x04, 0x16, 0x04, 0x14, 0x32, 0x67, 0xe1, 0xb1, /* ....2g.. */
  3421. 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, /* y....#.p */
  3422. 0x40, 0x50, 0xb5, 0x46, 0x56, 0xb8, 0x30, 0x36, /* @P.FV.06 */
  3423. 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, /* 0....U.# */
  3424. 0x04, 0x81, 0xbc, 0x30, 0x81, 0xb9, 0x80, 0x14, /* ...0.... */
  3425. 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, /* s.../... */
  3426. 0x47, 0xa5, 0x38, 0xd7, 0xb0, 0x04, 0x82, 0x3a, /* G.8....: */
  3427. 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, /* ~r.!.... */
  3428. 0x81, 0x9a, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, /* ..0..1.0 */
  3429. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, /* ...U.... */
  3430. 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, /* US1.0... */
  3431. 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, /* U....Was */
  3432. 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, /* hington1 */
  3433. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, /* .0...U.. */
  3434. 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, /* ..Seattl */
  3435. 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, /* e1.0...U */
  3436. 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  3437. 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, /* SSL1.0.. */
  3438. 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, /* .U....En */
  3439. 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, /* gineerin */
  3440. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, /* g1.0...U */
  3441. 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  3442. 0x53, 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, /* SSL root */
  3443. 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, /* CA1.0.. */
  3444. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, /* .*.H.... */
  3445. 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, /* ....info */
  3446. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, /* @wolfssl */
  3447. 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, /* .com..c0 */
  3448. 0x13, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x0c, /* ...U.%.. */
  3449. 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, /* 0...+... */
  3450. 0x05, 0x07, 0x03, 0x09, 0x30, 0x0d, 0x06, 0x09, /* ....0... */
  3451. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  3452. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, /* ........ */
  3453. 0x2f, 0xb7, 0x6b, 0xec, 0xb7, 0x12, 0x63, 0xb9, /* /.k...c. */
  3454. 0x57, 0xdc, 0x04, 0x4d, 0x9c, 0x67, 0x74, 0x98, /* W..M.gt. */
  3455. 0x06, 0x28, 0x68, 0x37, 0x34, 0xc2, 0x50, 0xe9, /* .(h74.P. */
  3456. 0x2a, 0xd4, 0x1a, 0xb2, 0x32, 0x1a, 0x9d, 0x2b, /* *...2..+ */
  3457. 0x4f, 0x23, 0x50, 0xea, 0xb4, 0x95, 0x86, 0xc3, /* O#P..... */
  3458. 0xb9, 0x5f, 0x34, 0x3e, 0x99, 0x91, 0xa7, 0x80, /* ._4>.... */
  3459. 0x5f, 0x6e, 0x1b, 0x6e, 0xdb, 0xe9, 0x02, 0x38, /* _n.n...8 */
  3460. 0x6f, 0xdf, 0xc5, 0x9b, 0x0d, 0xa3, 0x1c, 0xa9, /* o....... */
  3461. 0x15, 0x76, 0x16, 0x66, 0xa8, 0x4e, 0xfb, 0xd3, /* .v.f.N.. */
  3462. 0x43, 0x76, 0xf1, 0x72, 0xb7, 0xd1, 0xfa, 0xee, /* Cv.r.... */
  3463. 0x39, 0xa6, 0x96, 0xc1, 0xa2, 0x93, 0xa4, 0x9b, /* 9....... */
  3464. 0x1e, 0x9f, 0xba, 0x71, 0x8f, 0xba, 0xbd, 0x67, /* ...q...g */
  3465. 0x6a, 0xf2, 0x15, 0x5f, 0xf1, 0x64, 0xe7, 0xcf, /* j.._.d.. */
  3466. 0x26, 0xb8, 0x4c, 0xc0, 0xeb, 0x85, 0x04, 0x58, /* &.L....X */
  3467. 0xd9, 0x4a, 0x6b, 0xd9, 0x86, 0xf5, 0x80, 0x21, /* .Jk....! */
  3468. 0xbf, 0x91, 0xc8, 0x4b, 0x9f, 0x04, 0xed, 0x57, /* ...K...W */
  3469. 0x7a, 0xd2, 0x58, 0xac, 0x5b, 0x47, 0xaf, 0x4d, /* z.X.[G.M */
  3470. 0x7f, 0x5b, 0x1d, 0x6d, 0x68, 0x9b, 0x84, 0x98, /* .[.mh... */
  3471. 0x2a, 0x31, 0x02, 0x2c, 0xe9, 0x1b, 0xaf, 0x11, /* *1.,.... */
  3472. 0x0b, 0x78, 0x49, 0xbe, 0x68, 0x68, 0xcb, 0x9c, /* .xI.hh.. */
  3473. 0x41, 0x56, 0xe8, 0xb5, 0x59, 0xda, 0xff, 0xca, /* AV..Y... */
  3474. 0x59, 0x99, 0x17, 0x3e, 0x11, 0x0a, 0x8f, 0x49, /* Y..>...I */
  3475. 0x24, 0x0b, 0x81, 0x42, 0x63, 0xcd, 0x4f, 0xf6, /* $..Bc.O. */
  3476. 0x2b, 0x9d, 0xd1, 0x79, 0x75, 0xd7, 0x4a, 0xcc, /* +..yu.J. */
  3477. 0x4c, 0xb7, 0x2b, 0xd7, 0xe8, 0xe7, 0xd4, 0x48, /* L.+....H */
  3478. 0x3c, 0x14, 0x3b, 0x1c, 0x28, 0xe8, 0x46, 0x7a, /* <.;.(.Fz */
  3479. 0xdc, 0x11, 0x9d, 0x7f, 0x1c, 0xab, 0x10, 0x95, /* ........ */
  3480. 0x17, 0xb2, 0xc7, 0x7a, 0xbb, 0x17, 0x44, 0x59, /* ...z..DY */
  3481. 0x69, 0x8e, 0x16, 0x05, 0x94, 0x8c, 0x88, 0xd9, /* i....... */
  3482. 0xdc, 0x9a, 0xfd, 0xf2, 0x93, 0xbe, 0x68, 0xba, /* ......h. */
  3483. 0x3c, 0xd6, 0x2b, 0x61, 0x3a, 0x8b, 0xf7, 0x66, /* <.+a:..f */
  3484. 0xcb, 0x54, 0xe8, 0xe4, 0xdb, 0x9f, 0xcc, 0x9e /* .T...... */
  3485. };
  3486. OcspEntry entry[1];
  3487. CertStatus status[1];
  3488. OcspRequest* request = NULL;
  3489. #ifndef NO_FILESYSTEM
  3490. const char* ca_cert = "./certs/ca-cert.pem";
  3491. #endif
  3492. byte serial[] = {0x05};
  3493. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  3494. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  3495. XMEMSET(entry, 0, sizeof(OcspEntry));
  3496. XMEMSET(status, 0, sizeof(CertStatus));
  3497. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3498. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3499. DYNAMIC_TYPE_OCSP_REQUEST));
  3500. if ((request != NULL) && (request->serial != NULL)) {
  3501. request->serialSz = sizeof(serial);
  3502. XMEMCPY(request->serial, serial, sizeof(serial));
  3503. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3504. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  3505. }
  3506. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3507. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3508. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm,
  3509. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  3510. /* Response should be valid. */
  3511. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3512. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3513. /* Flip a byte in the request serial number, response should be invalid
  3514. * now. */
  3515. if ((request != NULL) && (request->serial != NULL))
  3516. request->serial[0] ^= request->serial[0];
  3517. ExpectIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3518. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3519. #ifndef NO_FILESYSTEM
  3520. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3521. sizeof(server_cert_der_2048)), ASN_NO_SIGNER_E);
  3522. ExpectIntEQ(WOLFSSL_SUCCESS,
  3523. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3524. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3525. sizeof(server_cert_der_2048)), 1);
  3526. #endif
  3527. wolfSSL_OCSP_REQUEST_free(request);
  3528. wolfSSL_CertManagerFree(cm);
  3529. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  3530. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  3531. #endif /* HAVE_OCSP */
  3532. return EXPECT_RESULT();
  3533. }
  3534. static int test_wolfSSL_CheckOCSPResponse(void)
  3535. {
  3536. EXPECT_DECLS;
  3537. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA) && \
  3538. defined(OPENSSL_ALL)
  3539. const char* responseFile = "./certs/ocsp/test-response.der";
  3540. const char* responseMultiFile = "./certs/ocsp/test-multi-response.der";
  3541. const char* responseNoInternFile =
  3542. "./certs/ocsp/test-response-nointern.der";
  3543. const char* caFile = "./certs/ocsp/root-ca-cert.pem";
  3544. OcspResponse* res = NULL;
  3545. byte data[4096];
  3546. const unsigned char* pt;
  3547. int dataSz = 0; /* initialize to mitigate spurious maybe-uninitialized from
  3548. * gcc sanitizer with --enable-heapmath.
  3549. */
  3550. XFILE f = XBADFILE;
  3551. WOLFSSL_OCSP_BASICRESP* bs = NULL;
  3552. WOLFSSL_X509_STORE* st = NULL;
  3553. WOLFSSL_X509* issuer = NULL;
  3554. ExpectTrue((f = XFOPEN(responseFile, "rb")) != XBADFILE);
  3555. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3556. if (f != XBADFILE) {
  3557. XFCLOSE(f);
  3558. f = XBADFILE;
  3559. }
  3560. pt = data;
  3561. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3562. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3563. SSL_FILETYPE_PEM));
  3564. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3565. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3566. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3567. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  3568. wolfSSL_OCSP_BASICRESP_free(bs);
  3569. bs = NULL;
  3570. wolfSSL_OCSP_RESPONSE_free(res);
  3571. res = NULL;
  3572. wolfSSL_X509_STORE_free(st);
  3573. st = NULL;
  3574. wolfSSL_X509_free(issuer);
  3575. issuer = NULL;
  3576. /* check loading a response with optional certs */
  3577. ExpectTrue((f = XFOPEN(responseNoInternFile, "rb")) != XBADFILE);
  3578. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3579. if (f != XBADFILE)
  3580. XFCLOSE(f);
  3581. f = XBADFILE;
  3582. pt = data;
  3583. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3584. wolfSSL_OCSP_RESPONSE_free(res);
  3585. res = NULL;
  3586. /* check loading a response with multiple certs */
  3587. {
  3588. WOLFSSL_CERT_MANAGER* cm = NULL;
  3589. OcspEntry *entry = NULL;
  3590. CertStatus* status = NULL;
  3591. OcspRequest* request = NULL;
  3592. byte serial1[] = {0x01};
  3593. byte serial[] = {0x02};
  3594. byte issuerHash[] = {
  3595. 0x44, 0xA8, 0xDB, 0xD1, 0xBC, 0x97, 0x0A, 0x83,
  3596. 0x3B, 0x5B, 0x31, 0x9A, 0x4C, 0xB8, 0xD2, 0x52,
  3597. 0x37, 0x15, 0x8A, 0x88
  3598. };
  3599. byte issuerKeyHash[] = {
  3600. 0x73, 0xB0, 0x1C, 0xA4, 0x2F, 0x82, 0xCB, 0xCF,
  3601. 0x47, 0xA5, 0x38, 0xD7, 0xB0, 0x04, 0x82, 0x3A,
  3602. 0x7E, 0x72, 0x15, 0x21
  3603. };
  3604. ExpectNotNull(entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  3605. DYNAMIC_TYPE_OPENSSL));
  3606. ExpectNotNull(status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  3607. DYNAMIC_TYPE_OPENSSL));
  3608. if (entry != NULL)
  3609. XMEMSET(entry, 0, sizeof(OcspEntry));
  3610. if (status != NULL)
  3611. XMEMSET(status, 0, sizeof(CertStatus));
  3612. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3613. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3614. DYNAMIC_TYPE_OCSP_REQUEST));
  3615. if (request != NULL && request->serial != NULL) {
  3616. request->serialSz = sizeof(serial);
  3617. XMEMCPY(request->serial, serial, sizeof(serial));
  3618. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3619. XMEMCPY(request->issuerKeyHash, issuerKeyHash,
  3620. sizeof(issuerKeyHash));
  3621. }
  3622. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3623. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3624. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, caFile, NULL),
  3625. WOLFSSL_SUCCESS);
  3626. ExpectTrue((f = XFOPEN(responseMultiFile, "rb")) != XBADFILE);
  3627. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3628. if (f != XBADFILE)
  3629. XFCLOSE(f);
  3630. f = XBADFILE;
  3631. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3632. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3633. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3634. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3635. ExpectNotNull(entry->status);
  3636. if (request != NULL && request->serial != NULL)
  3637. XMEMCPY(request->serial, serial1, sizeof(serial1));
  3638. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3639. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3640. /* store both status's in the entry to check that "next" is not
  3641. * overwritten */
  3642. if (EXPECT_SUCCESS() && status != NULL && entry != NULL) {
  3643. status->next = entry->status;
  3644. entry->status = status;
  3645. }
  3646. if (request != NULL && request->serial != NULL)
  3647. XMEMCPY(request->serial, serial, sizeof(serial));
  3648. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3649. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3650. ExpectNotNull(entry->status->next);
  3651. /* compare the status found */
  3652. ExpectIntEQ(status->serialSz, entry->status->serialSz);
  3653. ExpectIntEQ(XMEMCMP(status->serial, entry->status->serial,
  3654. status->serialSz), 0);
  3655. if (status != NULL && entry != NULL && entry->status != status) {
  3656. XFREE(status, NULL, DYNAMIC_TYPE_OPENSSL);
  3657. }
  3658. wolfSSL_OCSP_CERTID_free(entry);
  3659. wolfSSL_OCSP_REQUEST_free(request);
  3660. wolfSSL_CertManagerFree(cm);
  3661. }
  3662. #if defined(WC_RSA_PSS)
  3663. {
  3664. const char* responsePssFile = "./certs/ocsp/test-response-rsapss.der";
  3665. /* check loading a response with RSA-PSS signature */
  3666. ExpectTrue((f = XFOPEN(responsePssFile, "rb")) != XBADFILE);
  3667. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3668. if (f != XBADFILE)
  3669. XFCLOSE(f);
  3670. pt = data;
  3671. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3672. /* try to verify the response */
  3673. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3674. SSL_FILETYPE_PEM));
  3675. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3676. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3677. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3678. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0),
  3679. WOLFSSL_SUCCESS);
  3680. wolfSSL_OCSP_BASICRESP_free(bs);
  3681. wolfSSL_OCSP_RESPONSE_free(res);
  3682. wolfSSL_X509_STORE_free(st);
  3683. wolfSSL_X509_free(issuer);
  3684. }
  3685. #endif
  3686. #endif /* HAVE_OCSP */
  3687. return EXPECT_RESULT();
  3688. }
  3689. static int test_wolfSSL_FPKI(void)
  3690. {
  3691. EXPECT_DECLS;
  3692. #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3693. XFILE f = XBADFILE;
  3694. const char* fpkiCert = "./certs/fpki-cert.der";
  3695. DecodedCert cert;
  3696. byte buf[4096];
  3697. byte* uuid = NULL;
  3698. byte* fascn = NULL;
  3699. word32 fascnSz;
  3700. word32 uuidSz;
  3701. int bytes = 0;
  3702. ExpectTrue((f = XFOPEN(fpkiCert, "rb")) != XBADFILE);
  3703. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3704. if (f != XBADFILE)
  3705. XFCLOSE(f);
  3706. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3707. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3708. ExpectIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
  3709. ExpectNotNull(fascn = (byte*)XMALLOC(fascnSz, NULL,
  3710. DYNAMIC_TYPE_TMP_BUFFER));
  3711. ExpectIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
  3712. XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3713. ExpectIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
  3714. ExpectNotNull(uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  3715. ExpectIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
  3716. XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3717. wc_FreeDecodedCert(&cert);
  3718. #endif
  3719. return EXPECT_RESULT();
  3720. }
  3721. /* use RID in confuncture with other names to test parsing of unknown other
  3722. * names */
  3723. static int test_wolfSSL_OtherName(void)
  3724. {
  3725. EXPECT_DECLS;
  3726. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3727. XFILE f = XBADFILE;
  3728. const char* ridCert = "./certs/rid-cert.der";
  3729. DecodedCert cert;
  3730. byte buf[4096];
  3731. int bytes = 0;
  3732. ExpectTrue((f = XFOPEN(ridCert, "rb")) != XBADFILE);
  3733. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3734. if (f != XBADFILE)
  3735. XFCLOSE(f);
  3736. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3737. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3738. wc_FreeDecodedCert(&cert);
  3739. #endif
  3740. return EXPECT_RESULT();
  3741. }
  3742. static int test_wolfSSL_CertRsaPss(void)
  3743. {
  3744. EXPECT_DECLS;
  3745. /* FIPS v2 and below don't support long salts. */
  3746. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
  3747. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  3748. (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
  3749. (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
  3750. XFILE f = XBADFILE;
  3751. const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
  3752. const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
  3753. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3754. RSA_MAX_SIZE >= 3072
  3755. const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
  3756. #endif
  3757. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3758. const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
  3759. #endif
  3760. DecodedCert cert;
  3761. byte buf[4096];
  3762. int bytes = 0;
  3763. WOLFSSL_CERT_MANAGER* cm = NULL;
  3764. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3765. ExpectIntEQ(WOLFSSL_SUCCESS,
  3766. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
  3767. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3768. ExpectIntEQ(WOLFSSL_SUCCESS,
  3769. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
  3770. #endif
  3771. ExpectTrue((f = XFOPEN(rsaPssSha256Cert, "rb")) != XBADFILE);
  3772. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3773. if (f != XBADFILE) {
  3774. XFCLOSE(f);
  3775. f = XBADFILE;
  3776. }
  3777. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3778. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3779. wc_FreeDecodedCert(&cert);
  3780. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3781. RSA_MAX_SIZE >= 3072
  3782. ExpectTrue((f = XFOPEN(rsaPssSha384Cert, "rb")) != XBADFILE);
  3783. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3784. if (f != XBADFILE)
  3785. XFCLOSE(f);
  3786. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3787. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3788. wc_FreeDecodedCert(&cert);
  3789. #endif
  3790. wolfSSL_CertManagerFree(cm);
  3791. #endif
  3792. return EXPECT_RESULT();
  3793. }
  3794. static int test_wolfSSL_CTX_load_verify_locations_ex(void)
  3795. {
  3796. EXPECT_DECLS;
  3797. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3798. !defined(NO_WOLFSSL_CLIENT)
  3799. WOLFSSL_CTX* ctx = NULL;
  3800. const char* ca_cert = "./certs/ca-cert.pem";
  3801. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  3802. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3803. /* test good CA */
  3804. ExpectTrue(WOLFSSL_SUCCESS ==
  3805. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  3806. WOLFSSL_LOAD_FLAG_NONE));
  3807. /* test expired CA */
  3808. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3809. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3810. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3811. #else
  3812. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3813. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3814. #endif
  3815. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3816. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3817. wolfSSL_CTX_free(ctx);
  3818. #endif
  3819. return EXPECT_RESULT();
  3820. }
  3821. static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
  3822. {
  3823. EXPECT_DECLS;
  3824. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3825. defined(USE_CERT_BUFFERS_2048)
  3826. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  3827. WOLFSSL_CTX* ctx;
  3828. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  3829. byte ca_expired_cert[TWOK_BUF];
  3830. word32 sizeof_ca_expired_cert = 0;
  3831. XFILE fp = XBADFILE;
  3832. #ifndef NO_WOLFSSL_CLIENT
  3833. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3834. #else
  3835. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3836. #endif
  3837. ExpectNotNull(ctx);
  3838. /* test good CA */
  3839. ExpectTrue(WOLFSSL_SUCCESS ==
  3840. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  3841. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  3842. WOLFSSL_LOAD_FLAG_NONE));
  3843. /* load expired CA */
  3844. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  3845. ExpectTrue((fp = XFOPEN(ca_expired_cert_file, "rb")) != XBADFILE);
  3846. ExpectIntGT(sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  3847. sizeof(ca_expired_cert), fp), 0);
  3848. if (fp != XBADFILE)
  3849. XFCLOSE(fp);
  3850. /* test expired CA failure */
  3851. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3852. ExpectIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3853. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3854. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3855. #else
  3856. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3857. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3858. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3859. #endif
  3860. /* test expired CA success */
  3861. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3862. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3863. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3864. wolfSSL_CTX_free(ctx);
  3865. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  3866. #endif
  3867. return EXPECT_RESULT();
  3868. }
  3869. static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  3870. {
  3871. EXPECT_DECLS;
  3872. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  3873. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048) && \
  3874. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3875. WOLFSSL_CTX* ctx = NULL;
  3876. #ifndef NO_WOLFSSL_CLIENT
  3877. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3878. #else
  3879. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3880. #endif
  3881. ExpectTrue(WOLFSSL_SUCCESS == wolfSSL_CTX_load_verify_chain_buffer_format(
  3882. ctx, ca_cert_chain_der, sizeof_ca_cert_chain_der,
  3883. WOLFSSL_FILETYPE_ASN1));
  3884. wolfSSL_CTX_free(ctx);
  3885. #endif
  3886. return EXPECT_RESULT();
  3887. }
  3888. static int test_wolfSSL_CTX_add1_chain_cert(void)
  3889. {
  3890. EXPECT_DECLS;
  3891. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  3892. defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  3893. WOLFSSL_CTX* ctx;
  3894. WOLFSSL* ssl = NULL;
  3895. const char *certChain[] = {
  3896. "./certs/intermediate/client-int-cert.pem",
  3897. "./certs/intermediate/ca-int2-cert.pem",
  3898. "./certs/intermediate/ca-int-cert.pem",
  3899. "./certs/ca-cert.pem",
  3900. NULL
  3901. };
  3902. const char** cert;
  3903. WOLFSSL_X509* x509 = NULL;
  3904. WOLF_STACK_OF(X509)* chain = NULL;
  3905. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3906. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3907. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3908. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3909. WOLFSSL_FILETYPE_PEM));
  3910. ExpectIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  3911. X509_free(x509);
  3912. x509 = NULL;
  3913. }
  3914. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3915. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3916. WOLFSSL_FILETYPE_PEM));
  3917. ExpectIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  3918. X509_free(x509);
  3919. x509 = NULL;
  3920. }
  3921. ExpectIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  3922. ExpectIntEQ(sk_X509_num(chain), 3);
  3923. ExpectIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  3924. ExpectIntEQ(sk_X509_num(chain), 3);
  3925. SSL_free(ssl);
  3926. SSL_CTX_free(ctx);
  3927. #endif
  3928. return EXPECT_RESULT();
  3929. }
  3930. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  3931. {
  3932. EXPECT_DECLS;
  3933. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3934. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3935. const char* server_chain_der = "./certs/server-cert-chain.der";
  3936. const char* client_single_pem = "./certs/client-cert.pem";
  3937. WOLFSSL_CTX* ctx;
  3938. (void)server_chain_der;
  3939. (void)client_single_pem;
  3940. (void)ctx;
  3941. #ifndef NO_WOLFSSL_CLIENT
  3942. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3943. #else
  3944. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3945. #endif
  3946. ExpectNotNull(ctx);
  3947. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3948. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3949. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3950. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3951. wolfSSL_CTX_free(ctx);
  3952. #endif
  3953. return EXPECT_RESULT();
  3954. }
  3955. static int test_wolfSSL_CTX_SetTmpDH_file(void)
  3956. {
  3957. EXPECT_DECLS;
  3958. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3959. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3960. WOLFSSL_CTX *ctx = NULL;
  3961. (void)ctx;
  3962. #ifndef NO_WOLFSSL_CLIENT
  3963. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3964. #else
  3965. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3966. #endif
  3967. /* invalid context */
  3968. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  3969. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3970. /* invalid dhParamFile file */
  3971. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3972. NULL, WOLFSSL_FILETYPE_PEM));
  3973. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3974. bogusFile, WOLFSSL_FILETYPE_PEM));
  3975. /* success */
  3976. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  3977. WOLFSSL_FILETYPE_PEM));
  3978. wolfSSL_CTX_free(ctx);
  3979. #endif
  3980. return EXPECT_RESULT();
  3981. }
  3982. static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
  3983. {
  3984. EXPECT_DECLS;
  3985. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  3986. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3987. WOLFSSL_CTX *ctx = NULL;
  3988. #ifndef NO_WOLFSSL_CLIENT
  3989. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3990. #else
  3991. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3992. #endif
  3993. /* invalid context */
  3994. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL,
  3995. dh_key_der_2048, sizeof_dh_key_der_2048,
  3996. WOLFSSL_FILETYPE_ASN1));
  3997. /* invalid dhParamFile file */
  3998. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  3999. 0, WOLFSSL_FILETYPE_ASN1));
  4000. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4001. dsa_key_der_2048, sizeof_dsa_key_der_2048,
  4002. WOLFSSL_FILETYPE_ASN1));
  4003. /* success */
  4004. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4005. dh_key_der_2048, sizeof_dh_key_der_2048,
  4006. WOLFSSL_FILETYPE_ASN1));
  4007. wolfSSL_CTX_free(ctx);
  4008. #endif
  4009. return EXPECT_RESULT();
  4010. }
  4011. static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  4012. {
  4013. EXPECT_DECLS;
  4014. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  4015. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  4016. WOLFSSL_CTX *ctx;
  4017. (void)ctx;
  4018. #ifndef NO_WOLFSSL_CLIENT
  4019. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  4020. #else
  4021. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  4022. #endif
  4023. ExpectNotNull(ctx);
  4024. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  4025. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  4026. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4027. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  4028. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4029. dh_key_der_2048, sizeof_dh_key_der_2048,
  4030. WOLFSSL_FILETYPE_ASN1));
  4031. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  4032. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4033. dh_key_der_2048, sizeof_dh_key_der_2048,
  4034. WOLFSSL_FILETYPE_ASN1));
  4035. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  4036. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4037. dh_key_der_2048, sizeof_dh_key_der_2048,
  4038. WOLFSSL_FILETYPE_ASN1));
  4039. wolfSSL_CTX_free(ctx);
  4040. #endif
  4041. return EXPECT_RESULT();
  4042. }
  4043. static int test_wolfSSL_CTX_der_load_verify_locations(void)
  4044. {
  4045. EXPECT_DECLS;
  4046. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_DER_LOAD) && \
  4047. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  4048. WOLFSSL_CTX* ctx = NULL;
  4049. const char* derCert = "./certs/server-cert.der";
  4050. const char* nullPath = NULL;
  4051. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  4052. const char* emptyPath = "";
  4053. /* der load Case 1 ctx NULL */
  4054. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  4055. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4056. #ifndef NO_WOLFSSL_CLIENT
  4057. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4058. #else
  4059. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4060. #endif
  4061. /* Case 2 filePath NULL */
  4062. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  4063. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4064. /* Case 3 invalid format */
  4065. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  4066. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  4067. /* Case 4 filePath not valid */
  4068. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  4069. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4070. /* Case 5 filePath empty */
  4071. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  4072. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4073. #ifndef NO_RSA
  4074. /* Case 6 success case */
  4075. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  4076. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4077. #endif
  4078. wolfSSL_CTX_free(ctx);
  4079. #endif
  4080. return EXPECT_RESULT();
  4081. }
  4082. static int test_wolfSSL_CTX_enable_disable(void)
  4083. {
  4084. EXPECT_DECLS;
  4085. #ifndef NO_CERTS
  4086. WOLFSSL_CTX* ctx = NULL;
  4087. #ifdef HAVE_CRL
  4088. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  4089. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  4090. #endif
  4091. #ifdef HAVE_OCSP
  4092. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  4093. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  4094. #endif
  4095. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  4096. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  4097. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  4098. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  4099. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  4100. ExpectIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  4101. #endif
  4102. #ifndef NO_WOLFSSL_CLIENT
  4103. #ifdef HAVE_EXTENDED_MASTER
  4104. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  4105. #endif
  4106. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4107. #ifdef HAVE_EXTENDED_MASTER
  4108. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  4109. #endif
  4110. #elif !defined(NO_WOLFSSL_SERVER)
  4111. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4112. #endif
  4113. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  4114. #ifdef HAVE_CRL
  4115. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  4116. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  4117. #endif
  4118. #ifdef HAVE_OCSP
  4119. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  4120. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  4121. WOLFSSL_SUCCESS);
  4122. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  4123. WOLFSSL_SUCCESS);
  4124. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  4125. WOLFSSL_SUCCESS);
  4126. #endif
  4127. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  4128. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  4129. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  4130. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  4131. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  4132. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  4133. #endif
  4134. wolfSSL_CTX_free(ctx);
  4135. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  4136. #endif /* NO_CERTS */
  4137. return EXPECT_RESULT();
  4138. }
  4139. static int test_wolfSSL_CTX_ticket_API(void)
  4140. {
  4141. EXPECT_DECLS;
  4142. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  4143. WOLFSSL_CTX* ctx = NULL;
  4144. void *userCtx = (void*)"this is my ctx";
  4145. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4146. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  4147. ExpectTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  4148. wolfSSL_CTX_free(ctx);
  4149. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  4150. ExpectNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  4151. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  4152. return EXPECT_RESULT();
  4153. }
  4154. static int test_wolfSSL_set_minmax_proto_version(void)
  4155. {
  4156. EXPECT_DECLS;
  4157. #ifdef OPENSSL_EXTRA
  4158. WOLFSSL_CTX *ctx = NULL;
  4159. WOLFSSL *ssl = NULL;
  4160. (void)ssl;
  4161. #ifndef NO_WOLFSSL_CLIENT
  4162. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4163. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4164. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4165. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4166. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  4167. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  4168. ExpectIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4169. ExpectIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  4170. ExpectIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4171. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  4172. wolfSSL_free(ssl);
  4173. wolfSSL_CTX_free(ctx);
  4174. ctx = NULL;
  4175. #endif
  4176. #ifndef NO_WOLFSSL_SERVER
  4177. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4178. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4179. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4180. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  4181. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  4182. wolfSSL_CTX_free(ctx);
  4183. #endif
  4184. #endif
  4185. return EXPECT_RESULT();
  4186. }
  4187. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
  4188. defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  4189. static int test_wolfSSL_CTX_set_max_proto_version_on_result(WOLFSSL* ssl)
  4190. {
  4191. EXPECT_DECLS;
  4192. ExpectStrEQ(wolfSSL_get_version(ssl), "TLSv1.2");
  4193. return EXPECT_RESULT();
  4194. }
  4195. static int test_wolfSSL_CTX_set_max_proto_version_ctx_ready(WOLFSSL_CTX* ctx)
  4196. {
  4197. EXPECT_DECLS;
  4198. /* Set TLS 1.2 */
  4199. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  4200. WOLFSSL_SUCCESS);
  4201. return EXPECT_RESULT();
  4202. }
  4203. /* Test using wolfSSL_CTX_set_max_proto_version to limit the version below
  4204. * what was set at ctx creation. */
  4205. static int test_wolfSSL_CTX_set_max_proto_version(void)
  4206. {
  4207. EXPECT_DECLS;
  4208. test_ssl_cbf client_cbs;
  4209. test_ssl_cbf server_cbs;
  4210. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  4211. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  4212. client_cbs.method = wolfTLS_client_method;
  4213. server_cbs.method = wolfTLS_server_method;
  4214. server_cbs.ctx_ready = test_wolfSSL_CTX_set_max_proto_version_ctx_ready;
  4215. client_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  4216. server_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  4217. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  4218. &server_cbs, NULL), TEST_SUCCESS);
  4219. return EXPECT_RESULT();
  4220. }
  4221. #else
  4222. static int test_wolfSSL_CTX_set_max_proto_version(void)
  4223. {
  4224. return TEST_SKIPPED;
  4225. }
  4226. #endif
  4227. /*----------------------------------------------------------------------------*
  4228. | SSL
  4229. *----------------------------------------------------------------------------*/
  4230. static int test_server_wolfSSL_new(void)
  4231. {
  4232. EXPECT_DECLS;
  4233. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4234. !defined(NO_WOLFSSL_SERVER)
  4235. WOLFSSL_CTX *ctx = NULL;
  4236. WOLFSSL_CTX *ctx_nocert = NULL;
  4237. WOLFSSL *ssl = NULL;
  4238. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4239. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4240. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4241. WOLFSSL_FILETYPE_PEM));
  4242. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4243. WOLFSSL_FILETYPE_PEM));
  4244. /* invalid context */
  4245. ExpectNull(ssl = wolfSSL_new(NULL));
  4246. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && \
  4247. !defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_INIT_CTX_KEY)
  4248. ExpectNull(ssl = wolfSSL_new(ctx_nocert));
  4249. #endif
  4250. /* success */
  4251. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4252. wolfSSL_free(ssl);
  4253. wolfSSL_CTX_free(ctx);
  4254. wolfSSL_CTX_free(ctx_nocert);
  4255. #endif
  4256. return EXPECT_RESULT();
  4257. }
  4258. static int test_client_wolfSSL_new(void)
  4259. {
  4260. EXPECT_DECLS;
  4261. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4262. !defined(NO_WOLFSSL_CLIENT)
  4263. WOLFSSL_CTX *ctx = NULL;
  4264. WOLFSSL_CTX *ctx_nocert = NULL;
  4265. WOLFSSL *ssl = NULL;
  4266. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4267. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4268. ExpectTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  4269. /* invalid context */
  4270. ExpectNull(ssl = wolfSSL_new(NULL));
  4271. /* success */
  4272. ExpectNotNull(ssl = wolfSSL_new(ctx_nocert));
  4273. wolfSSL_free(ssl);
  4274. ssl = NULL;
  4275. /* success */
  4276. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4277. wolfSSL_free(ssl);
  4278. wolfSSL_CTX_free(ctx);
  4279. wolfSSL_CTX_free(ctx_nocert);
  4280. #endif
  4281. return EXPECT_RESULT();
  4282. }
  4283. static int test_wolfSSL_SetTmpDH_file(void)
  4284. {
  4285. EXPECT_DECLS;
  4286. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  4287. !defined(NO_WOLFSSL_SERVER)
  4288. WOLFSSL_CTX *ctx = NULL;
  4289. WOLFSSL *ssl = NULL;
  4290. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4291. #ifndef NO_RSA
  4292. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4293. WOLFSSL_FILETYPE_PEM));
  4294. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4295. WOLFSSL_FILETYPE_PEM));
  4296. #elif defined(HAVE_ECC)
  4297. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  4298. WOLFSSL_FILETYPE_PEM));
  4299. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  4300. WOLFSSL_FILETYPE_PEM));
  4301. #elif defined(HAVE_ED25519)
  4302. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  4303. WOLFSSL_FILETYPE_PEM));
  4304. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  4305. WOLFSSL_FILETYPE_PEM));
  4306. #elif defined(HAVE_ED448)
  4307. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  4308. WOLFSSL_FILETYPE_PEM));
  4309. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  4310. WOLFSSL_FILETYPE_PEM));
  4311. #endif
  4312. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4313. /* invalid ssl */
  4314. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  4315. dhParamFile, WOLFSSL_FILETYPE_PEM));
  4316. /* invalid dhParamFile file */
  4317. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  4318. NULL, WOLFSSL_FILETYPE_PEM));
  4319. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  4320. bogusFile, WOLFSSL_FILETYPE_PEM));
  4321. /* success */
  4322. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  4323. WOLFSSL_FILETYPE_PEM));
  4324. wolfSSL_free(ssl);
  4325. wolfSSL_CTX_free(ctx);
  4326. #endif
  4327. return EXPECT_RESULT();
  4328. }
  4329. static int test_wolfSSL_SetTmpDH_buffer(void)
  4330. {
  4331. EXPECT_DECLS;
  4332. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  4333. WOLFSSL_CTX *ctx = NULL;
  4334. WOLFSSL *ssl = NULL;
  4335. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4336. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  4337. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4338. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  4339. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4340. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4341. /* invalid ssl */
  4342. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  4343. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4344. /* invalid dhParamFile file */
  4345. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  4346. 0, WOLFSSL_FILETYPE_ASN1));
  4347. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  4348. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4349. /* success */
  4350. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4351. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4352. wolfSSL_free(ssl);
  4353. wolfSSL_CTX_free(ctx);
  4354. #endif
  4355. return EXPECT_RESULT();
  4356. }
  4357. static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
  4358. {
  4359. EXPECT_DECLS;
  4360. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  4361. WOLFSSL_CTX *ctx = NULL;
  4362. WOLFSSL_CTX *ctx2 = NULL;
  4363. WOLFSSL *ssl = NULL;
  4364. WOLFSSL *ssl2 = NULL;
  4365. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4366. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  4367. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4368. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  4369. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4370. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  4371. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4372. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4373. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  4374. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4375. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  4376. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4377. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  4378. ExpectNotNull(ssl2 = wolfSSL_new(ctx2));
  4379. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4380. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4381. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  4382. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4383. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4384. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  4385. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4386. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4387. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  4388. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4389. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  4390. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  4391. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4392. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  4393. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4394. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4395. wolfSSL_free(ssl2);
  4396. wolfSSL_CTX_free(ctx2);
  4397. wolfSSL_free(ssl);
  4398. wolfSSL_CTX_free(ctx);
  4399. #endif
  4400. return EXPECT_RESULT();
  4401. }
  4402. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  4403. * allowed.
  4404. * POST: return 1 on success.
  4405. */
  4406. static int test_wolfSSL_SetMinVersion(void)
  4407. {
  4408. int res = TEST_SKIPPED;
  4409. #ifndef NO_WOLFSSL_CLIENT
  4410. int failFlag = WOLFSSL_SUCCESS;
  4411. WOLFSSL_CTX* ctx = NULL;
  4412. WOLFSSL* ssl = NULL;
  4413. int itr;
  4414. #ifndef NO_OLD_TLS
  4415. const int versions[] = {
  4416. #ifdef WOLFSSL_ALLOW_TLSV10
  4417. WOLFSSL_TLSV1,
  4418. #endif
  4419. WOLFSSL_TLSV1_1,
  4420. WOLFSSL_TLSV1_2};
  4421. #elif !defined(WOLFSSL_NO_TLS12)
  4422. const int versions[] = { WOLFSSL_TLSV1_2 };
  4423. #else
  4424. const int versions[] = { WOLFSSL_TLSV1_3 };
  4425. #endif
  4426. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  4427. ssl = wolfSSL_new(ctx);
  4428. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  4429. if (wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS) {
  4430. failFlag = WOLFSSL_FAILURE;
  4431. }
  4432. }
  4433. wolfSSL_free(ssl);
  4434. wolfSSL_CTX_free(ctx);
  4435. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  4436. #endif
  4437. return res;
  4438. } /* END test_wolfSSL_SetMinVersion */
  4439. #ifdef OPENSSL_EXTRA
  4440. static int test_EC25519(void)
  4441. {
  4442. EXPECT_DECLS;
  4443. #if defined(HAVE_CURVE25519) && defined(WOLFSSL_KEY_GEN)
  4444. byte priv[CURVE25519_KEYSIZE];
  4445. unsigned int privSz = CURVE25519_KEYSIZE;
  4446. byte pub[CURVE25519_KEYSIZE];
  4447. unsigned int pubSz = CURVE25519_KEYSIZE;
  4448. byte priv2[CURVE25519_KEYSIZE];
  4449. unsigned int priv2Sz = CURVE25519_KEYSIZE;
  4450. byte pub2[CURVE25519_KEYSIZE];
  4451. unsigned int pub2Sz = CURVE25519_KEYSIZE;
  4452. byte shared[CURVE25519_KEYSIZE];
  4453. unsigned int sharedSz = CURVE25519_KEYSIZE;
  4454. byte shared2[CURVE25519_KEYSIZE];
  4455. unsigned int shared2Sz = CURVE25519_KEYSIZE;
  4456. /* Bad parameter testing of key generation. */
  4457. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, NULL, NULL, NULL), 0);
  4458. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, &privSz, NULL, &pubSz), 0);
  4459. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4460. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, NULL, pub, &pubSz), 0);
  4461. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4462. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, NULL), 0);
  4463. /* Bad length */
  4464. privSz = 1;
  4465. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4466. privSz = CURVE25519_KEYSIZE;
  4467. pubSz = 1;
  4468. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4469. pubSz = CURVE25519_KEYSIZE;
  4470. /* Good case of generating key. */
  4471. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 1);
  4472. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv2, &priv2Sz, pub2, &pub2Sz),
  4473. 1);
  4474. ExpectIntEQ(privSz, CURVE25519_KEYSIZE);
  4475. ExpectIntEQ(pubSz, CURVE25519_KEYSIZE);
  4476. /* Bad parameter testing of shared key. */
  4477. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, NULL, NULL, privSz,
  4478. NULL, pubSz), 0);
  4479. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, NULL, privSz,
  4480. NULL, pubSz), 0);
  4481. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, priv, privSz,
  4482. pub, pubSz), 0);
  4483. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, NULL, privSz,
  4484. pub, pubSz), 0);
  4485. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4486. NULL, pubSz), 0);
  4487. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, priv, privSz,
  4488. pub, pubSz), 0);
  4489. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, NULL, priv, privSz,
  4490. pub, pubSz), 0);
  4491. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, NULL, privSz,
  4492. pub, pubSz), 0);
  4493. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4494. NULL, pubSz), 0);
  4495. /* Bad length. */
  4496. sharedSz = 1;
  4497. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4498. pub, pubSz), 0);
  4499. sharedSz = CURVE25519_KEYSIZE;
  4500. privSz = 1;
  4501. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4502. pub, pubSz), 0);
  4503. privSz = CURVE25519_KEYSIZE;
  4504. pubSz = 1;
  4505. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4506. pub, pubSz), 0);
  4507. pubSz = CURVE25519_KEYSIZE;
  4508. /* Good case of shared key. */
  4509. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4510. pub2, pub2Sz), 1);
  4511. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared2, &shared2Sz, priv2, priv2Sz,
  4512. pub, pubSz), 1);
  4513. ExpectIntEQ(sharedSz, CURVE25519_KEYSIZE);
  4514. ExpectIntEQ(shared2Sz, CURVE25519_KEYSIZE);
  4515. ExpectIntEQ(XMEMCMP(shared, shared2, sharedSz), 0);
  4516. #endif /* HAVE_CURVE25519 && WOLFSSL_KEY_GEN */
  4517. return EXPECT_RESULT();
  4518. }
  4519. static int test_ED25519(void)
  4520. {
  4521. EXPECT_DECLS;
  4522. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  4523. defined(WOLFSSL_KEY_GEN)
  4524. byte priv[ED25519_PRV_KEY_SIZE];
  4525. unsigned int privSz = (unsigned int)sizeof(priv);
  4526. byte pub[ED25519_PUB_KEY_SIZE];
  4527. unsigned int pubSz = (unsigned int)sizeof(pub);
  4528. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4529. const char* msg = TEST_STRING;
  4530. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4531. byte sig[ED25519_SIG_SIZE];
  4532. unsigned int sigSz = (unsigned int)sizeof(sig);
  4533. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4534. /* Bad parameter testing of key generation. */
  4535. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, NULL, NULL), 0);
  4536. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, NULL, NULL, NULL), 0);
  4537. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, &privSz, NULL, NULL), 0);
  4538. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, pub, NULL), 0);
  4539. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, NULL, &pubSz), 0);
  4540. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4541. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, NULL, pub, &pubSz), 0);
  4542. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4543. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, NULL), 0);
  4544. /* Bad length. */
  4545. privSz = 1;
  4546. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4547. privSz = ED25519_PRV_KEY_SIZE;
  4548. pubSz = 1;
  4549. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4550. pubSz = ED25519_PUB_KEY_SIZE;
  4551. /* Good case of generating key. */
  4552. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  4553. 1);
  4554. ExpectIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  4555. ExpectIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  4556. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4557. /* Bad parameter testing of signing. */
  4558. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, NULL,
  4559. NULL), 0);
  4560. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, NULL, privSz, NULL,
  4561. NULL), 0);
  4562. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, priv, privSz, NULL,
  4563. NULL), 0);
  4564. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, sig,
  4565. NULL), 0);
  4566. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, NULL,
  4567. &sigSz), 0);
  4568. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, priv, privSz, sig,
  4569. &sigSz), 0);
  4570. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, NULL, privSz, sig,
  4571. &sigSz), 0);
  4572. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, NULL,
  4573. &sigSz), 0);
  4574. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4575. NULL), 0);
  4576. /* Bad length. */
  4577. privSz = 1;
  4578. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4579. &sigSz), 0);
  4580. privSz = ED25519_PRV_KEY_SIZE;
  4581. sigSz = 1;
  4582. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4583. &sigSz), 0);
  4584. sigSz = ED25519_SIG_SIZE;
  4585. /* Good case of signing. */
  4586. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4587. &sigSz), 1);
  4588. ExpectIntEQ(sigSz, ED25519_SIG_SIZE);
  4589. #ifdef HAVE_ED25519_VERIFY
  4590. /* Bad parameter testing of verification. */
  4591. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, NULL, pubSz, NULL,
  4592. sigSz), 0);
  4593. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, NULL, pubSz, NULL,
  4594. sigSz), 0);
  4595. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, pub, pubSz, NULL,
  4596. sigSz), 0);
  4597. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, NULL, pubSz, sig,
  4598. sigSz), 0);
  4599. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, pub, pubSz, sig,
  4600. sigSz), 0);
  4601. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, NULL, pubSz, sig,
  4602. sigSz), 0);
  4603. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, NULL,
  4604. sigSz), 0);
  4605. /* Bad length. */
  4606. pubSz = 1;
  4607. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4608. sigSz), 0);
  4609. pubSz = ED25519_PUB_KEY_SIZE;
  4610. sigSz = 1;
  4611. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4612. sigSz), 0);
  4613. sigSz = ED25519_SIG_SIZE;
  4614. /* Good case of verification. */
  4615. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4616. sigSz), 1);
  4617. /* Bad signature. */
  4618. if (EXPECT_SUCCESS()) {
  4619. sig[1] ^= 0x80;
  4620. }
  4621. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4622. sigSz), 0);
  4623. #endif /* HAVE_ED25519_VERIFY */
  4624. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4625. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4626. return EXPECT_RESULT();
  4627. }
  4628. static int test_EC448(void)
  4629. {
  4630. EXPECT_DECLS;
  4631. #if defined(HAVE_CURVE448) && defined(WOLFSSL_KEY_GEN)
  4632. byte priv[CURVE448_KEY_SIZE];
  4633. unsigned int privSz = CURVE448_KEY_SIZE;
  4634. byte pub[CURVE448_KEY_SIZE];
  4635. unsigned int pubSz = CURVE448_KEY_SIZE;
  4636. byte priv2[CURVE448_KEY_SIZE];
  4637. unsigned int priv2Sz = CURVE448_KEY_SIZE;
  4638. byte pub2[CURVE448_KEY_SIZE];
  4639. unsigned int pub2Sz = CURVE448_KEY_SIZE;
  4640. byte shared[CURVE448_KEY_SIZE];
  4641. unsigned int sharedSz = CURVE448_KEY_SIZE;
  4642. byte shared2[CURVE448_KEY_SIZE];
  4643. unsigned int shared2Sz = CURVE448_KEY_SIZE;
  4644. /* Bad parameter testing of key generation. */
  4645. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, NULL, NULL, NULL), 0);
  4646. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, &privSz, NULL, &pubSz), 0);
  4647. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4648. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, NULL, pub, &pubSz), 0);
  4649. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4650. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, NULL), 0);
  4651. /* Bad length. */
  4652. privSz = 1;
  4653. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4654. privSz = CURVE448_KEY_SIZE;
  4655. pubSz = 1;
  4656. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4657. pubSz = CURVE448_KEY_SIZE;
  4658. /* Good case of generating key. */
  4659. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 1);
  4660. ExpectIntEQ(wolfSSL_EC448_generate_key(priv2, &priv2Sz, pub2, &pub2Sz), 1);
  4661. ExpectIntEQ(privSz, CURVE448_KEY_SIZE);
  4662. ExpectIntEQ(pubSz, CURVE448_KEY_SIZE);
  4663. /* Bad parameter testing of shared key. */
  4664. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, NULL, NULL, privSz,
  4665. NULL, pubSz), 0);
  4666. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, NULL, privSz,
  4667. NULL, pubSz), 0);
  4668. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, priv, privSz,
  4669. pub, pubSz), 0);
  4670. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, NULL, privSz,
  4671. pub, pubSz), 0);
  4672. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4673. NULL, pubSz), 0);
  4674. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, priv, privSz,
  4675. pub, pubSz), 0);
  4676. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, NULL, priv, privSz,
  4677. pub, pubSz), 0);
  4678. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, NULL, privSz,
  4679. pub, pubSz), 0);
  4680. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4681. NULL, pubSz), 0);
  4682. /* Bad length. */
  4683. sharedSz = 1;
  4684. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4685. pub, pubSz), 0);
  4686. sharedSz = CURVE448_KEY_SIZE;
  4687. privSz = 1;
  4688. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4689. pub, pubSz), 0);
  4690. privSz = CURVE448_KEY_SIZE;
  4691. pubSz = 1;
  4692. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4693. pub, pubSz), 0);
  4694. pubSz = CURVE448_KEY_SIZE;
  4695. /* Good case of shared key. */
  4696. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4697. pub2, pub2Sz), 1);
  4698. ExpectIntEQ(wolfSSL_EC448_shared_key(shared2, &shared2Sz, priv2, priv2Sz,
  4699. pub, pubSz), 1);
  4700. ExpectIntEQ(sharedSz, CURVE448_KEY_SIZE);
  4701. ExpectIntEQ(shared2Sz, CURVE448_KEY_SIZE);
  4702. ExpectIntEQ(XMEMCMP(shared, shared2, sharedSz), 0);
  4703. #endif /* HAVE_CURVE448 && WOLFSSL_KEY_GEN */
  4704. return EXPECT_RESULT();
  4705. }
  4706. static int test_ED448(void)
  4707. {
  4708. EXPECT_DECLS;
  4709. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  4710. defined(WOLFSSL_KEY_GEN)
  4711. byte priv[ED448_PRV_KEY_SIZE];
  4712. unsigned int privSz = (unsigned int)sizeof(priv);
  4713. byte pub[ED448_PUB_KEY_SIZE];
  4714. unsigned int pubSz = (unsigned int)sizeof(pub);
  4715. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4716. const char* msg = TEST_STRING;
  4717. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4718. byte sig[ED448_SIG_SIZE];
  4719. unsigned int sigSz = (unsigned int)sizeof(sig);
  4720. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4721. /* Bad parameter testing of key generation. */
  4722. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, NULL, NULL), 0);
  4723. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, NULL, NULL, NULL), 0);
  4724. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, &privSz, NULL, NULL), 0);
  4725. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, pub, NULL), 0);
  4726. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, NULL, &pubSz), 0);
  4727. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4728. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, NULL, pub, &pubSz), 0);
  4729. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4730. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, NULL), 0);
  4731. /* Bad length. */
  4732. privSz = 1;
  4733. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4734. privSz = ED448_PRV_KEY_SIZE;
  4735. pubSz = 1;
  4736. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4737. pubSz = ED448_PUB_KEY_SIZE;
  4738. /* Good case of generating key. */
  4739. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 1);
  4740. ExpectIntEQ(privSz, ED448_PRV_KEY_SIZE);
  4741. ExpectIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  4742. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4743. /* Bad parameter testing of signing. */
  4744. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, NULL,
  4745. NULL), 0);
  4746. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, NULL, privSz, NULL,
  4747. NULL), 0);
  4748. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, priv, privSz, NULL,
  4749. NULL), 0);
  4750. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, sig,
  4751. NULL), 0);
  4752. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, NULL,
  4753. &sigSz), 0);
  4754. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, priv, privSz, sig,
  4755. &sigSz), 0);
  4756. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, NULL, privSz, sig,
  4757. &sigSz), 0);
  4758. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, NULL,
  4759. &sigSz), 0);
  4760. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4761. NULL), 0);
  4762. /* Bad length. */
  4763. privSz = 1;
  4764. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4765. &sigSz), 0);
  4766. privSz = ED448_PRV_KEY_SIZE;
  4767. sigSz = 1;
  4768. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4769. &sigSz), 0);
  4770. sigSz = ED448_SIG_SIZE;
  4771. /* Good case of signing. */
  4772. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4773. &sigSz), 1);
  4774. ExpectIntEQ(sigSz, ED448_SIG_SIZE);
  4775. #ifdef HAVE_ED448_VERIFY
  4776. /* Bad parameter testing of verification. */
  4777. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, NULL, pubSz, NULL,
  4778. sigSz), 0);
  4779. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, NULL, pubSz, NULL,
  4780. sigSz), 0);
  4781. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, pub, pubSz, NULL,
  4782. sigSz), 0);
  4783. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, NULL, pubSz, sig,
  4784. sigSz), 0);
  4785. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, pub, pubSz, sig,
  4786. sigSz), 0);
  4787. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, NULL, pubSz, sig,
  4788. sigSz), 0);
  4789. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, NULL,
  4790. sigSz), 0);
  4791. /* Bad length. */
  4792. pubSz = 1;
  4793. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4794. sigSz), 0);
  4795. pubSz = ED448_PUB_KEY_SIZE;
  4796. sigSz = 1;
  4797. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4798. sigSz), 0);
  4799. sigSz = ED448_SIG_SIZE;
  4800. /* Good case of verification. */
  4801. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4802. sigSz), 1);
  4803. /* Bad signature. */
  4804. if (EXPECT_SUCCESS()) {
  4805. sig[1] ^= 0x80;
  4806. }
  4807. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4808. sigSz), 0);
  4809. #endif /* HAVE_ED448_VERIFY */
  4810. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4811. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4812. return EXPECT_RESULT();
  4813. }
  4814. #endif /* OPENSSL_EXTRA */
  4815. #include <wolfssl/openssl/pem.h>
  4816. /*----------------------------------------------------------------------------*
  4817. | EVP
  4818. *----------------------------------------------------------------------------*/
  4819. static int test_wolfSSL_EVP_PKEY_print_public(void)
  4820. {
  4821. EXPECT_DECLS;
  4822. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  4823. WOLFSSL_BIO* rbio = NULL;
  4824. WOLFSSL_BIO* wbio = NULL;
  4825. WOLFSSL_EVP_PKEY* pkey = NULL;
  4826. char line[256] = { 0 };
  4827. char line1[256] = { 0 };
  4828. int i = 0;
  4829. /* test error cases */
  4830. ExpectIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  4831. /*
  4832. * test RSA public key print
  4833. * in this test, pass '3' for indent
  4834. */
  4835. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  4836. ExpectNotNull(rbio = BIO_new_mem_buf( client_keypub_der_1024,
  4837. sizeof_client_keypub_der_1024));
  4838. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4839. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4840. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  4841. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4842. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  4843. ExpectIntEQ(XSTRNCMP(line, line1, XSTRLEN(line1)), 0);
  4844. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4845. strcpy(line1, " Modulus:\n");
  4846. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4847. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4848. strcpy(line1, " 00:bc:73:0e:a8:49:f3:74:a2:a9:ef:18:a5:da:55:\n");
  4849. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4850. /* skip to the end of modulus element*/
  4851. for (i = 0; i < 8 ;i++) {
  4852. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4853. }
  4854. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4855. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  4856. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4857. /* should reach EOF */
  4858. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4859. EVP_PKEY_free(pkey);
  4860. pkey = NULL;
  4861. BIO_free(rbio);
  4862. BIO_free(wbio);
  4863. rbio = NULL;
  4864. wbio = NULL;
  4865. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  4866. /*
  4867. * test DSA public key print
  4868. */
  4869. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  4870. ExpectNotNull(rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  4871. sizeof_dsa_pub_key_der_2048));
  4872. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4873. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4874. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4875. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4876. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  4877. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4878. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4879. strcpy(line1, "pub:\n");
  4880. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4881. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4882. strcpy(line1,
  4883. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  4884. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4885. /* skip to the end of pub element*/
  4886. for (i = 0; i < 17 ;i++) {
  4887. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4888. }
  4889. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4890. strcpy(line1, "P:\n");
  4891. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4892. /* skip to the end of P element*/
  4893. for (i = 0; i < 18 ;i++) {
  4894. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4895. }
  4896. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4897. strcpy(line1, "Q:\n");
  4898. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4899. /* skip to the end of Q element*/
  4900. for (i = 0; i < 3 ;i++) {
  4901. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4902. }
  4903. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4904. strcpy(line1, "G:\n");
  4905. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4906. /* skip to the end of G element*/
  4907. for (i = 0; i < 18 ;i++) {
  4908. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4909. }
  4910. /* should reach EOF */
  4911. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4912. EVP_PKEY_free(pkey);
  4913. pkey = NULL;
  4914. BIO_free(rbio);
  4915. BIO_free(wbio);
  4916. rbio = NULL;
  4917. wbio = NULL;
  4918. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  4919. /*
  4920. * test ECC public key print
  4921. */
  4922. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  4923. ExpectNotNull(rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  4924. sizeof_ecc_clikeypub_der_256));
  4925. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4926. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4927. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4928. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4929. strcpy(line1, "Public-Key: (256 bit)\n");
  4930. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4931. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4932. strcpy(line1, "pub:\n");
  4933. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4934. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4935. strcpy(line1,
  4936. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  4937. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4938. /* skip to the end of pub element*/
  4939. for (i = 0; i < 4 ;i++) {
  4940. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4941. }
  4942. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4943. strcpy(line1, "ASN1 OID: prime256v1\n");
  4944. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4945. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4946. strcpy(line1, "NIST CURVE: P-256\n");
  4947. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4948. /* should reach EOF */
  4949. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4950. EVP_PKEY_free(pkey);
  4951. pkey = NULL;
  4952. BIO_free(rbio);
  4953. BIO_free(wbio);
  4954. rbio = NULL;
  4955. wbio = NULL;
  4956. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  4957. /*
  4958. * test DH public key print
  4959. */
  4960. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  4961. ExpectNotNull(rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  4962. sizeof_dh_pub_key_der_2048));
  4963. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4964. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4965. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL), 1);
  4966. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4967. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  4968. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4969. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4970. strcpy(line1, "public-key:\n");
  4971. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4972. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4973. strcpy(line1,
  4974. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  4975. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4976. /* skip to the end of public-key element*/
  4977. for (i = 0; i < 17 ;i++) {
  4978. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4979. }
  4980. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4981. strcpy(line1, "prime:\n");
  4982. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4983. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4984. strcpy(line1,
  4985. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  4986. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4987. /* skip to the end of prime element*/
  4988. for (i = 0; i < 17 ;i++) {
  4989. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4990. }
  4991. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4992. strcpy(line1, "generator: 2 (0x02)\n");
  4993. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4994. /* should reach EOF */
  4995. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4996. EVP_PKEY_free(pkey);
  4997. pkey = NULL;
  4998. BIO_free(rbio);
  4999. BIO_free(wbio);
  5000. rbio = NULL;
  5001. wbio = NULL;
  5002. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  5003. /* to prevent "unused variable" warning */
  5004. (void)pkey;
  5005. (void)wbio;
  5006. (void)rbio;
  5007. (void)line;
  5008. (void)line1;
  5009. (void)i;
  5010. #endif /* OPENSSL_EXTRA */
  5011. return EXPECT_RESULT();
  5012. }
  5013. /* Test functions for base64 encode/decode */
  5014. static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
  5015. {
  5016. EXPECT_DECLS;
  5017. #if defined(OPENSSL_EXTRA) && \
  5018. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  5019. EVP_ENCODE_CTX* ctx = NULL;
  5020. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5021. ExpectIntEQ(ctx->remaining,0);
  5022. ExpectIntEQ(ctx->data[0],0);
  5023. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1],0);
  5024. EVP_ENCODE_CTX_free(ctx);
  5025. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  5026. return EXPECT_RESULT();
  5027. }
  5028. static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
  5029. {
  5030. EXPECT_DECLS;
  5031. #if defined(OPENSSL_EXTRA) && \
  5032. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  5033. EVP_ENCODE_CTX* ctx = NULL;
  5034. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5035. EVP_ENCODE_CTX_free(ctx);
  5036. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  5037. return EXPECT_RESULT();
  5038. }
  5039. static int test_wolfSSL_EVP_EncodeInit(void)
  5040. {
  5041. EXPECT_DECLS;
  5042. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  5043. EVP_ENCODE_CTX* ctx = NULL;
  5044. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5045. ExpectIntEQ(ctx->remaining, 0);
  5046. ExpectIntEQ(ctx->data[0], 0);
  5047. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  5048. if (ctx != NULL) {
  5049. /* make ctx dirty */
  5050. ctx->remaining = 10;
  5051. XMEMSET(ctx->data, 0x77, sizeof(ctx->data));
  5052. }
  5053. EVP_EncodeInit(ctx);
  5054. ExpectIntEQ(ctx->remaining, 0);
  5055. ExpectIntEQ(ctx->data[0], 0);
  5056. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  5057. EVP_ENCODE_CTX_free(ctx);
  5058. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  5059. return EXPECT_RESULT();
  5060. }
  5061. static int test_wolfSSL_EVP_EncodeUpdate(void)
  5062. {
  5063. EXPECT_DECLS;
  5064. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  5065. int outl;
  5066. int total;
  5067. const unsigned char plain0[] = {"Th"};
  5068. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  5069. const unsigned char plain2[] = {"This is additional data."};
  5070. const unsigned char encBlock0[] = {"VGg="};
  5071. const unsigned char enc0[] = {"VGg=\n"};
  5072. /* expected encoded result for the first output 64 chars plus trailing LF*/
  5073. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  5074. const unsigned char enc2[] =
  5075. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  5076. unsigned char encOutBuff[300];
  5077. EVP_ENCODE_CTX* ctx = NULL;
  5078. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5079. EVP_EncodeInit(ctx);
  5080. /* illegal parameter test */
  5081. ExpectIntEQ(
  5082. EVP_EncodeUpdate(
  5083. NULL, /* pass NULL as ctx */
  5084. encOutBuff,
  5085. &outl,
  5086. plain1,
  5087. sizeof(plain1)-1),
  5088. 0 /* expected result code 0: fail */
  5089. );
  5090. ExpectIntEQ(
  5091. EVP_EncodeUpdate(
  5092. ctx,
  5093. NULL, /* pass NULL as out buff */
  5094. &outl,
  5095. plain1,
  5096. sizeof(plain1)-1),
  5097. 0 /* expected result code 0: fail */
  5098. );
  5099. ExpectIntEQ(
  5100. EVP_EncodeUpdate(
  5101. ctx,
  5102. encOutBuff,
  5103. NULL, /* pass NULL as outl */
  5104. plain1,
  5105. sizeof(plain1)-1),
  5106. 0 /* expected result code 0: fail */
  5107. );
  5108. ExpectIntEQ(
  5109. EVP_EncodeUpdate(
  5110. ctx,
  5111. encOutBuff,
  5112. &outl,
  5113. NULL, /* pass NULL as in */
  5114. sizeof(plain1)-1),
  5115. 0 /* expected result code 0: fail */
  5116. );
  5117. ExpectIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  5118. /* meaningless parameter test */
  5119. ExpectIntEQ(
  5120. EVP_EncodeUpdate(
  5121. ctx,
  5122. encOutBuff,
  5123. &outl,
  5124. plain1,
  5125. 0), /* pass zero input */
  5126. 1 /* expected result code 1: success */
  5127. );
  5128. /* very small data encoding test */
  5129. EVP_EncodeInit(ctx);
  5130. ExpectIntEQ(
  5131. EVP_EncodeUpdate(
  5132. ctx,
  5133. encOutBuff,
  5134. &outl,
  5135. plain0,
  5136. sizeof(plain0)-1),
  5137. 1 /* expected result code 1: success */
  5138. );
  5139. ExpectIntEQ(outl,0);
  5140. if (EXPECT_SUCCESS()) {
  5141. EVP_EncodeFinal(
  5142. ctx,
  5143. encOutBuff + outl,
  5144. &outl);
  5145. }
  5146. ExpectIntEQ( outl, sizeof(enc0)-1);
  5147. ExpectIntEQ(
  5148. XSTRNCMP(
  5149. (const char*)encOutBuff,
  5150. (const char*)enc0,sizeof(enc0) ),
  5151. 0);
  5152. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  5153. ExpectIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  5154. sizeof(encBlock0)-1);
  5155. ExpectStrEQ(encOutBuff, encBlock0);
  5156. /* pass small size( < 48bytes ) input, then make sure they are not
  5157. * encoded and just stored in ctx
  5158. */
  5159. EVP_EncodeInit(ctx);
  5160. total = 0;
  5161. outl = 0;
  5162. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  5163. ExpectIntEQ(
  5164. EVP_EncodeUpdate(
  5165. ctx,
  5166. encOutBuff, /* buffer for output */
  5167. &outl, /* size of output */
  5168. plain1, /* input */
  5169. sizeof(plain1)-1), /* size of input */
  5170. 1); /* expected result code 1:success */
  5171. total += outl;
  5172. ExpectIntEQ(outl, 0); /* no output expected */
  5173. ExpectIntEQ(ctx->remaining, sizeof(plain1) -1);
  5174. ExpectTrue(
  5175. XSTRNCMP((const char*)(ctx->data),
  5176. (const char*)plain1,
  5177. ctx->remaining) ==0 );
  5178. ExpectTrue(encOutBuff[0] == 0);
  5179. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  5180. * the stored data and the new input together
  5181. */
  5182. ExpectIntEQ(
  5183. EVP_EncodeUpdate(
  5184. ctx,
  5185. encOutBuff + outl, /* buffer for output */
  5186. &outl, /* size of output */
  5187. plain2, /* additional input */
  5188. sizeof(plain2) -1), /* size of additional input */
  5189. 1); /* expected result code 1:success */
  5190. total += outl;
  5191. ExpectIntNE(outl, 0); /* some output is expected this time*/
  5192. ExpectIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  5193. ExpectIntEQ(
  5194. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  5195. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  5196. EVP_EncodeFinal(
  5197. ctx,
  5198. encOutBuff + outl,
  5199. &outl);
  5200. total += outl;
  5201. ExpectIntNE(total,0);
  5202. ExpectIntNE(outl,0);
  5203. ExpectIntEQ(XSTRNCMP(
  5204. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  5205. /* test with illeagal parameters */
  5206. outl = 1;
  5207. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  5208. ExpectIntEQ(outl, 0);
  5209. outl = 1;
  5210. EVP_EncodeFinal(ctx, NULL, &outl);
  5211. ExpectIntEQ(outl, 0);
  5212. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  5213. EVP_EncodeFinal(NULL, NULL, NULL);
  5214. EVP_ENCODE_CTX_free(ctx);
  5215. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  5216. return EXPECT_RESULT();
  5217. }
  5218. static int test_wolfSSL_EVP_EncodeFinal(void)
  5219. {
  5220. int res = TEST_SKIPPED;
  5221. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  5222. /* tests for wolfSSL_EVP_EncodeFinal are included in
  5223. * test_wolfSSL_EVP_EncodeUpdate
  5224. */
  5225. res = TEST_SUCCESS;
  5226. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  5227. return res;
  5228. }
  5229. static int test_wolfSSL_EVP_DecodeInit(void)
  5230. {
  5231. EXPECT_DECLS;
  5232. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5233. EVP_ENCODE_CTX* ctx = NULL;
  5234. ExpectNotNull( ctx = EVP_ENCODE_CTX_new());
  5235. ExpectIntEQ( ctx->remaining,0);
  5236. ExpectIntEQ( ctx->data[0],0);
  5237. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  5238. if (ctx != NULL) {
  5239. /* make ctx dirty */
  5240. ctx->remaining = 10;
  5241. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  5242. }
  5243. EVP_DecodeInit(ctx);
  5244. ExpectIntEQ( ctx->remaining,0);
  5245. ExpectIntEQ( ctx->data[0],0);
  5246. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  5247. EVP_ENCODE_CTX_free(ctx);
  5248. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5249. return EXPECT_RESULT();
  5250. }
  5251. static int test_wolfSSL_EVP_DecodeUpdate(void)
  5252. {
  5253. EXPECT_DECLS;
  5254. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5255. int outl;
  5256. unsigned char decOutBuff[300];
  5257. EVP_ENCODE_CTX* ctx = NULL;
  5258. static const unsigned char enc1[] =
  5259. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  5260. /* const unsigned char plain1[] =
  5261. {"This is a base64 decoding test."} */
  5262. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5263. EVP_DecodeInit(ctx);
  5264. /* illegal parameter tests */
  5265. /* pass NULL as ctx */
  5266. ExpectIntEQ(
  5267. EVP_DecodeUpdate(
  5268. NULL, /* pass NULL as ctx */
  5269. decOutBuff,
  5270. &outl,
  5271. enc1,
  5272. sizeof(enc1)-1),
  5273. -1 /* expected result code -1: fail */
  5274. );
  5275. ExpectIntEQ( outl, 0);
  5276. /* pass NULL as output */
  5277. ExpectIntEQ(
  5278. EVP_DecodeUpdate(
  5279. ctx,
  5280. NULL, /* pass NULL as out buff */
  5281. &outl,
  5282. enc1,
  5283. sizeof(enc1)-1),
  5284. -1 /* expected result code -1: fail */
  5285. );
  5286. ExpectIntEQ( outl, 0);
  5287. /* pass NULL as outl */
  5288. ExpectIntEQ(
  5289. EVP_DecodeUpdate(
  5290. ctx,
  5291. decOutBuff,
  5292. NULL, /* pass NULL as outl */
  5293. enc1,
  5294. sizeof(enc1)-1),
  5295. -1 /* expected result code -1: fail */
  5296. );
  5297. /* pass NULL as input */
  5298. ExpectIntEQ(
  5299. EVP_DecodeUpdate(
  5300. ctx,
  5301. decOutBuff,
  5302. &outl,
  5303. NULL, /* pass NULL as in */
  5304. sizeof(enc1)-1),
  5305. -1 /* expected result code -1: fail */
  5306. );
  5307. ExpectIntEQ( outl, 0);
  5308. ExpectIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  5309. /* pass zero length input */
  5310. ExpectIntEQ(
  5311. EVP_DecodeUpdate(
  5312. ctx,
  5313. decOutBuff,
  5314. &outl,
  5315. enc1,
  5316. 0), /* pass zero as input len */
  5317. 1 /* expected result code 1: success */
  5318. );
  5319. /* decode correct base64 string */
  5320. {
  5321. static const unsigned char enc2[] =
  5322. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  5323. static const unsigned char plain2[] =
  5324. {"This is a base64 decoding test."};
  5325. EVP_EncodeInit(ctx);
  5326. ExpectIntEQ(
  5327. EVP_DecodeUpdate(
  5328. ctx,
  5329. decOutBuff,
  5330. &outl,
  5331. enc2,
  5332. sizeof(enc2)-1),
  5333. 0 /* expected result code 0: success */
  5334. );
  5335. ExpectIntEQ(outl,sizeof(plain2) -1);
  5336. ExpectIntEQ(
  5337. EVP_DecodeFinal(
  5338. ctx,
  5339. decOutBuff + outl,
  5340. &outl),
  5341. 1 /* expected result code 1: success */
  5342. );
  5343. ExpectIntEQ(outl, 0); /* expected DecodeFinal output no data */
  5344. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  5345. sizeof(plain2) -1 ),0);
  5346. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  5347. sizeof(plain2)-1);
  5348. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  5349. sizeof(plain2) -1 ),0);
  5350. }
  5351. /* decode correct base64 string which does not have '\n' in its last*/
  5352. {
  5353. static const unsigned char enc3[] =
  5354. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  5355. static const unsigned char plain3[] =
  5356. {"This is a base64 decoding test."}; /* 31 chars */
  5357. EVP_EncodeInit(ctx);
  5358. ExpectIntEQ(
  5359. EVP_DecodeUpdate(
  5360. ctx,
  5361. decOutBuff,
  5362. &outl,
  5363. enc3,
  5364. sizeof(enc3)-1),
  5365. 0 /* expected result code 0: success */
  5366. );
  5367. ExpectIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  5368. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  5369. sizeof(plain3) -1 ),0);
  5370. ExpectIntEQ(
  5371. EVP_DecodeFinal(
  5372. ctx,
  5373. decOutBuff + outl,
  5374. &outl),
  5375. 1 /* expected result code 1: success */
  5376. );
  5377. ExpectIntEQ(outl,0 );
  5378. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  5379. sizeof(plain3)-1);
  5380. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  5381. sizeof(plain3) -1 ),0);
  5382. }
  5383. /* decode string which has a padding char ('=') in the illegal position*/
  5384. {
  5385. static const unsigned char enc4[] =
  5386. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  5387. EVP_EncodeInit(ctx);
  5388. ExpectIntEQ(
  5389. EVP_DecodeUpdate(
  5390. ctx,
  5391. decOutBuff,
  5392. &outl,
  5393. enc4,
  5394. sizeof(enc4)-1),
  5395. -1 /* expected result code -1: error */
  5396. );
  5397. ExpectIntEQ(outl,0);
  5398. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  5399. }
  5400. /* small data decode test */
  5401. {
  5402. static const unsigned char enc00[] = {"VG"};
  5403. static const unsigned char enc01[] = {"g=\n"};
  5404. static const unsigned char plain4[] = {"Th"};
  5405. EVP_EncodeInit(ctx);
  5406. ExpectIntEQ(
  5407. EVP_DecodeUpdate(
  5408. ctx,
  5409. decOutBuff,
  5410. &outl,
  5411. enc00,
  5412. sizeof(enc00)-1),
  5413. 1 /* expected result code 1: success */
  5414. );
  5415. ExpectIntEQ(outl,0);
  5416. ExpectIntEQ(
  5417. EVP_DecodeUpdate(
  5418. ctx,
  5419. decOutBuff + outl,
  5420. &outl,
  5421. enc01,
  5422. sizeof(enc01)-1),
  5423. 0 /* expected result code 0: success */
  5424. );
  5425. ExpectIntEQ(outl,sizeof(plain4)-1);
  5426. /* test with illegal parameters */
  5427. ExpectIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  5428. ExpectIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  5429. ExpectIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  5430. ExpectIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  5431. if (EXPECT_SUCCESS()) {
  5432. EVP_DecodeFinal(
  5433. ctx,
  5434. decOutBuff + outl,
  5435. &outl);
  5436. }
  5437. ExpectIntEQ( outl, 0);
  5438. ExpectIntEQ(
  5439. XSTRNCMP(
  5440. (const char*)decOutBuff,
  5441. (const char*)plain4,sizeof(plain4)-1 ),
  5442. 0);
  5443. }
  5444. EVP_ENCODE_CTX_free(ctx);
  5445. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5446. return EXPECT_RESULT();
  5447. }
  5448. static int test_wolfSSL_EVP_DecodeFinal(void)
  5449. {
  5450. int res = TEST_SKIPPED;
  5451. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5452. /* tests for wolfSSL_EVP_DecodeFinal are included in
  5453. * test_wolfSSL_EVP_DecodeUpdate
  5454. */
  5455. res = TEST_SUCCESS;
  5456. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5457. return res;
  5458. }
  5459. /* Test function for wolfSSL_EVP_get_cipherbynid.
  5460. */
  5461. #ifdef OPENSSL_EXTRA
  5462. static int test_wolfSSL_EVP_get_cipherbynid(void)
  5463. {
  5464. EXPECT_DECLS;
  5465. #ifndef NO_AES
  5466. const WOLFSSL_EVP_CIPHER* c;
  5467. c = wolfSSL_EVP_get_cipherbynid(419);
  5468. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5469. defined(WOLFSSL_AES_128)
  5470. ExpectNotNull(c);
  5471. ExpectNotNull(XSTRCMP("EVP_AES_128_CBC", c));
  5472. #else
  5473. ExpectNull(c);
  5474. #endif
  5475. c = wolfSSL_EVP_get_cipherbynid(423);
  5476. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5477. defined(WOLFSSL_AES_192)
  5478. ExpectNotNull(c);
  5479. ExpectNotNull(XSTRCMP("EVP_AES_192_CBC", c));
  5480. #else
  5481. ExpectNull(c);
  5482. #endif
  5483. c = wolfSSL_EVP_get_cipherbynid(427);
  5484. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5485. defined(WOLFSSL_AES_256)
  5486. ExpectNotNull(c);
  5487. ExpectNotNull(XSTRCMP("EVP_AES_256_CBC", c));
  5488. #else
  5489. ExpectNull(c);
  5490. #endif
  5491. c = wolfSSL_EVP_get_cipherbynid(904);
  5492. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  5493. ExpectNotNull(c);
  5494. ExpectNotNull(XSTRCMP("EVP_AES_128_CTR", c));
  5495. #else
  5496. ExpectNull(c);
  5497. #endif
  5498. c = wolfSSL_EVP_get_cipherbynid(905);
  5499. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  5500. ExpectNotNull(c);
  5501. ExpectNotNull(XSTRCMP("EVP_AES_192_CTR", c));
  5502. #else
  5503. ExpectNull(c);
  5504. #endif
  5505. c = wolfSSL_EVP_get_cipherbynid(906);
  5506. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  5507. ExpectNotNull(c);
  5508. ExpectNotNull(XSTRCMP("EVP_AES_256_CTR", c));
  5509. #else
  5510. ExpectNull(c);
  5511. #endif
  5512. c = wolfSSL_EVP_get_cipherbynid(418);
  5513. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  5514. ExpectNotNull(c);
  5515. ExpectNotNull(XSTRCMP("EVP_AES_128_ECB", c));
  5516. #else
  5517. ExpectNull(c);
  5518. #endif
  5519. c = wolfSSL_EVP_get_cipherbynid(422);
  5520. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  5521. ExpectNotNull(c);
  5522. ExpectNotNull(XSTRCMP("EVP_AES_192_ECB", c));
  5523. #else
  5524. ExpectNull(c);
  5525. #endif
  5526. c = wolfSSL_EVP_get_cipherbynid(426);
  5527. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  5528. ExpectNotNull(c);
  5529. ExpectNotNull(XSTRCMP("EVP_AES_256_ECB", c));
  5530. #else
  5531. ExpectNull(c);
  5532. #endif
  5533. #endif /* !NO_AES */
  5534. #ifndef NO_DES3
  5535. ExpectNotNull(XSTRCMP("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  5536. #ifdef WOLFSSL_DES_ECB
  5537. ExpectNotNull(XSTRCMP("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  5538. #endif
  5539. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  5540. #ifdef WOLFSSL_DES_ECB
  5541. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  5542. #endif
  5543. #endif /* !NO_DES3 */
  5544. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  5545. ExpectNotNull(XSTRCMP("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
  5546. #endif
  5547. /* test for nid is out of range */
  5548. ExpectNull(wolfSSL_EVP_get_cipherbynid(1));
  5549. return EXPECT_RESULT();
  5550. }
  5551. static int test_wolfSSL_EVP_CIPHER_CTX(void)
  5552. {
  5553. EXPECT_DECLS;
  5554. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  5555. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  5556. const EVP_CIPHER *init = EVP_aes_128_cbc();
  5557. const EVP_CIPHER *test;
  5558. byte key[AES_BLOCK_SIZE] = {0};
  5559. byte iv[AES_BLOCK_SIZE] = {0};
  5560. ExpectNotNull(ctx);
  5561. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  5562. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  5563. test = EVP_CIPHER_CTX_cipher(ctx);
  5564. ExpectTrue(init == test);
  5565. ExpectIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  5566. ExpectIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  5567. ExpectIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  5568. EVP_CIPHER_CTX_free(ctx);
  5569. /* test EVP_CIPHER_CTX_cleanup with NULL */
  5570. ExpectIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  5571. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  5572. return EXPECT_RESULT();
  5573. }
  5574. #endif /* OPENSSL_EXTRA */
  5575. /*----------------------------------------------------------------------------*
  5576. | IO
  5577. *----------------------------------------------------------------------------*/
  5578. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) || \
  5579. defined(HAVE_IO_TESTS_DEPENDENCIES)
  5580. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5581. #ifdef WC_SHA512_DIGEST_SIZE
  5582. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  5583. #else
  5584. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  5585. #endif
  5586. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  5587. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  5588. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  5589. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  5590. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5591. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  5592. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  5593. defined(HAVE_AES_CBC)
  5594. typedef struct openssl_key_ctx {
  5595. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  5596. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  5597. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  5598. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  5599. } openssl_key_ctx;
  5600. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  5601. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  5602. static WC_INLINE int OpenSSLTicketInit(void)
  5603. {
  5604. int ret = wc_InitRng(&myOpenSSLKey_rng);
  5605. if (ret != 0) return ret;
  5606. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  5607. sizeof(myOpenSSLKey_ctx.name));
  5608. if (ret != 0) return ret;
  5609. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  5610. sizeof(myOpenSSLKey_ctx.key));
  5611. if (ret != 0) return ret;
  5612. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  5613. sizeof(myOpenSSLKey_ctx.hmacKey));
  5614. if (ret != 0) return ret;
  5615. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  5616. sizeof(myOpenSSLKey_ctx.iv));
  5617. if (ret != 0) return ret;
  5618. return 0;
  5619. }
  5620. static int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  5621. byte name[WOLFSSL_TICKET_NAME_SZ],
  5622. byte iv[WOLFSSL_TICKET_IV_SZ],
  5623. WOLFSSL_EVP_CIPHER_CTX *ectx,
  5624. WOLFSSL_HMAC_CTX *hctx, int enc) {
  5625. (void)ssl;
  5626. if (enc) {
  5627. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  5628. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  5629. }
  5630. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  5631. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  5632. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  5633. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  5634. return 0;
  5635. }
  5636. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  5637. if (enc)
  5638. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  5639. else
  5640. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  5641. return 1;
  5642. }
  5643. static WC_INLINE void OpenSSLTicketCleanup(void)
  5644. {
  5645. wc_FreeRng(&myOpenSSLKey_rng);
  5646. }
  5647. #endif
  5648. #endif
  5649. /* helper functions */
  5650. #ifdef HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  5651. static WC_INLINE int test_ssl_memio_write_cb(WOLFSSL *ssl, char *data, int sz,
  5652. void *ctx)
  5653. {
  5654. struct test_ssl_memio_ctx *test_ctx;
  5655. byte *buf;
  5656. int *len;
  5657. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  5658. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  5659. buf = test_ctx->c_buff;
  5660. len = &test_ctx->c_len;
  5661. }
  5662. else {
  5663. buf = test_ctx->s_buff;
  5664. len = &test_ctx->s_len;
  5665. }
  5666. if ((unsigned)(*len + sz) > TEST_SSL_MEMIO_BUF_SZ)
  5667. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  5668. XMEMCPY(buf + *len, data, sz);
  5669. *len += sz;
  5670. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  5671. {
  5672. /* This can be imported into Wireshark by transforming the file with
  5673. * od -Ax -tx1 -v test_output.dump > test_output.dump.hex
  5674. * And then loading test_output.dump.hex into Wireshark using the
  5675. * "Import from Hex Dump..." option ion and selecting the TCP
  5676. * encapsulation option. */
  5677. char dump_file_name[64];
  5678. WOLFSSL_BIO *dump_file;
  5679. sprintf(dump_file_name, "%s/%s.dump", tmpDirName, currentTestName);
  5680. dump_file = wolfSSL_BIO_new_file(dump_file_name, "a");
  5681. if (dump_file != NULL) {
  5682. (void)wolfSSL_BIO_write(dump_file, data, sz);
  5683. wolfSSL_BIO_free(dump_file);
  5684. }
  5685. }
  5686. #endif
  5687. return sz;
  5688. }
  5689. static WC_INLINE int test_ssl_memio_read_cb(WOLFSSL *ssl, char *data, int sz,
  5690. void *ctx)
  5691. {
  5692. struct test_ssl_memio_ctx *test_ctx;
  5693. int read_sz;
  5694. byte *buf;
  5695. int *len;
  5696. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  5697. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  5698. buf = test_ctx->s_buff;
  5699. len = &test_ctx->s_len;
  5700. }
  5701. else {
  5702. buf = test_ctx->c_buff;
  5703. len = &test_ctx->c_len;
  5704. }
  5705. if (*len == 0)
  5706. return WOLFSSL_CBIO_ERR_WANT_READ;
  5707. read_sz = sz < *len ? sz : *len;
  5708. XMEMCPY(data, buf, read_sz);
  5709. XMEMMOVE(buf, buf + read_sz, *len - read_sz);
  5710. *len -= read_sz;
  5711. return read_sz;
  5712. }
  5713. static WC_INLINE int test_ssl_memio_setup(test_ssl_memio_ctx *ctx)
  5714. {
  5715. EXPECT_DECLS;
  5716. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5717. int c_sharedCtx = 0;
  5718. int s_sharedCtx = 0;
  5719. #endif
  5720. const char* clientCertFile = cliCertFile;
  5721. const char* clientKeyFile = cliKeyFile;
  5722. const char* serverCertFile = svrCertFile;
  5723. const char* serverKeyFile = svrKeyFile;
  5724. /********************************
  5725. * Create WOLFSSL_CTX for client.
  5726. ********************************/
  5727. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5728. if (ctx->c_ctx != NULL) {
  5729. c_sharedCtx = ctx->c_cb.isSharedCtx;
  5730. }
  5731. else
  5732. #endif
  5733. {
  5734. WOLFSSL_METHOD* method = NULL;
  5735. if (ctx->c_cb.method != NULL) {
  5736. method = ctx->c_cb.method();
  5737. }
  5738. else {
  5739. method = wolfSSLv23_client_method();
  5740. }
  5741. ExpectNotNull(ctx->c_ctx = wolfSSL_CTX_new(method));
  5742. }
  5743. wolfSSL_SetIORecv(ctx->c_ctx, test_ssl_memio_read_cb);
  5744. wolfSSL_SetIOSend(ctx->c_ctx, test_ssl_memio_write_cb);
  5745. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5746. wolfSSL_CTX_set_default_passwd_cb(ctx->c_ctx, PasswordCallBack);
  5747. #endif
  5748. if (ctx->c_cb.caPemFile != NULL)
  5749. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx,
  5750. ctx->c_cb.caPemFile, 0), WOLFSSL_SUCCESS);
  5751. else
  5752. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx,
  5753. caCertFile, 0), WOLFSSL_SUCCESS);
  5754. if (ctx->c_cb.certPemFile != NULL) {
  5755. clientCertFile = ctx->c_cb.certPemFile;
  5756. }
  5757. if (ctx->c_cb.keyPemFile != NULL) {
  5758. clientKeyFile = ctx->c_cb.keyPemFile;
  5759. }
  5760. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5761. if (!c_sharedCtx)
  5762. #endif
  5763. {
  5764. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->c_ctx,
  5765. clientCertFile), WOLFSSL_SUCCESS);
  5766. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->c_ctx, clientKeyFile,
  5767. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5768. }
  5769. #ifdef HAVE_CRL
  5770. if (ctx->c_cb.crlPemFile != NULL) {
  5771. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx->c_ctx, WOLFSSL_CRL_CHECKALL),
  5772. WOLFSSL_SUCCESS);
  5773. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx->c_ctx, ctx->c_cb.crlPemFile,
  5774. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5775. }
  5776. #endif
  5777. if (ctx->c_ciphers != NULL) {
  5778. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->c_ctx, ctx->c_ciphers),
  5779. WOLFSSL_SUCCESS);
  5780. }
  5781. if (ctx->c_cb.ctx_ready != NULL) {
  5782. ExpectIntEQ(ctx->c_cb.ctx_ready(ctx->c_ctx), TEST_SUCCESS);
  5783. }
  5784. /********************************
  5785. * Create WOLFSSL_CTX for server.
  5786. ********************************/
  5787. if (ctx->s_ctx != NULL) {
  5788. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5789. s_sharedCtx = 1;
  5790. #endif
  5791. ctx->s_cb.isSharedCtx = 1;
  5792. }
  5793. else
  5794. {
  5795. WOLFSSL_METHOD* method = NULL;
  5796. if (ctx->s_cb.method != NULL) {
  5797. method = ctx->s_cb.method();
  5798. }
  5799. else {
  5800. method = wolfSSLv23_server_method();
  5801. }
  5802. ExpectNotNull(ctx->s_ctx = wolfSSL_CTX_new(method));
  5803. ctx->s_cb.isSharedCtx = 0;
  5804. }
  5805. if (!ctx->s_cb.ticNoInit && (ctx->s_ctx != NULL)) {
  5806. #if defined(HAVE_SESSION_TICKET) && \
  5807. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5808. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5809. OpenSSLTicketInit();
  5810. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx->s_ctx, myTicketEncCbOpenSSL);
  5811. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5812. TicketInit();
  5813. wolfSSL_CTX_set_TicketEncCb(ctx->s_ctx, myTicketEncCb);
  5814. #endif
  5815. #endif
  5816. }
  5817. wolfSSL_SetIORecv(ctx->s_ctx, test_ssl_memio_read_cb);
  5818. wolfSSL_SetIOSend(ctx->s_ctx, test_ssl_memio_write_cb);
  5819. wolfSSL_CTX_set_verify(ctx->s_ctx, WOLFSSL_VERIFY_PEER |
  5820. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5821. if (ctx->s_cb.caPemFile != NULL)
  5822. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx,
  5823. ctx->s_cb.caPemFile, 0), WOLFSSL_SUCCESS);
  5824. else
  5825. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx,
  5826. cliCertFile, 0), WOLFSSL_SUCCESS);
  5827. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5828. wolfSSL_CTX_set_default_passwd_cb(ctx->s_ctx, PasswordCallBack);
  5829. #endif
  5830. if (ctx->s_cb.certPemFile != NULL) {
  5831. serverCertFile = ctx->s_cb.certPemFile;
  5832. }
  5833. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5834. if (!s_sharedCtx)
  5835. #endif
  5836. {
  5837. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->s_ctx,
  5838. serverCertFile), WOLFSSL_SUCCESS);
  5839. }
  5840. if (ctx->s_cb.keyPemFile != NULL) {
  5841. serverKeyFile = ctx->s_cb.keyPemFile;
  5842. }
  5843. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5844. if (!s_sharedCtx)
  5845. #endif
  5846. {
  5847. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->s_ctx, serverKeyFile,
  5848. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5849. }
  5850. if (ctx->s_ciphers != NULL) {
  5851. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->s_ctx, ctx->s_ciphers),
  5852. WOLFSSL_SUCCESS);
  5853. }
  5854. if (ctx->s_cb.ctx_ready != NULL) {
  5855. ExpectIntEQ(ctx->s_cb.ctx_ready(ctx->s_ctx), TEST_SUCCESS);
  5856. }
  5857. /****************************
  5858. * Create WOLFSSL for client.
  5859. ****************************/
  5860. ExpectNotNull(ctx->c_ssl = wolfSSL_new(ctx->c_ctx));
  5861. wolfSSL_SetIOWriteCtx(ctx->c_ssl, ctx);
  5862. wolfSSL_SetIOReadCtx(ctx->c_ssl, ctx);
  5863. if (0
  5864. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5865. || c_sharedCtx
  5866. #endif
  5867. )
  5868. {
  5869. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->c_ssl,
  5870. clientCertFile), WOLFSSL_SUCCESS);
  5871. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->c_ssl, clientKeyFile,
  5872. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5873. }
  5874. if (ctx->c_cb.ssl_ready != NULL) {
  5875. ExpectIntEQ(ctx->c_cb.ssl_ready(ctx->c_ssl), TEST_SUCCESS);
  5876. }
  5877. /****************************
  5878. * Create WOLFSSL for server.
  5879. ****************************/
  5880. ExpectNotNull(ctx->s_ssl = wolfSSL_new(ctx->s_ctx));
  5881. wolfSSL_SetIOWriteCtx(ctx->s_ssl, ctx);
  5882. wolfSSL_SetIOReadCtx(ctx->s_ssl, ctx);
  5883. if (0
  5884. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5885. || s_sharedCtx
  5886. #endif
  5887. )
  5888. {
  5889. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->s_ssl,
  5890. serverCertFile), WOLFSSL_SUCCESS);
  5891. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->s_ssl, serverKeyFile,
  5892. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5893. }
  5894. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5895. wolfSSL_SetTmpDH_file(ctx->s_ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5896. #elif !defined(NO_DH)
  5897. /* will repick suites with DHE, higher priority than PSK */
  5898. SetDH(ctx->s_ssl);
  5899. #endif
  5900. if (ctx->s_cb.ssl_ready != NULL) {
  5901. ExpectIntEQ(ctx->s_cb.ssl_ready(ctx->s_ssl), TEST_SUCCESS);
  5902. }
  5903. return EXPECT_RESULT();
  5904. }
  5905. static int test_ssl_memio_do_handshake(test_ssl_memio_ctx* ctx, int max_rounds,
  5906. int* rounds)
  5907. {
  5908. int handshake_complete = 0;
  5909. int hs_c = 0;
  5910. int hs_s = 0;
  5911. int failing_s = 0;
  5912. int failing_c = 0;
  5913. int ret;
  5914. int err;
  5915. if (rounds != NULL) {
  5916. *rounds = 0;
  5917. }
  5918. while ((!handshake_complete) && (max_rounds > 0)) {
  5919. if (!hs_c) {
  5920. wolfSSL_SetLoggingPrefix("client");
  5921. ret = wolfSSL_connect(ctx->c_ssl);
  5922. wolfSSL_SetLoggingPrefix(NULL);
  5923. if (ret == WOLFSSL_SUCCESS) {
  5924. hs_c = 1;
  5925. }
  5926. else {
  5927. err = wolfSSL_get_error(ctx->c_ssl, ret);
  5928. if (err != WOLFSSL_ERROR_WANT_READ &&
  5929. err != WOLFSSL_ERROR_WANT_WRITE) {
  5930. char buff[WOLFSSL_MAX_ERROR_SZ];
  5931. fprintf(stderr, "error = %d, %s\n", err,
  5932. wolfSSL_ERR_error_string(err, buff));
  5933. failing_c = 1;
  5934. hs_c = 1;
  5935. if (failing_c && failing_s) {
  5936. break;
  5937. }
  5938. }
  5939. }
  5940. }
  5941. if (!hs_s) {
  5942. wolfSSL_SetLoggingPrefix("server");
  5943. ret = wolfSSL_accept(ctx->s_ssl);
  5944. wolfSSL_SetLoggingPrefix(NULL);
  5945. if (ret == WOLFSSL_SUCCESS) {
  5946. hs_s = 1;
  5947. }
  5948. else {
  5949. err = wolfSSL_get_error(ctx->s_ssl, ret);
  5950. if (err != WOLFSSL_ERROR_WANT_READ &&
  5951. err != WOLFSSL_ERROR_WANT_WRITE) {
  5952. char buff[WOLFSSL_MAX_ERROR_SZ];
  5953. fprintf(stderr, "error = %d, %s\n", err,
  5954. wolfSSL_ERR_error_string(err, buff));
  5955. failing_s = 1;
  5956. hs_s = 1;
  5957. if (failing_c && failing_s) {
  5958. break;
  5959. }
  5960. }
  5961. }
  5962. }
  5963. handshake_complete = hs_c && hs_s;
  5964. max_rounds--;
  5965. if (rounds != NULL) {
  5966. *rounds += 1;
  5967. }
  5968. }
  5969. if (!handshake_complete || failing_c || failing_s) {
  5970. return TEST_FAIL;
  5971. }
  5972. return TEST_SUCCESS;
  5973. }
  5974. static int test_ssl_memio_read_write(test_ssl_memio_ctx* ctx)
  5975. {
  5976. EXPECT_DECLS;
  5977. char input[1024];
  5978. int idx = 0;
  5979. const char* msg_c = "hello wolfssl!";
  5980. int msglen_c = (int)XSTRLEN(msg_c);
  5981. const char* msg_s = "I hear you fa shizzle!";
  5982. int msglen_s = (int)XSTRLEN(msg_s);
  5983. if (ctx->c_msg != NULL) {
  5984. msg_c = ctx->c_msg;
  5985. msglen_c = ctx->c_msglen;
  5986. }
  5987. if (ctx->s_msg != NULL) {
  5988. msg_s = ctx->s_msg;
  5989. msglen_s = ctx->s_msglen;
  5990. }
  5991. wolfSSL_SetLoggingPrefix("client");
  5992. ExpectIntEQ(wolfSSL_write(ctx->c_ssl, msg_c, msglen_c), msglen_c);
  5993. wolfSSL_SetLoggingPrefix("server");
  5994. ExpectIntGT(idx = wolfSSL_read(ctx->s_ssl, input, sizeof(input) - 1), 0);
  5995. if (idx >= 0) {
  5996. input[idx] = '\0';
  5997. }
  5998. ExpectIntGT(fprintf(stderr, "Client message: %s\n", input), 0);
  5999. ExpectIntEQ(wolfSSL_write(ctx->s_ssl, msg_s, msglen_s), msglen_s);
  6000. ctx->s_cb.return_code = EXPECT_RESULT();
  6001. wolfSSL_SetLoggingPrefix("client");
  6002. ExpectIntGT(idx = wolfSSL_read(ctx->c_ssl, input, sizeof(input) - 1), 0);
  6003. wolfSSL_SetLoggingPrefix(NULL);
  6004. if (idx >= 0) {
  6005. input[idx] = '\0';
  6006. }
  6007. ExpectIntGT(fprintf(stderr, "Server response: %s\n", input), 0);
  6008. ctx->c_cb.return_code = EXPECT_RESULT();
  6009. if (ctx->c_cb.on_result != NULL) {
  6010. ExpectIntEQ(ctx->c_cb.on_result(ctx->c_ssl), TEST_SUCCESS);
  6011. }
  6012. if (ctx->s_cb.on_result != NULL) {
  6013. ExpectIntEQ(ctx->s_cb.on_result(ctx->s_ssl), TEST_SUCCESS);
  6014. }
  6015. return EXPECT_RESULT();
  6016. }
  6017. static void test_ssl_memio_cleanup(test_ssl_memio_ctx* ctx)
  6018. {
  6019. ctx->c_cb.last_err = wolfSSL_get_error(ctx->c_ssl, 0);
  6020. ctx->s_cb.last_err = wolfSSL_get_error(ctx->s_ssl, 0);
  6021. if (ctx->c_cb.on_cleanup != NULL) {
  6022. ctx->c_cb.on_cleanup(ctx->c_ssl);
  6023. }
  6024. if (ctx->s_cb.on_cleanup != NULL) {
  6025. ctx->s_cb.on_cleanup(ctx->s_ssl);
  6026. }
  6027. wolfSSL_shutdown(ctx->s_ssl);
  6028. wolfSSL_shutdown(ctx->c_ssl);
  6029. wolfSSL_free(ctx->s_ssl);
  6030. wolfSSL_free(ctx->c_ssl);
  6031. if (ctx->c_cb.on_ctx_cleanup != NULL) {
  6032. ctx->c_cb.on_ctx_cleanup(ctx->c_ctx);
  6033. }
  6034. if (!ctx->c_cb.isSharedCtx) {
  6035. wolfSSL_CTX_free(ctx->c_ctx);
  6036. ctx->c_ctx = NULL;
  6037. }
  6038. if (ctx->s_cb.on_ctx_cleanup != NULL) {
  6039. ctx->s_cb.on_ctx_cleanup(ctx->s_ctx);
  6040. }
  6041. if (!ctx->s_cb.isSharedCtx) {
  6042. wolfSSL_CTX_free(ctx->s_ctx);
  6043. ctx->s_ctx = NULL;
  6044. }
  6045. if (!ctx->s_cb.ticNoInit) {
  6046. #if defined(HAVE_SESSION_TICKET) && \
  6047. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6048. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6049. OpenSSLTicketCleanup();
  6050. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6051. TicketCleanup();
  6052. #endif
  6053. #endif
  6054. }
  6055. }
  6056. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  6057. test_ssl_cbf* server_cb, cbType client_on_handshake)
  6058. {
  6059. EXPECT_DECLS;
  6060. struct test_ssl_memio_ctx test_ctx;
  6061. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6062. size_t msg_len;
  6063. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6064. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  6065. XMEMCPY(&test_ctx.c_cb, client_cb, sizeof(test_ssl_cbf));
  6066. XMEMCPY(&test_ctx.s_cb, server_cb, sizeof(test_ssl_cbf));
  6067. test_ctx.c_ctx = client_cb->ctx;
  6068. test_ctx.s_ctx = server_cb->ctx;
  6069. test_ctx.c_cb.return_code = TEST_FAIL;
  6070. test_ctx.s_cb.return_code = TEST_FAIL;
  6071. ExpectIntEQ(test_ssl_memio_setup(&test_ctx), TEST_SUCCESS);
  6072. ExpectIntEQ(test_ssl_memio_do_handshake(&test_ctx, 10, NULL), TEST_SUCCESS);
  6073. if (client_on_handshake != NULL) {
  6074. ExpectIntEQ(client_on_handshake(test_ctx.c_ctx, test_ctx.c_ssl),
  6075. TEST_SUCCESS);
  6076. }
  6077. if (client_cb->on_handshake != NULL) {
  6078. ExpectIntEQ(client_cb->on_handshake(&test_ctx.c_ctx, &test_ctx.c_ssl),
  6079. TEST_SUCCESS);
  6080. }
  6081. if (server_cb->on_handshake != NULL) {
  6082. ExpectIntEQ(server_cb->on_handshake(&test_ctx.s_ctx, &test_ctx.s_ssl),
  6083. TEST_SUCCESS);
  6084. }
  6085. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6086. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  6087. msg_len = wolfSSL_get_peer_finished(test_ctx.s_ssl, server_side_msg2,
  6088. MD_MAX_SIZE);
  6089. ExpectIntGE(msg_len, 0);
  6090. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  6091. msg_len = wolfSSL_get_finished(test_ctx.s_ssl, server_side_msg1,
  6092. MD_MAX_SIZE);
  6093. ExpectIntGE(msg_len, 0);
  6094. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6095. ExpectIntEQ(test_ssl_memio_read_write(&test_ctx), TEST_SUCCESS);
  6096. test_ssl_memio_cleanup(&test_ctx);
  6097. client_cb->return_code = test_ctx.c_cb.return_code;
  6098. client_cb->last_err = test_ctx.c_cb.last_err;
  6099. server_cb->return_code = test_ctx.s_cb.return_code;
  6100. server_cb->last_err = test_ctx.s_cb.last_err;
  6101. return EXPECT_RESULT();
  6102. }
  6103. #endif
  6104. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  6105. #ifdef WOLFSSL_SESSION_EXPORT
  6106. #ifdef WOLFSSL_DTLS
  6107. /* set up function for sending session information */
  6108. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  6109. {
  6110. WOLFSSL_CTX* ctx = NULL;
  6111. WOLFSSL* ssl = NULL;
  6112. AssertNotNull(inSsl);
  6113. AssertNotNull(buf);
  6114. AssertIntNE(0, sz);
  6115. /* Set ctx to DTLS 1.2 */
  6116. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  6117. AssertNotNull(ctx);
  6118. ssl = wolfSSL_new(ctx);
  6119. AssertNotNull(ssl);
  6120. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  6121. wolfSSL_free(ssl);
  6122. wolfSSL_CTX_free(ctx);
  6123. (void)userCtx;
  6124. return 0;
  6125. }
  6126. #endif
  6127. /* returns negative value on fail and positive (including 0) on success */
  6128. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  6129. {
  6130. int ret, err, loop_count, count, timeout = 10;
  6131. char msg[] = "I hear you fa shizzle!";
  6132. char input[1024];
  6133. loop_count = ((func_args*)args)->argc;
  6134. #ifdef WOLFSSL_ASYNC_CRYPT
  6135. err = 0; /* Reset error */
  6136. #endif
  6137. do {
  6138. #ifdef WOLFSSL_ASYNC_CRYPT
  6139. if (err == WC_PENDING_E) {
  6140. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6141. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6142. }
  6143. #endif
  6144. ret = wolfSSL_accept(ssl);
  6145. err = wolfSSL_get_error(ssl, 0);
  6146. if (err == WOLFSSL_ERROR_WANT_READ ||
  6147. err == WOLFSSL_ERROR_WANT_WRITE) {
  6148. int select_ret;
  6149. err = WC_PENDING_E;
  6150. select_ret = tcp_select(*sockfd, timeout);
  6151. if (select_ret == TEST_TIMEOUT) {
  6152. return WOLFSSL_FATAL_ERROR;
  6153. }
  6154. }
  6155. } while (err == WC_PENDING_E);
  6156. if (ret != WOLFSSL_SUCCESS) {
  6157. char buff[WOLFSSL_MAX_ERROR_SZ];
  6158. fprintf(stderr, "error = %d, %s\n", err,
  6159. wolfSSL_ERR_error_string(err, buff));
  6160. return ret;
  6161. }
  6162. for (count = 0; count < loop_count; count++) {
  6163. int select_ret;
  6164. select_ret = tcp_select(*sockfd, timeout);
  6165. if (select_ret == TEST_TIMEOUT) {
  6166. ret = WOLFSSL_FATAL_ERROR;
  6167. break;
  6168. }
  6169. do {
  6170. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  6171. if (ret > 0) {
  6172. input[ret] = '\0';
  6173. fprintf(stderr, "Client message: %s\n", input);
  6174. }
  6175. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  6176. do {
  6177. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  6178. return WOLFSSL_FATAL_ERROR;
  6179. }
  6180. err = wolfSSL_get_error(ssl, ret);
  6181. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  6182. }
  6183. return ret;
  6184. }
  6185. #endif /* WOLFSSL_SESSION_EXPORT */
  6186. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  6187. {
  6188. SOCKET_T sockfd = 0;
  6189. SOCKET_T clientfd = 0;
  6190. word16 port;
  6191. callback_functions* cbf;
  6192. WOLFSSL_CTX* ctx = NULL;
  6193. WOLFSSL* ssl = NULL;
  6194. func_args* opts = (func_args*)args;
  6195. char msg[] = "I hear you fa shizzle!";
  6196. char input[1024];
  6197. int idx;
  6198. int ret, err = 0;
  6199. int sharedCtx = 0;
  6200. int doUdp = 0;
  6201. SOCKADDR_IN_T cliAddr;
  6202. socklen_t cliLen;
  6203. const char* certFile = svrCertFile;
  6204. const char* keyFile = svrKeyFile;
  6205. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6206. size_t msg_len = 0;
  6207. #endif
  6208. wolfSSL_SetLoggingPrefix("server");
  6209. #ifdef WOLFSSL_TIRTOS
  6210. fdOpenSession(Task_self());
  6211. #endif
  6212. opts->return_code = TEST_FAIL;
  6213. cbf = opts->callbacks;
  6214. if (cbf != NULL && cbf->ctx) {
  6215. ctx = cbf->ctx;
  6216. sharedCtx = 1;
  6217. }
  6218. else
  6219. {
  6220. WOLFSSL_METHOD* method = NULL;
  6221. if (cbf != NULL && cbf->method != NULL) {
  6222. method = cbf->method();
  6223. }
  6224. else {
  6225. method = wolfSSLv23_server_method();
  6226. }
  6227. ctx = wolfSSL_CTX_new(method);
  6228. }
  6229. if (ctx == NULL) {
  6230. /* Release the wait for TCP ready. */
  6231. signal_ready(opts->signal);
  6232. goto done;
  6233. }
  6234. if (cbf == NULL || !cbf->ticNoInit) {
  6235. #if defined(HAVE_SESSION_TICKET) && \
  6236. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6237. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6238. OpenSSLTicketInit();
  6239. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  6240. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6241. TicketInit();
  6242. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  6243. #endif
  6244. #endif
  6245. }
  6246. #if defined(USE_WINDOWS_API)
  6247. port = opts->signal->port;
  6248. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6249. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6250. /* Let tcp_listen assign port */
  6251. port = 0;
  6252. #else
  6253. /* Use default port */
  6254. port = wolfSSLPort;
  6255. #endif
  6256. if (cbf != NULL)
  6257. doUdp = cbf->doUdp;
  6258. /* do it here to detect failure */
  6259. tcp_accept(
  6260. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  6261. if (doUdp) {
  6262. cliLen = sizeof(cliAddr);
  6263. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  6264. (struct sockaddr*)&cliAddr, &cliLen);
  6265. AssertIntGT(idx, 0);
  6266. }
  6267. else {
  6268. CloseSocket(sockfd);
  6269. }
  6270. wolfSSL_CTX_set_verify(ctx,
  6271. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6272. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6273. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6274. #endif
  6275. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  6276. != WOLFSSL_SUCCESS) {
  6277. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6278. goto done;
  6279. }
  6280. if (cbf != NULL && cbf->certPemFile != NULL)
  6281. certFile = cbf->certPemFile;
  6282. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6283. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, certFile,
  6284. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6285. #else
  6286. if (wolfSSL_CTX_use_certificate_file(ctx, certFile,
  6287. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6288. #endif
  6289. /*err_sys("can't load server cert chain file, "
  6290. "Please run from wolfSSL home dir");*/
  6291. goto done;
  6292. }
  6293. if (cbf != NULL && cbf->keyPemFile != NULL)
  6294. keyFile = cbf->keyPemFile;
  6295. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6296. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  6297. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6298. #else
  6299. if (wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  6300. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6301. #endif
  6302. /*err_sys("can't load server key file, "
  6303. "Please run from wolfSSL home dir");*/
  6304. goto done;
  6305. }
  6306. #ifdef HAVE_CRL
  6307. if (cbf != NULL && cbf->crlPemFile != NULL) {
  6308. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  6309. goto done;
  6310. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  6311. != WOLFSSL_SUCCESS)
  6312. goto done;
  6313. }
  6314. #endif
  6315. /* call ctx setup callback */
  6316. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6317. cbf->ctx_ready(ctx);
  6318. }
  6319. ssl = wolfSSL_new(ctx);
  6320. if (ssl == NULL) {
  6321. goto done;
  6322. }
  6323. if (doUdp) {
  6324. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  6325. if (err != WOLFSSL_SUCCESS)
  6326. goto done;
  6327. }
  6328. #ifdef WOLFSSL_SESSION_EXPORT
  6329. /* only add in more complex nonblocking case with session export tests */
  6330. if (args && opts->argc > 0) {
  6331. /* set as nonblock and time out for waiting on read/write */
  6332. tcp_set_nonblocking(&clientfd);
  6333. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  6334. }
  6335. #endif
  6336. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6337. if (sharedCtx && wolfSSL_use_certificate_file(ssl, certFile,
  6338. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6339. #else
  6340. if (wolfSSL_use_certificate_file(ssl, certFile,
  6341. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6342. #endif
  6343. /*err_sys("can't load server cert chain file, "
  6344. "Please run from wolfSSL home dir");*/
  6345. goto done;
  6346. }
  6347. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6348. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, keyFile,
  6349. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6350. #else
  6351. if (wolfSSL_use_PrivateKey_file(ssl, keyFile,
  6352. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6353. #endif
  6354. /*err_sys("can't load server key file, "
  6355. "Please run from wolfSSL home dir");*/
  6356. goto done;
  6357. }
  6358. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  6359. /*err_sys("SSL_set_fd failed");*/
  6360. goto done;
  6361. }
  6362. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6363. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6364. #elif !defined(NO_DH)
  6365. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6366. #endif
  6367. /* call ssl setup callback */
  6368. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6369. cbf->ssl_ready(ssl);
  6370. }
  6371. #ifdef WOLFSSL_SESSION_EXPORT
  6372. /* only add in more complex nonblocking case with session export tests */
  6373. if (opts->argc > 0) {
  6374. ret = nonblocking_accept_read(args, ssl, &clientfd);
  6375. if (ret >= 0) {
  6376. opts->return_code = TEST_SUCCESS;
  6377. }
  6378. #ifdef WOLFSSL_TIRTOS
  6379. Task_yield();
  6380. #endif
  6381. goto done;
  6382. }
  6383. #endif
  6384. #ifdef WOLFSSL_ASYNC_CRYPT
  6385. err = 0; /* Reset error */
  6386. #endif
  6387. do {
  6388. #ifdef WOLFSSL_ASYNC_CRYPT
  6389. if (err == WC_PENDING_E) {
  6390. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6391. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6392. }
  6393. #endif
  6394. ret = wolfSSL_negotiate(ssl);
  6395. err = wolfSSL_get_error(ssl, 0);
  6396. } while (err == WC_PENDING_E);
  6397. if (ret != WOLFSSL_SUCCESS) {
  6398. char buff[WOLFSSL_MAX_ERROR_SZ];
  6399. fprintf(stderr, "error = %d, %s\n", err,
  6400. wolfSSL_ERR_error_string(err, buff));
  6401. /*err_sys("SSL_accept failed");*/
  6402. goto done;
  6403. }
  6404. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6405. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  6406. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  6407. AssertIntGE(msg_len, 0);
  6408. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  6409. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  6410. AssertIntGE(msg_len, 0);
  6411. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6412. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6413. if (idx > 0) {
  6414. input[idx] = '\0';
  6415. fprintf(stderr, "Client message: %s\n", input);
  6416. }
  6417. else if (idx < 0) {
  6418. goto done;
  6419. }
  6420. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6421. /*err_sys("SSL_write failed");*/
  6422. goto done;
  6423. }
  6424. if (cbf != NULL && cbf->on_result != NULL)
  6425. cbf->on_result(ssl);
  6426. #ifdef WOLFSSL_TIRTOS
  6427. Task_yield();
  6428. #endif
  6429. opts->return_code = TEST_SUCCESS;
  6430. done:
  6431. if (cbf != NULL)
  6432. cbf->last_err = err;
  6433. if (cbf != NULL && cbf->on_cleanup != NULL)
  6434. cbf->on_cleanup(ssl);
  6435. wolfSSL_shutdown(ssl);
  6436. wolfSSL_free(ssl);
  6437. if (!sharedCtx)
  6438. wolfSSL_CTX_free(ctx);
  6439. CloseSocket(clientfd);
  6440. #ifdef WOLFSSL_TIRTOS
  6441. fdCloseSession(Task_self());
  6442. #endif
  6443. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6444. && defined(HAVE_THREAD_LS)
  6445. wc_ecc_fp_free(); /* free per thread cache */
  6446. #endif
  6447. if (cbf == NULL || !cbf->ticNoInit) {
  6448. #if defined(HAVE_SESSION_TICKET) && \
  6449. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6450. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6451. OpenSSLTicketCleanup();
  6452. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6453. TicketCleanup();
  6454. #endif
  6455. #endif
  6456. }
  6457. wolfSSL_SetLoggingPrefix(NULL);
  6458. WOLFSSL_RETURN_FROM_THREAD(0);
  6459. }
  6460. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6461. !defined(WOLFSSL_NO_TLS12)
  6462. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  6463. {
  6464. SOCKET_T sockfd;
  6465. SOCKET_T clientfd = -1;
  6466. word16 port;
  6467. callback_functions* cbf;
  6468. WOLFSSL_CTX* ctx = 0;
  6469. WOLFSSL* ssl = 0;
  6470. char msg[] = "I hear you fa shizzle!";
  6471. char input[1024];
  6472. int idx;
  6473. int ret, err = 0;
  6474. int sharedCtx = 0;
  6475. func_args* opts = (func_args*)args;
  6476. int loop_count = opts->argc;
  6477. int count = 0;
  6478. #ifdef WOLFSSL_TIRTOS
  6479. fdOpenSession(Task_self());
  6480. #endif
  6481. opts->return_code = TEST_FAIL;
  6482. cbf = opts->callbacks;
  6483. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6484. if (cbf != NULL && cbf->ctx) {
  6485. ctx = cbf->ctx;
  6486. sharedCtx = 1;
  6487. }
  6488. else
  6489. #endif
  6490. {
  6491. WOLFSSL_METHOD* method = NULL;
  6492. if (cbf != NULL && cbf->method != NULL) {
  6493. method = cbf->method();
  6494. }
  6495. else {
  6496. method = wolfSSLv23_server_method();
  6497. }
  6498. ctx = wolfSSL_CTX_new(method);
  6499. }
  6500. #if defined(USE_WINDOWS_API)
  6501. port = opts->signal->port;
  6502. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6503. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6504. /* Let tcp_listen assign port */
  6505. port = 0;
  6506. #else
  6507. /* Use default port */
  6508. port = wolfSSLPort;
  6509. #endif
  6510. wolfSSL_CTX_set_verify(ctx,
  6511. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6512. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6513. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6514. #endif
  6515. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  6516. != WOLFSSL_SUCCESS) {
  6517. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6518. /* Release the wait for TCP ready. */
  6519. signal_ready(opts->signal);
  6520. goto done;
  6521. }
  6522. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  6523. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6524. /*err_sys("can't load server cert chain file, "
  6525. "Please run from wolfSSL home dir");*/
  6526. /* Release the wait for TCP ready. */
  6527. signal_ready(opts->signal);
  6528. goto done;
  6529. }
  6530. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  6531. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6532. /*err_sys("can't load server key file, "
  6533. "Please run from wolfSSL home dir");*/
  6534. /* Release the wait for TCP ready. */
  6535. signal_ready(opts->signal);
  6536. goto done;
  6537. }
  6538. /* call ctx setup callback */
  6539. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6540. cbf->ctx_ready(ctx);
  6541. }
  6542. while (count != loop_count) {
  6543. ssl = wolfSSL_new(ctx);
  6544. if (ssl == NULL) {
  6545. signal_ready(opts->signal);
  6546. goto done;
  6547. }
  6548. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  6549. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6550. /*err_sys("can't load server cert chain file, "
  6551. "Please run from wolfSSL home dir");*/
  6552. /* Release the wait for TCP ready. */
  6553. signal_ready(opts->signal);
  6554. goto done;
  6555. }
  6556. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  6557. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6558. /*err_sys("can't load server key file, "
  6559. "Please run from wolfSSL home dir");*/
  6560. /* Release the wait for TCP ready. */
  6561. signal_ready(opts->signal);
  6562. goto done;
  6563. }
  6564. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6565. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6566. #elif !defined(NO_DH)
  6567. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6568. #endif
  6569. /* call ssl setup callback */
  6570. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6571. cbf->ssl_ready(ssl);
  6572. }
  6573. /* do it here to detect failure */
  6574. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0,
  6575. 0);
  6576. CloseSocket(sockfd);
  6577. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  6578. /*err_sys("SSL_set_fd failed");*/
  6579. goto done;
  6580. }
  6581. #ifdef WOLFSSL_ASYNC_CRYPT
  6582. err = 0; /* Reset error */
  6583. #endif
  6584. do {
  6585. #ifdef WOLFSSL_ASYNC_CRYPT
  6586. if (err == WC_PENDING_E) {
  6587. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6588. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6589. }
  6590. #endif
  6591. ret = wolfSSL_accept(ssl);
  6592. err = wolfSSL_get_error(ssl, 0);
  6593. } while (err == WC_PENDING_E);
  6594. if (ret != WOLFSSL_SUCCESS) {
  6595. char buff[WOLFSSL_MAX_ERROR_SZ];
  6596. fprintf(stderr, "error = %d, %s\n", err,
  6597. wolfSSL_ERR_error_string(err, buff));
  6598. /*err_sys("SSL_accept failed");*/
  6599. goto done;
  6600. }
  6601. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6602. if (idx > 0) {
  6603. input[idx] = '\0';
  6604. fprintf(stderr, "Client message: %s\n", input);
  6605. }
  6606. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6607. /*err_sys("SSL_write failed");*/
  6608. goto done;
  6609. }
  6610. /* free ssl for this connection */
  6611. wolfSSL_shutdown(ssl);
  6612. wolfSSL_free(ssl); ssl = NULL;
  6613. CloseSocket(clientfd);
  6614. clientfd = -1;
  6615. count++;
  6616. }
  6617. #ifdef WOLFSSL_TIRTOS
  6618. Task_yield();
  6619. #endif
  6620. opts->return_code = TEST_SUCCESS;
  6621. done:
  6622. if (ssl != NULL) {
  6623. wolfSSL_shutdown(ssl);
  6624. wolfSSL_free(ssl);
  6625. }
  6626. if (!sharedCtx)
  6627. wolfSSL_CTX_free(ctx);
  6628. if (clientfd >= 0)
  6629. CloseSocket(clientfd);
  6630. #ifdef WOLFSSL_TIRTOS
  6631. fdCloseSession(Task_self());
  6632. #endif
  6633. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6634. && defined(HAVE_THREAD_LS)
  6635. wc_ecc_fp_free(); /* free per thread cache */
  6636. #endif
  6637. WOLFSSL_RETURN_FROM_THREAD(0);
  6638. }
  6639. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  6640. static int test_client_nofail(void* args, cbType cb)
  6641. {
  6642. #if !defined(NO_WOLFSSL_CLIENT)
  6643. SOCKET_T sockfd = 0;
  6644. callback_functions* cbf;
  6645. WOLFSSL_CTX* ctx = 0;
  6646. WOLFSSL* ssl = 0;
  6647. WOLFSSL_CIPHER* cipher;
  6648. char msg[64] = "hello wolfssl!";
  6649. char reply[1024];
  6650. int input;
  6651. int msgSz = (int)XSTRLEN(msg);
  6652. int ret, err = 0;
  6653. int cipherSuite;
  6654. int sharedCtx = 0;
  6655. int doUdp = 0;
  6656. const char* cipherName1, *cipherName2;
  6657. wolfSSL_SetLoggingPrefix("client");
  6658. #ifdef WOLFSSL_TIRTOS
  6659. fdOpenSession(Task_self());
  6660. #endif
  6661. ((func_args*)args)->return_code = TEST_FAIL;
  6662. cbf = ((func_args*)args)->callbacks;
  6663. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6664. if (cbf != NULL && cbf->ctx) {
  6665. ctx = cbf->ctx;
  6666. sharedCtx = cbf->isSharedCtx;
  6667. }
  6668. else
  6669. #endif
  6670. {
  6671. WOLFSSL_METHOD* method = NULL;
  6672. if (cbf != NULL && cbf->method != NULL) {
  6673. method = cbf->method();
  6674. }
  6675. else {
  6676. method = wolfSSLv23_client_method();
  6677. }
  6678. ctx = wolfSSL_CTX_new(method);
  6679. }
  6680. if (cbf != NULL)
  6681. doUdp = cbf->doUdp;
  6682. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6683. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6684. #endif
  6685. /* Do connect here so server detects failures */
  6686. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6687. doUdp, 0, NULL);
  6688. /* Connect the socket so that we don't have to set the peer later on */
  6689. if (doUdp)
  6690. udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
  6691. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  6692. {
  6693. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6694. goto done;
  6695. }
  6696. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6697. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6698. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6699. #else
  6700. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6701. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6702. #endif
  6703. /*err_sys("can't load client cert file, "
  6704. "Please run from wolfSSL home dir");*/
  6705. goto done;
  6706. }
  6707. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6708. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6709. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6710. #else
  6711. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6712. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6713. #endif
  6714. /*err_sys("can't load client key file, "
  6715. "Please run from wolfSSL home dir");*/
  6716. goto done;
  6717. }
  6718. #ifdef HAVE_CRL
  6719. if (cbf != NULL && cbf->crlPemFile != NULL) {
  6720. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  6721. goto done;
  6722. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  6723. != WOLFSSL_SUCCESS)
  6724. goto done;
  6725. }
  6726. #endif
  6727. /* call ctx setup callback */
  6728. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6729. cbf->ctx_ready(ctx);
  6730. }
  6731. ssl = wolfSSL_new(ctx);
  6732. if (ssl == NULL) {
  6733. goto done;
  6734. }
  6735. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6736. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  6737. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6738. #else
  6739. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  6740. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6741. #endif
  6742. /*err_sys("can't load client cert file, "
  6743. "Please run from wolfSSL home dir");*/
  6744. goto done;
  6745. }
  6746. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6747. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6748. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6749. #else
  6750. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6751. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6752. #endif
  6753. /*err_sys("can't load client key file, "
  6754. "Please run from wolfSSL home dir");*/
  6755. goto done;
  6756. }
  6757. if (!doUdp) {
  6758. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6759. /*err_sys("SSL_set_fd failed");*/
  6760. goto done;
  6761. }
  6762. }
  6763. else {
  6764. #ifdef WOLFSSL_DTLS
  6765. if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6766. /*err_sys("SSL_set_fd failed");*/
  6767. goto done;
  6768. }
  6769. #else
  6770. goto done;
  6771. #endif
  6772. }
  6773. /* call ssl setup callback */
  6774. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6775. cbf->ssl_ready(ssl);
  6776. }
  6777. #ifdef WOLFSSL_ASYNC_CRYPT
  6778. err = 0; /* Reset error */
  6779. #endif
  6780. do {
  6781. #ifdef WOLFSSL_ASYNC_CRYPT
  6782. if (err == WC_PENDING_E) {
  6783. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6784. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6785. }
  6786. #endif
  6787. ret = wolfSSL_negotiate(ssl);
  6788. err = wolfSSL_get_error(ssl, 0);
  6789. } while (err == WC_PENDING_E);
  6790. if (ret != WOLFSSL_SUCCESS) {
  6791. char buff[WOLFSSL_MAX_ERROR_SZ];
  6792. fprintf(stderr, "error = %d, %s\n", err,
  6793. wolfSSL_ERR_error_string(err, buff));
  6794. /*err_sys("SSL_connect failed");*/
  6795. goto done;
  6796. }
  6797. /* test the various get cipher methods */
  6798. /* Internal cipher suite names */
  6799. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  6800. cipherName1 = wolfSSL_get_cipher_name(ssl);
  6801. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  6802. (cipherSuite >> 8), cipherSuite & 0xFF);
  6803. AssertStrEQ(cipherName1, cipherName2);
  6804. /* IANA Cipher Suites Names */
  6805. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  6806. then it's the internal cipher suite name */
  6807. cipher = wolfSSL_get_current_cipher(ssl);
  6808. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  6809. cipherName2 = wolfSSL_get_cipher(ssl);
  6810. AssertStrEQ(cipherName1, cipherName2);
  6811. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  6812. !defined(WOLFSSL_QT)
  6813. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  6814. (cipherSuite >> 8), cipherSuite & 0xFF);
  6815. AssertStrEQ(cipherName1, cipherName2);
  6816. #endif
  6817. if (cb != NULL)
  6818. (cb)(ctx, ssl);
  6819. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6820. /*err_sys("SSL_write failed");*/
  6821. goto done;
  6822. }
  6823. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6824. if (input > 0) {
  6825. reply[input] = '\0';
  6826. fprintf(stderr, "Server response: %s\n", reply);
  6827. }
  6828. if (cbf != NULL && cbf->on_result != NULL)
  6829. cbf->on_result(ssl);
  6830. ((func_args*)args)->return_code = TEST_SUCCESS;
  6831. done:
  6832. if (cbf != NULL)
  6833. cbf->last_err = err;
  6834. if (cbf != NULL && cbf->on_cleanup != NULL)
  6835. cbf->on_cleanup(ssl);
  6836. wolfSSL_free(ssl);
  6837. if (!sharedCtx)
  6838. wolfSSL_CTX_free(ctx);
  6839. CloseSocket(sockfd);
  6840. #ifdef WOLFSSL_TIRTOS
  6841. fdCloseSession(Task_self());
  6842. #endif
  6843. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6844. && defined(HAVE_THREAD_LS)
  6845. wc_ecc_fp_free(); /* free per thread cache */
  6846. #endif
  6847. #else
  6848. (void)args;
  6849. (void)cb;
  6850. #endif /* !NO_WOLFSSL_CLIENT */
  6851. wolfSSL_SetLoggingPrefix(NULL);
  6852. return 0;
  6853. }
  6854. void test_wolfSSL_client_server_nofail_ex(callback_functions* client_cb,
  6855. callback_functions* server_cb, cbType client_on_handshake)
  6856. {
  6857. func_args client_args;
  6858. func_args server_args;
  6859. tcp_ready ready;
  6860. THREAD_TYPE serverThread;
  6861. XMEMSET(&client_args, 0, sizeof(func_args));
  6862. XMEMSET(&server_args, 0, sizeof(func_args));
  6863. #ifdef WOLFSSL_TIRTOS
  6864. fdOpenSession(Task_self());
  6865. #endif
  6866. StartTCP();
  6867. InitTcpReady(&ready);
  6868. #if defined(USE_WINDOWS_API)
  6869. /* use RNG to get random port if using windows */
  6870. ready.port = GetRandomPort();
  6871. #endif
  6872. server_args.signal = &ready;
  6873. server_args.callbacks = server_cb;
  6874. client_args.signal = &ready;
  6875. client_args.callbacks = client_cb;
  6876. start_thread(test_server_nofail, &server_args, &serverThread);
  6877. wait_tcp_ready(&server_args);
  6878. test_client_nofail(&client_args, client_on_handshake);
  6879. join_thread(serverThread);
  6880. client_cb->return_code = client_args.return_code;
  6881. server_cb->return_code = server_args.return_code;
  6882. FreeTcpReady(&ready);
  6883. #ifdef WOLFSSL_TIRTOS
  6884. fdOpenSession(Task_self());
  6885. #endif
  6886. }
  6887. void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
  6888. callback_functions* server_cb)
  6889. {
  6890. test_wolfSSL_client_server_nofail_ex(client_cb, server_cb, NULL);
  6891. }
  6892. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6893. !defined(WOLFSSL_NO_TLS12) && !defined(NO_WOLFSSL_CLIENT)
  6894. static void test_client_reuse_WOLFSSLobj(void* args, cbType cb,
  6895. void* server_args)
  6896. {
  6897. SOCKET_T sockfd = 0;
  6898. callback_functions* cbf;
  6899. WOLFSSL_CTX* ctx = 0;
  6900. WOLFSSL* ssl = 0;
  6901. WOLFSSL_SESSION* session = NULL;
  6902. char msg[64] = "hello wolfssl!";
  6903. char reply[1024];
  6904. int input;
  6905. int msgSz = (int)XSTRLEN(msg);
  6906. int ret, err = 0;
  6907. int sharedCtx = 0;
  6908. #ifdef WOLFSSL_TIRTOS
  6909. fdOpenSession(Task_self());
  6910. #endif
  6911. ((func_args*)args)->return_code = TEST_FAIL;
  6912. cbf = ((func_args*)args)->callbacks;
  6913. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6914. if (cbf != NULL && cbf->ctx) {
  6915. ctx = cbf->ctx;
  6916. sharedCtx = 1;
  6917. }
  6918. else
  6919. #endif
  6920. {
  6921. WOLFSSL_METHOD* method = NULL;
  6922. if (cbf != NULL && cbf->method != NULL) {
  6923. method = cbf->method();
  6924. }
  6925. else {
  6926. method = wolfSSLv23_client_method();
  6927. }
  6928. ctx = wolfSSL_CTX_new(method);
  6929. }
  6930. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6931. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6932. #endif
  6933. /* Do connect here so server detects failures */
  6934. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6935. 0, 0, NULL);
  6936. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) !=
  6937. WOLFSSL_SUCCESS) {
  6938. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6939. goto done;
  6940. }
  6941. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6942. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6943. /*err_sys("can't load client cert file, "
  6944. "Please run from wolfSSL home dir");*/
  6945. goto done;
  6946. }
  6947. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6948. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6949. /*err_sys("can't load client key file, "
  6950. "Please run from wolfSSL home dir");*/
  6951. goto done;
  6952. }
  6953. /* call ctx setup callback */
  6954. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6955. cbf->ctx_ready(ctx);
  6956. }
  6957. ssl = wolfSSL_new(ctx);
  6958. if (ssl == NULL) {
  6959. goto done;
  6960. }
  6961. /* keep handshake resources for re-using WOLFSSL obj */
  6962. wolfSSL_KeepArrays(ssl);
  6963. if (wolfSSL_KeepHandshakeResources(ssl)) {
  6964. /* err_sys("SSL_KeepHandshakeResources failed"); */
  6965. goto done;
  6966. }
  6967. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  6968. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6969. /*err_sys("can't load client cert file, "
  6970. "Please run from wolfSSL home dir");*/
  6971. goto done;
  6972. }
  6973. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6974. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6975. /*err_sys("can't load client key file, "
  6976. "Please run from wolfSSL home dir");*/
  6977. goto done;
  6978. }
  6979. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6980. /*err_sys("SSL_set_fd failed");*/
  6981. goto done;
  6982. }
  6983. /* call ssl setup callback */
  6984. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6985. cbf->ssl_ready(ssl);
  6986. }
  6987. #ifdef WOLFSSL_ASYNC_CRYPT
  6988. err = 0; /* Reset error */
  6989. #endif
  6990. do {
  6991. #ifdef WOLFSSL_ASYNC_CRYPT
  6992. if (err == WC_PENDING_E) {
  6993. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6994. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6995. }
  6996. #endif
  6997. ret = wolfSSL_connect(ssl);
  6998. err = wolfSSL_get_error(ssl, 0);
  6999. } while (err == WC_PENDING_E);
  7000. if (ret != WOLFSSL_SUCCESS) {
  7001. char buff[WOLFSSL_MAX_ERROR_SZ];
  7002. fprintf(stderr, "error = %d, %s\n", err,
  7003. wolfSSL_ERR_error_string(err, buff));
  7004. /*err_sys("SSL_connect failed");*/
  7005. goto done;
  7006. }
  7007. /* Build first session */
  7008. if (cb != NULL)
  7009. cb(ctx, ssl);
  7010. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  7011. /*err_sys("SSL_write failed");*/
  7012. goto done;
  7013. }
  7014. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  7015. if (input > 0) {
  7016. reply[input] = '\0';
  7017. fprintf(stderr, "Server response: %s\n", reply);
  7018. }
  7019. /* Session Resumption by re-using WOLFSSL object */
  7020. wolfSSL_set_quiet_shutdown(ssl, 1);
  7021. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  7022. /* err_sys ("SSL shutdown failed"); */
  7023. goto done;
  7024. }
  7025. session = wolfSSL_get1_session(ssl);
  7026. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  7027. wolfSSL_SESSION_free(session);
  7028. /* err_sys ("SSL_clear failed"); */
  7029. goto done;
  7030. }
  7031. wolfSSL_set_session(ssl, session);
  7032. wolfSSL_SESSION_free(session);
  7033. session = NULL;
  7034. /* close socket once */
  7035. CloseSocket(sockfd);
  7036. sockfd = 0;
  7037. /* wait until server ready */
  7038. wait_tcp_ready((func_args*)server_args);
  7039. fprintf(stderr, "session resumption\n");
  7040. /* Do re-connect */
  7041. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7042. 0, 0, NULL);
  7043. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  7044. /*err_sys("SSL_set_fd failed");*/
  7045. goto done;
  7046. }
  7047. #ifdef WOLFSSL_ASYNC_CRYPT
  7048. err = 0; /* Reset error */
  7049. #endif
  7050. do {
  7051. #ifdef WOLFSSL_ASYNC_CRYPT
  7052. if (err == WC_PENDING_E) {
  7053. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7054. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7055. }
  7056. #endif
  7057. ret = wolfSSL_connect(ssl);
  7058. err = wolfSSL_get_error(ssl, 0);
  7059. } while (err == WC_PENDING_E);
  7060. if (ret != WOLFSSL_SUCCESS) {
  7061. char buff[WOLFSSL_MAX_ERROR_SZ];
  7062. fprintf(stderr, "error = %d, %s\n", err,
  7063. wolfSSL_ERR_error_string(err, buff));
  7064. /*err_sys("SSL_connect failed");*/
  7065. goto done;
  7066. }
  7067. /* Build first session */
  7068. if (cb != NULL)
  7069. cb(ctx, ssl);
  7070. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  7071. /*err_sys("SSL_write failed");*/
  7072. goto done;
  7073. }
  7074. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  7075. if (input > 0) {
  7076. reply[input] = '\0';
  7077. fprintf(stderr, "Server response: %s\n", reply);
  7078. }
  7079. ((func_args*)args)->return_code = TEST_SUCCESS;
  7080. done:
  7081. wolfSSL_free(ssl);
  7082. if (!sharedCtx)
  7083. wolfSSL_CTX_free(ctx);
  7084. CloseSocket(sockfd);
  7085. #ifdef WOLFSSL_TIRTOS
  7086. fdCloseSession(Task_self());
  7087. #endif
  7088. return;
  7089. }
  7090. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  7091. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  7092. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  7093. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  7094. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  7095. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  7096. #define HAVE_ALPN_PROTOS_SUPPORT
  7097. #endif
  7098. /* Generic TLS client / server with callbacks for API unit tests
  7099. * Used by SNI / ALPN / crypto callback helper functions */
  7100. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  7101. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  7102. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  7103. #define ENABLE_TLS_CALLBACK_TEST
  7104. #endif
  7105. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  7106. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  7107. /* TLS server for API unit testing - generic */
  7108. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  7109. {
  7110. callback_functions* callbacks = ((func_args*)args)->callbacks;
  7111. WOLFSSL_CTX* ctx = NULL;
  7112. WOLFSSL* ssl = NULL;
  7113. SOCKET_T sfd = 0;
  7114. SOCKET_T cfd = 0;
  7115. word16 port;
  7116. char msg[] = "I hear you fa shizzle!";
  7117. int len = (int) XSTRLEN(msg);
  7118. char input[1024];
  7119. int idx;
  7120. int ret, err = 0;
  7121. ((func_args*)args)->return_code = TEST_FAIL;
  7122. #if defined(USE_WINDOWS_API)
  7123. port = ((func_args*)args)->signal->port;
  7124. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  7125. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  7126. /* Let tcp_listen assign port */
  7127. port = 0;
  7128. #else
  7129. /* Use default port */
  7130. port = wolfSSLPort;
  7131. #endif
  7132. #ifdef WOLFSSL_DTLS
  7133. if (callbacks->method == wolfDTLS_server_method
  7134. #ifdef WOLFSSL_STATIC_MEMORY
  7135. || callbacks->method_ex == wolfDTLS_server_method_ex
  7136. #endif
  7137. #ifndef NO_OLD_TLS
  7138. || callbacks->method == wolfDTLSv1_server_method
  7139. #ifdef WOLFSSL_STATIC_MEMORY
  7140. || callbacks->method_ex == wolfDTLSv1_server_method_ex
  7141. #endif
  7142. #endif
  7143. #ifndef WOLFSSL_NO_TLS12
  7144. || callbacks->method == wolfDTLSv1_2_server_method
  7145. #ifdef WOLFSSL_STATIC_MEMORY
  7146. || callbacks->method_ex == wolfDTLSv1_2_server_method_ex
  7147. #endif
  7148. #endif
  7149. #ifdef WOLFSSL_DTLS13
  7150. || callbacks->method == wolfDTLSv1_3_server_method
  7151. #ifdef WOLFSSL_STATIC_MEMORY
  7152. || callbacks->method_ex == wolfDTLSv1_3_server_method_ex
  7153. #endif
  7154. #endif
  7155. ) {
  7156. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  7157. }
  7158. else
  7159. #endif
  7160. {
  7161. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  7162. }
  7163. #ifdef WOLFSSL_STATIC_MEMORY
  7164. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  7165. callbacks->memSz > 0) {
  7166. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  7167. callbacks->mem, callbacks->memSz, 0, 1);
  7168. if (ret != WOLFSSL_SUCCESS) {
  7169. fprintf(stderr, "CTX static new failed %d\n", ret);
  7170. goto cleanup;
  7171. }
  7172. }
  7173. #else
  7174. ctx = wolfSSL_CTX_new(callbacks->method());
  7175. #endif
  7176. if (ctx == NULL) {
  7177. fprintf(stderr, "CTX new failed\n");
  7178. goto cleanup;
  7179. }
  7180. /* set defaults */
  7181. if (callbacks->caPemFile == NULL)
  7182. callbacks->caPemFile = cliCertFile;
  7183. if (callbacks->certPemFile == NULL)
  7184. callbacks->certPemFile = svrCertFile;
  7185. if (callbacks->keyPemFile == NULL)
  7186. callbacks->keyPemFile = svrKeyFile;
  7187. #ifdef WOLFSSL_TIRTOS
  7188. fdOpenSession(Task_self());
  7189. #endif
  7190. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  7191. wolfSSL_CTX_set_verify(ctx,
  7192. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  7193. #ifdef WOLFSSL_ENCRYPTED_KEYS
  7194. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  7195. #endif
  7196. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  7197. if (callbacks->method == wolfDTLSv1_2_server_method) {
  7198. if (wolfSSL_CTX_dtls_set_export(ctx, test_export) != WOLFSSL_SUCCESS)
  7199. goto cleanup;
  7200. }
  7201. #endif
  7202. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  7203. WOLFSSL_SUCCESS) {
  7204. goto cleanup;
  7205. }
  7206. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  7207. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7208. goto cleanup;
  7209. }
  7210. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  7211. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7212. goto cleanup;
  7213. }
  7214. #ifdef HAVE_CRL
  7215. if (callbacks->crlPemFile != NULL) {
  7216. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  7217. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7218. goto cleanup;
  7219. }
  7220. }
  7221. #endif
  7222. if (callbacks->ctx_ready)
  7223. callbacks->ctx_ready(ctx);
  7224. ssl = wolfSSL_new(ctx);
  7225. if (ssl == NULL) {
  7226. fprintf(stderr, "SSL new failed\n");
  7227. goto cleanup;
  7228. }
  7229. if (wolfSSL_dtls(ssl)) {
  7230. SOCKADDR_IN_T cliAddr;
  7231. socklen_t cliLen;
  7232. cliLen = sizeof(cliAddr);
  7233. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  7234. (struct sockaddr*)&cliAddr, &cliLen);
  7235. if (idx <= 0) {
  7236. goto cleanup;
  7237. }
  7238. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  7239. }
  7240. else {
  7241. CloseSocket(sfd);
  7242. }
  7243. if (wolfSSL_set_fd(ssl, cfd) != WOLFSSL_SUCCESS) {
  7244. goto cleanup;
  7245. }
  7246. if (callbacks->loadToSSL) {
  7247. wolfSSL_SetDevId(ssl, callbacks->devId);
  7248. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  7249. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7250. goto cleanup;
  7251. }
  7252. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  7253. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7254. goto cleanup;
  7255. }
  7256. }
  7257. #ifdef NO_PSK
  7258. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  7259. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  7260. #elif !defined(NO_DH)
  7261. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  7262. #endif
  7263. #endif
  7264. if (callbacks->ssl_ready)
  7265. callbacks->ssl_ready(ssl);
  7266. #ifdef WOLFSSL_ASYNC_CRYPT
  7267. err = 0; /* Reset error */
  7268. #endif
  7269. do {
  7270. #ifdef WOLFSSL_ASYNC_CRYPT
  7271. if (err == WC_PENDING_E) {
  7272. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7273. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7274. }
  7275. #endif
  7276. ret = wolfSSL_accept(ssl);
  7277. err = wolfSSL_get_error(ssl, ret);
  7278. } while (err == WC_PENDING_E);
  7279. if (ret != WOLFSSL_SUCCESS) {
  7280. char buff[WOLFSSL_MAX_ERROR_SZ];
  7281. fprintf(stderr, "accept error = %d, %s\n", err,
  7282. wolfSSL_ERR_error_string(err, buff));
  7283. /*err_sys("SSL_accept failed");*/
  7284. }
  7285. else {
  7286. #ifdef WOLFSSL_ASYNC_CRYPT
  7287. err = 0; /* Reset error */
  7288. #endif
  7289. do {
  7290. #ifdef WOLFSSL_ASYNC_CRYPT
  7291. if (err == WC_PENDING_E) {
  7292. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7293. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7294. }
  7295. #endif
  7296. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  7297. err = wolfSSL_get_error(ssl, idx);
  7298. } while (err == WC_PENDING_E);
  7299. if (idx > 0) {
  7300. input[idx] = 0;
  7301. fprintf(stderr, "Client message: %s\n", input);
  7302. }
  7303. #ifdef WOLFSSL_ASYNC_CRYPT
  7304. err = 0; /* Reset error */
  7305. #endif
  7306. do {
  7307. #ifdef WOLFSSL_ASYNC_CRYPT
  7308. if (err == WC_PENDING_E) {
  7309. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7310. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7311. }
  7312. #endif
  7313. ret = wolfSSL_write(ssl, msg, len);
  7314. err = wolfSSL_get_error(ssl, ret);
  7315. } while (err == WC_PENDING_E);
  7316. if (len != ret) {
  7317. goto cleanup;
  7318. }
  7319. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  7320. defined(WOLFSSL_DTLS)
  7321. if (wolfSSL_dtls(ssl)) {
  7322. byte* import;
  7323. word32 sz;
  7324. wolfSSL_dtls_export(ssl, NULL, &sz);
  7325. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7326. if (import == NULL) {
  7327. goto cleanup;
  7328. }
  7329. idx = wolfSSL_dtls_export(ssl, import, &sz);
  7330. if (idx < 0) {
  7331. goto cleanup;
  7332. }
  7333. if (wolfSSL_dtls_import(ssl, import, idx) < 0) {
  7334. goto cleanup;
  7335. }
  7336. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7337. }
  7338. #endif
  7339. #ifdef WOLFSSL_TIRTOS
  7340. Task_yield();
  7341. #endif
  7342. ((func_args*)args)->return_code = TEST_SUCCESS;
  7343. }
  7344. if (callbacks->on_result)
  7345. callbacks->on_result(ssl);
  7346. wolfSSL_shutdown(ssl);
  7347. cleanup:
  7348. wolfSSL_free(ssl);
  7349. wolfSSL_CTX_free(ctx);
  7350. CloseSocket(cfd);
  7351. #ifdef WOLFSSL_TIRTOS
  7352. fdCloseSession(Task_self());
  7353. #endif
  7354. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  7355. && defined(HAVE_THREAD_LS)
  7356. wc_ecc_fp_free(); /* free per thread cache */
  7357. #endif
  7358. WOLFSSL_RETURN_FROM_THREAD(0);
  7359. }
  7360. /* TLS Client for API unit testing - generic */
  7361. static void run_wolfssl_client(void* args)
  7362. {
  7363. callback_functions* callbacks = ((func_args*)args)->callbacks;
  7364. WOLFSSL_CTX* ctx = NULL;
  7365. WOLFSSL* ssl = NULL;
  7366. SOCKET_T sfd = 0;
  7367. char msg[] = "hello wolfssl server!";
  7368. int len = (int) XSTRLEN(msg);
  7369. char input[1024];
  7370. int ret, err = 0;
  7371. ((func_args*)args)->return_code = TEST_FAIL;
  7372. /* set defaults */
  7373. if (callbacks->caPemFile == NULL)
  7374. callbacks->caPemFile = caCertFile;
  7375. if (callbacks->certPemFile == NULL)
  7376. callbacks->certPemFile = cliCertFile;
  7377. if (callbacks->keyPemFile == NULL)
  7378. callbacks->keyPemFile = cliKeyFile;
  7379. #ifdef WOLFSSL_STATIC_MEMORY
  7380. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  7381. callbacks->memSz > 0) {
  7382. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  7383. callbacks->mem, callbacks->memSz, 0, 1);
  7384. if (ret != WOLFSSL_SUCCESS) {
  7385. fprintf(stderr, "CTX static new failed %d\n", ret);
  7386. goto cleanup;
  7387. }
  7388. }
  7389. #else
  7390. ctx = wolfSSL_CTX_new(callbacks->method());
  7391. #endif
  7392. if (ctx == NULL) {
  7393. fprintf(stderr, "CTX new failed\n");
  7394. goto cleanup;
  7395. }
  7396. #ifdef WOLFSSL_TIRTOS
  7397. fdOpenSession(Task_self());
  7398. #endif
  7399. if (!callbacks->loadToSSL) {
  7400. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  7401. }
  7402. #ifdef WOLFSSL_ENCRYPTED_KEYS
  7403. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  7404. #endif
  7405. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  7406. WOLFSSL_SUCCESS) {
  7407. goto cleanup;
  7408. }
  7409. if (!callbacks->loadToSSL) {
  7410. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  7411. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7412. goto cleanup;
  7413. }
  7414. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  7415. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7416. goto cleanup;
  7417. }
  7418. }
  7419. #ifdef HAVE_CRL
  7420. if (callbacks->crlPemFile != NULL) {
  7421. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  7422. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7423. goto cleanup;
  7424. }
  7425. }
  7426. #endif
  7427. if (callbacks->ctx_ready)
  7428. callbacks->ctx_ready(ctx);
  7429. ssl = wolfSSL_new(ctx);
  7430. if (wolfSSL_dtls(ssl)) {
  7431. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7432. 1, 0, ssl);
  7433. }
  7434. else {
  7435. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7436. 0, 0, ssl);
  7437. }
  7438. if (wolfSSL_set_fd(ssl, sfd) != WOLFSSL_SUCCESS) {
  7439. goto cleanup;
  7440. }
  7441. if (callbacks->loadToSSL) {
  7442. wolfSSL_SetDevId(ssl, callbacks->devId);
  7443. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  7444. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7445. goto cleanup;
  7446. }
  7447. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  7448. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7449. goto cleanup;
  7450. }
  7451. }
  7452. if (callbacks->ssl_ready)
  7453. callbacks->ssl_ready(ssl);
  7454. #ifdef WOLFSSL_ASYNC_CRYPT
  7455. err = 0; /* Reset error */
  7456. #endif
  7457. do {
  7458. #ifdef WOLFSSL_ASYNC_CRYPT
  7459. if (err == WC_PENDING_E) {
  7460. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7461. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7462. }
  7463. #endif
  7464. ret = wolfSSL_connect(ssl);
  7465. err = wolfSSL_get_error(ssl, ret);
  7466. } while (err == WC_PENDING_E);
  7467. if (ret != WOLFSSL_SUCCESS) {
  7468. char buff[WOLFSSL_MAX_ERROR_SZ];
  7469. fprintf(stderr, "error = %d, %s\n", err,
  7470. wolfSSL_ERR_error_string(err, buff));
  7471. /*err_sys("SSL_connect failed");*/
  7472. }
  7473. else {
  7474. #ifdef WOLFSSL_ASYNC_CRYPT
  7475. err = 0; /* Reset error */
  7476. #endif
  7477. do {
  7478. #ifdef WOLFSSL_ASYNC_CRYPT
  7479. if (err == WC_PENDING_E) {
  7480. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7481. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7482. }
  7483. #endif
  7484. ret = wolfSSL_write(ssl, msg, len);
  7485. err = wolfSSL_get_error(ssl, ret);
  7486. } while (err == WC_PENDING_E);
  7487. if (len != ret)
  7488. goto cleanup;
  7489. #ifdef WOLFSSL_ASYNC_CRYPT
  7490. err = 0; /* Reset error */
  7491. #endif
  7492. do {
  7493. #ifdef WOLFSSL_ASYNC_CRYPT
  7494. if (err == WC_PENDING_E) {
  7495. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7496. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7497. }
  7498. #endif
  7499. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  7500. err = wolfSSL_get_error(ssl, ret);
  7501. } while (err == WC_PENDING_E);
  7502. if (ret > 0) {
  7503. input[ret] = '\0'; /* null term */
  7504. fprintf(stderr, "Server response: %s\n", input);
  7505. }
  7506. ((func_args*)args)->return_code = TEST_SUCCESS;
  7507. }
  7508. if (callbacks->on_result)
  7509. callbacks->on_result(ssl);
  7510. cleanup:
  7511. wolfSSL_free(ssl);
  7512. wolfSSL_CTX_free(ctx);
  7513. CloseSocket(sfd);
  7514. #ifdef WOLFSSL_TIRTOS
  7515. fdCloseSession(Task_self());
  7516. #endif
  7517. }
  7518. #endif /* ENABLE_TLS_CALLBACK_TEST */
  7519. static int test_wolfSSL_read_write(void)
  7520. {
  7521. /* The unit testing for read and write shall happen simultaneously, since
  7522. * one can't do anything with one without the other. (Except for a failure
  7523. * test case.) This function will call all the others that will set up,
  7524. * execute, and report their test findings.
  7525. *
  7526. * Set up the success case first. This function will become the template
  7527. * for the other tests. This should eventually be renamed
  7528. *
  7529. * The success case isn't interesting, how can this fail?
  7530. * - Do not give the client context a CA certificate. The connect should
  7531. * fail. Do not need server for this?
  7532. * - Using NULL for the ssl object on server. Do not need client for this.
  7533. * - Using NULL for the ssl object on client. Do not need server for this.
  7534. * - Good ssl objects for client and server. Client write() without server
  7535. * read().
  7536. * - Good ssl objects for client and server. Server write() without client
  7537. * read().
  7538. * - Forgetting the password callback?
  7539. */
  7540. tcp_ready ready;
  7541. func_args client_args;
  7542. func_args server_args;
  7543. THREAD_TYPE serverThread;
  7544. EXPECT_DECLS;
  7545. XMEMSET(&client_args, 0, sizeof(func_args));
  7546. XMEMSET(&server_args, 0, sizeof(func_args));
  7547. #ifdef WOLFSSL_TIRTOS
  7548. fdOpenSession(Task_self());
  7549. #endif
  7550. StartTCP();
  7551. InitTcpReady(&ready);
  7552. #if defined(USE_WINDOWS_API)
  7553. /* use RNG to get random port if using windows */
  7554. ready.port = GetRandomPort();
  7555. #endif
  7556. server_args.signal = &ready;
  7557. client_args.signal = &ready;
  7558. start_thread(test_server_nofail, &server_args, &serverThread);
  7559. wait_tcp_ready(&server_args);
  7560. test_client_nofail(&client_args, NULL);
  7561. join_thread(serverThread);
  7562. ExpectTrue(client_args.return_code);
  7563. ExpectTrue(server_args.return_code);
  7564. FreeTcpReady(&ready);
  7565. #ifdef WOLFSSL_TIRTOS
  7566. fdOpenSession(Task_self());
  7567. #endif
  7568. return EXPECT_RESULT();
  7569. }
  7570. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  7571. {
  7572. EXPECT_DECLS;
  7573. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  7574. !defined(WOLFSSL_NO_TLS12)
  7575. /* The unit test for session resumption by re-using WOLFSSL object.
  7576. * WOLFSSL object is not cleared after first session. It reuse the object
  7577. * for second connection.
  7578. */
  7579. tcp_ready ready;
  7580. func_args client_args;
  7581. func_args server_args;
  7582. THREAD_TYPE serverThread;
  7583. callback_functions client_cbf;
  7584. callback_functions server_cbf;
  7585. XMEMSET(&client_args, 0, sizeof(func_args));
  7586. XMEMSET(&server_args, 0, sizeof(func_args));
  7587. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  7588. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7589. #ifdef WOLFSSL_TIRTOS
  7590. fdOpenSession(Task_self());
  7591. #endif
  7592. StartTCP();
  7593. InitTcpReady(&ready);
  7594. #if defined(USE_WINDOWS_API)
  7595. /* use RNG to get random port if using windows */
  7596. ready.port = GetRandomPort();
  7597. #endif
  7598. client_cbf.method = wolfTLSv1_2_client_method;
  7599. server_cbf.method = wolfTLSv1_2_server_method;
  7600. client_args.callbacks = &client_cbf;
  7601. server_args.callbacks = &server_cbf;
  7602. server_args.signal = &ready;
  7603. client_args.signal = &ready;
  7604. /* the var is used for loop number */
  7605. server_args.argc = 2;
  7606. start_thread(test_server_loop, &server_args, &serverThread);
  7607. wait_tcp_ready(&server_args);
  7608. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  7609. join_thread(serverThread);
  7610. ExpectTrue(client_args.return_code);
  7611. ExpectTrue(server_args.return_code);
  7612. FreeTcpReady(&ready);
  7613. #ifdef WOLFSSL_TIRTOS
  7614. fdOpenSession(Task_self());
  7615. #endif
  7616. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  7617. * !defined(WOLFSSL_TLS13) */
  7618. return EXPECT_RESULT();
  7619. }
  7620. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7621. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7622. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready(
  7623. WOLFSSL_CTX* ctx)
  7624. {
  7625. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7626. myVerifyAction = VERIFY_USE_PREVERFIY;
  7627. wolfSSL_CTX_set_verify_depth(ctx, 2);
  7628. return TEST_SUCCESS;
  7629. }
  7630. #endif
  7631. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1(void)
  7632. {
  7633. EXPECT_DECLS;
  7634. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7635. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7636. test_ssl_cbf client_cbf;
  7637. test_ssl_cbf server_cbf;
  7638. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7639. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7640. #ifdef WOLFSSL_TLS13
  7641. client_cbf.method = wolfTLSv1_3_client_method;
  7642. #endif /* WOLFSSL_TLS13 */
  7643. client_cbf.ctx_ready =
  7644. test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready;
  7645. /* test case 1 verify depth is equal to peer chain */
  7646. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7647. &server_cbf, NULL), TEST_SUCCESS);
  7648. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7649. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7650. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7651. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7652. return EXPECT_RESULT();
  7653. }
  7654. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7655. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7656. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready(
  7657. WOLFSSL_CTX* ctx)
  7658. {
  7659. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7660. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  7661. wolfSSL_CTX_set_verify_depth(ctx, 0);
  7662. return TEST_SUCCESS;
  7663. }
  7664. #endif
  7665. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2(void)
  7666. {
  7667. EXPECT_DECLS;
  7668. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7669. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7670. test_ssl_cbf client_cbf;
  7671. test_ssl_cbf server_cbf;
  7672. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7673. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7674. #ifdef WOLFSSL_TLS13
  7675. client_cbf.method = wolfTLSv1_3_client_method;
  7676. #endif /* WOLFSSL_TLS13 */
  7677. client_cbf.ctx_ready =
  7678. test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready;
  7679. /* test case 2
  7680. * verify depth is zero, number of peer's chain is 2.
  7681. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  7682. * callback.
  7683. */
  7684. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7685. &server_cbf, NULL), TEST_SUCCESS);
  7686. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7687. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7688. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7689. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7690. return EXPECT_RESULT();
  7691. }
  7692. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7693. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7694. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready(
  7695. WOLFSSL_CTX* ctx)
  7696. {
  7697. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7698. myVerifyAction = VERIFY_USE_PREVERFIY;
  7699. wolfSSL_CTX_set_verify_depth(ctx, 0);
  7700. return TEST_SUCCESS;
  7701. }
  7702. #endif
  7703. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3(void)
  7704. {
  7705. EXPECT_DECLS;
  7706. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7707. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7708. test_ssl_cbf client_cbf;
  7709. test_ssl_cbf server_cbf;
  7710. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7711. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7712. #ifdef WOLFSSL_TLS13
  7713. client_cbf.method = wolfTLSv1_3_client_method;
  7714. #endif /* WOLFSSL_TLS13 */
  7715. client_cbf.ctx_ready =
  7716. test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready;
  7717. /* test case 3
  7718. * verify depth is zero, number of peer's chain is 2
  7719. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  7720. * therefore, handshake becomes failure.
  7721. */
  7722. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7723. &server_cbf, NULL), TEST_FAIL);
  7724. ExpectIntEQ(client_cbf.return_code, TEST_FAIL);
  7725. ExpectIntEQ(server_cbf.return_code, TEST_FAIL);
  7726. ExpectIntEQ(client_cbf.last_err, MAX_CHAIN_ERROR);
  7727. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  7728. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7729. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7730. return EXPECT_RESULT();
  7731. }
  7732. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7733. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  7734. && !defined(NO_SHA256) && defined(HAVE_ECC)
  7735. static int test_wolfSSL_CTX_set_cipher_list_server_ctx_ready(WOLFSSL_CTX* ctx)
  7736. {
  7737. EXPECT_DECLS;
  7738. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "DEFAULT:!NULL"));
  7739. return EXPECT_RESULT();
  7740. }
  7741. static int test_wolfSSL_CTX_set_cipher_list_client_ctx_ready(WOLFSSL_CTX* ctx)
  7742. {
  7743. EXPECT_DECLS;
  7744. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256"));
  7745. return EXPECT_RESULT();
  7746. }
  7747. #endif
  7748. static int test_wolfSSL_CTX_set_cipher_list(void)
  7749. {
  7750. EXPECT_DECLS;
  7751. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7752. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  7753. && !defined(NO_SHA256) && defined(HAVE_ECC)
  7754. WOLFSSL_CTX* ctxClient = NULL;
  7755. WOLFSSL* sslClient = NULL;
  7756. test_ssl_cbf client_cbf;
  7757. test_ssl_cbf server_cbf;
  7758. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7759. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7760. server_cbf.method = wolfTLSv1_2_server_method;
  7761. server_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_server_ctx_ready;
  7762. client_cbf.method = wolfTLSv1_2_client_method;
  7763. client_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_client_ctx_ready;
  7764. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7765. &server_cbf, NULL), TEST_SUCCESS);
  7766. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7767. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7768. /* check with cipher string that has '+' */
  7769. ExpectNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  7770. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE+AESGCM"));
  7771. ExpectNotNull((sslClient = wolfSSL_new(ctxClient)));
  7772. /* check for the existence of an ECDHE ECDSA cipher suite */
  7773. if (EXPECT_SUCCESS()) {
  7774. int i = 0;
  7775. int found = 0;
  7776. const char* suite;
  7777. WOLF_STACK_OF(WOLFSSL_CIPHER)* sk = NULL;
  7778. WOLFSSL_CIPHER* current;
  7779. ExpectNotNull((sk = wolfSSL_get_ciphers_compat(sslClient)));
  7780. do {
  7781. current = wolfSSL_sk_SSL_CIPHER_value(sk, i++);
  7782. if (current) {
  7783. suite = wolfSSL_CIPHER_get_name(current);
  7784. if (suite && XSTRSTR(suite, "ECDSA")) {
  7785. found = 1;
  7786. break;
  7787. }
  7788. }
  7789. } while (current);
  7790. ExpectIntEQ(found, 1);
  7791. }
  7792. wolfSSL_free(sslClient);
  7793. wolfSSL_CTX_free(ctxClient);
  7794. #endif
  7795. return EXPECT_RESULT();
  7796. }
  7797. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7798. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7799. static int test_wolfSSL_get_finished_client_on_handshake(WOLFSSL_CTX* ctx,
  7800. WOLFSSL* ssl)
  7801. {
  7802. EXPECT_DECLS;
  7803. size_t msg_len;
  7804. (void)ctx;
  7805. /* get_finished test */
  7806. /* 1. get own sent message */
  7807. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  7808. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  7809. ExpectIntGE(msg_len, 0);
  7810. /* 2. get peer message */
  7811. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  7812. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  7813. ExpectIntGE(msg_len, 0);
  7814. return EXPECT_RESULT();
  7815. }
  7816. #endif
  7817. static int test_wolfSSL_get_finished(void)
  7818. {
  7819. EXPECT_DECLS;
  7820. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7821. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7822. test_ssl_cbf client_cbf;
  7823. test_ssl_cbf server_cbf;
  7824. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7825. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7826. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7827. &server_cbf, test_wolfSSL_get_finished_client_on_handshake),
  7828. TEST_SUCCESS);
  7829. /* test received msg vs sent msg */
  7830. ExpectIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  7831. ExpectIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  7832. #endif /* HAVE_SSL_MEMIO_TESTS_DEPENDENCIES && WOLFSSL_HAVE_TLS_UNIQUE */
  7833. return EXPECT_RESULT();
  7834. }
  7835. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7836. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7837. !defined(NO_SESSION_CACHE)
  7838. /* Sessions to restore/store */
  7839. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  7840. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  7841. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  7842. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  7843. {
  7844. /* Don't store sessions. Lookup is still enabled. */
  7845. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7846. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7847. #ifdef OPENSSL_EXTRA
  7848. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7849. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7850. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7851. #endif
  7852. /* Require both peers to provide certs */
  7853. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7854. }
  7855. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  7856. {
  7857. WOLFSSL_SESSION** sess;
  7858. if (wolfSSL_is_server(ssl))
  7859. sess = &test_wolfSSL_CTX_add_session_server_sess;
  7860. else
  7861. sess = &test_wolfSSL_CTX_add_session_client_sess;
  7862. if (*sess == NULL) {
  7863. #ifdef NO_SESSION_CACHE_REF
  7864. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7865. #else
  7866. /* Test for backwards compatibility */
  7867. if (wolfSSL_is_server(ssl)) {
  7868. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7869. }
  7870. else {
  7871. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  7872. }
  7873. #endif
  7874. /* Now save the session in the internal store to make it available
  7875. * for lookup. For TLS 1.3, we can't save the session without
  7876. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  7877. * session from cache. */
  7878. if (wolfSSL_is_server(ssl)
  7879. #ifndef WOLFSSL_TICKET_HAVE_ID
  7880. && wolfSSL_version(ssl) != TLS1_3_VERSION
  7881. #endif
  7882. )
  7883. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  7884. *sess), WOLFSSL_SUCCESS);
  7885. }
  7886. else {
  7887. /* If we have a session retrieved then remaining connections should be
  7888. * resuming on that session */
  7889. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  7890. }
  7891. /* Save CTX to be able to decrypt tickets */
  7892. if (wolfSSL_is_server(ssl) &&
  7893. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  7894. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  7895. = wolfSSL_get_SSL_CTX(ssl));
  7896. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  7897. WOLFSSL_SUCCESS);
  7898. }
  7899. #ifdef SESSION_CERTS
  7900. #ifndef WOLFSSL_TICKET_HAVE_ID
  7901. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7902. wolfSSL_session_reused(ssl))
  7903. #endif
  7904. {
  7905. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  7906. * for all connections. TLS 1.3 only has tickets so if we don't
  7907. * include the session id in the ticket then the certificates
  7908. * will not be available on resumption. */
  7909. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  7910. AssertNotNull(peer);
  7911. wolfSSL_X509_free(peer);
  7912. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7913. #ifdef OPENSSL_EXTRA
  7914. AssertNotNull(SSL_SESSION_get0_peer(*sess));
  7915. #endif
  7916. }
  7917. #endif /* SESSION_CERTS */
  7918. }
  7919. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  7920. {
  7921. /* Set the session to reuse for the client */
  7922. AssertIntEQ(wolfSSL_set_session(ssl,
  7923. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  7924. }
  7925. #endif
  7926. static int test_wolfSSL_CTX_add_session(void)
  7927. {
  7928. EXPECT_DECLS;
  7929. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7930. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7931. !defined(NO_SESSION_CACHE)
  7932. tcp_ready ready;
  7933. func_args client_args;
  7934. func_args server_args;
  7935. THREAD_TYPE serverThread;
  7936. callback_functions client_cb;
  7937. callback_functions server_cb;
  7938. method_provider methods[][2] = {
  7939. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7940. !defined(NO_DES3))
  7941. /* Without AES there are almost no ciphersuites available. This leads
  7942. * to no ciphersuites being available and an error. */
  7943. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  7944. #endif
  7945. #ifndef WOLFSSL_NO_TLS12
  7946. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  7947. #endif
  7948. /* Needs the default ticket callback since it is tied to the
  7949. * connection context and this makes it easy to carry over the ticket
  7950. * crypto context between connections */
  7951. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7952. defined(HAVE_SESSION_TICKET)
  7953. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  7954. #endif
  7955. };
  7956. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  7957. size_t i, j;
  7958. for (i = 0; i < methodsLen; i++) {
  7959. /* First run creates a connection while the second+ run will attempt
  7960. * to resume the connection. The trick is that the internal cache
  7961. * is turned off. wolfSSL_CTX_add_session should put the session in
  7962. * the cache anyway. */
  7963. test_wolfSSL_CTX_add_session_client_sess = NULL;
  7964. test_wolfSSL_CTX_add_session_server_sess = NULL;
  7965. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  7966. #ifdef NO_SESSION_CACHE_REF
  7967. for (j = 0; j < 4; j++) {
  7968. #else
  7969. /* The session may be overwritten in this case. Do only one resumption
  7970. * to stop this test from failing intermittently. */
  7971. for (j = 0; j < 2; j++) {
  7972. #endif
  7973. #ifdef WOLFSSL_TIRTOS
  7974. fdOpenSession(Task_self());
  7975. #endif
  7976. StartTCP();
  7977. InitTcpReady(&ready);
  7978. XMEMSET(&client_args, 0, sizeof(func_args));
  7979. XMEMSET(&server_args, 0, sizeof(func_args));
  7980. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7981. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7982. client_cb.method = methods[i][0];
  7983. server_cb.method = methods[i][1];
  7984. server_args.signal = &ready;
  7985. server_args.callbacks = &server_cb;
  7986. client_args.signal = &ready;
  7987. client_args.callbacks = &client_cb;
  7988. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  7989. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  7990. server_cb.isSharedCtx = 1;
  7991. }
  7992. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  7993. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  7994. if (j != 0)
  7995. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  7996. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  7997. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  7998. server_cb.ticNoInit = 1; /* Use default builtin */
  7999. start_thread(test_server_nofail, &server_args, &serverThread);
  8000. wait_tcp_ready(&server_args);
  8001. test_client_nofail(&client_args, NULL);
  8002. join_thread(serverThread);
  8003. ExpectTrue(client_args.return_code);
  8004. ExpectTrue(server_args.return_code);
  8005. FreeTcpReady(&ready);
  8006. if (EXPECT_FAIL())
  8007. break;
  8008. }
  8009. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  8010. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  8011. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  8012. if (EXPECT_FAIL())
  8013. break;
  8014. }
  8015. #endif
  8016. return EXPECT_RESULT();
  8017. }
  8018. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8019. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8020. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8021. defined(HAVE_SESSION_TICKET) && \
  8022. !defined(TITAN_SESSION_CACHE) && \
  8023. !defined(HUGE_SESSION_CACHE) && \
  8024. !defined(BIG_SESSION_CACHE) && \
  8025. !defined(MEDIUM_SESSION_CACHE)
  8026. /* twcase - prefix for test_wolfSSL_CTX_add_session_ext */
  8027. /* Sessions to restore/store */
  8028. static WOLFSSL_SESSION* twcase_server_first_session_ptr;
  8029. static WOLFSSL_SESSION* twcase_client_first_session_ptr;
  8030. static WOLFSSL_CTX* twcase_server_current_ctx_ptr;
  8031. static int twcase_new_session_called = 0;
  8032. static int twcase_remove_session_called = 0;
  8033. static int twcase_get_session_called = 0;
  8034. /* Test default, SESSIONS_PER_ROW*SESSION_ROWS = 3*11, see ssl.c */
  8035. #define SESSION_CACHE_SIZE 33
  8036. typedef struct {
  8037. const byte* key; /* key, altSessionID, session ID, NULL if empty */
  8038. WOLFSSL_SESSION* value;
  8039. } hashTable_entry;
  8040. typedef struct {
  8041. hashTable_entry entries[SESSION_CACHE_SIZE]; /* hash slots */
  8042. size_t capacity; /* size of entries */
  8043. size_t length; /* number of items in the hash table */
  8044. wolfSSL_Mutex htLock; /* lock */
  8045. }hashTable;
  8046. static hashTable server_sessionCache;
  8047. static int twcase_new_sessionCb(WOLFSSL *ssl, WOLFSSL_SESSION *sess)
  8048. {
  8049. int i;
  8050. unsigned int len;
  8051. (void)ssl;
  8052. /*
  8053. * This example uses a hash table.
  8054. * Steps you should take for a non-demo code:
  8055. * - acquire a lock for the file named according to the session id
  8056. * - open the file
  8057. * - encrypt and write the SSL_SESSION object to the file
  8058. * - release the lock
  8059. *
  8060. * Return:
  8061. * 0: The callback does not wish to hold a reference of the sess
  8062. * 1: The callback wants to hold a reference of the sess. The callback is
  8063. * now also responsible for calling wolfSSL_SESSION_free() on sess.
  8064. */
  8065. if (sess == NULL)
  8066. return 0;
  8067. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  8068. return 0;
  8069. }
  8070. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8071. if (server_sessionCache.entries[i].value == NULL) {
  8072. server_sessionCache.entries[i].key = SSL_SESSION_get_id(sess, &len);
  8073. server_sessionCache.entries[i].value = sess;
  8074. server_sessionCache.length++;
  8075. break;
  8076. }
  8077. }
  8078. ++twcase_new_session_called;
  8079. wc_UnLockMutex(&server_sessionCache.htLock);
  8080. fprintf(stderr, "\t\ttwcase_new_session_called %d\n",
  8081. twcase_new_session_called);
  8082. return 1;
  8083. }
  8084. static void twcase_remove_sessionCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  8085. {
  8086. int i;
  8087. (void)ctx;
  8088. (void)sess;
  8089. if (sess == NULL)
  8090. return;
  8091. /*
  8092. * This example uses a hash table.
  8093. * Steps you should take for a non-demo code:
  8094. * - acquire a lock for the file named according to the session id
  8095. * - remove the file
  8096. * - release the lock
  8097. */
  8098. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  8099. return;
  8100. }
  8101. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8102. if (server_sessionCache.entries[i].key != NULL &&
  8103. XMEMCMP(server_sessionCache.entries[i].key,
  8104. sess->sessionID, SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  8105. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  8106. server_sessionCache.entries[i].value = NULL;
  8107. server_sessionCache.entries[i].key = NULL;
  8108. server_sessionCache.length--;
  8109. break;
  8110. }
  8111. }
  8112. ++twcase_remove_session_called;
  8113. wc_UnLockMutex(&server_sessionCache.htLock);
  8114. fprintf(stderr, "\t\ttwcase_remove_session_called %d\n",
  8115. twcase_remove_session_called);
  8116. }
  8117. static WOLFSSL_SESSION *twcase_get_sessionCb(WOLFSSL *ssl,
  8118. const unsigned char *id, int len, int *ref)
  8119. {
  8120. int i;
  8121. (void)ssl;
  8122. (void)id;
  8123. (void)len;
  8124. /*
  8125. * This example uses a hash table.
  8126. * Steps you should take for a non-demo code:
  8127. * - acquire a lock for the file named according to the session id in the
  8128. * 2nd arg
  8129. * - read and decrypt contents of file and create a new SSL_SESSION
  8130. * - object release the lock
  8131. * - return the new session object
  8132. */
  8133. fprintf(stderr, "\t\ttwcase_get_session_called %d\n",
  8134. ++twcase_get_session_called);
  8135. /* This callback want to retain a copy of the object. If we want wolfSSL to
  8136. * be responsible for the pointer then set to 0. */
  8137. *ref = 1;
  8138. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8139. if (server_sessionCache.entries[i].key != NULL &&
  8140. XMEMCMP(server_sessionCache.entries[i].key, id,
  8141. SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  8142. return server_sessionCache.entries[i].value;
  8143. }
  8144. }
  8145. return NULL;
  8146. }
  8147. static int twcase_get_sessionCb_cleanup(void)
  8148. {
  8149. int i;
  8150. int cnt = 0;
  8151. /* If twcase_get_sessionCb sets *ref = 1, the application is responsible
  8152. * for freeing sessions */
  8153. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8154. if (server_sessionCache.entries[i].value != NULL) {
  8155. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  8156. cnt++;
  8157. }
  8158. }
  8159. fprintf(stderr, "\t\ttwcase_get_sessionCb_cleanup freed %d sessions\n",
  8160. cnt);
  8161. return TEST_SUCCESS;
  8162. }
  8163. static int twcase_cache_intOff_extOff(WOLFSSL_CTX* ctx)
  8164. {
  8165. EXPECT_DECLS;
  8166. /* off - Disable internal cache */
  8167. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  8168. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  8169. #ifdef OPENSSL_EXTRA
  8170. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  8171. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  8172. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  8173. #endif
  8174. /* off - Do not setup external cache */
  8175. /* Require both peers to provide certs */
  8176. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8177. return EXPECT_RESULT();
  8178. }
  8179. static int twcase_cache_intOn_extOff(WOLFSSL_CTX* ctx)
  8180. {
  8181. /* on - internal cache is on by default */
  8182. /* off - Do not setup external cache */
  8183. /* Require both peers to provide certs */
  8184. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8185. return TEST_SUCCESS;
  8186. }
  8187. static int twcase_cache_intOff_extOn(WOLFSSL_CTX* ctx)
  8188. {
  8189. EXPECT_DECLS;
  8190. /* off - Disable internal cache */
  8191. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  8192. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  8193. #ifdef OPENSSL_EXTRA
  8194. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  8195. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  8196. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  8197. #endif
  8198. /* on - Enable external cache */
  8199. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8200. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8201. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8202. /* Require both peers to provide certs */
  8203. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8204. return EXPECT_RESULT();
  8205. }
  8206. static int twcase_cache_intOn_extOn(WOLFSSL_CTX* ctx)
  8207. {
  8208. /* on - internal cache is on by default */
  8209. /* on - Enable external cache */
  8210. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8211. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8212. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8213. /* Require both peers to provide certs */
  8214. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8215. return TEST_SUCCESS;
  8216. }
  8217. static int twcase_cache_intOn_extOn_noTicket(WOLFSSL_CTX* ctx)
  8218. {
  8219. /* on - internal cache is on by default */
  8220. /* on - Enable external cache */
  8221. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8222. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8223. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8224. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TICKET);
  8225. /* Require both peers to provide certs */
  8226. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8227. return TEST_SUCCESS;
  8228. }
  8229. static int twcase_server_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  8230. {
  8231. EXPECT_DECLS;
  8232. WOLFSSL_SESSION** sess;
  8233. if (wolfSSL_is_server(ssl))
  8234. sess = &twcase_server_first_session_ptr;
  8235. else
  8236. return TEST_SUCCESS;
  8237. if (*sess == NULL) {
  8238. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  8239. /* Now save the session in the internal store to make it available
  8240. * for lookup. For TLS 1.3, we can't save the session without
  8241. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  8242. * session from cache. */
  8243. if (wolfSSL_is_server(ssl)
  8244. #ifndef WOLFSSL_TICKET_HAVE_ID
  8245. && wolfSSL_version(ssl) != TLS1_3_VERSION
  8246. && wolfSSL_version(ssl) != DTLS1_3_VERSION
  8247. #endif
  8248. ) {
  8249. ExpectIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  8250. *sess), WOLFSSL_SUCCESS);
  8251. }
  8252. }
  8253. /* Save CTX to be able to decrypt tickets */
  8254. if (twcase_server_current_ctx_ptr == NULL) {
  8255. ExpectNotNull(twcase_server_current_ctx_ptr = wolfSSL_get_SSL_CTX(ssl));
  8256. ExpectIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  8257. WOLFSSL_SUCCESS);
  8258. }
  8259. #ifdef SESSION_CERTS
  8260. #ifndef WOLFSSL_TICKET_HAVE_ID
  8261. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  8262. wolfSSL_session_reused(ssl))
  8263. #endif
  8264. {
  8265. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  8266. * for all connections. TLS 1.3 only has tickets so if we don't
  8267. * include the session id in the ticket then the certificates
  8268. * will not be available on resumption. */
  8269. WOLFSSL_X509* peer = NULL;
  8270. ExpectNotNull(peer = wolfSSL_get_peer_certificate(ssl));
  8271. wolfSSL_X509_free(peer);
  8272. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  8273. }
  8274. #endif
  8275. return EXPECT_RESULT();
  8276. }
  8277. static int twcase_client_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  8278. {
  8279. EXPECT_DECLS;
  8280. WOLFSSL_SESSION** sess;
  8281. sess = &twcase_client_first_session_ptr;
  8282. if (*sess == NULL) {
  8283. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  8284. }
  8285. else {
  8286. /* If we have a session retrieved then remaining connections should be
  8287. * resuming on that session */
  8288. ExpectIntEQ(wolfSSL_session_reused(ssl), 1);
  8289. }
  8290. #ifdef SESSION_CERTS
  8291. #ifndef WOLFSSL_TICKET_HAVE_ID
  8292. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  8293. wolfSSL_session_reused(ssl))
  8294. #endif
  8295. {
  8296. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  8297. ExpectNotNull(peer);
  8298. wolfSSL_X509_free(peer);
  8299. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  8300. #ifdef OPENSSL_EXTRA
  8301. ExpectNotNull(wolfSSL_SESSION_get0_peer(*sess));
  8302. #endif
  8303. }
  8304. #endif
  8305. return EXPECT_RESULT();
  8306. }
  8307. static int twcase_client_set_sess_ssl_ready(WOLFSSL* ssl)
  8308. {
  8309. EXPECT_DECLS;
  8310. /* Set the session to reuse for the client */
  8311. ExpectNotNull(ssl);
  8312. ExpectNotNull(twcase_client_first_session_ptr);
  8313. ExpectIntEQ(wolfSSL_set_session(ssl,twcase_client_first_session_ptr),
  8314. WOLFSSL_SUCCESS);
  8315. return EXPECT_RESULT();
  8316. }
  8317. struct test_add_session_ext_params {
  8318. method_provider client_meth;
  8319. method_provider server_meth;
  8320. const char* tls_version;
  8321. };
  8322. static int test_wolfSSL_CTX_add_session_ext(
  8323. struct test_add_session_ext_params* param)
  8324. {
  8325. EXPECT_DECLS;
  8326. /* Test the default 33 sessions */
  8327. int j;
  8328. /* Clear cache before starting */
  8329. wolfSSL_CTX_flush_sessions(NULL, -1);
  8330. XMEMSET(&server_sessionCache, 0, sizeof(hashTable));
  8331. if (wc_InitMutex(&server_sessionCache.htLock) != 0)
  8332. return BAD_MUTEX_E;
  8333. server_sessionCache.capacity = SESSION_CACHE_SIZE;
  8334. fprintf(stderr, "\tBegin %s\n", param->tls_version);
  8335. for (j = 0; j < 5; j++) {
  8336. int tls13 = XSTRSTR(param->tls_version, "TLSv1_3") != NULL;
  8337. int dtls = XSTRSTR(param->tls_version, "DTLS") != NULL;
  8338. test_ssl_cbf client_cb;
  8339. test_ssl_cbf server_cb;
  8340. (void)dtls;
  8341. /* Test five cache configurations */
  8342. twcase_client_first_session_ptr = NULL;
  8343. twcase_server_first_session_ptr = NULL;
  8344. twcase_server_current_ctx_ptr = NULL;
  8345. twcase_new_session_called = 0;
  8346. twcase_remove_session_called = 0;
  8347. twcase_get_session_called = 0;
  8348. /* connection 1 - first connection */
  8349. fprintf(stderr, "\tconnect: %s: j=%d\n", param->tls_version, j);
  8350. XMEMSET(&client_cb, 0, sizeof(client_cb));
  8351. XMEMSET(&server_cb, 0, sizeof(server_cb));
  8352. client_cb.method = param->client_meth;
  8353. server_cb.method = param->server_meth;
  8354. if (dtls)
  8355. client_cb.doUdp = server_cb.doUdp = 1;
  8356. /* Setup internal and external cache */
  8357. switch (j) {
  8358. case 0:
  8359. /* SSL_OP_NO_TICKET stateful ticket case */
  8360. server_cb.ctx_ready = twcase_cache_intOn_extOn_noTicket;
  8361. break;
  8362. case 1:
  8363. server_cb.ctx_ready = twcase_cache_intOn_extOn;
  8364. break;
  8365. case 2:
  8366. server_cb.ctx_ready = twcase_cache_intOff_extOn;
  8367. break;
  8368. case 3:
  8369. server_cb.ctx_ready = twcase_cache_intOn_extOff;
  8370. break;
  8371. case 4:
  8372. server_cb.ctx_ready = twcase_cache_intOff_extOff;
  8373. break;
  8374. }
  8375. client_cb.ctx_ready = twcase_cache_intOff_extOff;
  8376. /* Add session to internal cache and save SSL session for testing */
  8377. server_cb.on_result = twcase_server_sess_ctx_pre_shutdown;
  8378. /* Save client SSL session for testing */
  8379. client_cb.on_result = twcase_client_sess_ctx_pre_shutdown;
  8380. server_cb.ticNoInit = 1; /* Use default builtin */
  8381. /* Don't free/release ctx */
  8382. server_cb.ctx = twcase_server_current_ctx_ptr;
  8383. server_cb.isSharedCtx = 1;
  8384. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  8385. &server_cb, NULL), TEST_SUCCESS);
  8386. ExpectIntEQ(twcase_get_session_called, 0);
  8387. if (EXPECT_FAIL()) {
  8388. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  8389. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  8390. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  8391. break;
  8392. }
  8393. switch (j) {
  8394. case 0:
  8395. case 1:
  8396. case 2:
  8397. /* cache cannot be searched with out a connection */
  8398. /* Add a new session */
  8399. ExpectIntEQ(twcase_new_session_called, 1);
  8400. /* In twcase_server_sess_ctx_pre_shutdown
  8401. * wolfSSL_CTX_add_session which evicts the existing session
  8402. * in cache and adds it back in */
  8403. ExpectIntLE(twcase_remove_session_called, 1);
  8404. break;
  8405. case 3:
  8406. case 4:
  8407. /* no external cache */
  8408. ExpectIntEQ(twcase_new_session_called, 0);
  8409. ExpectIntEQ(twcase_remove_session_called, 0);
  8410. break;
  8411. }
  8412. /* connection 2 - session resume */
  8413. fprintf(stderr, "\tresume: %s: j=%d\n", param->tls_version, j);
  8414. twcase_new_session_called = 0;
  8415. twcase_remove_session_called = 0;
  8416. twcase_get_session_called = 0;
  8417. server_cb.on_result = 0;
  8418. client_cb.on_result = 0;
  8419. server_cb.ticNoInit = 1; /* Use default builtin */
  8420. server_cb.ctx = twcase_server_current_ctx_ptr;
  8421. /* try session resumption */
  8422. client_cb.ssl_ready = twcase_client_set_sess_ssl_ready;
  8423. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  8424. &server_cb, NULL), TEST_SUCCESS);
  8425. /* Clear cache before checking */
  8426. wolfSSL_CTX_flush_sessions(NULL, -1);
  8427. switch (j) {
  8428. case 0:
  8429. if (tls13) {
  8430. /* (D)TLSv1.3 stateful case */
  8431. /* cache hit */
  8432. /* DTLS accesses cache once for stateless parsing and
  8433. * once for stateful parsing */
  8434. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8435. /* (D)TLSv1.3 creates a new ticket,
  8436. * updates both internal and external cache */
  8437. ExpectIntEQ(twcase_new_session_called, 1);
  8438. /* A new session ID is created for a new ticket */
  8439. ExpectIntEQ(twcase_remove_session_called, 2);
  8440. }
  8441. else {
  8442. /* non (D)TLSv1.3 case, no update */
  8443. /* DTLS accesses cache once for stateless parsing and
  8444. * once for stateful parsing */
  8445. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8446. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8447. #else
  8448. ExpectIntEQ(twcase_get_session_called, 1);
  8449. #endif
  8450. ExpectIntEQ(twcase_new_session_called, 0);
  8451. /* Called on session added in
  8452. * twcase_server_sess_ctx_pre_shutdown */
  8453. ExpectIntEQ(twcase_remove_session_called, 1);
  8454. }
  8455. break;
  8456. case 1:
  8457. if (tls13) {
  8458. /* (D)TLSv1.3 case */
  8459. /* cache hit */
  8460. ExpectIntEQ(twcase_get_session_called, 1);
  8461. /* (D)TLSv1.3 creates a new ticket,
  8462. * updates both internal and external cache */
  8463. ExpectIntEQ(twcase_new_session_called, 1);
  8464. /* Called on session added in
  8465. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  8466. ExpectIntEQ(twcase_remove_session_called, 1);
  8467. }
  8468. else {
  8469. /* non (D)TLSv1.3 case */
  8470. /* cache hit */
  8471. /* DTLS accesses cache once for stateless parsing and
  8472. * once for stateful parsing */
  8473. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8474. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8475. #else
  8476. ExpectIntEQ(twcase_get_session_called, 1);
  8477. #endif
  8478. ExpectIntEQ(twcase_new_session_called, 0);
  8479. /* Called on session added in
  8480. * twcase_server_sess_ctx_pre_shutdown */
  8481. ExpectIntEQ(twcase_remove_session_called, 1);
  8482. }
  8483. break;
  8484. case 2:
  8485. if (tls13) {
  8486. /* (D)TLSv1.3 case */
  8487. /* cache hit */
  8488. ExpectIntEQ(twcase_get_session_called, 1);
  8489. /* (D)TLSv1.3 creates a new ticket,
  8490. * updates both internal and external cache */
  8491. ExpectIntEQ(twcase_new_session_called, 1);
  8492. /* Called on session added in
  8493. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  8494. ExpectIntEQ(twcase_remove_session_called, 1);
  8495. }
  8496. else {
  8497. /* non (D)TLSv1.3 case */
  8498. /* cache hit */
  8499. /* DTLS accesses cache once for stateless parsing and
  8500. * once for stateful parsing */
  8501. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8502. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8503. #else
  8504. ExpectIntEQ(twcase_get_session_called, 1);
  8505. #endif
  8506. ExpectIntEQ(twcase_new_session_called, 0);
  8507. /* Called on session added in
  8508. * twcase_server_sess_ctx_pre_shutdown */
  8509. ExpectIntEQ(twcase_remove_session_called, 1);
  8510. }
  8511. break;
  8512. case 3:
  8513. case 4:
  8514. /* no external cache */
  8515. ExpectIntEQ(twcase_get_session_called, 0);
  8516. ExpectIntEQ(twcase_new_session_called, 0);
  8517. ExpectIntEQ(twcase_remove_session_called, 0);
  8518. break;
  8519. }
  8520. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  8521. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  8522. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  8523. if (EXPECT_FAIL())
  8524. break;
  8525. }
  8526. twcase_get_sessionCb_cleanup();
  8527. XMEMSET(&server_sessionCache.entries, 0,
  8528. sizeof(server_sessionCache.entries));
  8529. fprintf(stderr, "\tEnd %s\n", param->tls_version);
  8530. wc_FreeMutex(&server_sessionCache.htLock);
  8531. return EXPECT_RESULT();
  8532. }
  8533. #endif
  8534. static int test_wolfSSL_CTX_add_session_ext_tls13(void)
  8535. {
  8536. EXPECT_DECLS;
  8537. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8538. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8539. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8540. defined(HAVE_SESSION_TICKET) && \
  8541. !defined(TITAN_SESSION_CACHE) && \
  8542. !defined(HUGE_SESSION_CACHE) && \
  8543. !defined(BIG_SESSION_CACHE) && \
  8544. !defined(MEDIUM_SESSION_CACHE)
  8545. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  8546. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  8547. struct test_add_session_ext_params param[1] = {
  8548. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" }
  8549. };
  8550. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8551. #endif
  8552. #endif
  8553. return EXPECT_RESULT();
  8554. }
  8555. static int test_wolfSSL_CTX_add_session_ext_dtls13(void)
  8556. {
  8557. EXPECT_DECLS;
  8558. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8559. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8560. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8561. defined(HAVE_SESSION_TICKET) && \
  8562. !defined(TITAN_SESSION_CACHE) && \
  8563. !defined(HUGE_SESSION_CACHE) && \
  8564. !defined(BIG_SESSION_CACHE) && \
  8565. !defined(MEDIUM_SESSION_CACHE)
  8566. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  8567. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  8568. #ifdef WOLFSSL_DTLS13
  8569. struct test_add_session_ext_params param[1] = {
  8570. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" }
  8571. };
  8572. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8573. #endif
  8574. #endif
  8575. #endif
  8576. return EXPECT_RESULT();
  8577. }
  8578. static int test_wolfSSL_CTX_add_session_ext_tls12(void)
  8579. {
  8580. EXPECT_DECLS;
  8581. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8582. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8583. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8584. defined(HAVE_SESSION_TICKET) && \
  8585. !defined(TITAN_SESSION_CACHE) && \
  8586. !defined(HUGE_SESSION_CACHE) && \
  8587. !defined(BIG_SESSION_CACHE) && \
  8588. !defined(MEDIUM_SESSION_CACHE)
  8589. #ifndef WOLFSSL_NO_TLS12
  8590. struct test_add_session_ext_params param[1] = {
  8591. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" }
  8592. };
  8593. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8594. #endif
  8595. #endif
  8596. return EXPECT_RESULT();
  8597. }
  8598. static int test_wolfSSL_CTX_add_session_ext_dtls12(void)
  8599. {
  8600. EXPECT_DECLS;
  8601. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8602. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8603. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8604. defined(HAVE_SESSION_TICKET) && \
  8605. !defined(TITAN_SESSION_CACHE) && \
  8606. !defined(HUGE_SESSION_CACHE) && \
  8607. !defined(BIG_SESSION_CACHE) && \
  8608. !defined(MEDIUM_SESSION_CACHE)
  8609. #ifndef WOLFSSL_NO_TLS12
  8610. #ifdef WOLFSSL_DTLS
  8611. struct test_add_session_ext_params param[1] = {
  8612. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" }
  8613. };
  8614. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8615. #endif
  8616. #endif
  8617. #endif
  8618. return EXPECT_RESULT();
  8619. }
  8620. static int test_wolfSSL_CTX_add_session_ext_tls11(void)
  8621. {
  8622. EXPECT_DECLS;
  8623. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8624. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8625. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8626. defined(HAVE_SESSION_TICKET) && \
  8627. !defined(TITAN_SESSION_CACHE) && \
  8628. !defined(HUGE_SESSION_CACHE) && \
  8629. !defined(BIG_SESSION_CACHE) && \
  8630. !defined(MEDIUM_SESSION_CACHE)
  8631. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  8632. !defined(NO_DES3))
  8633. struct test_add_session_ext_params param[1] = {
  8634. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" }
  8635. };
  8636. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8637. #endif
  8638. #endif
  8639. return EXPECT_RESULT();
  8640. }
  8641. static int test_wolfSSL_CTX_add_session_ext_dtls1(void)
  8642. {
  8643. EXPECT_DECLS;
  8644. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8645. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8646. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8647. defined(HAVE_SESSION_TICKET) && \
  8648. !defined(TITAN_SESSION_CACHE) && \
  8649. !defined(HUGE_SESSION_CACHE) && \
  8650. !defined(BIG_SESSION_CACHE) && \
  8651. !defined(MEDIUM_SESSION_CACHE)
  8652. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  8653. !defined(NO_DES3))
  8654. #ifdef WOLFSSL_DTLS
  8655. struct test_add_session_ext_params param[1] = {
  8656. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" }
  8657. };
  8658. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8659. #endif
  8660. #endif
  8661. #endif
  8662. return EXPECT_RESULT();
  8663. }
  8664. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  8665. /* canned export of a session using older version 3 */
  8666. static unsigned char version_3[] = {
  8667. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  8668. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  8669. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  8670. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  8671. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  8672. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8673. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  8674. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  8675. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  8676. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8677. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  8678. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  8679. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8680. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  8681. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  8682. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  8683. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  8684. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  8685. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  8686. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  8687. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8688. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8689. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8690. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8691. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8692. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8693. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8694. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8695. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8696. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8697. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8698. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  8699. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  8700. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  8701. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  8702. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  8703. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  8704. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  8705. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  8706. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  8707. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  8708. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8709. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  8710. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  8711. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8712. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  8713. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  8714. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  8715. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  8716. 0x2E, 0x31, 0xED, 0x4F
  8717. };
  8718. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  8719. static int test_wolfSSL_dtls_export(void)
  8720. {
  8721. EXPECT_DECLS;
  8722. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  8723. tcp_ready ready;
  8724. func_args client_args;
  8725. func_args server_args;
  8726. THREAD_TYPE serverThread;
  8727. callback_functions server_cbf;
  8728. callback_functions client_cbf;
  8729. #ifdef WOLFSSL_TIRTOS
  8730. fdOpenSession(Task_self());
  8731. #endif
  8732. InitTcpReady(&ready);
  8733. #if defined(USE_WINDOWS_API)
  8734. /* use RNG to get random port if using windows */
  8735. ready.port = GetRandomPort();
  8736. #endif
  8737. /* set using dtls */
  8738. XMEMSET(&client_args, 0, sizeof(func_args));
  8739. XMEMSET(&server_args, 0, sizeof(func_args));
  8740. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8741. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  8742. server_cbf.method = wolfDTLSv1_2_server_method;
  8743. client_cbf.method = wolfDTLSv1_2_client_method;
  8744. server_args.callbacks = &server_cbf;
  8745. client_args.callbacks = &client_cbf;
  8746. server_args.signal = &ready;
  8747. client_args.signal = &ready;
  8748. start_thread(run_wolfssl_server, &server_args, &serverThread);
  8749. wait_tcp_ready(&server_args);
  8750. run_wolfssl_client(&client_args);
  8751. join_thread(serverThread);
  8752. ExpectTrue(client_args.return_code);
  8753. ExpectTrue(server_args.return_code);
  8754. FreeTcpReady(&ready);
  8755. #ifdef WOLFSSL_TIRTOS
  8756. fdOpenSession(Task_self());
  8757. #endif
  8758. if (EXPECT_SUCCESS()) {
  8759. SOCKET_T sockfd = 0;
  8760. WOLFSSL_CTX* ctx = NULL;
  8761. WOLFSSL* ssl = NULL;
  8762. char msg[64] = "hello wolfssl!";
  8763. char reply[1024];
  8764. int msgSz = (int)XSTRLEN(msg);
  8765. byte *session, *window;
  8766. unsigned int sessionSz = 0;
  8767. unsigned int windowSz = 0;
  8768. #ifndef TEST_IPV6
  8769. struct sockaddr_in peerAddr;
  8770. #else
  8771. struct sockaddr_in6 peerAddr;
  8772. #endif /* TEST_IPV6 */
  8773. int i;
  8774. /* Set ctx to DTLS 1.2 */
  8775. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  8776. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8777. /* test importing version 3 */
  8778. ExpectIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8779. /* test importing bad length and bad version */
  8780. version_3[2] += 1;
  8781. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8782. version_3[2] -= 1; version_3[1] = 0XA0;
  8783. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8784. wolfSSL_free(ssl);
  8785. wolfSSL_CTX_free(ctx);
  8786. /* check storing client state after connection and storing window only */
  8787. #ifdef WOLFSSL_TIRTOS
  8788. fdOpenSession(Task_self());
  8789. #endif
  8790. InitTcpReady(&ready);
  8791. #if defined(USE_WINDOWS_API)
  8792. /* use RNG to get random port if using windows */
  8793. ready.port = GetRandomPort();
  8794. #endif
  8795. /* set using dtls */
  8796. XMEMSET(&server_args, 0, sizeof(func_args));
  8797. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8798. server_cbf.method = wolfDTLSv1_2_server_method;
  8799. server_cbf.doUdp = 1;
  8800. server_args.callbacks = &server_cbf;
  8801. server_args.argc = 3; /* set loop_count to 3 */
  8802. server_args.signal = &ready;
  8803. start_thread(test_server_nofail, &server_args, &serverThread);
  8804. wait_tcp_ready(&server_args);
  8805. /* create and connect with client */
  8806. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  8807. ExpectIntEQ(WOLFSSL_SUCCESS,
  8808. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  8809. ExpectIntEQ(WOLFSSL_SUCCESS,
  8810. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  8811. ExpectIntEQ(WOLFSSL_SUCCESS,
  8812. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  8813. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  8814. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8815. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8816. /* store server information connected too */
  8817. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  8818. #ifndef TEST_IPV6
  8819. peerAddr.sin_family = AF_INET;
  8820. ExpectIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  8821. peerAddr.sin_port = XHTONS(server_args.signal->port);
  8822. #else
  8823. peerAddr.sin6_family = AF_INET6;
  8824. ExpectIntEQ(
  8825. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  8826. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  8827. #endif
  8828. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8829. WOLFSSL_SUCCESS);
  8830. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  8831. ExpectIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  8832. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8833. ExpectIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  8834. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8835. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8836. ExpectIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  8837. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8838. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8839. wolfSSL_free(ssl);
  8840. for (i = 1; EXPECT_SUCCESS() && i < server_args.argc; i++) {
  8841. /* restore state */
  8842. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8843. ExpectIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  8844. ExpectIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  8845. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8846. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8847. WOLFSSL_SUCCESS);
  8848. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8849. ExpectIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8850. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8851. wolfSSL_free(ssl);
  8852. }
  8853. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8854. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8855. wolfSSL_CTX_free(ctx);
  8856. fprintf(stderr, "done and waiting for server\n");
  8857. join_thread(serverThread);
  8858. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  8859. FreeTcpReady(&ready);
  8860. #ifdef WOLFSSL_TIRTOS
  8861. fdOpenSession(Task_self());
  8862. #endif
  8863. }
  8864. #endif
  8865. return EXPECT_RESULT();
  8866. }
  8867. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  8868. #ifdef WOLFSSL_TLS13
  8869. static const byte canned_client_tls13_session[] = {
  8870. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  8871. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8872. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  8873. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8874. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8875. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8876. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8877. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  8878. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8879. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8880. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8881. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  8882. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8883. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8884. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8885. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8886. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8887. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8888. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8889. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8890. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8891. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8892. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8893. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8894. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8895. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8896. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8897. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8898. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8899. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  8900. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  8901. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  8902. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8903. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8904. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8905. 0x00, 0x03
  8906. };
  8907. static const byte canned_server_tls13_session[] = {
  8908. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  8909. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8910. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  8911. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8912. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8913. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8914. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8915. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  8916. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8917. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8918. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8919. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  8920. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8921. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8922. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8923. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8924. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8925. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8926. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8927. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8928. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8929. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8930. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8931. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8932. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8933. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8934. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8935. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8936. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8937. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  8938. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  8939. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  8940. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8941. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8942. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8943. 0x00, 0x04
  8944. };
  8945. #endif /* WOLFSSL_TLS13 */
  8946. static const byte canned_client_session[] = {
  8947. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8948. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8949. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  8950. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8951. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8952. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8953. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8954. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  8955. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8956. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8957. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8958. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  8959. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  8960. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  8961. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  8962. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  8963. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  8964. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  8965. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  8966. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  8967. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  8968. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  8969. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  8970. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  8971. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  8972. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  8973. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  8974. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  8975. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  8976. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8977. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8978. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8979. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8980. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  8981. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  8982. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  8983. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  8984. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  8985. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  8986. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8987. 0x00, 0x03
  8988. };
  8989. static const byte canned_server_session[] = {
  8990. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8991. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8992. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  8993. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8994. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  8995. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8996. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8997. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  8998. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8999. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9000. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9001. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  9002. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  9003. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  9004. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  9005. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  9006. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  9007. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  9008. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  9009. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  9010. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  9011. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  9012. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  9013. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  9014. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  9015. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  9016. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  9017. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  9018. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  9019. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  9020. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9021. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9022. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9023. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  9024. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  9025. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  9026. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  9027. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  9028. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  9029. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  9030. 0x00, 0x04
  9031. };
  9032. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  9033. {
  9034. SOCKET_T sockfd = 0;
  9035. SOCKET_T clientfd = 0;
  9036. word16 port;
  9037. callback_functions* cbf;
  9038. WOLFSSL_CTX* ctx = 0;
  9039. WOLFSSL* ssl = 0;
  9040. char msg[] = "I hear you fa shizzle!";
  9041. char input[1024];
  9042. int idx;
  9043. #ifdef WOLFSSL_TIRTOS
  9044. fdOpenSession(Task_self());
  9045. #endif
  9046. ((func_args*)args)->return_code = TEST_FAIL;
  9047. cbf = ((func_args*)args)->callbacks;
  9048. #if defined(USE_WINDOWS_API)
  9049. port = ((func_args*)args)->signal->port;
  9050. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  9051. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  9052. /* Let tcp_listen assign port */
  9053. port = 0;
  9054. #else
  9055. /* Use default port */
  9056. port = wolfSSLPort;
  9057. #endif
  9058. /* do it here to detect failure */
  9059. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  9060. CloseSocket(sockfd);
  9061. {
  9062. WOLFSSL_METHOD* method = NULL;
  9063. if (cbf != NULL && cbf->method != NULL) {
  9064. method = cbf->method();
  9065. }
  9066. else {
  9067. method = wolfTLSv1_2_server_method();
  9068. }
  9069. ctx = wolfSSL_CTX_new(method);
  9070. }
  9071. if (ctx == NULL) {
  9072. goto done;
  9073. }
  9074. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  9075. /* call ctx setup callback */
  9076. if (cbf != NULL && cbf->ctx_ready != NULL) {
  9077. cbf->ctx_ready(ctx);
  9078. }
  9079. ssl = wolfSSL_new(ctx);
  9080. if (ssl == NULL) {
  9081. goto done;
  9082. }
  9083. wolfSSL_set_fd(ssl, clientfd);
  9084. /* call ssl setup callback */
  9085. if (cbf != NULL && cbf->ssl_ready != NULL) {
  9086. cbf->ssl_ready(ssl);
  9087. }
  9088. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  9089. if (idx > 0) {
  9090. input[idx] = '\0';
  9091. fprintf(stderr, "Client message export/import: %s\n", input);
  9092. }
  9093. else {
  9094. fprintf(stderr, "ret = %d error = %d\n", idx,
  9095. wolfSSL_get_error(ssl, idx));
  9096. goto done;
  9097. }
  9098. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  9099. /*err_sys("SSL_write failed");*/
  9100. WOLFSSL_RETURN_FROM_THREAD(0);
  9101. }
  9102. #ifdef WOLFSSL_TIRTOS
  9103. Task_yield();
  9104. #endif
  9105. ((func_args*)args)->return_code = TEST_SUCCESS;
  9106. done:
  9107. wolfSSL_shutdown(ssl);
  9108. wolfSSL_free(ssl);
  9109. wolfSSL_CTX_free(ctx);
  9110. CloseSocket(clientfd);
  9111. #ifdef WOLFSSL_TIRTOS
  9112. fdCloseSession(Task_self());
  9113. #endif
  9114. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  9115. && defined(HAVE_THREAD_LS)
  9116. wc_ecc_fp_free(); /* free per thread cache */
  9117. #endif
  9118. #if defined(HAVE_SESSION_TICKET) && \
  9119. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  9120. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  9121. OpenSSLTicketCleanup();
  9122. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  9123. TicketCleanup();
  9124. #endif
  9125. #endif
  9126. WOLFSSL_RETURN_FROM_THREAD(0);
  9127. }
  9128. static void load_tls12_canned_server(WOLFSSL* ssl)
  9129. {
  9130. int clientfd = wolfSSL_get_fd(ssl);
  9131. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  9132. sizeof(canned_server_session)), sizeof(canned_server_session));
  9133. wolfSSL_set_fd(ssl, clientfd);
  9134. }
  9135. #ifdef WOLFSSL_TLS13
  9136. static void load_tls13_canned_server(WOLFSSL* ssl)
  9137. {
  9138. int clientfd = wolfSSL_get_fd(ssl);
  9139. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  9140. sizeof(canned_server_tls13_session)),
  9141. sizeof(canned_server_tls13_session));
  9142. wolfSSL_set_fd(ssl, clientfd);
  9143. }
  9144. #endif
  9145. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  9146. static int test_wolfSSL_tls_export_run(int v)
  9147. {
  9148. EXPECT_DECLS;
  9149. SOCKET_T sockfd = 0;
  9150. WOLFSSL_CTX* ctx = 0;
  9151. WOLFSSL* ssl = 0;
  9152. char msg[64] = "hello wolfssl!";
  9153. char reply[1024];
  9154. word32 replySz;
  9155. int msgSz = (int)XSTRLEN(msg);
  9156. const byte* clientSession = NULL;
  9157. int clientSessionSz = 0;
  9158. tcp_ready ready;
  9159. func_args server_args;
  9160. THREAD_TYPE serverThread;
  9161. callback_functions server_cbf;
  9162. #ifdef WOLFSSL_TIRTOS
  9163. fdOpenSession(Task_self());
  9164. #endif
  9165. InitTcpReady(&ready);
  9166. #if defined(USE_WINDOWS_API)
  9167. /* use RNG to get random port if using windows */
  9168. ready.port = GetRandomPort();
  9169. #endif
  9170. XMEMSET(&server_args, 0, sizeof(func_args));
  9171. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  9172. switch (v) {
  9173. case WOLFSSL_TLSV1_2:
  9174. server_cbf.method = wolfTLSv1_2_server_method;
  9175. server_cbf.ssl_ready = load_tls12_canned_server;
  9176. /* setup the client side */
  9177. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  9178. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  9179. clientSession = canned_client_session;
  9180. clientSessionSz = sizeof(canned_client_session);
  9181. break;
  9182. #ifdef WOLFSSL_TLS13
  9183. case WOLFSSL_TLSV1_3:
  9184. server_cbf.method = wolfTLSv1_3_server_method;
  9185. server_cbf.ssl_ready = load_tls13_canned_server;
  9186. /* setup the client side */
  9187. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  9188. clientSession = canned_client_tls13_session;
  9189. clientSessionSz = sizeof(canned_client_tls13_session);
  9190. break;
  9191. #endif
  9192. }
  9193. server_args.callbacks = &server_cbf;
  9194. server_args.signal = &ready;
  9195. start_thread(tls_export_server, &server_args, &serverThread);
  9196. wait_tcp_ready(&server_args);
  9197. #ifdef WOLFSSL_TIRTOS
  9198. fdOpenSession(Task_self());
  9199. #endif
  9200. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9201. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  9202. ExpectIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  9203. clientSessionSz);
  9204. replySz = sizeof(reply);
  9205. ExpectIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  9206. #if !defined(NO_PSK) && defined(HAVE_ANON)
  9207. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  9208. ExpectIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  9209. #endif
  9210. wolfSSL_set_fd(ssl, sockfd);
  9211. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  9212. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  9213. wolfSSL_free(ssl);
  9214. wolfSSL_CTX_free(ctx);
  9215. CloseSocket(sockfd);
  9216. #ifdef WOLFSSL_TIRTOS
  9217. fdCloseSession(Task_self());
  9218. #endif
  9219. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  9220. && defined(HAVE_THREAD_LS)
  9221. wc_ecc_fp_free(); /* free per thread cache */
  9222. #endif
  9223. join_thread(serverThread);
  9224. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  9225. FreeTcpReady(&ready);
  9226. #ifdef WOLFSSL_TIRTOS
  9227. fdOpenSession(Task_self());
  9228. #endif
  9229. return EXPECT_RESULT();
  9230. }
  9231. #endif
  9232. static int test_wolfSSL_tls_export(void)
  9233. {
  9234. int res = TEST_SKIPPED;
  9235. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  9236. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  9237. #ifdef WOLFSSL_TLS13
  9238. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  9239. #endif
  9240. res = TEST_RES_CHECK(1);
  9241. #endif
  9242. return res;
  9243. }
  9244. /*----------------------------------------------------------------------------*
  9245. | TLS extensions tests
  9246. *----------------------------------------------------------------------------*/
  9247. #ifdef ENABLE_TLS_CALLBACK_TEST
  9248. /* Connection test runner - generic */
  9249. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  9250. callback_functions* server_callbacks)
  9251. {
  9252. tcp_ready ready;
  9253. func_args client_args;
  9254. func_args server_args;
  9255. THREAD_TYPE serverThread;
  9256. XMEMSET(&client_args, 0, sizeof(func_args));
  9257. XMEMSET(&server_args, 0, sizeof(func_args));
  9258. StartTCP();
  9259. client_args.callbacks = client_callbacks;
  9260. server_args.callbacks = server_callbacks;
  9261. #ifdef WOLFSSL_TIRTOS
  9262. fdOpenSession(Task_self());
  9263. #endif
  9264. /* RUN Server side */
  9265. InitTcpReady(&ready);
  9266. #if defined(USE_WINDOWS_API)
  9267. /* use RNG to get random port if using windows */
  9268. ready.port = GetRandomPort();
  9269. #endif
  9270. server_args.signal = &ready;
  9271. client_args.signal = &ready;
  9272. start_thread(run_wolfssl_server, &server_args, &serverThread);
  9273. wait_tcp_ready(&server_args);
  9274. /* RUN Client side */
  9275. run_wolfssl_client(&client_args);
  9276. join_thread(serverThread);
  9277. FreeTcpReady(&ready);
  9278. #ifdef WOLFSSL_TIRTOS
  9279. fdCloseSession(Task_self());
  9280. #endif
  9281. client_callbacks->return_code = client_args.return_code;
  9282. server_callbacks->return_code = server_args.return_code;
  9283. }
  9284. #endif /* ENABLE_TLS_CALLBACK_TEST */
  9285. #ifdef HAVE_SNI
  9286. static int test_wolfSSL_UseSNI_params(void)
  9287. {
  9288. EXPECT_DECLS;
  9289. #if !defined(NO_WOLFSSL_CLIENT)
  9290. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9291. WOLFSSL *ssl = wolfSSL_new(ctx);
  9292. ExpectNotNull(ctx);
  9293. ExpectNotNull(ssl);
  9294. /* invalid [ctx|ssl] */
  9295. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  9296. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  9297. /* invalid type */
  9298. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  9299. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  9300. /* invalid data */
  9301. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  9302. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  9303. /* success case */
  9304. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  9305. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  9306. wolfSSL_free(ssl);
  9307. wolfSSL_CTX_free(ctx);
  9308. #endif /* !NO_WOLFSSL_CLIENT */
  9309. return EXPECT_RESULT();
  9310. }
  9311. /* BEGIN of connection tests callbacks */
  9312. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9313. {
  9314. AssertIntEQ(WOLFSSL_SUCCESS,
  9315. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  9316. }
  9317. static void use_SNI_at_ssl(WOLFSSL* ssl)
  9318. {
  9319. AssertIntEQ(WOLFSSL_SUCCESS,
  9320. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  9321. }
  9322. static void different_SNI_at_ssl(WOLFSSL* ssl)
  9323. {
  9324. AssertIntEQ(WOLFSSL_SUCCESS,
  9325. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  9326. }
  9327. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  9328. {
  9329. use_SNI_at_ssl(ssl);
  9330. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9331. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  9332. }
  9333. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  9334. {
  9335. use_SNI_at_ssl(ssl);
  9336. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9337. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  9338. }
  9339. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9340. {
  9341. use_SNI_at_ctx(ctx);
  9342. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  9343. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9344. }
  9345. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  9346. {
  9347. use_SNI_at_ssl(ssl);
  9348. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9349. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9350. }
  9351. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9352. {
  9353. use_SNI_at_ctx(ctx);
  9354. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  9355. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9356. }
  9357. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  9358. {
  9359. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  9360. }
  9361. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  9362. {
  9363. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  9364. }
  9365. static void verify_SNI_no_matching(WOLFSSL* ssl)
  9366. {
  9367. byte type = WOLFSSL_SNI_HOST_NAME;
  9368. void* request = (void*) &type; /* to be overwritten */
  9369. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  9370. AssertNotNull(request);
  9371. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9372. AssertNull(request);
  9373. }
  9374. static void verify_SNI_real_matching(WOLFSSL* ssl)
  9375. {
  9376. byte type = WOLFSSL_SNI_HOST_NAME;
  9377. void* request = NULL;
  9378. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  9379. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9380. AssertNotNull(request);
  9381. AssertStrEQ("www.wolfssl.com", (char*)request);
  9382. }
  9383. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  9384. {
  9385. byte type = WOLFSSL_SNI_HOST_NAME;
  9386. void* request = NULL;
  9387. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  9388. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9389. AssertNotNull(request);
  9390. AssertStrEQ("ww2.wolfssl.com", (char*)request);
  9391. }
  9392. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  9393. {
  9394. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  9395. }
  9396. /* END of connection tests callbacks */
  9397. static int test_wolfSSL_UseSNI_connection(void)
  9398. {
  9399. int res = TEST_SKIPPED;
  9400. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9401. callback_functions client_cb;
  9402. callback_functions server_cb;
  9403. size_t i;
  9404. #ifdef WOLFSSL_STATIC_MEMORY
  9405. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  9406. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  9407. #endif
  9408. struct {
  9409. method_provider client_meth;
  9410. method_provider server_meth;
  9411. #ifdef WOLFSSL_STATIC_MEMORY
  9412. wolfSSL_method_func client_meth_ex;
  9413. wolfSSL_method_func server_meth_ex;
  9414. #endif
  9415. } methods[] = {
  9416. #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
  9417. {wolfSSLv23_client_method, wolfSSLv23_server_method
  9418. #ifdef WOLFSSL_STATIC_MEMORY
  9419. ,wolfSSLv23_client_method_ex, wolfSSLv23_server_method_ex
  9420. #endif
  9421. },
  9422. #endif
  9423. #ifndef WOLFSSL_NO_TLS12
  9424. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method
  9425. #ifdef WOLFSSL_STATIC_MEMORY
  9426. ,wolfTLSv1_2_client_method_ex, wolfTLSv1_2_server_method_ex
  9427. #endif
  9428. },
  9429. #endif
  9430. #ifdef WOLFSSL_TLS13
  9431. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method
  9432. #ifdef WOLFSSL_STATIC_MEMORY
  9433. ,wolfTLSv1_3_client_method_ex, wolfTLSv1_3_server_method_ex
  9434. #endif
  9435. },
  9436. #endif
  9437. };
  9438. size_t methodsSz = sizeof(methods) / sizeof(*methods);
  9439. for (i = 0; i < methodsSz; i++) {
  9440. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9441. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9442. client_cb.method = methods[i].client_meth;
  9443. server_cb.method = methods[i].server_meth;
  9444. client_cb.devId = testDevId;
  9445. server_cb.devId = testDevId;
  9446. #ifdef WOLFSSL_STATIC_MEMORY
  9447. client_cb.method_ex = methods[i].client_meth_ex;
  9448. server_cb.method_ex = methods[i].server_meth_ex;
  9449. client_cb.mem = cliMem;
  9450. client_cb.memSz = (word32)sizeof(cliMem);
  9451. server_cb.mem = svrMem;
  9452. server_cb.memSz = (word32)sizeof(svrMem);;
  9453. #endif
  9454. /* success case at ctx */
  9455. fprintf(stderr, "\n\tsuccess case at ctx\n");
  9456. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9457. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  9458. test_wolfSSL_client_server(&client_cb, &server_cb);
  9459. /* success case at ssl */
  9460. fprintf(stderr, "\tsuccess case at ssl\n");
  9461. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  9462. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  9463. test_wolfSSL_client_server(&client_cb, &server_cb);
  9464. /* default mismatch behavior */
  9465. fprintf(stderr, "\tdefault mismatch behavior\n");
  9466. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9467. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  9468. test_wolfSSL_client_server(&client_cb, &server_cb);
  9469. /* continue on mismatch */
  9470. fprintf(stderr, "\tcontinue on mismatch\n");
  9471. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9472. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  9473. test_wolfSSL_client_server(&client_cb, &server_cb);
  9474. /* fake answer on mismatch */
  9475. fprintf(stderr, "\tfake answer on mismatch\n");
  9476. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9477. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  9478. test_wolfSSL_client_server(&client_cb, &server_cb);
  9479. /* sni abort - success */
  9480. fprintf(stderr, "\tsni abort - success\n");
  9481. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9482. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  9483. test_wolfSSL_client_server(&client_cb, &server_cb);
  9484. /* sni abort - abort when absent (ctx) */
  9485. fprintf(stderr, "\tsni abort - abort when absent (ctx)\n");
  9486. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9487. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  9488. test_wolfSSL_client_server(&client_cb, &server_cb);
  9489. /* sni abort - abort when absent (ssl) */
  9490. fprintf(stderr, "\tsni abort - abort when absent (ssl)\n");
  9491. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9492. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  9493. test_wolfSSL_client_server(&client_cb, &server_cb);
  9494. /* sni abort - success when overwritten */
  9495. fprintf(stderr, "\tsni abort - success when overwritten\n");
  9496. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9497. 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;
  9498. test_wolfSSL_client_server(&client_cb, &server_cb);
  9499. /* sni abort - success when allowing mismatches */
  9500. fprintf(stderr, "\tsni abort - success when allowing mismatches\n");
  9501. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9502. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  9503. test_wolfSSL_client_server(&client_cb, &server_cb);
  9504. }
  9505. res = TEST_RES_CHECK(1);
  9506. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9507. return res;
  9508. }
  9509. static int test_wolfSSL_SNI_GetFromBuffer(void)
  9510. {
  9511. EXPECT_DECLS;
  9512. byte buff[] = { /* www.paypal.com */
  9513. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  9514. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  9515. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  9516. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  9517. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  9518. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  9519. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  9520. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  9521. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  9522. };
  9523. byte buff2[] = { /* api.textmate.org */
  9524. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  9525. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  9526. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  9527. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  9528. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  9529. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  9530. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  9531. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  9532. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  9533. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  9534. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  9535. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  9536. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  9537. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  9538. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  9539. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  9540. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  9541. };
  9542. byte buff3[] = { /* no sni extension */
  9543. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  9544. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  9545. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  9546. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  9547. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  9548. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  9549. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  9550. };
  9551. byte buff4[] = { /* last extension has zero size */
  9552. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  9553. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  9554. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  9555. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  9556. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  9557. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  9558. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  9559. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  9560. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  9561. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  9562. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  9563. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  9564. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  9565. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  9566. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  9567. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  9568. 0x12, 0x00, 0x00
  9569. };
  9570. byte buff5[] = { /* SSL v2.0 client hello */
  9571. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  9572. /* dummy bytes below, just to pass size check */
  9573. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  9574. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  9575. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  9576. };
  9577. byte result[32] = {0};
  9578. word32 length = 32;
  9579. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  9580. 0, result, &length));
  9581. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  9582. 0, result, &length));
  9583. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  9584. 1, result, &length));
  9585. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9586. 0, result, &length));
  9587. buff[0] = 0x16;
  9588. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9589. 0, result, &length));
  9590. buff[1] = 0x03;
  9591. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  9592. sizeof(buff), 0, result, &length));
  9593. buff[2] = 0x03;
  9594. ExpectIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  9595. sizeof(buff), 0, result, &length));
  9596. buff[4] = 0x64;
  9597. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9598. 0, result, &length));
  9599. if (EXPECT_SUCCESS())
  9600. result[length] = 0;
  9601. ExpectStrEQ("www.paypal.com", (const char*) result);
  9602. length = 32;
  9603. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  9604. 0, result, &length));
  9605. if (EXPECT_SUCCESS())
  9606. result[length] = 0;
  9607. ExpectStrEQ("api.textmate.org", (const char*) result);
  9608. /* SSL v2.0 tests */
  9609. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  9610. sizeof(buff5), 0, result, &length));
  9611. buff5[2] = 0x02;
  9612. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9613. sizeof(buff5), 0, result, &length));
  9614. buff5[2] = 0x01; buff5[6] = 0x08;
  9615. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9616. sizeof(buff5), 0, result, &length));
  9617. buff5[6] = 0x09; buff5[8] = 0x01;
  9618. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9619. sizeof(buff5), 0, result, &length));
  9620. return EXPECT_RESULT();
  9621. }
  9622. #endif /* HAVE_SNI */
  9623. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  9624. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
  9625. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  9626. /* Dummy peer functions to satisfy the exporter/importer */
  9627. static int test_wolfSSL_dtls_export_peers_get_peer(WOLFSSL* ssl, char* ip,
  9628. int* ipSz, unsigned short* port, int* fam)
  9629. {
  9630. (void)ssl;
  9631. ip[0] = -1;
  9632. *ipSz = 1;
  9633. *port = 1;
  9634. *fam = 2;
  9635. return 1;
  9636. }
  9637. static int test_wolfSSL_dtls_export_peers_set_peer(WOLFSSL* ssl, char* ip,
  9638. int ipSz, unsigned short port, int fam)
  9639. {
  9640. (void)ssl;
  9641. if (ip[0] != -1 || ipSz != 1 || port != 1 || fam != 2)
  9642. return 0;
  9643. return 1;
  9644. }
  9645. static int test_wolfSSL_dtls_export_peers_on_handshake(WOLFSSL_CTX **ctx,
  9646. WOLFSSL **ssl)
  9647. {
  9648. EXPECT_DECLS;
  9649. unsigned char* sessionBuf = NULL;
  9650. unsigned int sessionSz = 0;
  9651. void* ioWriteCtx = wolfSSL_GetIOWriteCtx(*ssl);
  9652. void* ioReadCtx = wolfSSL_GetIOReadCtx(*ssl);
  9653. wolfSSL_CTX_SetIOGetPeer(*ctx, test_wolfSSL_dtls_export_peers_get_peer);
  9654. wolfSSL_CTX_SetIOSetPeer(*ctx, test_wolfSSL_dtls_export_peers_set_peer);
  9655. ExpectIntGE(wolfSSL_dtls_export(*ssl, NULL, &sessionSz), 0);
  9656. ExpectNotNull(sessionBuf =
  9657. (unsigned char*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  9658. ExpectIntGE(wolfSSL_dtls_export(*ssl, sessionBuf, &sessionSz), 0);
  9659. wolfSSL_free(*ssl);
  9660. *ssl = NULL;
  9661. ExpectNotNull(*ssl = wolfSSL_new(*ctx));
  9662. ExpectIntGE(wolfSSL_dtls_import(*ssl, sessionBuf, sessionSz), 0);
  9663. wolfSSL_SetIOWriteCtx(*ssl, ioWriteCtx);
  9664. wolfSSL_SetIOReadCtx(*ssl, ioReadCtx);
  9665. XFREE(sessionBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9666. return EXPECT_RESULT();
  9667. }
  9668. #endif
  9669. static int test_wolfSSL_dtls_export_peers(void)
  9670. {
  9671. EXPECT_DECLS;
  9672. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
  9673. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  9674. test_ssl_cbf client_cbf;
  9675. test_ssl_cbf server_cbf;
  9676. size_t i, j;
  9677. struct test_params {
  9678. method_provider client_meth;
  9679. method_provider server_meth;
  9680. const char* dtls_version;
  9681. } params[] = {
  9682. #ifndef NO_OLD_TLS
  9683. {wolfDTLSv1_client_method, wolfDTLSv1_server_method, "1.0"},
  9684. #endif
  9685. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "1.2"},
  9686. /* TODO DTLS 1.3 exporting not supported
  9687. #ifdef WOLFSSL_DTLS13
  9688. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "1.3"},
  9689. #endif
  9690. */
  9691. };
  9692. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  9693. for (j = 0; j <= 0b11; j++) {
  9694. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  9695. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  9696. printf("\n\tTesting DTLS %s connection;", params[i].dtls_version);
  9697. client_cbf.method = params[i].client_meth;
  9698. server_cbf.method = params[i].server_meth;
  9699. if (j & 0b01) {
  9700. client_cbf.on_handshake =
  9701. test_wolfSSL_dtls_export_peers_on_handshake;
  9702. printf(" With client export;");
  9703. }
  9704. if (j & 0b10) {
  9705. server_cbf.on_handshake =
  9706. test_wolfSSL_dtls_export_peers_on_handshake;
  9707. printf(" With server export;");
  9708. }
  9709. printf("\n");
  9710. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  9711. &server_cbf, NULL), TEST_SUCCESS);
  9712. if (!EXPECT_SUCCESS())
  9713. break;
  9714. }
  9715. }
  9716. #endif
  9717. return EXPECT_RESULT();
  9718. }
  9719. static int test_wolfSSL_UseTrustedCA(void)
  9720. {
  9721. EXPECT_DECLS;
  9722. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  9723. && !defined(NO_RSA)
  9724. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9725. WOLFSSL_CTX *ctx = NULL;
  9726. WOLFSSL *ssl = NULL;
  9727. byte id[20];
  9728. #ifndef NO_WOLFSSL_SERVER
  9729. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  9730. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9731. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9732. #else
  9733. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  9734. #endif
  9735. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  9736. XMEMSET(id, 0, sizeof(id));
  9737. /* error cases */
  9738. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  9739. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9740. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  9741. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9742. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  9743. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9744. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  9745. #ifdef NO_SHA
  9746. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9747. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  9748. #endif
  9749. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9750. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  9751. /* success cases */
  9752. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9753. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  9754. #ifndef NO_SHA
  9755. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9756. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  9757. #endif
  9758. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9759. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  9760. wolfSSL_free(ssl);
  9761. wolfSSL_CTX_free(ctx);
  9762. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9763. #endif /* HAVE_TRUSTED_CA */
  9764. return EXPECT_RESULT();
  9765. }
  9766. static int test_wolfSSL_UseMaxFragment(void)
  9767. {
  9768. EXPECT_DECLS;
  9769. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  9770. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  9771. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9772. #ifndef NO_WOLFSSL_SERVER
  9773. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  9774. #else
  9775. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9776. #endif
  9777. WOLFSSL *ssl = NULL;
  9778. #ifdef OPENSSL_EXTRA
  9779. int (*UseMaxFragment)(SSL *s, uint8_t mode);
  9780. int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
  9781. #else
  9782. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  9783. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  9784. #endif
  9785. #ifndef NO_WOLFSSL_SERVER
  9786. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9787. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9788. #endif
  9789. ExpectNotNull(ctx);
  9790. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9791. #ifdef OPENSSL_EXTRA
  9792. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  9793. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  9794. #else
  9795. UseMaxFragment = wolfSSL_UseMaxFragment;
  9796. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  9797. #endif
  9798. /* error cases */
  9799. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  9800. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  9801. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  9802. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  9803. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  9804. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  9805. /* success case */
  9806. #ifdef OPENSSL_EXTRA
  9807. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  9808. #else
  9809. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  9810. #endif
  9811. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  9812. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  9813. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  9814. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  9815. #ifdef OPENSSL_EXTRA
  9816. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  9817. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  9818. #else
  9819. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  9820. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  9821. #endif
  9822. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  9823. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  9824. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  9825. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  9826. #ifdef OPENSSL_EXTRA
  9827. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  9828. #else
  9829. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  9830. #endif
  9831. wolfSSL_free(ssl);
  9832. wolfSSL_CTX_free(ctx);
  9833. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9834. #endif
  9835. return EXPECT_RESULT();
  9836. }
  9837. static int test_wolfSSL_UseTruncatedHMAC(void)
  9838. {
  9839. EXPECT_DECLS;
  9840. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  9841. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  9842. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9843. #ifndef NO_WOLFSSL_SERVER
  9844. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  9845. #else
  9846. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9847. #endif
  9848. WOLFSSL *ssl = NULL;
  9849. ExpectNotNull(ctx);
  9850. #ifndef NO_WOLFSSL_SERVER
  9851. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9852. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9853. #endif
  9854. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9855. /* error cases */
  9856. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  9857. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  9858. /* success case */
  9859. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  9860. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  9861. wolfSSL_free(ssl);
  9862. wolfSSL_CTX_free(ctx);
  9863. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9864. #endif
  9865. return EXPECT_RESULT();
  9866. }
  9867. static int test_wolfSSL_UseSupportedCurve(void)
  9868. {
  9869. EXPECT_DECLS;
  9870. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && \
  9871. !defined(NO_TLS)
  9872. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9873. WOLFSSL *ssl = wolfSSL_new(ctx);
  9874. ExpectNotNull(ctx);
  9875. ExpectNotNull(ssl);
  9876. /* error cases */
  9877. ExpectIntNE(WOLFSSL_SUCCESS,
  9878. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  9879. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  9880. ExpectIntNE(WOLFSSL_SUCCESS,
  9881. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  9882. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  9883. /* success case */
  9884. ExpectIntEQ(WOLFSSL_SUCCESS,
  9885. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  9886. ExpectIntEQ(WOLFSSL_SUCCESS,
  9887. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  9888. wolfSSL_free(ssl);
  9889. wolfSSL_CTX_free(ctx);
  9890. #endif
  9891. return EXPECT_RESULT();
  9892. }
  9893. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  9894. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  9895. {
  9896. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  9897. }
  9898. static void use_ALPN_all(WOLFSSL* ssl)
  9899. {
  9900. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9901. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9902. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9903. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9904. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9905. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9906. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9907. }
  9908. static void use_ALPN_all_continue(WOLFSSL* ssl)
  9909. {
  9910. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9911. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9912. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9913. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9914. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9915. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9916. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9917. }
  9918. static void use_ALPN_one(WOLFSSL* ssl)
  9919. {
  9920. /* spdy/2 */
  9921. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9922. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9923. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9924. }
  9925. static void use_ALPN_unknown(WOLFSSL* ssl)
  9926. {
  9927. /* http/2.0 */
  9928. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9929. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9930. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9931. }
  9932. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  9933. {
  9934. /* http/2.0 */
  9935. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9936. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9937. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9938. }
  9939. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  9940. {
  9941. /* spdy/3 */
  9942. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9943. char *proto = NULL;
  9944. word16 protoSz = 0;
  9945. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9946. /* check value */
  9947. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  9948. if (proto) {
  9949. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  9950. }
  9951. }
  9952. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  9953. {
  9954. char *proto = NULL;
  9955. word16 protoSz = 0;
  9956. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  9957. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9958. /* check value */
  9959. AssertIntEQ(1, (0 == protoSz));
  9960. AssertIntEQ(1, (NULL == proto));
  9961. }
  9962. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  9963. {
  9964. /* http/1.1 */
  9965. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9966. char *proto;
  9967. word16 protoSz = 0;
  9968. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9969. /* check value */
  9970. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9971. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9972. }
  9973. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  9974. {
  9975. /* spdy/2 */
  9976. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9977. char *proto;
  9978. word16 protoSz = 0;
  9979. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9980. /* check value */
  9981. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9982. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9983. }
  9984. static void verify_ALPN_client_list(WOLFSSL* ssl)
  9985. {
  9986. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9987. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9988. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9989. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9990. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9991. char *clist = NULL;
  9992. word16 clistSz = 0;
  9993. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  9994. &clistSz));
  9995. /* check value */
  9996. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  9997. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  9998. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  9999. }
  10000. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  10001. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)
  10002. /* ALPN select callback, success with spdy/2 */
  10003. static int select_ALPN_spdy2(WOLFSSL *ssl, const unsigned char **out,
  10004. unsigned char *outlen, const unsigned char *in,
  10005. unsigned int inlen, void *arg)
  10006. {
  10007. /* spdy/2 */
  10008. const char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  10009. (void)ssl;
  10010. (void)arg;
  10011. /* adding +1 since LEN byte comes first */
  10012. if (inlen < sizeof(proto) + 1) {
  10013. return SSL_TLSEXT_ERR_ALERT_FATAL;
  10014. }
  10015. if (XMEMCMP(in + 1, proto, sizeof(proto)) == 0) {
  10016. *out = in + 1;
  10017. *outlen = (unsigned char)sizeof(proto);
  10018. return SSL_TLSEXT_ERR_OK;
  10019. }
  10020. return SSL_TLSEXT_ERR_ALERT_FATAL;
  10021. }
  10022. /* ALPN select callback, force failure */
  10023. static int select_ALPN_failure(WOLFSSL *ssl, const unsigned char **out,
  10024. unsigned char *outlen, const unsigned char *in,
  10025. unsigned int inlen, void *arg)
  10026. {
  10027. (void)ssl;
  10028. (void)out;
  10029. (void)outlen;
  10030. (void)in;
  10031. (void)inlen;
  10032. (void)arg;
  10033. return SSL_TLSEXT_ERR_ALERT_FATAL;
  10034. }
  10035. static void use_ALPN_spdy2_callback(WOLFSSL* ssl)
  10036. {
  10037. wolfSSL_set_alpn_select_cb(ssl, select_ALPN_spdy2, NULL);
  10038. }
  10039. static void use_ALPN_failure_callback(WOLFSSL* ssl)
  10040. {
  10041. wolfSSL_set_alpn_select_cb(ssl, select_ALPN_failure, NULL);
  10042. }
  10043. #endif /* OPENSSL_ALL | NGINX | HAPROXY | LIGHTY | QUIC */
  10044. static int test_wolfSSL_UseALPN_connection(void)
  10045. {
  10046. int res = TEST_SKIPPED;
  10047. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  10048. callback_functions client_cb;
  10049. callback_functions server_cb;
  10050. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  10051. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  10052. client_cb.method = wolfSSLv23_client_method;
  10053. server_cb.method = wolfSSLv23_server_method;
  10054. client_cb.devId = testDevId;
  10055. server_cb.devId = testDevId;
  10056. /* success case same list */
  10057. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10058. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  10059. test_wolfSSL_client_server(&client_cb, &server_cb);
  10060. /* success case only one for server */
  10061. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10062. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  10063. test_wolfSSL_client_server(&client_cb, &server_cb);
  10064. /* success case only one for client */
  10065. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  10066. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  10067. test_wolfSSL_client_server(&client_cb, &server_cb);
  10068. /* success case none for client */
  10069. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  10070. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  10071. test_wolfSSL_client_server(&client_cb, &server_cb);
  10072. /* success case mismatch behavior but option 'continue' set */
  10073. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  10074. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  10075. test_wolfSSL_client_server(&client_cb, &server_cb);
  10076. /* success case read protocol send by client */
  10077. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10078. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  10079. test_wolfSSL_client_server(&client_cb, &server_cb);
  10080. /* mismatch behavior with same list
  10081. * the first and only this one must be taken */
  10082. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10083. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  10084. test_wolfSSL_client_server(&client_cb, &server_cb);
  10085. /* default mismatch behavior */
  10086. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10087. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  10088. test_wolfSSL_client_server(&client_cb, &server_cb);
  10089. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  10090. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)
  10091. /* WOLFSSL-level ALPN select callback tests */
  10092. /* Callback: success (one protocol, spdy/2) */
  10093. client_cb.ctx_ready = NULL;
  10094. client_cb.ssl_ready = use_ALPN_one;
  10095. client_cb.on_result = verify_ALPN_matching_spdy2;
  10096. server_cb.ctx_ready = NULL;
  10097. server_cb.ssl_ready = use_ALPN_spdy2_callback;
  10098. server_cb.on_result = verify_ALPN_matching_spdy2;
  10099. test_wolfSSL_client_server(&client_cb, &server_cb);
  10100. /* Callback: failure (one client protocol, spdy/2) */
  10101. client_cb.ctx_ready = NULL;
  10102. client_cb.ssl_ready = use_ALPN_one;
  10103. client_cb.on_result = NULL;
  10104. server_cb.ctx_ready = NULL;
  10105. server_cb.ssl_ready = use_ALPN_failure_callback;
  10106. server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  10107. test_wolfSSL_client_server(&client_cb, &server_cb);
  10108. #endif /* OPENSSL_ALL | NGINX | HAPROXY | LIGHTY */
  10109. res = TEST_RES_CHECK(1);
  10110. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  10111. return res;
  10112. }
  10113. static int test_wolfSSL_UseALPN_params(void)
  10114. {
  10115. EXPECT_DECLS;
  10116. #ifndef NO_WOLFSSL_CLIENT
  10117. /* "http/1.1" */
  10118. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  10119. /* "spdy/1" */
  10120. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  10121. /* "spdy/2" */
  10122. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  10123. /* "spdy/3" */
  10124. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10125. char buff[256];
  10126. word32 idx;
  10127. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10128. WOLFSSL *ssl = wolfSSL_new(ctx);
  10129. ExpectNotNull(ctx);
  10130. ExpectNotNull(ssl);
  10131. /* error cases */
  10132. ExpectIntNE(WOLFSSL_SUCCESS,
  10133. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  10134. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10135. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  10136. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10137. /* success case */
  10138. /* http1 only */
  10139. ExpectIntEQ(WOLFSSL_SUCCESS,
  10140. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  10141. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10142. /* http1, spdy1 */
  10143. XMEMCPY(buff, http1, sizeof(http1));
  10144. idx = sizeof(http1);
  10145. buff[idx++] = ',';
  10146. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10147. idx += sizeof(spdy1);
  10148. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10149. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10150. /* http1, spdy2, spdy1 */
  10151. XMEMCPY(buff, http1, sizeof(http1));
  10152. idx = sizeof(http1);
  10153. buff[idx++] = ',';
  10154. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  10155. idx += sizeof(spdy2);
  10156. buff[idx++] = ',';
  10157. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10158. idx += sizeof(spdy1);
  10159. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10160. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10161. /* spdy3, http1, spdy2, spdy1 */
  10162. XMEMCPY(buff, spdy3, sizeof(spdy3));
  10163. idx = sizeof(spdy3);
  10164. buff[idx++] = ',';
  10165. XMEMCPY(buff+idx, http1, sizeof(http1));
  10166. idx += sizeof(http1);
  10167. buff[idx++] = ',';
  10168. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  10169. idx += sizeof(spdy2);
  10170. buff[idx++] = ',';
  10171. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10172. idx += sizeof(spdy1);
  10173. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10174. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  10175. wolfSSL_free(ssl);
  10176. wolfSSL_CTX_free(ctx);
  10177. #endif
  10178. return EXPECT_RESULT();
  10179. }
  10180. #endif /* HAVE_ALPN */
  10181. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  10182. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  10183. {
  10184. unsigned char p[] = {
  10185. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  10186. 6, 's', 'p', 'd', 'y', '/', '2',
  10187. 6, 's', 'p', 'd', 'y', '/', '1',
  10188. };
  10189. unsigned char p_len = sizeof(p);
  10190. int ret;
  10191. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  10192. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  10193. AssertIntEQ(ret, 0);
  10194. #else
  10195. AssertIntEQ(ret, SSL_SUCCESS);
  10196. #endif
  10197. }
  10198. static void set_alpn_protos(SSL* ssl)
  10199. {
  10200. unsigned char p[] = {
  10201. 6, 's', 'p', 'd', 'y', '/', '3',
  10202. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  10203. 6, 's', 'p', 'd', 'y', '/', '2',
  10204. 6, 's', 'p', 'd', 'y', '/', '1',
  10205. };
  10206. unsigned char p_len = sizeof(p);
  10207. int ret;
  10208. ret = SSL_set_alpn_protos(ssl, p, p_len);
  10209. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  10210. AssertIntEQ(ret, 0);
  10211. #else
  10212. AssertIntEQ(ret, SSL_SUCCESS);
  10213. #endif
  10214. }
  10215. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  10216. {
  10217. /* "spdy/3" */
  10218. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10219. const unsigned char *proto;
  10220. unsigned int protoSz = 0;
  10221. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  10222. /* check value */
  10223. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  10224. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  10225. }
  10226. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  10227. {
  10228. /* "http/1.1" */
  10229. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  10230. const unsigned char *proto;
  10231. unsigned int protoSz = 0;
  10232. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  10233. /* check value */
  10234. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  10235. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  10236. }
  10237. static int test_wolfSSL_set_alpn_protos(void)
  10238. {
  10239. int res = TEST_SKIPPED;
  10240. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  10241. callback_functions client_cb;
  10242. callback_functions server_cb;
  10243. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  10244. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  10245. client_cb.method = wolfSSLv23_client_method;
  10246. server_cb.method = wolfSSLv23_server_method;
  10247. client_cb.devId = testDevId;
  10248. server_cb.devId = testDevId;
  10249. /* use CTX_alpn_protos */
  10250. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  10251. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  10252. test_wolfSSL_client_server(&client_cb, &server_cb);
  10253. /* use set_alpn_protos */
  10254. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  10255. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  10256. test_wolfSSL_client_server(&client_cb, &server_cb);
  10257. res = TEST_SUCCESS;
  10258. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  10259. return res;
  10260. }
  10261. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  10262. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  10263. {
  10264. EXPECT_DECLS;
  10265. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  10266. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10267. WOLFSSL *ssl = wolfSSL_new(ctx);
  10268. ExpectNotNull(ctx);
  10269. ExpectNotNull(ssl);
  10270. /* error cases */
  10271. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  10272. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  10273. /* success cases */
  10274. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  10275. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  10276. wolfSSL_free(ssl);
  10277. wolfSSL_CTX_free(ctx);
  10278. #endif
  10279. return EXPECT_RESULT();
  10280. }
  10281. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  10282. {
  10283. EXPECT_DECLS;
  10284. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  10285. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10286. WOLFSSL *ssl = wolfSSL_new(ctx);
  10287. ExpectNotNull(ctx);
  10288. ExpectNotNull(ssl);
  10289. /* error cases */
  10290. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  10291. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  10292. /* success cases */
  10293. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  10294. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  10295. wolfSSL_free(ssl);
  10296. wolfSSL_CTX_free(ctx);
  10297. #endif
  10298. return EXPECT_RESULT();
  10299. }
  10300. /* Test reconnecting with a different ciphersuite after a renegotiation. */
  10301. static int test_wolfSSL_SCR_Reconnect(void)
  10302. {
  10303. EXPECT_DECLS;
  10304. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  10305. defined(BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) && \
  10306. defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) && \
  10307. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  10308. struct test_memio_ctx test_ctx;
  10309. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  10310. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  10311. byte data;
  10312. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  10313. test_ctx.c_ciphers = "ECDHE-RSA-AES256-GCM-SHA384";
  10314. test_ctx.s_ciphers =
  10315. "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305";
  10316. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  10317. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  10318. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_c));
  10319. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_s));
  10320. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_c));
  10321. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_s));
  10322. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10323. /* WOLFSSL_FATAL_ERROR since it will block */
  10324. ExpectIntEQ(wolfSSL_Rehandshake(ssl_s), WOLFSSL_FATAL_ERROR);
  10325. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  10326. WOLFSSL_ERROR_WANT_READ);
  10327. ExpectIntEQ(wolfSSL_read(ssl_c, &data, 1), WOLFSSL_FATAL_ERROR);
  10328. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  10329. WOLFSSL_ERROR_WANT_READ);
  10330. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10331. wolfSSL_free(ssl_c);
  10332. ssl_c = NULL;
  10333. wolfSSL_free(ssl_s);
  10334. ssl_s = NULL;
  10335. wolfSSL_CTX_free(ctx_c);
  10336. ctx_c = NULL;
  10337. test_ctx.c_ciphers = "ECDHE-RSA-CHACHA20-POLY1305";
  10338. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  10339. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  10340. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10341. wolfSSL_free(ssl_s);
  10342. wolfSSL_free(ssl_c);
  10343. wolfSSL_CTX_free(ctx_s);
  10344. wolfSSL_CTX_free(ctx_c);
  10345. #endif
  10346. return EXPECT_RESULT();
  10347. }
  10348. #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_SERVER) && \
  10349. (!defined(NO_RSA) || defined(HAVE_ECC))
  10350. /* Called when writing. */
  10351. static int DummySend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  10352. {
  10353. (void)ssl;
  10354. (void)buf;
  10355. (void)sz;
  10356. (void)ctx;
  10357. /* Force error return from wolfSSL_accept_TLSv13(). */
  10358. return WANT_WRITE;
  10359. }
  10360. /* Called when reading. */
  10361. static int BufferInfoRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  10362. {
  10363. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  10364. int len = (int)msg->length;
  10365. (void)ssl;
  10366. (void)sz;
  10367. /* Pass back as much of message as will fit in buffer. */
  10368. if (len > sz)
  10369. len = sz;
  10370. XMEMCPY(buf, msg->buffer, len);
  10371. /* Move over returned data. */
  10372. msg->buffer += len;
  10373. msg->length -= len;
  10374. /* Amount actually copied. */
  10375. return len;
  10376. }
  10377. #endif
  10378. /* Test the detection of duplicate known TLS extensions.
  10379. * Specifically in a ClientHello.
  10380. */
  10381. static int test_tls_ext_duplicate(void)
  10382. {
  10383. EXPECT_DECLS;
  10384. #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC)) && \
  10385. !defined(NO_FILESYSTEM)
  10386. const unsigned char clientHelloDupTlsExt[] = {
  10387. 0x16, 0x03, 0x03, 0x00, 0x6a, 0x01, 0x00, 0x00,
  10388. 0x66, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  10389. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  10390. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  10391. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  10392. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  10393. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  10394. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  10395. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  10396. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x13, 0x01,
  10397. 0x00, 0x9e, 0x01, 0x00,
  10398. /* Extensions - duplicate signature algorithms. */
  10399. 0x00, 0x19, 0x00, 0x0d,
  10400. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01, 0x00, 0x0d,
  10401. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01,
  10402. /* Supported Versions extension for TLS 1.3. */
  10403. 0x00, 0x2b,
  10404. 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03
  10405. };
  10406. WOLFSSL_BUFFER_INFO msg;
  10407. const char* testCertFile;
  10408. const char* testKeyFile;
  10409. WOLFSSL_CTX *ctx = NULL;
  10410. WOLFSSL *ssl = NULL;
  10411. #ifndef NO_RSA
  10412. testCertFile = svrCertFile;
  10413. testKeyFile = svrKeyFile;
  10414. #elif defined(HAVE_ECC)
  10415. testCertFile = eccCertFile;
  10416. testKeyFile = eccKeyFile;
  10417. #endif
  10418. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10419. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  10420. WOLFSSL_FILETYPE_PEM));
  10421. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  10422. WOLFSSL_FILETYPE_PEM));
  10423. /* Read from 'msg'. */
  10424. wolfSSL_SetIORecv(ctx, BufferInfoRecv);
  10425. /* No where to send to - dummy sender. */
  10426. wolfSSL_SetIOSend(ctx, DummySend);
  10427. ssl = wolfSSL_new(ctx);
  10428. ExpectNotNull(ssl);
  10429. msg.buffer = (unsigned char*)clientHelloDupTlsExt;
  10430. msg.length = (unsigned int)sizeof(clientHelloDupTlsExt);
  10431. wolfSSL_SetIOReadCtx(ssl, &msg);
  10432. ExpectIntNE(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  10433. /* can return duplicate ext error or socket error if the peer closed down
  10434. * while sending alert */
  10435. if (wolfSSL_get_error(ssl, 0) != SOCKET_ERROR_E) {
  10436. ExpectIntEQ(wolfSSL_get_error(ssl, 0), DUPLICATE_TLS_EXT_E);
  10437. }
  10438. wolfSSL_free(ssl);
  10439. wolfSSL_CTX_free(ctx);
  10440. #endif
  10441. return EXPECT_RESULT();
  10442. }
  10443. /*----------------------------------------------------------------------------*
  10444. | X509 Tests
  10445. *----------------------------------------------------------------------------*/
  10446. static int test_wolfSSL_X509_NAME_get_entry(void)
  10447. {
  10448. EXPECT_DECLS;
  10449. #if !defined(NO_CERTS) && !defined(NO_RSA)
  10450. #if defined(OPENSSL_ALL) || \
  10451. (defined(OPENSSL_EXTRA) && \
  10452. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  10453. /* use openssl like name to test mapping */
  10454. X509_NAME_ENTRY* ne = NULL;
  10455. X509_NAME* name = NULL;
  10456. X509* x509 = NULL;
  10457. #ifndef NO_FILESYSTEM
  10458. ASN1_STRING* asn = NULL;
  10459. char* subCN = NULL;
  10460. #endif
  10461. int idx = 0;
  10462. ASN1_OBJECT *object = NULL;
  10463. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  10464. defined(WOLFSSL_NGINX)
  10465. #ifndef NO_BIO
  10466. BIO* bio = NULL;
  10467. #endif
  10468. #endif
  10469. #ifndef NO_FILESYSTEM
  10470. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10471. WOLFSSL_FILETYPE_PEM));
  10472. ExpectNotNull(name = X509_get_subject_name(x509));
  10473. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  10474. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  10475. ExpectNotNull(asn = X509_NAME_ENTRY_get_data(ne));
  10476. ExpectNotNull(subCN = (char*)ASN1_STRING_data(asn));
  10477. wolfSSL_FreeX509(x509);
  10478. x509 = NULL;
  10479. #endif
  10480. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10481. WOLFSSL_FILETYPE_PEM));
  10482. ExpectNotNull(name = X509_get_subject_name(x509));
  10483. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  10484. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  10485. defined(WOLFSSL_NGINX)
  10486. #ifndef NO_BIO
  10487. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10488. ExpectIntEQ(X509_NAME_print_ex(bio, name, 4,
  10489. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  10490. ExpectIntEQ(X509_NAME_print_ex_fp(stderr, name, 4,
  10491. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  10492. BIO_free(bio);
  10493. #endif
  10494. #endif
  10495. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  10496. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  10497. wolfSSL_FreeX509(x509);
  10498. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  10499. #endif /* !NO_CERTS && !NO_RSA */
  10500. return EXPECT_RESULT();
  10501. }
  10502. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  10503. static int test_wolfSSL_PKCS12(void)
  10504. {
  10505. EXPECT_DECLS;
  10506. /* .p12 file is encrypted with DES3 */
  10507. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  10508. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  10509. * Password Key
  10510. */
  10511. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  10512. !defined(NO_STDIO_FILESYSTEM) && \
  10513. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  10514. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  10515. byte buf[6000];
  10516. char file[] = "./certs/test-servercert.p12";
  10517. char order[] = "./certs/ecc-rsa-server.p12";
  10518. #ifdef WC_RC2
  10519. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  10520. #endif
  10521. char pass[] = "a password";
  10522. const char goodPsw[] = "wolfSSL test";
  10523. const char badPsw[] = "bad";
  10524. #ifdef HAVE_ECC
  10525. WOLFSSL_X509_NAME *subject = NULL;
  10526. WOLFSSL_X509 *x509 = NULL;
  10527. #endif
  10528. XFILE f = XBADFILE;
  10529. int bytes = 0, ret = 0, goodPswLen = 0, badPswLen = 0;
  10530. WOLFSSL_BIO *bio = NULL;
  10531. WOLFSSL_EVP_PKEY *pkey = NULL;
  10532. WC_PKCS12 *pkcs12 = NULL;
  10533. WC_PKCS12 *pkcs12_2 = NULL;
  10534. WOLFSSL_X509 *cert = NULL;
  10535. WOLFSSL_X509 *tmp = NULL;
  10536. WOLF_STACK_OF(WOLFSSL_X509) *ca = NULL;
  10537. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10538. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10539. WOLFSSL_CTX *ctx = NULL;
  10540. WOLFSSL *ssl = NULL;
  10541. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  10542. #endif
  10543. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10544. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10545. if (f != XBADFILE) {
  10546. XFCLOSE(f);
  10547. f = XBADFILE;
  10548. }
  10549. goodPswLen = (int)XSTRLEN(goodPsw);
  10550. badPswLen = (int)XSTRLEN(badPsw);
  10551. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  10552. ExpectIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  10553. ExpectNotNull(d2i_PKCS12_bio(bio, &pkcs12));
  10554. ExpectNotNull(pkcs12);
  10555. BIO_free(bio);
  10556. bio = NULL;
  10557. /* check verify MAC directly */
  10558. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen), 1);
  10559. /* check verify MAC fail case directly */
  10560. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen), 0);
  10561. /* check verify MAC fail case */
  10562. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10563. ExpectNull(pkey);
  10564. ExpectNull(cert);
  10565. /* check parse with no extra certs kept */
  10566. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10567. 1);
  10568. ExpectNotNull(pkey);
  10569. ExpectNotNull(cert);
  10570. wolfSSL_EVP_PKEY_free(pkey);
  10571. pkey = NULL;
  10572. wolfSSL_X509_free(cert);
  10573. cert = NULL;
  10574. /* check parse with extra certs kept */
  10575. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10576. 1);
  10577. ExpectNotNull(pkey);
  10578. ExpectNotNull(cert);
  10579. ExpectNotNull(ca);
  10580. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10581. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10582. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  10583. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10584. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  10585. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10586. #else
  10587. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10588. #endif
  10589. /* Copy stack structure */
  10590. ExpectNotNull(tmp_ca = X509_chain_up_ref(ca));
  10591. ExpectIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  10592. /* CTX now owns the tmp_ca stack structure */
  10593. tmp_ca = NULL;
  10594. ExpectIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  10595. ExpectNotNull(tmp_ca);
  10596. ExpectIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  10597. /* Check that the main cert is also set */
  10598. ExpectNotNull(SSL_CTX_get0_certificate(ctx));
  10599. ExpectNotNull(ssl = SSL_new(ctx));
  10600. ExpectNotNull(SSL_get_certificate(ssl));
  10601. SSL_free(ssl);
  10602. SSL_CTX_free(ctx);
  10603. ctx = NULL;
  10604. #endif
  10605. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10606. /* should be 2 other certs on stack */
  10607. ExpectNotNull(tmp = sk_X509_pop(ca));
  10608. X509_free(tmp);
  10609. ExpectNotNull(tmp = sk_X509_pop(ca));
  10610. X509_free(tmp);
  10611. ExpectNull(sk_X509_pop(ca));
  10612. EVP_PKEY_free(pkey);
  10613. pkey = NULL;
  10614. X509_free(cert);
  10615. cert = NULL;
  10616. sk_X509_pop_free(ca, X509_free);
  10617. ca = NULL;
  10618. /* check PKCS12_create */
  10619. ExpectNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  10620. ExpectIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10621. SSL_SUCCESS);
  10622. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  10623. -1, -1, 100, -1, 0)));
  10624. EVP_PKEY_free(pkey);
  10625. pkey = NULL;
  10626. X509_free(cert);
  10627. cert = NULL;
  10628. sk_X509_pop_free(ca, NULL);
  10629. ca = NULL;
  10630. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10631. SSL_SUCCESS);
  10632. PKCS12_free(pkcs12_2);
  10633. pkcs12_2 = NULL;
  10634. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  10635. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  10636. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  10637. 2000, 1, 0)));
  10638. EVP_PKEY_free(pkey);
  10639. pkey = NULL;
  10640. X509_free(cert);
  10641. cert = NULL;
  10642. sk_X509_pop_free(ca, NULL);
  10643. ca = NULL;
  10644. /* convert to DER then back and parse */
  10645. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10646. ExpectIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  10647. PKCS12_free(pkcs12_2);
  10648. pkcs12_2 = NULL;
  10649. ExpectNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  10650. BIO_free(bio);
  10651. bio = NULL;
  10652. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10653. SSL_SUCCESS);
  10654. /* should be 2 other certs on stack */
  10655. ExpectNotNull(tmp = sk_X509_pop(ca));
  10656. X509_free(tmp);
  10657. ExpectNotNull(tmp = sk_X509_pop(ca));
  10658. X509_free(tmp);
  10659. ExpectNull(sk_X509_pop(ca));
  10660. #ifndef NO_RC4
  10661. PKCS12_free(pkcs12_2);
  10662. pkcs12_2 = NULL;
  10663. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  10664. NID_pbe_WithSHA1And128BitRC4,
  10665. NID_pbe_WithSHA1And128BitRC4,
  10666. 2000, 1, 0)));
  10667. EVP_PKEY_free(pkey);
  10668. pkey = NULL;
  10669. X509_free(cert);
  10670. cert = NULL;
  10671. sk_X509_pop_free(ca, NULL);
  10672. ca = NULL;
  10673. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10674. SSL_SUCCESS);
  10675. #endif /* NO_RC4 */
  10676. EVP_PKEY_free(pkey);
  10677. pkey = NULL;
  10678. X509_free(cert);
  10679. cert = NULL;
  10680. PKCS12_free(pkcs12);
  10681. pkcs12 = NULL;
  10682. PKCS12_free(pkcs12_2);
  10683. pkcs12_2 = NULL;
  10684. sk_X509_pop_free(ca, NULL);
  10685. ca = NULL;
  10686. #ifdef HAVE_ECC
  10687. /* test order of parsing */
  10688. ExpectTrue((f = XFOPEN(order, "rb")) != XBADFILE);
  10689. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10690. if (f != XBADFILE) {
  10691. XFCLOSE(f);
  10692. f = XBADFILE;
  10693. }
  10694. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  10695. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  10696. ExpectIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  10697. WOLFSSL_SUCCESS);
  10698. /* check use of pkey after parse */
  10699. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10700. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10701. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10702. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  10703. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10704. #else
  10705. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10706. #endif
  10707. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  10708. SSL_CTX_free(ctx);
  10709. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10710. #endif
  10711. ExpectNotNull(pkey);
  10712. ExpectNotNull(cert);
  10713. ExpectNotNull(ca);
  10714. /* compare subject lines of certificates */
  10715. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  10716. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  10717. SSL_FILETYPE_PEM));
  10718. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10719. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10720. X509_free(x509);
  10721. x509 = NULL;
  10722. /* test expected fail case */
  10723. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  10724. SSL_FILETYPE_PEM));
  10725. ExpectIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10726. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10727. X509_free(x509);
  10728. x509 = NULL;
  10729. X509_free(cert);
  10730. cert = NULL;
  10731. /* get subject line from ca stack */
  10732. ExpectNotNull(cert = sk_X509_pop(ca));
  10733. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  10734. /* compare subject from certificate in ca to expected */
  10735. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  10736. SSL_FILETYPE_PEM));
  10737. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10738. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10739. EVP_PKEY_free(pkey);
  10740. pkey = NULL;
  10741. X509_free(x509);
  10742. x509 = NULL;
  10743. X509_free(cert);
  10744. cert = NULL;
  10745. BIO_free(bio);
  10746. bio = NULL;
  10747. PKCS12_free(pkcs12);
  10748. pkcs12 = NULL;
  10749. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  10750. ca = NULL;
  10751. /* test order of parsing */
  10752. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10753. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  10754. if (f != XBADFILE) {
  10755. XFCLOSE(f);
  10756. f = XBADFILE;
  10757. }
  10758. /* check verify MAC fail case */
  10759. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10760. ExpectNull(pkey);
  10761. ExpectNull(cert);
  10762. /* check parse with no extra certs kept */
  10763. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10764. 1);
  10765. ExpectNotNull(pkey);
  10766. ExpectNotNull(cert);
  10767. wolfSSL_EVP_PKEY_free(pkey);
  10768. pkey = NULL;
  10769. wolfSSL_X509_free(cert);
  10770. cert = NULL;
  10771. /* check parse with extra certs kept */
  10772. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10773. 1);
  10774. ExpectNotNull(pkey);
  10775. ExpectNotNull(cert);
  10776. ExpectNotNull(ca);
  10777. wolfSSL_EVP_PKEY_free(pkey);
  10778. pkey = NULL;
  10779. wolfSSL_X509_free(cert);
  10780. cert = NULL;
  10781. sk_X509_pop_free(ca, NULL);
  10782. ca = NULL;
  10783. PKCS12_free(pkcs12);
  10784. pkcs12 = NULL;
  10785. #endif /* HAVE_ECC */
  10786. #ifdef WC_RC2
  10787. /* test PKCS#12 with RC2 encryption */
  10788. ExpectTrue((f = XFOPEN(rc2p12, "rb")) != XBADFILE);
  10789. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10790. if (f != XBADFILE) {
  10791. XFCLOSE(f);
  10792. f = XBADFILE;
  10793. }
  10794. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  10795. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  10796. /* check verify MAC fail case */
  10797. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10798. ExpectNull(pkey);
  10799. ExpectNull(cert);
  10800. /* check parse with not extra certs kept */
  10801. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10802. WOLFSSL_SUCCESS);
  10803. ExpectNotNull(pkey);
  10804. ExpectNotNull(cert);
  10805. wolfSSL_EVP_PKEY_free(pkey);
  10806. pkey = NULL;
  10807. wolfSSL_X509_free(cert);
  10808. cert = NULL;
  10809. /* check parse with extra certs kept */
  10810. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10811. WOLFSSL_SUCCESS);
  10812. ExpectNotNull(pkey);
  10813. ExpectNotNull(cert);
  10814. ExpectNotNull(ca);
  10815. wolfSSL_EVP_PKEY_free(pkey);
  10816. wolfSSL_X509_free(cert);
  10817. sk_X509_pop_free(ca, NULL);
  10818. BIO_free(bio);
  10819. bio = NULL;
  10820. PKCS12_free(pkcs12);
  10821. pkcs12 = NULL;
  10822. #endif /* WC_RC2 */
  10823. /* Test i2d_PKCS12_bio */
  10824. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10825. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  10826. if (f != XBADFILE)
  10827. XFCLOSE(f);
  10828. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10829. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, pkcs12), 1);
  10830. ExpectIntEQ(ret = i2d_PKCS12_bio(NULL, pkcs12), 0);
  10831. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, NULL), 0);
  10832. PKCS12_free(pkcs12);
  10833. BIO_free(bio);
  10834. (void)order;
  10835. #endif /* OPENSSL_EXTRA */
  10836. #endif /* HAVE_FIPS */
  10837. return EXPECT_RESULT();
  10838. }
  10839. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  10840. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  10841. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  10842. #define TEST_PKCS8_ENC
  10843. #endif
  10844. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  10845. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  10846. /* used to keep track if FailTestCallback was called */
  10847. static int failTestCallbackCalled = 0;
  10848. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  10849. {
  10850. (void)passwd;
  10851. (void)sz;
  10852. (void)rw;
  10853. (void)userdata;
  10854. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  10855. failTestCallbackCalled = 1;
  10856. return -1;
  10857. }
  10858. #endif
  10859. static int test_wolfSSL_no_password_cb(void)
  10860. {
  10861. EXPECT_DECLS;
  10862. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  10863. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  10864. WOLFSSL_CTX* ctx = NULL;
  10865. byte buff[FOURK_BUF];
  10866. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  10867. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  10868. XFILE f = XBADFILE;
  10869. int bytes = 0;
  10870. #ifndef NO_WOLFSSL_CLIENT
  10871. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  10872. #else
  10873. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  10874. #endif
  10875. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  10876. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  10877. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10878. if (f != XBADFILE) {
  10879. XFCLOSE(f);
  10880. f = XBADFILE;
  10881. }
  10882. ExpectIntLE(bytes, sizeof(buff));
  10883. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10884. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10885. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  10886. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10887. if (f != XBADFILE)
  10888. XFCLOSE(f);
  10889. ExpectIntLE(bytes, sizeof(buff));
  10890. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10891. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10892. wolfSSL_CTX_free(ctx);
  10893. /* Password callback should not be called by default */
  10894. ExpectIntEQ(failTestCallbackCalled, 0);
  10895. #endif
  10896. return EXPECT_RESULT();
  10897. }
  10898. #ifdef TEST_PKCS8_ENC
  10899. /* for PKCS8 test case */
  10900. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  10901. {
  10902. int flag = 0;
  10903. (void)rw;
  10904. if (userdata != NULL) {
  10905. flag = *((int*)userdata); /* user set data */
  10906. }
  10907. switch (flag) {
  10908. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  10909. * can be anything the user wishes to be passed to the callback
  10910. * associated with the WOLFSSL_CTX */
  10911. XSTRNCPY(passwd, "yassl123", sz);
  10912. return 8;
  10913. default:
  10914. return BAD_FUNC_ARG;
  10915. }
  10916. }
  10917. #endif /* TEST_PKCS8_ENC */
  10918. /* Testing functions dealing with PKCS8 */
  10919. static int test_wolfSSL_PKCS8(void)
  10920. {
  10921. EXPECT_DECLS;
  10922. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  10923. !defined(WOLFCRYPT_ONLY)
  10924. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10925. byte buff[FOURK_BUF];
  10926. byte der[FOURK_BUF];
  10927. #ifndef NO_RSA
  10928. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  10929. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  10930. #endif
  10931. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  10932. #ifdef HAVE_ECC
  10933. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  10934. #endif
  10935. XFILE f = XBADFILE;
  10936. int bytes = 0;
  10937. WOLFSSL_CTX* ctx = NULL;
  10938. #if defined(HAVE_ECC) && !defined(NO_CODING) && !defined(WOLFSSL_NO_PEM)
  10939. int ret;
  10940. ecc_key key;
  10941. word32 x = 0;
  10942. #endif
  10943. #ifdef TEST_PKCS8_ENC
  10944. #if !defined(NO_RSA) && !defined(NO_SHA)
  10945. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  10946. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  10947. #endif
  10948. #if defined(HAVE_ECC) && !defined(NO_SHA)
  10949. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  10950. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  10951. #endif
  10952. int flag;
  10953. #endif
  10954. (void)der;
  10955. #ifndef NO_WOLFSSL_CLIENT
  10956. #ifndef WOLFSSL_NO_TLS12
  10957. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  10958. #else
  10959. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10960. #endif
  10961. #else
  10962. #ifndef WOLFSSL_NO_TLS12
  10963. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10964. #else
  10965. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10966. #endif
  10967. #endif
  10968. #ifdef TEST_PKCS8_ENC
  10969. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  10970. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  10971. flag = 1; /* used by password callback as return code */
  10972. #if !defined(NO_RSA) && !defined(NO_SHA)
  10973. /* test loading PEM PKCS8 encrypted file */
  10974. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncPemFile, "rb")) != XBADFILE);
  10975. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10976. if (f != XBADFILE) {
  10977. XFCLOSE(f);
  10978. f = XBADFILE;
  10979. }
  10980. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10981. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10982. /* this next case should fail because of password callback return code */
  10983. flag = 0; /* used by password callback as return code */
  10984. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10985. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10986. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  10987. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10988. "yassl123"), 0);
  10989. /* test that error value is returned with a bad password */
  10990. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10991. "bad"), 0);
  10992. /* test loading PEM PKCS8 encrypted file */
  10993. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncDerFile, "rb")) != XBADFILE);
  10994. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10995. if (f != XBADFILE) {
  10996. XFCLOSE(f);
  10997. f = XBADFILE;
  10998. }
  10999. flag = 1; /* used by password callback as return code */
  11000. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11001. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11002. /* this next case should fail because of password callback return code */
  11003. flag = 0; /* used by password callback as return code */
  11004. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11005. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11006. #endif /* !NO_RSA && !NO_SHA */
  11007. #if defined(HAVE_ECC) && !defined(NO_SHA)
  11008. /* test loading PEM PKCS8 encrypted ECC Key file */
  11009. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb")) != XBADFILE);
  11010. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11011. if (f != XBADFILE) {
  11012. XFCLOSE(f);
  11013. f = XBADFILE;
  11014. }
  11015. flag = 1; /* used by password callback as return code */
  11016. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11017. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11018. /* this next case should fail because of password callback return code */
  11019. flag = 0; /* used by password callback as return code */
  11020. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11021. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11022. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  11023. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11024. "yassl123"), 0);
  11025. /* test that error value is returned with a bad password */
  11026. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11027. "bad"), 0);
  11028. /* test loading DER PKCS8 encrypted ECC Key file */
  11029. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb")) != XBADFILE);
  11030. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11031. if (f != XBADFILE) {
  11032. XFCLOSE(f);
  11033. f = XBADFILE;
  11034. }
  11035. flag = 1; /* used by password callback as return code */
  11036. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11037. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11038. /* this next case should fail because of password callback return code */
  11039. flag = 0; /* used by password callback as return code */
  11040. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11041. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11042. /* leave flag as "okay" */
  11043. flag = 1;
  11044. #endif /* HAVE_ECC && !NO_SHA */
  11045. #endif /* TEST_PKCS8_ENC */
  11046. #ifndef NO_RSA
  11047. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  11048. ExpectTrue((f = XFOPEN(serverKeyPkcs8DerFile, "rb")) != XBADFILE);
  11049. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11050. if (f != XBADFILE) {
  11051. XFCLOSE(f);
  11052. f = XBADFILE;
  11053. }
  11054. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11055. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11056. /* test loading PEM PKCS8 private key file (not encrypted) */
  11057. ExpectTrue((f = XFOPEN(serverKeyPkcs8PemFile, "rb")) != XBADFILE);
  11058. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11059. if (f != XBADFILE) {
  11060. XFCLOSE(f);
  11061. f = XBADFILE;
  11062. }
  11063. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11064. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11065. #endif /* !NO_RSA */
  11066. /* Test PKCS8 PEM ECC key no crypt */
  11067. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  11068. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11069. if (f != XBADFILE) {
  11070. XFCLOSE(f);
  11071. f = XBADFILE;
  11072. }
  11073. #ifdef HAVE_ECC
  11074. /* Test PKCS8 PEM ECC key no crypt */
  11075. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11076. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11077. #if !defined(NO_CODING) && !defined(WOLFSSL_NO_PEM)
  11078. /* decrypt PKCS8 PEM to key in DER format */
  11079. ExpectIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  11080. (word32)sizeof(der), NULL)), 0);
  11081. ret = wc_ecc_init(&key);
  11082. if (ret == 0) {
  11083. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  11084. wc_ecc_free(&key);
  11085. }
  11086. ExpectIntEQ(ret, 0);
  11087. #endif
  11088. /* Test PKCS8 DER ECC key no crypt */
  11089. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  11090. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11091. if (f != XBADFILE)
  11092. XFCLOSE(f);
  11093. /* Test using a PKCS8 ECC PEM */
  11094. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11095. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11096. #else
  11097. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  11098. ExpectIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  11099. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  11100. #endif /* HAVE_ECC */
  11101. wolfSSL_CTX_free(ctx);
  11102. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11103. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  11104. return EXPECT_RESULT();
  11105. }
  11106. static int test_wolfSSL_PKCS8_ED25519(void)
  11107. {
  11108. EXPECT_DECLS;
  11109. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  11110. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  11111. defined(HAVE_ED25519_KEY_IMPORT)
  11112. const byte encPrivKey[] = \
  11113. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  11114. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  11115. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  11116. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  11117. "2L9W4v7swXkV+X+a1ww=\n"
  11118. "-----END ENCRYPTED PRIVATE KEY-----\n";
  11119. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  11120. byte der[FOURK_BUF];
  11121. WOLFSSL_CTX* ctx = NULL;
  11122. int bytes;
  11123. XMEMSET(der, 0, sizeof(der));
  11124. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  11125. (word32)sizeof(der), password)), 0);
  11126. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  11127. #ifndef NO_WOLFSSL_SERVER
  11128. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  11129. #else
  11130. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  11131. #endif
  11132. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  11133. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11134. wolfSSL_CTX_free(ctx);
  11135. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11136. #endif
  11137. return EXPECT_RESULT();
  11138. }
  11139. static int test_wolfSSL_PKCS8_ED448(void)
  11140. {
  11141. EXPECT_DECLS;
  11142. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  11143. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  11144. defined(HAVE_ED448_KEY_IMPORT)
  11145. const byte encPrivKey[] = \
  11146. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  11147. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  11148. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  11149. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  11150. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  11151. "-----END ENCRYPTED PRIVATE KEY-----\n";
  11152. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  11153. byte der[FOURK_BUF];
  11154. WOLFSSL_CTX* ctx = NULL;
  11155. int bytes;
  11156. XMEMSET(der, 0, sizeof(der));
  11157. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  11158. (word32)sizeof(der), password)), 0);
  11159. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  11160. #ifndef NO_WOLFSSL_SERVER
  11161. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  11162. #else
  11163. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  11164. #endif
  11165. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  11166. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11167. wolfSSL_CTX_free(ctx);
  11168. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11169. #endif
  11170. return EXPECT_RESULT();
  11171. }
  11172. /* Testing functions dealing with PKCS5 */
  11173. static int test_wolfSSL_PKCS5(void)
  11174. {
  11175. EXPECT_DECLS;
  11176. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  11177. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  11178. const char* passwd = "myfipsPa$$W0rd";
  11179. #else
  11180. const char *passwd = "pass1234";
  11181. #endif
  11182. const unsigned char *salt = (unsigned char *)"salt1234";
  11183. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  11184. DYNAMIC_TYPE_TMP_BUFFER);
  11185. int ret = 0;
  11186. ExpectNotNull(out);
  11187. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  11188. (int)XSTRLEN((const char *) salt), 10, WC_SHA_DIGEST_SIZE,out),
  11189. WOLFSSL_SUCCESS);
  11190. #ifdef WOLFSSL_SHA512
  11191. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  11192. (int)XSTRLEN((const char *) salt), 10, wolfSSL_EVP_sha512(),
  11193. WC_SHA_DIGEST_SIZE, out), SSL_SUCCESS);
  11194. #endif
  11195. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11196. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  11197. return EXPECT_RESULT();
  11198. }
  11199. /* test parsing URI from certificate */
  11200. static int test_wolfSSL_URI(void)
  11201. {
  11202. EXPECT_DECLS;
  11203. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  11204. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  11205. defined(OPENSSL_EXTRA))
  11206. WOLFSSL_X509* x509 = NULL;
  11207. const char uri[] = "./certs/client-uri-cert.pem";
  11208. const char urn[] = "./certs/client-absolute-urn.pem";
  11209. const char badUri[] = "./certs/client-relative-uri.pem";
  11210. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(uri,
  11211. WOLFSSL_FILETYPE_PEM));
  11212. wolfSSL_FreeX509(x509);
  11213. x509 = NULL;
  11214. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(urn,
  11215. WOLFSSL_FILETYPE_PEM));
  11216. wolfSSL_FreeX509(x509);
  11217. x509 = NULL;
  11218. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  11219. && !defined(WOLFSSL_FPKI)
  11220. ExpectNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  11221. WOLFSSL_FILETYPE_PEM));
  11222. #else
  11223. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  11224. WOLFSSL_FILETYPE_PEM));
  11225. #endif
  11226. wolfSSL_FreeX509(x509);
  11227. #endif
  11228. return EXPECT_RESULT();
  11229. }
  11230. static int test_wolfSSL_TBS(void)
  11231. {
  11232. EXPECT_DECLS;
  11233. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  11234. && defined(OPENSSL_EXTRA)
  11235. WOLFSSL_X509* x509 = NULL;
  11236. const unsigned char* tbs;
  11237. int tbsSz;
  11238. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caCertFile,
  11239. WOLFSSL_FILETYPE_PEM));
  11240. ExpectNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  11241. ExpectNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  11242. ExpectNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  11243. ExpectIntEQ(tbsSz, 1003);
  11244. wolfSSL_FreeX509(x509);
  11245. #endif
  11246. return EXPECT_RESULT();
  11247. }
  11248. static int test_wolfSSL_X509_verify(void)
  11249. {
  11250. EXPECT_DECLS;
  11251. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  11252. defined(OPENSSL_EXTRA)
  11253. WOLFSSL_X509* ca = NULL;
  11254. WOLFSSL_X509* serv = NULL;
  11255. WOLFSSL_EVP_PKEY* pkey = NULL;
  11256. unsigned char buf[2048];
  11257. const unsigned char* pt = NULL;
  11258. int bufSz;
  11259. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(caCertFile,
  11260. WOLFSSL_FILETYPE_PEM));
  11261. ExpectIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  11262. WOLFSSL_SUCCESS);
  11263. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  11264. WOLFSSL_SUCCESS);
  11265. ExpectIntEQ(bufSz, 294);
  11266. bufSz = 2048;
  11267. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  11268. WOLFSSL_SUCCESS);
  11269. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  11270. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  11271. ExpectNotNull(serv = wolfSSL_X509_load_certificate_file(svrCertFile,
  11272. WOLFSSL_FILETYPE_PEM));
  11273. /* success case */
  11274. pt = buf;
  11275. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  11276. ExpectIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  11277. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  11278. wolfSSL_EVP_PKEY_free(pkey);
  11279. pkey = NULL;
  11280. /* fail case */
  11281. bufSz = 2048;
  11282. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  11283. WOLFSSL_SUCCESS);
  11284. pt = buf;
  11285. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  11286. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  11287. ExpectIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  11288. ExpectIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  11289. wolfSSL_EVP_PKEY_free(pkey);
  11290. wolfSSL_FreeX509(ca);
  11291. wolfSSL_FreeX509(serv);
  11292. #endif
  11293. return EXPECT_RESULT();
  11294. }
  11295. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11296. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11297. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11298. /* create certificate with version 2 */
  11299. static int test_set_x509_badversion(WOLFSSL_CTX* ctx)
  11300. {
  11301. EXPECT_DECLS;
  11302. WOLFSSL_X509 *x509 = NULL, *x509v2 = NULL;
  11303. WOLFSSL_EVP_PKEY *priv = NULL, *pub = NULL;
  11304. unsigned char *der = NULL, *key = NULL, *pt;
  11305. char *header = NULL, *name = NULL;
  11306. int derSz;
  11307. long keySz;
  11308. XFILE fp = XBADFILE;
  11309. WOLFSSL_ASN1_TIME *notBefore = NULL, *notAfter = NULL;
  11310. time_t t;
  11311. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  11312. WOLFSSL_FILETYPE_PEM));
  11313. ExpectTrue((fp = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  11314. ExpectIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  11315. WOLFSSL_SUCCESS);
  11316. if (fp != XBADFILE)
  11317. XFCLOSE(fp);
  11318. pt = key;
  11319. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  11320. (const unsigned char**)&pt, keySz));
  11321. /* create the version 2 certificate */
  11322. ExpectNotNull(x509v2 = X509_new());
  11323. ExpectIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  11324. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  11325. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  11326. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  11327. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  11328. ExpectNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  11329. ExpectIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  11330. t = time(NULL);
  11331. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  11332. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  11333. ExpectTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  11334. ExpectTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  11335. ExpectIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  11336. derSz = wolfSSL_i2d_X509(x509v2, &der);
  11337. ExpectIntGT(derSz, 0);
  11338. ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  11339. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11340. /* TODO: Replace with API call */
  11341. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  11342. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11343. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11344. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11345. wolfSSL_X509_free(x509);
  11346. wolfSSL_X509_free(x509v2);
  11347. wolfSSL_EVP_PKEY_free(priv);
  11348. wolfSSL_EVP_PKEY_free(pub);
  11349. wolfSSL_ASN1_TIME_free(notBefore);
  11350. wolfSSL_ASN1_TIME_free(notAfter);
  11351. return EXPECT_RESULT();
  11352. }
  11353. /* override certificate version error */
  11354. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  11355. {
  11356. EXPECT_DECLS;
  11357. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  11358. ExpectIntEQ(store->error, ASN_VERSION_E);
  11359. #else
  11360. ExpectIntEQ(store->error, 0);
  11361. #endif
  11362. ExpectIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  11363. (void)preverify;
  11364. return EXPECT_RESULT() == TEST_SUCCESS;
  11365. }
  11366. /* set verify callback that will override bad certificate version */
  11367. static int test_set_override_x509(WOLFSSL_CTX* ctx)
  11368. {
  11369. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  11370. return TEST_SUCCESS;
  11371. }
  11372. #endif
  11373. static int test_wolfSSL_X509_TLS_version_test_1(void)
  11374. {
  11375. EXPECT_DECLS;
  11376. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11377. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11378. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11379. test_ssl_cbf func_cb_client;
  11380. test_ssl_cbf func_cb_server;
  11381. /* test server rejects a client certificate that is not version 3 */
  11382. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  11383. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  11384. func_cb_client.ctx_ready = &test_set_x509_badversion;
  11385. #ifndef WOLFSSL_NO_TLS12
  11386. func_cb_client.method = wolfTLSv1_2_client_method;
  11387. #else
  11388. func_cb_client.method = wolfTLSv1_3_client_method;
  11389. #endif
  11390. #ifndef WOLFSSL_NO_TLS12
  11391. func_cb_server.method = wolfTLSv1_2_server_method;
  11392. #else
  11393. func_cb_server.method = wolfTLSv1_3_server_method;
  11394. #endif
  11395. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  11396. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11397. &func_cb_server, NULL), TEST_FAIL);
  11398. #else
  11399. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11400. &func_cb_server, NULL), TEST_SUCCESS);
  11401. #endif
  11402. #endif
  11403. return EXPECT_RESULT();
  11404. }
  11405. static int test_wolfSSL_X509_TLS_version_test_2(void)
  11406. {
  11407. EXPECT_DECLS;
  11408. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11409. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11410. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11411. test_ssl_cbf func_cb_client;
  11412. test_ssl_cbf func_cb_server;
  11413. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  11414. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  11415. func_cb_client.ctx_ready = &test_set_x509_badversion;
  11416. func_cb_server.ctx_ready = &test_set_override_x509;
  11417. #ifndef WOLFSSL_NO_TLS12
  11418. func_cb_client.method = wolfTLSv1_2_client_method;
  11419. #else
  11420. func_cb_client.method = wolfTLSv1_3_client_method;
  11421. #endif
  11422. #ifndef WOLFSSL_NO_TLS12
  11423. func_cb_server.method = wolfTLSv1_2_server_method;
  11424. #else
  11425. func_cb_server.method = wolfTLSv1_3_server_method;
  11426. #endif
  11427. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11428. &func_cb_server, NULL), TEST_SUCCESS);
  11429. #endif
  11430. return EXPECT_RESULT();
  11431. }
  11432. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  11433. * version allowed.
  11434. * POST: 1 on success.
  11435. */
  11436. static int test_wolfSSL_CTX_SetMinVersion(void)
  11437. {
  11438. int res = TEST_SKIPPED;
  11439. #ifndef NO_WOLFSSL_CLIENT
  11440. int failFlag = WOLFSSL_SUCCESS;
  11441. WOLFSSL_CTX* ctx;
  11442. int itr;
  11443. #ifndef NO_OLD_TLS
  11444. const int versions[] = {
  11445. #ifdef WOLFSSL_ALLOW_TLSV10
  11446. WOLFSSL_TLSV1,
  11447. #endif
  11448. WOLFSSL_TLSV1_1,
  11449. WOLFSSL_TLSV1_2 };
  11450. #elif !defined(WOLFSSL_NO_TLS12)
  11451. const int versions[] = { WOLFSSL_TLSV1_2 };
  11452. #elif defined(WOLFSSL_TLS13)
  11453. const int versions[] = { WOLFSSL_TLSV1_3 };
  11454. #else
  11455. const int versions[0];
  11456. #endif
  11457. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  11458. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  11459. if (wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr))
  11460. != WOLFSSL_SUCCESS) {
  11461. failFlag = WOLFSSL_FAILURE;
  11462. }
  11463. }
  11464. wolfSSL_CTX_free(ctx);
  11465. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  11466. #endif
  11467. return res;
  11468. } /* END test_wolfSSL_CTX_SetMinVersion */
  11469. /*----------------------------------------------------------------------------*
  11470. | OCSP Stapling
  11471. *----------------------------------------------------------------------------*/
  11472. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  11473. * need to contact the CA, lowering the cost of cert revocation checking.
  11474. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  11475. * POST: 1 returned for success.
  11476. */
  11477. static int test_wolfSSL_UseOCSPStapling(void)
  11478. {
  11479. EXPECT_DECLS;
  11480. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  11481. !defined(NO_WOLFSSL_CLIENT)
  11482. WOLFSSL_CTX* ctx = NULL;
  11483. WOLFSSL* ssl = NULL;
  11484. #ifndef NO_WOLFSSL_CLIENT
  11485. #ifndef WOLFSSL_NO_TLS12
  11486. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11487. #else
  11488. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11489. #endif
  11490. #else
  11491. #ifndef WOLFSSL_NO_TLS12
  11492. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11493. #else
  11494. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11495. #endif
  11496. #endif
  11497. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11498. ExpectIntEQ(wolfSSL_UseOCSPStapling(NULL, WOLFSSL_CSR2_OCSP,
  11499. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11500. #ifndef NO_WOLFSSL_CLIENT
  11501. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  11502. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  11503. #else
  11504. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  11505. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11506. #endif
  11507. wolfSSL_free(ssl);
  11508. wolfSSL_CTX_free(ctx);
  11509. #endif
  11510. return EXPECT_RESULT();
  11511. } /* END test_wolfSSL_UseOCSPStapling */
  11512. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  11513. * stapling eliminates the need to contact the CA and lowers cert revocation
  11514. * check.
  11515. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  11516. */
  11517. static int test_wolfSSL_UseOCSPStaplingV2(void)
  11518. {
  11519. EXPECT_DECLS;
  11520. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  11521. !defined(NO_WOLFSSL_CLIENT)
  11522. WOLFSSL_CTX* ctx = NULL;
  11523. WOLFSSL* ssl = NULL;
  11524. #ifndef NO_WOLFSSL_CLIENT
  11525. #ifndef WOLFSSL_NO_TLS12
  11526. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11527. #else
  11528. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11529. #endif
  11530. #else
  11531. #ifndef WOLFSSL_NO_TLS12
  11532. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11533. #else
  11534. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11535. #endif
  11536. #endif
  11537. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11538. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(NULL, WOLFSSL_CSR2_OCSP,
  11539. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11540. #ifndef NO_WOLFSSL_CLIENT
  11541. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  11542. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  11543. #else
  11544. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  11545. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11546. #endif
  11547. wolfSSL_free(ssl);
  11548. wolfSSL_CTX_free(ctx);
  11549. #endif
  11550. return EXPECT_RESULT();
  11551. } /* END test_wolfSSL_UseOCSPStaplingV2 */
  11552. /*----------------------------------------------------------------------------*
  11553. | Multicast Tests
  11554. *----------------------------------------------------------------------------*/
  11555. static int test_wolfSSL_mcast(void)
  11556. {
  11557. EXPECT_DECLS;
  11558. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  11559. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  11560. WOLFSSL_CTX* ctx = NULL;
  11561. WOLFSSL* ssl = NULL;
  11562. byte preMasterSecret[512];
  11563. byte clientRandom[32];
  11564. byte serverRandom[32];
  11565. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  11566. byte buf[256];
  11567. word16 newId;
  11568. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  11569. ExpectIntEQ(wolfSSL_CTX_mcast_set_member_id(ctx, 0), WOLFSSL_SUCCESS);
  11570. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11571. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  11572. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  11573. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  11574. ExpectIntEQ(wolfSSL_set_secret(ssl, 23, preMasterSecret,
  11575. sizeof(preMasterSecret), clientRandom, serverRandom, suite),
  11576. WOLFSSL_SUCCESS);
  11577. ExpectIntLE(wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf)), 0);
  11578. ExpectIntLE(newId, 100);
  11579. wolfSSL_free(ssl);
  11580. wolfSSL_CTX_free(ctx);
  11581. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 ||
  11582. * WOLFSSL_SNIFFER) */
  11583. return EXPECT_RESULT();
  11584. }
  11585. /*----------------------------------------------------------------------------*
  11586. | Wolfcrypt
  11587. *----------------------------------------------------------------------------*/
  11588. /*
  11589. * Unit test for the wc_InitBlake2b()
  11590. */
  11591. static int test_wc_InitBlake2b(void)
  11592. {
  11593. EXPECT_DECLS;
  11594. #ifdef HAVE_BLAKE2
  11595. Blake2b blake;
  11596. /* Test good arg. */
  11597. ExpectIntEQ(wc_InitBlake2b(&blake, 64), 0);
  11598. /* Test bad arg. */
  11599. ExpectIntEQ(wc_InitBlake2b(NULL, 64), BAD_FUNC_ARG);
  11600. ExpectIntEQ(wc_InitBlake2b(NULL, 128), BAD_FUNC_ARG);
  11601. ExpectIntEQ(wc_InitBlake2b(&blake, 128), BAD_FUNC_ARG);
  11602. ExpectIntEQ(wc_InitBlake2b(NULL, 0), BAD_FUNC_ARG);
  11603. ExpectIntEQ(wc_InitBlake2b(&blake, 0), BAD_FUNC_ARG);
  11604. #endif
  11605. return EXPECT_RESULT();
  11606. } /* END test_wc_InitBlake2b*/
  11607. /*
  11608. * Unit test for the wc_InitBlake2b_WithKey()
  11609. */
  11610. static int test_wc_InitBlake2b_WithKey(void)
  11611. {
  11612. EXPECT_DECLS;
  11613. #ifdef HAVE_BLAKE2
  11614. Blake2b blake;
  11615. word32 digestSz = BLAKE2B_KEYBYTES;
  11616. byte key[BLAKE2B_KEYBYTES];
  11617. word32 keylen = BLAKE2B_KEYBYTES;
  11618. XMEMSET(key, 0, sizeof(key));
  11619. /* Test good arg. */
  11620. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen), 0);
  11621. /* Test bad args. */
  11622. ExpectIntEQ(wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen),
  11623. BAD_FUNC_ARG);
  11624. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, 256),
  11625. BAD_FUNC_ARG);
  11626. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen), 0);
  11627. #endif
  11628. return EXPECT_RESULT();
  11629. } /* END wc_InitBlake2b_WithKey*/
  11630. /*
  11631. * Unit test for the wc_InitBlake2s_WithKey()
  11632. */
  11633. static int test_wc_InitBlake2s_WithKey(void)
  11634. {
  11635. EXPECT_DECLS;
  11636. #ifdef HAVE_BLAKE2S
  11637. Blake2s blake;
  11638. word32 digestSz = BLAKE2S_KEYBYTES;
  11639. byte *key = (byte*)"01234567890123456789012345678901";
  11640. word32 keylen = BLAKE2S_KEYBYTES;
  11641. /* Test good arg. */
  11642. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen), 0);
  11643. /* Test bad args. */
  11644. ExpectIntEQ(wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen),
  11645. BAD_FUNC_ARG);
  11646. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, 256),
  11647. BAD_FUNC_ARG);
  11648. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen), 0);
  11649. #endif
  11650. return EXPECT_RESULT();
  11651. } /* END wc_InitBlake2s_WithKey*/
  11652. /*
  11653. * Unit test for the wc_InitMd5()
  11654. */
  11655. static int test_wc_InitMd5(void)
  11656. {
  11657. EXPECT_DECLS;
  11658. #ifndef NO_MD5
  11659. wc_Md5 md5;
  11660. /* Test good arg. */
  11661. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11662. /* Test bad arg. */
  11663. ExpectIntEQ(wc_InitMd5(NULL), BAD_FUNC_ARG);
  11664. wc_Md5Free(&md5);
  11665. #endif
  11666. return EXPECT_RESULT();
  11667. } /* END test_wc_InitMd5 */
  11668. /*
  11669. * Testing wc_UpdateMd5()
  11670. */
  11671. static int test_wc_Md5Update(void)
  11672. {
  11673. EXPECT_DECLS;
  11674. #ifndef NO_MD5
  11675. wc_Md5 md5;
  11676. byte hash[WC_MD5_DIGEST_SIZE];
  11677. testVector a, b, c;
  11678. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11679. /* Input */
  11680. a.input = "a";
  11681. a.inLen = XSTRLEN(a.input);
  11682. ExpectIntEQ(wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen), 0);
  11683. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  11684. /* Update input. */
  11685. a.input = "abc";
  11686. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  11687. "\x72";
  11688. a.inLen = XSTRLEN(a.input);
  11689. a.outLen = XSTRLEN(a.output);
  11690. ExpectIntEQ(wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen), 0);
  11691. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  11692. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  11693. /* Pass in bad values. */
  11694. b.input = NULL;
  11695. b.inLen = 0;
  11696. ExpectIntEQ(wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen), 0);
  11697. c.input = NULL;
  11698. c.inLen = WC_MD5_DIGEST_SIZE;
  11699. ExpectIntEQ(wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen),
  11700. BAD_FUNC_ARG);
  11701. ExpectIntEQ(wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen),
  11702. BAD_FUNC_ARG);
  11703. wc_Md5Free(&md5);
  11704. #endif
  11705. return EXPECT_RESULT();
  11706. } /* END test_wc_Md5Update() */
  11707. /*
  11708. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  11709. */
  11710. static int test_wc_Md5Final(void)
  11711. {
  11712. EXPECT_DECLS;
  11713. #ifndef NO_MD5
  11714. /* Instantiate */
  11715. wc_Md5 md5;
  11716. byte* hash_test[3];
  11717. byte hash1[WC_MD5_DIGEST_SIZE];
  11718. byte hash2[2*WC_MD5_DIGEST_SIZE];
  11719. byte hash3[5*WC_MD5_DIGEST_SIZE];
  11720. int times, i;
  11721. /* Initialize */
  11722. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11723. hash_test[0] = hash1;
  11724. hash_test[1] = hash2;
  11725. hash_test[2] = hash3;
  11726. times = sizeof(hash_test)/sizeof(byte*);
  11727. for (i = 0; i < times; i++) {
  11728. ExpectIntEQ(wc_Md5Final(&md5, hash_test[i]), 0);
  11729. }
  11730. /* Test bad args. */
  11731. ExpectIntEQ(wc_Md5Final(NULL, NULL), BAD_FUNC_ARG);
  11732. ExpectIntEQ(wc_Md5Final(NULL, hash1), BAD_FUNC_ARG);
  11733. ExpectIntEQ(wc_Md5Final(&md5, NULL), BAD_FUNC_ARG);
  11734. wc_Md5Free(&md5);
  11735. #endif
  11736. return EXPECT_RESULT();
  11737. }
  11738. /*
  11739. * Unit test for the wc_InitSha()
  11740. */
  11741. static int test_wc_InitSha(void)
  11742. {
  11743. EXPECT_DECLS;
  11744. #ifndef NO_SHA
  11745. wc_Sha sha;
  11746. /* Test good arg. */
  11747. ExpectIntEQ(wc_InitSha(&sha), 0);
  11748. /* Test bad arg. */
  11749. ExpectIntEQ(wc_InitSha(NULL), BAD_FUNC_ARG);
  11750. wc_ShaFree(&sha);
  11751. #endif
  11752. return EXPECT_RESULT();
  11753. } /* END test_wc_InitSha */
  11754. /*
  11755. * Tesing wc_ShaUpdate()
  11756. */
  11757. static int test_wc_ShaUpdate(void)
  11758. {
  11759. EXPECT_DECLS;
  11760. #ifndef NO_SHA
  11761. wc_Sha sha;
  11762. byte hash[WC_SHA_DIGEST_SIZE];
  11763. testVector a, b, c;
  11764. ExpectIntEQ(wc_InitSha(&sha), 0);
  11765. /* Input. */
  11766. a.input = "a";
  11767. a.inLen = XSTRLEN(a.input);
  11768. ExpectIntEQ(wc_ShaUpdate(&sha, NULL, 0), 0);
  11769. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, 0), 0);
  11770. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  11771. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  11772. /* Update input. */
  11773. a.input = "abc";
  11774. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  11775. "\x6C\x9C\xD0\xD8\x9D";
  11776. a.inLen = XSTRLEN(a.input);
  11777. a.outLen = XSTRLEN(a.output);
  11778. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  11779. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  11780. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  11781. /* Try passing in bad values. */
  11782. b.input = NULL;
  11783. b.inLen = 0;
  11784. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen), 0);
  11785. c.input = NULL;
  11786. c.inLen = WC_SHA_DIGEST_SIZE;
  11787. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen),
  11788. BAD_FUNC_ARG);
  11789. ExpectIntEQ(wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  11790. BAD_FUNC_ARG);
  11791. wc_ShaFree(&sha);
  11792. #endif
  11793. return EXPECT_RESULT();
  11794. } /* END test_wc_ShaUpdate() */
  11795. /*
  11796. * Unit test on wc_ShaFinal
  11797. */
  11798. static int test_wc_ShaFinal(void)
  11799. {
  11800. EXPECT_DECLS;
  11801. #ifndef NO_SHA
  11802. wc_Sha sha;
  11803. byte* hash_test[3];
  11804. byte hash1[WC_SHA_DIGEST_SIZE];
  11805. byte hash2[2*WC_SHA_DIGEST_SIZE];
  11806. byte hash3[5*WC_SHA_DIGEST_SIZE];
  11807. int times, i;
  11808. /* Initialize*/
  11809. ExpectIntEQ(wc_InitSha(&sha), 0);
  11810. hash_test[0] = hash1;
  11811. hash_test[1] = hash2;
  11812. hash_test[2] = hash3;
  11813. times = sizeof(hash_test)/sizeof(byte*);
  11814. for (i = 0; i < times; i++) {
  11815. ExpectIntEQ(wc_ShaFinal(&sha, hash_test[i]), 0);
  11816. }
  11817. /* Test bad args. */
  11818. ExpectIntEQ(wc_ShaFinal(NULL, NULL), BAD_FUNC_ARG);
  11819. ExpectIntEQ(wc_ShaFinal(NULL, hash1), BAD_FUNC_ARG);
  11820. ExpectIntEQ(wc_ShaFinal(&sha, NULL), BAD_FUNC_ARG);
  11821. wc_ShaFree(&sha);
  11822. #endif
  11823. return EXPECT_RESULT();
  11824. } /* END test_wc_ShaFinal */
  11825. /*
  11826. * Unit test for wc_InitSha256()
  11827. */
  11828. static int test_wc_InitSha256(void)
  11829. {
  11830. EXPECT_DECLS;
  11831. #ifndef NO_SHA256
  11832. wc_Sha256 sha256;
  11833. /* Test good arg. */
  11834. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11835. /* Test bad arg. */
  11836. ExpectIntEQ(wc_InitSha256(NULL), BAD_FUNC_ARG);
  11837. wc_Sha256Free(&sha256);
  11838. #endif
  11839. return EXPECT_RESULT();
  11840. } /* END test_wc_InitSha256 */
  11841. /*
  11842. * Unit test for wc_Sha256Update()
  11843. */
  11844. static int test_wc_Sha256Update(void)
  11845. {
  11846. EXPECT_DECLS;
  11847. #ifndef NO_SHA256
  11848. wc_Sha256 sha256;
  11849. byte hash[WC_SHA256_DIGEST_SIZE];
  11850. byte hash_unaligned[WC_SHA256_DIGEST_SIZE+1];
  11851. testVector a, b, c;
  11852. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11853. /* Input. */
  11854. a.input = "a";
  11855. a.inLen = XSTRLEN(a.input);
  11856. ExpectIntEQ(wc_Sha256Update(&sha256, NULL, 0), 0);
  11857. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, 0), 0);
  11858. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  11859. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  11860. /* Update input. */
  11861. a.input = "abc";
  11862. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  11863. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  11864. "\x15\xAD";
  11865. a.inLen = XSTRLEN(a.input);
  11866. a.outLen = XSTRLEN(a.output);
  11867. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  11868. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  11869. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  11870. /* Unaligned check. */
  11871. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input+1, (word32)a.inLen-1),
  11872. 0);
  11873. ExpectIntEQ(wc_Sha256Final(&sha256, hash_unaligned + 1), 0);
  11874. /* Try passing in bad values */
  11875. b.input = NULL;
  11876. b.inLen = 0;
  11877. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen), 0);
  11878. c.input = NULL;
  11879. c.inLen = WC_SHA256_DIGEST_SIZE;
  11880. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen),
  11881. BAD_FUNC_ARG);
  11882. ExpectIntEQ(wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen),
  11883. BAD_FUNC_ARG);
  11884. wc_Sha256Free(&sha256);
  11885. #endif
  11886. return EXPECT_RESULT();
  11887. } /* END test_wc_Sha256Update */
  11888. /*
  11889. * Unit test function for wc_Sha256Final()
  11890. */
  11891. static int test_wc_Sha256Final(void)
  11892. {
  11893. EXPECT_DECLS;
  11894. #ifndef NO_SHA256
  11895. wc_Sha256 sha256;
  11896. byte* hash_test[3];
  11897. byte hash1[WC_SHA256_DIGEST_SIZE];
  11898. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  11899. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  11900. int times, i;
  11901. /* Initialize */
  11902. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11903. hash_test[0] = hash1;
  11904. hash_test[1] = hash2;
  11905. hash_test[2] = hash3;
  11906. times = sizeof(hash_test) / sizeof(byte*);
  11907. for (i = 0; i < times; i++) {
  11908. ExpectIntEQ(wc_Sha256Final(&sha256, hash_test[i]), 0);
  11909. }
  11910. /* Test bad args. */
  11911. ExpectIntEQ(wc_Sha256Final(NULL, NULL), BAD_FUNC_ARG);
  11912. ExpectIntEQ(wc_Sha256Final(NULL, hash1), BAD_FUNC_ARG);
  11913. ExpectIntEQ(wc_Sha256Final(&sha256, NULL), BAD_FUNC_ARG);
  11914. wc_Sha256Free(&sha256);
  11915. #endif
  11916. return EXPECT_RESULT();
  11917. } /* END test_wc_Sha256Final */
  11918. /*
  11919. * Unit test function for wc_Sha256FinalRaw()
  11920. */
  11921. static int test_wc_Sha256FinalRaw(void)
  11922. {
  11923. EXPECT_DECLS;
  11924. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  11925. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  11926. !defined(WOLFSSL_NO_HASH_RAW)
  11927. wc_Sha256 sha256;
  11928. byte* hash_test[3];
  11929. byte hash1[WC_SHA256_DIGEST_SIZE];
  11930. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  11931. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  11932. int times, i;
  11933. /* Initialize */
  11934. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11935. hash_test[0] = hash1;
  11936. hash_test[1] = hash2;
  11937. hash_test[2] = hash3;
  11938. times = sizeof(hash_test) / sizeof(byte*);
  11939. for (i = 0; i < times; i++) {
  11940. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, hash_test[i]), 0);
  11941. }
  11942. /* Test bad args. */
  11943. ExpectIntEQ(wc_Sha256FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  11944. ExpectIntEQ(wc_Sha256FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  11945. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, NULL), BAD_FUNC_ARG);
  11946. wc_Sha256Free(&sha256);
  11947. #endif
  11948. return EXPECT_RESULT();
  11949. } /* END test_wc_Sha256FinalRaw */
  11950. /*
  11951. * Unit test function for wc_Sha256GetFlags()
  11952. */
  11953. static int test_wc_Sha256GetFlags(void)
  11954. {
  11955. EXPECT_DECLS;
  11956. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  11957. wc_Sha256 sha256;
  11958. word32 flags = 0;
  11959. /* Initialize */
  11960. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11961. ExpectIntEQ(wc_Sha256GetFlags(&sha256, &flags), 0);
  11962. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11963. wc_Sha256Free(&sha256);
  11964. #endif
  11965. return EXPECT_RESULT();
  11966. } /* END test_wc_Sha256GetFlags */
  11967. /*
  11968. * Unit test function for wc_Sha256Free()
  11969. */
  11970. static int test_wc_Sha256Free(void)
  11971. {
  11972. EXPECT_DECLS;
  11973. #ifndef NO_SHA256
  11974. wc_Sha256Free(NULL);
  11975. /* Set result to SUCCESS. */
  11976. ExpectTrue(1);
  11977. #endif
  11978. return EXPECT_RESULT();
  11979. } /* END test_wc_Sha256Free */
  11980. /*
  11981. * Unit test function for wc_Sha256GetHash()
  11982. */
  11983. static int test_wc_Sha256GetHash(void)
  11984. {
  11985. EXPECT_DECLS;
  11986. #ifndef NO_SHA256
  11987. wc_Sha256 sha256;
  11988. byte hash1[WC_SHA256_DIGEST_SIZE];
  11989. /* Initialize */
  11990. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11991. ExpectIntEQ(wc_Sha256GetHash(&sha256, hash1), 0);
  11992. /* test bad arguments*/
  11993. ExpectIntEQ(wc_Sha256GetHash(NULL, NULL), BAD_FUNC_ARG);
  11994. ExpectIntEQ(wc_Sha256GetHash(NULL, hash1), BAD_FUNC_ARG);
  11995. ExpectIntEQ(wc_Sha256GetHash(&sha256, NULL), BAD_FUNC_ARG);
  11996. wc_Sha256Free(&sha256);
  11997. #endif
  11998. return EXPECT_RESULT();
  11999. } /* END test_wc_Sha256GetHash */
  12000. /*
  12001. * Unit test function for wc_Sha256Copy()
  12002. */
  12003. static int test_wc_Sha256Copy(void)
  12004. {
  12005. EXPECT_DECLS;
  12006. #ifndef NO_SHA256
  12007. wc_Sha256 sha256;
  12008. wc_Sha256 temp;
  12009. XMEMSET(&sha256, 0, sizeof(sha256));
  12010. XMEMSET(&temp, 0, sizeof(temp));
  12011. /* Initialize */
  12012. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  12013. ExpectIntEQ(wc_InitSha256(&temp), 0);
  12014. ExpectIntEQ(wc_Sha256Copy(&sha256, &temp), 0);
  12015. /* test bad arguments*/
  12016. ExpectIntEQ(wc_Sha256Copy(NULL, NULL), BAD_FUNC_ARG);
  12017. ExpectIntEQ(wc_Sha256Copy(NULL, &temp), BAD_FUNC_ARG);
  12018. ExpectIntEQ(wc_Sha256Copy(&sha256, NULL), BAD_FUNC_ARG);
  12019. wc_Sha256Free(&sha256);
  12020. wc_Sha256Free(&temp);
  12021. #endif
  12022. return EXPECT_RESULT();
  12023. } /* END test_wc_Sha256Copy */
  12024. /*
  12025. * Testing wc_InitSha512()
  12026. */
  12027. static int test_wc_InitSha512(void)
  12028. {
  12029. EXPECT_DECLS;
  12030. #ifdef WOLFSSL_SHA512
  12031. wc_Sha512 sha512;
  12032. /* Test good arg. */
  12033. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12034. /* Test bad arg. */
  12035. ExpectIntEQ(wc_InitSha512(NULL), BAD_FUNC_ARG);
  12036. wc_Sha512Free(&sha512);
  12037. #endif
  12038. return EXPECT_RESULT();
  12039. } /* END test_wc_InitSha512 */
  12040. /*
  12041. * wc_Sha512Update() test.
  12042. */
  12043. static int test_wc_Sha512Update(void)
  12044. {
  12045. EXPECT_DECLS;
  12046. #ifdef WOLFSSL_SHA512
  12047. wc_Sha512 sha512;
  12048. byte hash[WC_SHA512_DIGEST_SIZE];
  12049. byte hash_unaligned[WC_SHA512_DIGEST_SIZE + 1];
  12050. testVector a, b, c;
  12051. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12052. /* Input. */
  12053. a.input = "a";
  12054. a.inLen = XSTRLEN(a.input);
  12055. ExpectIntEQ(wc_Sha512Update(&sha512, NULL, 0), 0);
  12056. ExpectIntEQ(wc_Sha512Update(&sha512,(byte*)a.input, 0), 0);
  12057. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen), 0);
  12058. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  12059. /* Update input. */
  12060. a.input = "abc";
  12061. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  12062. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  12063. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  12064. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  12065. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  12066. a.inLen = XSTRLEN(a.input);
  12067. a.outLen = XSTRLEN(a.output);
  12068. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen), 0);
  12069. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  12070. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE), 0);
  12071. /* Unaligned check. */
  12072. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input+1, (word32)a.inLen-1),
  12073. 0);
  12074. ExpectIntEQ(wc_Sha512Final(&sha512, hash_unaligned+1), 0);
  12075. /* Try passing in bad values */
  12076. b.input = NULL;
  12077. b.inLen = 0;
  12078. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen), 0);
  12079. c.input = NULL;
  12080. c.inLen = WC_SHA512_DIGEST_SIZE;
  12081. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12082. BAD_FUNC_ARG);
  12083. ExpectIntEQ(wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen),
  12084. BAD_FUNC_ARG);
  12085. wc_Sha512Free(&sha512);
  12086. #endif
  12087. return EXPECT_RESULT();
  12088. } /* END test_wc_Sha512Update */
  12089. #ifdef WOLFSSL_SHA512
  12090. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12091. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  12092. /* Performs test for
  12093. * - wc_Sha512Final/wc_Sha512FinalRaw
  12094. * - wc_Sha512_224Final/wc_Sha512_224Final
  12095. * - wc_Sha512_256Final/wc_Sha512_256Final
  12096. * parameter:
  12097. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  12098. * WC_HASH_TYPE_SHA512_256
  12099. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  12100. *return 0 on success
  12101. */
  12102. static int test_Sha512_Family_Final(int type, int isRaw)
  12103. {
  12104. EXPECT_DECLS;
  12105. wc_Sha512 sha512;
  12106. byte* hash_test[3];
  12107. byte hash1[WC_SHA512_DIGEST_SIZE];
  12108. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12109. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12110. int times, i;
  12111. int(*initFp)(wc_Sha512*);
  12112. int(*finalFp)(wc_Sha512*, byte*);
  12113. void(*freeFp)(wc_Sha512*);
  12114. if (type == WC_HASH_TYPE_SHA512) {
  12115. initFp = wc_InitSha512;
  12116. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12117. !defined(WOLFSSL_NO_HASH_RAW)
  12118. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  12119. #else
  12120. finalFp = (isRaw)? NULL : wc_Sha512Final;
  12121. #endif
  12122. freeFp = wc_Sha512Free;
  12123. }
  12124. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12125. #if !defined(WOLFSSL_NOSHA512_224)
  12126. else if (type == WC_HASH_TYPE_SHA512_224) {
  12127. initFp = wc_InitSha512_224;
  12128. #if !defined(WOLFSSL_NO_HASH_RAW)
  12129. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  12130. #else
  12131. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  12132. #endif
  12133. freeFp = wc_Sha512_224Free;
  12134. }
  12135. #endif
  12136. #if !defined(WOLFSSL_NOSHA512_256)
  12137. else if (type == WC_HASH_TYPE_SHA512_256) {
  12138. initFp = wc_InitSha512_256;
  12139. #if !defined(WOLFSSL_NO_HASH_RAW)
  12140. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  12141. #else
  12142. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  12143. #endif
  12144. freeFp = wc_Sha512_256Free;
  12145. }
  12146. #endif
  12147. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12148. else
  12149. return TEST_FAIL;
  12150. /* Initialize */
  12151. ExpectIntEQ(initFp(&sha512), 0);
  12152. hash_test[0] = hash1;
  12153. hash_test[1] = hash2;
  12154. hash_test[2] = hash3;
  12155. times = sizeof(hash_test) / sizeof(byte *);
  12156. /* Good test args. */
  12157. for (i = 0; i < times; i++) {
  12158. ExpectIntEQ(finalFp(&sha512, hash_test[i]), 0);
  12159. }
  12160. /* Test bad args. */
  12161. ExpectIntEQ(finalFp(NULL, NULL), BAD_FUNC_ARG);
  12162. ExpectIntEQ(finalFp(NULL, hash1), BAD_FUNC_ARG);
  12163. ExpectIntEQ(finalFp(&sha512, NULL), BAD_FUNC_ARG);
  12164. freeFp(&sha512);
  12165. return EXPECT_RESULT();
  12166. }
  12167. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  12168. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  12169. #endif /* WOLFSSL_SHA512 */
  12170. /*
  12171. * Unit test function for wc_Sha512Final()
  12172. */
  12173. static int test_wc_Sha512Final(void)
  12174. {
  12175. EXPECT_DECLS;
  12176. #ifdef WOLFSSL_SHA512
  12177. wc_Sha512 sha512;
  12178. byte* hash_test[3];
  12179. byte hash1[WC_SHA512_DIGEST_SIZE];
  12180. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12181. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12182. int times, i;
  12183. /* Initialize */
  12184. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12185. hash_test[0] = hash1;
  12186. hash_test[1] = hash2;
  12187. hash_test[2] = hash3;
  12188. times = sizeof(hash_test) / sizeof(byte *);
  12189. for (i = 0; i < times; i++) {
  12190. ExpectIntEQ(wc_Sha512Final(&sha512, hash_test[i]), 0);
  12191. }
  12192. /* Test bad args. */
  12193. ExpectIntEQ(wc_Sha512Final(NULL, NULL), BAD_FUNC_ARG);
  12194. ExpectIntEQ(wc_Sha512Final(NULL, hash1), BAD_FUNC_ARG);
  12195. ExpectIntEQ(wc_Sha512Final(&sha512, NULL), BAD_FUNC_ARG);
  12196. wc_Sha512Free(&sha512);
  12197. #endif
  12198. return EXPECT_RESULT();
  12199. } /* END test_wc_Sha512Final */
  12200. /*
  12201. * Unit test function for wc_Sha512GetFlags()
  12202. */
  12203. static int test_wc_Sha512GetFlags(void)
  12204. {
  12205. EXPECT_DECLS;
  12206. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  12207. wc_Sha512 sha512;
  12208. word32 flags = 0;
  12209. /* Initialize */
  12210. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12211. ExpectIntEQ(wc_Sha512GetFlags(&sha512, &flags), 0);
  12212. ExpectIntEQ((flags & WC_HASH_FLAG_ISCOPY), 0);
  12213. wc_Sha512Free(&sha512);
  12214. #endif
  12215. return EXPECT_RESULT();
  12216. } /* END test_wc_Sha512GetFlags */
  12217. /*
  12218. * Unit test function for wc_Sha512FinalRaw()
  12219. */
  12220. static int test_wc_Sha512FinalRaw(void)
  12221. {
  12222. EXPECT_DECLS;
  12223. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  12224. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  12225. !defined(WOLFSSL_NO_HASH_RAW)
  12226. wc_Sha512 sha512;
  12227. byte* hash_test[3];
  12228. byte hash1[WC_SHA512_DIGEST_SIZE];
  12229. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12230. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12231. int times, i;
  12232. /* Initialize */
  12233. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12234. hash_test[0] = hash1;
  12235. hash_test[1] = hash2;
  12236. hash_test[2] = hash3;
  12237. times = sizeof(hash_test) / sizeof(byte*);
  12238. /* Good test args. */
  12239. for (i = 0; i < times; i++) {
  12240. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, hash_test[i]), 0);
  12241. }
  12242. /* Test bad args. */
  12243. ExpectIntEQ(wc_Sha512FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12244. ExpectIntEQ(wc_Sha512FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12245. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, NULL), BAD_FUNC_ARG);
  12246. wc_Sha512Free(&sha512);
  12247. #endif
  12248. return EXPECT_RESULT();
  12249. } /* END test_wc_Sha512FinalRaw */
  12250. /*
  12251. * Unit test function for wc_Sha512Free()
  12252. */
  12253. static int test_wc_Sha512Free(void)
  12254. {
  12255. EXPECT_DECLS;
  12256. #ifdef WOLFSSL_SHA512
  12257. wc_Sha512Free(NULL);
  12258. /* Set result to SUCCESS. */
  12259. ExpectTrue(1);
  12260. #endif
  12261. return EXPECT_RESULT();
  12262. } /* END test_wc_Sha512Free */
  12263. #ifdef WOLFSSL_SHA512
  12264. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12265. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  12266. static int test_Sha512_Family_GetHash(int type )
  12267. {
  12268. EXPECT_DECLS;
  12269. int(*initFp)(wc_Sha512*);
  12270. int(*ghashFp)(wc_Sha512*, byte*);
  12271. wc_Sha512 sha512;
  12272. byte hash1[WC_SHA512_DIGEST_SIZE];
  12273. if (type == WC_HASH_TYPE_SHA512) {
  12274. initFp = wc_InitSha512;
  12275. ghashFp = wc_Sha512GetHash;
  12276. }
  12277. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12278. #if !defined(WOLFSSL_NOSHA512_224)
  12279. else if (type == WC_HASH_TYPE_SHA512_224) {
  12280. initFp = wc_InitSha512_224;
  12281. ghashFp = wc_Sha512_224GetHash;
  12282. }
  12283. #endif
  12284. #if !defined(WOLFSSL_NOSHA512_256)
  12285. else if (type == WC_HASH_TYPE_SHA512_256) {
  12286. initFp = wc_InitSha512_256;
  12287. ghashFp = wc_Sha512_256GetHash;
  12288. }
  12289. #endif
  12290. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12291. else {
  12292. initFp = NULL;
  12293. ghashFp = NULL;
  12294. }
  12295. if (initFp == NULL || ghashFp == NULL)
  12296. return TEST_FAIL;
  12297. ExpectIntEQ(initFp(&sha512), 0);
  12298. ExpectIntEQ(ghashFp(&sha512, hash1), 0);
  12299. /* test bad arguments*/
  12300. ExpectIntEQ(ghashFp(NULL, NULL), BAD_FUNC_ARG);
  12301. ExpectIntEQ(ghashFp(NULL, hash1), BAD_FUNC_ARG);
  12302. ExpectIntEQ(ghashFp(&sha512, NULL), BAD_FUNC_ARG);
  12303. wc_Sha512Free(&sha512);
  12304. return EXPECT_RESULT();
  12305. }
  12306. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  12307. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  12308. #endif /* WOLFSSL_SHA512 */
  12309. /*
  12310. * Unit test function for wc_Sha512GetHash()
  12311. */
  12312. static int test_wc_Sha512GetHash(void)
  12313. {
  12314. EXPECT_DECLS;
  12315. #ifdef WOLFSSL_SHA512
  12316. wc_Sha512 sha512;
  12317. byte hash1[WC_SHA512_DIGEST_SIZE];
  12318. /* Initialize */
  12319. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12320. ExpectIntEQ(wc_Sha512GetHash(&sha512, hash1), 0);
  12321. /* test bad arguments*/
  12322. ExpectIntEQ(wc_Sha512GetHash(NULL, NULL), BAD_FUNC_ARG);
  12323. ExpectIntEQ(wc_Sha512GetHash(NULL, hash1), BAD_FUNC_ARG);
  12324. ExpectIntEQ(wc_Sha512GetHash(&sha512, NULL), BAD_FUNC_ARG);
  12325. wc_Sha512Free(&sha512);
  12326. #endif
  12327. return EXPECT_RESULT();
  12328. } /* END test_wc_Sha512GetHash */
  12329. /*
  12330. * Unit test function for wc_Sha512Copy()
  12331. */
  12332. static int test_wc_Sha512Copy(void)
  12333. {
  12334. EXPECT_DECLS;
  12335. #ifdef WOLFSSL_SHA512
  12336. wc_Sha512 sha512;
  12337. wc_Sha512 temp;
  12338. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12339. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12340. /* Initialize */
  12341. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12342. ExpectIntEQ(wc_InitSha512(&temp), 0);
  12343. ExpectIntEQ(wc_Sha512Copy(&sha512, &temp), 0);
  12344. /* test bad arguments*/
  12345. ExpectIntEQ(wc_Sha512Copy(NULL, NULL), BAD_FUNC_ARG);
  12346. ExpectIntEQ(wc_Sha512Copy(NULL, &temp), BAD_FUNC_ARG);
  12347. ExpectIntEQ(wc_Sha512Copy(&sha512, NULL), BAD_FUNC_ARG);
  12348. wc_Sha512Free(&sha512);
  12349. wc_Sha512Free(&temp);
  12350. #endif
  12351. return EXPECT_RESULT();
  12352. } /* END test_wc_Sha512Copy */
  12353. static int test_wc_InitSha512_224(void)
  12354. {
  12355. EXPECT_DECLS;
  12356. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12357. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12358. wc_Sha512 sha512;
  12359. /* Test good arg. */
  12360. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12361. /* Test bad arg. */
  12362. ExpectIntEQ(wc_InitSha512_224(NULL), BAD_FUNC_ARG);
  12363. wc_Sha512_224Free(&sha512);
  12364. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12365. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12366. return EXPECT_RESULT();
  12367. }
  12368. static int test_wc_Sha512_224Update(void)
  12369. {
  12370. EXPECT_DECLS;
  12371. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12372. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12373. wc_Sha512 sha512;
  12374. byte hash[WC_SHA512_DIGEST_SIZE];
  12375. testVector a, c;
  12376. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12377. /* Input. */
  12378. a.input = "a";
  12379. a.inLen = XSTRLEN(a.input);
  12380. ExpectIntEQ(wc_Sha512_224Update(&sha512, NULL, 0), 0);
  12381. ExpectIntEQ(wc_Sha512_224Update(&sha512,(byte*)a.input, 0), 0);
  12382. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen),
  12383. 0);
  12384. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  12385. /* Update input. */
  12386. a.input = "abc";
  12387. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  12388. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  12389. a.inLen = XSTRLEN(a.input);
  12390. a.outLen = XSTRLEN(a.output);
  12391. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen),
  12392. 0);
  12393. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  12394. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE), 0);
  12395. c.input = NULL;
  12396. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  12397. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12398. BAD_FUNC_ARG);
  12399. ExpectIntEQ(wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen),
  12400. BAD_FUNC_ARG);
  12401. wc_Sha512_224Free(&sha512);
  12402. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12403. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12404. return EXPECT_RESULT();
  12405. }
  12406. static int test_wc_Sha512_224Final(void)
  12407. {
  12408. EXPECT_DECLS;
  12409. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12410. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12411. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0),
  12412. TEST_SUCCESS);
  12413. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12414. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12415. return EXPECT_RESULT();
  12416. }
  12417. static int test_wc_Sha512_224GetFlags(void)
  12418. {
  12419. EXPECT_DECLS;
  12420. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12421. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  12422. wc_Sha512 sha512;
  12423. wc_Sha512 copy;
  12424. word32 flags = 0;
  12425. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12426. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  12427. /* Initialize */
  12428. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12429. ExpectIntEQ(wc_InitSha512_224(&copy), 0);
  12430. ExpectIntEQ(wc_Sha512_224GetFlags(&sha512, &flags), 0);
  12431. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12432. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &copy), 0);
  12433. ExpectIntEQ(wc_Sha512_224GetFlags(&copy, &flags), 0);
  12434. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  12435. wc_Sha512_224Free(&copy);
  12436. wc_Sha512_224Free(&sha512);
  12437. #endif
  12438. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12439. return EXPECT_RESULT();
  12440. }
  12441. static int test_wc_Sha512_224FinalRaw(void)
  12442. {
  12443. EXPECT_DECLS;
  12444. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12445. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  12446. !defined(WOLFSSL_NO_HASH_RAW)
  12447. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1),
  12448. TEST_SUCCESS);
  12449. #endif
  12450. return EXPECT_RESULT();
  12451. }
  12452. static int test_wc_Sha512_224Free(void)
  12453. {
  12454. EXPECT_DECLS;
  12455. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12456. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12457. wc_Sha512_224Free(NULL);
  12458. /* Set result to SUCCESS. */
  12459. ExpectTrue(1);
  12460. #endif
  12461. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12462. return EXPECT_RESULT();
  12463. }
  12464. static int test_wc_Sha512_224GetHash(void)
  12465. {
  12466. EXPECT_DECLS;
  12467. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12468. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12469. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224),
  12470. TEST_SUCCESS);
  12471. #endif
  12472. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12473. return EXPECT_RESULT();
  12474. }
  12475. static int test_wc_Sha512_224Copy(void)
  12476. {
  12477. EXPECT_DECLS;
  12478. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12479. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12480. wc_Sha512 sha512;
  12481. wc_Sha512 temp;
  12482. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12483. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12484. /* Initialize */
  12485. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12486. ExpectIntEQ(wc_InitSha512_224(&temp), 0);
  12487. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &temp), 0);
  12488. /* test bad arguments*/
  12489. ExpectIntEQ(wc_Sha512_224Copy(NULL, NULL), BAD_FUNC_ARG);
  12490. ExpectIntEQ(wc_Sha512_224Copy(NULL, &temp), BAD_FUNC_ARG);
  12491. ExpectIntEQ(wc_Sha512_224Copy(&sha512, NULL), BAD_FUNC_ARG);
  12492. wc_Sha512_224Free(&sha512);
  12493. wc_Sha512_224Free(&temp);
  12494. #endif
  12495. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12496. return EXPECT_RESULT();
  12497. }
  12498. static int test_wc_InitSha512_256(void)
  12499. {
  12500. EXPECT_DECLS;
  12501. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12502. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12503. wc_Sha512 sha512;
  12504. /* Test good arg. */
  12505. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12506. /* Test bad arg. */
  12507. ExpectIntEQ(wc_InitSha512_256(NULL), BAD_FUNC_ARG);
  12508. wc_Sha512_256Free(&sha512);
  12509. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12510. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12511. return EXPECT_RESULT();
  12512. }
  12513. static int test_wc_Sha512_256Update(void)
  12514. {
  12515. EXPECT_DECLS;
  12516. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12517. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12518. wc_Sha512 sha512;
  12519. byte hash[WC_SHA512_DIGEST_SIZE];
  12520. testVector a, c;
  12521. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12522. /* Input. */
  12523. a.input = "a";
  12524. a.inLen = XSTRLEN(a.input);
  12525. ExpectIntEQ(wc_Sha512_256Update(&sha512, NULL, 0), 0);
  12526. ExpectIntEQ(wc_Sha512_256Update(&sha512,(byte*)a.input, 0), 0);
  12527. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen),
  12528. 0);
  12529. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  12530. /* Update input. */
  12531. a.input = "abc";
  12532. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  12533. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  12534. "\x07\xe7\xaf\x23";
  12535. a.inLen = XSTRLEN(a.input);
  12536. a.outLen = XSTRLEN(a.output);
  12537. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen),
  12538. 0);
  12539. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  12540. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE), 0);
  12541. c.input = NULL;
  12542. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  12543. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12544. BAD_FUNC_ARG);
  12545. ExpectIntEQ(wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen),
  12546. BAD_FUNC_ARG);
  12547. wc_Sha512_256Free(&sha512);
  12548. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12549. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12550. return EXPECT_RESULT();
  12551. }
  12552. static int test_wc_Sha512_256Final(void)
  12553. {
  12554. EXPECT_DECLS;
  12555. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12556. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12557. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0),
  12558. TEST_SUCCESS);
  12559. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12560. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12561. return EXPECT_RESULT();
  12562. }
  12563. static int test_wc_Sha512_256GetFlags(void)
  12564. {
  12565. EXPECT_DECLS;
  12566. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12567. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  12568. wc_Sha512 sha512, copy;
  12569. word32 flags = 0;
  12570. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12571. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  12572. /* Initialize */
  12573. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12574. ExpectIntEQ(wc_InitSha512_256(&copy), 0);
  12575. ExpectIntEQ(wc_Sha512_256GetFlags(&sha512, &flags), 0);
  12576. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12577. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &copy), 0);
  12578. ExpectIntEQ(wc_Sha512_256GetFlags(&copy, &flags), 0);
  12579. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  12580. wc_Sha512_256Free(&sha512);
  12581. #endif
  12582. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12583. return EXPECT_RESULT();
  12584. }
  12585. static int test_wc_Sha512_256FinalRaw(void)
  12586. {
  12587. EXPECT_DECLS;
  12588. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12589. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  12590. !defined(WOLFSSL_NO_HASH_RAW)
  12591. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1),
  12592. TEST_SUCCESS);
  12593. #endif
  12594. return EXPECT_RESULT();
  12595. }
  12596. static int test_wc_Sha512_256Free(void)
  12597. {
  12598. EXPECT_DECLS;
  12599. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12600. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12601. wc_Sha512_256Free(NULL);
  12602. /* Set result to SUCCESS. */
  12603. ExpectTrue(1);
  12604. #endif
  12605. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12606. return EXPECT_RESULT();
  12607. }
  12608. static int test_wc_Sha512_256GetHash(void)
  12609. {
  12610. EXPECT_DECLS;
  12611. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12612. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12613. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256),
  12614. TEST_SUCCESS);
  12615. #endif
  12616. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12617. return EXPECT_RESULT();
  12618. }
  12619. static int test_wc_Sha512_256Copy(void)
  12620. {
  12621. EXPECT_DECLS;
  12622. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12623. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12624. wc_Sha512 sha512;
  12625. wc_Sha512 temp;
  12626. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12627. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12628. /* Initialize */
  12629. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12630. ExpectIntEQ(wc_InitSha512_256(&temp), 0);
  12631. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &temp), 0);
  12632. /* test bad arguments*/
  12633. ExpectIntEQ(wc_Sha512_256Copy(NULL, NULL), BAD_FUNC_ARG);
  12634. ExpectIntEQ(wc_Sha512_256Copy(NULL, &temp), BAD_FUNC_ARG);
  12635. ExpectIntEQ(wc_Sha512_256Copy(&sha512, NULL), BAD_FUNC_ARG);
  12636. wc_Sha512_256Free(&sha512);
  12637. wc_Sha512_256Free(&temp);
  12638. #endif
  12639. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12640. return EXPECT_RESULT();
  12641. }
  12642. /*
  12643. * Testing wc_InitSha384()
  12644. */
  12645. static int test_wc_InitSha384(void)
  12646. {
  12647. EXPECT_DECLS;
  12648. #ifdef WOLFSSL_SHA384
  12649. wc_Sha384 sha384;
  12650. /* Test good arg. */
  12651. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12652. /* Test bad arg. */
  12653. ExpectIntEQ(wc_InitSha384(NULL), BAD_FUNC_ARG);
  12654. wc_Sha384Free(&sha384);
  12655. #endif
  12656. return EXPECT_RESULT();
  12657. } /* END test_wc_InitSha384 */
  12658. /*
  12659. * test wc_Sha384Update()
  12660. */
  12661. static int test_wc_Sha384Update(void)
  12662. {
  12663. EXPECT_DECLS;
  12664. #ifdef WOLFSSL_SHA384
  12665. wc_Sha384 sha384;
  12666. byte hash[WC_SHA384_DIGEST_SIZE];
  12667. testVector a, b, c;
  12668. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12669. /* Input */
  12670. a.input = "a";
  12671. a.inLen = XSTRLEN(a.input);
  12672. ExpectIntEQ(wc_Sha384Update(&sha384, NULL, 0), 0);
  12673. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, 0), 0);
  12674. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  12675. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  12676. /* Update input. */
  12677. a.input = "abc";
  12678. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  12679. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  12680. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  12681. "\xc8\x25\xa7";
  12682. a.inLen = XSTRLEN(a.input);
  12683. a.outLen = XSTRLEN(a.output);
  12684. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  12685. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  12686. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  12687. /* Pass in bad values. */
  12688. b.input = NULL;
  12689. b.inLen = 0;
  12690. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen), 0);
  12691. c.input = NULL;
  12692. c.inLen = WC_SHA384_DIGEST_SIZE;
  12693. ExpectIntEQ( wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen),
  12694. BAD_FUNC_ARG);
  12695. ExpectIntEQ(wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen),
  12696. BAD_FUNC_ARG);
  12697. wc_Sha384Free(&sha384);
  12698. #endif
  12699. return EXPECT_RESULT();
  12700. } /* END test_wc_Sha384Update */
  12701. /*
  12702. * Unit test function for wc_Sha384Final();
  12703. */
  12704. static int test_wc_Sha384Final(void)
  12705. {
  12706. EXPECT_DECLS;
  12707. #ifdef WOLFSSL_SHA384
  12708. wc_Sha384 sha384;
  12709. byte* hash_test[3];
  12710. byte hash1[WC_SHA384_DIGEST_SIZE];
  12711. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  12712. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  12713. int times, i;
  12714. /* Initialize */
  12715. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12716. hash_test[0] = hash1;
  12717. hash_test[1] = hash2;
  12718. hash_test[2] = hash3;
  12719. times = sizeof(hash_test) / sizeof(byte*);
  12720. /* Good test args. */
  12721. for (i = 0; i < times; i++) {
  12722. ExpectIntEQ(wc_Sha384Final(&sha384, hash_test[i]), 0);
  12723. }
  12724. /* Test bad args. */
  12725. ExpectIntEQ(wc_Sha384Final(NULL, NULL), BAD_FUNC_ARG);
  12726. ExpectIntEQ(wc_Sha384Final(NULL, hash1), BAD_FUNC_ARG);
  12727. ExpectIntEQ(wc_Sha384Final(&sha384, NULL), BAD_FUNC_ARG);
  12728. wc_Sha384Free(&sha384);
  12729. #endif
  12730. return EXPECT_RESULT();
  12731. } /* END test_wc_Sha384Final */
  12732. /*
  12733. * Unit test function for wc_Sha384GetFlags()
  12734. */
  12735. static int test_wc_Sha384GetFlags(void)
  12736. {
  12737. EXPECT_DECLS;
  12738. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  12739. wc_Sha384 sha384;
  12740. word32 flags = 0;
  12741. /* Initialize */
  12742. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12743. ExpectIntEQ(wc_Sha384GetFlags(&sha384, &flags), 0);
  12744. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12745. wc_Sha384Free(&sha384);
  12746. #endif
  12747. return EXPECT_RESULT();
  12748. } /* END test_wc_Sha384GetFlags */
  12749. /*
  12750. * Unit test function for wc_Sha384FinalRaw()
  12751. */
  12752. static int test_wc_Sha384FinalRaw(void)
  12753. {
  12754. EXPECT_DECLS;
  12755. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  12756. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  12757. !defined(WOLFSSL_NO_HASH_RAW)
  12758. wc_Sha384 sha384;
  12759. byte* hash_test[3];
  12760. byte hash1[WC_SHA384_DIGEST_SIZE];
  12761. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  12762. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  12763. int times, i;
  12764. /* Initialize */
  12765. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12766. hash_test[0] = hash1;
  12767. hash_test[1] = hash2;
  12768. hash_test[2] = hash3;
  12769. times = sizeof(hash_test) / sizeof(byte*);
  12770. /* Good test args. */
  12771. for (i = 0; i < times; i++) {
  12772. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, hash_test[i]), 0);
  12773. }
  12774. /* Test bad args. */
  12775. ExpectIntEQ(wc_Sha384FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12776. ExpectIntEQ(wc_Sha384FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12777. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, NULL), BAD_FUNC_ARG);
  12778. wc_Sha384Free(&sha384);
  12779. #endif
  12780. return EXPECT_RESULT();
  12781. } /* END test_wc_Sha384FinalRaw */
  12782. /*
  12783. * Unit test function for wc_Sha384Free()
  12784. */
  12785. static int test_wc_Sha384Free(void)
  12786. {
  12787. EXPECT_DECLS;
  12788. #ifdef WOLFSSL_SHA384
  12789. wc_Sha384Free(NULL);
  12790. /* Set result to SUCCESS. */
  12791. ExpectTrue(1);
  12792. #endif
  12793. return EXPECT_RESULT();
  12794. } /* END test_wc_Sha384Free */
  12795. /*
  12796. * Unit test function for wc_Sha384GetHash()
  12797. */
  12798. static int test_wc_Sha384GetHash(void)
  12799. {
  12800. EXPECT_DECLS;
  12801. #ifdef WOLFSSL_SHA384
  12802. wc_Sha384 sha384;
  12803. byte hash1[WC_SHA384_DIGEST_SIZE];
  12804. /* Initialize */
  12805. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12806. ExpectIntEQ(wc_Sha384GetHash(&sha384, hash1), 0);
  12807. /* test bad arguments*/
  12808. ExpectIntEQ(wc_Sha384GetHash(NULL, NULL), BAD_FUNC_ARG);
  12809. ExpectIntEQ(wc_Sha384GetHash(NULL, hash1), BAD_FUNC_ARG);
  12810. ExpectIntEQ(wc_Sha384GetHash(&sha384, NULL), BAD_FUNC_ARG);
  12811. wc_Sha384Free(&sha384);
  12812. #endif
  12813. return EXPECT_RESULT();
  12814. } /* END test_wc_Sha384GetHash */
  12815. /*
  12816. * Unit test function for wc_Sha384Copy()
  12817. */
  12818. static int test_wc_Sha384Copy(void)
  12819. {
  12820. EXPECT_DECLS;
  12821. #ifdef WOLFSSL_SHA384
  12822. wc_Sha384 sha384;
  12823. wc_Sha384 temp;
  12824. XMEMSET(&sha384, 0, sizeof(wc_Sha384));
  12825. XMEMSET(&temp, 0, sizeof(wc_Sha384));
  12826. /* Initialize */
  12827. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12828. ExpectIntEQ(wc_InitSha384(&temp), 0);
  12829. ExpectIntEQ(wc_Sha384Copy(&sha384, &temp), 0);
  12830. /* test bad arguments*/
  12831. ExpectIntEQ(wc_Sha384Copy(NULL, NULL), BAD_FUNC_ARG);
  12832. ExpectIntEQ(wc_Sha384Copy(NULL, &temp), BAD_FUNC_ARG);
  12833. ExpectIntEQ(wc_Sha384Copy(&sha384, NULL), BAD_FUNC_ARG);
  12834. wc_Sha384Free(&sha384);
  12835. wc_Sha384Free(&temp);
  12836. #endif
  12837. return EXPECT_RESULT();
  12838. } /* END test_wc_Sha384Copy */
  12839. /*
  12840. * Testing wc_InitSha224();
  12841. */
  12842. static int test_wc_InitSha224(void)
  12843. {
  12844. EXPECT_DECLS;
  12845. #ifdef WOLFSSL_SHA224
  12846. wc_Sha224 sha224;
  12847. /* Test good arg. */
  12848. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12849. /* Test bad arg. */
  12850. ExpectIntEQ(wc_InitSha224(NULL), BAD_FUNC_ARG);
  12851. wc_Sha224Free(&sha224);
  12852. #endif
  12853. return EXPECT_RESULT();
  12854. } /* END test_wc_InitSha224 */
  12855. /*
  12856. * Unit test on wc_Sha224Update
  12857. */
  12858. static int test_wc_Sha224Update(void)
  12859. {
  12860. EXPECT_DECLS;
  12861. #ifdef WOLFSSL_SHA224
  12862. wc_Sha224 sha224;
  12863. byte hash[WC_SHA224_DIGEST_SIZE];
  12864. testVector a, b, c;
  12865. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12866. /* Input. */
  12867. a.input = "a";
  12868. a.inLen = XSTRLEN(a.input);
  12869. ExpectIntEQ(wc_Sha224Update(&sha224, NULL, 0), 0);
  12870. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, 0), 0);
  12871. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  12872. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  12873. /* Update input. */
  12874. a.input = "abc";
  12875. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  12876. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  12877. a.inLen = XSTRLEN(a.input);
  12878. a.outLen = XSTRLEN(a.output);
  12879. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  12880. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  12881. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  12882. /* Pass in bad values. */
  12883. b.input = NULL;
  12884. b.inLen = 0;
  12885. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen), 0);
  12886. c.input = NULL;
  12887. c.inLen = WC_SHA224_DIGEST_SIZE;
  12888. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen),
  12889. BAD_FUNC_ARG);
  12890. ExpectIntEQ(wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen),
  12891. BAD_FUNC_ARG);
  12892. wc_Sha224Free(&sha224);
  12893. #endif
  12894. return EXPECT_RESULT();
  12895. } /* END test_wc_Sha224Update */
  12896. /*
  12897. * Unit test for wc_Sha224Final();
  12898. */
  12899. static int test_wc_Sha224Final(void)
  12900. {
  12901. EXPECT_DECLS;
  12902. #ifdef WOLFSSL_SHA224
  12903. wc_Sha224 sha224;
  12904. byte* hash_test[3];
  12905. byte hash1[WC_SHA224_DIGEST_SIZE];
  12906. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  12907. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  12908. int times, i;
  12909. /* Initialize */
  12910. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12911. hash_test[0] = hash1;
  12912. hash_test[1] = hash2;
  12913. hash_test[2] = hash3;
  12914. times = sizeof(hash_test) / sizeof(byte*);
  12915. /* Good test args. */
  12916. /* Testing oversized buffers. */
  12917. for (i = 0; i < times; i++) {
  12918. ExpectIntEQ(wc_Sha224Final(&sha224, hash_test[i]), 0);
  12919. }
  12920. /* Test bad args. */
  12921. ExpectIntEQ(wc_Sha224Final(NULL, NULL), BAD_FUNC_ARG);
  12922. ExpectIntEQ(wc_Sha224Final(NULL, hash1), BAD_FUNC_ARG);
  12923. ExpectIntEQ(wc_Sha224Final(&sha224, NULL), BAD_FUNC_ARG);
  12924. wc_Sha224Free(&sha224);
  12925. #endif
  12926. return EXPECT_RESULT();
  12927. } /* END test_wc_Sha224Final */
  12928. /*
  12929. * Unit test function for wc_Sha224SetFlags()
  12930. */
  12931. static int test_wc_Sha224SetFlags(void)
  12932. {
  12933. EXPECT_DECLS;
  12934. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  12935. wc_Sha224 sha224;
  12936. word32 flags = WC_HASH_FLAG_WILLCOPY;
  12937. /* Initialize */
  12938. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12939. ExpectIntEQ(wc_Sha224SetFlags(&sha224, flags), 0);
  12940. flags = 0;
  12941. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  12942. ExpectTrue(flags == WC_HASH_FLAG_WILLCOPY);
  12943. wc_Sha224Free(&sha224);
  12944. #endif
  12945. return EXPECT_RESULT();
  12946. } /* END test_wc_Sha224SetFlags */
  12947. /*
  12948. * Unit test function for wc_Sha224GetFlags()
  12949. */
  12950. static int test_wc_Sha224GetFlags(void)
  12951. {
  12952. EXPECT_DECLS;
  12953. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  12954. wc_Sha224 sha224;
  12955. word32 flags = 0;
  12956. /* Initialize */
  12957. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12958. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  12959. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12960. wc_Sha224Free(&sha224);
  12961. #endif
  12962. return EXPECT_RESULT();
  12963. } /* END test_wc_Sha224GetFlags */
  12964. /*
  12965. * Unit test function for wc_Sha224Free()
  12966. */
  12967. static int test_wc_Sha224Free(void)
  12968. {
  12969. EXPECT_DECLS;
  12970. #ifdef WOLFSSL_SHA224
  12971. wc_Sha224Free(NULL);
  12972. /* Set result to SUCCESS. */
  12973. ExpectTrue(1);
  12974. #endif
  12975. return EXPECT_RESULT();
  12976. } /* END test_wc_Sha224Free */
  12977. /*
  12978. * Unit test function for wc_Sha224GetHash()
  12979. */
  12980. static int test_wc_Sha224GetHash(void)
  12981. {
  12982. EXPECT_DECLS;
  12983. #ifdef WOLFSSL_SHA224
  12984. wc_Sha224 sha224;
  12985. byte hash1[WC_SHA224_DIGEST_SIZE];
  12986. /* Initialize */
  12987. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12988. ExpectIntEQ(wc_Sha224GetHash(&sha224, hash1), 0);
  12989. /* test bad arguments*/
  12990. ExpectIntEQ(wc_Sha224GetHash(NULL, NULL), BAD_FUNC_ARG);
  12991. ExpectIntEQ(wc_Sha224GetHash(NULL, hash1), BAD_FUNC_ARG);
  12992. ExpectIntEQ(wc_Sha224GetHash(&sha224, NULL), BAD_FUNC_ARG);
  12993. wc_Sha224Free(&sha224);
  12994. #endif
  12995. return EXPECT_RESULT();
  12996. } /* END test_wc_Sha224GetHash */
  12997. /*
  12998. * Unit test function for wc_Sha224Copy()
  12999. */
  13000. static int test_wc_Sha224Copy(void)
  13001. {
  13002. EXPECT_DECLS;
  13003. #ifdef WOLFSSL_SHA224
  13004. wc_Sha224 sha224;
  13005. wc_Sha224 temp;
  13006. XMEMSET(&sha224, 0, sizeof(wc_Sha224));
  13007. XMEMSET(&temp, 0, sizeof(wc_Sha224));
  13008. /* Initialize */
  13009. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  13010. ExpectIntEQ(wc_InitSha224(&temp), 0);
  13011. ExpectIntEQ(wc_Sha224Copy(&sha224, &temp), 0);
  13012. /* test bad arguments*/
  13013. ExpectIntEQ(wc_Sha224Copy(NULL, NULL), BAD_FUNC_ARG);
  13014. ExpectIntEQ(wc_Sha224Copy(NULL, &temp), BAD_FUNC_ARG);
  13015. ExpectIntEQ(wc_Sha224Copy(&sha224, NULL), BAD_FUNC_ARG);
  13016. wc_Sha224Free(&sha224);
  13017. wc_Sha224Free(&temp);
  13018. #endif
  13019. return EXPECT_RESULT();
  13020. } /* END test_wc_Sha224Copy */
  13021. /*
  13022. * Testing wc_InitRipeMd()
  13023. */
  13024. static int test_wc_InitRipeMd(void)
  13025. {
  13026. EXPECT_DECLS;
  13027. #ifdef WOLFSSL_RIPEMD
  13028. RipeMd ripemd;
  13029. /* Test good arg. */
  13030. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13031. /* Test bad arg. */
  13032. ExpectIntEQ(wc_InitRipeMd(NULL), BAD_FUNC_ARG);
  13033. #endif
  13034. return EXPECT_RESULT();
  13035. } /* END test_wc_InitRipeMd */
  13036. /*
  13037. * Testing wc_RipeMdUpdate()
  13038. */
  13039. static int test_wc_RipeMdUpdate(void)
  13040. {
  13041. EXPECT_DECLS;
  13042. #ifdef WOLFSSL_RIPEMD
  13043. RipeMd ripemd;
  13044. byte hash[RIPEMD_DIGEST_SIZE];
  13045. testVector a, b, c;
  13046. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13047. /* Input */
  13048. a.input = "a";
  13049. a.inLen = XSTRLEN(a.input);
  13050. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  13051. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  13052. /* Update input. */
  13053. a.input = "abc";
  13054. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  13055. "\xb0\x87\xf1\x5a\x0b\xfc";
  13056. a.inLen = XSTRLEN(a.input);
  13057. a.outLen = XSTRLEN(a.output);
  13058. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  13059. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  13060. ExpectIntEQ(XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE), 0);
  13061. /* Pass in bad values. */
  13062. b.input = NULL;
  13063. b.inLen = 0;
  13064. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen), 0);
  13065. c.input = NULL;
  13066. c.inLen = RIPEMD_DIGEST_SIZE;
  13067. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen),
  13068. BAD_FUNC_ARG);
  13069. ExpectIntEQ(wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13070. BAD_FUNC_ARG);
  13071. #endif
  13072. return EXPECT_RESULT();
  13073. } /* END test_wc_RipeMdUdpate */
  13074. /*
  13075. * Unit test function for wc_RipeMdFinal()
  13076. */
  13077. static int test_wc_RipeMdFinal(void)
  13078. {
  13079. EXPECT_DECLS;
  13080. #ifdef WOLFSSL_RIPEMD
  13081. RipeMd ripemd;
  13082. byte* hash_test[3];
  13083. byte hash1[RIPEMD_DIGEST_SIZE];
  13084. byte hash2[2*RIPEMD_DIGEST_SIZE];
  13085. byte hash3[5*RIPEMD_DIGEST_SIZE];
  13086. int times, i;
  13087. /* Initialize */
  13088. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13089. hash_test[0] = hash1;
  13090. hash_test[1] = hash2;
  13091. hash_test[2] = hash3;
  13092. times = sizeof(hash_test) / sizeof(byte*);
  13093. /* Testing oversized buffers. */
  13094. for (i = 0; i < times; i++) {
  13095. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash_test[i]), 0);
  13096. }
  13097. /* Test bad args. */
  13098. ExpectIntEQ(wc_RipeMdFinal(NULL, NULL), BAD_FUNC_ARG);
  13099. ExpectIntEQ(wc_RipeMdFinal(NULL, hash1), BAD_FUNC_ARG);
  13100. ExpectIntEQ(wc_RipeMdFinal(&ripemd, NULL), BAD_FUNC_ARG);
  13101. #endif
  13102. return EXPECT_RESULT();
  13103. } /* END test_wc_RipeMdFinal */
  13104. /*
  13105. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  13106. * wc_InitSha3_512
  13107. */
  13108. static int test_wc_InitSha3(void)
  13109. {
  13110. EXPECT_DECLS;
  13111. #if defined(WOLFSSL_SHA3)
  13112. wc_Sha3 sha3;
  13113. (void)sha3;
  13114. #if !defined(WOLFSSL_NOSHA3_224)
  13115. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13116. /* Test bad args. */
  13117. ExpectIntEQ(wc_InitSha3_224(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13118. wc_Sha3_224_Free(&sha3);
  13119. #endif /* NOSHA3_224 */
  13120. #if !defined(WOLFSSL_NOSHA3_256)
  13121. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13122. /* Test bad args. */
  13123. ExpectIntEQ(wc_InitSha3_256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13124. wc_Sha3_256_Free(&sha3);
  13125. #endif /* NOSHA3_256 */
  13126. #if !defined(WOLFSSL_NOSHA3_384)
  13127. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13128. /* Test bad args. */
  13129. ExpectIntEQ(wc_InitSha3_384(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13130. wc_Sha3_384_Free(&sha3);
  13131. #endif /* NOSHA3_384 */
  13132. #if !defined(WOLFSSL_NOSHA3_512)
  13133. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13134. /* Test bad args. */
  13135. ExpectIntEQ(wc_InitSha3_512(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13136. wc_Sha3_512_Free(&sha3);
  13137. #endif /* NOSHA3_512 */
  13138. #endif
  13139. return EXPECT_RESULT();
  13140. } /* END test_wc_InitSha3 */
  13141. /*
  13142. * Testing wc_Sha3_Update()
  13143. */
  13144. static int testing_wc_Sha3_Update(void)
  13145. {
  13146. EXPECT_DECLS;
  13147. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  13148. !defined(WOLFSSL_AFALG_XILINX)
  13149. wc_Sha3 sha3;
  13150. byte msg[] = "Everybody's working for the weekend.";
  13151. byte msg2[] = "Everybody gets Friday off.";
  13152. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  13153. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  13154. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  13155. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  13156. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  13157. word32 msglen = sizeof(msg) - 1;
  13158. word32 msg2len = sizeof(msg2);
  13159. word32 msgCmplen = sizeof(msgCmp);
  13160. #if !defined(WOLFSSL_NOSHA3_224)
  13161. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13162. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg, msglen), 0);
  13163. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13164. ExpectTrue(sha3.i == msglen);
  13165. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  13166. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13167. /* Pass bad args. */
  13168. ExpectIntEQ(wc_Sha3_224_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13169. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13170. wc_Sha3_224_Free(&sha3);
  13171. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13172. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 0), 0);
  13173. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  13174. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13175. wc_Sha3_224_Free(&sha3);
  13176. #endif /* SHA3_224 */
  13177. #if !defined(WOLFSSL_NOSHA3_256)
  13178. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13179. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg, msglen), 0);
  13180. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13181. ExpectTrue(sha3.i == msglen);
  13182. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  13183. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13184. /* Pass bad args. */
  13185. ExpectIntEQ(wc_Sha3_256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13186. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13187. wc_Sha3_256_Free(&sha3);
  13188. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13189. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 0), 0);
  13190. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  13191. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13192. wc_Sha3_256_Free(&sha3);
  13193. #endif /* SHA3_256 */
  13194. #if !defined(WOLFSSL_NOSHA3_384)
  13195. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13196. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg, msglen), 0);
  13197. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13198. ExpectTrue(sha3.i == msglen);
  13199. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  13200. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13201. /* Pass bad args. */
  13202. ExpectIntEQ(wc_Sha3_384_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13203. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13204. wc_Sha3_384_Free(&sha3);
  13205. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13206. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 0), 0);
  13207. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  13208. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13209. wc_Sha3_384_Free(&sha3);
  13210. #endif /* SHA3_384 */
  13211. #if !defined(WOLFSSL_NOSHA3_512)
  13212. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13213. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg, msglen), 0);
  13214. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13215. ExpectTrue(sha3.i == msglen);
  13216. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  13217. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13218. /* Pass bad args. */
  13219. ExpectIntEQ(wc_Sha3_512_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13220. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13221. wc_Sha3_512_Free(&sha3);
  13222. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13223. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 0), 0);
  13224. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  13225. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13226. wc_Sha3_512_Free(&sha3);
  13227. #endif /* SHA3_512 */
  13228. #endif /* WOLFSSL_SHA3 */
  13229. return EXPECT_RESULT();
  13230. } /* END testing_wc_Sha3_Update */
  13231. /*
  13232. * Testing wc_Sha3_224_Final()
  13233. */
  13234. static int test_wc_Sha3_224_Final(void)
  13235. {
  13236. EXPECT_DECLS;
  13237. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  13238. wc_Sha3 sha3;
  13239. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13240. "nopnopq";
  13241. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  13242. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  13243. "\x64\xea\xd0\xfc\xce\x33";
  13244. byte hash[WC_SHA3_224_DIGEST_SIZE];
  13245. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  13246. /* Init stack variables. */
  13247. XMEMSET(hash, 0, sizeof(hash));
  13248. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13249. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13250. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13251. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE), 0);
  13252. /* Test bad args. */
  13253. ExpectIntEQ(wc_Sha3_224_Final(NULL, hash), BAD_FUNC_ARG);
  13254. ExpectIntEQ(wc_Sha3_224_Final(&sha3, NULL), BAD_FUNC_ARG);
  13255. wc_Sha3_224_Free(&sha3);
  13256. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13257. /* Init stack variables. */
  13258. XMEMSET(hash, 0, sizeof(hash));
  13259. XMEMSET(hashRet, 0, sizeof(hashRet));
  13260. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13261. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, hashRet), 0);
  13262. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13263. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE), 0);
  13264. /* Test bad args. */
  13265. ExpectIntEQ(wc_Sha3_224_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13266. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13267. wc_Sha3_224_Free(&sha3);
  13268. #endif
  13269. return EXPECT_RESULT();
  13270. } /* END test_wc_Sha3_224_Final */
  13271. /*
  13272. * Testing wc_Sha3_256_Final()
  13273. */
  13274. static int test_wc_Sha3_256_Final(void)
  13275. {
  13276. EXPECT_DECLS;
  13277. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  13278. wc_Sha3 sha3;
  13279. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13280. "nopnopq";
  13281. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  13282. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  13283. "\xdd\x97\x49\x6d\x33\x76";
  13284. byte hash[WC_SHA3_256_DIGEST_SIZE];
  13285. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  13286. /* Init stack variables. */
  13287. XMEMSET(hash, 0, sizeof(hash));
  13288. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13289. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13290. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13291. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE), 0);
  13292. /* Test bad args. */
  13293. ExpectIntEQ(wc_Sha3_256_Final(NULL, hash), BAD_FUNC_ARG);
  13294. ExpectIntEQ(wc_Sha3_256_Final(&sha3, NULL), BAD_FUNC_ARG);
  13295. wc_Sha3_256_Free(&sha3);
  13296. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13297. /* Init stack variables. */
  13298. XMEMSET(hash, 0, sizeof(hash));
  13299. XMEMSET(hashRet, 0, sizeof(hashRet));
  13300. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13301. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, hashRet), 0);
  13302. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13303. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE), 0);
  13304. /* Test bad args. */
  13305. ExpectIntEQ(wc_Sha3_256_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13306. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13307. wc_Sha3_256_Free(&sha3);
  13308. #endif
  13309. return EXPECT_RESULT();
  13310. } /* END test_wc_Sha3_256_Final */
  13311. /*
  13312. * Testing wc_Sha3_384_Final()
  13313. */
  13314. static int test_wc_Sha3_384_Final(void)
  13315. {
  13316. EXPECT_DECLS;
  13317. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  13318. wc_Sha3 sha3;
  13319. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13320. "nopnopq";
  13321. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  13322. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  13323. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  13324. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  13325. byte hash[WC_SHA3_384_DIGEST_SIZE];
  13326. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  13327. /* Init stack variables. */
  13328. XMEMSET(hash, 0, sizeof(hash));
  13329. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13330. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13331. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13332. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE), 0);
  13333. /* Test bad args. */
  13334. ExpectIntEQ(wc_Sha3_384_Final(NULL, hash), BAD_FUNC_ARG);
  13335. ExpectIntEQ(wc_Sha3_384_Final(&sha3, NULL), BAD_FUNC_ARG);
  13336. wc_Sha3_384_Free(&sha3);
  13337. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13338. /* Init stack variables. */
  13339. XMEMSET(hash, 0, sizeof(hash));
  13340. XMEMSET(hashRet, 0, sizeof(hashRet));
  13341. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13342. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, hashRet), 0);
  13343. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13344. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE), 0);
  13345. /* Test bad args. */
  13346. ExpectIntEQ(wc_Sha3_384_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13347. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13348. wc_Sha3_384_Free(&sha3);
  13349. #endif
  13350. return EXPECT_RESULT();
  13351. } /* END test_wc_Sha3_384_Final */
  13352. /*
  13353. * Testing wc_Sha3_512_Final()
  13354. */
  13355. static int test_wc_Sha3_512_Final(void)
  13356. {
  13357. EXPECT_DECLS;
  13358. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  13359. !defined(WOLFSSL_NOSHA3_384)
  13360. wc_Sha3 sha3;
  13361. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13362. "nopnopq";
  13363. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  13364. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  13365. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  13366. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  13367. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  13368. byte hash[WC_SHA3_512_DIGEST_SIZE];
  13369. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  13370. /* Init stack variables. */
  13371. XMEMSET(hash, 0, sizeof(hash));
  13372. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13373. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13374. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13375. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE), 0);
  13376. /* Test bad args. */
  13377. ExpectIntEQ(wc_Sha3_512_Final(NULL, hash), BAD_FUNC_ARG);
  13378. ExpectIntEQ(wc_Sha3_512_Final(&sha3, NULL), BAD_FUNC_ARG);
  13379. wc_Sha3_512_Free(&sha3);
  13380. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13381. /* Init stack variables. */
  13382. XMEMSET(hash, 0, sizeof(hash));
  13383. XMEMSET(hashRet, 0, sizeof(hashRet));
  13384. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13385. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, hashRet), 0);
  13386. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13387. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE), 0);
  13388. /* Test bad args. */
  13389. ExpectIntEQ(wc_Sha3_512_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13390. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13391. wc_Sha3_512_Free(&sha3);
  13392. #endif
  13393. return EXPECT_RESULT();
  13394. } /* END test_wc_Sha3_512_Final */
  13395. /*
  13396. * Testing wc_Sha3_224_Copy()
  13397. */
  13398. static int test_wc_Sha3_224_Copy(void)
  13399. {
  13400. EXPECT_DECLS;
  13401. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  13402. wc_Sha3 sha3, sha3Cpy;
  13403. const char* msg = TEST_STRING;
  13404. word32 msglen = (word32)TEST_STRING_SZ;
  13405. byte hash[WC_SHA3_224_DIGEST_SIZE];
  13406. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  13407. XMEMSET(hash, 0, sizeof(hash));
  13408. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13409. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13410. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13411. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13412. ExpectIntEQ(wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13413. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, msglen), 0);
  13414. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, &sha3), 0);
  13415. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13416. ExpectIntEQ(wc_Sha3_224_Final(&sha3Cpy, hashCpy), 0);
  13417. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13418. /* Test bad args. */
  13419. ExpectIntEQ(wc_Sha3_224_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13420. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13421. wc_Sha3_224_Free(&sha3);
  13422. wc_Sha3_224_Free(&sha3Cpy);
  13423. #endif
  13424. return EXPECT_RESULT();
  13425. } /* END test_wc_Sha3_224_Copy */
  13426. /*
  13427. * Testing wc_Sha3_256_Copy()
  13428. */
  13429. static int test_wc_Sha3_256_Copy(void)
  13430. {
  13431. EXPECT_DECLS;
  13432. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  13433. wc_Sha3 sha3, sha3Cpy;
  13434. const char* msg = TEST_STRING;
  13435. word32 msglen = (word32)TEST_STRING_SZ;
  13436. byte hash[WC_SHA3_256_DIGEST_SIZE];
  13437. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  13438. XMEMSET(hash, 0, sizeof(hash));
  13439. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13440. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13441. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13442. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13443. ExpectIntEQ(wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13444. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, msglen), 0);
  13445. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, &sha3), 0);
  13446. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13447. ExpectIntEQ(wc_Sha3_256_Final(&sha3Cpy, hashCpy), 0);
  13448. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13449. /* Test bad args. */
  13450. ExpectIntEQ(wc_Sha3_256_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13451. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13452. wc_Sha3_256_Free(&sha3);
  13453. wc_Sha3_256_Free(&sha3Cpy);
  13454. #endif
  13455. return EXPECT_RESULT();
  13456. } /* END test_wc_Sha3_256_Copy */
  13457. /*
  13458. * Testing wc_Sha3_384_Copy()
  13459. */
  13460. static int test_wc_Sha3_384_Copy(void)
  13461. {
  13462. EXPECT_DECLS;
  13463. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  13464. wc_Sha3 sha3, sha3Cpy;
  13465. const char* msg = TEST_STRING;
  13466. word32 msglen = (word32)TEST_STRING_SZ;
  13467. byte hash[WC_SHA3_384_DIGEST_SIZE];
  13468. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  13469. XMEMSET(hash, 0, sizeof(hash));
  13470. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13471. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13472. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13473. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13474. ExpectIntEQ(wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13475. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, msglen), 0);
  13476. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, &sha3), 0);
  13477. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13478. ExpectIntEQ(wc_Sha3_384_Final(&sha3Cpy, hashCpy), 0);
  13479. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13480. /* Test bad args. */
  13481. ExpectIntEQ(wc_Sha3_384_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13482. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13483. wc_Sha3_384_Free(&sha3);
  13484. wc_Sha3_384_Free(&sha3Cpy);
  13485. #endif
  13486. return EXPECT_RESULT();
  13487. } /* END test_wc_Sha3_384_Copy */
  13488. /*
  13489. * Testing wc_Sha3_512_Copy()
  13490. */
  13491. static int test_wc_Sha3_512_Copy(void)
  13492. {
  13493. EXPECT_DECLS;
  13494. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  13495. wc_Sha3 sha3, sha3Cpy;
  13496. const char* msg = TEST_STRING;
  13497. word32 msglen = (word32)TEST_STRING_SZ;
  13498. byte hash[WC_SHA3_512_DIGEST_SIZE];
  13499. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  13500. XMEMSET(hash, 0, sizeof(hash));
  13501. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13502. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13503. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13504. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13505. ExpectIntEQ(wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13506. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, msglen), 0);
  13507. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, &sha3), 0);
  13508. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13509. ExpectIntEQ(wc_Sha3_512_Final(&sha3Cpy, hashCpy), 0);
  13510. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13511. /* Test bad args. */
  13512. ExpectIntEQ(wc_Sha3_512_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13513. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13514. wc_Sha3_512_Free(&sha3);
  13515. wc_Sha3_512_Free(&sha3Cpy);
  13516. #endif
  13517. return EXPECT_RESULT();
  13518. } /* END test_wc_Sha3_512_Copy */
  13519. /*
  13520. * Unit test function for wc_Sha3_GetFlags()
  13521. */
  13522. static int test_wc_Sha3_GetFlags(void)
  13523. {
  13524. EXPECT_DECLS;
  13525. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  13526. wc_Sha3 sha3;
  13527. word32 flags = 0;
  13528. /* Initialize */
  13529. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13530. ExpectIntEQ(wc_Sha3_GetFlags(&sha3, &flags), 0);
  13531. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  13532. wc_Sha3_224_Free(&sha3);
  13533. #endif
  13534. return EXPECT_RESULT();
  13535. } /* END test_wc_Sha3_GetFlags */
  13536. static int test_wc_InitShake256(void)
  13537. {
  13538. EXPECT_DECLS;
  13539. #ifdef WOLFSSL_SHAKE256
  13540. wc_Shake shake;
  13541. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13542. /* Test bad args. */
  13543. ExpectIntEQ(wc_InitShake256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13544. wc_Shake256_Free(&shake);
  13545. #endif
  13546. return EXPECT_RESULT();
  13547. }
  13548. static int testing_wc_Shake256_Update(void)
  13549. {
  13550. EXPECT_DECLS;
  13551. #ifdef WOLFSSL_SHAKE256
  13552. wc_Shake shake;
  13553. byte msg[] = "Everybody's working for the weekend.";
  13554. byte msg2[] = "Everybody gets Friday off.";
  13555. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  13556. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  13557. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  13558. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  13559. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  13560. word32 msglen = sizeof(msg) - 1;
  13561. word32 msg2len = sizeof(msg2);
  13562. word32 msgCmplen = sizeof(msgCmp);
  13563. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13564. ExpectIntEQ(wc_Shake256_Update(&shake, msg, msglen), 0);
  13565. ExpectIntEQ(XMEMCMP(msg, shake.t, msglen), 0);
  13566. ExpectTrue(shake.i == msglen);
  13567. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  13568. ExpectIntEQ(XMEMCMP(shake.t, msgCmp, msgCmplen), 0);
  13569. /* Pass bad args. */
  13570. ExpectIntEQ(wc_Shake256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13571. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 5), BAD_FUNC_ARG);
  13572. wc_Shake256_Free(&shake);
  13573. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13574. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 0), 0);
  13575. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  13576. ExpectIntEQ(XMEMCMP(msg2, shake.t, msg2len), 0);
  13577. wc_Shake256_Free(&shake);
  13578. #endif /* WOLFSSL_SHAKE256 */
  13579. return EXPECT_RESULT();
  13580. }
  13581. static int test_wc_Shake256_Final(void)
  13582. {
  13583. EXPECT_DECLS;
  13584. #ifdef WOLFSSL_SHAKE256
  13585. wc_Shake shake;
  13586. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13587. "nopnopq";
  13588. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  13589. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  13590. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  13591. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  13592. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  13593. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  13594. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  13595. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  13596. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  13597. byte hash[114];
  13598. /* Init stack variables. */
  13599. XMEMSET(hash, 0, sizeof(hash));
  13600. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13601. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg)),
  13602. 0);
  13603. ExpectIntEQ(wc_Shake256_Final(&shake, hash, (word32)sizeof(hash)), 0);
  13604. ExpectIntEQ(XMEMCMP(expOut, hash, (word32)sizeof(hash)), 0);
  13605. /* Test bad args. */
  13606. ExpectIntEQ(wc_Shake256_Final(NULL, hash, (word32)sizeof(hash)),
  13607. BAD_FUNC_ARG);
  13608. ExpectIntEQ(wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash)),
  13609. BAD_FUNC_ARG);
  13610. wc_Shake256_Free(&shake);
  13611. #endif
  13612. return EXPECT_RESULT();
  13613. }
  13614. /*
  13615. * Testing wc_Shake256_Copy()
  13616. */
  13617. static int test_wc_Shake256_Copy(void)
  13618. {
  13619. EXPECT_DECLS;
  13620. #ifdef WOLFSSL_SHAKE256
  13621. wc_Shake shake, shakeCpy;
  13622. const char* msg = TEST_STRING;
  13623. word32 msglen = (word32)TEST_STRING_SZ;
  13624. byte hash[144];
  13625. byte hashCpy[144];
  13626. word32 hashLen = sizeof(hash);
  13627. word32 hashLenCpy = sizeof(hashCpy);
  13628. XMEMSET(hash, 0, sizeof(hash));
  13629. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13630. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13631. ExpectIntEQ(wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId), 0);
  13632. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, msglen), 0);
  13633. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, &shake), 0);
  13634. ExpectIntEQ(wc_Shake256_Final(&shake, hash, hashLen), 0);
  13635. ExpectIntEQ(wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy), 0);
  13636. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13637. /* Test bad args. */
  13638. ExpectIntEQ(wc_Shake256_Copy(NULL, &shake), BAD_FUNC_ARG);
  13639. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, NULL), BAD_FUNC_ARG);
  13640. wc_Shake256_Free(&shake);
  13641. wc_Shake256_Free(&shakeCpy);
  13642. #endif
  13643. return EXPECT_RESULT();
  13644. } /* END test_wc_Shake256_Copy */
  13645. /*
  13646. * Unit test function for wc_Shake256Hash()
  13647. */
  13648. static int test_wc_Shake256Hash(void)
  13649. {
  13650. EXPECT_DECLS;
  13651. #ifdef WOLFSSL_SHAKE256
  13652. const byte data[] = { /* Hello World */
  13653. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  13654. 0x72,0x6c,0x64
  13655. };
  13656. word32 len = sizeof(data);
  13657. byte hash[144];
  13658. word32 hashLen = sizeof(hash);
  13659. ExpectIntEQ(wc_Shake256Hash(data, len, hash, hashLen), 0);
  13660. #endif
  13661. return EXPECT_RESULT();
  13662. } /* END test_wc_Shake256Hash */
  13663. /*
  13664. * Testing wc_InitSm3(), wc_Sm3Free()
  13665. */
  13666. static int test_wc_InitSm3Free(void)
  13667. {
  13668. EXPECT_DECLS;
  13669. #ifdef WOLFSSL_SM3
  13670. wc_Sm3 sm3;
  13671. /* Invalid Parameters */
  13672. ExpectIntEQ(wc_InitSm3(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  13673. /* Valid Parameters */
  13674. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13675. wc_Sm3Free(NULL);
  13676. wc_Sm3Free(&sm3);
  13677. #endif
  13678. return EXPECT_RESULT();
  13679. } /* END test_wc_InitSm3 */
  13680. /*
  13681. * Testing wc_Sm3Update(), wc_Sm3Final()
  13682. */
  13683. static int test_wc_Sm3UpdateFinal(void)
  13684. {
  13685. EXPECT_DECLS;
  13686. #ifdef WOLFSSL_SM3
  13687. wc_Sm3 sm3;
  13688. byte data[WC_SM3_BLOCK_SIZE * 4];
  13689. byte hash[WC_SM3_DIGEST_SIZE];
  13690. byte calcHash[WC_SM3_DIGEST_SIZE];
  13691. byte expHash[WC_SM3_DIGEST_SIZE] = {
  13692. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  13693. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  13694. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  13695. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  13696. };
  13697. word32 chunk;
  13698. word32 i;
  13699. XMEMSET(data, 0, sizeof(data));
  13700. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13701. /* Invalid Parameters */
  13702. ExpectIntEQ(wc_Sm3Update(NULL, NULL, 1), BAD_FUNC_ARG);
  13703. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 1), BAD_FUNC_ARG);
  13704. ExpectIntEQ(wc_Sm3Update(NULL, data, 1), BAD_FUNC_ARG);
  13705. /* Valid Parameters */
  13706. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 0), 0);
  13707. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13708. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13709. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE), 0);
  13710. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE - 2), 0);
  13711. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE * 2), 0);
  13712. /* Ensure too many bytes for lengths. */
  13713. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_PAD_SIZE), 0);
  13714. /* Invalid Parameters */
  13715. ExpectIntEQ(wc_Sm3Final(NULL, NULL), BAD_FUNC_ARG);
  13716. ExpectIntEQ(wc_Sm3Final(&sm3, NULL), BAD_FUNC_ARG);
  13717. ExpectIntEQ(wc_Sm3Final(NULL, hash), BAD_FUNC_ARG);
  13718. /* Valid Parameters */
  13719. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13720. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  13721. /* Chunk tests. */
  13722. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  13723. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13724. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  13725. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  13726. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, chunk), 0);
  13727. }
  13728. if (i < (word32)sizeof(data)) {
  13729. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, (word32)sizeof(data) - i),
  13730. 0);
  13731. }
  13732. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13733. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13734. }
  13735. /* Not testing when the low 32-bit length overflows. */
  13736. wc_Sm3Free(&sm3);
  13737. #endif
  13738. return EXPECT_RESULT();
  13739. } /* END test_wc_Sm3Update */
  13740. /*
  13741. * Testing wc_Sm3GetHash()
  13742. */
  13743. static int test_wc_Sm3GetHash(void)
  13744. {
  13745. EXPECT_DECLS;
  13746. #ifdef WOLFSSL_SM3
  13747. wc_Sm3 sm3;
  13748. byte hash[WC_SM3_DIGEST_SIZE];
  13749. byte calcHash[WC_SM3_DIGEST_SIZE];
  13750. byte data[WC_SM3_BLOCK_SIZE];
  13751. XMEMSET(data, 0, sizeof(data));
  13752. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13753. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13754. /* Invalid Parameters */
  13755. ExpectIntEQ(wc_Sm3GetHash(NULL, NULL), BAD_FUNC_ARG);
  13756. ExpectIntEQ(wc_Sm3GetHash(&sm3, NULL), BAD_FUNC_ARG);
  13757. ExpectIntEQ(wc_Sm3GetHash(NULL, hash), BAD_FUNC_ARG);
  13758. /* Valid Parameters */
  13759. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  13760. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13761. /* With update. */
  13762. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  13763. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  13764. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13765. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13766. wc_Sm3Free(&sm3);
  13767. #endif
  13768. return EXPECT_RESULT();
  13769. } /* END test_wc_Sm3Update */
  13770. /*
  13771. * Testing wc_Sm3Copy()
  13772. */
  13773. static int test_wc_Sm3Copy(void)
  13774. {
  13775. EXPECT_DECLS;
  13776. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13777. wc_Sm3 sm3;
  13778. wc_Sm3 sm3Copy;
  13779. byte hash[WC_SM3_DIGEST_SIZE];
  13780. byte hashCopy[WC_SM3_DIGEST_SIZE];
  13781. byte data[WC_SM3_BLOCK_SIZE + 1];
  13782. int i;
  13783. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13784. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  13785. /* Invalid Parameters */
  13786. ExpectIntEQ(wc_Sm3Copy(NULL, NULL), BAD_FUNC_ARG);
  13787. ExpectIntEQ(wc_Sm3Copy(&sm3, NULL), BAD_FUNC_ARG);
  13788. ExpectIntEQ(wc_Sm3Copy(NULL, &sm3Copy), BAD_FUNC_ARG);
  13789. /* Valid Parameters */
  13790. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13791. /* Ensure all parts of data updated during hashing are copied. */
  13792. for (i = 0; i < WC_SM3_BLOCK_SIZE + 1; i++) {
  13793. ExpectIntEQ(wc_Sm3Update(&sm3, data, i), 0);
  13794. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13795. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13796. ExpectIntEQ(wc_Sm3Update(&sm3Copy, data, 1), 0);
  13797. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13798. ExpectIntEQ(wc_Sm3Final(&sm3Copy, hashCopy), 0);
  13799. ExpectBufEQ(hash, hashCopy, WC_SM3_DIGEST_SIZE);
  13800. }
  13801. wc_Sm3Free(&sm3Copy);
  13802. wc_Sm3Free(&sm3);
  13803. #endif
  13804. return EXPECT_RESULT();
  13805. } /* END test_wc_Sm3Copy */
  13806. /*
  13807. * Testing wc_Sm3FinalRaw()
  13808. */
  13809. static int test_wc_Sm3FinalRaw(void)
  13810. {
  13811. EXPECT_DECLS;
  13812. #if defined(WOLFSSL_SM3) && !defined(HAVE_SELFTEST) && \
  13813. !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  13814. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  13815. !defined(WOLFSSL_NO_HASH_RAW)
  13816. wc_Sm3 sm3;
  13817. byte hash1[WC_SM3_DIGEST_SIZE];
  13818. byte hash2[WC_SM3_DIGEST_SIZE];
  13819. byte hash3[WC_SM3_DIGEST_SIZE];
  13820. byte* hash_test[3] = { hash1, hash2, hash3 };
  13821. int times;
  13822. int i;
  13823. XMEMSET(&sm3, 0, sizeof(sm3));
  13824. /* Initialize */
  13825. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13826. /* Invalid Parameters */
  13827. ExpectIntEQ(wc_Sm3FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  13828. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, NULL), BAD_FUNC_ARG);
  13829. ExpectIntEQ(wc_Sm3FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  13830. times = sizeof(hash_test) / sizeof(byte*);
  13831. for (i = 0; i < times; i++) {
  13832. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, hash_test[i]), 0);
  13833. }
  13834. wc_Sm3Free(&sm3);
  13835. #endif
  13836. return EXPECT_RESULT();
  13837. } /* END test_wc_Sm3FinalRaw */
  13838. /*
  13839. * Testing wc_Sm3GetFlags, wc_Sm3SetFlags()
  13840. */
  13841. static int test_wc_Sm3GetSetFlags(void)
  13842. {
  13843. EXPECT_DECLS;
  13844. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13845. wc_Sm3 sm3;
  13846. wc_Sm3 sm3Copy;
  13847. word32 flags = 0;
  13848. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13849. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  13850. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  13851. ExpectIntEQ(flags, 0);
  13852. ExpectIntEQ(wc_Sm3SetFlags(NULL, WC_HASH_FLAG_WILLCOPY), 0);
  13853. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  13854. ExpectIntEQ(flags, 0);
  13855. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  13856. ExpectIntEQ(flags, 0);
  13857. ExpectIntEQ(wc_Sm3SetFlags(&sm3, WC_HASH_FLAG_WILLCOPY), 0);
  13858. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  13859. ExpectIntEQ(flags, WC_HASH_FLAG_WILLCOPY);
  13860. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13861. ExpectIntEQ(wc_Sm3GetFlags(&sm3Copy, &flags), 0);
  13862. ExpectIntEQ(flags, WC_HASH_FLAG_ISCOPY | WC_HASH_FLAG_WILLCOPY);
  13863. wc_Sm3Free(&sm3Copy);
  13864. wc_Sm3Free(&sm3);
  13865. #endif
  13866. return EXPECT_RESULT();
  13867. } /* END test_wc_Sm3Update */
  13868. /*
  13869. * Testing wc_Sm3Hash()
  13870. */
  13871. static int test_wc_Sm3Hash(void)
  13872. {
  13873. EXPECT_DECLS;
  13874. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13875. byte data[WC_SM3_BLOCK_SIZE];
  13876. byte hash[WC_SM3_DIGEST_SIZE];
  13877. /* Invalid parameters. */
  13878. ExpectIntEQ(wc_Sm3Hash(NULL, sizeof(data), hash), BAD_FUNC_ARG);
  13879. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), NULL), BAD_FUNC_ARG);
  13880. /* Valid parameters. */
  13881. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), hash), 0);
  13882. #endif
  13883. return EXPECT_RESULT();
  13884. } /* END test_wc_Sm3Hash */
  13885. /*
  13886. * Test function for wc_HmacSetKey
  13887. */
  13888. static int test_wc_Md5HmacSetKey(void)
  13889. {
  13890. EXPECT_DECLS;
  13891. #if !defined(NO_HMAC) && !defined(NO_MD5)
  13892. Hmac hmac;
  13893. int ret, times, itr;
  13894. const char* keys[]=
  13895. {
  13896. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  13897. #ifndef HAVE_FIPS
  13898. "Jefe", /* smaller than minimum FIPS key size */
  13899. #endif
  13900. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13901. };
  13902. times = sizeof(keys) / sizeof(char*);
  13903. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13904. for (itr = 0; itr < times; itr++) {
  13905. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  13906. (word32)XSTRLEN(keys[itr]));
  13907. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13908. wc_HmacFree(&hmac);
  13909. ExpectIntEQ(ret, BAD_FUNC_ARG);
  13910. #else
  13911. ExpectIntEQ(ret, 0);
  13912. #endif
  13913. }
  13914. /* Bad args. */
  13915. ExpectIntEQ(wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  13916. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13917. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0])),
  13918. BAD_FUNC_ARG);
  13919. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13920. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13921. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  13922. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13923. ExpectIntEQ(ret, BAD_FUNC_ARG);
  13924. #elif defined(HAVE_FIPS)
  13925. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13926. #else
  13927. ExpectIntEQ(ret, 0);
  13928. #endif
  13929. wc_HmacFree(&hmac);
  13930. #endif
  13931. return EXPECT_RESULT();
  13932. } /* END test_wc_Md5HmacSetKey */
  13933. /*
  13934. * testing wc_HmacSetKey() on wc_Sha hash.
  13935. */
  13936. static int test_wc_ShaHmacSetKey(void)
  13937. {
  13938. EXPECT_DECLS;
  13939. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13940. Hmac hmac;
  13941. int ret, times, itr;
  13942. const char* keys[]=
  13943. {
  13944. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13945. "\x0b\x0b\x0b",
  13946. #ifndef HAVE_FIPS
  13947. "Jefe", /* smaller than minimum FIPS key size */
  13948. #endif
  13949. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13950. "\xAA\xAA\xAA"
  13951. };
  13952. times = sizeof(keys) / sizeof(char*);
  13953. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13954. for (itr = 0; itr < times; itr++) {
  13955. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  13956. (word32)XSTRLEN(keys[itr])), 0);
  13957. }
  13958. /* Bad args. */
  13959. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  13960. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13961. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0])),
  13962. BAD_FUNC_ARG);
  13963. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13964. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13965. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  13966. #ifdef HAVE_FIPS
  13967. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13968. #else
  13969. ExpectIntEQ(ret, 0);
  13970. #endif
  13971. wc_HmacFree(&hmac);
  13972. #endif
  13973. return EXPECT_RESULT();
  13974. } /* END test_wc_ShaHmacSetKey() */
  13975. /*
  13976. * testing wc_HmacSetKey() on Sha224 hash.
  13977. */
  13978. static int test_wc_Sha224HmacSetKey(void)
  13979. {
  13980. EXPECT_DECLS;
  13981. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13982. Hmac hmac;
  13983. int ret, times, itr;
  13984. const char* keys[]=
  13985. {
  13986. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13987. "\x0b\x0b\x0b",
  13988. #ifndef HAVE_FIPS
  13989. "Jefe", /* smaller than minimum FIPS key size */
  13990. #endif
  13991. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13992. "\xAA\xAA\xAA"
  13993. };
  13994. times = sizeof(keys) / sizeof(char*);
  13995. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13996. for (itr = 0; itr < times; itr++) {
  13997. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  13998. (word32)XSTRLEN(keys[itr])), 0);
  13999. }
  14000. /* Bad args. */
  14001. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  14002. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14003. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0])),
  14004. BAD_FUNC_ARG);
  14005. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14006. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14007. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  14008. #ifdef HAVE_FIPS
  14009. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14010. #else
  14011. ExpectIntEQ(ret, 0);
  14012. #endif
  14013. wc_HmacFree(&hmac);
  14014. #endif
  14015. return EXPECT_RESULT();
  14016. } /* END test_wc_Sha224HmacSetKey() */
  14017. /*
  14018. * testing wc_HmacSetKey() on Sha256 hash
  14019. */
  14020. static int test_wc_Sha256HmacSetKey(void)
  14021. {
  14022. EXPECT_DECLS;
  14023. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14024. Hmac hmac;
  14025. int ret, times, itr;
  14026. const char* keys[]=
  14027. {
  14028. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14029. "\x0b\x0b\x0b",
  14030. #ifndef HAVE_FIPS
  14031. "Jefe", /* smaller than minimum FIPS key size */
  14032. #endif
  14033. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14034. "\xAA\xAA\xAA"
  14035. };
  14036. times = sizeof(keys) / sizeof(char*);
  14037. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14038. for (itr = 0; itr < times; itr++) {
  14039. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  14040. (word32)XSTRLEN(keys[itr])), 0);
  14041. }
  14042. /* Bad args. */
  14043. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  14044. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14045. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0])),
  14046. BAD_FUNC_ARG);
  14047. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14048. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14049. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  14050. #ifdef HAVE_FIPS
  14051. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14052. #else
  14053. ExpectIntEQ(ret, 0);
  14054. #endif
  14055. wc_HmacFree(&hmac);
  14056. #endif
  14057. return EXPECT_RESULT();
  14058. } /* END test_wc_Sha256HmacSetKey() */
  14059. /*
  14060. * testing wc_HmacSetKey on Sha384 hash.
  14061. */
  14062. static int test_wc_Sha384HmacSetKey(void)
  14063. {
  14064. EXPECT_DECLS;
  14065. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14066. Hmac hmac;
  14067. int ret, times, itr;
  14068. const char* keys[]=
  14069. {
  14070. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14071. "\x0b\x0b\x0b",
  14072. #ifndef HAVE_FIPS
  14073. "Jefe", /* smaller than minimum FIPS key size */
  14074. #endif
  14075. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14076. "\xAA\xAA\xAA"
  14077. };
  14078. times = sizeof(keys) / sizeof(char*);
  14079. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14080. for (itr = 0; itr < times; itr++) {
  14081. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  14082. (word32)XSTRLEN(keys[itr])), 0);
  14083. }
  14084. /* Bad args. */
  14085. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  14086. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14087. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0])),
  14088. BAD_FUNC_ARG);
  14089. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14090. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14091. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  14092. #ifdef HAVE_FIPS
  14093. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14094. #else
  14095. ExpectIntEQ(ret, 0);
  14096. #endif
  14097. wc_HmacFree(&hmac);
  14098. #endif
  14099. return EXPECT_RESULT();
  14100. } /* END test_wc_Sha384HmacSetKey() */
  14101. /*
  14102. * testing wc_HmacUpdate on wc_Md5 hash.
  14103. */
  14104. static int test_wc_Md5HmacUpdate(void)
  14105. {
  14106. EXPECT_DECLS;
  14107. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  14108. Hmac hmac;
  14109. testVector a, b;
  14110. #ifdef HAVE_FIPS
  14111. const char* keys =
  14112. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14113. #else
  14114. const char* keys = "Jefe";
  14115. #endif
  14116. a.input = "what do ya want for nothing?";
  14117. a.inLen = XSTRLEN(a.input);
  14118. b.input = "Hi There";
  14119. b.inLen = XSTRLEN(b.input);
  14120. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14121. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys,
  14122. (word32)XSTRLEN(keys)), 0);
  14123. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14124. /* Update Hmac. */
  14125. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14126. /* Test bad args. */
  14127. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14128. BAD_FUNC_ARG);
  14129. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14130. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14131. wc_HmacFree(&hmac);
  14132. #endif
  14133. return EXPECT_RESULT();
  14134. } /* END test_wc_Md5HmacUpdate */
  14135. /*
  14136. * testing wc_HmacUpdate on SHA hash.
  14137. */
  14138. static int test_wc_ShaHmacUpdate(void)
  14139. {
  14140. EXPECT_DECLS;
  14141. #if !defined(NO_HMAC) && !defined(NO_SHA)
  14142. Hmac hmac;
  14143. testVector a, b;
  14144. #ifdef HAVE_FIPS
  14145. const char* keys =
  14146. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14147. #else
  14148. const char* keys = "Jefe";
  14149. #endif
  14150. a.input = "what do ya want for nothing?";
  14151. a.inLen = XSTRLEN(a.input);
  14152. b.input = "Hi There";
  14153. b.inLen = XSTRLEN(b.input);
  14154. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14155. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys,
  14156. (word32)XSTRLEN(keys)), 0);
  14157. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14158. /* Update Hmac. */
  14159. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14160. /* Test bad args. */
  14161. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14162. BAD_FUNC_ARG);
  14163. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14164. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14165. wc_HmacFree(&hmac);
  14166. #endif
  14167. return EXPECT_RESULT();
  14168. } /* END test_wc_ShaHmacUpdate */
  14169. /*
  14170. * testing wc_HmacUpdate on SHA224 hash.
  14171. */
  14172. static int test_wc_Sha224HmacUpdate(void)
  14173. {
  14174. EXPECT_DECLS;
  14175. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14176. Hmac hmac;
  14177. testVector a, b;
  14178. #ifdef HAVE_FIPS
  14179. const char* keys =
  14180. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14181. #else
  14182. const char* keys = "Jefe";
  14183. #endif
  14184. a.input = "what do ya want for nothing?";
  14185. a.inLen = XSTRLEN(a.input);
  14186. b.input = "Hi There";
  14187. b.inLen = XSTRLEN(b.input);
  14188. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14189. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys,
  14190. (word32)XSTRLEN(keys)), 0);
  14191. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14192. /* Update Hmac. */
  14193. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14194. /* Test bad args. */
  14195. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14196. BAD_FUNC_ARG);
  14197. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14198. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14199. wc_HmacFree(&hmac);
  14200. #endif
  14201. return EXPECT_RESULT();
  14202. } /* END test_wc_Sha224HmacUpdate */
  14203. /*
  14204. * testing wc_HmacUpdate on SHA256 hash.
  14205. */
  14206. static int test_wc_Sha256HmacUpdate(void)
  14207. {
  14208. EXPECT_DECLS;
  14209. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14210. Hmac hmac;
  14211. testVector a, b;
  14212. #ifdef HAVE_FIPS
  14213. const char* keys =
  14214. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14215. #else
  14216. const char* keys = "Jefe";
  14217. #endif
  14218. a.input = "what do ya want for nothing?";
  14219. a.inLen = XSTRLEN(a.input);
  14220. b.input = "Hi There";
  14221. b.inLen = XSTRLEN(b.input);
  14222. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14223. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys,
  14224. (word32)XSTRLEN(keys)), 0);
  14225. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14226. /* Update Hmac. */
  14227. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14228. /* Test bad args. */
  14229. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14230. BAD_FUNC_ARG);
  14231. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14232. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14233. wc_HmacFree(&hmac);
  14234. #endif
  14235. return EXPECT_RESULT();
  14236. } /* END test_wc_Sha256HmacUpdate */
  14237. /*
  14238. * testing wc_HmacUpdate on SHA384 hash.
  14239. */
  14240. static int test_wc_Sha384HmacUpdate(void)
  14241. {
  14242. EXPECT_DECLS;
  14243. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14244. Hmac hmac;
  14245. testVector a, b;
  14246. #ifdef HAVE_FIPS
  14247. const char* keys =
  14248. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14249. #else
  14250. const char* keys = "Jefe";
  14251. #endif
  14252. a.input = "what do ya want for nothing?";
  14253. a.inLen = XSTRLEN(a.input);
  14254. b.input = "Hi There";
  14255. b.inLen = XSTRLEN(b.input);
  14256. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14257. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys,
  14258. (word32)XSTRLEN(keys)), 0);
  14259. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14260. /* Update Hmac. */
  14261. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14262. /* Test bad args. */
  14263. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14264. BAD_FUNC_ARG);
  14265. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14266. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14267. wc_HmacFree(&hmac);
  14268. #endif
  14269. return EXPECT_RESULT();
  14270. } /* END test_wc_Sha384HmacUpdate */
  14271. /*
  14272. * Testing wc_HmacFinal() with MD5
  14273. */
  14274. static int test_wc_Md5HmacFinal(void)
  14275. {
  14276. EXPECT_DECLS;
  14277. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  14278. Hmac hmac;
  14279. byte hash[WC_MD5_DIGEST_SIZE];
  14280. testVector a;
  14281. const char* key;
  14282. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14283. a.input = "Hi There";
  14284. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  14285. "\x9d";
  14286. a.inLen = XSTRLEN(a.input);
  14287. a.outLen = XSTRLEN(a.output);
  14288. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14289. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key)),
  14290. 0);
  14291. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14292. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14293. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  14294. /* Try bad parameters. */
  14295. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14296. #ifndef HAVE_FIPS
  14297. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14298. #endif
  14299. wc_HmacFree(&hmac);
  14300. #endif
  14301. return EXPECT_RESULT();
  14302. } /* END test_wc_Md5HmacFinal */
  14303. /*
  14304. * Testing wc_HmacFinal() with SHA
  14305. */
  14306. static int test_wc_ShaHmacFinal(void)
  14307. {
  14308. EXPECT_DECLS;
  14309. #if !defined(NO_HMAC) && !defined(NO_SHA)
  14310. Hmac hmac;
  14311. byte hash[WC_SHA_DIGEST_SIZE];
  14312. testVector a;
  14313. const char* key;
  14314. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14315. "\x0b\x0b\x0b";
  14316. a.input = "Hi There";
  14317. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  14318. "\x8e\xf1\x46\xbe\x00";
  14319. a.inLen = XSTRLEN(a.input);
  14320. a.outLen = XSTRLEN(a.output);
  14321. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14322. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key)),
  14323. 0);
  14324. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14325. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14326. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  14327. /* Try bad parameters. */
  14328. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14329. #ifndef HAVE_FIPS
  14330. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14331. #endif
  14332. wc_HmacFree(&hmac);
  14333. #endif
  14334. return EXPECT_RESULT();
  14335. } /* END test_wc_ShaHmacFinal */
  14336. /*
  14337. * Testing wc_HmacFinal() with SHA224
  14338. */
  14339. static int test_wc_Sha224HmacFinal(void)
  14340. {
  14341. EXPECT_DECLS;
  14342. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14343. Hmac hmac;
  14344. byte hash[WC_SHA224_DIGEST_SIZE];
  14345. testVector a;
  14346. const char* key;
  14347. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14348. "\x0b\x0b\x0b";
  14349. a.input = "Hi There";
  14350. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  14351. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  14352. a.inLen = XSTRLEN(a.input);
  14353. a.outLen = XSTRLEN(a.output);
  14354. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14355. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key,
  14356. (word32)XSTRLEN(key)), 0);
  14357. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14358. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14359. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  14360. /* Try bad parameters. */
  14361. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14362. #ifndef HAVE_FIPS
  14363. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14364. #endif
  14365. wc_HmacFree(&hmac);
  14366. #endif
  14367. return EXPECT_RESULT();
  14368. } /* END test_wc_Sha224HmacFinal */
  14369. /*
  14370. * Testing wc_HmacFinal() with SHA256
  14371. */
  14372. static int test_wc_Sha256HmacFinal(void)
  14373. {
  14374. EXPECT_DECLS;
  14375. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14376. Hmac hmac;
  14377. byte hash[WC_SHA256_DIGEST_SIZE];
  14378. testVector a;
  14379. const char* key;
  14380. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14381. "\x0b\x0b\x0b";
  14382. a.input = "Hi There";
  14383. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  14384. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  14385. "\xcf\xf7";
  14386. a.inLen = XSTRLEN(a.input);
  14387. a.outLen = XSTRLEN(a.output);
  14388. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14389. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key,
  14390. (word32)XSTRLEN(key)), 0);
  14391. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14392. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14393. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  14394. /* Try bad parameters. */
  14395. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14396. #ifndef HAVE_FIPS
  14397. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14398. #endif
  14399. wc_HmacFree(&hmac);
  14400. #endif
  14401. return EXPECT_RESULT();
  14402. } /* END test_wc_Sha256HmacFinal */
  14403. /*
  14404. * Testing wc_HmacFinal() with SHA384
  14405. */
  14406. static int test_wc_Sha384HmacFinal(void)
  14407. {
  14408. EXPECT_DECLS;
  14409. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14410. Hmac hmac;
  14411. byte hash[WC_SHA384_DIGEST_SIZE];
  14412. testVector a;
  14413. const char* key;
  14414. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14415. "\x0b\x0b\x0b";
  14416. a.input = "Hi There";
  14417. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  14418. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  14419. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  14420. "\xfa\x9c\xb6";
  14421. a.inLen = XSTRLEN(a.input);
  14422. a.outLen = XSTRLEN(a.output);
  14423. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14424. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key,
  14425. (word32)XSTRLEN(key)), 0);
  14426. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14427. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14428. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  14429. /* Try bad parameters. */
  14430. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14431. #ifndef HAVE_FIPS
  14432. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14433. #endif
  14434. wc_HmacFree(&hmac);
  14435. #endif
  14436. return EXPECT_RESULT();
  14437. } /* END test_wc_Sha384HmacFinal */
  14438. /*
  14439. * Testing wc_InitCmac()
  14440. */
  14441. static int test_wc_InitCmac(void)
  14442. {
  14443. EXPECT_DECLS;
  14444. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  14445. Cmac cmac1;
  14446. Cmac cmac2;
  14447. Cmac cmac3;
  14448. /* AES 128 key. */
  14449. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14450. "\x09\x10\x11\x12\x13\x14\x15\x16";
  14451. /* AES 192 key. */
  14452. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14453. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14454. "\x01\x02\x03\x04\x05\x06\x07\x08";
  14455. /* AES 256 key. */
  14456. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14457. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14458. "\x01\x02\x03\x04\x05\x06\x07\x08"
  14459. "\x09\x01\x11\x12\x13\x14\x15\x16";
  14460. word32 key1Sz = (word32)sizeof(key1) - 1;
  14461. word32 key2Sz = (word32)sizeof(key2) - 1;
  14462. word32 key3Sz = (word32)sizeof(key3) - 1;
  14463. int type = WC_CMAC_AES;
  14464. (void)key1;
  14465. (void)key1Sz;
  14466. (void)key2;
  14467. (void)key2Sz;
  14468. XMEMSET(&cmac1, 0, sizeof(Cmac));
  14469. XMEMSET(&cmac2, 0, sizeof(Cmac));
  14470. XMEMSET(&cmac3, 0, sizeof(Cmac));
  14471. #ifdef WOLFSSL_AES_128
  14472. ExpectIntEQ(wc_InitCmac(&cmac1, key1, key1Sz, type, NULL), 0);
  14473. #endif
  14474. #ifdef WOLFSSL_AES_192
  14475. wc_AesFree(&cmac1.aes);
  14476. ExpectIntEQ(wc_InitCmac(&cmac2, key2, key2Sz, type, NULL), 0);
  14477. #endif
  14478. #ifdef WOLFSSL_AES_256
  14479. wc_AesFree(&cmac2.aes);
  14480. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, type, NULL), 0);
  14481. #endif
  14482. wc_AesFree(&cmac3.aes);
  14483. /* Test bad args. */
  14484. ExpectIntEQ(wc_InitCmac(NULL, key3, key3Sz, type, NULL), BAD_FUNC_ARG);
  14485. ExpectIntEQ(wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL), BAD_FUNC_ARG);
  14486. ExpectIntEQ(wc_InitCmac(&cmac3, key3, 0, type, NULL), BAD_FUNC_ARG);
  14487. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL), BAD_FUNC_ARG);
  14488. #endif
  14489. return EXPECT_RESULT();
  14490. } /* END test_wc_InitCmac */
  14491. /*
  14492. * Testing wc_CmacUpdate()
  14493. */
  14494. static int test_wc_CmacUpdate(void)
  14495. {
  14496. EXPECT_DECLS;
  14497. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14498. Cmac cmac;
  14499. byte key[] = {
  14500. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14501. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14502. };
  14503. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  14504. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  14505. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  14506. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  14507. "\xf4";
  14508. word32 inSz = (word32)sizeof(in) - 1;
  14509. word32 keySz = (word32)sizeof(key);
  14510. int type = WC_CMAC_AES;
  14511. XMEMSET(&cmac, 0, sizeof(Cmac));
  14512. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  14513. ExpectIntEQ(wc_CmacUpdate(&cmac, in, inSz), 0);
  14514. /* Test bad args. */
  14515. ExpectIntEQ(wc_CmacUpdate(NULL, in, inSz), BAD_FUNC_ARG);
  14516. ExpectIntEQ(wc_CmacUpdate(&cmac, NULL, 30), BAD_FUNC_ARG);
  14517. wc_AesFree(&cmac.aes);
  14518. #endif
  14519. return EXPECT_RESULT();
  14520. } /* END test_wc_CmacUpdate */
  14521. /*
  14522. * Testing wc_CmacFinal()
  14523. */
  14524. static int test_wc_CmacFinal(void)
  14525. {
  14526. EXPECT_DECLS;
  14527. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14528. Cmac cmac;
  14529. byte key[] = {
  14530. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14531. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14532. };
  14533. byte msg[] = {
  14534. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  14535. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  14536. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  14537. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  14538. 0xf4
  14539. };
  14540. /* Test vectors from CMACGenAES128.rsp from
  14541. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  14542. * Per RFC4493 truncation of lsb is possible.
  14543. */
  14544. byte expMac[] = {
  14545. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  14546. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  14547. };
  14548. byte mac[AES_BLOCK_SIZE];
  14549. word32 msgSz = (word32)sizeof(msg);
  14550. word32 keySz = (word32)sizeof(key);
  14551. word32 macSz = sizeof(mac);
  14552. word32 badMacSz = 17;
  14553. int expMacSz = sizeof(expMac);
  14554. int type = WC_CMAC_AES;
  14555. XMEMSET(&cmac, 0, sizeof(Cmac));
  14556. XMEMSET(mac, 0, macSz);
  14557. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  14558. ExpectIntEQ(wc_CmacUpdate(&cmac, msg, msgSz), 0);
  14559. #if (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  14560. /* Pass in bad args. */
  14561. ExpectIntEQ(wc_CmacFinalNoFree(NULL, mac, &macSz), BAD_FUNC_ARG);
  14562. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  14563. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, mac, &badMacSz), BUFFER_E);
  14564. /* For the last call, use the API with implicit wc_CmacFree(). */
  14565. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  14566. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14567. #else /* !HAVE_FIPS || FIPS>=5.3 */
  14568. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  14569. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14570. /* Pass in bad args. */
  14571. ExpectIntEQ(wc_CmacFinal(NULL, mac, &macSz), BAD_FUNC_ARG);
  14572. ExpectIntEQ(wc_CmacFinal(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  14573. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &badMacSz), BUFFER_E);
  14574. #endif /* !HAVE_FIPS || FIPS>=5.3 */
  14575. #endif
  14576. return EXPECT_RESULT();
  14577. } /* END test_wc_CmacFinal */
  14578. /*
  14579. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  14580. */
  14581. static int test_wc_AesCmacGenerate(void)
  14582. {
  14583. EXPECT_DECLS;
  14584. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14585. byte key[] = {
  14586. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  14587. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  14588. };
  14589. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  14590. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  14591. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  14592. "\x3d\x32\x65\x4c\x66\x23\xc5";
  14593. byte mac[AES_BLOCK_SIZE];
  14594. word32 keySz = sizeof(key);
  14595. word32 macSz = sizeof(mac);
  14596. word32 msgSz = sizeof(msg) - 1;
  14597. word32 expMacSz = sizeof(expMac) - 1;
  14598. XMEMSET(mac, 0, macSz);
  14599. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz), 0);
  14600. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14601. /* Pass in bad args. */
  14602. ExpectIntEQ(wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz),
  14603. BAD_FUNC_ARG);
  14604. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz),
  14605. BAD_FUNC_ARG);
  14606. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0),
  14607. BAD_FUNC_ARG);
  14608. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz),
  14609. BAD_FUNC_ARG);
  14610. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz), 0);
  14611. /* Test bad args. */
  14612. ExpectIntEQ(wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz),
  14613. BAD_FUNC_ARG);
  14614. ExpectIntEQ(wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz),
  14615. BAD_FUNC_ARG);
  14616. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz),
  14617. BAD_FUNC_ARG);
  14618. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0),
  14619. BAD_FUNC_ARG);
  14620. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz),
  14621. BAD_FUNC_ARG);
  14622. #endif
  14623. return EXPECT_RESULT();
  14624. } /* END test_wc_AesCmacGenerate */
  14625. /*
  14626. * Testing streaming AES-GCM API.
  14627. */
  14628. static int test_wc_AesGcmStream(void)
  14629. {
  14630. EXPECT_DECLS;
  14631. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  14632. defined(WOLFSSL_AESGCM_STREAM)
  14633. int i;
  14634. WC_RNG rng[1];
  14635. Aes aesEnc[1];
  14636. Aes aesDec[1];
  14637. byte tag[AES_BLOCK_SIZE];
  14638. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14639. byte out[AES_BLOCK_SIZE * 3 + 2];
  14640. byte plain[AES_BLOCK_SIZE * 3 + 2];
  14641. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14642. byte key[AES_128_KEY_SIZE] = { 0, };
  14643. byte iv[AES_IV_SIZE] = { 1, };
  14644. byte ivOut[AES_IV_SIZE];
  14645. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  14646. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  14647. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  14648. };
  14649. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  14650. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  14651. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  14652. };
  14653. static const byte expTag[AES_BLOCK_SIZE] = {
  14654. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  14655. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  14656. };
  14657. XMEMSET(&rng, 0, sizeof(WC_RNG));
  14658. XMEMSET(&aesEnc, 0, sizeof(Aes));
  14659. XMEMSET(&aesDec, 0, sizeof(Aes));
  14660. /* Create a random for generating IV/nonce. */
  14661. ExpectIntEQ(wc_InitRng(rng), 0);
  14662. /* Initialize data structures. */
  14663. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14664. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14665. /* BadParameters to streaming init. */
  14666. ExpectIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14667. ExpectIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14668. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  14669. BAD_FUNC_ARG);
  14670. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  14671. BAD_FUNC_ARG);
  14672. /* Bad parameters to encrypt update. */
  14673. ExpectIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14674. BAD_FUNC_ARG);
  14675. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  14676. BAD_FUNC_ARG);
  14677. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  14678. BAD_FUNC_ARG);
  14679. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  14680. BAD_FUNC_ARG);
  14681. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  14682. BAD_FUNC_ARG);
  14683. /* Bad parameters to decrypt update. */
  14684. ExpectIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14685. BAD_FUNC_ARG);
  14686. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  14687. BAD_FUNC_ARG);
  14688. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  14689. BAD_FUNC_ARG);
  14690. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  14691. BAD_FUNC_ARG);
  14692. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  14693. BAD_FUNC_ARG);
  14694. /* Bad parameters to encrypt final. */
  14695. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14696. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14697. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14698. BAD_FUNC_ARG);
  14699. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  14700. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  14701. BAD_FUNC_ARG);
  14702. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  14703. BAD_FUNC_ARG);
  14704. /* Bad parameters to decrypt final. */
  14705. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14706. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14707. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14708. BAD_FUNC_ARG);
  14709. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  14710. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  14711. BAD_FUNC_ARG);
  14712. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  14713. BAD_FUNC_ARG);
  14714. /* Check calling final before setting key fails. */
  14715. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  14716. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  14717. /* Check calling update before setting key else fails. */
  14718. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14719. MISSING_KEY);
  14720. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14721. MISSING_KEY);
  14722. /* Set key but not IV. */
  14723. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  14724. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  14725. /* Check calling final before setting IV fails. */
  14726. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  14727. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  14728. /* Check calling update before setting IV else fails. */
  14729. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14730. MISSING_IV);
  14731. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14732. MISSING_IV);
  14733. /* Set IV using fixed part IV and external IV APIs. */
  14734. ExpectIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  14735. rng), 0);
  14736. ExpectIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  14737. GCM_NONCE_MID_SZ), 0);
  14738. ExpectIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  14739. ExpectIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  14740. /* Encrypt and decrypt data. */
  14741. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  14742. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  14743. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  14744. /* Finalize and check tag matches. */
  14745. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14746. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14747. /* Set key and IV through streaming init API. */
  14748. wc_AesFree(aesEnc);
  14749. wc_AesFree(aesDec);
  14750. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14751. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14752. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14753. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14754. /* Encrypt/decrypt one block and AAD of one block. */
  14755. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  14756. AES_BLOCK_SIZE), 0);
  14757. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  14758. AES_BLOCK_SIZE), 0);
  14759. ExpectIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  14760. /* Finalize and check tag matches. */
  14761. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14762. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14763. /* Set key and IV through streaming init API. */
  14764. wc_AesFree(aesEnc);
  14765. wc_AesFree(aesDec);
  14766. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14767. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14768. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14769. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14770. /* No data to encrypt/decrypt one byte of AAD. */
  14771. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  14772. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  14773. /* Finalize and check tag matches. */
  14774. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14775. ExpectIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  14776. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14777. /* Set key and IV through streaming init API. */
  14778. wc_AesFree(aesEnc);
  14779. wc_AesFree(aesDec);
  14780. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14781. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14782. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14783. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14784. /* Encrypt/decrypt one byte and no AAD. */
  14785. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  14786. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  14787. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  14788. /* Finalize and check tag matches. */
  14789. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14790. ExpectIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  14791. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14792. /* Set key and IV through streaming init API. */
  14793. wc_AesFree(aesEnc);
  14794. wc_AesFree(aesDec);
  14795. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14796. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14797. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14798. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14799. /* Encryption AES is one byte at a time */
  14800. for (i = 0; i < (int)sizeof(aad); i++) {
  14801. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  14802. 0);
  14803. }
  14804. for (i = 0; i < (int)sizeof(in); i++) {
  14805. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  14806. 0);
  14807. }
  14808. /* Decryption AES is two bytes at a time */
  14809. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14810. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  14811. 0);
  14812. }
  14813. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14814. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  14815. 0), 0);
  14816. }
  14817. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14818. /* Finalize and check tag matches. */
  14819. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14820. ExpectIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  14821. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14822. /* Check streaming encryption can be decrypted with one shot. */
  14823. wc_AesFree(aesDec);
  14824. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14825. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14826. ExpectIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  14827. ExpectIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  14828. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14829. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14830. wc_AesFree(aesEnc);
  14831. wc_AesFree(aesDec);
  14832. wc_FreeRng(rng);
  14833. #endif
  14834. return EXPECT_RESULT();
  14835. } /* END test_wc_AesGcmStream */
  14836. /*
  14837. * Testing streaming SM4 API.
  14838. */
  14839. static int test_wc_Sm4(void)
  14840. {
  14841. int res = TEST_SKIPPED;
  14842. #ifdef WOLFSSL_SM4
  14843. EXPECT_DECLS;
  14844. wc_Sm4 sm4;
  14845. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  14846. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  14847. unsigned char key[SM4_KEY_SIZE];
  14848. #endif
  14849. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  14850. unsigned char iv[SM4_IV_SIZE];
  14851. #endif
  14852. /* Invalid parameters - wc_Sm4Init */
  14853. ExpectIntEQ(wc_Sm4Init(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  14854. /* Valid cases - wc_Sm4Init */
  14855. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14856. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  14857. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  14858. XMEMSET(key, 0, sizeof(key));
  14859. /* Invalid parameters - wc_Sm4SetKey. */
  14860. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  14861. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  14862. ExpectIntEQ(wc_Sm4SetKey(NULL, key, 0), BAD_FUNC_ARG);
  14863. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14864. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, 0), BAD_FUNC_ARG);
  14865. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14866. ExpectIntEQ(wc_Sm4SetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14867. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE-1), BAD_FUNC_ARG);
  14868. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE+1), BAD_FUNC_ARG);
  14869. /* Valid cases - wc_Sm4SetKey. */
  14870. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14871. #endif
  14872. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  14873. XMEMSET(iv, 0, sizeof(iv));
  14874. /* Invalid parameters - wc_Sm4SetIV. */
  14875. ExpectIntEQ(wc_Sm4SetIV(NULL, NULL), BAD_FUNC_ARG);
  14876. ExpectIntEQ(wc_Sm4SetIV(&sm4, NULL), BAD_FUNC_ARG);
  14877. ExpectIntEQ(wc_Sm4SetIV(NULL, iv), BAD_FUNC_ARG);
  14878. /* Valid cases - wc_Sm4SetIV. */
  14879. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14880. #endif
  14881. /* Valid cases - wc_Sm4Free */
  14882. wc_Sm4Free(NULL);
  14883. wc_Sm4Free(&sm4);
  14884. res = EXPECT_RESULT();
  14885. #endif
  14886. return res;
  14887. } /* END test_wc_Sm4 */
  14888. /*
  14889. * Testing block based SM4-ECB API.
  14890. */
  14891. static int test_wc_Sm4Ecb(void)
  14892. {
  14893. int res = TEST_SKIPPED;
  14894. #ifdef WOLFSSL_SM4_ECB
  14895. EXPECT_DECLS;
  14896. wc_Sm4 sm4;
  14897. unsigned char key[SM4_KEY_SIZE];
  14898. unsigned char in[SM4_BLOCK_SIZE * 2];
  14899. unsigned char out[SM4_BLOCK_SIZE * 2];
  14900. unsigned char out2[SM4_BLOCK_SIZE];
  14901. XMEMSET(key, 0, sizeof(key));
  14902. XMEMSET(in, 0, sizeof(in));
  14903. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14904. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14905. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), MISSING_KEY);
  14906. /* Tested in test_wc_Sm4. */
  14907. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14908. /* Invalid parameters - wc_Sm4EcbEncrypt. */
  14909. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14910. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14911. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14912. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14913. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14914. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14915. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14916. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14917. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14918. /* Valid cases - wc_Sm4EcbEncrypt. */
  14919. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), 0);
  14920. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14921. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14922. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14923. /* In and out are same pointer. */
  14924. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14925. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14926. /* Invalid parameters - wc_Sm4EcbDecrypt. */
  14927. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14928. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14929. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14930. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14931. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14932. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14933. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14934. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14935. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14936. /* Valid cases - wc_Sm4EcbDecrypt. */
  14937. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), 0);
  14938. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14939. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14940. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14941. /* In and out are same pointer. */
  14942. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14943. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14944. wc_Sm4Free(&sm4);
  14945. res = EXPECT_RESULT();
  14946. #endif
  14947. return res;
  14948. } /* END test_wc_Sm4Ecb */
  14949. /*
  14950. * Testing block based SM4-CBC API.
  14951. */
  14952. static int test_wc_Sm4Cbc(void)
  14953. {
  14954. int res = TEST_SKIPPED;
  14955. #ifdef WOLFSSL_SM4_CBC
  14956. EXPECT_DECLS;
  14957. wc_Sm4 sm4;
  14958. unsigned char key[SM4_KEY_SIZE];
  14959. unsigned char iv[SM4_IV_SIZE];
  14960. unsigned char in[SM4_BLOCK_SIZE * 2];
  14961. unsigned char out[SM4_BLOCK_SIZE * 2];
  14962. unsigned char out2[SM4_BLOCK_SIZE];
  14963. XMEMSET(key, 0, sizeof(key));
  14964. XMEMSET(iv, 0, sizeof(iv));
  14965. XMEMSET(in, 0, sizeof(in));
  14966. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14967. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14968. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_KEY);
  14969. /* Tested in test_wc_Sm4. */
  14970. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14971. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_IV);
  14972. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_IV);
  14973. /* Tested in test_wc_Sm4. */
  14974. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14975. /* Invalid parameters - wc_Sm4CbcEncrypt. */
  14976. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14977. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14978. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14979. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14980. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14981. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14982. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14983. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14984. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14985. /* Valid cases - wc_Sm4CbcEncrypt. */
  14986. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), 0);
  14987. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14988. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14989. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14990. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14991. /* In and out are same pointer. */
  14992. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14993. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14994. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14995. /* Invalid parameters - wc_Sm4CbcDecrypt. */
  14996. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14997. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14998. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14999. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  15000. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  15001. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  15002. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  15003. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  15004. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  15005. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15006. /* Valid cases - wc_Sm4CbcDecrypt. */
  15007. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), 0);
  15008. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15009. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15010. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15011. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15012. /* In and out are same pointer. */
  15013. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15014. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  15015. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15016. wc_Sm4Free(&sm4);
  15017. res = EXPECT_RESULT();
  15018. #endif
  15019. return res;
  15020. } /* END test_wc_Sm4Cbc */
  15021. /*
  15022. * Testing streaming SM4-CTR API.
  15023. */
  15024. static int test_wc_Sm4Ctr(void)
  15025. {
  15026. int res = TEST_SKIPPED;
  15027. #ifdef WOLFSSL_SM4_CTR
  15028. EXPECT_DECLS;
  15029. wc_Sm4 sm4;
  15030. unsigned char key[SM4_KEY_SIZE];
  15031. unsigned char iv[SM4_IV_SIZE];
  15032. unsigned char in[SM4_BLOCK_SIZE * 4];
  15033. unsigned char out[SM4_BLOCK_SIZE * 4];
  15034. unsigned char out2[SM4_BLOCK_SIZE * 4];
  15035. word32 chunk;
  15036. word32 i;
  15037. XMEMSET(key, 0, sizeof(key));
  15038. XMEMSET(iv, 0, sizeof(iv));
  15039. XMEMSET(in, 0, sizeof(in));
  15040. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15041. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_KEY);
  15042. /* Tested in test_wc_Sm4. */
  15043. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15044. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_IV);
  15045. /* Tested in test_wc_Sm4. */
  15046. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15047. /* Invalid parameters - wc_Sm4CtrEncrypt. */
  15048. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  15049. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, NULL, 0), BAD_FUNC_ARG);
  15050. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, NULL, 0), BAD_FUNC_ARG);
  15051. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, in, 0), BAD_FUNC_ARG);
  15052. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  15053. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  15054. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  15055. /* Valid cases - wc_Sm4CtrEncrypt. */
  15056. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), 0);
  15057. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, 1), 0);
  15058. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15059. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 2), 0);
  15060. ExpectIntEQ(XMEMCMP(out, out2, 1), 0);
  15061. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15062. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15063. ExpectIntEQ(XMEMCMP(out2, out, 2), 0);
  15064. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15065. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15066. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15067. /* In and out are same pointer. Also check encrypt of cipher text produces
  15068. * plaintext.
  15069. */
  15070. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15071. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, out, SM4_BLOCK_SIZE * 2), 0);
  15072. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15073. /* Chunking tests. */
  15074. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15075. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, (word32)sizeof(in)), 0);
  15076. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  15077. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15078. for (i = 0; i + chunk <= (word32)sizeof(in); i += chunk) {
  15079. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i, chunk), 0);
  15080. }
  15081. if (i < (word32)sizeof(in)) {
  15082. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i,
  15083. (word32)sizeof(in) - i), 0);
  15084. }
  15085. ExpectIntEQ(XMEMCMP(out, out2, (word32)sizeof(out)), 0);
  15086. }
  15087. for (i = 0; i < (word32)sizeof(iv); i++) {
  15088. iv[i] = 0xff;
  15089. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15090. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15091. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15092. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, out, SM4_BLOCK_SIZE * 2), 0);
  15093. ExpectIntEQ(XMEMCMP(out2, in, SM4_BLOCK_SIZE * 2), 0);
  15094. }
  15095. wc_Sm4Free(&sm4);
  15096. res = EXPECT_RESULT();
  15097. #endif
  15098. return res;
  15099. } /* END test_wc_Sm4Ctr */
  15100. /*
  15101. * Testing stream SM4-GCM API.
  15102. */
  15103. static int test_wc_Sm4Gcm(void)
  15104. {
  15105. int res = TEST_SKIPPED;
  15106. #ifdef WOLFSSL_SM4_GCM
  15107. EXPECT_DECLS;
  15108. wc_Sm4 sm4;
  15109. unsigned char key[SM4_KEY_SIZE];
  15110. unsigned char nonce[GCM_NONCE_MAX_SZ];
  15111. unsigned char in[SM4_BLOCK_SIZE * 2];
  15112. unsigned char in2[SM4_BLOCK_SIZE * 2];
  15113. unsigned char out[SM4_BLOCK_SIZE * 2];
  15114. unsigned char out2[SM4_BLOCK_SIZE * 2];
  15115. unsigned char dec[SM4_BLOCK_SIZE * 2];
  15116. unsigned char tag[SM4_BLOCK_SIZE];
  15117. unsigned char aad[SM4_BLOCK_SIZE * 2];
  15118. word32 i;
  15119. XMEMSET(key, 0, sizeof(key));
  15120. XMEMSET(nonce, 0, sizeof(nonce));
  15121. XMEMSET(in, 0, sizeof(in));
  15122. XMEMSET(in2, 0, sizeof(in2));
  15123. XMEMSET(aad, 0, sizeof(aad));
  15124. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15125. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  15126. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15127. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  15128. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15129. /* Invalid parameters - wc_Sm4GcmSetKey. */
  15130. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  15131. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  15132. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, 0), BAD_FUNC_ARG);
  15133. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15134. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, 0), BAD_FUNC_ARG);
  15135. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15136. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15137. /* Valid parameters - wc_Sm4GcmSetKey. */
  15138. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15139. /* Invalid parameters - wc_Sm4GcmEncrypt. */
  15140. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15141. 0), BAD_FUNC_ARG);
  15142. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15143. 0), BAD_FUNC_ARG);
  15144. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15145. 0), BAD_FUNC_ARG);
  15146. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15147. 0), BAD_FUNC_ARG);
  15148. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15149. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15150. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15151. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15152. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15153. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15154. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  15155. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15156. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15157. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15158. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  15159. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15160. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  15161. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15162. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  15163. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15164. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15165. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15166. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15167. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15168. /* Invalid parameters - wc_Sm4GcmDecrypt. */
  15169. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15170. 0), BAD_FUNC_ARG);
  15171. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15172. 0), BAD_FUNC_ARG);
  15173. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15174. 0), BAD_FUNC_ARG);
  15175. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15176. 0), BAD_FUNC_ARG);
  15177. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15178. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15179. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15180. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15181. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15182. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15183. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  15184. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15185. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15186. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15187. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  15188. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15189. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  15190. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15191. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  15192. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15193. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15194. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15195. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15196. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15197. /* Valid cases - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15198. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15199. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15200. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15201. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15202. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15203. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15204. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15205. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15206. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15207. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15208. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15209. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15210. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15211. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15212. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15213. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15214. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15215. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15216. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15217. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15218. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15219. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15220. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  15221. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15222. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15223. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  15224. /* Check vald values of nonce - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15225. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15226. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15227. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15228. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15229. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15230. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15231. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15232. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15233. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15234. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  15235. SM4_GCM_AUTH_E);
  15236. /* Check valid values of tag size - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15237. for (i = WOLFSSL_MIN_AUTH_TAG_SZ; i < SM4_BLOCK_SIZE; i++) {
  15238. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15239. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  15240. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15241. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  15242. }
  15243. /* Check different in/out sizes. */
  15244. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce,
  15245. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15246. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce,
  15247. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15248. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce,
  15249. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15250. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  15251. XMEMCPY(out2, out, i - 1);
  15252. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, i, nonce, GCM_NONCE_MID_SZ,
  15253. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15254. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  15255. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, dec, out, i, nonce, GCM_NONCE_MID_SZ,
  15256. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15257. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  15258. }
  15259. /* Force the counter to roll over in first byte. */
  15260. {
  15261. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  15262. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  15263. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  15264. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15265. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  15266. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15267. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  15268. }
  15269. wc_Sm4Free(&sm4);
  15270. res = EXPECT_RESULT();
  15271. #endif
  15272. return res;
  15273. } /* END test_wc_Sm4Gcm */
  15274. /*
  15275. * Testing stream SM4-CCM API.
  15276. */
  15277. static int test_wc_Sm4Ccm(void)
  15278. {
  15279. int res = TEST_SKIPPED;
  15280. #ifdef WOLFSSL_SM4_CCM
  15281. EXPECT_DECLS;
  15282. wc_Sm4 sm4;
  15283. unsigned char key[SM4_KEY_SIZE];
  15284. unsigned char nonce[CCM_NONCE_MAX_SZ];
  15285. unsigned char in[SM4_BLOCK_SIZE * 2];
  15286. unsigned char in2[SM4_BLOCK_SIZE * 2];
  15287. unsigned char out[SM4_BLOCK_SIZE * 2];
  15288. unsigned char out2[SM4_BLOCK_SIZE * 2];
  15289. unsigned char dec[SM4_BLOCK_SIZE * 2];
  15290. unsigned char tag[SM4_BLOCK_SIZE];
  15291. unsigned char aad[SM4_BLOCK_SIZE * 2];
  15292. word32 i;
  15293. XMEMSET(key, 0, sizeof(key));
  15294. XMEMSET(nonce, 0, sizeof(nonce));
  15295. XMEMSET(in, 0, sizeof(in));
  15296. XMEMSET(in2, 0, sizeof(in2));
  15297. XMEMSET(aad, 0, sizeof(aad));
  15298. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15299. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  15300. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15301. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  15302. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15303. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15304. /* Invalid parameters - wc_Sm4CcmEncrypt. */
  15305. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15306. 0), BAD_FUNC_ARG);
  15307. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15308. 0), BAD_FUNC_ARG);
  15309. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15310. 0), BAD_FUNC_ARG);
  15311. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15312. 0), BAD_FUNC_ARG);
  15313. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15314. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15315. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15316. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15317. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15318. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15319. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15320. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15321. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15322. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15323. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  15324. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15325. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  15326. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15327. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15328. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15329. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15330. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15331. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15332. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15333. /* Invalid parameters - wc_Sm4CcmDecrypt. */
  15334. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15335. 0), BAD_FUNC_ARG);
  15336. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15337. 0), BAD_FUNC_ARG);
  15338. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15339. 0), BAD_FUNC_ARG);
  15340. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15341. 0), BAD_FUNC_ARG);
  15342. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15343. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15344. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15345. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15346. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15347. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15348. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15349. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15350. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15351. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15352. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  15353. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15354. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  15355. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15356. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15357. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15358. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15359. WOLFSSL_MIN_AUTH_TAG_SZ - 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15360. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15361. SM4_BLOCK_SIZE + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15362. /* Valid cases - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15363. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15364. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15365. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15366. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15367. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15368. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15369. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15370. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15371. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15372. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15373. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15374. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15375. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15376. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15377. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15378. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15379. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15380. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15381. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15382. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15383. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15384. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15385. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  15386. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15387. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15388. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  15389. /* Check vald values of nonce - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15390. for (i = CCM_NONCE_MIN_SZ; i <= CCM_NONCE_MAX_SZ; i++) {
  15391. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15392. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15393. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15394. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15395. }
  15396. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15397. CCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  15398. SM4_CCM_AUTH_E);
  15399. /* Check invalid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15400. for (i = 0; i < 4; i++) {
  15401. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15402. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15403. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15404. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15405. }
  15406. /* Odd values in range 4..SM4_BLOCK_SIZE. */
  15407. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  15408. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15409. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15410. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15411. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15412. }
  15413. /* Check valid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt.
  15414. * Even values in range 4..SM4_BLOCK_SIZE.
  15415. */
  15416. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  15417. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15418. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  15419. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15420. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  15421. }
  15422. /* Check different in/out sizes. */
  15423. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce,
  15424. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15425. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce,
  15426. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15427. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce,
  15428. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15429. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  15430. XMEMCPY(out2, out, i - 1);
  15431. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, i, nonce, CCM_NONCE_MAX_SZ,
  15432. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15433. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  15434. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, dec, out, i, nonce, CCM_NONCE_MAX_SZ,
  15435. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15436. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  15437. }
  15438. /* Force the counter to roll over in first byte. */
  15439. {
  15440. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  15441. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  15442. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  15443. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15444. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  15445. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15446. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  15447. }
  15448. wc_Sm4Free(&sm4);
  15449. res = EXPECT_RESULT();
  15450. #endif
  15451. return res;
  15452. } /* END test_wc_Sm4Ccm */
  15453. /*
  15454. * unit test for wc_Des3_SetIV()
  15455. */
  15456. static int test_wc_Des3_SetIV(void)
  15457. {
  15458. EXPECT_DECLS;
  15459. #ifndef NO_DES3
  15460. Des3 des;
  15461. const byte key[] = {
  15462. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15463. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15464. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15465. };
  15466. const byte iv[] = {
  15467. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15468. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15469. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15470. };
  15471. XMEMSET(&des, 0, sizeof(Des3));
  15472. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15473. /* DES_ENCRYPTION or DES_DECRYPTION */
  15474. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15475. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  15476. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  15477. /* Test explicitly wc_Des3_SetIV() */
  15478. ExpectIntEQ(wc_Des3_SetIV(NULL, iv), BAD_FUNC_ARG);
  15479. ExpectIntEQ(wc_Des3_SetIV(&des, NULL), 0);
  15480. #endif
  15481. wc_Des3Free(&des);
  15482. #endif
  15483. return EXPECT_RESULT();
  15484. } /* END test_wc_Des3_SetIV */
  15485. /*
  15486. * unit test for wc_Des3_SetKey()
  15487. */
  15488. static int test_wc_Des3_SetKey(void)
  15489. {
  15490. EXPECT_DECLS;
  15491. #ifndef NO_DES3
  15492. Des3 des;
  15493. const byte key[] = {
  15494. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15495. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15496. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15497. };
  15498. const byte iv[] = {
  15499. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15500. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15501. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15502. };
  15503. XMEMSET(&des, 0, sizeof(Des3));
  15504. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15505. /* DES_ENCRYPTION or DES_DECRYPTION */
  15506. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15507. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  15508. /* Test bad args. */
  15509. ExpectIntEQ(wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  15510. ExpectIntEQ(wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  15511. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, -1), BAD_FUNC_ARG);
  15512. /* Default case. Should return 0. */
  15513. ExpectIntEQ(wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION), 0);
  15514. wc_Des3Free(&des);
  15515. #endif
  15516. return EXPECT_RESULT();
  15517. } /* END test_wc_Des3_SetKey */
  15518. /*
  15519. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  15520. */
  15521. static int test_wc_Des3_CbcEncryptDecrypt(void)
  15522. {
  15523. EXPECT_DECLS;
  15524. #ifndef NO_DES3
  15525. Des3 des;
  15526. byte cipher[24];
  15527. byte plain[24];
  15528. const byte key[] = {
  15529. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15530. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15531. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15532. };
  15533. const byte iv[] = {
  15534. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15535. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15536. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15537. };
  15538. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  15539. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15540. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15541. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15542. };
  15543. XMEMSET(&des, 0, sizeof(Des3));
  15544. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15545. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15546. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, vector, 24), 0);
  15547. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION), 0);
  15548. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, cipher, 24), 0);
  15549. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  15550. /* Pass in bad args. */
  15551. ExpectIntEQ(wc_Des3_CbcEncrypt(NULL, cipher, vector, 24), BAD_FUNC_ARG);
  15552. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, NULL, vector, 24), BAD_FUNC_ARG);
  15553. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector)),
  15554. BAD_FUNC_ARG);
  15555. ExpectIntEQ(wc_Des3_CbcDecrypt(NULL, plain, cipher, 24), BAD_FUNC_ARG);
  15556. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, NULL, cipher, 24), BAD_FUNC_ARG);
  15557. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, NULL, 24), BAD_FUNC_ARG);
  15558. wc_Des3Free(&des);
  15559. #endif
  15560. return EXPECT_RESULT();
  15561. } /* END wc_Des3_CbcEncrypt */
  15562. /*
  15563. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  15564. */
  15565. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  15566. {
  15567. EXPECT_DECLS;
  15568. #ifndef NO_DES3
  15569. word32 vectorSz, cipherSz;
  15570. byte cipher[24];
  15571. byte plain[24];
  15572. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15573. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15574. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15575. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15576. };
  15577. byte key[] = {
  15578. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15579. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15580. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15581. };
  15582. byte iv[] = {
  15583. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15584. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15585. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15586. };
  15587. vectorSz = sizeof(byte) * 24;
  15588. cipherSz = sizeof(byte) * 24;
  15589. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv),
  15590. 0);
  15591. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv), 0);
  15592. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  15593. /* pass in bad args. */
  15594. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv),
  15595. BAD_FUNC_ARG);
  15596. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv),
  15597. BAD_FUNC_ARG);
  15598. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv),
  15599. BAD_FUNC_ARG);
  15600. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, NULL),
  15601. 0);
  15602. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv),
  15603. BAD_FUNC_ARG);
  15604. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv),
  15605. BAD_FUNC_ARG);
  15606. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv),
  15607. BAD_FUNC_ARG);
  15608. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL),
  15609. 0);
  15610. #endif
  15611. return EXPECT_RESULT();
  15612. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  15613. /*
  15614. * Unit test for wc_Des3_EcbEncrypt
  15615. */
  15616. static int test_wc_Des3_EcbEncrypt(void)
  15617. {
  15618. EXPECT_DECLS;
  15619. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  15620. Des3 des;
  15621. byte cipher[24];
  15622. word32 cipherSz = sizeof(cipher);
  15623. const byte key[] = {
  15624. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15625. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15626. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15627. };
  15628. const byte iv[] = {
  15629. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15630. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15631. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15632. };
  15633. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  15634. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15635. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15636. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15637. };
  15638. XMEMSET(&des, 0, sizeof(Des3));
  15639. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15640. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15641. /* Bad Cases */
  15642. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15643. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz),
  15644. BAD_FUNC_ARG);
  15645. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz), BAD_FUNC_ARG);
  15646. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz), BAD_FUNC_ARG);
  15647. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, 0), 0);
  15648. /* Good Cases */
  15649. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz), 0);
  15650. wc_Des3Free(&des);
  15651. #endif
  15652. return EXPECT_RESULT();
  15653. } /* END test_wc_Des3_EcbEncrypt */
  15654. /*
  15655. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  15656. */
  15657. static int test_wc_Chacha_SetKey(void)
  15658. {
  15659. EXPECT_DECLS;
  15660. #ifdef HAVE_CHACHA
  15661. ChaCha ctx;
  15662. const byte key[] = {
  15663. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15664. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15665. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15666. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15667. };
  15668. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  15669. byte cipher[128];
  15670. XMEMSET(cipher, 0, sizeof(cipher));
  15671. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, keySz), 0);
  15672. /* Test bad args. */
  15673. ExpectIntEQ(wc_Chacha_SetKey(NULL, key, keySz), BAD_FUNC_ARG);
  15674. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  15675. ExpectIntEQ(wc_Chacha_SetIV(&ctx, cipher, 0), 0);
  15676. /* Test bad args. */
  15677. ExpectIntEQ(wc_Chacha_SetIV(NULL, cipher, 0), BAD_FUNC_ARG);
  15678. #endif
  15679. return EXPECT_RESULT();
  15680. } /* END test_wc_Chacha_SetKey */
  15681. /*
  15682. * unit test for wc_Poly1305SetKey()
  15683. */
  15684. static int test_wc_Poly1305SetKey(void)
  15685. {
  15686. EXPECT_DECLS;
  15687. #ifdef HAVE_POLY1305
  15688. Poly1305 ctx;
  15689. const byte key[] =
  15690. {
  15691. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15692. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15693. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15694. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15695. };
  15696. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  15697. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, keySz), 0);
  15698. /* Test bad args. */
  15699. ExpectIntEQ(wc_Poly1305SetKey(NULL, key,keySz), BAD_FUNC_ARG);
  15700. ExpectIntEQ(wc_Poly1305SetKey(&ctx, NULL, keySz), BAD_FUNC_ARG);
  15701. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  15702. #endif
  15703. return EXPECT_RESULT();
  15704. } /* END test_wc_Poly1305_SetKey() */
  15705. /*
  15706. * Testing wc_Chacha_Process()
  15707. */
  15708. static int test_wc_Chacha_Process(void)
  15709. {
  15710. EXPECT_DECLS;
  15711. #ifdef HAVE_CHACHA
  15712. ChaCha enc, dec;
  15713. byte cipher[128];
  15714. byte plain[128];
  15715. const byte key[] =
  15716. {
  15717. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15718. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15719. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15720. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15721. };
  15722. const char* input = "Everybody gets Friday off.";
  15723. word32 keySz = sizeof(key)/sizeof(byte);
  15724. unsigned long int inlen = XSTRLEN(input);
  15725. /* Initialize stack variables. */
  15726. XMEMSET(cipher, 0, 128);
  15727. XMEMSET(plain, 0, 128);
  15728. ExpectIntEQ(wc_Chacha_SetKey(&enc, key, keySz), 0);
  15729. ExpectIntEQ(wc_Chacha_SetKey(&dec, key, keySz), 0);
  15730. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  15731. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  15732. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen),
  15733. 0);
  15734. ExpectIntEQ(wc_Chacha_Process(&dec, plain, cipher, (word32)inlen), 0);
  15735. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  15736. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  15737. /* test checking and using leftovers, currently just in C code */
  15738. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  15739. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  15740. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input,
  15741. (word32)inlen - 2), 0);
  15742. ExpectIntEQ(wc_Chacha_Process(&enc, cipher + (inlen - 2),
  15743. (byte*)input + (inlen - 2), 2), 0);
  15744. ExpectIntEQ(wc_Chacha_Process(&dec, plain, (byte*)cipher,
  15745. (word32)inlen - 2), 0);
  15746. ExpectIntEQ(wc_Chacha_Process(&dec, cipher + (inlen - 2),
  15747. (byte*)input + (inlen - 2), 2), 0);
  15748. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  15749. /* check edge cases with counter increment */
  15750. {
  15751. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  15752. const byte expected[] = {
  15753. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  15754. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  15755. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  15756. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  15757. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  15758. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  15759. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  15760. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  15761. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  15762. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  15763. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  15764. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  15765. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  15766. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  15767. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  15768. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  15769. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  15770. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  15771. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  15772. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  15773. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  15774. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  15775. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  15776. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  15777. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  15778. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  15779. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  15780. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  15781. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  15782. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  15783. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  15784. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  15785. };
  15786. const byte iv2[] = {
  15787. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  15788. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  15789. };
  15790. byte input2[256];
  15791. int i;
  15792. for (i = 0; i < 256; i++)
  15793. input2[i] = i;
  15794. ExpectIntEQ(wc_Chacha_SetIV(&enc, iv2, 0), 0);
  15795. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2, 64), 0);
  15796. ExpectIntEQ(XMEMCMP(expected, cipher, 64), 0);
  15797. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 64, 128), 0);
  15798. ExpectIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  15799. /* partial */
  15800. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 192, 32), 0);
  15801. ExpectIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  15802. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 224, 32), 0);
  15803. ExpectIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  15804. }
  15805. #endif
  15806. /* Test bad args. */
  15807. ExpectIntEQ(wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen),
  15808. BAD_FUNC_ARG);
  15809. #endif
  15810. return EXPECT_RESULT();
  15811. } /* END test_wc_Chacha_Process */
  15812. /*
  15813. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  15814. */
  15815. static int test_wc_ChaCha20Poly1305_aead(void)
  15816. {
  15817. EXPECT_DECLS;
  15818. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  15819. const byte key[] = {
  15820. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  15821. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  15822. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  15823. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  15824. };
  15825. const byte plaintext[] = {
  15826. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  15827. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  15828. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  15829. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  15830. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  15831. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  15832. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  15833. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  15834. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  15835. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  15836. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  15837. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  15838. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  15839. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  15840. 0x74, 0x2e
  15841. };
  15842. const byte iv[] = {
  15843. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  15844. 0x44, 0x45, 0x46, 0x47
  15845. };
  15846. const byte aad[] = { /* additional data */
  15847. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  15848. 0xc4, 0xc5, 0xc6, 0xc7
  15849. };
  15850. const byte cipher[] = { /* expected output from operation */
  15851. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  15852. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  15853. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  15854. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  15855. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  15856. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  15857. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  15858. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  15859. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  15860. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  15861. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  15862. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  15863. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  15864. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  15865. 0x61, 0x16
  15866. };
  15867. const byte authTag[] = { /* expected output from operation */
  15868. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  15869. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  15870. };
  15871. byte generatedCiphertext[272];
  15872. byte generatedPlaintext[272];
  15873. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  15874. /* Initialize stack variables. */
  15875. XMEMSET(generatedCiphertext, 0, 272);
  15876. XMEMSET(generatedPlaintext, 0, 272);
  15877. /* Test Encrypt */
  15878. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15879. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15880. 0);
  15881. ExpectIntEQ(XMEMCMP(generatedCiphertext, cipher,
  15882. sizeof(cipher)/sizeof(byte)), 0);
  15883. /* Test bad args. */
  15884. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad),
  15885. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15886. BAD_FUNC_ARG);
  15887. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  15888. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15889. BAD_FUNC_ARG);
  15890. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  15891. sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15892. BAD_FUNC_ARG);
  15893. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15894. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15895. BAD_FUNC_ARG);
  15896. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15897. plaintext, sizeof(plaintext), NULL, generatedAuthTag), BAD_FUNC_ARG);
  15898. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15899. plaintext, sizeof(plaintext), generatedCiphertext, NULL), BAD_FUNC_ARG);
  15900. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15901. sizeof(cipher), authTag, generatedPlaintext), 0);
  15902. ExpectIntEQ(XMEMCMP(generatedPlaintext, plaintext,
  15903. sizeof(plaintext)/sizeof(byte)), 0);
  15904. /* Test bad args. */
  15905. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  15906. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15907. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  15908. cipher, sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15909. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15910. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15911. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15912. sizeof(cipher), NULL, generatedPlaintext), BAD_FUNC_ARG);
  15913. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15914. sizeof(cipher), authTag, NULL), BAD_FUNC_ARG);
  15915. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15916. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15917. #endif
  15918. return EXPECT_RESULT();
  15919. } /* END test_wc_ChaCha20Poly1305_aead */
  15920. /*
  15921. * Testing function for wc_Rc2SetKey().
  15922. */
  15923. static int test_wc_Rc2SetKey(void)
  15924. {
  15925. EXPECT_DECLS;
  15926. #ifdef WC_RC2
  15927. Rc2 rc2;
  15928. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  15929. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15930. /* valid key and IV */
  15931. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15932. iv, 40), 0);
  15933. /* valid key, no IV */
  15934. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15935. NULL, 40), 0);
  15936. /* bad arguments */
  15937. /* null Rc2 struct */
  15938. ExpectIntEQ(wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  15939. iv, 40), BAD_FUNC_ARG);
  15940. /* null key */
  15941. ExpectIntEQ(wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  15942. iv, 40), BAD_FUNC_ARG);
  15943. /* key size == 0 */
  15944. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 0, iv, 40), WC_KEY_SIZE_E);
  15945. /* key size > 128 */
  15946. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 129, iv, 40), WC_KEY_SIZE_E);
  15947. /* effective bits == 0 */
  15948. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15949. iv, 0), WC_KEY_SIZE_E);
  15950. /* effective bits > 1024 */
  15951. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15952. iv, 1025), WC_KEY_SIZE_E);
  15953. #endif
  15954. return EXPECT_RESULT();
  15955. } /* END test_wc_Rc2SetKey */
  15956. /*
  15957. * Testing function for wc_Rc2SetIV().
  15958. */
  15959. static int test_wc_Rc2SetIV(void)
  15960. {
  15961. EXPECT_DECLS;
  15962. #ifdef WC_RC2
  15963. Rc2 rc2;
  15964. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15965. /* valid IV */
  15966. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  15967. /* valid NULL IV */
  15968. ExpectIntEQ(wc_Rc2SetIV(&rc2, NULL), 0);
  15969. /* bad arguments */
  15970. ExpectIntEQ(wc_Rc2SetIV(NULL, iv), BAD_FUNC_ARG);
  15971. ExpectIntEQ(wc_Rc2SetIV(NULL, NULL), BAD_FUNC_ARG);
  15972. #endif
  15973. return EXPECT_RESULT();
  15974. } /* END test_wc_Rc2SetIV */
  15975. /*
  15976. * Testing function for wc_Rc2EcbEncrypt() and wc_Rc2EcbDecrypt().
  15977. */
  15978. static int test_wc_Rc2EcbEncryptDecrypt(void)
  15979. {
  15980. EXPECT_DECLS;
  15981. #ifdef WC_RC2
  15982. Rc2 rc2;
  15983. int effectiveKeyBits = 63;
  15984. byte cipher[RC2_BLOCK_SIZE];
  15985. byte plain[RC2_BLOCK_SIZE];
  15986. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  15987. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  15988. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  15989. XMEMSET(cipher, 0, sizeof(cipher));
  15990. XMEMSET(plain, 0, sizeof(plain));
  15991. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  15992. NULL, effectiveKeyBits), 0);
  15993. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE), 0);
  15994. ExpectIntEQ(XMEMCMP(cipher, output, RC2_BLOCK_SIZE), 0);
  15995. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE), 0);
  15996. ExpectIntEQ(XMEMCMP(plain, input, RC2_BLOCK_SIZE), 0);
  15997. /* Rc2EcbEncrypt bad arguments */
  15998. /* null Rc2 struct */
  15999. ExpectIntEQ(wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE),
  16000. BAD_FUNC_ARG);
  16001. /* null out buffer */
  16002. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE),
  16003. BAD_FUNC_ARG);
  16004. /* null input buffer */
  16005. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE),
  16006. BAD_FUNC_ARG);
  16007. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  16008. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, 7), BUFFER_E);
  16009. /* Rc2EcbDecrypt bad arguments */
  16010. /* null Rc2 struct */
  16011. ExpectIntEQ(wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE),
  16012. BAD_FUNC_ARG);
  16013. /* null out buffer */
  16014. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE),
  16015. BAD_FUNC_ARG);
  16016. /* null input buffer */
  16017. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE),
  16018. BAD_FUNC_ARG);
  16019. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  16020. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, output, 7), BUFFER_E);
  16021. #endif
  16022. return EXPECT_RESULT();
  16023. } /* END test_wc_Rc2EcbEncryptDecrypt */
  16024. /*
  16025. * Testing function for wc_Rc2CbcEncrypt() and wc_Rc2CbcDecrypt().
  16026. */
  16027. static int test_wc_Rc2CbcEncryptDecrypt(void)
  16028. {
  16029. EXPECT_DECLS;
  16030. #ifdef WC_RC2
  16031. Rc2 rc2;
  16032. int effectiveKeyBits = 63;
  16033. byte cipher[RC2_BLOCK_SIZE*2];
  16034. byte plain[RC2_BLOCK_SIZE*2];
  16035. /* vector taken from test.c */
  16036. byte key[] = {
  16037. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16038. };
  16039. byte iv[] = {
  16040. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16041. };
  16042. byte input[] = {
  16043. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  16044. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16045. };
  16046. byte output[] = {
  16047. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  16048. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  16049. };
  16050. XMEMSET(cipher, 0, sizeof(cipher));
  16051. XMEMSET(plain, 0, sizeof(plain));
  16052. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  16053. iv, effectiveKeyBits), 0);
  16054. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input)), 0);
  16055. ExpectIntEQ(XMEMCMP(cipher, output, sizeof(output)), 0);
  16056. /* reset IV for decrypt */
  16057. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  16058. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher)), 0);
  16059. ExpectIntEQ(XMEMCMP(plain, input, sizeof(input)), 0);
  16060. /* Rc2CbcEncrypt bad arguments */
  16061. /* null Rc2 struct */
  16062. ExpectIntEQ(wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input)),
  16063. BAD_FUNC_ARG);
  16064. /* null out buffer */
  16065. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input)),
  16066. BAD_FUNC_ARG);
  16067. /* null input buffer */
  16068. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input)),
  16069. BAD_FUNC_ARG);
  16070. /* Rc2CbcDecrypt bad arguments */
  16071. /* in size is 0 */
  16072. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, output, 0), 0);
  16073. /* null Rc2 struct */
  16074. ExpectIntEQ(wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output)),
  16075. BAD_FUNC_ARG);
  16076. /* null out buffer */
  16077. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output)),
  16078. BAD_FUNC_ARG);
  16079. /* null input buffer */
  16080. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output)),
  16081. BAD_FUNC_ARG);
  16082. #endif
  16083. return EXPECT_RESULT();
  16084. } /* END test_wc_Rc2CbcEncryptDecrypt */
  16085. /*
  16086. * Testing function for wc_AesSetIV
  16087. */
  16088. static int test_wc_AesSetIV(void)
  16089. {
  16090. int res = TEST_SKIPPED;
  16091. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  16092. Aes aes;
  16093. int ret = 0;
  16094. byte key16[] =
  16095. {
  16096. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16097. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16098. };
  16099. byte iv1[] = "1234567890abcdef";
  16100. byte iv2[] = "0987654321fedcba";
  16101. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  16102. if (ret != 0)
  16103. return ret;
  16104. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  16105. iv1, AES_ENCRYPTION);
  16106. if (ret == 0) {
  16107. ret = wc_AesSetIV(&aes, iv2);
  16108. }
  16109. /* Test bad args. */
  16110. if (ret == 0) {
  16111. ret = wc_AesSetIV(NULL, iv1);
  16112. if (ret == BAD_FUNC_ARG) {
  16113. /* NULL iv should return 0. */
  16114. ret = wc_AesSetIV(&aes, NULL);
  16115. }
  16116. else {
  16117. ret = WOLFSSL_FATAL_ERROR;
  16118. }
  16119. }
  16120. wc_AesFree(&aes);
  16121. res = TEST_RES_CHECK(ret == 0);
  16122. #endif
  16123. return res;
  16124. } /* test_wc_AesSetIV */
  16125. /*
  16126. * Testing function for wc_AesSetKey().
  16127. */
  16128. static int test_wc_AesSetKey(void)
  16129. {
  16130. EXPECT_DECLS;
  16131. #ifndef NO_AES
  16132. Aes aes;
  16133. byte key16[] = {
  16134. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16135. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16136. };
  16137. #ifdef WOLFSSL_AES_192
  16138. byte key24[] = {
  16139. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16140. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16141. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16142. };
  16143. #endif
  16144. #ifdef WOLFSSL_AES_256
  16145. byte key32[] = {
  16146. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16147. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16148. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16149. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16150. };
  16151. #endif
  16152. byte badKey16[] = {
  16153. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16154. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16155. };
  16156. byte iv[] = "1234567890abcdef";
  16157. XMEMSET(&aes, 0, sizeof(Aes));
  16158. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16159. #ifdef WOLFSSL_AES_128
  16160. ExpectIntEQ(wc_AesSetKey(&aes, key16, (word32)sizeof(key16) / sizeof(byte),
  16161. iv, AES_ENCRYPTION), 0);
  16162. #endif
  16163. #ifdef WOLFSSL_AES_192
  16164. ExpectIntEQ(wc_AesSetKey(&aes, key24, (word32)sizeof(key24) / sizeof(byte),
  16165. iv, AES_ENCRYPTION), 0);
  16166. #endif
  16167. #ifdef WOLFSSL_AES_256
  16168. ExpectIntEQ(wc_AesSetKey(&aes, key32, (word32)sizeof(key32) / sizeof(byte),
  16169. iv, AES_ENCRYPTION), 0);
  16170. #endif
  16171. /* Pass in bad args. */
  16172. ExpectIntEQ(wc_AesSetKey(NULL, key16, (word32)sizeof(key16) / sizeof(byte),
  16173. iv, AES_ENCRYPTION), BAD_FUNC_ARG);
  16174. ExpectIntEQ(wc_AesSetKey(&aes, badKey16,
  16175. (word32)sizeof(badKey16) / sizeof(byte), iv, AES_ENCRYPTION),
  16176. BAD_FUNC_ARG);
  16177. wc_AesFree(&aes);
  16178. #endif
  16179. return EXPECT_RESULT();
  16180. } /* END test_wc_AesSetKey */
  16181. /*
  16182. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  16183. * and wc_AesCbcDecryptWithKey()
  16184. */
  16185. static int test_wc_AesCbcEncryptDecrypt(void)
  16186. {
  16187. EXPECT_DECLS;
  16188. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  16189. defined(WOLFSSL_AES_256)
  16190. Aes aes;
  16191. byte key32[] = {
  16192. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16193. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16194. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16195. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16196. };
  16197. byte vector[] = { /* Now is the time for all good men w/o trailing 0 */
  16198. 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
  16199. 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20,
  16200. 0x66, 0x6f, 0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20,
  16201. 0x67, 0x6f, 0x6f, 0x64, 0x20, 0x6d, 0x65, 0x6e
  16202. };
  16203. byte iv[] = "1234567890abcdef";
  16204. byte enc[sizeof(vector)];
  16205. byte dec[sizeof(vector)];
  16206. byte dec2[sizeof(vector)];
  16207. /* Init stack variables. */
  16208. XMEMSET(&aes, 0, sizeof(Aes));
  16209. XMEMSET(enc, 0, sizeof(enc));
  16210. XMEMSET(dec, 0, sizeof(vector));
  16211. XMEMSET(dec2, 0, sizeof(vector));
  16212. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16213. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  16214. AES_ENCRYPTION), 0);
  16215. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector)), 0);
  16216. /* Re init for decrypt and set flag. */
  16217. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  16218. AES_DECRYPTION), 0);
  16219. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector)), 0);
  16220. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16221. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE, key32,
  16222. sizeof(key32)/sizeof(byte), iv), 0);
  16223. ExpectIntEQ(XMEMCMP(vector, dec2, AES_BLOCK_SIZE), 0);
  16224. /* Pass in bad args */
  16225. ExpectIntEQ(wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector)),
  16226. BAD_FUNC_ARG);
  16227. ExpectIntEQ(wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector)),
  16228. BAD_FUNC_ARG);
  16229. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector)),
  16230. BAD_FUNC_ARG);
  16231. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  16232. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1),
  16233. BAD_LENGTH_E);
  16234. #endif
  16235. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16236. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  16237. fprintf(stderr, "Zero length inputs not supported with AESNI in FIPS "
  16238. "mode (v2), skip test");
  16239. #else
  16240. /* Test passing in size of 0 */
  16241. XMEMSET(enc, 0, sizeof(enc));
  16242. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, 0), 0);
  16243. /* Check enc was not modified */
  16244. {
  16245. int i;
  16246. for (i = 0; i < (int)sizeof(enc); i++)
  16247. ExpectIntEQ(enc[i], 0);
  16248. }
  16249. #endif
  16250. ExpectIntEQ(wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE), BAD_FUNC_ARG);
  16251. ExpectIntEQ(wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE),
  16252. BAD_FUNC_ARG);
  16253. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE),
  16254. BAD_FUNC_ARG);
  16255. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  16256. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  16257. BAD_LENGTH_E);
  16258. #else
  16259. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  16260. BAD_FUNC_ARG);
  16261. #endif
  16262. /* Test passing in size of 0 */
  16263. XMEMSET(dec, 0, sizeof(dec));
  16264. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, 0), 0);
  16265. /* Check dec was not modified */
  16266. {
  16267. int i;
  16268. for (i = 0; i < (int)sizeof(dec); i++)
  16269. ExpectIntEQ(dec[i], 0);
  16270. }
  16271. ExpectIntEQ(wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  16272. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16273. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  16274. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16275. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  16276. NULL, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16277. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  16278. key32, sizeof(key32)/sizeof(byte), NULL), BAD_FUNC_ARG);
  16279. wc_AesFree(&aes);
  16280. #endif
  16281. return EXPECT_RESULT();
  16282. } /* END test_wc_AesCbcEncryptDecrypt */
  16283. /*
  16284. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  16285. */
  16286. static int test_wc_AesCtrEncryptDecrypt(void)
  16287. {
  16288. EXPECT_DECLS;
  16289. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  16290. Aes aesEnc;
  16291. Aes aesDec;
  16292. byte key32[] = {
  16293. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16294. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16295. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16296. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16297. };
  16298. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  16299. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16300. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16301. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16302. };
  16303. byte iv[] = "1234567890abcdef";
  16304. byte enc[AES_BLOCK_SIZE * 2];
  16305. byte dec[AES_BLOCK_SIZE * 2];
  16306. /* Init stack variables. */
  16307. XMEMSET(&aesEnc, 0, sizeof(Aes));
  16308. XMEMSET(&aesDec, 0, sizeof(Aes));
  16309. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  16310. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  16311. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  16312. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  16313. ExpectIntEQ(wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2, iv,
  16314. AES_ENCRYPTION), 0);
  16315. ExpectIntEQ(wc_AesCtrEncrypt(&aesEnc, enc, vector,
  16316. sizeof(vector)/sizeof(byte)), 0);
  16317. /* Decrypt with wc_AesCtrEncrypt() */
  16318. ExpectIntEQ(wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2, iv,
  16319. AES_ENCRYPTION), 0);
  16320. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte)),
  16321. 0);
  16322. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16323. /* Test bad args. */
  16324. ExpectIntEQ(wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte)),
  16325. BAD_FUNC_ARG);
  16326. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte)),
  16327. BAD_FUNC_ARG);
  16328. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte)),
  16329. BAD_FUNC_ARG);
  16330. wc_AesFree(&aesEnc);
  16331. wc_AesFree(&aesDec);
  16332. #endif
  16333. return EXPECT_RESULT();
  16334. } /* END test_wc_AesCtrEncryptDecrypt */
  16335. /*
  16336. * test function for wc_AesGcmSetKey()
  16337. */
  16338. static int test_wc_AesGcmSetKey(void)
  16339. {
  16340. EXPECT_DECLS;
  16341. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16342. Aes aes;
  16343. #ifdef WOLFSSL_AES_128
  16344. byte key16[] = {
  16345. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16346. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16347. };
  16348. #endif
  16349. #ifdef WOLFSSL_AES_192
  16350. byte key24[] = {
  16351. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16352. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16353. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16354. };
  16355. #endif
  16356. #ifdef WOLFSSL_AES_256
  16357. byte key32[] = {
  16358. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16359. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16360. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16361. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16362. };
  16363. #endif
  16364. byte badKey16[] = {
  16365. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16366. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16367. };
  16368. byte badKey24[] = {
  16369. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16370. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16371. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  16372. };
  16373. byte badKey32[] = {
  16374. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  16375. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16376. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16377. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16378. };
  16379. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16380. #ifdef WOLFSSL_AES_128
  16381. ExpectIntEQ(wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte)), 0);
  16382. #endif
  16383. #ifdef WOLFSSL_AES_192
  16384. ExpectIntEQ(wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte)), 0);
  16385. #endif
  16386. #ifdef WOLFSSL_AES_256
  16387. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  16388. #endif
  16389. /* Pass in bad args. */
  16390. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte)),
  16391. BAD_FUNC_ARG);
  16392. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte)),
  16393. BAD_FUNC_ARG);
  16394. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte)),
  16395. BAD_FUNC_ARG);
  16396. wc_AesFree(&aes);
  16397. #endif
  16398. return EXPECT_RESULT();
  16399. } /* END test_wc_AesGcmSetKey */
  16400. /*
  16401. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  16402. */
  16403. static int test_wc_AesGcmEncryptDecrypt(void)
  16404. {
  16405. EXPECT_DECLS;
  16406. /* WOLFSSL_AFALG requires 12 byte IV */
  16407. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  16408. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  16409. Aes aes;
  16410. byte key32[] = {
  16411. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16412. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16413. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16414. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16415. };
  16416. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  16417. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16418. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16419. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16420. };
  16421. const byte a[] = {
  16422. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16423. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16424. 0xab, 0xad, 0xda, 0xd2
  16425. };
  16426. byte iv[] = "1234567890a";
  16427. byte longIV[] = "1234567890abcdefghij";
  16428. byte enc[sizeof(vector)];
  16429. byte resultT[AES_BLOCK_SIZE];
  16430. byte dec[sizeof(vector)];
  16431. /* Init stack variables. */
  16432. XMEMSET(&aes, 0, sizeof(Aes));
  16433. XMEMSET(enc, 0, sizeof(vector));
  16434. XMEMSET(dec, 0, sizeof(vector));
  16435. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  16436. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16437. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  16438. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16439. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  16440. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector), iv,
  16441. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  16442. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16443. /* Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  16444. ExpectIntEQ(wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector), iv,
  16445. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16446. BAD_FUNC_ARG);
  16447. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16448. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16449. BAD_FUNC_ARG);
  16450. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16451. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) - 5, a, sizeof(a)),
  16452. BAD_FUNC_ARG);
  16453. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16454. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  16455. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16456. /* FIPS does not check the lower bound of ivSz */
  16457. #else
  16458. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv, 0,
  16459. resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  16460. #endif
  16461. /* This case is now considered good. Long IVs are now allowed.
  16462. * Except for the original FIPS release, it still has an upper
  16463. * bound on the IV length. */
  16464. #if (!defined(HAVE_FIPS) || \
  16465. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16466. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16467. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  16468. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16469. 0);
  16470. #else
  16471. (void)longIV;
  16472. #endif /* Old FIPS */
  16473. /* END wc_AesGcmEncrypt */
  16474. #ifdef HAVE_AES_DECRYPT
  16475. ExpectIntEQ(wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16476. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16477. BAD_FUNC_ARG);
  16478. ExpectIntEQ(wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte), iv,
  16479. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16480. BAD_FUNC_ARG);
  16481. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte), iv,
  16482. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16483. BAD_FUNC_ARG);
  16484. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), NULL,
  16485. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16486. BAD_FUNC_ARG);
  16487. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16488. sizeof(iv)/sizeof(byte), NULL, sizeof(resultT), a, sizeof(a)),
  16489. BAD_FUNC_ARG);
  16490. #if (defined(HAVE_FIPS) && FIPS_VERSION_LE(2,0) && defined(WOLFSSL_ARMASM))
  16491. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16492. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16493. AES_GCM_AUTH_E);
  16494. #else
  16495. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16496. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16497. BAD_FUNC_ARG);
  16498. #endif
  16499. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16500. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  16501. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16502. /* FIPS does not check the lower bound of ivSz */
  16503. #else
  16504. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  16505. iv, 0, resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  16506. #endif
  16507. #endif /* HAVE_AES_DECRYPT */
  16508. wc_AesFree(&aes);
  16509. #endif
  16510. return EXPECT_RESULT();
  16511. } /* END test_wc_AesGcmEncryptDecrypt */
  16512. /*
  16513. * test function for mixed (one-shot encrpytion + stream decryption) AES GCM
  16514. * using a long IV (older FIPS does NOT support long IVs). Relates to zd15423
  16515. */
  16516. static int test_wc_AesGcmMixedEncDecLongIV(void)
  16517. {
  16518. EXPECT_DECLS;
  16519. #if (!defined(HAVE_FIPS) || \
  16520. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16521. !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AESGCM_STREAM)
  16522. const byte key[] = {
  16523. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16524. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16525. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16526. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16527. };
  16528. const byte in[] = {
  16529. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16530. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16531. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16532. };
  16533. const byte aad[] = {
  16534. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16535. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16536. 0xab, 0xad, 0xda, 0xd2
  16537. };
  16538. Aes aesEnc;
  16539. Aes aesDec;
  16540. byte iv[] = "1234567890abcdefghij";
  16541. byte out[sizeof(in)];
  16542. byte plain[sizeof(in)];
  16543. byte tag[AES_BLOCK_SIZE];
  16544. XMEMSET(&aesEnc, 0, sizeof(Aes));
  16545. XMEMSET(&aesDec, 0, sizeof(Aes));
  16546. XMEMSET(out, 0, sizeof(out));
  16547. XMEMSET(plain, 0, sizeof(plain));
  16548. XMEMSET(tag, 0, sizeof(tag));
  16549. /* Perform one-shot encryption using long IV */
  16550. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  16551. ExpectIntEQ(wc_AesGcmSetKey(&aesEnc, key, sizeof(key)), 0);
  16552. ExpectIntEQ(wc_AesGcmEncrypt(&aesEnc, out, in, sizeof(in), iv, sizeof(iv),
  16553. tag, sizeof(tag), aad, sizeof(aad)), 0);
  16554. /* Perform streaming decryption using long IV */
  16555. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  16556. ExpectIntEQ(wc_AesGcmInit(&aesDec, key, sizeof(key), iv, sizeof(iv)), 0);
  16557. ExpectIntEQ(wc_AesGcmDecryptUpdate(&aesDec, plain, out, sizeof(out), aad,
  16558. sizeof(aad)), 0);
  16559. ExpectIntEQ(wc_AesGcmDecryptFinal(&aesDec, tag, sizeof(tag)), 0);
  16560. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  16561. /* Free resources */
  16562. wc_AesFree(&aesEnc);
  16563. wc_AesFree(&aesDec);
  16564. #endif
  16565. return EXPECT_RESULT();
  16566. } /* END wc_AesGcmMixedEncDecLongIV */
  16567. /*
  16568. * unit test for wc_GmacSetKey()
  16569. */
  16570. static int test_wc_GmacSetKey(void)
  16571. {
  16572. EXPECT_DECLS;
  16573. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16574. Gmac gmac;
  16575. byte key16[] = {
  16576. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16577. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16578. };
  16579. #ifdef WOLFSSL_AES_192
  16580. byte key24[] = {
  16581. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16582. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16583. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16584. };
  16585. #endif
  16586. #ifdef WOLFSSL_AES_256
  16587. byte key32[] = {
  16588. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16589. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16590. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16591. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16592. };
  16593. #endif
  16594. byte badKey16[] = {
  16595. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16596. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  16597. };
  16598. byte badKey24[] = {
  16599. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  16600. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16601. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16602. };
  16603. byte badKey32[] = {
  16604. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16605. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  16606. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16607. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16608. };
  16609. XMEMSET(&gmac, 0, sizeof(Gmac));
  16610. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16611. #ifdef WOLFSSL_AES_128
  16612. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte)), 0);
  16613. #endif
  16614. #ifdef WOLFSSL_AES_192
  16615. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  16616. #endif
  16617. #ifdef WOLFSSL_AES_256
  16618. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  16619. #endif
  16620. /* Pass in bad args. */
  16621. ExpectIntEQ(wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte)),
  16622. BAD_FUNC_ARG);
  16623. ExpectIntEQ(wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte)),
  16624. BAD_FUNC_ARG);
  16625. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte)),
  16626. BAD_FUNC_ARG);
  16627. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte)),
  16628. BAD_FUNC_ARG);
  16629. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte)),
  16630. BAD_FUNC_ARG);
  16631. wc_AesFree(&gmac.aes);
  16632. #endif
  16633. return EXPECT_RESULT();
  16634. } /* END test_wc_GmacSetKey */
  16635. /*
  16636. * unit test for wc_GmacUpdate
  16637. */
  16638. static int test_wc_GmacUpdate(void)
  16639. {
  16640. EXPECT_DECLS;
  16641. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16642. Gmac gmac;
  16643. #ifdef WOLFSSL_AES_128
  16644. const byte key16[] = {
  16645. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  16646. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  16647. };
  16648. #endif
  16649. #ifdef WOLFSSL_AES_192
  16650. byte key24[] = {
  16651. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  16652. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  16653. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  16654. };
  16655. #endif
  16656. #ifdef WOLFSSL_AES_256
  16657. byte key32[] = {
  16658. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  16659. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  16660. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  16661. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  16662. };
  16663. #endif
  16664. #ifdef WOLFSSL_AES_128
  16665. const byte authIn[] = {
  16666. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  16667. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  16668. };
  16669. #endif
  16670. #ifdef WOLFSSL_AES_192
  16671. const byte authIn2[] = {
  16672. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  16673. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  16674. };
  16675. #endif
  16676. const byte authIn3[] = {
  16677. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  16678. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  16679. };
  16680. #ifdef WOLFSSL_AES_128
  16681. const byte tag1[] = { /* Known. */
  16682. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  16683. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  16684. };
  16685. #endif
  16686. #ifdef WOLFSSL_AES_192
  16687. const byte tag2[] = { /* Known */
  16688. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  16689. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  16690. };
  16691. #endif
  16692. const byte tag3[] = { /* Known */
  16693. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  16694. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  16695. };
  16696. #ifdef WOLFSSL_AES_128
  16697. const byte iv[] = {
  16698. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  16699. 0xe2, 0x8c, 0x8f, 0x16
  16700. };
  16701. #endif
  16702. #ifdef WOLFSSL_AES_192
  16703. const byte iv2[] = {
  16704. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  16705. 0x7e, 0x1a, 0x6f, 0xbc
  16706. };
  16707. #endif
  16708. const byte iv3[] = {
  16709. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  16710. 0xc3, 0xfb, 0x6c, 0x8a
  16711. };
  16712. byte tagOut[16];
  16713. byte tagOut2[24];
  16714. byte tagOut3[32];
  16715. /* Init stack variables. */
  16716. XMEMSET(&gmac, 0, sizeof(Gmac));
  16717. XMEMSET(tagOut, 0, sizeof(tagOut));
  16718. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  16719. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  16720. #ifdef WOLFSSL_AES_128
  16721. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16722. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)), 0);
  16723. ExpectIntEQ(wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  16724. tagOut, sizeof(tag1)), 0);
  16725. ExpectIntEQ(XMEMCMP(tag1, tagOut, sizeof(tag1)), 0);
  16726. wc_AesFree(&gmac.aes);
  16727. #endif
  16728. #ifdef WOLFSSL_AES_192
  16729. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  16730. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  16731. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  16732. ExpectIntEQ(wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2, sizeof(authIn2),
  16733. tagOut2, sizeof(tag2)), 0);
  16734. ExpectIntEQ(XMEMCMP(tagOut2, tag2, sizeof(tag2)), 0);
  16735. wc_AesFree(&gmac.aes);
  16736. #endif
  16737. #ifdef WOLFSSL_AES_256
  16738. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  16739. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  16740. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  16741. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16742. tagOut3, sizeof(tag3)), 0);
  16743. ExpectIntEQ(XMEMCMP(tag3, tagOut3, sizeof(tag3)), 0);
  16744. wc_AesFree(&gmac.aes);
  16745. #endif
  16746. /* Pass bad args. */
  16747. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16748. ExpectIntEQ(wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16749. tagOut3, sizeof(tag3)), BAD_FUNC_ARG);
  16750. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16751. tagOut3, sizeof(tag3) - 5), BAD_FUNC_ARG);
  16752. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16753. tagOut3, sizeof(tag3) + 1), BAD_FUNC_ARG);
  16754. wc_AesFree(&gmac.aes);
  16755. #endif
  16756. return EXPECT_RESULT();
  16757. } /* END test_wc_GmacUpdate */
  16758. /*
  16759. * testing wc_CamelliaSetKey
  16760. */
  16761. static int test_wc_CamelliaSetKey(void)
  16762. {
  16763. EXPECT_DECLS;
  16764. #ifdef HAVE_CAMELLIA
  16765. Camellia camellia;
  16766. /*128-bit key*/
  16767. static const byte key16[] = {
  16768. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16769. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  16770. };
  16771. /* 192-bit key */
  16772. static const byte key24[] = {
  16773. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16774. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16775. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16776. };
  16777. /* 256-bit key */
  16778. static const byte key32[] = {
  16779. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16780. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16781. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  16782. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  16783. };
  16784. static const byte iv[] = {
  16785. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16786. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16787. };
  16788. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv),
  16789. 0);
  16790. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16),
  16791. NULL), 0);
  16792. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  16793. 0);
  16794. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16795. NULL), 0);
  16796. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32), iv),
  16797. 0);
  16798. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32),
  16799. NULL), 0);
  16800. /* Bad args. */
  16801. ExpectIntEQ(wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv),
  16802. BAD_FUNC_ARG);
  16803. #endif
  16804. return EXPECT_RESULT();
  16805. } /* END test_wc_CammeliaSetKey */
  16806. /*
  16807. * Testing wc_CamelliaSetIV()
  16808. */
  16809. static int test_wc_CamelliaSetIV(void)
  16810. {
  16811. EXPECT_DECLS;
  16812. #ifdef HAVE_CAMELLIA
  16813. Camellia camellia;
  16814. static const byte iv[] = {
  16815. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16816. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16817. };
  16818. ExpectIntEQ(wc_CamelliaSetIV(&camellia, iv), 0);
  16819. ExpectIntEQ(wc_CamelliaSetIV(&camellia, NULL), 0);
  16820. /* Bad args. */
  16821. ExpectIntEQ(wc_CamelliaSetIV(NULL, NULL), BAD_FUNC_ARG);
  16822. ExpectIntEQ(wc_CamelliaSetIV(NULL, iv), BAD_FUNC_ARG);
  16823. #endif
  16824. return EXPECT_RESULT();
  16825. } /* END test_wc_CamelliaSetIV*/
  16826. /*
  16827. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  16828. */
  16829. static int test_wc_CamelliaEncryptDecryptDirect(void)
  16830. {
  16831. EXPECT_DECLS;
  16832. #ifdef HAVE_CAMELLIA
  16833. Camellia camellia;
  16834. static const byte key24[] = {
  16835. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16836. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16837. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16838. };
  16839. static const byte iv[] = {
  16840. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16841. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16842. };
  16843. static const byte plainT[] = {
  16844. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16845. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16846. };
  16847. byte enc[sizeof(plainT)];
  16848. byte dec[sizeof(enc)];
  16849. /* Init stack variables.*/
  16850. XMEMSET(enc, 0, 16);
  16851. XMEMSET(enc, 0, 16);
  16852. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  16853. 0);
  16854. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, plainT), 0);
  16855. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, enc), 0);
  16856. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  16857. /* Pass bad args. */
  16858. ExpectIntEQ(wc_CamelliaEncryptDirect(NULL, enc, plainT), BAD_FUNC_ARG);
  16859. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, NULL, plainT),
  16860. BAD_FUNC_ARG);
  16861. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, NULL), BAD_FUNC_ARG);
  16862. ExpectIntEQ(wc_CamelliaDecryptDirect(NULL, dec, enc), BAD_FUNC_ARG);
  16863. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, NULL, enc), BAD_FUNC_ARG);
  16864. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, NULL), BAD_FUNC_ARG);
  16865. #endif
  16866. return EXPECT_RESULT();
  16867. } /* END test-wc_CamelliaEncryptDecryptDirect */
  16868. /*
  16869. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  16870. */
  16871. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  16872. {
  16873. EXPECT_DECLS;
  16874. #ifdef HAVE_CAMELLIA
  16875. Camellia camellia;
  16876. static const byte key24[] = {
  16877. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16878. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16879. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16880. };
  16881. static const byte plainT[] = {
  16882. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16883. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16884. };
  16885. byte enc[CAMELLIA_BLOCK_SIZE];
  16886. byte dec[CAMELLIA_BLOCK_SIZE];
  16887. /* Init stack variables. */
  16888. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16889. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16890. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16891. NULL), 0);
  16892. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, plainT,
  16893. CAMELLIA_BLOCK_SIZE), 0);
  16894. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16895. NULL), 0);
  16896. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE),
  16897. 0);
  16898. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  16899. /* Pass in bad args. */
  16900. ExpectIntEQ(wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE),
  16901. BAD_FUNC_ARG);
  16902. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  16903. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16904. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  16905. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16906. ExpectIntEQ(wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE),
  16907. BAD_FUNC_ARG);
  16908. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  16909. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16910. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  16911. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16912. #endif
  16913. return EXPECT_RESULT();
  16914. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  16915. /*
  16916. * Testing wc_Arc4SetKey()
  16917. */
  16918. static int test_wc_Arc4SetKey(void)
  16919. {
  16920. EXPECT_DECLS;
  16921. #ifndef NO_RC4
  16922. Arc4 arc;
  16923. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16924. int keyLen = 8;
  16925. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, keyLen), 0);
  16926. /* Test bad args. */
  16927. ExpectIntEQ(wc_Arc4SetKey(NULL, (byte*)key, keyLen), BAD_FUNC_ARG);
  16928. ExpectIntEQ(wc_Arc4SetKey(&arc, NULL , keyLen), BAD_FUNC_ARG);
  16929. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, 0 ), BAD_FUNC_ARG);
  16930. #endif
  16931. return EXPECT_RESULT();
  16932. } /* END test_wc_Arc4SetKey */
  16933. /*
  16934. * Testing wc_Arc4Process for ENC/DEC.
  16935. */
  16936. static int test_wc_Arc4Process(void)
  16937. {
  16938. EXPECT_DECLS;
  16939. #ifndef NO_RC4
  16940. Arc4 enc;
  16941. Arc4 dec;
  16942. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16943. int keyLen = 8;
  16944. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16945. byte cipher[8];
  16946. byte plain[8];
  16947. /* Init stack variables */
  16948. XMEMSET(&enc, 0, sizeof(Arc4));
  16949. XMEMSET(&dec, 0, sizeof(Arc4));
  16950. XMEMSET(cipher, 0, sizeof(cipher));
  16951. XMEMSET(plain, 0, sizeof(plain));
  16952. /* Use for async. */
  16953. ExpectIntEQ(wc_Arc4Init(&enc, NULL, INVALID_DEVID), 0);
  16954. ExpectIntEQ(wc_Arc4Init(&dec, NULL, INVALID_DEVID), 0);
  16955. ExpectIntEQ(wc_Arc4SetKey(&enc, (byte*)key, keyLen), 0);
  16956. ExpectIntEQ(wc_Arc4SetKey(&dec, (byte*)key, keyLen), 0);
  16957. ExpectIntEQ(wc_Arc4Process(&enc, cipher, (byte*)input, keyLen), 0);
  16958. ExpectIntEQ(wc_Arc4Process(&dec, plain, cipher, keyLen), 0);
  16959. ExpectIntEQ(XMEMCMP(plain, input, keyLen), 0);
  16960. /* Bad args. */
  16961. ExpectIntEQ(wc_Arc4Process(NULL, plain, cipher, keyLen), BAD_FUNC_ARG);
  16962. ExpectIntEQ(wc_Arc4Process(&dec, NULL, cipher, keyLen), BAD_FUNC_ARG);
  16963. ExpectIntEQ(wc_Arc4Process(&dec, plain, NULL, keyLen), BAD_FUNC_ARG);
  16964. wc_Arc4Free(&enc);
  16965. wc_Arc4Free(&dec);
  16966. #endif
  16967. return EXPECT_RESULT();
  16968. } /* END test_wc_Arc4Process */
  16969. /*
  16970. * Testing wc_Init RsaKey()
  16971. */
  16972. static int test_wc_InitRsaKey(void)
  16973. {
  16974. EXPECT_DECLS;
  16975. #ifndef NO_RSA
  16976. RsaKey key;
  16977. XMEMSET(&key, 0, sizeof(RsaKey));
  16978. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16979. /* Test bad args. */
  16980. ExpectIntEQ(wc_InitRsaKey(NULL, HEAP_HINT), BAD_FUNC_ARG);
  16981. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16982. #endif
  16983. return EXPECT_RESULT();
  16984. } /* END test_wc_InitRsaKey */
  16985. /*
  16986. * Testing wc_RsaPrivateKeyDecode()
  16987. */
  16988. static int test_wc_RsaPrivateKeyDecode(void)
  16989. {
  16990. EXPECT_DECLS;
  16991. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  16992. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16993. RsaKey key;
  16994. byte* tmp = NULL;
  16995. word32 idx = 0;
  16996. int bytes = 0;
  16997. XMEMSET(&key, 0, sizeof(RsaKey));
  16998. ExpectNotNull(tmp = (byte*)XMALLOC(FOURK_BUF, NULL,
  16999. DYNAMIC_TYPE_TMP_BUFFER));
  17000. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17001. if (tmp != NULL) {
  17002. #ifdef USE_CERT_BUFFERS_1024
  17003. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  17004. bytes = sizeof_client_key_der_1024;
  17005. #else
  17006. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  17007. bytes = sizeof_client_key_der_2048;
  17008. #endif /* Use cert buffers. */
  17009. }
  17010. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes), 0);
  17011. /* Test bad args. */
  17012. ExpectIntEQ(wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes),
  17013. BAD_FUNC_ARG);
  17014. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes),
  17015. BAD_FUNC_ARG);
  17016. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes),
  17017. BAD_FUNC_ARG);
  17018. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17019. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17020. #endif
  17021. return EXPECT_RESULT();
  17022. } /* END test_wc_RsaPrivateKeyDecode */
  17023. /*
  17024. * Testing wc_RsaPublicKeyDecode()
  17025. */
  17026. static int test_wc_RsaPublicKeyDecode(void)
  17027. {
  17028. EXPECT_DECLS;
  17029. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  17030. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  17031. RsaKey keyPub;
  17032. byte* tmp = NULL;
  17033. word32 idx = 0;
  17034. int bytes = 0;
  17035. word32 keySz = 0;
  17036. word32 tstKeySz = 0;
  17037. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  17038. XFILE f = XBADFILE;
  17039. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  17040. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  17041. byte buf[4096];
  17042. #endif
  17043. XMEMSET(&keyPub, 0, sizeof(RsaKey));
  17044. ExpectNotNull(tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17045. ExpectIntEQ(wc_InitRsaKey(&keyPub, HEAP_HINT), 0);
  17046. if (tmp != NULL) {
  17047. #ifdef USE_CERT_BUFFERS_1024
  17048. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  17049. bytes = sizeof_client_keypub_der_1024;
  17050. keySz = 1024;
  17051. #else
  17052. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  17053. bytes = sizeof_client_keypub_der_2048;
  17054. keySz = 2048;
  17055. #endif
  17056. }
  17057. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes), 0);
  17058. /* Pass in bad args. */
  17059. ExpectIntEQ(wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes),
  17060. BAD_FUNC_ARG);
  17061. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes),
  17062. BAD_FUNC_ARG);
  17063. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes),
  17064. BAD_FUNC_ARG);
  17065. DoExpectIntEQ(wc_FreeRsaKey(&keyPub), 0);
  17066. /* Test for getting modulus key size */
  17067. idx = 0;
  17068. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  17069. &tstKeySz, NULL, NULL), 0);
  17070. ExpectIntEQ(tstKeySz, keySz/8);
  17071. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  17072. ExpectTrue((f = XFOPEN(rsaPssPubKey, "rb")) != XBADFILE);
  17073. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  17074. if (f != XBADFILE) {
  17075. XFCLOSE(f);
  17076. f = XBADFILE;
  17077. }
  17078. idx = 0;
  17079. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  17080. NULL), 0);
  17081. ExpectTrue((f = XFOPEN(rsaPssPubKeyNoParams, "rb")) != XBADFILE);
  17082. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  17083. if (f != XBADFILE)
  17084. XFCLOSE(f);
  17085. idx = 0;
  17086. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  17087. NULL), 0);
  17088. #endif
  17089. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17090. #endif
  17091. return EXPECT_RESULT();
  17092. } /* END test_wc_RsaPublicKeyDecode */
  17093. /*
  17094. * Testing wc_RsaPublicKeyDecodeRaw()
  17095. */
  17096. static int test_wc_RsaPublicKeyDecodeRaw(void)
  17097. {
  17098. EXPECT_DECLS;
  17099. #if !defined(NO_RSA)
  17100. RsaKey key;
  17101. const byte n = 0x23;
  17102. const byte e = 0x03;
  17103. int nSz = sizeof(n);
  17104. int eSz = sizeof(e);
  17105. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17106. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key), 0);
  17107. /* Pass in bad args. */
  17108. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key),
  17109. BAD_FUNC_ARG);
  17110. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key),
  17111. BAD_FUNC_ARG);
  17112. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL),
  17113. BAD_FUNC_ARG);
  17114. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17115. #endif
  17116. return EXPECT_RESULT();
  17117. } /* END test_wc_RsaPublicKeyDecodeRaw */
  17118. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17119. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  17120. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  17121. * trying until it gets a probable prime. */
  17122. #ifdef HAVE_FIPS
  17123. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  17124. {
  17125. int ret;
  17126. for (;;) {
  17127. ret = wc_MakeRsaKey(key, size, e, rng);
  17128. if (ret != PRIME_GEN_E) break;
  17129. fprintf(stderr, "MakeRsaKey couldn't find prime; "
  17130. "trying again.\n");
  17131. }
  17132. return ret;
  17133. }
  17134. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  17135. #else
  17136. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  17137. #endif
  17138. #endif
  17139. /*
  17140. * Testing wc_MakeRsaKey()
  17141. */
  17142. static int test_wc_MakeRsaKey(void)
  17143. {
  17144. EXPECT_DECLS;
  17145. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17146. RsaKey genKey;
  17147. WC_RNG rng;
  17148. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17149. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17150. int bits = 1024;
  17151. #else
  17152. int bits = 2048;
  17153. #endif
  17154. XMEMSET(&genKey, 0, sizeof(RsaKey));
  17155. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17156. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  17157. ExpectIntEQ(wc_InitRng(&rng), 0);
  17158. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  17159. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  17160. /* Test bad args. */
  17161. ExpectIntEQ(MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng), BAD_FUNC_ARG);
  17162. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL),
  17163. BAD_FUNC_ARG);
  17164. /* e < 3 */
  17165. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 2, &rng), BAD_FUNC_ARG);
  17166. /* e & 1 == 0 */
  17167. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 6, &rng), BAD_FUNC_ARG);
  17168. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17169. #endif
  17170. return EXPECT_RESULT();
  17171. } /* END test_wc_MakeRsaKey */
  17172. /*
  17173. * Test the bounds checking on the cipher text versus the key modulus.
  17174. * 1. Make a new RSA key.
  17175. * 2. Set c to 1.
  17176. * 3. Decrypt c into k. (error)
  17177. * 4. Copy the key modulus to c and sub 1 from the copy.
  17178. * 5. Decrypt c into k. (error)
  17179. * Valid bounds test cases are covered by all the other RSA tests.
  17180. */
  17181. static int test_RsaDecryptBoundsCheck(void)
  17182. {
  17183. EXPECT_DECLS;
  17184. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  17185. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  17186. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  17187. WC_RNG rng;
  17188. RsaKey key;
  17189. byte flatC[256];
  17190. word32 flatCSz;
  17191. byte out[256];
  17192. word32 outSz = sizeof(out);
  17193. XMEMSET(&key, 0, sizeof(RsaKey));
  17194. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17195. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17196. ExpectIntEQ(wc_InitRng(&rng), 0);
  17197. if (EXPECT_SUCCESS()) {
  17198. const byte* derKey;
  17199. word32 derKeySz;
  17200. word32 idx = 0;
  17201. #ifdef USE_CERT_BUFFERS_1024
  17202. derKey = server_key_der_1024;
  17203. derKeySz = (word32)sizeof_server_key_der_1024;
  17204. flatCSz = 128;
  17205. #else
  17206. derKey = server_key_der_2048;
  17207. derKeySz = (word32)sizeof_server_key_der_2048;
  17208. flatCSz = 256;
  17209. #endif
  17210. ExpectIntEQ(wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz), 0);
  17211. }
  17212. if (EXPECT_SUCCESS()) {
  17213. XMEMSET(flatC, 0, flatCSz);
  17214. flatC[flatCSz-1] = 1;
  17215. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17216. RSA_PRIVATE_DECRYPT, &rng), RSA_OUT_OF_RANGE_E);
  17217. if (EXPECT_SUCCESS()) {
  17218. mp_int c;
  17219. ExpectIntEQ(mp_init_copy(&c, &key.n), 0);
  17220. ExpectIntEQ(mp_sub_d(&c, 1, &c), 0);
  17221. ExpectIntEQ(mp_to_unsigned_bin(&c, flatC), 0);
  17222. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17223. RSA_PRIVATE_DECRYPT, NULL), RSA_OUT_OF_RANGE_E);
  17224. mp_clear(&c);
  17225. }
  17226. }
  17227. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17228. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17229. #endif
  17230. return EXPECT_RESULT();
  17231. } /* END test_wc_RsaDecryptBoundsCheck */
  17232. /*
  17233. * Testing wc_SetKeyUsage()
  17234. */
  17235. static int test_wc_SetKeyUsage(void)
  17236. {
  17237. EXPECT_DECLS;
  17238. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  17239. Cert myCert;
  17240. ExpectIntEQ(wc_InitCert(&myCert), 0);
  17241. ExpectIntEQ(wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement"), 0);
  17242. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation"), 0);
  17243. ExpectIntEQ(wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly"), 0);
  17244. ExpectIntEQ(wc_SetKeyUsage(&myCert, "decipherOnly"), 0);
  17245. ExpectIntEQ(wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign"), 0);
  17246. /* Test bad args. */
  17247. ExpectIntEQ(wc_SetKeyUsage(NULL, "decipherOnly"), BAD_FUNC_ARG);
  17248. ExpectIntEQ(wc_SetKeyUsage(&myCert, NULL), BAD_FUNC_ARG);
  17249. ExpectIntEQ(wc_SetKeyUsage(&myCert, ""), KEYUSAGE_E);
  17250. ExpectIntEQ(wc_SetKeyUsage(&myCert, ","), KEYUSAGE_E);
  17251. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign"),
  17252. KEYUSAGE_E);
  17253. #endif
  17254. return EXPECT_RESULT();
  17255. } /* END test_wc_SetKeyUsage */
  17256. /*
  17257. * Testing wc_CheckProbablePrime()
  17258. */
  17259. static int test_wc_CheckProbablePrime(void)
  17260. {
  17261. EXPECT_DECLS;
  17262. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17263. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  17264. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  17265. RsaKey key;
  17266. WC_RNG rng;
  17267. byte e[3];
  17268. word32 eSz = (word32)sizeof(e);
  17269. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  17270. word32 nSz = (word32)sizeof(n);
  17271. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  17272. word32 dSz = (word32)sizeof(d);
  17273. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  17274. word32 pSz = (word32)sizeof(p);
  17275. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  17276. word32 qSz = (word32)sizeof(q);
  17277. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  17278. int* isPrime;
  17279. int test[5];
  17280. isPrime = test;
  17281. XMEMSET(&key, 0, sizeof(RsaKey));
  17282. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17283. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17284. ExpectIntEQ(wc_InitRng(&rng), 0);
  17285. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17286. ExpectIntEQ(wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS,
  17287. WC_RSA_EXPONENT, &rng), 0);
  17288. PRIVATE_KEY_UNLOCK();
  17289. ExpectIntEQ(wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q,
  17290. &qSz), 0);
  17291. PRIVATE_KEY_LOCK();
  17292. /* Bad cases */
  17293. ExpectIntEQ(wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz, nlen, isPrime),
  17294. BAD_FUNC_ARG);
  17295. ExpectIntEQ(wc_CheckProbablePrime(p, 0, q, qSz, e, eSz, nlen, isPrime),
  17296. BAD_FUNC_ARG);
  17297. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz, nlen, isPrime),
  17298. BAD_FUNC_ARG);
  17299. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, 0, e, eSz, nlen, isPrime),
  17300. BAD_FUNC_ARG);
  17301. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz, nlen, isPrime),
  17302. BAD_FUNC_ARG);
  17303. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, 0, nlen, isPrime),
  17304. BAD_FUNC_ARG);
  17305. ExpectIntEQ(wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0, nlen, isPrime),
  17306. BAD_FUNC_ARG);
  17307. /* Good case */
  17308. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz, nlen, isPrime),
  17309. 0);
  17310. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17311. wc_FreeRng(&rng);
  17312. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  17313. #endif
  17314. return EXPECT_RESULT();
  17315. } /* END test_wc_CheckProbablePrime */
  17316. /*
  17317. * Testing wc_RsaPSS_Verify()
  17318. */
  17319. static int test_wc_RsaPSS_Verify(void)
  17320. {
  17321. EXPECT_DECLS;
  17322. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17323. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17324. RsaKey key;
  17325. WC_RNG rng;
  17326. int sz = 256;
  17327. const char* szMessage = "This is the string to be signed";
  17328. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17329. unsigned char pDecrypted[2048/8];
  17330. byte* pt = pDecrypted;
  17331. word32 outLen = sizeof(pDecrypted);
  17332. XMEMSET(&key, 0, sizeof(RsaKey));
  17333. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17334. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17335. ExpectIntEQ(wc_InitRng(&rng), 0);
  17336. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17337. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17338. ExpectIntGT(sz = wc_RsaPSS_Sign((byte*)szMessage,
  17339. (word32)XSTRLEN(szMessage)+1, pSignature, sizeof(pSignature),
  17340. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17341. /* Bad cases */
  17342. ExpectIntEQ(wc_RsaPSS_Verify(NULL, sz, pt, outLen,
  17343. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17344. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  17345. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17346. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
  17347. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17348. ExpectIntEQ(wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  17349. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17350. /* Good case */
  17351. ExpectIntGT(wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
  17352. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17353. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17354. wc_FreeRng(&rng);
  17355. #endif
  17356. return EXPECT_RESULT();
  17357. } /* END test_wc_RsaPSS_Verify */
  17358. /*
  17359. * Testing wc_RsaPSS_VerifyCheck()
  17360. */
  17361. static int test_wc_RsaPSS_VerifyCheck(void)
  17362. {
  17363. EXPECT_DECLS;
  17364. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17365. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17366. RsaKey key;
  17367. WC_RNG rng;
  17368. int sz = 256; /* 2048/8 */
  17369. byte digest[32];
  17370. word32 digestSz = sizeof(digest);
  17371. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17372. word32 pSignatureSz = sizeof(pSignature);
  17373. unsigned char pDecrypted[2048/8];
  17374. byte* pt = pDecrypted;
  17375. word32 outLen = sizeof(pDecrypted);
  17376. XMEMSET(&key, 0, sizeof(RsaKey));
  17377. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17378. XMEMSET(digest, 0, sizeof(digest));
  17379. XMEMSET(pSignature, 0, sizeof(pSignature));
  17380. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17381. ExpectIntEQ(wc_InitRng(&rng), 0);
  17382. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17383. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17384. ExpectTrue((digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  17385. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz),
  17386. 0);
  17387. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  17388. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17389. /* Bad cases */
  17390. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen, digest,
  17391. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17392. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen, digest,
  17393. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17394. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen, digest,
  17395. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17396. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen, digest,
  17397. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17398. /* Good case */
  17399. ExpectIntGT(wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen, digest,
  17400. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17401. ExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17402. wc_FreeRng(&rng);
  17403. #endif
  17404. return EXPECT_RESULT();
  17405. } /* END test_wc_RsaPSS_VerifyCheck */
  17406. /*
  17407. * Testing wc_RsaPSS_VerifyCheckInline()
  17408. */
  17409. static int test_wc_RsaPSS_VerifyCheckInline(void)
  17410. {
  17411. EXPECT_DECLS;
  17412. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17413. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17414. RsaKey key;
  17415. WC_RNG rng;
  17416. int sz = 256;
  17417. byte digest[32];
  17418. word32 digestSz = sizeof(digest);
  17419. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17420. unsigned char pDecrypted[2048/8];
  17421. byte* pt = pDecrypted;
  17422. XMEMSET(&key, 0, sizeof(RsaKey));
  17423. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17424. XMEMSET(digest, 0, sizeof(digest));
  17425. XMEMSET(pSignature, 0, sizeof(pSignature));
  17426. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17427. ExpectIntEQ(wc_InitRng(&rng), 0);
  17428. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17429. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17430. ExpectTrue((digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  17431. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz),
  17432. 0);
  17433. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature,
  17434. sizeof(pSignature), WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17435. /* Bad Cases */
  17436. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt, digest,
  17437. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17438. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL, digest,
  17439. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17440. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt, digest,
  17441. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17442. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt, digest,
  17443. digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17444. /* Good case */
  17445. ExpectIntGT(wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt, digest,
  17446. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17447. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17448. wc_FreeRng(&rng);
  17449. #endif
  17450. return EXPECT_RESULT();
  17451. } /* END test_wc_RsaPSS_VerifyCheckInline */
  17452. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17453. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  17454. {
  17455. (void)flag;
  17456. (void)type;
  17457. (void)file;
  17458. (void)line;
  17459. }
  17460. #endif
  17461. /*
  17462. * Testing wc_LockMutex_ex
  17463. */
  17464. static int test_wc_LockMutex_ex(void)
  17465. {
  17466. EXPECT_DECLS;
  17467. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17468. int flag = CRYPTO_LOCK;
  17469. int type = 0;
  17470. const char* file = "./test-LockMutex_ex.txt";
  17471. int line = 0;
  17472. /* without SetMutexCb */
  17473. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), BAD_STATE_E);
  17474. /* with SetMutexCb */
  17475. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  17476. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), 0);
  17477. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  17478. #endif
  17479. return EXPECT_RESULT();
  17480. } /* End test_wc_LockMutex_ex*/
  17481. /*
  17482. * Testing wc_SetMutexCb
  17483. */
  17484. static int test_wc_SetMutexCb(void)
  17485. {
  17486. EXPECT_DECLS;
  17487. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17488. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  17489. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  17490. #endif
  17491. return EXPECT_RESULT();
  17492. } /* End test_wc_SetMutexCb*/
  17493. /*
  17494. * Testing wc_RsaKeyToDer()
  17495. */
  17496. static int test_wc_RsaKeyToDer(void)
  17497. {
  17498. EXPECT_DECLS;
  17499. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17500. RsaKey genKey;
  17501. WC_RNG rng;
  17502. byte* der = NULL;
  17503. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17504. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17505. int bits = 1024;
  17506. word32 derSz = 611;
  17507. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  17508. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  17509. #else
  17510. int bits = 2048;
  17511. word32 derSz = 1196;
  17512. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  17513. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  17514. #endif
  17515. XMEMSET(&rng, 0, sizeof(rng));
  17516. XMEMSET(&genKey, 0, sizeof(genKey));
  17517. ExpectNotNull(der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17518. /* Init structures. */
  17519. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  17520. ExpectIntEQ(wc_InitRng(&rng), 0);
  17521. /* Make key. */
  17522. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  17523. ExpectIntGT(wc_RsaKeyToDer(&genKey, der, derSz), 0);
  17524. /* Pass good/bad args. */
  17525. ExpectIntEQ(wc_RsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  17526. /* Get just the output length */
  17527. ExpectIntGT(wc_RsaKeyToDer(&genKey, NULL, 0), 0);
  17528. /* Try Public Key. */
  17529. genKey.type = 0;
  17530. ExpectIntEQ(wc_RsaKeyToDer(&genKey, der, FOURK_BUF), BAD_FUNC_ARG);
  17531. #ifdef WOLFSSL_CHECK_MEM_ZERO
  17532. /* Put back to Private Key */
  17533. genKey.type = 1;
  17534. #endif
  17535. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17536. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  17537. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17538. #endif
  17539. return EXPECT_RESULT();
  17540. } /* END test_wc_RsaKeyToDer */
  17541. /*
  17542. * Testing wc_RsaKeyToPublicDer()
  17543. */
  17544. static int test_wc_RsaKeyToPublicDer(void)
  17545. {
  17546. EXPECT_DECLS;
  17547. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17548. RsaKey key;
  17549. WC_RNG rng;
  17550. byte* der = NULL;
  17551. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17552. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17553. int bits = 1024;
  17554. word32 derLen = 162;
  17555. #else
  17556. int bits = 2048;
  17557. word32 derLen = 294;
  17558. #endif
  17559. int ret;
  17560. XMEMSET(&rng, 0, sizeof(rng));
  17561. XMEMSET(&key, 0, sizeof(key));
  17562. ExpectNotNull(der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17563. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17564. ExpectIntEQ(wc_InitRng(&rng), 0);
  17565. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17566. /* test getting size only */
  17567. ExpectIntGT(wc_RsaKeyToPublicDer(&key, NULL, derLen), 0);
  17568. ExpectIntGT(wc_RsaKeyToPublicDer(&key, der, derLen), 0);
  17569. /* test getting size only */
  17570. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0), 0);
  17571. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0), 0);
  17572. /* Pass in bad args. */
  17573. ExpectIntEQ(wc_RsaKeyToPublicDer(NULL, der, derLen), BAD_FUNC_ARG);
  17574. ExpectIntLT(ret = wc_RsaKeyToPublicDer(&key, der, -1), 0);
  17575. ExpectTrue((ret == BUFFER_E) || (ret == BAD_FUNC_ARG));
  17576. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17577. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17578. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17579. #endif
  17580. return EXPECT_RESULT();
  17581. } /* END test_wc_RsaKeyToPublicDer */
  17582. /*
  17583. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  17584. */
  17585. static int test_wc_RsaPublicEncryptDecrypt(void)
  17586. {
  17587. EXPECT_DECLS;
  17588. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17589. RsaKey key;
  17590. WC_RNG rng;
  17591. const char inStr[] = TEST_STRING;
  17592. const word32 plainLen = (word32)TEST_STRING_SZ;
  17593. const word32 inLen = (word32)TEST_STRING_SZ;
  17594. int bits = TEST_RSA_BITS;
  17595. const word32 cipherLen = TEST_RSA_BYTES;
  17596. word32 cipherLenResult = cipherLen;
  17597. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17598. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17599. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17600. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17601. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17602. WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17603. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17604. ExpectNotNull(in);
  17605. ExpectNotNull(plain);
  17606. ExpectNotNull(cipher);
  17607. #endif
  17608. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17609. /* Initialize stack structures. */
  17610. XMEMSET(&key, 0, sizeof(RsaKey));
  17611. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17612. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17613. ExpectIntEQ(wc_InitRng(&rng), 0);
  17614. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17615. /* Encrypt. */
  17616. ExpectIntGT(cipherLenResult = wc_RsaPublicEncrypt(in, inLen, cipher,
  17617. cipherLen, &key, &rng), 0);
  17618. /* Pass bad args - tested in another testing function.*/
  17619. /* Decrypt */
  17620. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17621. /* Bind rng */
  17622. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17623. #endif
  17624. ExpectIntGE(wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen,
  17625. &key), 0);
  17626. ExpectIntEQ(XMEMCMP(plain, inStr, plainLen), 0);
  17627. /* Pass bad args - tested in another testing function.*/
  17628. WC_FREE_VAR(in, NULL);
  17629. WC_FREE_VAR(plain, NULL);
  17630. WC_FREE_VAR(cipher, NULL);
  17631. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17632. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17633. #endif
  17634. return EXPECT_RESULT();
  17635. } /* END test_wc_RsaPublicEncryptDecrypt */
  17636. /*
  17637. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  17638. */
  17639. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  17640. {
  17641. EXPECT_DECLS;
  17642. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  17643. && !defined(WC_NO_RSA_OAEP) && !defined(NO_SHA256)
  17644. RsaKey key;
  17645. WC_RNG rng;
  17646. const char inStr[] = TEST_STRING;
  17647. const word32 inLen = (word32)TEST_STRING_SZ;
  17648. const word32 plainSz = (word32)TEST_STRING_SZ;
  17649. byte* res = NULL;
  17650. int idx = 0;
  17651. int bits = TEST_RSA_BITS;
  17652. const word32 cipherSz = TEST_RSA_BYTES;
  17653. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17654. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17655. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17656. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17657. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17658. WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17659. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17660. ExpectNotNull(in);
  17661. ExpectNotNull(plain);
  17662. ExpectNotNull(cipher);
  17663. #endif
  17664. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17665. /* Initialize stack structures. */
  17666. XMEMSET(&key, 0, sizeof(RsaKey));
  17667. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17668. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID), 0);
  17669. ExpectIntEQ(wc_InitRng(&rng), 0);
  17670. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17671. /* Encrypt */
  17672. ExpectIntGE(idx = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key,
  17673. &rng, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17674. /* Pass bad args - tested in another testing function.*/
  17675. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17676. /* Decrypt */
  17677. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17678. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17679. #endif
  17680. ExpectIntGE(wc_RsaPrivateDecrypt_ex(cipher, (word32)idx, plain, plainSz,
  17681. &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17682. ExpectIntEQ(XMEMCMP(plain, inStr, plainSz), 0);
  17683. /* Pass bad args - tested in another testing function.*/
  17684. ExpectIntGE(wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx, &res, &key,
  17685. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17686. ExpectIntEQ(XMEMCMP(inStr, res, plainSz), 0);
  17687. #endif
  17688. WC_FREE_VAR(in, NULL);
  17689. WC_FREE_VAR(plain, NULL);
  17690. WC_FREE_VAR(cipher, NULL);
  17691. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17692. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17693. #endif
  17694. return EXPECT_RESULT();
  17695. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  17696. /*
  17697. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  17698. */
  17699. static int test_wc_RsaSSL_SignVerify(void)
  17700. {
  17701. EXPECT_DECLS;
  17702. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17703. RsaKey key;
  17704. WC_RNG rng;
  17705. const char inStr[] = TEST_STRING;
  17706. const word32 plainSz = (word32)TEST_STRING_SZ;
  17707. const word32 inLen = (word32)TEST_STRING_SZ;
  17708. word32 idx = 0;
  17709. int bits = TEST_RSA_BITS;
  17710. const word32 outSz = TEST_RSA_BYTES;
  17711. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17712. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17713. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17714. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17715. WC_ALLOC_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17716. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17717. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17718. ExpectNotNull(in);
  17719. ExpectNotNull(out);
  17720. ExpectNotNull(plain);
  17721. #endif
  17722. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17723. XMEMSET(&key, 0, sizeof(RsaKey));
  17724. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17725. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17726. ExpectIntEQ(wc_InitRng(&rng), 0);
  17727. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17728. /* Sign. */
  17729. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng), (int)outSz);
  17730. idx = (int)outSz;
  17731. /* Test bad args. */
  17732. ExpectIntEQ(wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng),
  17733. BAD_FUNC_ARG);
  17734. ExpectIntEQ(wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng),
  17735. BAD_FUNC_ARG);
  17736. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng),
  17737. BAD_FUNC_ARG);
  17738. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng),
  17739. BAD_FUNC_ARG);
  17740. /* Verify. */
  17741. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, &key), (int)inLen);
  17742. /* Pass bad args. */
  17743. ExpectIntEQ(wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key),
  17744. BAD_FUNC_ARG);
  17745. ExpectIntEQ(wc_RsaSSL_Verify(out, 0, plain, plainSz, &key),
  17746. BAD_FUNC_ARG);
  17747. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key),
  17748. BAD_FUNC_ARG);
  17749. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL),
  17750. BAD_FUNC_ARG);
  17751. WC_FREE_VAR(in, NULL);
  17752. WC_FREE_VAR(out, NULL);
  17753. WC_FREE_VAR(plain, NULL);
  17754. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17755. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17756. #endif
  17757. return EXPECT_RESULT();
  17758. } /* END test_wc_RsaSSL_SignVerify */
  17759. /*
  17760. * Testing wc_RsaEncryptSize()
  17761. */
  17762. static int test_wc_RsaEncryptSize(void)
  17763. {
  17764. EXPECT_DECLS;
  17765. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17766. RsaKey key;
  17767. WC_RNG rng;
  17768. XMEMSET(&key, 0, sizeof(RsaKey));
  17769. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17770. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17771. ExpectIntEQ(wc_InitRng(&rng), 0);
  17772. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17773. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17774. ExpectIntEQ(MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng), 0);
  17775. ExpectIntEQ(wc_RsaEncryptSize(&key), 128);
  17776. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17777. #endif
  17778. ExpectIntEQ(MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17779. ExpectIntEQ(wc_RsaEncryptSize(&key), 256);
  17780. /* Pass in bad arg. */
  17781. ExpectIntEQ(wc_RsaEncryptSize(NULL), BAD_FUNC_ARG);
  17782. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17783. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17784. #endif
  17785. return EXPECT_RESULT();
  17786. } /* END test_wc_RsaEncryptSize*/
  17787. /*
  17788. * Testing wc_RsaFlattenPublicKey()
  17789. */
  17790. static int test_wc_RsaFlattenPublicKey(void)
  17791. {
  17792. EXPECT_DECLS;
  17793. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17794. RsaKey key;
  17795. WC_RNG rng;
  17796. byte e[256];
  17797. byte n[256];
  17798. word32 eSz = sizeof(e);
  17799. word32 nSz = sizeof(n);
  17800. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17801. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17802. int bits = 1024;
  17803. #else
  17804. int bits = 2048;
  17805. #endif
  17806. XMEMSET(&key, 0, sizeof(RsaKey));
  17807. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17808. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17809. ExpectIntEQ(wc_InitRng(&rng), 0);
  17810. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17811. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz), 0);
  17812. /* Pass bad args. */
  17813. ExpectIntEQ(wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz),
  17814. BAD_FUNC_ARG);
  17815. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz),
  17816. BAD_FUNC_ARG);
  17817. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz),
  17818. BAD_FUNC_ARG);
  17819. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz),
  17820. BAD_FUNC_ARG);
  17821. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL),
  17822. BAD_FUNC_ARG);
  17823. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17824. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17825. #endif
  17826. return EXPECT_RESULT();
  17827. } /* END test_wc_RsaFlattenPublicKey */
  17828. /*
  17829. * unit test for wc_AesCcmSetKey
  17830. */
  17831. static int test_wc_AesCcmSetKey(void)
  17832. {
  17833. EXPECT_DECLS;
  17834. #ifdef HAVE_AESCCM
  17835. Aes aes;
  17836. const byte key16[] = {
  17837. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17838. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17839. };
  17840. const byte key24[] = {
  17841. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17842. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17843. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  17844. };
  17845. const byte key32[] = {
  17846. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17847. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17848. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17849. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  17850. };
  17851. XMEMSET(&aes, 0, sizeof(Aes));
  17852. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  17853. #ifdef WOLFSSL_AES_128
  17854. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  17855. #endif
  17856. #ifdef WOLFSSL_AES_192
  17857. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24)), 0);
  17858. #endif
  17859. #ifdef WOLFSSL_AES_256
  17860. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32)), 0);
  17861. #endif
  17862. /* Test bad args. */
  17863. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1), BAD_FUNC_ARG);
  17864. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1), BAD_FUNC_ARG);
  17865. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1), BAD_FUNC_ARG);
  17866. wc_AesFree(&aes);
  17867. #endif
  17868. return EXPECT_RESULT();
  17869. } /* END test_wc_AesCcmSetKey */
  17870. /*
  17871. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  17872. */
  17873. static int test_wc_AesCcmEncryptDecrypt(void)
  17874. {
  17875. EXPECT_DECLS;
  17876. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  17877. Aes aes;
  17878. const byte key16[] = {
  17879. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17880. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17881. };
  17882. /* plaintext */
  17883. const byte plainT[] = {
  17884. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  17885. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  17886. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  17887. };
  17888. /* nonce */
  17889. const byte iv[] = {
  17890. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  17891. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  17892. };
  17893. const byte c[] = { /* cipher text. */
  17894. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  17895. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  17896. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  17897. };
  17898. const byte t[] = { /* Auth tag */
  17899. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  17900. };
  17901. const byte authIn[] = {
  17902. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  17903. };
  17904. byte cipherOut[sizeof(plainT)];
  17905. byte authTag[sizeof(t)];
  17906. #ifdef HAVE_AES_DECRYPT
  17907. byte plainOut[sizeof(cipherOut)];
  17908. #endif
  17909. XMEMSET(&aes, 0, sizeof(Aes));
  17910. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  17911. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  17912. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17913. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)), 0);
  17914. ExpectIntEQ(XMEMCMP(cipherOut, c, sizeof(c)), 0);
  17915. ExpectIntEQ(XMEMCMP(t, authTag, sizeof(t)), 0);
  17916. #ifdef HAVE_AES_DECRYPT
  17917. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17918. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)), 0);
  17919. ExpectIntEQ(XMEMCMP(plainOut, plainT, sizeof(plainT)), 0);
  17920. #endif
  17921. /* Pass in bad args. Encrypt*/
  17922. ExpectIntEQ(wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  17923. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17924. BAD_FUNC_ARG);
  17925. ExpectIntEQ(wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  17926. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17927. BAD_FUNC_ARG);
  17928. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  17929. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17930. BAD_FUNC_ARG);
  17931. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17932. NULL, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17933. BAD_FUNC_ARG);
  17934. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17935. iv, sizeof(iv), NULL, sizeof(authTag), authIn , sizeof(authIn)),
  17936. BAD_FUNC_ARG);
  17937. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17938. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17939. BAD_FUNC_ARG);
  17940. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17941. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17942. BAD_FUNC_ARG);
  17943. #ifdef HAVE_AES_DECRYPT
  17944. /* Pass in bad args. Decrypt*/
  17945. ExpectIntEQ(wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  17946. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17947. BAD_FUNC_ARG);
  17948. ExpectIntEQ(wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  17949. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17950. BAD_FUNC_ARG);
  17951. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  17952. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17953. BAD_FUNC_ARG);
  17954. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17955. NULL, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17956. BAD_FUNC_ARG);
  17957. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17958. iv, sizeof(iv), NULL, sizeof(authTag), authIn, sizeof(authIn)),
  17959. BAD_FUNC_ARG);
  17960. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17961. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17962. BAD_FUNC_ARG);
  17963. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17964. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17965. BAD_FUNC_ARG);
  17966. #endif
  17967. wc_AesFree(&aes);
  17968. #endif /* HAVE_AESCCM */
  17969. return EXPECT_RESULT();
  17970. } /* END test_wc_AesCcmEncryptDecrypt */
  17971. #if defined(WOLFSSL_AES_EAX) && \
  17972. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  17973. /*
  17974. * Testing test_wc_AesEaxVectors()
  17975. */
  17976. static int test_wc_AesEaxVectors(void)
  17977. {
  17978. EXPECT_DECLS;
  17979. typedef struct {
  17980. byte key[AES_256_KEY_SIZE];
  17981. int key_length;
  17982. byte iv[AES_BLOCK_SIZE];
  17983. int iv_length;
  17984. byte aad[AES_BLOCK_SIZE * 2];
  17985. int aad_length;
  17986. byte msg[AES_BLOCK_SIZE * 5];
  17987. int msg_length;
  17988. byte ct[AES_BLOCK_SIZE * 5];
  17989. int ct_length;
  17990. byte tag[AES_BLOCK_SIZE];
  17991. int tag_length;
  17992. int valid;
  17993. } AadVector;
  17994. /* Test vectors obtained from Google wycheproof project
  17995. * https://github.com/google/wycheproof
  17996. * from testvectors/aes_eax_test.json
  17997. */
  17998. const AadVector vectors[] = {
  17999. {
  18000. /* key, key length */
  18001. {0x23, 0x39, 0x52, 0xde, 0xe4, 0xd5, 0xed, 0x5f,
  18002. 0x9b, 0x9c, 0x6d, 0x6f, 0xf8, 0x0f, 0xf4, 0x78}, 16,
  18003. /* iv, iv length */
  18004. {0x62, 0xec, 0x67, 0xf9, 0xc3, 0xa4, 0xa4, 0x07,
  18005. 0xfc, 0xb2, 0xa8, 0xc4, 0x90, 0x31, 0xa8, 0xb3}, 16,
  18006. /* aad, aad length */
  18007. {0x6b, 0xfb, 0x91, 0x4f, 0xd0, 0x7e, 0xae, 0x6b}, 8,
  18008. /* msg, msg length */
  18009. {0x00}, 0,
  18010. /* ct, ct length */
  18011. {0x00}, 0,
  18012. /* tag, tag length */
  18013. {0xe0, 0x37, 0x83, 0x0e, 0x83, 0x89, 0xf2, 0x7b,
  18014. 0x02, 0x5a, 0x2d, 0x65, 0x27, 0xe7, 0x9d, 0x01}, 16,
  18015. /* valid */
  18016. 1,
  18017. },
  18018. {
  18019. /* key, key length */
  18020. {0x91, 0x94, 0x5d, 0x3f, 0x4d, 0xcb, 0xee, 0x0b,
  18021. 0xf4, 0x5e, 0xf5, 0x22, 0x55, 0xf0, 0x95, 0xa4}, 16,
  18022. /* iv, iv length */
  18023. {0xbe, 0xca, 0xf0, 0x43, 0xb0, 0xa2, 0x3d, 0x84,
  18024. 0x31, 0x94, 0xba, 0x97, 0x2c, 0x66, 0xde, 0xbd}, 16,
  18025. /* aad, aad length */
  18026. {0xfa, 0x3b, 0xfd, 0x48, 0x06, 0xeb, 0x53, 0xfa}, 8,
  18027. /* msg, msg length */
  18028. {0xf7, 0xfb}, 2,
  18029. /* ct, ct length */
  18030. {0x19, 0xdd}, 2,
  18031. /* tag, tag length */
  18032. {0x5c, 0x4c, 0x93, 0x31, 0x04, 0x9d, 0x0b, 0xda,
  18033. 0xb0, 0x27, 0x74, 0x08, 0xf6, 0x79, 0x67, 0xe5}, 16,
  18034. /* valid */
  18035. 1,
  18036. },
  18037. {
  18038. /* key, key length */
  18039. {0x01, 0xf7, 0x4a, 0xd6, 0x40, 0x77, 0xf2, 0xe7,
  18040. 0x04, 0xc0, 0xf6, 0x0a, 0xda, 0x3d, 0xd5, 0x23}, 16,
  18041. /* iv, iv length */
  18042. {0x70, 0xc3, 0xdb, 0x4f, 0x0d, 0x26, 0x36, 0x84,
  18043. 0x00, 0xa1, 0x0e, 0xd0, 0x5d, 0x2b, 0xff, 0x5e}, 16,
  18044. /* aad, aad length */
  18045. {0x23, 0x4a, 0x34, 0x63, 0xc1, 0x26, 0x4a, 0xc6}, 8,
  18046. /* msg, msg length */
  18047. {0x1a, 0x47, 0xcb, 0x49, 0x33}, 5,
  18048. /* ct, ct length */
  18049. {0xd8, 0x51, 0xd5, 0xba, 0xe0}, 5,
  18050. /* tag, tag length */
  18051. {0x3a, 0x59, 0xf2, 0x38, 0xa2, 0x3e, 0x39, 0x19,
  18052. 0x9d, 0xc9, 0x26, 0x66, 0x26, 0xc4, 0x0f, 0x80}, 16,
  18053. /* valid */
  18054. 1,
  18055. },
  18056. {
  18057. /* key, key length */
  18058. {0xd0, 0x7c, 0xf6, 0xcb, 0xb7, 0xf3, 0x13, 0xbd,
  18059. 0xde, 0x66, 0xb7, 0x27, 0xaf, 0xd3, 0xc5, 0xe8}, 16,
  18060. /* iv, iv length */
  18061. {0x84, 0x08, 0xdf, 0xff, 0x3c, 0x1a, 0x2b, 0x12,
  18062. 0x92, 0xdc, 0x19, 0x9e, 0x46, 0xb7, 0xd6, 0x17}, 16,
  18063. /* aad, aad length */
  18064. {0x33, 0xcc, 0xe2, 0xea, 0xbf, 0xf5, 0xa7, 0x9d}, 8,
  18065. /* msg, msg length */
  18066. {0x48, 0x1c, 0x9e, 0x39, 0xb1}, 5,
  18067. /* ct, ct length */
  18068. {0x63, 0x2a, 0x9d, 0x13, 0x1a}, 5,
  18069. /* tag, tag length */
  18070. {0xd4, 0xc1, 0x68, 0xa4, 0x22, 0x5d, 0x8e, 0x1f,
  18071. 0xf7, 0x55, 0x93, 0x99, 0x74, 0xa7, 0xbe, 0xde}, 16,
  18072. /* valid */
  18073. 1,
  18074. },
  18075. {
  18076. /* key, key length */
  18077. {0x35, 0xb6, 0xd0, 0x58, 0x00, 0x05, 0xbb, 0xc1,
  18078. 0x2b, 0x05, 0x87, 0x12, 0x45, 0x57, 0xd2, 0xc2}, 16,
  18079. /* iv, iv length */
  18080. {0xfd, 0xb6, 0xb0, 0x66, 0x76, 0xee, 0xdc, 0x5c,
  18081. 0x61, 0xd7, 0x42, 0x76, 0xe1, 0xf8, 0xe8, 0x16}, 16,
  18082. /* aad, aad length */
  18083. {0xae, 0xb9, 0x6e, 0xae, 0xbe, 0x29, 0x70, 0xe9}, 8,
  18084. /* msg, msg length */
  18085. {0x40, 0xd0, 0xc0, 0x7d, 0xa5, 0xe4}, 6,
  18086. /* ct, ct length */
  18087. {0x07, 0x1d, 0xfe, 0x16, 0xc6, 0x75}, 6,
  18088. /* tag, tag length */
  18089. {0xcb, 0x06, 0x77, 0xe5, 0x36, 0xf7, 0x3a, 0xfe,
  18090. 0x6a, 0x14, 0xb7, 0x4e, 0xe4, 0x98, 0x44, 0xdd}, 16,
  18091. /* valid */
  18092. 1,
  18093. },
  18094. {
  18095. /* key, key length */
  18096. {0xbd, 0x8e, 0x6e, 0x11, 0x47, 0x5e, 0x60, 0xb2,
  18097. 0x68, 0x78, 0x4c, 0x38, 0xc6, 0x2f, 0xeb, 0x22}, 16,
  18098. /* iv, iv length */
  18099. {0x6e, 0xac, 0x5c, 0x93, 0x07, 0x2d, 0x8e, 0x85,
  18100. 0x13, 0xf7, 0x50, 0x93, 0x5e, 0x46, 0xda, 0x1b}, 16,
  18101. /* aad, aad length */
  18102. {0xd4, 0x48, 0x2d, 0x1c, 0xa7, 0x8d, 0xce, 0x0f}, 8,
  18103. /* msg, msg length */
  18104. {0x4d, 0xe3, 0xb3, 0x5c, 0x3f, 0xc0, 0x39, 0x24,
  18105. 0x5b, 0xd1, 0xfb, 0x7d}, 12,
  18106. /* ct, ct length */
  18107. {0x83, 0x5b, 0xb4, 0xf1, 0x5d, 0x74, 0x3e, 0x35,
  18108. 0x0e, 0x72, 0x84, 0x14}, 12,
  18109. /* tag, tag length */
  18110. {0xab, 0xb8, 0x64, 0x4f, 0xd6, 0xcc, 0xb8, 0x69,
  18111. 0x47, 0xc5, 0xe1, 0x05, 0x90, 0x21, 0x0a, 0x4f}, 16,
  18112. /* valid */
  18113. 1,
  18114. },
  18115. {
  18116. /* key, key length */
  18117. {0x7c, 0x77, 0xd6, 0xe8, 0x13, 0xbe, 0xd5, 0xac,
  18118. 0x98, 0xba, 0xa4, 0x17, 0x47, 0x7a, 0x2e, 0x7d}, 16,
  18119. /* iv, iv length */
  18120. {0x1a, 0x8c, 0x98, 0xdc, 0xd7, 0x3d, 0x38, 0x39,
  18121. 0x3b, 0x2b, 0xf1, 0x56, 0x9d, 0xee, 0xfc, 0x19}, 16,
  18122. /* aad, aad length */
  18123. {0x65, 0xd2, 0x01, 0x79, 0x90, 0xd6, 0x25, 0x28}, 8,
  18124. /* msg, msg length */
  18125. {0x8b, 0x0a, 0x79, 0x30, 0x6c, 0x9c, 0xe7, 0xed,
  18126. 0x99, 0xda, 0xe4, 0xf8, 0x7f, 0x8d, 0xd6, 0x16,
  18127. 0x36}, 17,
  18128. /* ct, ct length */
  18129. {0x02, 0x08, 0x3e, 0x39, 0x79, 0xda, 0x01, 0x48,
  18130. 0x12, 0xf5, 0x9f, 0x11, 0xd5, 0x26, 0x30, 0xda,
  18131. 0x30}, 17,
  18132. /* tag, tag length */
  18133. {0x13, 0x73, 0x27, 0xd1, 0x06, 0x49, 0xb0, 0xaa,
  18134. 0x6e, 0x1c, 0x18, 0x1d, 0xb6, 0x17, 0xd7, 0xf2}, 16,
  18135. /* valid */
  18136. 1,
  18137. },
  18138. {
  18139. /* key, key length */
  18140. {0x5f, 0xff, 0x20, 0xca, 0xfa, 0xb1, 0x19, 0xca,
  18141. 0x2f, 0xc7, 0x35, 0x49, 0xe2, 0x0f, 0x5b, 0x0d}, 16,
  18142. /* iv, iv length */
  18143. {0xdd, 0xe5, 0x9b, 0x97, 0xd7, 0x22, 0x15, 0x6d,
  18144. 0x4d, 0x9a, 0xff, 0x2b, 0xc7, 0x55, 0x98, 0x26}, 16,
  18145. /* aad, aad length */
  18146. {0x54, 0xb9, 0xf0, 0x4e, 0x6a, 0x09, 0x18, 0x9a}, 8,
  18147. /* msg, msg length */
  18148. {0x1b, 0xda, 0x12, 0x2b, 0xce, 0x8a, 0x8d, 0xba,
  18149. 0xf1, 0x87, 0x7d, 0x96, 0x2b, 0x85, 0x92, 0xdd,
  18150. 0x2d, 0x56}, 18,
  18151. /* ct, ct length */
  18152. {0x2e, 0xc4, 0x7b, 0x2c, 0x49, 0x54, 0xa4, 0x89,
  18153. 0xaf, 0xc7, 0xba, 0x48, 0x97, 0xed, 0xcd, 0xae,
  18154. 0x8c, 0xc3}, 18,
  18155. /* tag, tag length */
  18156. {0x3b, 0x60, 0x45, 0x05, 0x99, 0xbd, 0x02, 0xc9,
  18157. 0x63, 0x82, 0x90, 0x2a, 0xef, 0x7f, 0x83, 0x2a}, 16,
  18158. /* valid */
  18159. 1,
  18160. },
  18161. {
  18162. /* key, key length */
  18163. {0xa4, 0xa4, 0x78, 0x2b, 0xcf, 0xfd, 0x3e, 0xc5,
  18164. 0xe7, 0xef, 0x6d, 0x8c, 0x34, 0xa5, 0x61, 0x23}, 16,
  18165. /* iv, iv length */
  18166. {0xb7, 0x81, 0xfc, 0xf2, 0xf7, 0x5f, 0xa5, 0xa8,
  18167. 0xde, 0x97, 0xa9, 0xca, 0x48, 0xe5, 0x22, 0xec}, 16,
  18168. /* aad, aad length */
  18169. {0x89, 0x9a, 0x17, 0x58, 0x97, 0x56, 0x1d, 0x7e}, 8,
  18170. /* msg, msg length */
  18171. {0x6c, 0xf3, 0x67, 0x20, 0x87, 0x2b, 0x85, 0x13,
  18172. 0xf6, 0xea, 0xb1, 0xa8, 0xa4, 0x44, 0x38, 0xd5,
  18173. 0xef, 0x11}, 18,
  18174. /* ct, ct length */
  18175. {0x0d, 0xe1, 0x8f, 0xd0, 0xfd, 0xd9, 0x1e, 0x7a,
  18176. 0xf1, 0x9f, 0x1d, 0x8e, 0xe8, 0x73, 0x39, 0x38,
  18177. 0xb1, 0xe8}, 18,
  18178. /* tag, tag length */
  18179. {0xe7, 0xf6, 0xd2, 0x23, 0x16, 0x18, 0x10, 0x2f,
  18180. 0xdb, 0x7f, 0xe5, 0x5f, 0xf1, 0x99, 0x17, 0x00}, 16,
  18181. /* valid */
  18182. 1,
  18183. },
  18184. {
  18185. /* key, key length */
  18186. {0x83, 0x95, 0xfc, 0xf1, 0xe9, 0x5b, 0xeb, 0xd6,
  18187. 0x97, 0xbd, 0x01, 0x0b, 0xc7, 0x66, 0xaa, 0xc3}, 16,
  18188. /* iv, iv length */
  18189. {0x22, 0xe7, 0xad, 0xd9, 0x3c, 0xfc, 0x63, 0x93,
  18190. 0xc5, 0x7e, 0xc0, 0xb3, 0xc1, 0x7d, 0x6b, 0x44}, 16,
  18191. /* aad, aad length */
  18192. {0x12, 0x67, 0x35, 0xfc, 0xc3, 0x20, 0xd2, 0x5a}, 8,
  18193. /* msg, msg length */
  18194. {0xca, 0x40, 0xd7, 0x44, 0x6e, 0x54, 0x5f, 0xfa,
  18195. 0xed, 0x3b, 0xd1, 0x2a, 0x74, 0x0a, 0x65, 0x9f,
  18196. 0xfb, 0xbb, 0x3c, 0xea, 0xb7}, 21,
  18197. /* ct, ct length */
  18198. {0xcb, 0x89, 0x20, 0xf8, 0x7a, 0x6c, 0x75, 0xcf,
  18199. 0xf3, 0x96, 0x27, 0xb5, 0x6e, 0x3e, 0xd1, 0x97,
  18200. 0xc5, 0x52, 0xd2, 0x95, 0xa7}, 21,
  18201. /* tag, tag length */
  18202. {0xcf, 0xc4, 0x6a, 0xfc, 0x25, 0x3b, 0x46, 0x52,
  18203. 0xb1, 0xaf, 0x37, 0x95, 0xb1, 0x24, 0xab, 0x6e}, 16,
  18204. /* valid */
  18205. 1,
  18206. },
  18207. {
  18208. /* key, key length */
  18209. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18210. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18211. /* iv, iv length */
  18212. {0x3c, 0x8c, 0xc2, 0x97, 0x0a, 0x00, 0x8f, 0x75,
  18213. 0xcc, 0x5b, 0xea, 0xe2, 0x84, 0x72, 0x58, 0xc2}, 16,
  18214. /* aad, aad length */
  18215. {0x00}, 0,
  18216. /* msg, msg length */
  18217. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18218. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18219. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18220. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18221. /* ct, ct length */
  18222. {0x3c, 0x44, 0x1f, 0x32, 0xce, 0x07, 0x82, 0x23,
  18223. 0x64, 0xd7, 0xa2, 0x99, 0x0e, 0x50, 0xbb, 0x13,
  18224. 0xd7, 0xb0, 0x2a, 0x26, 0x96, 0x9e, 0x4a, 0x93,
  18225. 0x7e, 0x5e, 0x90, 0x73, 0xb0, 0xd9, 0xc9, 0x68}, 32,
  18226. /* tag, tag length */
  18227. {0xdb, 0x90, 0xbd, 0xb3, 0xda, 0x3d, 0x00, 0xaf,
  18228. 0xd0, 0xfc, 0x6a, 0x83, 0x55, 0x1d, 0xa9, 0x5e}, 16,
  18229. /* valid */
  18230. 1,
  18231. },
  18232. {
  18233. /* key, key length */
  18234. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18235. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18236. /* iv, iv length */
  18237. {0xae, 0xf0, 0x3d, 0x00, 0x59, 0x84, 0x94, 0xe9,
  18238. 0xfb, 0x03, 0xcd, 0x7d, 0x8b, 0x59, 0x08, 0x66}, 16,
  18239. /* aad, aad length */
  18240. {0x00}, 0,
  18241. /* msg, msg length */
  18242. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18243. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18244. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18245. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18246. /* ct, ct length */
  18247. {0xd1, 0x9a, 0xc5, 0x98, 0x49, 0x02, 0x6a, 0x91,
  18248. 0xaa, 0x1b, 0x9a, 0xec, 0x29, 0xb1, 0x1a, 0x20,
  18249. 0x2a, 0x4d, 0x73, 0x9f, 0xd8, 0x6c, 0x28, 0xe3,
  18250. 0xae, 0x3d, 0x58, 0x8e, 0xa2, 0x1d, 0x70, 0xc6}, 32,
  18251. /* tag, tag length */
  18252. {0xc3, 0x0f, 0x6c, 0xd9, 0x20, 0x20, 0x74, 0xed,
  18253. 0x6e, 0x2a, 0x2a, 0x36, 0x0e, 0xac, 0x8c, 0x47}, 16,
  18254. /* valid */
  18255. 1,
  18256. },
  18257. {
  18258. /* key, key length */
  18259. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18260. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18261. /* iv, iv length */
  18262. {0x55, 0xd1, 0x25, 0x11, 0xc6, 0x96, 0xa8, 0x0d,
  18263. 0x05, 0x14, 0xd1, 0xff, 0xba, 0x49, 0xca, 0xda}, 16,
  18264. /* aad, aad length */
  18265. {0x00}, 0,
  18266. /* msg, msg length */
  18267. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18268. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18269. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18270. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18271. /* ct, ct length */
  18272. {0x21, 0x08, 0x55, 0x8a, 0xc4, 0xb2, 0xc2, 0xd5,
  18273. 0xcc, 0x66, 0xce, 0xa5, 0x1d, 0x62, 0x10, 0xe0,
  18274. 0x46, 0x17, 0x7a, 0x67, 0x63, 0x1c, 0xd2, 0xdd,
  18275. 0x8f, 0x09, 0x46, 0x97, 0x33, 0xac, 0xb5, 0x17}, 32,
  18276. /* tag, tag length */
  18277. {0xfc, 0x35, 0x5e, 0x87, 0xa2, 0x67, 0xbe, 0x3a,
  18278. 0xe3, 0xe4, 0x4c, 0x0b, 0xf3, 0xf9, 0x9b, 0x2b}, 16,
  18279. /* valid */
  18280. 1,
  18281. },
  18282. {
  18283. /* key, key length */
  18284. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18285. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18286. /* iv, iv length */
  18287. {0x79, 0x42, 0x2d, 0xdd, 0x91, 0xc4, 0xee, 0xe2,
  18288. 0xde, 0xae, 0xf1, 0xf9, 0x68, 0x30, 0x53, 0x04}, 16,
  18289. /* aad, aad length */
  18290. {0x00}, 0,
  18291. /* msg, msg length */
  18292. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18293. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18294. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18295. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18296. /* ct, ct length */
  18297. {0x4d, 0x2c, 0x15, 0x24, 0xca, 0x4b, 0xaa, 0x4e,
  18298. 0xef, 0xcc, 0xe6, 0xb9, 0x1b, 0x22, 0x7e, 0xe8,
  18299. 0x3a, 0xba, 0xff, 0x81, 0x05, 0xdc, 0xaf, 0xa2,
  18300. 0xab, 0x19, 0x1f, 0x5d, 0xf2, 0x57, 0x50, 0x35}, 32,
  18301. /* tag, tag length */
  18302. {0xe2, 0xc8, 0x65, 0xce, 0x2d, 0x7a, 0xbd, 0xac,
  18303. 0x02, 0x4c, 0x6f, 0x99, 0x1a, 0x84, 0x83, 0x90}, 16,
  18304. /* valid */
  18305. 1,
  18306. },
  18307. {
  18308. /* key, key length */
  18309. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18310. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18311. /* iv, iv length */
  18312. {0x0a, 0xf5, 0xaa, 0x7a, 0x76, 0x76, 0xe2, 0x83,
  18313. 0x06, 0x30, 0x6b, 0xcd, 0x9b, 0xf2, 0x00, 0x3a}, 16,
  18314. /* aad, aad length */
  18315. {0x00}, 0,
  18316. /* msg, msg length */
  18317. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18318. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18319. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18320. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18321. /* ct, ct length */
  18322. {0x8e, 0xb0, 0x1e, 0x62, 0x18, 0x5d, 0x78, 0x2e,
  18323. 0xb9, 0x28, 0x7a, 0x34, 0x1a, 0x68, 0x62, 0xac,
  18324. 0x52, 0x57, 0xd6, 0xf9, 0xad, 0xc9, 0x9e, 0xe0,
  18325. 0xa2, 0x4d, 0x9c, 0x22, 0xb3, 0xe9, 0xb3, 0x8a}, 32,
  18326. /* tag, tag length */
  18327. {0x39, 0xc3, 0x39, 0xbc, 0x8a, 0x74, 0xc7, 0x5e,
  18328. 0x2c, 0x65, 0xc6, 0x11, 0x95, 0x44, 0xd6, 0x1e}, 16,
  18329. /* valid */
  18330. 1,
  18331. },
  18332. {
  18333. /* key, key length */
  18334. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18335. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18336. /* iv, iv length */
  18337. {0xaf, 0x5a, 0x03, 0xae, 0x7e, 0xdd, 0x73, 0x47,
  18338. 0x1b, 0xdc, 0xdf, 0xac, 0x5e, 0x19, 0x4a, 0x60}, 16,
  18339. /* aad, aad length */
  18340. {0x00}, 0,
  18341. /* msg, msg length */
  18342. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18343. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18344. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18345. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18346. /* ct, ct length */
  18347. {0x94, 0xc5, 0xd2, 0xac, 0xa6, 0xdb, 0xbc, 0xe8,
  18348. 0xc2, 0x45, 0x13, 0xa2, 0x5e, 0x09, 0x5c, 0x0e,
  18349. 0x54, 0xa9, 0x42, 0x86, 0x0d, 0x32, 0x7a, 0x22,
  18350. 0x2a, 0x81, 0x5c, 0xc7, 0x13, 0xb1, 0x63, 0xb4}, 32,
  18351. /* tag, tag length */
  18352. {0xf5, 0x0b, 0x30, 0x30, 0x4e, 0x45, 0xc9, 0xd4,
  18353. 0x11, 0xe8, 0xdf, 0x45, 0x08, 0xa9, 0x86, 0x12}, 16,
  18354. /* valid */
  18355. 1,
  18356. },
  18357. {
  18358. /* key, key length */
  18359. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18360. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18361. /* iv, iv length */
  18362. {0xb3, 0x70, 0x87, 0x68, 0x0f, 0x0e, 0xdd, 0x5a,
  18363. 0x52, 0x22, 0x8b, 0x8c, 0x7a, 0xae, 0xa6, 0x64}, 16,
  18364. /* aad, aad length */
  18365. {0x00}, 0,
  18366. /* msg, msg length */
  18367. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18368. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18369. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18370. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18371. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18372. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18373. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18374. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33}, 64,
  18375. /* ct, ct length */
  18376. {0x3b, 0xb6, 0x17, 0x3e, 0x37, 0x72, 0xd4, 0xb6,
  18377. 0x2e, 0xef, 0x37, 0xf9, 0xef, 0x07, 0x81, 0xf3,
  18378. 0x60, 0xb6, 0xc7, 0x4b, 0xe3, 0xbf, 0x6b, 0x37,
  18379. 0x10, 0x67, 0xbc, 0x1b, 0x09, 0x0d, 0x9d, 0x66,
  18380. 0x22, 0xa1, 0xfb, 0xec, 0x6a, 0xc4, 0x71, 0xb3,
  18381. 0x34, 0x9c, 0xd4, 0x27, 0x7a, 0x10, 0x1d, 0x40,
  18382. 0x89, 0x0f, 0xbf, 0x27, 0xdf, 0xdc, 0xd0, 0xb4,
  18383. 0xe3, 0x78, 0x1f, 0x98, 0x06, 0xda, 0xab, 0xb6}, 64,
  18384. /* tag, tag length */
  18385. {0xa0, 0x49, 0x87, 0x45, 0xe5, 0x99, 0x99, 0xdd,
  18386. 0xc3, 0x2d, 0x5b, 0x14, 0x02, 0x41, 0x12, 0x4e}, 16,
  18387. /* valid */
  18388. 1,
  18389. },
  18390. {
  18391. /* key, key length */
  18392. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18393. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18394. /* iv, iv length */
  18395. {0x4f, 0x80, 0x2d, 0xa6, 0x2a, 0x38, 0x45, 0x55,
  18396. 0xa1, 0x9b, 0xc2, 0xb3, 0x82, 0xeb, 0x25, 0xaf}, 16,
  18397. /* aad, aad length */
  18398. {0x00}, 0,
  18399. /* msg, msg length */
  18400. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18401. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18402. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18403. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18404. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18405. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18406. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18407. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18408. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  18409. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44}, 80,
  18410. /* ct, ct length */
  18411. {0xe9, 0xb0, 0xbb, 0x88, 0x57, 0x81, 0x8c, 0xe3,
  18412. 0x20, 0x1c, 0x36, 0x90, 0xd2, 0x1d, 0xaa, 0x7f,
  18413. 0x26, 0x4f, 0xb8, 0xee, 0x93, 0xcc, 0x7a, 0x46,
  18414. 0x74, 0xea, 0x2f, 0xc3, 0x2b, 0xf1, 0x82, 0xfb,
  18415. 0x2a, 0x7e, 0x8a, 0xd5, 0x15, 0x07, 0xad, 0x4f,
  18416. 0x31, 0xce, 0xfc, 0x23, 0x56, 0xfe, 0x79, 0x36,
  18417. 0xa7, 0xf6, 0xe1, 0x9f, 0x95, 0xe8, 0x8f, 0xdb,
  18418. 0xf1, 0x76, 0x20, 0x91, 0x6d, 0x3a, 0x6f, 0x3d,
  18419. 0x01, 0xfc, 0x17, 0xd3, 0x58, 0x67, 0x2f, 0x77,
  18420. 0x7f, 0xd4, 0x09, 0x92, 0x46, 0xe4, 0x36, 0xe1}, 80,
  18421. /* tag, tag length */
  18422. {0x67, 0x91, 0x0b, 0xe7, 0x44, 0xb8, 0x31, 0x5a,
  18423. 0xe0, 0xeb, 0x61, 0x24, 0x59, 0x0c, 0x5d, 0x8b}, 16,
  18424. /* valid */
  18425. 1,
  18426. },
  18427. {
  18428. /* key, key length */
  18429. {0xb6, 0x7b, 0x1a, 0x6e, 0xfd, 0xd4, 0x0d, 0x37,
  18430. 0x08, 0x0f, 0xbe, 0x8f, 0x80, 0x47, 0xae, 0xb9}, 16,
  18431. /* iv, iv length */
  18432. {0xfa, 0x29, 0x4b, 0x12, 0x99, 0x72, 0xf7, 0xfc,
  18433. 0x5b, 0xbd, 0x5b, 0x96, 0xbb, 0xa8, 0x37, 0xc9}, 16,
  18434. /* aad, aad length */
  18435. {0x00}, 0,
  18436. /* msg, msg length */
  18437. {0x00}, 0,
  18438. /* ct, ct length */
  18439. {0x00}, 0,
  18440. /* tag, tag length */
  18441. {0xb1, 0x4b, 0x64, 0xfb, 0x58, 0x98, 0x99, 0x69,
  18442. 0x95, 0x70, 0xcc, 0x91, 0x60, 0xe3, 0x98, 0x96}, 16,
  18443. /* valid */
  18444. 1,
  18445. },
  18446. {
  18447. /* key, key length */
  18448. {0x20, 0x9e, 0x6d, 0xbf, 0x2a, 0xd2, 0x6a, 0x10,
  18449. 0x54, 0x45, 0xfc, 0x02, 0x07, 0xcd, 0x9e, 0x9a}, 16,
  18450. /* iv, iv length */
  18451. {0x94, 0x77, 0x84, 0x9d, 0x6c, 0xcd, 0xfc, 0xa1,
  18452. 0x12, 0xd9, 0x2e, 0x53, 0xfa, 0xe4, 0xa7, 0xca}, 16,
  18453. /* aad, aad length */
  18454. {0x00}, 0,
  18455. /* msg, msg length */
  18456. {0x01}, 1,
  18457. /* ct, ct length */
  18458. {0x1d}, 1,
  18459. /* tag, tag length */
  18460. {0x52, 0xa5, 0xf6, 0x00, 0xfe, 0x53, 0x38, 0x02,
  18461. 0x6a, 0x7c, 0xb0, 0x9c, 0x11, 0x64, 0x00, 0x82}, 16,
  18462. /* valid */
  18463. 1,
  18464. },
  18465. {
  18466. /* key, key length */
  18467. {0xa5, 0x49, 0x44, 0x2e, 0x35, 0x15, 0x40, 0x32,
  18468. 0xd0, 0x7c, 0x86, 0x66, 0x00, 0x6a, 0xa6, 0xa2}, 16,
  18469. /* iv, iv length */
  18470. {0x51, 0x71, 0x52, 0x45, 0x68, 0xe8, 0x1d, 0x97,
  18471. 0xe8, 0xc4, 0xde, 0x4b, 0xa5, 0x6c, 0x10, 0xa0}, 16,
  18472. /* aad, aad length */
  18473. {0x00}, 0,
  18474. /* msg, msg length */
  18475. {0x11, 0x82, 0xe9, 0x35, 0x96, 0xca, 0xc5, 0x60,
  18476. 0x89, 0x46, 0x40, 0x0b, 0xc7, 0x3f, 0x3a}, 15,
  18477. /* ct, ct length */
  18478. {0xd7, 0xb8, 0xa6, 0xb4, 0x3d, 0x2e, 0x9f, 0x98,
  18479. 0xc2, 0xb4, 0x4c, 0xe5, 0xe3, 0xcf, 0xdb}, 15,
  18480. /* tag, tag length */
  18481. {0x1b, 0xdd, 0x52, 0xfc, 0x98, 0x7d, 0xaf, 0x0e,
  18482. 0xe1, 0x92, 0x34, 0xc9, 0x05, 0xea, 0x64, 0x5f}, 16,
  18483. /* valid */
  18484. 1,
  18485. },
  18486. {
  18487. /* key, key length */
  18488. {0x95, 0x8b, 0xcd, 0xb6, 0x6a, 0x39, 0x52, 0xb5,
  18489. 0x37, 0x01, 0x58, 0x2a, 0x68, 0xa0, 0xe4, 0x74}, 16,
  18490. /* iv, iv length */
  18491. {0x0e, 0x6e, 0xc8, 0x79, 0xb0, 0x2c, 0x6f, 0x51,
  18492. 0x69, 0x76, 0xe3, 0x58, 0x98, 0x42, 0x8d, 0xa7}, 16,
  18493. /* aad, aad length */
  18494. {0x00}, 0,
  18495. /* msg, msg length */
  18496. {0x14, 0x04, 0x15, 0x82, 0x3e, 0xcc, 0x89, 0x32,
  18497. 0xa0, 0x58, 0x38, 0x4b, 0x73, 0x8e, 0xa6, 0xea,
  18498. 0x6d, 0x4d, 0xfe, 0x3b, 0xbe, 0xee}, 22,
  18499. /* ct, ct length */
  18500. {0x73, 0xe5, 0xc6, 0xf0, 0xe7, 0x03, 0xa5, 0x2d,
  18501. 0x02, 0xf7, 0xf7, 0xfa, 0xeb, 0x1b, 0x77, 0xfd,
  18502. 0x4f, 0xd0, 0xcb, 0x42, 0x1e, 0xaf}, 22,
  18503. /* tag, tag length */
  18504. {0x6c, 0x15, 0x4a, 0x85, 0x96, 0x8e, 0xdd, 0x74,
  18505. 0x77, 0x65, 0x75, 0xa4, 0x45, 0x0b, 0xd8, 0x97}, 16,
  18506. /* valid */
  18507. 1,
  18508. },
  18509. {
  18510. /* key, key length */
  18511. {0x96, 0x5b, 0x75, 0x7b, 0xa5, 0x01, 0x8a, 0x8d,
  18512. 0x66, 0xed, 0xc7, 0x8e, 0x0c, 0xee, 0xe8, 0x6b}, 16,
  18513. /* iv, iv length */
  18514. {0x2e, 0x35, 0x90, 0x1a, 0xe7, 0xd4, 0x91, 0xee,
  18515. 0xcc, 0x88, 0x38, 0xfe, 0xdd, 0x63, 0x14, 0x05}, 16,
  18516. /* aad, aad length */
  18517. {0xdf, 0x10, 0xd0, 0xd2, 0x12, 0x24, 0x24, 0x50}, 8,
  18518. /* msg, msg length */
  18519. {0x36, 0xe5, 0x7a, 0x76, 0x39, 0x58, 0xb0, 0x2c,
  18520. 0xea, 0x9d, 0x6a, 0x67, 0x6e, 0xbc, 0xe8, 0x1f}, 16,
  18521. /* ct, ct length */
  18522. {0x93, 0x6b, 0x69, 0xb6, 0xc9, 0x55, 0xad, 0xfd,
  18523. 0x15, 0x53, 0x9b, 0x9b, 0xe4, 0x98, 0x9c, 0xb6}, 16,
  18524. /* tag, tag length */
  18525. {0xee, 0x15, 0xa1, 0x45, 0x4e, 0x88, 0xfa, 0xad,
  18526. 0x8e, 0x48, 0xa8, 0xdf, 0x29, 0x83, 0xb4, 0x25}, 16,
  18527. /* valid */
  18528. 1,
  18529. },
  18530. {
  18531. /* key, key length */
  18532. {0x88, 0xd0, 0x20, 0x33, 0x78, 0x1c, 0x7b, 0x41,
  18533. 0x64, 0x71, 0x1a, 0x05, 0x42, 0x0f, 0x25, 0x6e}, 16,
  18534. /* iv, iv length */
  18535. {0x7f, 0x29, 0x85, 0x29, 0x63, 0x15, 0x50, 0x7a,
  18536. 0xa4, 0xc0, 0xa9, 0x3d, 0x5c, 0x12, 0xbd, 0x77}, 16,
  18537. /* aad, aad length */
  18538. {0x7c, 0x57, 0x1d, 0x2f, 0xbb, 0x5f, 0x62, 0x52,
  18539. 0x3c, 0x0e, 0xb3, 0x38, 0xbe, 0xf9, 0xa9}, 15,
  18540. /* msg, msg length */
  18541. {0xd9, 0x8a, 0xdc, 0x03, 0xd9, 0xd5, 0x82, 0x73,
  18542. 0x2e, 0xb0, 0x7d, 0xf2, 0x3d, 0x7b, 0x9f, 0x74}, 16,
  18543. /* ct, ct length */
  18544. {0x67, 0xca, 0xac, 0x35, 0x44, 0x3a, 0x31, 0x38,
  18545. 0xd2, 0xcb, 0x81, 0x1f, 0x0c, 0xe0, 0x4d, 0xd2}, 16,
  18546. /* tag, tag length */
  18547. {0xb7, 0x96, 0x8e, 0x0b, 0x56, 0x40, 0xe3, 0xb2,
  18548. 0x36, 0x56, 0x96, 0x53, 0x20, 0x8b, 0x9d, 0xeb}, 16,
  18549. /* valid */
  18550. 1,
  18551. },
  18552. {
  18553. /* key, key length */
  18554. {0x51, 0x58, 0x40, 0xcf, 0x67, 0xd2, 0xe4, 0x0e,
  18555. 0xb6, 0x5e, 0x54, 0xa2, 0x4c, 0x72, 0xcb, 0xf2}, 16,
  18556. /* iv, iv length */
  18557. {0xbf, 0x47, 0xaf, 0xdf, 0xd4, 0x92, 0x13, 0x7a,
  18558. 0x24, 0x23, 0x6b, 0xc3, 0x67, 0x97, 0xa8, 0x8e}, 16,
  18559. /* aad, aad length */
  18560. {0x16, 0x84, 0x3c, 0x09, 0x1d, 0x43, 0xb0, 0xa1,
  18561. 0x91, 0xd0, 0xc7, 0x3d, 0x15, 0x60, 0x1b, 0xe9}, 16,
  18562. /* msg, msg length */
  18563. {0xc8, 0x34, 0x58, 0x8c, 0xb6, 0xda, 0xf9, 0xf0,
  18564. 0x6d, 0xd2, 0x35, 0x19, 0xf4, 0xbe, 0x9f, 0x56}, 16,
  18565. /* ct, ct length */
  18566. {0x20, 0x0a, 0xc4, 0x51, 0xfb, 0xeb, 0x0f, 0x61,
  18567. 0x51, 0xd6, 0x15, 0x83, 0xa4, 0x3b, 0x73, 0x43}, 16,
  18568. /* tag, tag length */
  18569. {0x2a, 0xd4, 0x3e, 0x4c, 0xaa, 0x51, 0x98, 0x3a,
  18570. 0x9d, 0x4d, 0x24, 0x48, 0x1b, 0xf4, 0xc8, 0x39}, 16,
  18571. /* valid */
  18572. 1,
  18573. },
  18574. {
  18575. /* key, key length */
  18576. {0x2e, 0x44, 0x92, 0xd4, 0x44, 0xe5, 0xb6, 0xf4,
  18577. 0xce, 0xc8, 0xc2, 0xd3, 0x61, 0x5a, 0xc8, 0x58}, 16,
  18578. /* iv, iv length */
  18579. {0xd0, 0x2b, 0xf0, 0x76, 0x3a, 0x9f, 0xef, 0xbf,
  18580. 0x70, 0xc3, 0x3a, 0xee, 0x1e, 0x9d, 0xa1, 0xd6}, 16,
  18581. /* aad, aad length */
  18582. {0x90, 0x4d, 0x86, 0xf1, 0x33, 0xce, 0xc1, 0x5a,
  18583. 0x0c, 0x3c, 0xaf, 0x14, 0xd7, 0xe0, 0x29, 0xc8,
  18584. 0x2a, 0x07, 0x70, 0x5a, 0x23, 0xf0, 0xd0, 0x80}, 24,
  18585. /* msg, msg length */
  18586. {0x9e, 0x62, 0xd6, 0x51, 0x1b, 0x0b, 0xda, 0x7d,
  18587. 0xd7, 0x74, 0x0b, 0x61, 0x4d, 0x97, 0xba, 0xe0}, 16,
  18588. /* ct, ct length */
  18589. {0x27, 0xc6, 0xe9, 0xa6, 0x53, 0xc5, 0x25, 0x3c,
  18590. 0xa1, 0xc5, 0x67, 0x3f, 0x97, 0xb9, 0xb3, 0x3e}, 16,
  18591. /* tag, tag length */
  18592. {0x2d, 0x58, 0x12, 0x71, 0xe1, 0xfa, 0x9e, 0x36,
  18593. 0x86, 0x13, 0x6c, 0xaa, 0x8f, 0x4d, 0x6c, 0x8e}, 16,
  18594. /* valid */
  18595. 1,
  18596. },
  18597. {
  18598. /* key, key length */
  18599. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18600. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18601. /* iv, iv length */
  18602. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18603. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18604. /* aad, aad length */
  18605. {0x00}, 0,
  18606. /* msg, msg length */
  18607. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18608. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18609. /* ct, ct length */
  18610. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18611. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18612. /* tag, tag length */
  18613. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18614. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18615. /* valid */
  18616. 0,
  18617. },
  18618. {
  18619. /* key, key length */
  18620. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18621. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18622. /* iv, iv length */
  18623. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18624. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18625. /* aad, aad length */
  18626. {0x00}, 0,
  18627. /* msg, msg length */
  18628. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18629. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18630. /* ct, ct length */
  18631. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18632. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18633. /* tag, tag length */
  18634. {0xe4, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18635. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18636. /* valid */
  18637. 0,
  18638. },
  18639. {
  18640. /* key, key length */
  18641. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18642. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18643. /* iv, iv length */
  18644. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18645. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18646. /* aad, aad length */
  18647. {0x00}, 0,
  18648. /* msg, msg length */
  18649. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18650. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18651. /* ct, ct length */
  18652. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18653. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18654. /* tag, tag length */
  18655. {0x66, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18656. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18657. /* valid */
  18658. 0,
  18659. },
  18660. {
  18661. /* key, key length */
  18662. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18663. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18664. /* iv, iv length */
  18665. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18666. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18667. /* aad, aad length */
  18668. {0x00}, 0,
  18669. /* msg, msg length */
  18670. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18671. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18672. /* ct, ct length */
  18673. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18674. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18675. /* tag, tag length */
  18676. {0xe6, 0x0f, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18677. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18678. /* valid */
  18679. 0,
  18680. },
  18681. {
  18682. /* key, key length */
  18683. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18684. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18685. /* iv, iv length */
  18686. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18687. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18688. /* aad, aad length */
  18689. {0x00}, 0,
  18690. /* msg, msg length */
  18691. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18692. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18693. /* ct, ct length */
  18694. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18695. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18696. /* tag, tag length */
  18697. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0xf2,
  18698. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18699. /* valid */
  18700. 0,
  18701. },
  18702. {
  18703. /* key, key length */
  18704. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18705. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18706. /* iv, iv length */
  18707. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18708. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18709. /* aad, aad length */
  18710. {0x00}, 0,
  18711. /* msg, msg length */
  18712. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18713. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18714. /* ct, ct length */
  18715. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18716. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18717. /* tag, tag length */
  18718. {0xe6, 0x0e, 0x7c, 0x50, 0x12, 0xa6, 0xdb, 0xf2,
  18719. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18720. /* valid */
  18721. 0,
  18722. },
  18723. {
  18724. /* key, key length */
  18725. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18726. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18727. /* iv, iv length */
  18728. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18729. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18730. /* aad, aad length */
  18731. {0x00}, 0,
  18732. /* msg, msg length */
  18733. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18734. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18735. /* ct, ct length */
  18736. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18737. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18738. /* tag, tag length */
  18739. {0xe6, 0x0e, 0x7c, 0x50, 0x11, 0xa6, 0xdb, 0xf2,
  18740. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18741. /* valid */
  18742. 0,
  18743. },
  18744. {
  18745. /* key, key length */
  18746. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18747. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18748. /* iv, iv length */
  18749. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18750. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18751. /* aad, aad length */
  18752. {0x00}, 0,
  18753. /* msg, msg length */
  18754. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18755. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18756. /* ct, ct length */
  18757. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18758. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18759. /* tag, tag length */
  18760. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  18761. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18762. /* valid */
  18763. 0,
  18764. },
  18765. {
  18766. /* key, key length */
  18767. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18768. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18769. /* iv, iv length */
  18770. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18771. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18772. /* aad, aad length */
  18773. {0x00}, 0,
  18774. /* msg, msg length */
  18775. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18776. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18777. /* ct, ct length */
  18778. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18779. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18780. /* tag, tag length */
  18781. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18782. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18783. /* valid */
  18784. 0,
  18785. },
  18786. {
  18787. /* key, key length */
  18788. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18789. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18790. /* iv, iv length */
  18791. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18792. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18793. /* aad, aad length */
  18794. {0x00}, 0,
  18795. /* msg, msg length */
  18796. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18797. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18798. /* ct, ct length */
  18799. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18800. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18801. /* tag, tag length */
  18802. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18803. 0xd2, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18804. /* valid */
  18805. 0,
  18806. },
  18807. {
  18808. /* key, key length */
  18809. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18810. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18811. /* iv, iv length */
  18812. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18813. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18814. /* aad, aad length */
  18815. {0x00}, 0,
  18816. /* msg, msg length */
  18817. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18818. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18819. /* ct, ct length */
  18820. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18821. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18822. /* tag, tag length */
  18823. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18824. 0x52, 0xb8, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18825. /* valid */
  18826. 0,
  18827. },
  18828. {
  18829. /* key, key length */
  18830. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18831. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18832. /* iv, iv length */
  18833. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18834. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18835. /* aad, aad length */
  18836. {0x00}, 0,
  18837. /* msg, msg length */
  18838. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18839. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18840. /* ct, ct length */
  18841. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18842. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18843. /* tag, tag length */
  18844. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18845. 0x52, 0x98, 0xb0, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18846. /* valid */
  18847. 0,
  18848. },
  18849. {
  18850. /* key, key length */
  18851. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18852. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18853. /* iv, iv length */
  18854. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18855. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18856. /* aad, aad length */
  18857. {0x00}, 0,
  18858. /* msg, msg length */
  18859. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18860. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18861. /* ct, ct length */
  18862. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18863. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18864. /* tag, tag length */
  18865. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18866. 0x52, 0x98, 0xb1, 0x92, 0x9a, 0xc3, 0x56, 0xa7}, 16,
  18867. /* valid */
  18868. 0,
  18869. },
  18870. {
  18871. /* key, key length */
  18872. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18873. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18874. /* iv, iv length */
  18875. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18876. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18877. /* aad, aad length */
  18878. {0x00}, 0,
  18879. /* msg, msg length */
  18880. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18881. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18882. /* ct, ct length */
  18883. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18884. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18885. /* tag, tag length */
  18886. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18887. 0x52, 0x98, 0xb1, 0x92, 0x99, 0xc3, 0x56, 0xa7}, 16,
  18888. /* valid */
  18889. 0,
  18890. },
  18891. {
  18892. /* key, key length */
  18893. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18894. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18895. /* iv, iv length */
  18896. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18897. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18898. /* aad, aad length */
  18899. {0x00}, 0,
  18900. /* msg, msg length */
  18901. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18902. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18903. /* ct, ct length */
  18904. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18905. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18906. /* tag, tag length */
  18907. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18908. 0x52, 0x98, 0xb1, 0x92, 0x1b, 0xc3, 0x56, 0xa7}, 16,
  18909. /* valid */
  18910. 0,
  18911. },
  18912. {
  18913. /* key, key length */
  18914. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18915. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18916. /* iv, iv length */
  18917. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18918. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18919. /* aad, aad length */
  18920. {0x00}, 0,
  18921. /* msg, msg length */
  18922. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18923. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18924. /* ct, ct length */
  18925. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18926. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18927. /* tag, tag length */
  18928. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18929. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa6}, 16,
  18930. /* valid */
  18931. 0,
  18932. },
  18933. {
  18934. /* key, key length */
  18935. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18936. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18937. /* iv, iv length */
  18938. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18939. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18940. /* aad, aad length */
  18941. {0x00}, 0,
  18942. /* msg, msg length */
  18943. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18944. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18945. /* ct, ct length */
  18946. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18947. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18948. /* tag, tag length */
  18949. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18950. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa5}, 16,
  18951. /* valid */
  18952. 0,
  18953. },
  18954. {
  18955. /* key, key length */
  18956. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18957. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18958. /* iv, iv length */
  18959. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18960. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18961. /* aad, aad length */
  18962. {0x00}, 0,
  18963. /* msg, msg length */
  18964. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18965. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18966. /* ct, ct length */
  18967. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18968. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18969. /* tag, tag length */
  18970. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18971. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xe7}, 16,
  18972. /* valid */
  18973. 0,
  18974. },
  18975. {
  18976. /* key, key length */
  18977. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18978. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18979. /* iv, iv length */
  18980. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18981. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18982. /* aad, aad length */
  18983. {0x00}, 0,
  18984. /* msg, msg length */
  18985. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18986. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18987. /* ct, ct length */
  18988. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18989. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18990. /* tag, tag length */
  18991. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18992. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  18993. /* valid */
  18994. 0,
  18995. },
  18996. {
  18997. /* key, key length */
  18998. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18999. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19000. /* iv, iv length */
  19001. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19002. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19003. /* aad, aad length */
  19004. {0x00}, 0,
  19005. /* msg, msg length */
  19006. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19007. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19008. /* ct, ct length */
  19009. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19010. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19011. /* tag, tag length */
  19012. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19013. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19014. /* valid */
  19015. 0,
  19016. },
  19017. {
  19018. /* key, key length */
  19019. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19020. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19021. /* iv, iv length */
  19022. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19023. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19024. /* aad, aad length */
  19025. {0x00}, 0,
  19026. /* msg, msg length */
  19027. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19028. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19029. /* ct, ct length */
  19030. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19031. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19032. /* tag, tag length */
  19033. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0x72,
  19034. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19035. /* valid */
  19036. 0,
  19037. },
  19038. {
  19039. /* key, key length */
  19040. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19041. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19042. /* iv, iv length */
  19043. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19044. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19045. /* aad, aad length */
  19046. {0x00}, 0,
  19047. /* msg, msg length */
  19048. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19049. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19050. /* ct, ct length */
  19051. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19052. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19053. /* tag, tag length */
  19054. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  19055. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  19056. /* valid */
  19057. 0,
  19058. },
  19059. {
  19060. /* key, key length */
  19061. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19062. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19063. /* iv, iv length */
  19064. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19065. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19066. /* aad, aad length */
  19067. {0x00}, 0,
  19068. /* msg, msg length */
  19069. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19070. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19071. /* ct, ct length */
  19072. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19073. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19074. /* tag, tag length */
  19075. {0x19, 0xf1, 0x83, 0xaf, 0xec, 0x59, 0x24, 0x0d,
  19076. 0xad, 0x67, 0x4e, 0x6d, 0x64, 0x3c, 0xa9, 0x58}, 16,
  19077. /* valid */
  19078. 0,
  19079. },
  19080. {
  19081. /* key, key length */
  19082. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19083. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19084. /* iv, iv length */
  19085. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19086. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19087. /* aad, aad length */
  19088. {0x00}, 0,
  19089. /* msg, msg length */
  19090. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19091. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19092. /* ct, ct length */
  19093. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19094. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19095. /* tag, tag length */
  19096. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  19097. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 16,
  19098. /* valid */
  19099. 0,
  19100. },
  19101. {
  19102. /* key, key length */
  19103. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19104. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19105. /* iv, iv length */
  19106. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19107. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19108. /* aad, aad length */
  19109. {0x00}, 0,
  19110. /* msg, msg length */
  19111. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19112. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19113. /* ct, ct length */
  19114. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19115. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19116. /* tag, tag length */
  19117. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  19118. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 16,
  19119. /* valid */
  19120. 0,
  19121. },
  19122. {
  19123. /* key, key length */
  19124. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19125. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19126. /* iv, iv length */
  19127. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19128. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19129. /* aad, aad length */
  19130. {0x00}, 0,
  19131. /* msg, msg length */
  19132. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19133. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19134. /* ct, ct length */
  19135. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19136. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19137. /* tag, tag length */
  19138. {0x66, 0x8e, 0xfc, 0xd0, 0x93, 0x26, 0x5b, 0x72,
  19139. 0xd2, 0x18, 0x31, 0x12, 0x1b, 0x43, 0xd6, 0x27}, 16,
  19140. /* valid */
  19141. 0,
  19142. },
  19143. {
  19144. /* key, key length */
  19145. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19146. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19147. /* iv, iv length */
  19148. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19149. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19150. /* aad, aad length */
  19151. {0x00}, 0,
  19152. /* msg, msg length */
  19153. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19154. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19155. /* ct, ct length */
  19156. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19157. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19158. /* tag, tag length */
  19159. {0xe7, 0x0f, 0x7d, 0x51, 0x12, 0xa7, 0xda, 0xf3,
  19160. 0x53, 0x99, 0xb0, 0x93, 0x9a, 0xc2, 0x57, 0xa6}, 16,
  19161. /* valid */
  19162. 0,
  19163. },
  19164. };
  19165. byte ciphertext[sizeof(vectors[0].ct)];
  19166. byte authtag[sizeof(vectors[0].tag)];
  19167. int i;
  19168. int len;
  19169. int ret;
  19170. for (i = 0; i < (int)(sizeof(vectors)/sizeof(vectors[0])); i++) {
  19171. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  19172. len = sizeof(authtag);
  19173. ExpectIntEQ(wc_AesEaxEncryptAuth(vectors[i].key, vectors[i].key_length,
  19174. ciphertext,
  19175. vectors[i].msg, vectors[i].msg_length,
  19176. vectors[i].iv, vectors[i].iv_length,
  19177. authtag, len,
  19178. vectors[i].aad, vectors[i].aad_length),
  19179. 0);
  19180. /* check ciphertext matches vector */
  19181. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].ct, vectors[i].ct_length),
  19182. 0);
  19183. /* check that computed tag matches vector only for vectors marked as valid */
  19184. ret = XMEMCMP(authtag, vectors[i].tag, len);
  19185. if (vectors[i].valid) {
  19186. ExpectIntEQ(ret, 0);
  19187. }
  19188. else {
  19189. ExpectIntNE(ret, 0);
  19190. }
  19191. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  19192. /* Decrypt, checking that the computed auth tags match */
  19193. ExpectIntEQ(wc_AesEaxDecryptAuth(vectors[i].key, vectors[i].key_length,
  19194. ciphertext,
  19195. vectors[i].ct, vectors[i].ct_length,
  19196. vectors[i].iv, vectors[i].iv_length,
  19197. authtag, len,
  19198. vectors[i].aad, vectors[i].aad_length),
  19199. 0);
  19200. /* check decrypted ciphertext matches vector plaintext */
  19201. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].msg, vectors[i].msg_length),
  19202. 0);
  19203. }
  19204. return EXPECT_RESULT();
  19205. } /* END test_wc_AesEaxVectors */
  19206. /*
  19207. * Testing test_wc_AesEaxEncryptAuth()
  19208. */
  19209. static int test_wc_AesEaxEncryptAuth(void)
  19210. {
  19211. EXPECT_DECLS;
  19212. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19213. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19214. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19215. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19216. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19217. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19218. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19219. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19220. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  19221. const byte msg[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  19222. byte ciphertext[sizeof(msg)];
  19223. byte authtag[AES_BLOCK_SIZE];
  19224. int i;
  19225. int len;
  19226. len = sizeof(authtag);
  19227. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19228. ciphertext,
  19229. msg, sizeof(msg),
  19230. iv, sizeof(iv),
  19231. authtag, len,
  19232. aad, sizeof(aad)),
  19233. 0);
  19234. /* Test null checking */
  19235. ExpectIntEQ(wc_AesEaxEncryptAuth(NULL, sizeof(key),
  19236. ciphertext,
  19237. msg, sizeof(msg),
  19238. iv, sizeof(iv),
  19239. authtag, len,
  19240. aad, sizeof(aad)),
  19241. BAD_FUNC_ARG);
  19242. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19243. NULL,
  19244. msg, sizeof(msg),
  19245. iv, sizeof(iv),
  19246. authtag, len,
  19247. aad, sizeof(aad)),
  19248. BAD_FUNC_ARG);
  19249. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19250. ciphertext,
  19251. NULL, sizeof(msg),
  19252. iv, sizeof(iv),
  19253. authtag, len,
  19254. aad, sizeof(aad)),
  19255. BAD_FUNC_ARG);
  19256. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19257. ciphertext,
  19258. msg, sizeof(msg),
  19259. NULL, sizeof(iv),
  19260. authtag, len,
  19261. aad, sizeof(aad)),
  19262. BAD_FUNC_ARG);
  19263. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19264. ciphertext,
  19265. msg, sizeof(msg),
  19266. iv, sizeof(iv),
  19267. NULL, len,
  19268. aad, sizeof(aad)),
  19269. BAD_FUNC_ARG);
  19270. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19271. ciphertext,
  19272. msg, sizeof(msg),
  19273. iv, sizeof(iv),
  19274. authtag, len,
  19275. NULL, sizeof(aad)),
  19276. BAD_FUNC_ARG);
  19277. /* Test bad key lengths */
  19278. for (i = 0; i <= 32; i++) {
  19279. int exp_ret;
  19280. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  19281. || i == AES_256_KEY_SIZE) {
  19282. exp_ret = 0;
  19283. }
  19284. else {
  19285. exp_ret = BAD_FUNC_ARG;
  19286. }
  19287. ExpectIntEQ(wc_AesEaxEncryptAuth(key, i,
  19288. ciphertext,
  19289. msg, sizeof(msg),
  19290. iv, sizeof(iv),
  19291. authtag, len,
  19292. aad, sizeof(aad)),
  19293. exp_ret);
  19294. }
  19295. /* Test auth tag size out of range */
  19296. len = AES_BLOCK_SIZE + 1;
  19297. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19298. ciphertext,
  19299. msg, sizeof(msg),
  19300. iv, sizeof(iv),
  19301. authtag, len,
  19302. aad, sizeof(aad)),
  19303. BAD_FUNC_ARG);
  19304. return EXPECT_RESULT();
  19305. } /* END test_wc_AesEaxEncryptAuth() */
  19306. /*
  19307. * Testing test_wc_AesEaxDecryptAuth()
  19308. */
  19309. static int test_wc_AesEaxDecryptAuth(void)
  19310. {
  19311. EXPECT_DECLS;
  19312. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19313. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19314. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19315. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19316. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19317. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19318. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19319. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19320. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  19321. const byte ct[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  19322. /* Garbage tag that should always fail for above aad */
  19323. const byte tag[] = {0xFE, 0xED, 0xBE, 0xEF, 0xDE, 0xAD, 0xC0, 0xDE,
  19324. 0xCA, 0xFE, 0xBE, 0xEF, 0xDE, 0xAF, 0xBE, 0xEF};
  19325. byte plaintext[sizeof(ct)];
  19326. int i;
  19327. int len;
  19328. len = sizeof(tag);
  19329. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19330. plaintext,
  19331. ct, sizeof(ct),
  19332. iv, sizeof(iv),
  19333. tag, len,
  19334. aad, sizeof(aad)),
  19335. AES_EAX_AUTH_E);
  19336. /* Test null checking */
  19337. ExpectIntEQ(wc_AesEaxDecryptAuth(NULL, sizeof(key),
  19338. plaintext,
  19339. ct, sizeof(ct),
  19340. iv, sizeof(iv),
  19341. tag, len,
  19342. aad, sizeof(aad)),
  19343. BAD_FUNC_ARG);
  19344. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19345. NULL,
  19346. ct, sizeof(ct),
  19347. iv, sizeof(iv),
  19348. tag, len,
  19349. aad, sizeof(aad)),
  19350. BAD_FUNC_ARG);
  19351. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19352. plaintext,
  19353. NULL, sizeof(ct),
  19354. iv, sizeof(iv),
  19355. tag, len,
  19356. aad, sizeof(aad)),
  19357. BAD_FUNC_ARG);
  19358. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19359. plaintext,
  19360. ct, sizeof(ct),
  19361. NULL, sizeof(iv),
  19362. tag, len,
  19363. aad, sizeof(aad)),
  19364. BAD_FUNC_ARG);
  19365. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19366. plaintext,
  19367. ct, sizeof(ct),
  19368. iv, sizeof(iv),
  19369. NULL, len,
  19370. aad, sizeof(aad)),
  19371. BAD_FUNC_ARG);
  19372. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19373. plaintext,
  19374. ct, sizeof(ct),
  19375. iv, sizeof(iv),
  19376. tag, len,
  19377. NULL, sizeof(aad)),
  19378. BAD_FUNC_ARG);
  19379. /* Test bad key lengths */
  19380. for (i = 0; i <= 32; i++) {
  19381. int exp_ret;
  19382. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  19383. || i == AES_256_KEY_SIZE) {
  19384. exp_ret = AES_EAX_AUTH_E;
  19385. }
  19386. else {
  19387. exp_ret = BAD_FUNC_ARG;
  19388. }
  19389. ExpectIntEQ(wc_AesEaxDecryptAuth(key, i,
  19390. plaintext,
  19391. ct, sizeof(ct),
  19392. iv, sizeof(iv),
  19393. tag, len,
  19394. aad, sizeof(aad)),
  19395. exp_ret);
  19396. }
  19397. /* Test auth tag size out of range */
  19398. len = AES_BLOCK_SIZE + 1;
  19399. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19400. plaintext,
  19401. ct, sizeof(ct),
  19402. iv, sizeof(iv),
  19403. tag, len,
  19404. aad, sizeof(aad)),
  19405. BAD_FUNC_ARG);
  19406. return EXPECT_RESULT();
  19407. } /* END test_wc_AesEaxDecryptAuth() */
  19408. #endif /* WOLFSSL_AES_EAX &&
  19409. * (!HAVE_FIPS || FIPS_VERSION_GE(5, 3)) && !HAVE_SELFTEST
  19410. */
  19411. /*
  19412. * Testing wc_InitDsaKey()
  19413. */
  19414. static int test_wc_InitDsaKey(void)
  19415. {
  19416. EXPECT_DECLS;
  19417. #ifndef NO_DSA
  19418. DsaKey key;
  19419. XMEMSET(&key, 0, sizeof(DsaKey));
  19420. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19421. /* Pass in bad args. */
  19422. ExpectIntEQ(wc_InitDsaKey(NULL), BAD_FUNC_ARG);
  19423. wc_FreeDsaKey(&key);
  19424. #endif
  19425. return EXPECT_RESULT();
  19426. } /* END test_wc_InitDsaKey */
  19427. /*
  19428. * Testing wc_DsaSign() and wc_DsaVerify()
  19429. */
  19430. static int test_wc_DsaSignVerify(void)
  19431. {
  19432. EXPECT_DECLS;
  19433. #if !defined(NO_DSA)
  19434. DsaKey key;
  19435. WC_RNG rng;
  19436. wc_Sha sha;
  19437. byte signature[DSA_SIG_SIZE];
  19438. byte hash[WC_SHA_DIGEST_SIZE];
  19439. word32 idx = 0;
  19440. word32 bytes;
  19441. int answer;
  19442. #ifdef USE_CERT_BUFFERS_1024
  19443. byte tmp[ONEK_BUF];
  19444. XMEMSET(tmp, 0, sizeof(tmp));
  19445. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19446. bytes = sizeof_dsa_key_der_1024;
  19447. #elif defined(USE_CERT_BUFFERS_2048)
  19448. byte tmp[TWOK_BUF];
  19449. XMEMSET(tmp, 0, sizeof(tmp));
  19450. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19451. bytes = sizeof_dsa_key_der_2048;
  19452. #else
  19453. byte tmp[TWOK_BUF];
  19454. XFILE fp = XBADFILE;
  19455. XMEMSET(tmp, 0, sizeof(tmp));
  19456. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19457. ExpectTrue((bytes = (word32)XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19458. if (fp != XBADFILE)
  19459. XFCLOSE(fp);
  19460. #endif /* END USE_CERT_BUFFERS_1024 */
  19461. ExpectIntEQ(wc_InitSha(&sha), 0);
  19462. ExpectIntEQ(wc_ShaUpdate(&sha, tmp, bytes), 0);
  19463. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  19464. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19465. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19466. ExpectIntEQ(wc_InitRng(&rng), 0);
  19467. /* Sign. */
  19468. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), 0);
  19469. /* Test bad args. */
  19470. ExpectIntEQ(wc_DsaSign(NULL, signature, &key, &rng), BAD_FUNC_ARG);
  19471. ExpectIntEQ(wc_DsaSign(hash, NULL, &key, &rng), BAD_FUNC_ARG);
  19472. ExpectIntEQ(wc_DsaSign(hash, signature, NULL, &rng), BAD_FUNC_ARG);
  19473. ExpectIntEQ(wc_DsaSign(hash, signature, &key, NULL), BAD_FUNC_ARG);
  19474. /* Verify. */
  19475. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, &answer), 0);
  19476. ExpectIntEQ(answer, 1);
  19477. /* Pass in bad args. */
  19478. ExpectIntEQ(wc_DsaVerify(NULL, signature, &key, &answer), BAD_FUNC_ARG);
  19479. ExpectIntEQ(wc_DsaVerify(hash, NULL, &key, &answer), BAD_FUNC_ARG);
  19480. ExpectIntEQ(wc_DsaVerify(hash, signature, NULL, &answer), BAD_FUNC_ARG);
  19481. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, NULL), BAD_FUNC_ARG);
  19482. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  19483. /* hard set q to 0 and test fail case */
  19484. mp_free(&key.q);
  19485. mp_init(&key.q);
  19486. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19487. mp_set(&key.q, 1);
  19488. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19489. #endif
  19490. DoExpectIntEQ(wc_FreeRng(&rng),0);
  19491. wc_FreeDsaKey(&key);
  19492. wc_ShaFree(&sha);
  19493. #endif
  19494. return EXPECT_RESULT();
  19495. } /* END test_wc_DsaSign */
  19496. /*
  19497. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  19498. */
  19499. static int test_wc_DsaPublicPrivateKeyDecode(void)
  19500. {
  19501. EXPECT_DECLS;
  19502. #if !defined(NO_DSA)
  19503. DsaKey key;
  19504. word32 bytes = 0;
  19505. word32 idx = 0;
  19506. int ret = 0;
  19507. #ifdef USE_CERT_BUFFERS_1024
  19508. byte tmp[ONEK_BUF];
  19509. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19510. bytes = sizeof_dsa_key_der_1024;
  19511. #elif defined(USE_CERT_BUFFERS_2048)
  19512. byte tmp[TWOK_BUF];
  19513. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19514. bytes = sizeof_dsa_key_der_2048;
  19515. #else
  19516. byte tmp[TWOK_BUF];
  19517. XFILE fp = XBADFILE;
  19518. XMEMSET(tmp, 0, sizeof(tmp));
  19519. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19520. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19521. if (fp != XBADFILE)
  19522. XFCLOSE(fp);
  19523. #endif /* END USE_CERT_BUFFERS_1024 */
  19524. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19525. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19526. /* Test bad args. */
  19527. ExpectIntEQ(wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  19528. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  19529. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  19530. ExpectIntLT(ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19531. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  19532. wc_FreeDsaKey(&key);
  19533. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19534. idx = 0; /* Reset */
  19535. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  19536. /* Test bad args. */
  19537. ExpectIntEQ(wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  19538. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  19539. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  19540. ExpectIntLT(ret = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  19541. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  19542. wc_FreeDsaKey(&key);
  19543. #endif /* !NO_DSA */
  19544. return EXPECT_RESULT();
  19545. } /* END test_wc_DsaPublicPrivateKeyDecode */
  19546. /*
  19547. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  19548. */
  19549. static int test_wc_MakeDsaKey(void)
  19550. {
  19551. EXPECT_DECLS;
  19552. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19553. DsaKey genKey;
  19554. WC_RNG rng;
  19555. XMEMSET(&genKey, 0, sizeof(genKey));
  19556. XMEMSET(&rng, 0, sizeof(rng));
  19557. ExpectIntEQ(wc_InitDsaKey(&genKey), 0);
  19558. ExpectIntEQ(wc_InitRng(&rng), 0);
  19559. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey), 0);
  19560. /* Test bad args. */
  19561. ExpectIntEQ(wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey), BAD_FUNC_ARG);
  19562. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, NULL), BAD_FUNC_ARG);
  19563. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey),
  19564. BAD_FUNC_ARG);
  19565. ExpectIntEQ(wc_MakeDsaKey(&rng, &genKey), 0);
  19566. /* Test bad args. */
  19567. ExpectIntEQ(wc_MakeDsaKey(NULL, &genKey), BAD_FUNC_ARG);
  19568. ExpectIntEQ(wc_MakeDsaKey(&rng, NULL), BAD_FUNC_ARG);
  19569. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19570. wc_FreeDsaKey(&genKey);
  19571. #endif
  19572. return EXPECT_RESULT();
  19573. } /* END test_wc_MakeDsaKey */
  19574. /*
  19575. * Testing wc_DsaKeyToDer()
  19576. */
  19577. static int test_wc_DsaKeyToDer(void)
  19578. {
  19579. EXPECT_DECLS;
  19580. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19581. DsaKey key;
  19582. word32 bytes;
  19583. word32 idx = 0;
  19584. #ifdef USE_CERT_BUFFERS_1024
  19585. byte tmp[ONEK_BUF];
  19586. byte der[ONEK_BUF];
  19587. XMEMSET(tmp, 0, sizeof(tmp));
  19588. XMEMSET(der, 0, sizeof(der));
  19589. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19590. bytes = sizeof_dsa_key_der_1024;
  19591. #elif defined(USE_CERT_BUFFERS_2048)
  19592. byte tmp[TWOK_BUF];
  19593. byte der[TWOK_BUF];
  19594. XMEMSET(tmp, 0, sizeof(tmp));
  19595. XMEMSET(der, 0, sizeof(der));
  19596. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19597. bytes = sizeof_dsa_key_der_2048;
  19598. #else
  19599. byte tmp[TWOK_BUF];
  19600. byte der[TWOK_BUF];
  19601. XFILE fp = XBADFILE;
  19602. XMEMSET(tmp, 0, sizeof(tmp));
  19603. XMEMSET(der, 0, sizeof(der));
  19604. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19605. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19606. if (fp != XBADFILE)
  19607. XFCLOSE(fp);
  19608. #endif /* END USE_CERT_BUFFERS_1024 */
  19609. XMEMSET(&key, 0, sizeof(DsaKey));
  19610. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19611. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19612. ExpectIntGE(wc_DsaKeyToDer(&key, der, bytes), 0);
  19613. ExpectIntEQ(XMEMCMP(der, tmp, bytes), 0);
  19614. /* Test bad args. */
  19615. ExpectIntEQ(wc_DsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  19616. ExpectIntEQ(wc_DsaKeyToDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  19617. wc_FreeDsaKey(&key);
  19618. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19619. return EXPECT_RESULT();
  19620. } /* END test_wc_DsaKeyToDer */
  19621. /*
  19622. * Testing wc_DsaKeyToPublicDer()
  19623. * (indirectly testing setDsaPublicKey())
  19624. */
  19625. static int test_wc_DsaKeyToPublicDer(void)
  19626. {
  19627. EXPECT_DECLS;
  19628. #ifndef HAVE_SELFTEST
  19629. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19630. DsaKey key;
  19631. WC_RNG rng;
  19632. byte* der = NULL;
  19633. word32 sz = 0;
  19634. word32 idx = 0;
  19635. XMEMSET(&key, 0, sizeof(DsaKey));
  19636. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19637. ExpectNotNull(der = (byte*)XMALLOC(ONEK_BUF, NULL,
  19638. DYNAMIC_TYPE_TMP_BUFFER));
  19639. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19640. ExpectIntEQ(wc_InitRng(&rng), 0);
  19641. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &key), 0);
  19642. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  19643. ExpectIntGE(sz = wc_DsaKeyToPublicDer(&key, der, ONEK_BUF), 0);
  19644. wc_FreeDsaKey(&key);
  19645. idx = 0;
  19646. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  19647. /* Test without the SubjectPublicKeyInfo header */
  19648. ExpectIntGE(sz = wc_SetDsaPublicKey(der, &key, ONEK_BUF, 0), 0);
  19649. wc_FreeDsaKey(&key);
  19650. idx = 0;
  19651. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  19652. /* Test bad args. */
  19653. ExpectIntEQ(wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  19654. ExpectIntEQ(wc_DsaKeyToPublicDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  19655. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19656. wc_FreeDsaKey(&key);
  19657. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  19658. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19659. #endif /* !HAVE_SELFTEST */
  19660. return EXPECT_RESULT();
  19661. } /* END test_wc_DsaKeyToPublicDer */
  19662. /*
  19663. * Testing wc_DsaImportParamsRaw()
  19664. */
  19665. static int test_wc_DsaImportParamsRaw(void)
  19666. {
  19667. EXPECT_DECLS;
  19668. #if !defined(NO_DSA)
  19669. DsaKey key;
  19670. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19671. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19672. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19673. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19674. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19675. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19676. "80a0093113a8bd73";
  19677. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19678. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19679. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19680. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19681. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19682. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19683. "76341a7e7d9";
  19684. /* invalid p and q parameters */
  19685. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19686. const char* invalidQ = "96c5390a";
  19687. XMEMSET(&key, 0, sizeof(DsaKey));
  19688. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19689. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  19690. /* test bad args */
  19691. /* null key struct */
  19692. ExpectIntEQ(wc_DsaImportParamsRaw(NULL, p, q, g), BAD_FUNC_ARG);
  19693. /* null param pointers */
  19694. ExpectIntEQ(wc_DsaImportParamsRaw(&key, NULL, NULL, NULL), BAD_FUNC_ARG);
  19695. /* illegal p length */
  19696. ExpectIntEQ(wc_DsaImportParamsRaw(&key, invalidP, q, g), BAD_FUNC_ARG);
  19697. /* illegal q length */
  19698. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, invalidQ, g), BAD_FUNC_ARG);
  19699. wc_FreeDsaKey(&key);
  19700. #endif
  19701. return EXPECT_RESULT();
  19702. } /* END test_wc_DsaImportParamsRaw */
  19703. /*
  19704. * Testing wc_DsaImportParamsRawCheck()
  19705. */
  19706. static int test_wc_DsaImportParamsRawCheck(void)
  19707. {
  19708. EXPECT_DECLS;
  19709. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  19710. DsaKey key;
  19711. int trusted = 0;
  19712. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19713. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19714. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19715. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19716. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19717. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19718. "80a0093113a8bd73";
  19719. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19720. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19721. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19722. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19723. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19724. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19725. "76341a7e7d9";
  19726. /* invalid p and q parameters */
  19727. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19728. const char* invalidQ = "96c5390a";
  19729. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19730. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL), 0);
  19731. /* test bad args */
  19732. /* null key struct */
  19733. ExpectIntEQ(wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL),
  19734. BAD_FUNC_ARG);
  19735. /* null param pointers */
  19736. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted,
  19737. NULL), BAD_FUNC_ARG);
  19738. /* illegal p length */
  19739. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL),
  19740. BAD_FUNC_ARG);
  19741. /* illegal q length */
  19742. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL),
  19743. BAD_FUNC_ARG);
  19744. wc_FreeDsaKey(&key);
  19745. #endif
  19746. return EXPECT_RESULT();
  19747. } /* END test_wc_DsaImportParamsRawCheck */
  19748. /*
  19749. * Testing wc_DsaExportParamsRaw()
  19750. */
  19751. static int test_wc_DsaExportParamsRaw(void)
  19752. {
  19753. EXPECT_DECLS;
  19754. #if !defined(NO_DSA)
  19755. DsaKey key;
  19756. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19757. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19758. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19759. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19760. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19761. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19762. "80a0093113a8bd73";
  19763. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19764. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19765. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19766. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19767. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19768. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19769. "76341a7e7d9";
  19770. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  19771. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  19772. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  19773. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  19774. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  19775. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  19776. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  19777. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  19778. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  19779. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  19780. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  19781. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  19782. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  19783. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  19784. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  19785. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  19786. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  19787. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  19788. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  19789. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  19790. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  19791. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  19792. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  19793. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  19794. byte pOut[MAX_DSA_PARAM_SIZE];
  19795. byte qOut[MAX_DSA_PARAM_SIZE];
  19796. byte gOut[MAX_DSA_PARAM_SIZE];
  19797. word32 pOutSz;
  19798. word32 qOutSz;
  19799. word32 gOutSz;
  19800. XMEMSET(&key, 0, sizeof(DsaKey));
  19801. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19802. /* first test using imported raw parameters, for expected */
  19803. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  19804. pOutSz = sizeof(pOut);
  19805. qOutSz = sizeof(qOut);
  19806. gOutSz = sizeof(gOut);
  19807. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19808. &gOutSz), 0);
  19809. /* validate exported parameters are correct */
  19810. ExpectIntEQ(XMEMCMP(pOut, pCompare, pOutSz), 0);
  19811. ExpectIntEQ(XMEMCMP(qOut, qCompare, qOutSz), 0);
  19812. ExpectIntEQ(XMEMCMP(gOut, gCompare, gOutSz), 0);
  19813. /* test bad args */
  19814. /* null key struct */
  19815. ExpectIntEQ(wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19816. &gOutSz), BAD_FUNC_ARG);
  19817. /* null output pointers */
  19818. ExpectIntEQ(wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz, NULL,
  19819. &gOutSz), LENGTH_ONLY_E);
  19820. /* null output size pointers */
  19821. ExpectIntEQ( wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL, gOut,
  19822. NULL), BAD_FUNC_ARG);
  19823. /* p output buffer size too small */
  19824. pOutSz = 1;
  19825. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19826. &gOutSz), BUFFER_E);
  19827. pOutSz = sizeof(pOut);
  19828. /* q output buffer size too small */
  19829. qOutSz = 1;
  19830. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19831. &gOutSz), BUFFER_E);
  19832. qOutSz = sizeof(qOut);
  19833. /* g output buffer size too small */
  19834. gOutSz = 1;
  19835. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19836. &gOutSz), BUFFER_E);
  19837. wc_FreeDsaKey(&key);
  19838. #endif
  19839. return EXPECT_RESULT();
  19840. } /* END test_wc_DsaExportParamsRaw */
  19841. /*
  19842. * Testing wc_DsaExportKeyRaw()
  19843. */
  19844. static int test_wc_DsaExportKeyRaw(void)
  19845. {
  19846. EXPECT_DECLS;
  19847. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19848. DsaKey key;
  19849. WC_RNG rng;
  19850. byte xOut[MAX_DSA_PARAM_SIZE];
  19851. byte yOut[MAX_DSA_PARAM_SIZE];
  19852. word32 xOutSz, yOutSz;
  19853. XMEMSET(&key, 0, sizeof(key));
  19854. XMEMSET(&rng, 0, sizeof(rng));
  19855. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19856. ExpectIntEQ(wc_InitRng(&rng), 0);
  19857. ExpectIntEQ(wc_MakeDsaParameters(&rng, 1024, &key), 0);
  19858. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  19859. /* try successful export */
  19860. xOutSz = sizeof(xOut);
  19861. yOutSz = sizeof(yOut);
  19862. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz), 0);
  19863. /* test bad args */
  19864. /* null key struct */
  19865. ExpectIntEQ(wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz),
  19866. BAD_FUNC_ARG);
  19867. /* null output pointers */
  19868. ExpectIntEQ(wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz),
  19869. LENGTH_ONLY_E);
  19870. /* null output size pointers */
  19871. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL),
  19872. BAD_FUNC_ARG);
  19873. /* x output buffer size too small */
  19874. xOutSz = 1;
  19875. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  19876. BUFFER_E);
  19877. xOutSz = sizeof(xOut);
  19878. /* y output buffer size too small */
  19879. yOutSz = 1;
  19880. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  19881. BUFFER_E);
  19882. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19883. wc_FreeDsaKey(&key);
  19884. #endif
  19885. return EXPECT_RESULT();
  19886. } /* END test_wc_DsaExportParamsRaw */
  19887. /*
  19888. * Testing wc_ed25519_make_key().
  19889. */
  19890. static int test_wc_ed25519_make_key(void)
  19891. {
  19892. EXPECT_DECLS;
  19893. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_MAKE_KEY)
  19894. ed25519_key key;
  19895. WC_RNG rng;
  19896. unsigned char pubkey[ED25519_PUB_KEY_SIZE+1];
  19897. int pubkey_sz = ED25519_PUB_KEY_SIZE;
  19898. XMEMSET(&key, 0, sizeof(ed25519_key));
  19899. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19900. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19901. ExpectIntEQ(wc_InitRng(&rng), 0);
  19902. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey, pubkey_sz),
  19903. ECC_PRIV_KEY_E);
  19904. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey+1, pubkey_sz),
  19905. ECC_PRIV_KEY_E);
  19906. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19907. /* Test bad args. */
  19908. ExpectIntEQ(wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key),
  19909. BAD_FUNC_ARG);
  19910. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL),
  19911. BAD_FUNC_ARG);
  19912. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key),
  19913. BAD_FUNC_ARG);
  19914. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key),
  19915. BAD_FUNC_ARG);
  19916. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19917. wc_ed25519_free(&key);
  19918. #endif
  19919. return EXPECT_RESULT();
  19920. } /* END test_wc_ed25519_make_key */
  19921. /*
  19922. * Testing wc_ed25519_init()
  19923. */
  19924. static int test_wc_ed25519_init(void)
  19925. {
  19926. EXPECT_DECLS;
  19927. #if defined(HAVE_ED25519)
  19928. ed25519_key key;
  19929. XMEMSET(&key, 0, sizeof(ed25519_key));
  19930. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19931. /* Test bad args. */
  19932. ExpectIntEQ(wc_ed25519_init(NULL), BAD_FUNC_ARG);
  19933. wc_ed25519_free(&key);
  19934. #endif
  19935. return EXPECT_RESULT();
  19936. } /* END test_wc_ed25519_init */
  19937. /*
  19938. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  19939. */
  19940. static int test_wc_ed25519_sign_msg(void)
  19941. {
  19942. EXPECT_DECLS;
  19943. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  19944. WC_RNG rng;
  19945. ed25519_key key;
  19946. byte msg[] = "Everybody gets Friday off.\n";
  19947. byte sig[ED25519_SIG_SIZE+1];
  19948. word32 msglen = sizeof(msg);
  19949. word32 siglen = ED25519_SIG_SIZE;
  19950. word32 badSigLen = ED25519_SIG_SIZE - 1;
  19951. #ifdef HAVE_ED25519_VERIFY
  19952. int verify_ok = 0; /*1 = Verify success.*/
  19953. #endif
  19954. /* Initialize stack variables. */
  19955. XMEMSET(&key, 0, sizeof(ed25519_key));
  19956. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19957. XMEMSET(sig, 0, sizeof(sig));
  19958. /* Initialize key. */
  19959. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19960. ExpectIntEQ(wc_InitRng(&rng), 0);
  19961. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19962. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key), 0);
  19963. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  19964. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig+1, &siglen, &key), 0);
  19965. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  19966. /* Test bad args. */
  19967. ExpectIntEQ(wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key),
  19968. BAD_FUNC_ARG);
  19969. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key),
  19970. BAD_FUNC_ARG);
  19971. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key),
  19972. BAD_FUNC_ARG);
  19973. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL),
  19974. BAD_FUNC_ARG);
  19975. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key),
  19976. BUFFER_E);
  19977. ExpectIntEQ(badSigLen, ED25519_SIG_SIZE);
  19978. badSigLen -= 1;
  19979. #ifdef HAVE_ED25519_VERIFY
  19980. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  19981. &key), 0);
  19982. ExpectIntEQ(verify_ok, 1);
  19983. /* Test bad args. */
  19984. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen - 1, msg, msglen,
  19985. &verify_ok, &key), BAD_FUNC_ARG);
  19986. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen + 1, msg, msglen,
  19987. &verify_ok, &key), BAD_FUNC_ARG);
  19988. ExpectIntEQ(wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  19989. &key), BAD_FUNC_ARG);
  19990. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, NULL, msglen, &verify_ok,
  19991. &key), BAD_FUNC_ARG);
  19992. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, NULL, &key),
  19993. BAD_FUNC_ARG);
  19994. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  19995. NULL), BAD_FUNC_ARG);
  19996. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, badSigLen, msg, msglen, &verify_ok,
  19997. &key), BAD_FUNC_ARG);
  19998. #endif /* Verify. */
  19999. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20000. wc_ed25519_free(&key);
  20001. #endif
  20002. return EXPECT_RESULT();
  20003. } /* END test_wc_ed25519_sign_msg */
  20004. /*
  20005. * Testing wc_ed25519_import_public()
  20006. */
  20007. static int test_wc_ed25519_import_public(void)
  20008. {
  20009. EXPECT_DECLS;
  20010. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  20011. ed25519_key pubKey;
  20012. WC_RNG rng;
  20013. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  20014. word32 inlen = sizeof(in);
  20015. XMEMSET(&pubKey, 0, sizeof(ed25519_key));
  20016. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20017. ExpectIntEQ(wc_ed25519_init(&pubKey), 0);
  20018. ExpectIntEQ(wc_InitRng(&rng), 0);
  20019. #ifdef HAVE_ED25519_MAKE_KEY
  20020. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey), 0);
  20021. #endif
  20022. ExpectIntEQ(wc_ed25519_import_public_ex(in, inlen, &pubKey, 1), 0);
  20023. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  20024. /* Test bad args. */
  20025. ExpectIntEQ(wc_ed25519_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  20026. ExpectIntEQ(wc_ed25519_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  20027. ExpectIntEQ(wc_ed25519_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  20028. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20029. wc_ed25519_free(&pubKey);
  20030. #endif
  20031. return EXPECT_RESULT();
  20032. } /* END wc_ed25519_import_public */
  20033. /*
  20034. * Testing wc_ed25519_import_private_key()
  20035. */
  20036. static int test_wc_ed25519_import_private_key(void)
  20037. {
  20038. EXPECT_DECLS;
  20039. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  20040. ed25519_key key;
  20041. WC_RNG rng;
  20042. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  20043. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  20044. word32 privKeySz = sizeof(privKey);
  20045. word32 pubKeySz = sizeof(pubKey);
  20046. #ifdef HAVE_ED25519_KEY_EXPORT
  20047. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  20048. word32 bothKeysSz = sizeof(bothKeys);
  20049. #endif
  20050. XMEMSET(&key, 0, sizeof(ed25519_key));
  20051. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20052. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20053. ExpectIntEQ(wc_InitRng(&rng), 0);
  20054. #ifdef HAVE_ED25519_MAKE_KEY
  20055. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20056. #endif
  20057. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  20058. pubKeySz, &key, 1), 0);
  20059. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20060. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20061. #ifdef HAVE_ED25519_KEY_EXPORT
  20062. PRIVATE_KEY_UNLOCK();
  20063. ExpectIntEQ(wc_ed25519_export_private(&key, bothKeys, &bothKeysSz), 0);
  20064. PRIVATE_KEY_LOCK();
  20065. ExpectIntEQ(wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  20066. &key, 1), 0);
  20067. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20068. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20069. #endif
  20070. /* Test bad args. */
  20071. ExpectIntEQ(wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  20072. &key), BAD_FUNC_ARG);
  20073. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  20074. pubKeySz, &key), BAD_FUNC_ARG);
  20075. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  20076. pubKeySz, NULL), BAD_FUNC_ARG);
  20077. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  20078. pubKeySz, &key), BAD_FUNC_ARG);
  20079. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  20080. pubKeySz - 1, &key), BAD_FUNC_ARG);
  20081. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL, 0,
  20082. &key), BAD_FUNC_ARG);
  20083. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20084. wc_ed25519_free(&key);
  20085. #endif
  20086. return EXPECT_RESULT();
  20087. } /* END test_wc_ed25519_import_private_key */
  20088. /*
  20089. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  20090. */
  20091. static int test_wc_ed25519_export(void)
  20092. {
  20093. EXPECT_DECLS;
  20094. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20095. ed25519_key key;
  20096. WC_RNG rng;
  20097. byte priv[ED25519_PRV_KEY_SIZE];
  20098. byte pub[ED25519_PUB_KEY_SIZE];
  20099. word32 privSz = sizeof(priv);
  20100. word32 pubSz = sizeof(pub);
  20101. #ifndef HAVE_ED25519_MAKE_KEY
  20102. const byte privKey[] = {
  20103. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20104. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20105. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20106. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20107. };
  20108. const byte pubKey[] = {
  20109. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20110. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20111. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20112. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20113. };
  20114. #endif
  20115. XMEMSET(&key, 0, sizeof(ed25519_key));
  20116. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20117. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20118. ExpectIntEQ(wc_InitRng(&rng), 0);
  20119. #ifdef HAVE_ED25519_MAKE_KEY
  20120. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20121. #else
  20122. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20123. pubKey, sizeof(pubKey), &key, 1), 0);
  20124. #endif
  20125. PRIVATE_KEY_UNLOCK();
  20126. ExpectIntEQ(wc_ed25519_export_public(&key, pub, &pubSz), 0);
  20127. ExpectIntEQ(pubSz, ED25519_KEY_SIZE);
  20128. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  20129. /* Test bad args. */
  20130. ExpectIntEQ(wc_ed25519_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  20131. ExpectIntEQ(wc_ed25519_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  20132. ExpectIntEQ(wc_ed25519_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  20133. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, &privSz), 0);
  20134. ExpectIntEQ(privSz, ED25519_KEY_SIZE);
  20135. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  20136. /* Test bad args. */
  20137. ExpectIntEQ(wc_ed25519_export_private_only(NULL, priv, &privSz),
  20138. BAD_FUNC_ARG);
  20139. ExpectIntEQ(wc_ed25519_export_private_only(&key, NULL, &privSz),
  20140. BAD_FUNC_ARG);
  20141. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, NULL),
  20142. BAD_FUNC_ARG);
  20143. PRIVATE_KEY_LOCK();
  20144. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20145. wc_ed25519_free(&key);
  20146. #endif
  20147. return EXPECT_RESULT();
  20148. } /* END test_wc_ed25519_export */
  20149. /*
  20150. * Testing wc_ed25519_size()
  20151. */
  20152. static int test_wc_ed25519_size(void)
  20153. {
  20154. EXPECT_DECLS;
  20155. #if defined(HAVE_ED25519)
  20156. ed25519_key key;
  20157. WC_RNG rng;
  20158. #ifndef HAVE_ED25519_MAKE_KEY
  20159. const byte privKey[] = {
  20160. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20161. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20162. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20163. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20164. };
  20165. const byte pubKey[] = {
  20166. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20167. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20168. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20169. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20170. };
  20171. #endif
  20172. XMEMSET(&key, 0, sizeof(ed25519_key));
  20173. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20174. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20175. ExpectIntEQ(wc_InitRng(&rng), 0);
  20176. #ifdef HAVE_ED25519_MAKE_KEY
  20177. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20178. #else
  20179. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20180. pubKey, sizeof(pubKey), &key, 1), 0);
  20181. #endif
  20182. ExpectIntEQ(wc_ed25519_size(&key), ED25519_KEY_SIZE);
  20183. /* Test bad args. */
  20184. ExpectIntEQ(wc_ed25519_size(NULL), BAD_FUNC_ARG);
  20185. ExpectIntEQ(wc_ed25519_sig_size(&key), ED25519_SIG_SIZE);
  20186. /* Test bad args. */
  20187. ExpectIntEQ(wc_ed25519_sig_size(NULL), BAD_FUNC_ARG);
  20188. ExpectIntEQ(wc_ed25519_pub_size(&key), ED25519_PUB_KEY_SIZE);
  20189. /* Test bad args. */
  20190. ExpectIntEQ(wc_ed25519_pub_size(NULL), BAD_FUNC_ARG);
  20191. ExpectIntEQ(wc_ed25519_priv_size(&key), ED25519_PRV_KEY_SIZE);
  20192. /* Test bad args. */
  20193. ExpectIntEQ(wc_ed25519_priv_size(NULL), BAD_FUNC_ARG);
  20194. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20195. wc_ed25519_free(&key);
  20196. #endif
  20197. return EXPECT_RESULT();
  20198. } /* END test_wc_ed25519_size */
  20199. /*
  20200. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  20201. */
  20202. static int test_wc_ed25519_exportKey(void)
  20203. {
  20204. EXPECT_DECLS;
  20205. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20206. WC_RNG rng;
  20207. ed25519_key key;
  20208. byte priv[ED25519_PRV_KEY_SIZE];
  20209. byte pub[ED25519_PUB_KEY_SIZE];
  20210. byte privOnly[ED25519_PRV_KEY_SIZE];
  20211. word32 privSz = sizeof(priv);
  20212. word32 pubSz = sizeof(pub);
  20213. word32 privOnlySz = sizeof(privOnly);
  20214. #ifndef HAVE_ED25519_MAKE_KEY
  20215. const byte privKey[] = {
  20216. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20217. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20218. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20219. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20220. };
  20221. const byte pubKey[] = {
  20222. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20223. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20224. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20225. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20226. };
  20227. #endif
  20228. XMEMSET(&key, 0, sizeof(ed25519_key));
  20229. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20230. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20231. ExpectIntEQ(wc_InitRng(&rng), 0);
  20232. #ifdef HAVE_ED25519_MAKE_KEY
  20233. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20234. #else
  20235. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20236. pubKey, sizeof(pubKey), &key, 1), 0);
  20237. #endif
  20238. PRIVATE_KEY_UNLOCK();
  20239. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, &privOnlySz), 0);
  20240. /* Test bad args. */
  20241. ExpectIntEQ(wc_ed25519_export_private(NULL, privOnly, &privOnlySz),
  20242. BAD_FUNC_ARG);
  20243. ExpectIntEQ(wc_ed25519_export_private(&key, NULL, &privOnlySz),
  20244. BAD_FUNC_ARG);
  20245. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  20246. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  20247. /* Test bad args. */
  20248. ExpectIntEQ(wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz),
  20249. BAD_FUNC_ARG);
  20250. ExpectIntEQ(wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz),
  20251. BAD_FUNC_ARG);
  20252. ExpectIntEQ(wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz),
  20253. BAD_FUNC_ARG);
  20254. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz),
  20255. BAD_FUNC_ARG);
  20256. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, NULL),
  20257. BAD_FUNC_ARG);
  20258. PRIVATE_KEY_LOCK();
  20259. /* Cross check output. */
  20260. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  20261. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20262. wc_ed25519_free(&key);
  20263. #endif
  20264. return EXPECT_RESULT();
  20265. } /* END test_wc_ed25519_exportKey */
  20266. /*
  20267. * Testing wc_Ed25519PublicKeyToDer
  20268. */
  20269. static int test_wc_Ed25519PublicKeyToDer(void)
  20270. {
  20271. EXPECT_DECLS;
  20272. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  20273. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20274. ed25519_key key;
  20275. byte derBuf[1024];
  20276. XMEMSET(&key, 0, sizeof(ed25519_key));
  20277. /* Test bad args */
  20278. ExpectIntEQ(wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  20279. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20280. ExpectIntEQ(wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  20281. wc_ed25519_free(&key);
  20282. /* Test good args */
  20283. if (EXPECT_SUCCESS()) {
  20284. WC_RNG rng;
  20285. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20286. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20287. ExpectIntEQ(wc_InitRng(&rng), 0);
  20288. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20289. ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  20290. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20291. wc_ed25519_free(&key);
  20292. }
  20293. #endif
  20294. return EXPECT_RESULT();
  20295. } /* END testing wc_Ed25519PublicKeyToDer */
  20296. /*
  20297. * Testing wc_curve25519_init and wc_curve25519_free.
  20298. */
  20299. static int test_wc_curve25519_init(void)
  20300. {
  20301. EXPECT_DECLS;
  20302. #if defined(HAVE_CURVE25519)
  20303. curve25519_key key;
  20304. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20305. /* Test bad args for wc_curve25519_init */
  20306. ExpectIntEQ(wc_curve25519_init(NULL), BAD_FUNC_ARG);
  20307. /* Test good args for wc_curve_25519_free */
  20308. wc_curve25519_free(&key);
  20309. /* Test bad args for wc_curve25519 free. */
  20310. wc_curve25519_free(NULL);
  20311. #endif
  20312. return EXPECT_RESULT();
  20313. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  20314. /*
  20315. * Testing test_wc_curve25519_size.
  20316. */
  20317. static int test_wc_curve25519_size(void)
  20318. {
  20319. EXPECT_DECLS;
  20320. #if defined(HAVE_CURVE25519)
  20321. curve25519_key key;
  20322. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20323. /* Test good args for wc_curve25519_size */
  20324. ExpectIntEQ(wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  20325. /* Test bad args for wc_curve25519_size */
  20326. ExpectIntEQ(wc_curve25519_size(NULL), 0);
  20327. wc_curve25519_free(&key);
  20328. #endif
  20329. return EXPECT_RESULT();
  20330. } /* END test_wc_curve25519_size*/
  20331. /*
  20332. * Testing test_wc_curve25519_export_key_raw().
  20333. */
  20334. static int test_wc_curve25519_export_key_raw(void)
  20335. {
  20336. EXPECT_DECLS;
  20337. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20338. curve25519_key key;
  20339. WC_RNG rng;
  20340. byte privateKey[CURVE25519_KEYSIZE];
  20341. byte publicKey[CURVE25519_KEYSIZE];
  20342. word32 prvkSz;
  20343. word32 pubkSz;
  20344. byte prik[CURVE25519_KEYSIZE];
  20345. byte pubk[CURVE25519_KEYSIZE];
  20346. word32 prksz;
  20347. word32 pbksz;
  20348. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20349. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20350. ExpectIntEQ(wc_InitRng(&rng), 0);
  20351. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20352. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  20353. prvkSz = CURVE25519_KEYSIZE;
  20354. pubkSz = CURVE25519_KEYSIZE;
  20355. ExpectIntEQ(wc_curve25519_export_key_raw(NULL, privateKey, &prvkSz,
  20356. publicKey, &pubkSz), BAD_FUNC_ARG);
  20357. prvkSz = CURVE25519_KEYSIZE;
  20358. pubkSz = CURVE25519_KEYSIZE;
  20359. ExpectIntEQ(wc_curve25519_export_key_raw(&key, NULL, &prvkSz, publicKey,
  20360. &pubkSz), BAD_FUNC_ARG);
  20361. prvkSz = CURVE25519_KEYSIZE;
  20362. pubkSz = CURVE25519_KEYSIZE;
  20363. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, NULL,
  20364. publicKey, &pubkSz), BAD_FUNC_ARG);
  20365. /* prvkSz = CURVE25519_KEYSIZE; */
  20366. pubkSz = CURVE25519_KEYSIZE;
  20367. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20368. NULL, &pubkSz), BAD_FUNC_ARG);
  20369. prvkSz = CURVE25519_KEYSIZE;
  20370. pubkSz = CURVE25519_KEYSIZE;
  20371. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20372. publicKey, NULL), BAD_FUNC_ARG);
  20373. /* cross-testing */
  20374. prksz = CURVE25519_KEYSIZE;
  20375. ExpectIntEQ(wc_curve25519_export_private_raw(&key, prik, &prksz), 0);
  20376. pbksz = CURVE25519_KEYSIZE;
  20377. ExpectIntEQ(wc_curve25519_export_public(&key, pubk, &pbksz), 0);
  20378. prvkSz = CURVE25519_KEYSIZE;
  20379. /* pubkSz = CURVE25519_KEYSIZE; */
  20380. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20381. publicKey, &pubkSz), 0);
  20382. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  20383. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  20384. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20385. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20386. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  20387. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  20388. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20389. wc_curve25519_free(&key);
  20390. #endif
  20391. return EXPECT_RESULT();
  20392. } /* end of test_wc_curve25519_export_key_raw */
  20393. /*
  20394. * Testing test_wc_curve25519_export_key_raw_ex().
  20395. */
  20396. static int test_wc_curve25519_export_key_raw_ex(void)
  20397. {
  20398. EXPECT_DECLS;
  20399. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20400. curve25519_key key;
  20401. WC_RNG rng;
  20402. byte privateKey[CURVE25519_KEYSIZE];
  20403. byte publicKey[CURVE25519_KEYSIZE];
  20404. word32 prvkSz;
  20405. word32 pubkSz;
  20406. byte prik[CURVE25519_KEYSIZE];
  20407. byte pubk[CURVE25519_KEYSIZE];
  20408. word32 prksz;
  20409. word32 pbksz;
  20410. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20411. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20412. ExpectIntEQ(wc_InitRng(&rng), 0);
  20413. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20414. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  20415. prvkSz = CURVE25519_KEYSIZE;
  20416. pubkSz = CURVE25519_KEYSIZE;
  20417. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  20418. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20419. prvkSz = CURVE25519_KEYSIZE;
  20420. pubkSz = CURVE25519_KEYSIZE;
  20421. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  20422. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20423. prvkSz = CURVE25519_KEYSIZE;
  20424. pubkSz = CURVE25519_KEYSIZE;
  20425. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20426. NULL, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20427. /* prvkSz = CURVE25519_KEYSIZE; */
  20428. pubkSz = CURVE25519_KEYSIZE;
  20429. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20430. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20431. prvkSz = CURVE25519_KEYSIZE;
  20432. pubkSz = CURVE25519_KEYSIZE;
  20433. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20434. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20435. prvkSz = CURVE25519_KEYSIZE;
  20436. /* pubkSz = CURVE25519_KEYSIZE; */
  20437. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  20438. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20439. prvkSz = CURVE25519_KEYSIZE;
  20440. pubkSz = CURVE25519_KEYSIZE;
  20441. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  20442. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20443. prvkSz = CURVE25519_KEYSIZE;
  20444. pubkSz = CURVE25519_KEYSIZE;
  20445. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20446. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20447. /* prvkSz = CURVE25519_KEYSIZE; */
  20448. pubkSz = CURVE25519_KEYSIZE;
  20449. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20450. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20451. prvkSz = CURVE25519_KEYSIZE;
  20452. pubkSz = CURVE25519_KEYSIZE;
  20453. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20454. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20455. /* illegal value for endian */
  20456. prvkSz = CURVE25519_KEYSIZE;
  20457. /* pubkSz = CURVE25519_KEYSIZE; */
  20458. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20459. publicKey, NULL, EC25519_BIG_ENDIAN + 10), BAD_FUNC_ARG);
  20460. /* cross-testing */
  20461. prksz = CURVE25519_KEYSIZE;
  20462. ExpectIntEQ(wc_curve25519_export_private_raw( &key, prik, &prksz), 0);
  20463. pbksz = CURVE25519_KEYSIZE;
  20464. ExpectIntEQ(wc_curve25519_export_public( &key, pubk, &pbksz), 0);
  20465. prvkSz = CURVE25519_KEYSIZE;
  20466. /* pubkSz = CURVE25519_KEYSIZE; */
  20467. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20468. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  20469. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  20470. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  20471. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20472. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20473. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  20474. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  20475. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20476. publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), 0);
  20477. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20478. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20479. /* try once with another endian */
  20480. prvkSz = CURVE25519_KEYSIZE;
  20481. pubkSz = CURVE25519_KEYSIZE;
  20482. ExpectIntEQ(wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  20483. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  20484. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20485. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20486. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20487. wc_curve25519_free(&key);
  20488. #endif
  20489. return EXPECT_RESULT();
  20490. } /* end of test_wc_curve25519_export_key_raw_ex */
  20491. /*
  20492. * Testing wc_curve25519_make_key
  20493. */
  20494. static int test_wc_curve25519_make_key(void)
  20495. {
  20496. EXPECT_DECLS;
  20497. #if defined(HAVE_CURVE25519)
  20498. curve25519_key key;
  20499. WC_RNG rng;
  20500. int keysize;
  20501. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20502. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20503. ExpectIntEQ(wc_InitRng(&rng), 0);
  20504. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20505. ExpectIntEQ(keysize = wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  20506. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, &key), 0);
  20507. /* test bad cases*/
  20508. ExpectIntEQ(wc_curve25519_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  20509. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  20510. ExpectIntEQ(wc_curve25519_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  20511. ExpectIntEQ(wc_curve25519_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  20512. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20513. wc_curve25519_free(&key);
  20514. #endif
  20515. return EXPECT_RESULT();
  20516. } /* END test_wc_curve25519_make_key*/
  20517. /*
  20518. * Testing wc_curve25519_shared_secret_ex
  20519. */
  20520. static int test_wc_curve25519_shared_secret_ex(void)
  20521. {
  20522. EXPECT_DECLS;
  20523. #if defined(HAVE_CURVE25519)
  20524. curve25519_key private_key;
  20525. curve25519_key public_key;
  20526. WC_RNG rng;
  20527. byte out[CURVE25519_KEYSIZE];
  20528. word32 outLen = sizeof(out);
  20529. int endian = EC25519_BIG_ENDIAN;
  20530. ExpectIntEQ(wc_curve25519_init(&private_key), 0);
  20531. ExpectIntEQ(wc_curve25519_init(&public_key), 0);
  20532. ExpectIntEQ(wc_InitRng(&rng), 0);
  20533. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key),
  20534. 0);
  20535. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key),
  20536. 0);
  20537. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20538. &outLen, endian), 0);
  20539. /* test bad cases*/
  20540. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  20541. BAD_FUNC_ARG);
  20542. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, &public_key, out, &outLen,
  20543. endian), BAD_FUNC_ARG);
  20544. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, NULL, out, &outLen,
  20545. endian), BAD_FUNC_ARG);
  20546. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  20547. &outLen, endian), BAD_FUNC_ARG);
  20548. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20549. NULL, endian), BAD_FUNC_ARG);
  20550. /* curve25519.c is checking for public_key size less than or equal to 0x7f,
  20551. * increasing to 0x8f checks for error being returned*/
  20552. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  20553. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20554. &outLen, endian), ECC_BAD_ARG_E);
  20555. outLen = outLen - 2;
  20556. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20557. &outLen, endian), BAD_FUNC_ARG);
  20558. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20559. wc_curve25519_free(&private_key);
  20560. wc_curve25519_free(&public_key);
  20561. #endif
  20562. return EXPECT_RESULT();
  20563. } /* END test_wc_curve25519_shared_secret_ex*/
  20564. /*
  20565. * Testing wc_curve25519_make_pub
  20566. */
  20567. static int test_wc_curve25519_make_pub(void)
  20568. {
  20569. EXPECT_DECLS;
  20570. #ifdef HAVE_CURVE25519
  20571. curve25519_key key;
  20572. WC_RNG rng;
  20573. byte out[CURVE25519_KEYSIZE];
  20574. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20575. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20576. ExpectIntEQ(wc_InitRng(&rng), 0);
  20577. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20578. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(out), out,
  20579. (int)sizeof(key.k), key.k), 0);
  20580. /* test bad cases*/
  20581. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k,
  20582. (int)sizeof out, out), ECC_BAD_ARG_E);
  20583. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20584. NULL), ECC_BAD_ARG_E);
  20585. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out - 1, out,
  20586. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  20587. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, NULL,
  20588. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  20589. /* verify clamping test */
  20590. key.k[0] |= ~248;
  20591. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20592. key.k), ECC_BAD_ARG_E);
  20593. key.k[0] &= 248;
  20594. /* repeat the expected-to-succeed test. */
  20595. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20596. key.k), 0);
  20597. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20598. wc_curve25519_free(&key);
  20599. #endif
  20600. return EXPECT_RESULT();
  20601. } /* END test_wc_curve25519_make_pub */
  20602. /*
  20603. * Testing test_wc_curve25519_export_public_ex
  20604. */
  20605. static int test_wc_curve25519_export_public_ex(void)
  20606. {
  20607. EXPECT_DECLS;
  20608. #if defined(HAVE_CURVE25519)
  20609. curve25519_key key;
  20610. WC_RNG rng;
  20611. byte out[CURVE25519_KEYSIZE];
  20612. word32 outLen = sizeof(out);
  20613. int endian = EC25519_BIG_ENDIAN;
  20614. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20615. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20616. ExpectIntEQ(wc_InitRng(&rng), 0);
  20617. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20618. ExpectIntEQ(wc_curve25519_export_public(&key, out, &outLen), 0);
  20619. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian), 0);
  20620. /* test bad cases*/
  20621. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, NULL, NULL, endian),
  20622. BAD_FUNC_ARG);
  20623. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, out, &outLen, endian),
  20624. BAD_FUNC_ARG);
  20625. ExpectIntEQ(wc_curve25519_export_public_ex(&key, NULL, &outLen, endian),
  20626. BAD_FUNC_ARG);
  20627. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, NULL, endian),
  20628. BAD_FUNC_ARG);
  20629. outLen = outLen - 2;
  20630. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian),
  20631. ECC_BAD_ARG_E);
  20632. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20633. wc_curve25519_free(&key);
  20634. #endif
  20635. return EXPECT_RESULT();
  20636. } /* END test_wc_curve25519_export_public_ex*/
  20637. /*
  20638. * Testing test_wc_curve25519_import_private_raw_ex
  20639. */
  20640. static int test_wc_curve25519_import_private_raw_ex(void)
  20641. {
  20642. EXPECT_DECLS;
  20643. #if defined(HAVE_CURVE25519)
  20644. curve25519_key key;
  20645. WC_RNG rng;
  20646. byte priv[CURVE25519_KEYSIZE];
  20647. byte pub[CURVE25519_KEYSIZE];
  20648. word32 privSz = sizeof(priv);
  20649. word32 pubSz = sizeof(pub);
  20650. int endian = EC25519_BIG_ENDIAN;
  20651. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20652. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20653. ExpectIntEQ(wc_InitRng(&rng), 0);
  20654. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20655. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, priv, &privSz,
  20656. endian), 0);
  20657. ExpectIntEQ(wc_curve25519_export_public(&key, pub, &pubSz), 0);
  20658. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20659. &key, endian), 0);
  20660. /* test bad cases*/
  20661. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  20662. endian), BAD_FUNC_ARG);
  20663. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20664. &key, endian), BAD_FUNC_ARG);
  20665. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20666. &key, endian), BAD_FUNC_ARG);
  20667. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20668. NULL, endian), BAD_FUNC_ARG);
  20669. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  20670. &key, endian), ECC_BAD_ARG_E);
  20671. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  20672. &key, endian), ECC_BAD_ARG_E);
  20673. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20674. &key, EC25519_LITTLE_ENDIAN), 0);
  20675. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20676. wc_curve25519_free(&key);
  20677. #endif
  20678. return EXPECT_RESULT();
  20679. } /* END test_wc_curve25519_import_private_raw_ex*/
  20680. /*
  20681. * Testing test_wc_curve25519_import_private
  20682. */
  20683. static int test_wc_curve25519_import_private(void)
  20684. {
  20685. EXPECT_DECLS;
  20686. #if defined(HAVE_CURVE25519)
  20687. curve25519_key key;
  20688. WC_RNG rng;
  20689. byte priv[CURVE25519_KEYSIZE];
  20690. word32 privSz = sizeof(priv);
  20691. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20692. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20693. ExpectIntEQ(wc_InitRng(&rng), 0);
  20694. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20695. ExpectIntEQ(wc_curve25519_export_private_raw(&key, priv, &privSz), 0);
  20696. ExpectIntEQ(wc_curve25519_import_private(priv, privSz, &key), 0);
  20697. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20698. wc_curve25519_free(&key);
  20699. #endif
  20700. return EXPECT_RESULT();
  20701. } /* END test_wc_curve25519_import*/
  20702. /*
  20703. * Testing test_wc_curve25519_export_private_raw_ex
  20704. */
  20705. static int test_wc_curve25519_export_private_raw_ex(void)
  20706. {
  20707. EXPECT_DECLS;
  20708. #if defined(HAVE_CURVE25519)
  20709. curve25519_key key;
  20710. byte out[CURVE25519_KEYSIZE];
  20711. word32 outLen = sizeof(out);
  20712. int endian = EC25519_BIG_ENDIAN;
  20713. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20714. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  20715. 0);
  20716. /* test bad cases*/
  20717. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian),
  20718. BAD_FUNC_ARG);
  20719. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian),
  20720. BAD_FUNC_ARG);
  20721. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, NULL, &outLen,
  20722. endian), BAD_FUNC_ARG);
  20723. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, NULL, endian),
  20724. BAD_FUNC_ARG);
  20725. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  20726. EC25519_LITTLE_ENDIAN), 0);
  20727. outLen = outLen - 2;
  20728. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  20729. ECC_BAD_ARG_E);
  20730. wc_curve25519_free(&key);
  20731. #endif
  20732. return EXPECT_RESULT();
  20733. } /* END test_wc_curve25519_export_private_raw_ex*/
  20734. /*
  20735. * Testing wc_ed448_make_key().
  20736. */
  20737. static int test_wc_ed448_make_key(void)
  20738. {
  20739. EXPECT_DECLS;
  20740. #if defined(HAVE_ED448)
  20741. ed448_key key;
  20742. WC_RNG rng;
  20743. unsigned char pubkey[ED448_PUB_KEY_SIZE];
  20744. XMEMSET(&key, 0, sizeof(ed448_key));
  20745. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20746. ExpectIntEQ(wc_ed448_init(&key), 0);
  20747. ExpectIntEQ(wc_InitRng(&rng), 0);
  20748. ExpectIntEQ(wc_ed448_make_public(&key, pubkey, sizeof(pubkey)),
  20749. ECC_PRIV_KEY_E);
  20750. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20751. /* Test bad args. */
  20752. ExpectIntEQ(wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key), BAD_FUNC_ARG);
  20753. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL), BAD_FUNC_ARG);
  20754. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key),
  20755. BAD_FUNC_ARG);
  20756. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key),
  20757. BAD_FUNC_ARG);
  20758. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20759. wc_ed448_free(&key);
  20760. #endif
  20761. return EXPECT_RESULT();
  20762. } /* END test_wc_ed448_make_key */
  20763. /*
  20764. * Testing wc_ed448_init()
  20765. */
  20766. static int test_wc_ed448_init(void)
  20767. {
  20768. EXPECT_DECLS;
  20769. #if defined(HAVE_ED448)
  20770. ed448_key key;
  20771. XMEMSET(&key, 0, sizeof(ed448_key));
  20772. ExpectIntEQ(wc_ed448_init(&key), 0);
  20773. /* Test bad args. */
  20774. ExpectIntEQ(wc_ed448_init(NULL), BAD_FUNC_ARG);
  20775. wc_ed448_free(&key);
  20776. #endif
  20777. return EXPECT_RESULT();
  20778. } /* END test_wc_ed448_init */
  20779. /*
  20780. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  20781. */
  20782. static int test_wc_ed448_sign_msg(void)
  20783. {
  20784. EXPECT_DECLS;
  20785. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  20786. ed448_key key;
  20787. WC_RNG rng;
  20788. byte msg[] = "Everybody gets Friday off.\n";
  20789. byte sig[ED448_SIG_SIZE];
  20790. word32 msglen = sizeof(msg);
  20791. word32 siglen = sizeof(sig);
  20792. word32 badSigLen = sizeof(sig) - 1;
  20793. #ifdef HAVE_ED448_VERIFY
  20794. int verify_ok = 0; /*1 = Verify success.*/
  20795. #endif
  20796. /* Initialize stack variables. */
  20797. XMEMSET(&key, 0, sizeof(ed448_key));
  20798. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20799. XMEMSET(sig, 0, siglen);
  20800. /* Initialize key. */
  20801. ExpectIntEQ(wc_ed448_init(&key), 0);
  20802. ExpectIntEQ(wc_InitRng(&rng), 0);
  20803. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20804. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0), 0);
  20805. ExpectIntEQ(siglen, ED448_SIG_SIZE);
  20806. /* Test bad args. */
  20807. ExpectIntEQ(wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0),
  20808. BAD_FUNC_ARG);
  20809. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0),
  20810. BAD_FUNC_ARG);
  20811. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0),
  20812. BAD_FUNC_ARG);
  20813. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0),
  20814. BAD_FUNC_ARG);
  20815. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key, NULL, 0),
  20816. BUFFER_E);
  20817. ExpectIntEQ(badSigLen, ED448_SIG_SIZE);
  20818. badSigLen -= 1;
  20819. #ifdef HAVE_ED448_VERIFY
  20820. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key,
  20821. NULL, 0), 0);
  20822. ExpectIntEQ(verify_ok, 1);
  20823. /* Test bad args. */
  20824. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg, msglen, &verify_ok,
  20825. &key, NULL, 0), BAD_FUNC_ARG);
  20826. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg, msglen, &verify_ok,
  20827. &key, NULL, 0), BAD_FUNC_ARG);
  20828. ExpectIntEQ(wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  20829. &key, NULL, 0), BAD_FUNC_ARG);
  20830. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, NULL, msglen, &verify_ok,
  20831. &key, NULL, 0), BAD_FUNC_ARG);
  20832. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, NULL,
  20833. &key, NULL, 0), BAD_FUNC_ARG);
  20834. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  20835. NULL, NULL, 0), BAD_FUNC_ARG);
  20836. ExpectIntEQ(wc_ed448_verify_msg(sig, badSigLen, msg, msglen, &verify_ok,
  20837. &key, NULL, 0), BAD_FUNC_ARG);
  20838. #endif /* Verify. */
  20839. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20840. wc_ed448_free(&key);
  20841. #endif
  20842. return EXPECT_RESULT();
  20843. } /* END test_wc_ed448_sign_msg */
  20844. /*
  20845. * Testing wc_ed448_import_public()
  20846. */
  20847. static int test_wc_ed448_import_public(void)
  20848. {
  20849. EXPECT_DECLS;
  20850. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20851. ed448_key pubKey;
  20852. WC_RNG rng;
  20853. const byte in[] =
  20854. "Ed448PublicKeyUnitTest.................................\n";
  20855. word32 inlen = sizeof(in);
  20856. XMEMSET(&pubKey, 0, sizeof(ed448_key));
  20857. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20858. ExpectIntEQ(wc_ed448_init(&pubKey), 0);
  20859. ExpectIntEQ(wc_InitRng(&rng), 0);
  20860. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey), 0);
  20861. ExpectIntEQ(wc_ed448_import_public_ex(in, inlen, &pubKey, 1), 0);
  20862. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  20863. /* Test bad args. */
  20864. ExpectIntEQ(wc_ed448_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  20865. ExpectIntEQ(wc_ed448_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  20866. ExpectIntEQ(wc_ed448_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  20867. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20868. wc_ed448_free(&pubKey);
  20869. #endif
  20870. return EXPECT_RESULT();
  20871. } /* END wc_ed448_import_public */
  20872. /*
  20873. * Testing wc_ed448_import_private_key()
  20874. */
  20875. static int test_wc_ed448_import_private_key(void)
  20876. {
  20877. EXPECT_DECLS;
  20878. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20879. ed448_key key;
  20880. WC_RNG rng;
  20881. const byte privKey[] =
  20882. "Ed448PrivateKeyUnitTest................................\n";
  20883. const byte pubKey[] =
  20884. "Ed448PublicKeyUnitTest.................................\n";
  20885. word32 privKeySz = sizeof(privKey);
  20886. word32 pubKeySz = sizeof(pubKey);
  20887. #ifdef HAVE_ED448_KEY_EXPORT
  20888. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  20889. word32 bothKeysSz = sizeof(bothKeys);
  20890. #endif
  20891. XMEMSET(&key, 0, sizeof(ed448_key));
  20892. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20893. ExpectIntEQ(wc_ed448_init(&key), 0);
  20894. ExpectIntEQ(wc_InitRng(&rng), 0);
  20895. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20896. ExpectIntEQ(wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  20897. pubKeySz, &key, 1), 0);
  20898. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20899. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20900. #ifdef HAVE_ED448_KEY_EXPORT
  20901. PRIVATE_KEY_UNLOCK();
  20902. ExpectIntEQ(wc_ed448_export_private(&key, bothKeys, &bothKeysSz), 0);
  20903. PRIVATE_KEY_LOCK();
  20904. ExpectIntEQ(wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  20905. &key, 1), 0);
  20906. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20907. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20908. #endif
  20909. /* Test bad args. */
  20910. ExpectIntEQ(wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  20911. &key), BAD_FUNC_ARG);
  20912. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, pubKeySz,
  20913. &key), BAD_FUNC_ARG);
  20914. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20915. pubKeySz, NULL), BAD_FUNC_ARG);
  20916. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  20917. pubKeySz, &key), BAD_FUNC_ARG);
  20918. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20919. pubKeySz - 1, &key), BAD_FUNC_ARG);
  20920. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, 0, &key),
  20921. BAD_FUNC_ARG);
  20922. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20923. wc_ed448_free(&key);
  20924. #endif
  20925. return EXPECT_RESULT();
  20926. } /* END test_wc_ed448_import_private_key */
  20927. /*
  20928. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  20929. */
  20930. static int test_wc_ed448_export(void)
  20931. {
  20932. EXPECT_DECLS;
  20933. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20934. ed448_key key;
  20935. WC_RNG rng;
  20936. byte priv[ED448_PRV_KEY_SIZE];
  20937. byte pub[ED448_PUB_KEY_SIZE];
  20938. word32 privSz = sizeof(priv);
  20939. word32 pubSz = sizeof(pub);
  20940. XMEMSET(&key, 0, sizeof(ed448_key));
  20941. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20942. ExpectIntEQ(wc_ed448_init(&key), 0);
  20943. ExpectIntEQ(wc_InitRng(&rng), 0);
  20944. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20945. ExpectIntEQ(wc_ed448_export_public(&key, pub, &pubSz), 0);
  20946. ExpectIntEQ(pubSz, ED448_KEY_SIZE);
  20947. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  20948. /* Test bad args. */
  20949. ExpectIntEQ(wc_ed448_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  20950. ExpectIntEQ(wc_ed448_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  20951. ExpectIntEQ(wc_ed448_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  20952. PRIVATE_KEY_UNLOCK();
  20953. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, &privSz), 0);
  20954. ExpectIntEQ(privSz, ED448_KEY_SIZE);
  20955. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  20956. /* Test bad args. */
  20957. ExpectIntEQ(wc_ed448_export_private_only(NULL, priv, &privSz),
  20958. BAD_FUNC_ARG);
  20959. ExpectIntEQ(wc_ed448_export_private_only(&key, NULL, &privSz),
  20960. BAD_FUNC_ARG);
  20961. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, NULL), BAD_FUNC_ARG);
  20962. PRIVATE_KEY_LOCK();
  20963. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20964. wc_ed448_free(&key);
  20965. #endif
  20966. return EXPECT_RESULT();
  20967. } /* END test_wc_ed448_export */
  20968. /*
  20969. * Testing wc_ed448_size()
  20970. */
  20971. static int test_wc_ed448_size(void)
  20972. {
  20973. EXPECT_DECLS;
  20974. #if defined(HAVE_ED448)
  20975. ed448_key key;
  20976. WC_RNG rng;
  20977. XMEMSET(&key, 0, sizeof(ed448_key));
  20978. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20979. ExpectIntEQ(wc_ed448_init(&key), 0);
  20980. ExpectIntEQ(wc_InitRng(&rng), 0);
  20981. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20982. ExpectIntEQ(wc_ed448_size(&key), ED448_KEY_SIZE);
  20983. /* Test bad args. */
  20984. ExpectIntEQ(wc_ed448_size(NULL), BAD_FUNC_ARG);
  20985. ExpectIntEQ(wc_ed448_sig_size(&key), ED448_SIG_SIZE);
  20986. /* Test bad args. */
  20987. ExpectIntEQ(wc_ed448_sig_size(NULL), BAD_FUNC_ARG);
  20988. ExpectIntEQ(wc_ed448_pub_size(&key), ED448_PUB_KEY_SIZE);
  20989. /* Test bad args. */
  20990. ExpectIntEQ(wc_ed448_pub_size(NULL), BAD_FUNC_ARG);
  20991. ExpectIntEQ(wc_ed448_priv_size(&key), ED448_PRV_KEY_SIZE);
  20992. /* Test bad args. */
  20993. ExpectIntEQ(wc_ed448_priv_size(NULL), BAD_FUNC_ARG);
  20994. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20995. wc_ed448_free(&key);
  20996. #endif
  20997. return EXPECT_RESULT();
  20998. } /* END test_wc_ed448_size */
  20999. /*
  21000. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  21001. */
  21002. static int test_wc_ed448_exportKey(void)
  21003. {
  21004. EXPECT_DECLS;
  21005. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21006. ed448_key key;
  21007. WC_RNG rng;
  21008. byte priv[ED448_PRV_KEY_SIZE];
  21009. byte pub[ED448_PUB_KEY_SIZE];
  21010. byte privOnly[ED448_PRV_KEY_SIZE];
  21011. word32 privSz = sizeof(priv);
  21012. word32 pubSz = sizeof(pub);
  21013. word32 privOnlySz = sizeof(privOnly);
  21014. XMEMSET(&key, 0, sizeof(ed448_key));
  21015. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21016. ExpectIntEQ(wc_ed448_init(&key), 0);
  21017. ExpectIntEQ(wc_InitRng(&rng), 0);
  21018. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21019. PRIVATE_KEY_UNLOCK();
  21020. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, &privOnlySz), 0);
  21021. /* Test bad args. */
  21022. ExpectIntEQ(wc_ed448_export_private(NULL, privOnly, &privOnlySz),
  21023. BAD_FUNC_ARG);
  21024. ExpectIntEQ(wc_ed448_export_private(&key, NULL, &privOnlySz), BAD_FUNC_ARG);
  21025. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  21026. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  21027. /* Test bad args. */
  21028. ExpectIntEQ(wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz),
  21029. BAD_FUNC_ARG);
  21030. ExpectIntEQ(wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz),
  21031. BAD_FUNC_ARG);
  21032. ExpectIntEQ(wc_ed448_export_key(&key, priv, NULL, pub, &pubSz),
  21033. BAD_FUNC_ARG);
  21034. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz),
  21035. BAD_FUNC_ARG);
  21036. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, NULL),
  21037. BAD_FUNC_ARG);
  21038. PRIVATE_KEY_LOCK();
  21039. /* Cross check output. */
  21040. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  21041. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21042. wc_ed448_free(&key);
  21043. #endif
  21044. return EXPECT_RESULT();
  21045. } /* END test_wc_ed448_exportKey */
  21046. /*
  21047. * Testing wc_Ed448PublicKeyToDer
  21048. */
  21049. static int test_wc_Ed448PublicKeyToDer(void)
  21050. {
  21051. EXPECT_DECLS;
  21052. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  21053. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  21054. ed448_key key;
  21055. byte derBuf[1024];
  21056. XMEMSET(&key, 0, sizeof(ed448_key));
  21057. /* Test bad args */
  21058. ExpectIntEQ(wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  21059. ExpectIntEQ(wc_ed448_init(&key), 0);
  21060. ExpectIntEQ(wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  21061. wc_ed448_free(&key);
  21062. /* Test good args */
  21063. if (EXPECT_SUCCESS()) {
  21064. WC_RNG rng;
  21065. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21066. ExpectIntEQ(wc_ed448_init(&key), 0);
  21067. ExpectIntEQ(wc_InitRng(&rng), 0);
  21068. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21069. ExpectIntGT(wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  21070. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21071. wc_ed448_free(&key);
  21072. }
  21073. #endif
  21074. return EXPECT_RESULT();
  21075. } /* END testing wc_Ed448PublicKeyToDer */
  21076. /*
  21077. * Testing wc_curve448_init and wc_curve448_free.
  21078. */
  21079. static int test_wc_curve448_init(void)
  21080. {
  21081. EXPECT_DECLS;
  21082. #if defined(HAVE_CURVE448)
  21083. curve448_key key;
  21084. /* Test bad args for wc_curve448_init */
  21085. ExpectIntEQ(wc_curve448_init(&key), 0);
  21086. /* Test bad args for wc_curve448_init */
  21087. ExpectIntEQ(wc_curve448_init(NULL), BAD_FUNC_ARG);
  21088. /* Test good args for wc_curve_448_free */
  21089. wc_curve448_free(&key);
  21090. /* Test bad args for wc_curve448_free */
  21091. wc_curve448_free(NULL);
  21092. #endif
  21093. return EXPECT_RESULT();
  21094. } /* END test_wc_curve448_init and wc_curve_448_free*/
  21095. /*
  21096. * Testing wc_curve448_make_key
  21097. */
  21098. static int test_wc_curve448_make_key(void)
  21099. {
  21100. EXPECT_DECLS;
  21101. #if defined(HAVE_CURVE448)
  21102. curve448_key key;
  21103. WC_RNG rng;
  21104. int keysize;
  21105. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21106. ExpectIntEQ(wc_curve448_init(&key), 0);
  21107. ExpectIntEQ(wc_InitRng(&rng), 0);
  21108. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21109. ExpectIntEQ(keysize = wc_curve448_size(&key), CURVE448_KEY_SIZE);
  21110. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, &key), 0);
  21111. /* test bad cases */
  21112. ExpectIntEQ(wc_curve448_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  21113. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  21114. ExpectIntEQ(wc_curve448_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  21115. ExpectIntEQ(wc_curve448_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  21116. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21117. wc_curve448_free(&key);
  21118. #endif
  21119. return EXPECT_RESULT();
  21120. } /* END test_wc_curve448_make_key*/
  21121. /*
  21122. * Testing test_wc_curve448_shared_secret_ex
  21123. */
  21124. static int test_wc_curve448_shared_secret_ex(void)
  21125. {
  21126. EXPECT_DECLS;
  21127. #if defined(HAVE_CURVE448)
  21128. curve448_key private_key;
  21129. curve448_key public_key;
  21130. WC_RNG rng;
  21131. byte out[CURVE448_KEY_SIZE];
  21132. word32 outLen = sizeof(out);
  21133. int endian = EC448_BIG_ENDIAN;
  21134. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21135. ExpectIntEQ(wc_curve448_init(&private_key), 0);
  21136. ExpectIntEQ(wc_InitRng(&rng), 0);
  21137. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key), 0);
  21138. ExpectIntEQ(wc_curve448_init(&public_key), 0);
  21139. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key), 0);
  21140. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21141. &outLen, endian), 0);
  21142. /* test bad cases */
  21143. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  21144. BAD_FUNC_ARG);
  21145. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, &public_key, out, &outLen,
  21146. endian), BAD_FUNC_ARG);
  21147. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, NULL, out, &outLen,
  21148. endian), BAD_FUNC_ARG);
  21149. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  21150. &outLen, endian), BAD_FUNC_ARG);
  21151. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21152. NULL, endian), BAD_FUNC_ARG);
  21153. outLen = outLen - 2;
  21154. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21155. &outLen, endian), BAD_FUNC_ARG);
  21156. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21157. wc_curve448_free(&private_key);
  21158. wc_curve448_free(&public_key);
  21159. #endif
  21160. return EXPECT_RESULT();
  21161. } /* END test_wc_curve448_shared_secret_ex*/
  21162. /*
  21163. * Testing test_wc_curve448_export_public_ex
  21164. */
  21165. static int test_wc_curve448_export_public_ex(void)
  21166. {
  21167. EXPECT_DECLS;
  21168. #if defined(HAVE_CURVE448)
  21169. WC_RNG rng;
  21170. curve448_key key;
  21171. byte out[CURVE448_KEY_SIZE];
  21172. word32 outLen = sizeof(out);
  21173. int endian = EC448_BIG_ENDIAN;
  21174. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21175. ExpectIntEQ(wc_curve448_init(&key), 0);
  21176. ExpectIntEQ(wc_InitRng(&rng), 0);
  21177. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21178. ExpectIntEQ(wc_curve448_export_public(&key, out, &outLen), 0);
  21179. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian), 0);
  21180. /* test bad cases*/
  21181. ExpectIntEQ(wc_curve448_export_public_ex(NULL, NULL, NULL, endian),
  21182. BAD_FUNC_ARG);
  21183. ExpectIntEQ(wc_curve448_export_public_ex(NULL, out, &outLen, endian),
  21184. BAD_FUNC_ARG);
  21185. ExpectIntEQ(wc_curve448_export_public_ex(&key, NULL, &outLen, endian),
  21186. BAD_FUNC_ARG);
  21187. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, NULL, endian),
  21188. BAD_FUNC_ARG);
  21189. outLen = outLen - 2;
  21190. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian),
  21191. ECC_BAD_ARG_E);
  21192. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21193. wc_curve448_free(&key);
  21194. #endif
  21195. return EXPECT_RESULT();
  21196. } /* END test_wc_curve448_export_public_ex*/
  21197. /*
  21198. * Testing test_wc_curve448_export_private_raw_ex
  21199. */
  21200. static int test_wc_curve448_export_private_raw_ex(void)
  21201. {
  21202. EXPECT_DECLS;
  21203. #if defined(HAVE_CURVE448)
  21204. curve448_key key;
  21205. byte out[CURVE448_KEY_SIZE];
  21206. word32 outLen = sizeof(out);
  21207. int endian = EC448_BIG_ENDIAN;
  21208. ExpectIntEQ(wc_curve448_init(&key), 0);
  21209. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  21210. 0);
  21211. /* test bad cases*/
  21212. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian),
  21213. BAD_FUNC_ARG);
  21214. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian),
  21215. BAD_FUNC_ARG);
  21216. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian),
  21217. BAD_FUNC_ARG);
  21218. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, NULL, endian),
  21219. BAD_FUNC_ARG);
  21220. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen,
  21221. EC448_LITTLE_ENDIAN), 0);
  21222. outLen = outLen - 2;
  21223. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  21224. ECC_BAD_ARG_E);
  21225. wc_curve448_free(&key);
  21226. #endif
  21227. return EXPECT_RESULT();
  21228. } /* END test_wc_curve448_export_private_raw_ex*/
  21229. /*
  21230. * Testing test_wc_curve448_import_private_raw_ex
  21231. */
  21232. static int test_wc_curve448_import_private_raw_ex(void)
  21233. {
  21234. EXPECT_DECLS;
  21235. #if defined(HAVE_CURVE448)
  21236. curve448_key key;
  21237. WC_RNG rng;
  21238. byte priv[CURVE448_KEY_SIZE];
  21239. byte pub[CURVE448_KEY_SIZE];
  21240. word32 privSz = sizeof(priv);
  21241. word32 pubSz = sizeof(pub);
  21242. int endian = EC448_BIG_ENDIAN;
  21243. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21244. ExpectIntEQ(wc_curve448_init(&key), 0);
  21245. ExpectIntEQ(wc_InitRng(&rng), 0);
  21246. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21247. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21248. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  21249. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21250. &key, endian), 0);
  21251. /* test bad cases */
  21252. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0),
  21253. BAD_FUNC_ARG);
  21254. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  21255. &key, endian), BAD_FUNC_ARG);
  21256. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  21257. &key, endian), BAD_FUNC_ARG);
  21258. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21259. NULL, endian), BAD_FUNC_ARG);
  21260. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  21261. &key, endian), ECC_BAD_ARG_E);
  21262. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  21263. &key, endian), ECC_BAD_ARG_E);
  21264. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21265. &key, EC448_LITTLE_ENDIAN), 0);
  21266. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21267. wc_curve448_free(&key);
  21268. #endif
  21269. return EXPECT_RESULT();
  21270. } /* END test_wc_curve448_import_private_raw_ex*/
  21271. /*
  21272. * Testing test_curve448_export_key_raw
  21273. */
  21274. static int test_wc_curve448_export_key_raw(void)
  21275. {
  21276. EXPECT_DECLS;
  21277. #if defined(HAVE_CURVE448)
  21278. curve448_key key;
  21279. WC_RNG rng;
  21280. byte priv[CURVE448_KEY_SIZE];
  21281. byte pub[CURVE448_KEY_SIZE];
  21282. word32 privSz = sizeof(priv);
  21283. word32 pubSz = sizeof(pub);
  21284. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21285. ExpectIntEQ(wc_curve448_init(&key), 0);
  21286. ExpectIntEQ(wc_InitRng(&rng), 0);
  21287. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21288. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21289. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  21290. ExpectIntEQ(wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz),
  21291. 0);
  21292. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21293. wc_curve448_free(&key);
  21294. #endif
  21295. return EXPECT_RESULT();
  21296. } /* END test_wc_curve448_import_private_raw_ex*/
  21297. /*
  21298. * Testing test_wc_curve448_import_private
  21299. */
  21300. static int test_wc_curve448_import_private(void)
  21301. {
  21302. EXPECT_DECLS;
  21303. #if defined(HAVE_CURVE448)
  21304. curve448_key key;
  21305. WC_RNG rng;
  21306. byte priv[CURVE448_KEY_SIZE];
  21307. word32 privSz = sizeof(priv);
  21308. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21309. ExpectIntEQ(wc_curve448_init(&key), 0);
  21310. ExpectIntEQ(wc_InitRng(&rng), 0);
  21311. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21312. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21313. ExpectIntEQ(wc_curve448_import_private(priv, privSz, &key), 0);
  21314. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21315. wc_curve448_free(&key);
  21316. #endif
  21317. return EXPECT_RESULT();
  21318. } /* END test_wc_curve448_import*/
  21319. /*
  21320. * Testing test_wc_curve448_size.
  21321. */
  21322. static int test_wc_curve448_size(void)
  21323. {
  21324. EXPECT_DECLS;
  21325. #if defined(HAVE_CURVE448)
  21326. curve448_key key;
  21327. ExpectIntEQ(wc_curve448_init(&key), 0);
  21328. /* Test good args for wc_curve448_size */
  21329. ExpectIntEQ(wc_curve448_size(&key), CURVE448_KEY_SIZE);
  21330. /* Test bad args for wc_curve448_size */
  21331. ExpectIntEQ(wc_curve448_size(NULL), 0);
  21332. wc_curve448_free(&key);
  21333. #endif
  21334. return EXPECT_RESULT();
  21335. } /* END test_wc_curve448_size*/
  21336. /*
  21337. * Testing wc_ecc_make_key.
  21338. */
  21339. static int test_wc_ecc_make_key(void)
  21340. {
  21341. EXPECT_DECLS;
  21342. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21343. ecc_key key;
  21344. WC_RNG rng;
  21345. int ret;
  21346. XMEMSET(&key, 0, sizeof(ecc_key));
  21347. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21348. ExpectIntEQ(wc_ecc_init(&key), 0);
  21349. ExpectIntEQ(wc_InitRng(&rng), 0);
  21350. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21351. #if defined(WOLFSSL_ASYNC_CRYPT)
  21352. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21353. #endif
  21354. ExpectIntEQ(ret, 0);
  21355. /* Pass in bad args. */
  21356. ExpectIntEQ(wc_ecc_make_key(NULL, KEY14, &key), BAD_FUNC_ARG);
  21357. ExpectIntEQ(wc_ecc_make_key(&rng, KEY14, NULL), BAD_FUNC_ARG);
  21358. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21359. wc_ecc_free(&key);
  21360. #ifdef FP_ECC
  21361. wc_ecc_fp_free();
  21362. #endif
  21363. #endif
  21364. return EXPECT_RESULT();
  21365. } /* END test_wc_ecc_make_key */
  21366. /*
  21367. * Testing wc_ecc_init()
  21368. */
  21369. static int test_wc_ecc_init(void)
  21370. {
  21371. EXPECT_DECLS;
  21372. #ifdef HAVE_ECC
  21373. ecc_key key;
  21374. XMEMSET(&key, 0, sizeof(ecc_key));
  21375. ExpectIntEQ(wc_ecc_init(&key), 0);
  21376. /* Pass in bad args. */
  21377. ExpectIntEQ(wc_ecc_init(NULL), BAD_FUNC_ARG);
  21378. wc_ecc_free(&key);
  21379. #endif
  21380. return EXPECT_RESULT();
  21381. } /* END test_wc_ecc_init */
  21382. /*
  21383. * Testing wc_ecc_check_key()
  21384. */
  21385. static int test_wc_ecc_check_key(void)
  21386. {
  21387. EXPECT_DECLS;
  21388. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21389. ecc_key key;
  21390. WC_RNG rng;
  21391. int ret;
  21392. XMEMSET(&rng, 0, sizeof(rng));
  21393. XMEMSET(&key, 0, sizeof(key));
  21394. ExpectIntEQ(wc_ecc_init(&key), 0);
  21395. ExpectIntEQ(wc_InitRng(&rng), 0);
  21396. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21397. #if defined(WOLFSSL_ASYNC_CRYPT)
  21398. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21399. #endif
  21400. ExpectIntEQ(ret, 0);
  21401. ExpectIntEQ(wc_ecc_check_key(&key), 0);
  21402. /* Pass in bad args. */
  21403. ExpectIntEQ(wc_ecc_check_key(NULL), BAD_FUNC_ARG);
  21404. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21405. wc_ecc_free(&key);
  21406. #ifdef FP_ECC
  21407. wc_ecc_fp_free();
  21408. #endif
  21409. #endif
  21410. return EXPECT_RESULT();
  21411. } /* END test_wc_ecc_check_key */
  21412. /*
  21413. * Testing wc_ecc_get_generator()
  21414. */
  21415. static int test_wc_ecc_get_generator(void)
  21416. {
  21417. EXPECT_DECLS;
  21418. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  21419. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  21420. ecc_point* pt = NULL;
  21421. ExpectNotNull(pt = wc_ecc_new_point());
  21422. ExpectIntEQ(wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  21423. MP_OKAY);
  21424. /* Test bad args. */
  21425. /* Returns Zero for bad arg. */
  21426. ExpectIntNE(wc_ecc_get_generator(pt, -1), MP_OKAY);
  21427. ExpectIntNE(wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  21428. MP_OKAY);
  21429. /* If we ever get to 1000 curves increase this number */
  21430. ExpectIntNE(wc_ecc_get_generator(pt, 1000), MP_OKAY);
  21431. ExpectIntNE(wc_ecc_get_generator(NULL, -1), MP_OKAY);
  21432. wc_ecc_del_point(pt);
  21433. #endif
  21434. return EXPECT_RESULT();
  21435. } /* END test_wc_ecc_get_generator */
  21436. /*
  21437. * Testing wc_ecc_size()
  21438. */
  21439. static int test_wc_ecc_size(void)
  21440. {
  21441. EXPECT_DECLS;
  21442. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21443. WC_RNG rng;
  21444. ecc_key key;
  21445. int ret;
  21446. XMEMSET(&key, 0, sizeof(ecc_key));
  21447. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21448. ExpectIntEQ(wc_ecc_init(&key), 0);
  21449. ExpectIntEQ(wc_InitRng(&rng), 0);
  21450. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21451. #if defined(WOLFSSL_ASYNC_CRYPT)
  21452. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21453. #endif
  21454. ExpectIntEQ(ret, 0);
  21455. ExpectIntEQ(wc_ecc_size(&key), KEY14);
  21456. /* Test bad args. */
  21457. /* Returns Zero for bad arg. */
  21458. ExpectIntEQ(wc_ecc_size(NULL), 0);
  21459. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21460. wc_ecc_free(&key);
  21461. #endif
  21462. return EXPECT_RESULT();
  21463. } /* END test_wc_ecc_size */
  21464. static int test_wc_ecc_params(void)
  21465. {
  21466. EXPECT_DECLS;
  21467. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  21468. It was added after certifications */
  21469. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  21470. const ecc_set_type* ecc_set;
  21471. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  21472. /* Test for SECP256R1 curve */
  21473. int curve_id = ECC_SECP256R1;
  21474. int curve_idx;
  21475. ExpectIntNE(curve_idx = wc_ecc_get_curve_idx(curve_id), ECC_CURVE_INVALID);
  21476. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(curve_idx));
  21477. ExpectIntEQ(ecc_set->id, curve_id);
  21478. #endif
  21479. /* Test case when SECP256R1 is not enabled */
  21480. /* Test that we get curve params for index 0 */
  21481. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(0));
  21482. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  21483. return EXPECT_RESULT();
  21484. }
  21485. /*
  21486. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  21487. */
  21488. static int test_wc_ecc_signVerify_hash(void)
  21489. {
  21490. EXPECT_DECLS;
  21491. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  21492. ecc_key key;
  21493. WC_RNG rng;
  21494. int ret;
  21495. #ifdef HAVE_ECC_VERIFY
  21496. int verify = 0;
  21497. #endif
  21498. word32 siglen = ECC_BUFSIZE;
  21499. byte sig[ECC_BUFSIZE];
  21500. byte adjustedSig[ECC_BUFSIZE+1];
  21501. byte digest[] = TEST_STRING;
  21502. word32 digestlen = (word32)TEST_STRING_SZ;
  21503. /* Init stack var */
  21504. XMEMSET(&key, 0, sizeof(ecc_key));
  21505. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21506. XMEMSET(sig, 0, siglen);
  21507. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  21508. /* Init structs. */
  21509. ExpectIntEQ(wc_ecc_init(&key), 0);
  21510. ExpectIntEQ(wc_InitRng(&rng), 0);
  21511. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21512. #if defined(WOLFSSL_ASYNC_CRYPT)
  21513. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21514. #endif
  21515. ExpectIntEQ(ret, 0);
  21516. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key),
  21517. 0);
  21518. /* Check bad args. */
  21519. ExpectIntEQ(wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key),
  21520. ECC_BAD_ARG_E);
  21521. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, NULL, &siglen, &rng, &key),
  21522. ECC_BAD_ARG_E);
  21523. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, NULL, &rng, &key),
  21524. ECC_BAD_ARG_E);
  21525. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, NULL, &key),
  21526. ECC_BAD_ARG_E);
  21527. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, NULL),
  21528. ECC_BAD_ARG_E);
  21529. #ifdef HAVE_ECC_VERIFY
  21530. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  21531. &key), 0);
  21532. ExpectIntEQ(verify, 1);
  21533. /* test check on length of signature passed in */
  21534. XMEMCPY(adjustedSig, sig, siglen);
  21535. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  21536. #ifndef NO_STRICT_ECDSA_LEN
  21537. ExpectIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21538. &verify, &key), 0);
  21539. #else
  21540. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  21541. * is allowed */
  21542. ExpectIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21543. &verify, &key), 0);
  21544. #endif
  21545. /* Test bad args. */
  21546. ExpectIntEQ(wc_ecc_verify_hash(NULL, siglen, digest, digestlen, &verify,
  21547. &key), ECC_BAD_ARG_E);
  21548. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, NULL, digestlen, &verify, &key),
  21549. ECC_BAD_ARG_E);
  21550. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, NULL, &key),
  21551. ECC_BAD_ARG_E);
  21552. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  21553. NULL), ECC_BAD_ARG_E);
  21554. #endif /* HAVE_ECC_VERIFY */
  21555. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21556. wc_ecc_free(&key);
  21557. #ifdef FP_ECC
  21558. wc_ecc_fp_free();
  21559. #endif
  21560. #endif
  21561. return EXPECT_RESULT();
  21562. } /* END test_wc_ecc_sign_hash */
  21563. /*
  21564. * Testing wc_ecc_shared_secret()
  21565. */
  21566. static int test_wc_ecc_shared_secret(void)
  21567. {
  21568. EXPECT_DECLS;
  21569. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  21570. ecc_key key;
  21571. ecc_key pubKey;
  21572. WC_RNG rng;
  21573. #if defined(NO_ECC256)
  21574. int ret;
  21575. #endif
  21576. byte out[KEY32];
  21577. int keySz = sizeof(out);
  21578. word32 outlen = (word32)sizeof(out);
  21579. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21580. const char* qx =
  21581. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21582. const char* qy =
  21583. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21584. const char* d =
  21585. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21586. const char* curveName = "SECP256R1";
  21587. const byte expected_shared_secret[] =
  21588. {
  21589. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  21590. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  21591. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  21592. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  21593. };
  21594. #endif
  21595. PRIVATE_KEY_UNLOCK();
  21596. /* Initialize variables. */
  21597. XMEMSET(&key, 0, sizeof(ecc_key));
  21598. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  21599. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21600. XMEMSET(out, 0, keySz);
  21601. ExpectIntEQ(wc_ecc_init(&key), 0);
  21602. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  21603. ExpectIntEQ(wc_InitRng(&rng), 0);
  21604. #if !defined(NO_ECC256)
  21605. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  21606. ExpectIntEQ(wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName), 0);
  21607. #else
  21608. ret = wc_ecc_make_key(&rng, keySz, &key);
  21609. #if defined(WOLFSSL_ASYNC_CRYPT)
  21610. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21611. #endif
  21612. ExpectIntEQ(ret, 0);
  21613. ret = wc_ecc_make_key(&rng, keySz, &key);
  21614. #if defined(WOLFSSL_ASYNC_CRYPT)
  21615. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21616. #endif
  21617. ExpectIntEQ(ret, 0);
  21618. #endif
  21619. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21620. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21621. !defined(HAVE_SELFTEST)
  21622. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  21623. #endif
  21624. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen), 0);
  21625. #if !defined(NO_ECC256)
  21626. ExpectIntEQ(XMEMCMP(out, expected_shared_secret, outlen), 0);
  21627. #endif
  21628. /* Test bad args. */
  21629. ExpectIntEQ(wc_ecc_shared_secret(NULL, &pubKey, out, &outlen),
  21630. BAD_FUNC_ARG);
  21631. ExpectIntEQ(wc_ecc_shared_secret(&key, NULL, out, &outlen),
  21632. BAD_FUNC_ARG);
  21633. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen),
  21634. BAD_FUNC_ARG);
  21635. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, NULL),
  21636. BAD_FUNC_ARG);
  21637. /* Invalid length */
  21638. outlen = 1;
  21639. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen),
  21640. BUFFER_E);
  21641. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21642. wc_ecc_free(&pubKey);
  21643. wc_ecc_free(&key);
  21644. #ifdef FP_ECC
  21645. wc_ecc_fp_free();
  21646. #endif
  21647. PRIVATE_KEY_LOCK();
  21648. #endif
  21649. return EXPECT_RESULT();
  21650. } /* END tests_wc_ecc_shared_secret */
  21651. /*
  21652. * testint wc_ecc_export_x963()
  21653. */
  21654. static int test_wc_ecc_export_x963(void)
  21655. {
  21656. EXPECT_DECLS;
  21657. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21658. ecc_key key;
  21659. WC_RNG rng;
  21660. byte out[ECC_ASN963_MAX_BUF_SZ];
  21661. word32 outlen = sizeof(out);
  21662. int ret;
  21663. PRIVATE_KEY_UNLOCK();
  21664. /* Initialize variables. */
  21665. XMEMSET(&key, 0, sizeof(ecc_key));
  21666. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21667. XMEMSET(out, 0, outlen);
  21668. ExpectIntEQ(wc_ecc_init(&key), 0);
  21669. ExpectIntEQ(wc_InitRng(&rng), 0);
  21670. ret = wc_ecc_make_key(&rng, KEY20, &key);
  21671. #if defined(WOLFSSL_ASYNC_CRYPT)
  21672. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21673. #endif
  21674. ExpectIntEQ(ret, 0);
  21675. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), 0);
  21676. /* Test bad args. */
  21677. ExpectIntEQ(wc_ecc_export_x963(NULL, out, &outlen), ECC_BAD_ARG_E);
  21678. ExpectIntEQ(wc_ecc_export_x963(&key, NULL, &outlen), LENGTH_ONLY_E);
  21679. ExpectIntEQ(wc_ecc_export_x963(&key, out, NULL), ECC_BAD_ARG_E);
  21680. key.idx = -4;
  21681. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), ECC_BAD_ARG_E);
  21682. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21683. wc_ecc_free(&key);
  21684. #ifdef FP_ECC
  21685. wc_ecc_fp_free();
  21686. #endif
  21687. PRIVATE_KEY_LOCK();
  21688. #endif
  21689. return EXPECT_RESULT();
  21690. } /* END test_wc_ecc_export_x963 */
  21691. /*
  21692. * Testing wc_ecc_export_x963_ex()
  21693. * compile with --enable-compkey will use compression.
  21694. */
  21695. static int test_wc_ecc_export_x963_ex(void)
  21696. {
  21697. EXPECT_DECLS;
  21698. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21699. ecc_key key;
  21700. WC_RNG rng;
  21701. int ret;
  21702. byte out[ECC_ASN963_MAX_BUF_SZ];
  21703. word32 outlen = sizeof(out);
  21704. #ifdef HAVE_COMP_KEY
  21705. word32 badOutLen = 5;
  21706. #endif
  21707. /* Init stack variables. */
  21708. XMEMSET(&key, 0, sizeof(ecc_key));
  21709. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21710. XMEMSET(out, 0, outlen);
  21711. ExpectIntEQ(wc_ecc_init(&key), 0);
  21712. ExpectIntEQ(wc_InitRng(&rng), 0);
  21713. ret = wc_ecc_make_key(&rng, KEY64, &key);
  21714. #if defined(WOLFSSL_ASYNC_CRYPT)
  21715. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21716. #endif
  21717. ExpectIntEQ(ret, 0);
  21718. #ifdef HAVE_COMP_KEY
  21719. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), 0);
  21720. #else
  21721. ExpectIntEQ(ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP), 0);
  21722. #endif
  21723. /* Test bad args. */
  21724. #ifdef HAVE_COMP_KEY
  21725. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, COMP), BAD_FUNC_ARG);
  21726. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP), BAD_FUNC_ARG);
  21727. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, COMP), BAD_FUNC_ARG);
  21728. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  21729. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP), BUFFER_E);
  21730. #else
  21731. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP),
  21732. LENGTH_ONLY_E);
  21733. #endif
  21734. key.idx = -4;
  21735. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), ECC_BAD_ARG_E);
  21736. #else
  21737. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP),
  21738. ECC_BAD_ARG_E);
  21739. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP),
  21740. LENGTH_ONLY_E);
  21741. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, 1), NOT_COMPILED_IN);
  21742. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP),
  21743. ECC_BAD_ARG_E);
  21744. key.idx = -4;
  21745. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP),
  21746. ECC_BAD_ARG_E);
  21747. #endif
  21748. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21749. wc_ecc_free(&key);
  21750. #ifdef FP_ECC
  21751. wc_ecc_fp_free();
  21752. #endif
  21753. #endif
  21754. return EXPECT_RESULT();
  21755. } /* END test_wc_ecc_export_x963_ex */
  21756. /*
  21757. * testing wc_ecc_import_x963()
  21758. */
  21759. static int test_wc_ecc_import_x963(void)
  21760. {
  21761. EXPECT_DECLS;
  21762. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21763. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21764. ecc_key pubKey;
  21765. ecc_key key;
  21766. WC_RNG rng;
  21767. byte x963[ECC_ASN963_MAX_BUF_SZ];
  21768. word32 x963Len = (word32)sizeof(x963);
  21769. int ret;
  21770. /* Init stack variables. */
  21771. XMEMSET(&key, 0, sizeof(ecc_key));
  21772. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  21773. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21774. XMEMSET(x963, 0, x963Len);
  21775. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  21776. ExpectIntEQ(wc_ecc_init(&key), 0);
  21777. ExpectIntEQ(wc_InitRng(&rng), 0);
  21778. #if FIPS_VERSION3_GE(6,0,0)
  21779. ret = wc_ecc_make_key(&rng, KEY32, &key);
  21780. #else
  21781. ret = wc_ecc_make_key(&rng, KEY24, &key);
  21782. #endif
  21783. #if defined(WOLFSSL_ASYNC_CRYPT)
  21784. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21785. #endif
  21786. ExpectIntEQ(ret, 0);
  21787. PRIVATE_KEY_UNLOCK();
  21788. ExpectIntEQ(wc_ecc_export_x963(&key, x963, &x963Len), 0);
  21789. PRIVATE_KEY_LOCK();
  21790. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, &pubKey), 0);
  21791. /* Test bad args. */
  21792. ExpectIntEQ(wc_ecc_import_x963(NULL, x963Len, &pubKey), BAD_FUNC_ARG);
  21793. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, NULL), BAD_FUNC_ARG);
  21794. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len + 1, &pubKey), ECC_BAD_ARG_E);
  21795. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21796. wc_ecc_free(&key);
  21797. wc_ecc_free(&pubKey);
  21798. #ifdef FP_ECC
  21799. wc_ecc_fp_free();
  21800. #endif
  21801. #endif
  21802. return EXPECT_RESULT();
  21803. } /* END wc_ecc_import_x963 */
  21804. /*
  21805. * testing wc_ecc_import_private_key()
  21806. */
  21807. static int test_wc_ecc_import_private_key(void)
  21808. {
  21809. EXPECT_DECLS;
  21810. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21811. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21812. ecc_key key;
  21813. ecc_key keyImp;
  21814. WC_RNG rng;
  21815. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  21816. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  21817. word32 privKeySz = (word32)sizeof(privKey);
  21818. word32 x963KeySz = (word32)sizeof(x963Key);
  21819. int ret;
  21820. /* Init stack variables. */
  21821. XMEMSET(&key, 0, sizeof(ecc_key));
  21822. XMEMSET(&keyImp, 0, sizeof(ecc_key));
  21823. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21824. XMEMSET(privKey, 0, privKeySz);
  21825. XMEMSET(x963Key, 0, x963KeySz);
  21826. ExpectIntEQ(wc_ecc_init(&key), 0);
  21827. ExpectIntEQ(wc_ecc_init(&keyImp), 0);
  21828. ExpectIntEQ(wc_InitRng(&rng), 0);
  21829. ret = wc_ecc_make_key(&rng, KEY48, &key);
  21830. #if defined(WOLFSSL_ASYNC_CRYPT)
  21831. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21832. #endif
  21833. ExpectIntEQ(ret, 0);
  21834. PRIVATE_KEY_UNLOCK();
  21835. ExpectIntEQ(wc_ecc_export_x963(&key, x963Key, &x963KeySz), 0);
  21836. PRIVATE_KEY_LOCK();
  21837. ExpectIntEQ(wc_ecc_export_private_only(&key, privKey, &privKeySz), 0);
  21838. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21839. x963KeySz, &keyImp), 0);
  21840. /* Pass in bad args. */
  21841. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21842. x963KeySz, NULL), BAD_FUNC_ARG);
  21843. ExpectIntEQ(wc_ecc_import_private_key(NULL, privKeySz, x963Key, x963KeySz,
  21844. &keyImp), BAD_FUNC_ARG);
  21845. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21846. wc_ecc_free(&keyImp);
  21847. wc_ecc_free(&key);
  21848. #ifdef FP_ECC
  21849. wc_ecc_fp_free();
  21850. #endif
  21851. #endif
  21852. return EXPECT_RESULT();
  21853. } /* END test_wc_ecc_import_private_key */
  21854. /*
  21855. * Testing wc_ecc_export_private_only()
  21856. */
  21857. static int test_wc_ecc_export_private_only(void)
  21858. {
  21859. EXPECT_DECLS;
  21860. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21861. ecc_key key;
  21862. WC_RNG rng;
  21863. byte out[ECC_PRIV_KEY_BUF];
  21864. word32 outlen = sizeof(out);
  21865. int ret;
  21866. /* Init stack variables. */
  21867. XMEMSET(&key, 0, sizeof(ecc_key));
  21868. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21869. XMEMSET(out, 0, outlen);
  21870. ExpectIntEQ(wc_ecc_init(&key), 0);
  21871. ExpectIntEQ(wc_InitRng(&rng), 0);
  21872. ret = wc_ecc_make_key(&rng, KEY32, &key);
  21873. #if defined(WOLFSSL_ASYNC_CRYPT)
  21874. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21875. #endif
  21876. ExpectIntEQ(ret, 0);
  21877. ExpectIntEQ(wc_ecc_export_private_only(&key, out, &outlen), 0);
  21878. /* Pass in bad args. */
  21879. ExpectIntEQ(wc_ecc_export_private_only(NULL, out, &outlen), BAD_FUNC_ARG);
  21880. ExpectIntEQ(wc_ecc_export_private_only(&key, NULL, &outlen), BAD_FUNC_ARG);
  21881. ExpectIntEQ(wc_ecc_export_private_only(&key, out, NULL), BAD_FUNC_ARG);
  21882. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21883. wc_ecc_free(&key);
  21884. #ifdef FP_ECC
  21885. wc_ecc_fp_free();
  21886. #endif
  21887. #endif
  21888. return EXPECT_RESULT();
  21889. } /* END test_wc_ecc_export_private_only */
  21890. /*
  21891. * Testing wc_ecc_rs_to_sig()
  21892. */
  21893. static int test_wc_ecc_rs_to_sig(void)
  21894. {
  21895. EXPECT_DECLS;
  21896. #if defined(HAVE_ECC) && !defined(NO_ASN)
  21897. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  21898. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  21899. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  21900. const char* zeroStr = "0";
  21901. byte sig[ECC_MAX_SIG_SIZE];
  21902. word32 siglen = (word32)sizeof(sig);
  21903. /* R and S max size is the order of curve. 2^192.*/
  21904. int keySz = KEY24;
  21905. byte r[KEY24];
  21906. byte s[KEY24];
  21907. word32 rlen = (word32)sizeof(r);
  21908. word32 slen = (word32)sizeof(s);
  21909. /* Init stack variables. */
  21910. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  21911. XMEMSET(r, 0, keySz);
  21912. XMEMSET(s, 0, keySz);
  21913. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, &siglen), 0);
  21914. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen), 0);
  21915. /* Test bad args. */
  21916. ExpectIntEQ(wc_ecc_rs_to_sig(NULL, S, sig, &siglen), ECC_BAD_ARG_E);
  21917. ExpectIntEQ(wc_ecc_rs_to_sig(R, NULL, sig, &siglen), ECC_BAD_ARG_E);
  21918. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, NULL), ECC_BAD_ARG_E);
  21919. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, NULL, &siglen), ECC_BAD_ARG_E);
  21920. ExpectIntEQ(wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen), MP_ZERO_E);
  21921. ExpectIntEQ(wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen), MP_ZERO_E);
  21922. ExpectIntEQ(wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen),
  21923. ECC_BAD_ARG_E);
  21924. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen),
  21925. ECC_BAD_ARG_E);
  21926. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen),
  21927. ECC_BAD_ARG_E);
  21928. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen),
  21929. ECC_BAD_ARG_E);
  21930. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL),
  21931. ECC_BAD_ARG_E);
  21932. #endif
  21933. return EXPECT_RESULT();
  21934. } /* END test_wc_ecc_rs_to_sig */
  21935. static int test_wc_ecc_import_raw(void)
  21936. {
  21937. EXPECT_DECLS;
  21938. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21939. ecc_key key;
  21940. const char* qx =
  21941. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21942. const char* qy =
  21943. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21944. const char* d =
  21945. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21946. const char* curveName = "SECP256R1";
  21947. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21948. const char* kNullStr = "";
  21949. int ret;
  21950. #endif
  21951. XMEMSET(&key, 0, sizeof(ecc_key));
  21952. ExpectIntEQ(wc_ecc_init(&key), 0);
  21953. /* Test good import */
  21954. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  21955. /* Test bad args. */
  21956. ExpectIntEQ(wc_ecc_import_raw(NULL, qx, qy, d, curveName), BAD_FUNC_ARG);
  21957. ExpectIntEQ(wc_ecc_import_raw(&key, NULL, qy, d, curveName), BAD_FUNC_ARG);
  21958. ExpectIntEQ(wc_ecc_import_raw(&key, qx, NULL, d, curveName), BAD_FUNC_ARG);
  21959. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, NULL), BAD_FUNC_ARG);
  21960. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21961. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21962. wc_ecc_free(&key);
  21963. #endif
  21964. ExpectIntLT(ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr,
  21965. curveName), 0);
  21966. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  21967. #endif
  21968. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  21969. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21970. wc_ecc_free(&key);
  21971. #endif
  21972. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21973. ExpectIntLT(ret = wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  21974. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  21975. #else
  21976. ExpectIntEQ(wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  21977. #endif
  21978. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21979. wc_ecc_free(&key);
  21980. #endif
  21981. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21982. ExpectIntLT(ret = wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  21983. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  21984. #else
  21985. ExpectIntEQ(wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  21986. #endif
  21987. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21988. wc_ecc_free(&key);
  21989. #endif
  21990. ExpectIntEQ(wc_ecc_import_raw(&key, "0", "0", d, curveName), ECC_INF_E);
  21991. #endif
  21992. wc_ecc_free(&key);
  21993. #endif
  21994. return EXPECT_RESULT();
  21995. } /* END test_wc_ecc_import_raw */
  21996. static int test_wc_ecc_import_unsigned(void)
  21997. {
  21998. EXPECT_DECLS;
  21999. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22000. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  22001. HAVE_FIPS_VERSION >= 2))
  22002. ecc_key key;
  22003. const byte qx[] = {
  22004. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  22005. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  22006. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  22007. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  22008. };
  22009. const byte qy[] = {
  22010. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  22011. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  22012. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  22013. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  22014. };
  22015. const byte d[] = {
  22016. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  22017. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  22018. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  22019. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  22020. };
  22021. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22022. const byte nullBytes[32] = {0};
  22023. int ret;
  22024. #endif
  22025. int curveId = ECC_SECP256R1;
  22026. XMEMSET(&key, 0, sizeof(ecc_key));
  22027. ExpectIntEQ(wc_ecc_init(&key), 0);
  22028. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  22029. curveId), 0);
  22030. /* Test bad args. */
  22031. ExpectIntEQ(wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  22032. curveId), BAD_FUNC_ARG);
  22033. ExpectIntEQ(wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  22034. curveId), BAD_FUNC_ARG);
  22035. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  22036. curveId), BAD_FUNC_ARG);
  22037. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  22038. ECC_CURVE_INVALID), BAD_FUNC_ARG);
  22039. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22040. ExpectIntLT(ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  22041. (byte*)nullBytes, (byte*)nullBytes, curveId), 0);
  22042. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  22043. #endif
  22044. wc_ecc_free(&key);
  22045. #endif
  22046. return EXPECT_RESULT();
  22047. } /* END test_wc_ecc_import_unsigned */
  22048. /*
  22049. * Testing wc_ecc_sig_size()
  22050. */
  22051. static int test_wc_ecc_sig_size(void)
  22052. {
  22053. EXPECT_DECLS;
  22054. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22055. ecc_key key;
  22056. WC_RNG rng;
  22057. int keySz = KEY16;
  22058. int ret;
  22059. XMEMSET(&rng, 0, sizeof(rng));
  22060. XMEMSET(&key, 0, sizeof(key));
  22061. ExpectIntEQ(wc_ecc_init(&key), 0);
  22062. ExpectIntEQ(wc_InitRng(&rng), 0);
  22063. ret = wc_ecc_make_key(&rng, keySz, &key);
  22064. #if defined(WOLFSSL_ASYNC_CRYPT)
  22065. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22066. #endif
  22067. ExpectIntEQ(ret, 0);
  22068. ExpectIntLE(wc_ecc_sig_size(&key),
  22069. (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ));
  22070. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22071. wc_ecc_free(&key);
  22072. #endif
  22073. return EXPECT_RESULT();
  22074. } /* END test_wc_ecc_sig_size */
  22075. /*
  22076. * Testing wc_ecc_ctx_new()
  22077. */
  22078. static int test_wc_ecc_ctx_new(void)
  22079. {
  22080. EXPECT_DECLS;
  22081. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22082. WC_RNG rng;
  22083. ecEncCtx* cli = NULL;
  22084. ecEncCtx* srv = NULL;
  22085. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22086. ExpectIntEQ(wc_InitRng(&rng), 0);
  22087. ExpectNotNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22088. ExpectNotNull(srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  22089. wc_ecc_ctx_free(cli);
  22090. cli = NULL;
  22091. wc_ecc_ctx_free(srv);
  22092. /* Test bad args. */
  22093. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  22094. ExpectNull(cli = wc_ecc_ctx_new(0, &rng));
  22095. ExpectNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL));
  22096. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22097. wc_ecc_ctx_free(cli);
  22098. #endif
  22099. return EXPECT_RESULT();
  22100. } /* END test_wc_ecc_ctx_new */
  22101. /*
  22102. * Tesing wc_ecc_reset()
  22103. */
  22104. static int test_wc_ecc_ctx_reset(void)
  22105. {
  22106. EXPECT_DECLS;
  22107. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22108. ecEncCtx* ctx = NULL;
  22109. WC_RNG rng;
  22110. XMEMSET(&rng, 0, sizeof(rng));
  22111. ExpectIntEQ(wc_InitRng(&rng), 0);
  22112. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22113. ExpectIntEQ(wc_ecc_ctx_reset(ctx, &rng), 0);
  22114. /* Pass in bad args. */
  22115. ExpectIntEQ(wc_ecc_ctx_reset(NULL, &rng), BAD_FUNC_ARG);
  22116. ExpectIntEQ(wc_ecc_ctx_reset(ctx, NULL), BAD_FUNC_ARG);
  22117. wc_ecc_ctx_free(ctx);
  22118. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22119. #endif
  22120. return EXPECT_RESULT();
  22121. } /* END test_wc_ecc_ctx_reset */
  22122. /*
  22123. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  22124. */
  22125. static int test_wc_ecc_ctx_set_peer_salt(void)
  22126. {
  22127. EXPECT_DECLS;
  22128. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22129. WC_RNG rng;
  22130. ecEncCtx* cliCtx = NULL;
  22131. ecEncCtx* servCtx = NULL;
  22132. const byte* cliSalt = NULL;
  22133. const byte* servSalt = NULL;
  22134. XMEMSET(&rng, 0, sizeof(rng));
  22135. ExpectIntEQ(wc_InitRng(&rng), 0);
  22136. ExpectNotNull(cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22137. ExpectNotNull(servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  22138. /* Test bad args. */
  22139. ExpectNull(cliSalt = wc_ecc_ctx_get_own_salt(NULL));
  22140. ExpectNotNull(cliSalt = wc_ecc_ctx_get_own_salt(cliCtx));
  22141. ExpectNotNull(servSalt = wc_ecc_ctx_get_own_salt(servCtx));
  22142. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, servSalt), 0);
  22143. /* Test bad args. */
  22144. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(NULL, servSalt), BAD_FUNC_ARG);
  22145. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, NULL), BAD_FUNC_ARG);
  22146. wc_ecc_ctx_free(cliCtx);
  22147. wc_ecc_ctx_free(servCtx);
  22148. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22149. #endif
  22150. return EXPECT_RESULT();
  22151. } /* END test_wc_ecc_ctx_set_peer_salt */
  22152. /*
  22153. * Testing wc_ecc_ctx_set_info()
  22154. */
  22155. static int test_wc_ecc_ctx_set_info(void)
  22156. {
  22157. EXPECT_DECLS;
  22158. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22159. ecEncCtx* ctx = NULL;
  22160. WC_RNG rng;
  22161. const char* optInfo = "Optional Test Info.";
  22162. int optInfoSz = (int)XSTRLEN(optInfo);
  22163. const char* badOptInfo = NULL;
  22164. XMEMSET(&rng, 0, sizeof(rng));
  22165. ExpectIntEQ(wc_InitRng(&rng), 0);
  22166. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22167. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz), 0);
  22168. /* Test bad args. */
  22169. ExpectIntEQ(wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz),
  22170. BAD_FUNC_ARG);
  22171. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz),
  22172. BAD_FUNC_ARG);
  22173. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1), BAD_FUNC_ARG);
  22174. wc_ecc_ctx_free(ctx);
  22175. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22176. #endif
  22177. return EXPECT_RESULT();
  22178. } /* END test_wc_ecc_ctx_set_info */
  22179. /*
  22180. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  22181. */
  22182. static int test_wc_ecc_encryptDecrypt(void)
  22183. {
  22184. EXPECT_DECLS;
  22185. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  22186. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  22187. ecc_key srvKey;
  22188. ecc_key cliKey;
  22189. ecc_key tmpKey;
  22190. WC_RNG rng;
  22191. int ret;
  22192. const char* msg = "EccBlock Size 16";
  22193. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  22194. #ifdef WOLFSSL_ECIES_OLD
  22195. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22196. #elif defined(WOLFSSL_ECIES_GEN_IV)
  22197. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  22198. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22199. #else
  22200. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) +
  22201. WC_SHA256_DIGEST_SIZE];
  22202. #endif
  22203. word32 outSz = (word32)sizeof(out);
  22204. byte plain[sizeof("EccBlock Size 16")];
  22205. word32 plainSz = (word32)sizeof(plain);
  22206. int keySz = KEY20;
  22207. /* Init stack variables. */
  22208. XMEMSET(out, 0, outSz);
  22209. XMEMSET(plain, 0, plainSz);
  22210. XMEMSET(&rng, 0, sizeof(rng));
  22211. XMEMSET(&srvKey, 0, sizeof(ecc_key));
  22212. XMEMSET(&cliKey, 0, sizeof(ecc_key));
  22213. XMEMSET(&tmpKey, 0, sizeof(ecc_key));
  22214. ExpectIntEQ(wc_InitRng(&rng), 0);
  22215. ExpectIntEQ(wc_ecc_init(&cliKey), 0);
  22216. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  22217. #if defined(WOLFSSL_ASYNC_CRYPT)
  22218. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22219. #endif
  22220. ExpectIntEQ(ret, 0);
  22221. ExpectIntEQ(wc_ecc_init(&srvKey), 0);
  22222. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  22223. #if defined(WOLFSSL_ASYNC_CRYPT)
  22224. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22225. #endif
  22226. ExpectIntEQ(ret, 0);
  22227. ExpectIntEQ(wc_ecc_init(&tmpKey), 0);
  22228. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22229. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22230. !defined(HAVE_SELFTEST)
  22231. ExpectIntEQ(wc_ecc_set_rng(&srvKey, &rng), 0);
  22232. ExpectIntEQ(wc_ecc_set_rng(&cliKey, &rng), 0);
  22233. #endif
  22234. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  22235. &outSz, NULL), 0);
  22236. /* Test bad args. */
  22237. ExpectIntEQ(wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out, &outSz,
  22238. NULL), BAD_FUNC_ARG);
  22239. ExpectIntEQ(wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out, &outSz,
  22240. NULL), BAD_FUNC_ARG);
  22241. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out, &outSz,
  22242. NULL), BAD_FUNC_ARG);
  22243. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  22244. &outSz, NULL), BAD_FUNC_ARG);
  22245. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out, NULL,
  22246. NULL), BAD_FUNC_ARG);
  22247. #ifdef WOLFSSL_ECIES_OLD
  22248. tmpKey.dp = cliKey.dp;
  22249. ExpectIntEQ(wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey), 0);
  22250. #endif
  22251. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, &plainSz,
  22252. NULL), 0);
  22253. ExpectIntEQ(wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain, &plainSz,
  22254. NULL), BAD_FUNC_ARG);
  22255. #ifdef WOLFSSL_ECIES_OLD
  22256. /* NULL parameter allowed in new implementations - public key comes from
  22257. * the message. */
  22258. ExpectIntEQ(wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain, &plainSz,
  22259. NULL), BAD_FUNC_ARG);
  22260. #endif
  22261. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain, &plainSz,
  22262. NULL), BAD_FUNC_ARG);
  22263. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL, &plainSz,
  22264. NULL), BAD_FUNC_ARG);
  22265. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, NULL, NULL),
  22266. BAD_FUNC_ARG);
  22267. ExpectIntEQ(XMEMCMP(msg, plain, msgSz), 0);
  22268. wc_ecc_free(&tmpKey);
  22269. wc_ecc_free(&srvKey);
  22270. wc_ecc_free(&cliKey);
  22271. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22272. #endif
  22273. return EXPECT_RESULT();
  22274. } /* END test_wc_ecc_encryptDecrypt */
  22275. /*
  22276. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  22277. */
  22278. static int test_wc_ecc_del_point(void)
  22279. {
  22280. EXPECT_DECLS;
  22281. #if defined(HAVE_ECC)
  22282. ecc_point* pt = NULL;
  22283. ExpectNotNull(pt = wc_ecc_new_point());
  22284. wc_ecc_del_point(pt);
  22285. #endif
  22286. return EXPECT_RESULT();
  22287. } /* END test_wc_ecc_del_point */
  22288. /*
  22289. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  22290. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  22291. * and wc_ecc_cmp_point()
  22292. */
  22293. static int test_wc_ecc_pointFns(void)
  22294. {
  22295. EXPECT_DECLS;
  22296. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  22297. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22298. !defined(WOLFSSL_ATECC608A)
  22299. ecc_key key;
  22300. WC_RNG rng;
  22301. int ret;
  22302. ecc_point* point = NULL;
  22303. ecc_point* cpypt = NULL;
  22304. int idx = 0;
  22305. int keySz = KEY32;
  22306. byte der[DER_SZ(KEY32)];
  22307. word32 derlenChk = 0;
  22308. word32 derSz = DER_SZ(KEY32);
  22309. /* Init stack variables. */
  22310. XMEMSET(der, 0, derSz);
  22311. XMEMSET(&key, 0, sizeof(ecc_key));
  22312. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22313. ExpectIntEQ(wc_InitRng(&rng), 0);
  22314. ExpectIntEQ(wc_ecc_init(&key), 0);
  22315. ret = wc_ecc_make_key(&rng, keySz, &key);
  22316. #if defined(WOLFSSL_ASYNC_CRYPT)
  22317. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22318. #endif
  22319. ExpectIntEQ(ret, 0);
  22320. ExpectNotNull(point = wc_ecc_new_point());
  22321. ExpectNotNull(cpypt = wc_ecc_new_point());
  22322. /* Export */
  22323. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, NULL,
  22324. &derlenChk), LENGTH_ONLY_E);
  22325. /* Check length value. */
  22326. ExpectIntEQ(derSz, derlenChk);
  22327. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  22328. &derSz), 0);
  22329. /* Test bad args. */
  22330. ExpectIntEQ(wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz),
  22331. ECC_BAD_ARG_E);
  22332. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz),
  22333. ECC_BAD_ARG_E);
  22334. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  22335. NULL), ECC_BAD_ARG_E);
  22336. /* Import */
  22337. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, point), 0);
  22338. ExpectIntEQ(wc_ecc_cmp_point(&key.pubkey, point), 0);
  22339. /* Test bad args. */
  22340. ExpectIntEQ( wc_ecc_import_point_der(NULL, derSz, idx, point),
  22341. ECC_BAD_ARG_E);
  22342. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, NULL), ECC_BAD_ARG_E);
  22343. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, -1, point), ECC_BAD_ARG_E);
  22344. ExpectIntEQ(wc_ecc_import_point_der(der, derSz + 1, idx, point),
  22345. ECC_BAD_ARG_E);
  22346. /* Copy */
  22347. ExpectIntEQ(wc_ecc_copy_point(point, cpypt), 0);
  22348. /* Test bad args. */
  22349. ExpectIntEQ(wc_ecc_copy_point(NULL, cpypt), ECC_BAD_ARG_E);
  22350. ExpectIntEQ(wc_ecc_copy_point(point, NULL), ECC_BAD_ARG_E);
  22351. /* Compare point */
  22352. ExpectIntEQ(wc_ecc_cmp_point(point, cpypt), 0);
  22353. /* Test bad args. */
  22354. ExpectIntEQ(wc_ecc_cmp_point(NULL, cpypt), BAD_FUNC_ARG);
  22355. ExpectIntEQ(wc_ecc_cmp_point(point, NULL), BAD_FUNC_ARG);
  22356. /* At infinity if return == 1, otherwise return == 0. */
  22357. ExpectIntEQ(wc_ecc_point_is_at_infinity(point), 0);
  22358. /* Test bad args. */
  22359. ExpectIntEQ(wc_ecc_point_is_at_infinity(NULL), BAD_FUNC_ARG);
  22360. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  22361. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  22362. #ifdef USE_ECC_B_PARAM
  22363. /* On curve if ret == 0 */
  22364. ExpectIntEQ(wc_ecc_point_is_on_curve(point, idx), 0);
  22365. /* Test bad args. */
  22366. ExpectIntEQ(wc_ecc_point_is_on_curve(NULL, idx), BAD_FUNC_ARG);
  22367. ExpectIntEQ(wc_ecc_point_is_on_curve(point, 1000), ECC_BAD_ARG_E);
  22368. #endif /* USE_ECC_B_PARAM */
  22369. #endif /* !HAVE_SELFTEST && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  22370. /* Free */
  22371. wc_ecc_del_point(point);
  22372. wc_ecc_del_point(cpypt);
  22373. wc_ecc_free(&key);
  22374. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22375. #endif
  22376. return EXPECT_RESULT();
  22377. } /* END test_wc_ecc_pointFns */
  22378. /*
  22379. * Testing wc_ecc_shared_secret_ssh()
  22380. */
  22381. static int test_wc_ecc_shared_secret_ssh(void)
  22382. {
  22383. EXPECT_DECLS;
  22384. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  22385. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22386. !defined(WOLFSSL_ATECC608A) && !defined(PLUTON_CRYPTO_ECC) && \
  22387. !defined(WOLFSSL_CRYPTOCELL)
  22388. ecc_key key;
  22389. ecc_key key2;
  22390. WC_RNG rng;
  22391. int ret;
  22392. int keySz = KEY32;
  22393. #if FIPS_VERSION3_GE(6,0,0)
  22394. int key2Sz = KEY28;
  22395. #else
  22396. int key2Sz = KEY24;
  22397. #endif
  22398. byte secret[KEY32];
  22399. word32 secretLen = keySz;
  22400. /* Init stack variables. */
  22401. XMEMSET(&key, 0, sizeof(ecc_key));
  22402. XMEMSET(&key2, 0, sizeof(ecc_key));
  22403. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22404. XMEMSET(secret, 0, secretLen);
  22405. /* Make keys */
  22406. ExpectIntEQ(wc_ecc_init(&key), 0);
  22407. ExpectIntEQ(wc_InitRng(&rng), 0);
  22408. ret = wc_ecc_make_key(&rng, keySz, &key);
  22409. #if defined(WOLFSSL_ASYNC_CRYPT)
  22410. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22411. #endif
  22412. ExpectIntEQ(ret, 0);
  22413. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22414. ExpectIntEQ(wc_ecc_init(&key2), 0);
  22415. ExpectIntEQ(wc_InitRng(&rng), 0);
  22416. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  22417. #if defined(WOLFSSL_ASYNC_CRYPT)
  22418. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  22419. #endif
  22420. ExpectIntEQ(ret, 0);
  22421. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22422. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22423. !defined(HAVE_SELFTEST)
  22424. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  22425. #endif
  22426. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  22427. &secretLen), 0);
  22428. /* Pass in bad args. */
  22429. ExpectIntEQ(wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret,
  22430. &secretLen), BAD_FUNC_ARG);
  22431. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen),
  22432. BAD_FUNC_ARG);
  22433. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen),
  22434. BAD_FUNC_ARG);
  22435. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL),
  22436. BAD_FUNC_ARG);
  22437. key.type = ECC_PUBLICKEY;
  22438. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  22439. &secretLen), ECC_BAD_ARG_E);
  22440. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22441. wc_ecc_free(&key);
  22442. wc_ecc_free(&key2);
  22443. #ifdef FP_ECC
  22444. wc_ecc_fp_free();
  22445. #endif
  22446. #endif
  22447. return EXPECT_RESULT();
  22448. } /* END test_wc_ecc_shared_secret_ssh */
  22449. /*
  22450. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  22451. */
  22452. static int test_wc_ecc_verify_hash_ex(void)
  22453. {
  22454. EXPECT_DECLS;
  22455. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  22456. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22457. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  22458. ecc_key key;
  22459. WC_RNG rng;
  22460. int ret;
  22461. mp_int r;
  22462. mp_int s;
  22463. mp_int z;
  22464. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  22465. unsigned char iHash[] = "Everyone gets Friday off.......";
  22466. unsigned char shortHash[] = TEST_STRING;
  22467. word32 hashlen = sizeof(hash);
  22468. word32 iHashLen = sizeof(iHash);
  22469. word32 shortHashLen = sizeof(shortHash);
  22470. int keySz = KEY32;
  22471. int verify_ok = 0;
  22472. XMEMSET(&key, 0, sizeof(ecc_key));
  22473. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22474. XMEMSET(&r, 0, sizeof(mp_int));
  22475. XMEMSET(&s, 0, sizeof(mp_int));
  22476. XMEMSET(&z, 0, sizeof(mp_int));
  22477. /* Initialize r, s and z. */
  22478. ExpectIntEQ(mp_init_multi(&r, &s, &z, NULL, NULL, NULL), MP_OKAY);
  22479. ExpectIntEQ(wc_ecc_init(&key), 0);
  22480. ExpectIntEQ(wc_InitRng(&rng), 0);
  22481. ret = wc_ecc_make_key(&rng, keySz, &key);
  22482. #if defined(WOLFSSL_ASYNC_CRYPT)
  22483. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22484. #endif
  22485. ExpectIntEQ(ret, 0);
  22486. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s), 0);
  22487. /* verify_ok should be 1. */
  22488. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key),
  22489. 0);
  22490. ExpectIntEQ(verify_ok, 1);
  22491. /* verify_ok should be 0 */
  22492. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen, &verify_ok,
  22493. &key), 0);
  22494. ExpectIntEQ(verify_ok, 0);
  22495. /* verify_ok should be 0. */
  22496. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22497. &verify_ok, &key), 0);
  22498. ExpectIntEQ(verify_ok, 0);
  22499. /* Test bad args. */
  22500. ExpectIntEQ(wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s),
  22501. ECC_BAD_ARG_E);
  22502. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s),
  22503. ECC_BAD_ARG_E);
  22504. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s),
  22505. ECC_BAD_ARG_E);
  22506. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s),
  22507. ECC_BAD_ARG_E);
  22508. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL),
  22509. ECC_BAD_ARG_E);
  22510. /* Test bad args. */
  22511. ExpectIntEQ(wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen,
  22512. &verify_ok, &key), ECC_BAD_ARG_E);
  22513. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  22514. &verify_ok, &key), ECC_BAD_ARG_E);
  22515. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen,
  22516. &verify_ok, &key), MP_ZERO_E);
  22517. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen,
  22518. &verify_ok, &key), MP_ZERO_E);
  22519. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen,
  22520. &verify_ok, &key), MP_ZERO_E);
  22521. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  22522. &key), ECC_BAD_ARG_E);
  22523. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen, NULL,
  22524. &key), ECC_BAD_ARG_E);
  22525. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22526. &verify_ok, NULL), ECC_BAD_ARG_E);
  22527. wc_ecc_free(&key);
  22528. mp_free(&r);
  22529. mp_free(&s);
  22530. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22531. #endif
  22532. return EXPECT_RESULT();
  22533. } /* END test_wc_ecc_verify_hash_ex */
  22534. /*
  22535. * Testing wc_ecc_mulmod()
  22536. */
  22537. static int test_wc_ecc_mulmod(void)
  22538. {
  22539. EXPECT_DECLS;
  22540. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  22541. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  22542. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  22543. ecc_key key1;
  22544. ecc_key key2;
  22545. ecc_key key3;
  22546. WC_RNG rng;
  22547. int ret;
  22548. XMEMSET(&key1, 0, sizeof(ecc_key));
  22549. XMEMSET(&key2, 0, sizeof(ecc_key));
  22550. XMEMSET(&key3, 0, sizeof(ecc_key));
  22551. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22552. ExpectIntEQ(wc_ecc_init(&key1), 0);
  22553. ExpectIntEQ(wc_ecc_init(&key2), 0);
  22554. ExpectIntEQ(wc_ecc_init(&key3), 0);
  22555. ExpectIntEQ(wc_InitRng(&rng), 0);
  22556. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  22557. #if defined(WOLFSSL_ASYNC_CRYPT)
  22558. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  22559. #endif
  22560. ExpectIntEQ(ret, 0);
  22561. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22562. ExpectIntEQ(wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy,
  22563. key1.dp->Af, ECC_SECP256R1), 0);
  22564. ExpectIntEQ(wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  22565. key1.dp->prime, ECC_SECP256R1), 0);
  22566. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  22567. &key3.pubkey, wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3),
  22568. 1), 0);
  22569. /* Test bad args. */
  22570. ExpectIntEQ(ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey,
  22571. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22572. ECC_BAD_ARG_E);
  22573. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), NULL, &key3.pubkey,
  22574. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22575. ECC_BAD_ARG_E);
  22576. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey, NULL,
  22577. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22578. ECC_BAD_ARG_E);
  22579. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  22580. &key3.pubkey, wc_ecc_key_get_priv(&key2), NULL, 1), ECC_BAD_ARG_E);
  22581. wc_ecc_free(&key1);
  22582. wc_ecc_free(&key2);
  22583. wc_ecc_free(&key3);
  22584. #ifdef FP_ECC
  22585. wc_ecc_fp_free();
  22586. #endif
  22587. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  22588. return EXPECT_RESULT();
  22589. } /* END test_wc_ecc_mulmod */
  22590. /*
  22591. * Testing wc_ecc_is_valid_idx()
  22592. */
  22593. static int test_wc_ecc_is_valid_idx(void)
  22594. {
  22595. EXPECT_DECLS;
  22596. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22597. ecc_key key;
  22598. WC_RNG rng;
  22599. int ret;
  22600. int iVal = -2;
  22601. int iVal2 = 3000;
  22602. XMEMSET(&key, 0, sizeof(ecc_key));
  22603. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22604. ExpectIntEQ(wc_ecc_init(&key), 0);
  22605. ExpectIntEQ(wc_InitRng(&rng), 0);
  22606. ret = wc_ecc_make_key(&rng, 32, &key);
  22607. #if defined(WOLFSSL_ASYNC_CRYPT)
  22608. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22609. #endif
  22610. ExpectIntEQ(ret, 0);
  22611. ExpectIntEQ(wc_ecc_is_valid_idx(key.idx), 1);
  22612. /* Test bad args. */
  22613. ExpectIntEQ(wc_ecc_is_valid_idx(iVal), 0);
  22614. ExpectIntEQ(wc_ecc_is_valid_idx(iVal2), 0);
  22615. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22616. wc_ecc_free(&key);
  22617. #ifdef FP_ECC
  22618. wc_ecc_fp_free();
  22619. #endif
  22620. #endif
  22621. return EXPECT_RESULT();
  22622. } /* END test_wc_ecc_is_valid_idx */
  22623. /*
  22624. * Testing wc_ecc_get_curve_id_from_oid()
  22625. */
  22626. static int test_wc_ecc_get_curve_id_from_oid(void)
  22627. {
  22628. EXPECT_DECLS;
  22629. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22630. !defined(HAVE_FIPS)
  22631. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  22632. word32 len = sizeof(oid);
  22633. /* Bad Cases */
  22634. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(NULL, len), BAD_FUNC_ARG);
  22635. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, 0), ECC_CURVE_INVALID);
  22636. /* Good Case */
  22637. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, len), ECC_SECP256R1);
  22638. #endif
  22639. return EXPECT_RESULT();
  22640. } /* END test_wc_ecc_get_curve_id_from_oid */
  22641. /*
  22642. * Testing wc_ecc_sig_size_calc()
  22643. */
  22644. static int test_wc_ecc_sig_size_calc(void)
  22645. {
  22646. EXPECT_DECLS;
  22647. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  22648. ecc_key key;
  22649. WC_RNG rng;
  22650. int sz = 0;
  22651. int ret;
  22652. XMEMSET(&key, 0, sizeof(ecc_key));
  22653. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22654. ExpectIntEQ(wc_ecc_init(&key), 0);
  22655. ExpectIntEQ(wc_InitRng(&rng), 0);
  22656. ret = wc_ecc_make_key(&rng, 16, &key);
  22657. #if defined(WOLFSSL_ASYNC_CRYPT)
  22658. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22659. #endif
  22660. #if FIPS_VERSION3_GE(6,0,0)
  22661. ExpectIntEQ(ret, BAD_FUNC_ARG);
  22662. #else
  22663. ExpectIntEQ(ret, 0);
  22664. #endif
  22665. #if FIPS_VERSION3_LT(6,0,0)
  22666. sz = key.dp->size;
  22667. ExpectIntGT(wc_ecc_sig_size_calc(sz), 0);
  22668. #else
  22669. (void) sz;
  22670. #endif
  22671. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22672. wc_ecc_free(&key);
  22673. #endif
  22674. return EXPECT_RESULT();
  22675. } /* END test_wc_ecc_sig_size_calc */
  22676. /*
  22677. * Testing wc_ecc_sm2_make_key()
  22678. */
  22679. static int test_wc_ecc_sm2_make_key(void)
  22680. {
  22681. int res = TEST_SKIPPED;
  22682. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  22683. EXPECT_DECLS;
  22684. WC_RNG rng[1];
  22685. ecc_key key[1];
  22686. XMEMSET(rng, 0, sizeof(*rng));
  22687. XMEMSET(key, 0, sizeof(*key));
  22688. ExpectIntEQ(wc_InitRng(rng), 0);
  22689. ExpectIntEQ(wc_ecc_init(key), 0);
  22690. /* Test invalid parameters. */
  22691. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, NULL, WC_ECC_FLAG_NONE),
  22692. BAD_FUNC_ARG);
  22693. ExpectIntEQ(wc_ecc_sm2_make_key(rng, NULL, WC_ECC_FLAG_NONE),
  22694. BAD_FUNC_ARG);
  22695. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, key, WC_ECC_FLAG_NONE),
  22696. BAD_FUNC_ARG);
  22697. /* Test valid parameters. */
  22698. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  22699. ExpectIntEQ(key->dp->id, ECC_SM2P256V1);
  22700. wc_ecc_free(key);
  22701. wc_FreeRng(rng);
  22702. #ifdef FP_ECC
  22703. wc_ecc_fp_free();
  22704. #endif
  22705. res = EXPECT_RESULT();
  22706. #endif
  22707. return res;
  22708. }
  22709. /*
  22710. * Testing wc_ecc_sm2_shared_secret()
  22711. */
  22712. static int test_wc_ecc_sm2_shared_secret(void)
  22713. {
  22714. int res = TEST_SKIPPED;
  22715. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  22716. EXPECT_DECLS;
  22717. WC_RNG rng[1];
  22718. ecc_key keyA[1];
  22719. ecc_key keyB[1];
  22720. byte outA[32];
  22721. byte outB[32];
  22722. word32 outALen = 32;
  22723. word32 outBLen = 32;
  22724. XMEMSET(rng, 0, sizeof(*rng));
  22725. XMEMSET(keyA, 0, sizeof(*keyA));
  22726. XMEMSET(keyB, 0, sizeof(*keyB));
  22727. ExpectIntEQ(wc_InitRng(rng), 0);
  22728. ExpectIntEQ(wc_ecc_init(keyA), 0);
  22729. ExpectIntEQ(wc_ecc_init(keyB), 0);
  22730. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyA, WC_ECC_FLAG_NONE), 0);
  22731. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyB, WC_ECC_FLAG_NONE), 0);
  22732. #ifdef ECC_TIMING_RESISTANT
  22733. ExpectIntEQ(wc_ecc_set_rng(keyA, rng), 0);
  22734. ExpectIntEQ(wc_ecc_set_rng(keyB, rng), 0);
  22735. #endif
  22736. /* Test invalid parameters. */
  22737. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  22738. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, NULL, NULL), BAD_FUNC_ARG);
  22739. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, NULL, NULL), BAD_FUNC_ARG);
  22740. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, outA, NULL), BAD_FUNC_ARG);
  22741. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, &outALen),
  22742. BAD_FUNC_ARG);
  22743. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, outA, &outALen),
  22744. BAD_FUNC_ARG);
  22745. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, outA, &outALen),
  22746. BAD_FUNC_ARG);
  22747. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, NULL, &outALen),
  22748. BAD_FUNC_ARG);
  22749. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, NULL), BAD_FUNC_ARG);
  22750. /* Test valid parameters. */
  22751. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, &outALen), 0);
  22752. ExpectIntLE(outALen, 32);
  22753. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyB, keyA, outB, &outBLen), 0);
  22754. ExpectIntLE(outBLen, 32);
  22755. ExpectIntEQ(outALen, outBLen);
  22756. ExpectBufEQ(outA, outB, outALen);
  22757. wc_ecc_free(keyB);
  22758. wc_ecc_free(keyA);
  22759. wc_FreeRng(rng);
  22760. #ifdef FP_ECC
  22761. wc_ecc_fp_free();
  22762. #endif
  22763. res = EXPECT_RESULT();
  22764. #endif
  22765. return res;
  22766. }
  22767. /*
  22768. * Testing wc_ecc_sm2_create_digest()
  22769. */
  22770. static int test_wc_ecc_sm2_create_digest(void)
  22771. {
  22772. int res = TEST_SKIPPED;
  22773. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && !defined(NO_HASH_WRAPPER) && \
  22774. (defined(WOLFSSL_SM3) || !defined(NO_SHA256))
  22775. EXPECT_DECLS;
  22776. ecc_key key[1];
  22777. enum wc_HashType hashType;
  22778. unsigned char pub[] = {
  22779. 0x04,
  22780. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22781. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22782. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22783. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22784. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22785. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22786. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22787. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22788. };
  22789. unsigned char id[] = {
  22790. 0x01, 0x02, 0x03,
  22791. };
  22792. unsigned char msg[] = {
  22793. 0x01, 0x02, 0x03,
  22794. };
  22795. unsigned char hash[32];
  22796. #ifdef WOLFSSL_SM3
  22797. unsigned char expHash[32] = {
  22798. 0xc1, 0xdd, 0x92, 0xc5, 0x60, 0xd3, 0x94, 0x28,
  22799. 0xeb, 0x0f, 0x57, 0x79, 0x3f, 0xc9, 0x96, 0xc5,
  22800. 0xfa, 0xf5, 0x90, 0xb2, 0x64, 0x2f, 0xaf, 0x9c,
  22801. 0xc8, 0x57, 0x21, 0x6a, 0x52, 0x7e, 0xf1, 0x95
  22802. };
  22803. #else
  22804. unsigned char expHash[32] = {
  22805. 0xea, 0x41, 0x55, 0x21, 0x61, 0x00, 0x5c, 0x9a,
  22806. 0x57, 0x35, 0x6b, 0x49, 0xca, 0x8f, 0x65, 0xc2,
  22807. 0x0e, 0x29, 0x0c, 0xa0, 0x1d, 0xa7, 0xc4, 0xed,
  22808. 0xdd, 0x51, 0x12, 0xf6, 0xe7, 0x55, 0xc5, 0xf4
  22809. };
  22810. #endif
  22811. #ifdef WOLFSSL_SM3
  22812. hashType = WC_HASH_TYPE_SM3;
  22813. #else
  22814. hashType = WC_HASH_TYPE_SHA256;
  22815. #endif
  22816. XMEMSET(key, 0, sizeof(*key));
  22817. ExpectIntEQ(wc_ecc_init(key), 0);
  22818. /* Test with no curve set. */
  22819. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22820. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22821. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22822. /* Test invalid parameters. */
  22823. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22824. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22825. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  22826. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22827. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  22828. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22829. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22830. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  22831. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22832. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  22833. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  22834. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22835. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  22836. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22837. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22838. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  22839. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22840. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  22841. /* Bad hash type. */
  22842. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22843. -1, hash, 0, key), BAD_FUNC_ARG);
  22844. /* Bad hash size. */
  22845. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22846. hashType, hash, 0, key), BUFFER_E);
  22847. /* Test valid parameters. */
  22848. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22849. hashType, hash, sizeof(hash), key), 0);
  22850. ExpectBufEQ(hash, expHash, sizeof(expHash));
  22851. wc_ecc_free(key);
  22852. res = EXPECT_RESULT();
  22853. #endif
  22854. return res;
  22855. }
  22856. /*
  22857. * Testing wc_ecc_sm2_verify_hash_ex()
  22858. */
  22859. static int test_wc_ecc_sm2_verify_hash_ex(void)
  22860. {
  22861. int res = TEST_SKIPPED;
  22862. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY) && \
  22863. defined(WOLFSSL_PUBLIC_MP)
  22864. EXPECT_DECLS;
  22865. ecc_key key[1];
  22866. mp_int r[1];
  22867. mp_int s[1];
  22868. int verified;
  22869. unsigned char pub[] = {
  22870. 0x04,
  22871. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22872. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22873. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22874. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22875. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22876. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22877. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22878. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22879. };
  22880. unsigned char hash[] = {
  22881. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  22882. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  22883. 0x50, 0x69, 0x5B, 0x20
  22884. };
  22885. unsigned char rData[] = {
  22886. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  22887. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  22888. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  22889. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE7,
  22890. };
  22891. unsigned char sData[] = {
  22892. 0x1D,
  22893. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22894. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22895. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22896. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  22897. };
  22898. unsigned char rBadData[] = {
  22899. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  22900. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  22901. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  22902. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE8,
  22903. };
  22904. XMEMSET(key, 0, sizeof(*key));
  22905. XMEMSET(r, 0, sizeof(*r));
  22906. XMEMSET(s, 0, sizeof(*s));
  22907. ExpectIntEQ(mp_init(r), 0);
  22908. ExpectIntEQ(mp_init(s), 0);
  22909. ExpectIntEQ(mp_read_unsigned_bin(r, rData, sizeof(rData)), 0);
  22910. ExpectIntEQ(mp_read_unsigned_bin(s, sData, sizeof(sData)), 0);
  22911. ExpectIntEQ(wc_ecc_init(key), 0);
  22912. /* Test with no curve set. */
  22913. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22914. &verified, key), BAD_FUNC_ARG);
  22915. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22916. /* Test invalid parameters. */
  22917. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22918. NULL, NULL), BAD_FUNC_ARG);
  22919. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, NULL, sizeof(hash),
  22920. NULL, NULL), BAD_FUNC_ARG);
  22921. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, NULL, sizeof(hash),
  22922. NULL, NULL), BAD_FUNC_ARG);
  22923. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, hash, sizeof(hash),
  22924. NULL, NULL), BAD_FUNC_ARG);
  22925. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22926. &verified, NULL), BAD_FUNC_ARG);
  22927. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22928. NULL, key), BAD_FUNC_ARG);
  22929. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, hash, sizeof(hash),
  22930. &verified, key), BAD_FUNC_ARG);
  22931. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, hash, sizeof(hash),
  22932. &verified, key), BAD_FUNC_ARG);
  22933. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, NULL, sizeof(hash),
  22934. &verified, key), BAD_FUNC_ARG);
  22935. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22936. NULL, key), BAD_FUNC_ARG);
  22937. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22938. &verified, NULL), BAD_FUNC_ARG);
  22939. /* Make key not on the SM2 curve. */
  22940. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22941. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22942. &verified, key), BAD_FUNC_ARG);
  22943. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22944. /* Test valid parameters. */
  22945. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22946. &verified, key), 0);
  22947. ExpectIntEQ(verified, 1);
  22948. ExpectIntEQ(mp_read_unsigned_bin(r, rBadData, sizeof(rBadData)), 0);
  22949. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22950. &verified, key), 0);
  22951. ExpectIntEQ(verified, 0);
  22952. mp_free(s);
  22953. mp_free(r);
  22954. wc_ecc_free(key);
  22955. #ifdef FP_ECC
  22956. wc_ecc_fp_free();
  22957. #endif
  22958. res = EXPECT_RESULT();
  22959. #endif
  22960. return res;
  22961. }
  22962. /*
  22963. * Testing wc_ecc_sm2_verify_hash()
  22964. */
  22965. static int test_wc_ecc_sm2_verify_hash(void)
  22966. {
  22967. int res = TEST_SKIPPED;
  22968. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY)
  22969. EXPECT_DECLS;
  22970. ecc_key key[1];
  22971. int verified;
  22972. unsigned char pub[] = {
  22973. 0x04,
  22974. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22975. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22976. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22977. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22978. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22979. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22980. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22981. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22982. };
  22983. unsigned char hash[] = {
  22984. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  22985. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  22986. 0x50, 0x69, 0x5B, 0x20
  22987. };
  22988. unsigned char sig[] = {
  22989. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  22990. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  22991. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  22992. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  22993. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  22994. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22995. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22996. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22997. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  22998. };
  22999. unsigned char sigBad[] = {
  23000. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  23001. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  23002. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  23003. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  23004. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  23005. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  23006. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  23007. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  23008. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDD
  23009. };
  23010. XMEMSET(key, 0, sizeof(*key));
  23011. ExpectIntEQ(wc_ecc_init(key), 0);
  23012. /* Test with no curve set. */
  23013. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23014. &verified, key), BAD_FUNC_ARG);
  23015. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  23016. /* Test invalid parameters. */
  23017. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  23018. NULL, NULL), BAD_FUNC_ARG);
  23019. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  23020. NULL, NULL), BAD_FUNC_ARG);
  23021. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  23022. NULL, NULL), BAD_FUNC_ARG);
  23023. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  23024. &verified, NULL), BAD_FUNC_ARG);
  23025. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  23026. NULL, key), BAD_FUNC_ARG);
  23027. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  23028. &verified, key), BAD_FUNC_ARG);
  23029. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  23030. &verified, key), BAD_FUNC_ARG);
  23031. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23032. NULL, key), BAD_FUNC_ARG);
  23033. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23034. &verified, NULL), BAD_FUNC_ARG);
  23035. /* Make key not on the SM2 curve. */
  23036. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23037. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23038. &verified, key), BAD_FUNC_ARG);
  23039. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23040. /* Test valid parameters. */
  23041. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23042. &verified, key), 0);
  23043. ExpectIntEQ(verified, 1);
  23044. ExpectIntEQ(wc_ecc_sm2_verify_hash(sigBad, sizeof(sigBad), hash,
  23045. sizeof(hash), &verified, key), 0);
  23046. ExpectIntEQ(verified, 0);
  23047. wc_ecc_free(key);
  23048. #ifdef FP_ECC
  23049. wc_ecc_fp_free();
  23050. #endif
  23051. res = EXPECT_RESULT();
  23052. #endif
  23053. return res;
  23054. }
  23055. /*
  23056. * Testing wc_ecc_sm2_verify_hash_ex()
  23057. */
  23058. static int test_wc_ecc_sm2_sign_hash_ex(void)
  23059. {
  23060. int res = TEST_SKIPPED;
  23061. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN) && \
  23062. defined(WOLFSSL_PUBLIC_MP)
  23063. EXPECT_DECLS;
  23064. WC_RNG rng[1];
  23065. ecc_key key[1];
  23066. mp_int r[1];
  23067. mp_int s[1];
  23068. unsigned char hash[32];
  23069. #ifdef HAVE_ECC_VERIFY
  23070. int verified;
  23071. #endif
  23072. XMEMSET(rng, 0, sizeof(*rng));
  23073. XMEMSET(key, 0, sizeof(*key));
  23074. XMEMSET(r, 0, sizeof(*r));
  23075. XMEMSET(s, 0, sizeof(*s));
  23076. ExpectIntEQ(wc_InitRng(rng), 0);
  23077. ExpectIntEQ(mp_init(r), 0);
  23078. ExpectIntEQ(mp_init(s), 0);
  23079. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  23080. ExpectIntEQ(wc_ecc_init(key), 0);
  23081. /* Test with no curve set. */
  23082. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23083. BAD_FUNC_ARG);
  23084. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  23085. /* Test invalid parameters. */
  23086. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  23087. NULL), BAD_FUNC_ARG);
  23088. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, NULL, NULL,
  23089. NULL), BAD_FUNC_ARG);
  23090. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, NULL, NULL,
  23091. NULL), BAD_FUNC_ARG);
  23092. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, key, NULL,
  23093. NULL), BAD_FUNC_ARG);
  23094. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, r,
  23095. NULL), BAD_FUNC_ARG);
  23096. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  23097. s), BAD_FUNC_ARG);
  23098. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, key, r, s),
  23099. BAD_FUNC_ARG);
  23100. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, key, r, s),
  23101. BAD_FUNC_ARG);
  23102. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, NULL, r, s),
  23103. BAD_FUNC_ARG);
  23104. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, NULL, s),
  23105. BAD_FUNC_ARG);
  23106. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, NULL),
  23107. BAD_FUNC_ARG);
  23108. /* Make key not on the SM2 curve. */
  23109. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23110. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23111. BAD_FUNC_ARG);
  23112. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23113. #ifdef WOLFSSL_SP_MATH_ALL
  23114. {
  23115. mp_int smallR[1];
  23116. sp_init_size(smallR, 1);
  23117. /* Force failure in _ecc_sm2_calc_r_s by r being too small. */
  23118. ExpectIntLT(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key,
  23119. smallR, s), 0);
  23120. }
  23121. #endif
  23122. /* Test valid parameters. */
  23123. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23124. 0);
  23125. #ifdef HAVE_ECC_VERIFY
  23126. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash), &verified,
  23127. key), 0);
  23128. ExpectIntEQ(verified, 1);
  23129. #endif
  23130. mp_free(s);
  23131. mp_free(r);
  23132. wc_ecc_free(key);
  23133. wc_FreeRng(rng);
  23134. #ifdef FP_ECC
  23135. wc_ecc_fp_free();
  23136. #endif
  23137. res = EXPECT_RESULT();
  23138. #endif
  23139. return res;
  23140. }
  23141. /*
  23142. * Testing wc_ecc_sm2_verify_hash()
  23143. */
  23144. static int test_wc_ecc_sm2_sign_hash(void)
  23145. {
  23146. int res = TEST_SKIPPED;
  23147. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN)
  23148. EXPECT_DECLS;
  23149. WC_RNG rng[1];
  23150. ecc_key key[1];
  23151. unsigned char hash[32];
  23152. unsigned char sig[72];
  23153. word32 sigSz = sizeof(sig);
  23154. #ifdef HAVE_ECC_VERIFY
  23155. int verified;
  23156. #endif
  23157. XMEMSET(rng, 0, sizeof(*rng));
  23158. XMEMSET(key, 0, sizeof(*key));
  23159. ExpectIntEQ(wc_InitRng(rng), 0);
  23160. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  23161. ExpectIntEQ(wc_ecc_init(key), 0);
  23162. /* Test with no curve set. */
  23163. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23164. BAD_FUNC_ARG);
  23165. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  23166. /* Test invalid parameters. */
  23167. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  23168. NULL), BAD_FUNC_ARG);
  23169. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, NULL, NULL,
  23170. NULL), BAD_FUNC_ARG);
  23171. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, NULL, NULL,
  23172. NULL), BAD_FUNC_ARG);
  23173. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, &sigSz, NULL,
  23174. NULL), BAD_FUNC_ARG);
  23175. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, rng,
  23176. NULL), BAD_FUNC_ARG);
  23177. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  23178. key), BAD_FUNC_ARG);
  23179. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, &sigSz, rng,
  23180. key), BAD_FUNC_ARG);
  23181. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, &sigSz, rng,
  23182. key), BAD_FUNC_ARG);
  23183. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, NULL, rng,
  23184. key), BAD_FUNC_ARG);
  23185. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, NULL,
  23186. key), BAD_FUNC_ARG);
  23187. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng,
  23188. NULL), BAD_FUNC_ARG);
  23189. /* Make key not on the SM2 curve. */
  23190. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23191. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23192. BAD_FUNC_ARG);
  23193. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23194. /* Test valid parameters. */
  23195. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23196. 0);
  23197. #ifdef HAVE_ECC_VERIFY
  23198. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sigSz, hash, sizeof(hash),
  23199. &verified, key), 0);
  23200. ExpectIntEQ(verified, 1);
  23201. #endif
  23202. wc_ecc_free(key);
  23203. wc_FreeRng(rng);
  23204. #ifdef FP_ECC
  23205. wc_ecc_fp_free();
  23206. #endif
  23207. res = EXPECT_RESULT();
  23208. #endif
  23209. return res;
  23210. }
  23211. /*
  23212. * Testing ToTraditional
  23213. */
  23214. static int test_ToTraditional(void)
  23215. {
  23216. EXPECT_DECLS;
  23217. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  23218. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  23219. defined(OPENSSL_EXTRA_X509_SMALL)) && !defined(NO_FILESYSTEM)
  23220. XFILE f = XBADFILE;
  23221. byte input[TWOK_BUF];
  23222. word32 sz = 0;
  23223. ExpectTrue((f = XFOPEN("./certs/server-keyPkcs8.der", "rb")) != XBADFILE);
  23224. ExpectTrue((sz = (word32)XFREAD(input, 1, sizeof(input), f)) > 0);
  23225. if (f != XBADFILE)
  23226. XFCLOSE(f);
  23227. /* Good case */
  23228. ExpectIntGT(ToTraditional(input, sz), 0);
  23229. /* Bad cases */
  23230. ExpectIntEQ(ToTraditional(NULL, 0), BAD_FUNC_ARG);
  23231. ExpectIntEQ(ToTraditional(NULL, sz), BAD_FUNC_ARG);
  23232. #ifdef WOLFSSL_ASN_TEMPLATE
  23233. ExpectIntEQ(ToTraditional(input, 0), BUFFER_E);
  23234. #else
  23235. ExpectIntEQ(ToTraditional(input, 0), ASN_PARSE_E);
  23236. #endif
  23237. #endif
  23238. return EXPECT_RESULT();
  23239. } /* End test_ToTraditional*/
  23240. /*
  23241. * Testing wc_EccPrivateKeyToDer
  23242. */
  23243. static int test_wc_EccPrivateKeyToDer(void)
  23244. {
  23245. EXPECT_DECLS;
  23246. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  23247. byte output[ONEK_BUF];
  23248. ecc_key eccKey;
  23249. WC_RNG rng;
  23250. word32 inLen;
  23251. word32 outLen = 0;
  23252. int ret;
  23253. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23254. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23255. ExpectIntEQ(wc_InitRng(&rng), 0);
  23256. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23257. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23258. #if defined(WOLFSSL_ASYNC_CRYPT)
  23259. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23260. #endif
  23261. ExpectIntEQ(ret, 0);
  23262. inLen = (word32)sizeof(output);
  23263. /* Bad Cases */
  23264. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23265. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23266. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, NULL, inLen), LENGTH_ONLY_E);
  23267. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, output, 0), BAD_FUNC_ARG);
  23268. /* Good Case */
  23269. ExpectIntGT(outLen = wc_EccPrivateKeyToDer(&eccKey, output, inLen), 0);
  23270. wc_ecc_free(&eccKey);
  23271. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23272. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  23273. {
  23274. /* test importing private only into a PKEY struct */
  23275. EC_KEY* ec = NULL;
  23276. EVP_PKEY* pkey = NULL;
  23277. const unsigned char* der;
  23278. der = output;
  23279. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, outLen));
  23280. der = output;
  23281. ExpectNotNull(ec = d2i_ECPrivateKey(NULL, &der, outLen));
  23282. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  23283. if (EXPECT_FAIL()) {
  23284. EC_KEY_free(ec);
  23285. }
  23286. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  23287. }
  23288. #endif
  23289. #endif
  23290. return EXPECT_RESULT();
  23291. } /* End test_wc_EccPrivateKeyToDer*/
  23292. /*
  23293. * Testing wc_DhPublicKeyDecode
  23294. */
  23295. static int test_wc_DhPublicKeyDecode(void)
  23296. {
  23297. EXPECT_DECLS;
  23298. #ifndef NO_DH
  23299. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  23300. DhKey key;
  23301. word32 inOutIdx;
  23302. XMEMSET(&key, 0, sizeof(DhKey));
  23303. ExpectIntEQ(wc_InitDhKey(&key), 0);
  23304. ExpectIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0), BAD_FUNC_ARG);
  23305. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23306. BAD_FUNC_ARG);
  23307. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23308. BAD_FUNC_ARG);
  23309. inOutIdx = 0;
  23310. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  23311. BAD_FUNC_ARG);
  23312. inOutIdx = 0;
  23313. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  23314. BAD_FUNC_ARG);
  23315. inOutIdx = 0;
  23316. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  23317. sizeof_dh_pub_key_der_2048), 0);
  23318. ExpectIntNE(key.p.used, 0);
  23319. ExpectIntNE(key.g.used, 0);
  23320. ExpectIntEQ(key.q.used, 0);
  23321. ExpectIntNE(key.pub.used, 0);
  23322. ExpectIntEQ(key.priv.used, 0);
  23323. DoExpectIntEQ(wc_FreeDhKey(&key), 0);
  23324. #endif
  23325. #endif /* !NO_DH */
  23326. return EXPECT_RESULT();
  23327. }
  23328. /*
  23329. * Testing wc_Ed25519KeyToDer
  23330. */
  23331. static int test_wc_Ed25519KeyToDer(void)
  23332. {
  23333. EXPECT_DECLS;
  23334. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23335. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23336. byte output[ONEK_BUF];
  23337. ed25519_key ed25519Key;
  23338. WC_RNG rng;
  23339. word32 inLen;
  23340. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23341. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23342. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23343. ExpectIntEQ(wc_InitRng(&rng), 0);
  23344. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23345. inLen = (word32)sizeof(output);
  23346. /* Bad Cases */
  23347. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23348. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23349. ExpectIntEQ(wc_Ed25519KeyToDer(&ed25519Key, output, 0), BAD_FUNC_ARG);
  23350. /* Good Cases */
  23351. /* length only */
  23352. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen), 0);
  23353. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, output, inLen), 0);
  23354. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23355. wc_ed25519_free(&ed25519Key);
  23356. #endif
  23357. return EXPECT_RESULT();
  23358. } /* End test_wc_Ed25519KeyToDer*/
  23359. /*
  23360. * Testing wc_Ed25519PrivateKeyToDer
  23361. */
  23362. static int test_wc_Ed25519PrivateKeyToDer(void)
  23363. {
  23364. EXPECT_DECLS;
  23365. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23366. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23367. byte output[ONEK_BUF];
  23368. ed25519_key ed25519PrivKey;
  23369. WC_RNG rng;
  23370. word32 inLen;
  23371. XMEMSET(&ed25519PrivKey, 0, sizeof(ed25519_key));
  23372. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23373. ExpectIntEQ(wc_ed25519_init(&ed25519PrivKey), 0);
  23374. ExpectIntEQ(wc_InitRng(&rng), 0);
  23375. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey),
  23376. 0);
  23377. inLen = (word32)sizeof(output);
  23378. /* Bad Cases */
  23379. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23380. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23381. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0),
  23382. BAD_FUNC_ARG);
  23383. /* Good Cases */
  23384. /* length only */
  23385. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen), 0);
  23386. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen), 0);
  23387. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23388. wc_ed25519_free(&ed25519PrivKey);
  23389. #endif
  23390. return EXPECT_RESULT();
  23391. } /* End test_wc_Ed25519PrivateKeyToDer*/
  23392. /*
  23393. * Testing wc_Ed448KeyToDer
  23394. */
  23395. static int test_wc_Ed448KeyToDer(void)
  23396. {
  23397. EXPECT_DECLS;
  23398. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23399. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23400. byte output[ONEK_BUF];
  23401. ed448_key ed448Key;
  23402. WC_RNG rng;
  23403. word32 inLen;
  23404. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23405. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23406. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23407. ExpectIntEQ(wc_InitRng(&rng), 0);
  23408. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23409. inLen = (word32)sizeof(output);
  23410. /* Bad Cases */
  23411. ExpectIntEQ(wc_Ed448KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23412. ExpectIntEQ(wc_Ed448KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23413. ExpectIntEQ(wc_Ed448KeyToDer(&ed448Key, output, 0), BAD_FUNC_ARG);
  23414. /* Good Cases */
  23415. /* length only */
  23416. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, NULL, inLen), 0);
  23417. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, output, inLen), 0);
  23418. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23419. wc_ed448_free(&ed448Key);
  23420. #endif
  23421. return EXPECT_RESULT();
  23422. } /* End test_wc_Ed448KeyToDer*/
  23423. /*
  23424. * Testing wc_Ed448PrivateKeyToDer
  23425. */
  23426. static int test_wc_Ed448PrivateKeyToDer(void)
  23427. {
  23428. EXPECT_DECLS;
  23429. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23430. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23431. byte output[ONEK_BUF];
  23432. ed448_key ed448PrivKey;
  23433. WC_RNG rng;
  23434. word32 inLen;
  23435. XMEMSET(&ed448PrivKey, 0, sizeof(ed448_key));
  23436. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23437. ExpectIntEQ(wc_ed448_init(&ed448PrivKey), 0);
  23438. ExpectIntEQ(wc_InitRng(&rng), 0);
  23439. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey),
  23440. 0);
  23441. inLen = (word32)sizeof(output);
  23442. /* Bad Cases */
  23443. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23444. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23445. ExpectIntEQ(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0),
  23446. BAD_FUNC_ARG);
  23447. /* Good cases */
  23448. /* length only */
  23449. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen), 0);
  23450. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen), 0);
  23451. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23452. wc_ed448_free(&ed448PrivKey);
  23453. #endif
  23454. return EXPECT_RESULT();
  23455. } /* End test_wc_Ed448PrivateKeyToDer*/
  23456. /*
  23457. * Testing wc_SetSubjectBuffer
  23458. */
  23459. static int test_wc_SetSubjectBuffer(void)
  23460. {
  23461. EXPECT_DECLS;
  23462. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  23463. Cert cert;
  23464. XFILE file = XBADFILE;
  23465. byte* der = NULL;
  23466. word32 derSz;
  23467. derSz = FOURK_BUF;
  23468. ExpectNotNull(der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  23469. DYNAMIC_TYPE_TMP_BUFFER));
  23470. ExpectTrue((file = XFOPEN("./certs/ca-cert.der", "rb")) != XBADFILE);
  23471. ExpectTrue((derSz = (word32)XFREAD(der, 1, FOURK_BUF, file)) > 0);
  23472. if (file != XBADFILE)
  23473. XFCLOSE(file);
  23474. ExpectIntEQ(wc_InitCert(&cert), 0);
  23475. ExpectIntEQ(wc_SetSubjectBuffer(&cert, der, derSz), 0);
  23476. ExpectIntEQ(wc_SetSubjectBuffer(NULL, der, derSz), BAD_FUNC_ARG);
  23477. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23478. #endif
  23479. return EXPECT_RESULT();
  23480. } /* End test_wc_SetSubjectBuffer*/
  23481. /*
  23482. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  23483. */
  23484. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  23485. {
  23486. EXPECT_DECLS;
  23487. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23488. WC_RNG rng;
  23489. Cert cert;
  23490. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23491. RsaKey rsaKey;
  23492. int bits = 2048;
  23493. #endif
  23494. #if defined(HAVE_ECC)
  23495. ecc_key eccKey;
  23496. int ret;
  23497. #endif
  23498. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23499. ed25519_key ed25519Key;
  23500. #endif
  23501. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23502. ed448_key ed448Key;
  23503. #endif
  23504. #ifndef HAVE_FIPS
  23505. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  23506. #else
  23507. ExpectIntEQ(wc_InitRng(&rng), 0);
  23508. #endif
  23509. ExpectIntEQ(wc_InitCert(&cert), 0);
  23510. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23511. /* RSA */
  23512. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  23513. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  23514. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  23515. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey),
  23516. 0);
  23517. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  23518. #endif
  23519. #if defined(HAVE_ECC)
  23520. /* ECC */
  23521. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23522. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23523. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23524. #if defined(WOLFSSL_ASYNC_CRYPT)
  23525. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23526. #endif
  23527. ExpectIntEQ(ret, 0);
  23528. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey),
  23529. 0);
  23530. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  23531. #endif
  23532. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23533. /* ED25519 */
  23534. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23535. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23536. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23537. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23538. &ed25519Key), 0);
  23539. wc_ed25519_free(&ed25519Key);
  23540. #endif
  23541. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23542. /* ED448 */
  23543. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23544. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23545. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23546. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  23547. &ed448Key), 0);
  23548. wc_ed448_free(&ed448Key);
  23549. #endif
  23550. wc_FreeRng(&rng);
  23551. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23552. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  23553. return EXPECT_RESULT();
  23554. } /* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  23555. /*
  23556. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  23557. */
  23558. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  23559. {
  23560. EXPECT_DECLS;
  23561. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23562. WC_RNG rng;
  23563. Cert cert;
  23564. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23565. RsaKey rsaKey;
  23566. int bits = 2048;
  23567. #endif
  23568. #if defined(HAVE_ECC)
  23569. ecc_key eccKey;
  23570. int ret;
  23571. #endif
  23572. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23573. ed25519_key ed25519Key;
  23574. #endif
  23575. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23576. ed448_key ed448Key;
  23577. #endif
  23578. #ifndef HAVE_FIPS
  23579. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  23580. #else
  23581. ExpectIntEQ(wc_InitRng(&rng), 0);
  23582. #endif
  23583. ExpectIntEQ(wc_InitCert(&cert), 0);
  23584. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23585. /* RSA */
  23586. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  23587. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  23588. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  23589. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey), 0);
  23590. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  23591. #endif
  23592. #if defined(HAVE_ECC)
  23593. /* ECC */
  23594. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23595. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23596. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23597. #if defined(WOLFSSL_ASYNC_CRYPT)
  23598. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23599. #endif
  23600. ExpectIntEQ(ret, 0);
  23601. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey), 0);
  23602. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  23603. #endif
  23604. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23605. /* ED25519 */
  23606. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23607. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23608. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23609. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23610. &ed25519Key), 0);
  23611. wc_ed25519_free(&ed25519Key);
  23612. #endif
  23613. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23614. /* ED448 */
  23615. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23616. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23617. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23618. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, &ed448Key),
  23619. 0);
  23620. wc_ed448_free(&ed448Key);
  23621. #endif
  23622. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23623. #endif /* defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  23624. return EXPECT_RESULT();
  23625. } /* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  23626. /*
  23627. * Testing wc_PKCS7_New()
  23628. */
  23629. static int test_wc_PKCS7_New(void)
  23630. {
  23631. EXPECT_DECLS;
  23632. #if defined(HAVE_PKCS7)
  23633. PKCS7* pkcs7 = NULL;
  23634. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, testDevId));
  23635. wc_PKCS7_Free(pkcs7);
  23636. #endif
  23637. return EXPECT_RESULT();
  23638. } /* END test-wc_PKCS7_New */
  23639. /*
  23640. * Testing wc_PKCS7_Init()
  23641. */
  23642. static int test_wc_PKCS7_Init(void)
  23643. {
  23644. EXPECT_DECLS;
  23645. #if defined(HAVE_PKCS7)
  23646. PKCS7* pkcs7 = NULL;
  23647. void* heap = NULL;
  23648. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  23649. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  23650. /* Pass in bad args. */
  23651. ExpectIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  23652. wc_PKCS7_Free(pkcs7);
  23653. #endif
  23654. return EXPECT_RESULT();
  23655. } /* END test-wc_PKCS7_Init */
  23656. /*
  23657. * Testing wc_PKCS7_InitWithCert()
  23658. */
  23659. static int test_wc_PKCS7_InitWithCert(void)
  23660. {
  23661. EXPECT_DECLS;
  23662. #if defined(HAVE_PKCS7)
  23663. PKCS7* pkcs7 = NULL;
  23664. #ifndef NO_RSA
  23665. #if defined(USE_CERT_BUFFERS_2048)
  23666. unsigned char cert[sizeof(client_cert_der_2048)];
  23667. int certSz = (int)sizeof(cert);
  23668. XMEMSET(cert, 0, certSz);
  23669. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  23670. #elif defined(USE_CERT_BUFFERS_1024)
  23671. unsigned char cert[sizeof(client_cert_der_1024)];
  23672. int certSz = (int)sizeof(cert);
  23673. XMEMSET(cert, 0, certSz);
  23674. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  23675. #else
  23676. unsigned char cert[ONEK_BUF];
  23677. XFILE fp = XBADFILE;
  23678. int certSz;
  23679. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23680. XBADFILE);
  23681. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23682. fp), 0);
  23683. if (fp != XBADFILE)
  23684. XFCLOSE(fp);
  23685. #endif
  23686. #elif defined(HAVE_ECC)
  23687. #if defined(USE_CERT_BUFFERS_256)
  23688. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23689. int certSz = (int)sizeof(cert);
  23690. XMEMSET(cert, 0, certSz);
  23691. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  23692. #else
  23693. unsigned char cert[ONEK_BUF];
  23694. XFILE fp = XBADFILE;
  23695. int certSz;
  23696. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23697. XBADFILE);
  23698. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256),
  23699. fp), 0);
  23700. if (fp != XBADFILE)
  23701. XFCLOSE(fp);
  23702. #endif
  23703. #else
  23704. #error PKCS7 requires ECC or RSA
  23705. #endif
  23706. #ifdef HAVE_ECC
  23707. {
  23708. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  23709. static unsigned char certWithInvalidEccKey[] = {
  23710. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  23711. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  23712. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  23713. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  23714. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23715. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  23716. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23717. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23718. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23719. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23720. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23721. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23722. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23723. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23724. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23725. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  23726. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  23727. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  23728. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  23729. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  23730. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  23731. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  23732. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  23733. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  23734. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  23735. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  23736. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  23737. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  23738. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  23739. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  23740. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  23741. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  23742. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  23743. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  23744. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  23745. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  23746. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  23747. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  23748. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  23749. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  23750. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23751. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  23752. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  23753. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23754. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  23755. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23756. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  23757. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23758. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23759. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23760. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23761. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23762. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23763. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23764. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23765. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23766. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  23767. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  23768. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  23769. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  23770. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  23771. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  23772. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  23773. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  23774. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  23775. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  23776. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  23777. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  23778. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  23779. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  23780. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  23781. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  23782. 0x08, 0xA8, 0xB4
  23783. };
  23784. #endif
  23785. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23786. /* If initialization is not successful, it's free'd in init func. */
  23787. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz),
  23788. 0);
  23789. wc_PKCS7_Free(pkcs7);
  23790. pkcs7 = NULL;
  23791. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23792. /* Valid initialization usage. */
  23793. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23794. /* Pass in bad args. No need free for null checks, free at end.*/
  23795. ExpectIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  23796. BAD_FUNC_ARG);
  23797. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  23798. BAD_FUNC_ARG);
  23799. #ifdef HAVE_ECC
  23800. ExpectIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  23801. sizeof(certWithInvalidEccKey)), 0);
  23802. }
  23803. #endif
  23804. wc_PKCS7_Free(pkcs7);
  23805. #endif
  23806. return EXPECT_RESULT();
  23807. } /* END test_wc_PKCS7_InitWithCert */
  23808. /*
  23809. * Testing wc_PKCS7_EncodeData()
  23810. */
  23811. static int test_wc_PKCS7_EncodeData(void)
  23812. {
  23813. EXPECT_DECLS;
  23814. #if defined(HAVE_PKCS7)
  23815. PKCS7* pkcs7 = NULL;
  23816. byte output[FOURK_BUF];
  23817. byte data[] = "My encoded DER cert.";
  23818. #ifndef NO_RSA
  23819. #if defined(USE_CERT_BUFFERS_2048)
  23820. unsigned char cert[sizeof(client_cert_der_2048)];
  23821. unsigned char key[sizeof(client_key_der_2048)];
  23822. int certSz = (int)sizeof(cert);
  23823. int keySz = (int)sizeof(key);
  23824. XMEMSET(cert, 0, certSz);
  23825. XMEMSET(key, 0, keySz);
  23826. XMEMCPY(cert, client_cert_der_2048, certSz);
  23827. XMEMCPY(key, client_key_der_2048, keySz);
  23828. #elif defined(USE_CERT_BUFFERS_1024)
  23829. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  23830. unsigned char key[sizeof_client_key_der_1024];
  23831. int certSz = (int)sizeof(cert);
  23832. int keySz = (int)sizeof(key);
  23833. XMEMSET(cert, 0, certSz);
  23834. XMEMSET(key, 0, keySz);
  23835. XMEMCPY(cert, client_cert_der_1024, certSz);
  23836. XMEMCPY(key, client_key_der_1024, keySz);
  23837. #else
  23838. unsigned char cert[ONEK_BUF];
  23839. unsigned char key[ONEK_BUF];
  23840. XFILE fp = XBADFILE;
  23841. int certSz;
  23842. int keySz;
  23843. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23844. XBADFILE);
  23845. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23846. fp), 0);
  23847. if (fp != XBADFILE) {
  23848. XFCLOSE(fp);
  23849. fp = XBADFILE;
  23850. }
  23851. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  23852. XBADFILE);
  23853. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  23854. 0);
  23855. if (fp != XBADFILE)
  23856. XFCLOSE(fp);
  23857. #endif
  23858. #elif defined(HAVE_ECC)
  23859. #if defined(USE_CERT_BUFFERS_256)
  23860. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23861. unsigned char key[sizeof(ecc_clikey_der_256)];
  23862. int certSz = (int)sizeof(cert);
  23863. int keySz = (int)sizeof(key);
  23864. XMEMSET(cert, 0, certSz);
  23865. XMEMSET(key, 0, keySz);
  23866. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23867. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23868. #else
  23869. unsigned char cert[ONEK_BUF];
  23870. unsigned char key[ONEK_BUF];
  23871. XFILE fp = XBADFILE;
  23872. int certSz, keySz;
  23873. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23874. XBADFILE);
  23875. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  23876. fp), 0);
  23877. if (fp != XBADFILE) {
  23878. XFCLOSE(fp);
  23879. fp = XBADFILE;
  23880. }
  23881. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  23882. XBADFILE);
  23883. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  23884. 0);
  23885. if (fp != XBADFILE)
  23886. XFCLOSE(fp);
  23887. #endif
  23888. #endif
  23889. XMEMSET(output, 0, sizeof(output));
  23890. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23891. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23892. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  23893. if (pkcs7 != NULL) {
  23894. pkcs7->content = data;
  23895. pkcs7->contentSz = sizeof(data);
  23896. pkcs7->privateKey = key;
  23897. pkcs7->privateKeySz = keySz;
  23898. }
  23899. ExpectIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  23900. /* Test bad args. */
  23901. ExpectIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  23902. BAD_FUNC_ARG);
  23903. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  23904. BAD_FUNC_ARG);
  23905. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  23906. wc_PKCS7_Free(pkcs7);
  23907. #endif
  23908. return EXPECT_RESULT();
  23909. } /* END test_wc_PKCS7_EncodeData */
  23910. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  23911. !defined(NO_RSA) && !defined(NO_SHA256)
  23912. /* RSA sign raw digest callback */
  23913. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  23914. byte* out, word32 outSz, byte* privateKey,
  23915. word32 privateKeySz, int devid, int hashOID)
  23916. {
  23917. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  23918. byte digInfoEncoding[] = {
  23919. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  23920. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  23921. 0x00, 0x04, 0x20
  23922. };
  23923. int ret;
  23924. byte digestInfo[ONEK_BUF];
  23925. byte sig[FOURK_BUF];
  23926. word32 digestInfoSz = 0;
  23927. word32 idx = 0;
  23928. RsaKey rsa;
  23929. /* SHA-256 required only for this example callback due to above
  23930. * digInfoEncoding[] */
  23931. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  23932. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  23933. (hashOID != SHA256h)) {
  23934. return -1;
  23935. }
  23936. /* build DigestInfo */
  23937. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  23938. digestInfoSz += sizeof(digInfoEncoding);
  23939. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  23940. digestInfoSz += digestSz;
  23941. /* set up RSA key */
  23942. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  23943. if (ret != 0) {
  23944. return ret;
  23945. }
  23946. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  23947. /* sign DigestInfo */
  23948. if (ret == 0) {
  23949. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  23950. &rsa, pkcs7->rng);
  23951. if (ret > 0) {
  23952. if (ret > (int)outSz) {
  23953. /* output buffer too small */
  23954. ret = -1;
  23955. }
  23956. else {
  23957. /* success, ret holds sig size */
  23958. XMEMCPY(out, sig, ret);
  23959. }
  23960. }
  23961. }
  23962. wc_FreeRsaKey(&rsa);
  23963. return ret;
  23964. }
  23965. #endif
  23966. #if defined(HAVE_PKCS7) && defined(ASN_BER_TO_DER)
  23967. typedef struct encodeSignedDataStream {
  23968. byte out[FOURK_BUF*3];
  23969. int idx;
  23970. word32 outIdx;
  23971. } encodeSignedDataStream;
  23972. /* content is 8k of partially created bundle */
  23973. static int GetContentCB(PKCS7* pkcs7, byte** content, void* ctx)
  23974. {
  23975. int ret = 0;
  23976. encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
  23977. if (strm->outIdx < pkcs7->contentSz) {
  23978. ret = (pkcs7->contentSz > strm->outIdx + FOURK_BUF)?
  23979. FOURK_BUF : pkcs7->contentSz - strm->outIdx;
  23980. *content = strm->out + strm->outIdx;
  23981. strm->outIdx += ret;
  23982. }
  23983. (void)pkcs7;
  23984. return ret;
  23985. }
  23986. static int StreamOutputCB(PKCS7* pkcs7, const byte* output, word32 outputSz,
  23987. void* ctx)
  23988. {
  23989. encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
  23990. XMEMCPY(strm->out + strm->idx, output, outputSz);
  23991. strm->idx += outputSz;
  23992. (void)pkcs7;
  23993. return 0;
  23994. }
  23995. #endif
  23996. /*
  23997. * Testing wc_PKCS7_EncodeSignedData()
  23998. */
  23999. static int test_wc_PKCS7_EncodeSignedData(void)
  24000. {
  24001. EXPECT_DECLS;
  24002. #if defined(HAVE_PKCS7)
  24003. PKCS7* pkcs7 = NULL;
  24004. WC_RNG rng;
  24005. byte output[FOURK_BUF];
  24006. byte badOut[1];
  24007. word32 outputSz = (word32)sizeof(output);
  24008. word32 badOutSz = 0;
  24009. byte data[] = "Test data to encode.";
  24010. #ifndef NO_RSA
  24011. #if defined(USE_CERT_BUFFERS_2048)
  24012. byte key[sizeof(client_key_der_2048)];
  24013. byte cert[sizeof(client_cert_der_2048)];
  24014. word32 keySz = (word32)sizeof(key);
  24015. word32 certSz = (word32)sizeof(cert);
  24016. XMEMSET(key, 0, keySz);
  24017. XMEMSET(cert, 0, certSz);
  24018. XMEMCPY(key, client_key_der_2048, keySz);
  24019. XMEMCPY(cert, client_cert_der_2048, certSz);
  24020. #elif defined(USE_CERT_BUFFERS_1024)
  24021. byte key[sizeof_client_key_der_1024];
  24022. byte cert[sizeof(sizeof_client_cert_der_1024)];
  24023. word32 keySz = (word32)sizeof(key);
  24024. word32 certSz = (word32)sizeof(cert);
  24025. XMEMSET(key, 0, keySz);
  24026. XMEMSET(cert, 0, certSz);
  24027. XMEMCPY(key, client_key_der_1024, keySz);
  24028. XMEMCPY(cert, client_cert_der_1024, certSz);
  24029. #else
  24030. unsigned char cert[ONEK_BUF];
  24031. unsigned char key[ONEK_BUF];
  24032. XFILE fp = XBADFILE;
  24033. int certSz;
  24034. int keySz;
  24035. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  24036. XBADFILE);
  24037. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  24038. fp), 0);
  24039. if (fp != XBADFILE) {
  24040. XFCLOSE(fp);
  24041. fp = XBADFILE;
  24042. }
  24043. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  24044. XBADFILE);
  24045. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  24046. 0);
  24047. if (fp != XBADFILE)
  24048. XFCLOSE(fp);
  24049. #endif
  24050. #elif defined(HAVE_ECC)
  24051. #if defined(USE_CERT_BUFFERS_256)
  24052. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24053. unsigned char key[sizeof(ecc_clikey_der_256)];
  24054. int certSz = (int)sizeof(cert);
  24055. int keySz = (int)sizeof(key);
  24056. XMEMSET(cert, 0, certSz);
  24057. XMEMSET(key, 0, keySz);
  24058. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  24059. XMEMCPY(key, ecc_clikey_der_256, keySz);
  24060. #else
  24061. unsigned char cert[ONEK_BUF];
  24062. unsigned char key[ONEK_BUF];
  24063. XFILE fp = XBADFILE;
  24064. int certSz;
  24065. int keySz;
  24066. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  24067. XBADFILE);
  24068. ExpectIntGT(certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp), 0);
  24069. if (fp != XBADFILE) {
  24070. XFCLOSE(fp);
  24071. fp = XBADFILE;
  24072. }
  24073. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  24074. XBADFILE);
  24075. ExpectIntGT(keySz = (int)XFREAD(key, 1, ONEK_BUF, fp), 0);
  24076. if (fp != XBADFILE)
  24077. XFCLOSE(fp);
  24078. #endif
  24079. #endif
  24080. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24081. XMEMSET(output, 0, outputSz);
  24082. ExpectIntEQ(wc_InitRng(&rng), 0);
  24083. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24084. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24085. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24086. if (pkcs7 != NULL) {
  24087. pkcs7->content = data;
  24088. pkcs7->contentSz = (word32)sizeof(data);
  24089. pkcs7->privateKey = key;
  24090. pkcs7->privateKeySz = (word32)sizeof(key);
  24091. pkcs7->encryptOID = RSAk;
  24092. #ifdef NO_SHA
  24093. pkcs7->hashOID = SHA256h;
  24094. #else
  24095. pkcs7->hashOID = SHAh;
  24096. #endif
  24097. pkcs7->rng = &rng;
  24098. }
  24099. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  24100. wc_PKCS7_Free(pkcs7);
  24101. pkcs7 = NULL;
  24102. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24103. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24104. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24105. #ifdef ASN_BER_TO_DER
  24106. wc_PKCS7_Free(pkcs7);
  24107. /* reinitialize and test setting stream mode */
  24108. {
  24109. int signedSz = 0;
  24110. encodeSignedDataStream strm;
  24111. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24112. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24113. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24114. if (pkcs7 != NULL) {
  24115. pkcs7->content = data;
  24116. pkcs7->contentSz = (word32)sizeof(data);
  24117. pkcs7->privateKey = key;
  24118. pkcs7->privateKeySz = (word32)sizeof(key);
  24119. pkcs7->encryptOID = RSAk;
  24120. #ifdef NO_SHA
  24121. pkcs7->hashOID = SHA256h;
  24122. #else
  24123. pkcs7->hashOID = SHAh;
  24124. #endif
  24125. pkcs7->rng = &rng;
  24126. }
  24127. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  24128. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL), 0);
  24129. ExpectIntEQ(wc_PKCS7_SetStreamMode(NULL, 1, NULL, NULL, NULL),
  24130. BAD_FUNC_ARG);
  24131. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 1);
  24132. ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, output,
  24133. outputSz), 0);
  24134. wc_PKCS7_Free(pkcs7);
  24135. pkcs7 = NULL;
  24136. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24137. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24138. /* use exact signed buffer size since BER encoded */
  24139. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, signedSz), 0);
  24140. wc_PKCS7_Free(pkcs7);
  24141. /* now try with using callbacks for IO */
  24142. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24143. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24144. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24145. if (pkcs7 != NULL) {
  24146. pkcs7->contentSz = FOURK_BUF*2;
  24147. pkcs7->privateKey = key;
  24148. pkcs7->privateKeySz = (word32)sizeof(key);
  24149. pkcs7->encryptOID = RSAk;
  24150. #ifdef NO_SHA
  24151. pkcs7->hashOID = SHA256h;
  24152. #else
  24153. pkcs7->hashOID = SHAh;
  24154. #endif
  24155. pkcs7->rng = &rng;
  24156. }
  24157. XMEMSET(&strm, 0, sizeof(strm));
  24158. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
  24159. StreamOutputCB, (void*)&strm), 0);
  24160. ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, NULL, 0), 0);
  24161. wc_PKCS7_Free(pkcs7);
  24162. pkcs7 = NULL;
  24163. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24164. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24165. /* use exact signed buffer size since BER encoded */
  24166. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, strm.out, signedSz), 0);
  24167. }
  24168. #endif
  24169. #ifndef NO_PKCS7_STREAM
  24170. wc_PKCS7_Free(pkcs7);
  24171. pkcs7 = NULL;
  24172. {
  24173. word32 z;
  24174. int ret;
  24175. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24176. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24177. /* test for streaming mode */
  24178. ret = -1;
  24179. for (z = 0; z < outputSz && ret != 0; z++) {
  24180. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24181. if (ret < 0){
  24182. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24183. }
  24184. }
  24185. ExpectIntEQ(ret, 0);
  24186. ExpectIntNE(pkcs7->contentSz, 0);
  24187. ExpectNotNull(pkcs7->contentDynamic);
  24188. }
  24189. #endif /* !NO_PKCS7_STREAM */
  24190. /* Pass in bad args. */
  24191. ExpectIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz),
  24192. BAD_FUNC_ARG);
  24193. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  24194. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  24195. badOutSz), BAD_FUNC_ARG);
  24196. if (pkcs7 != NULL) {
  24197. pkcs7->hashOID = 0; /* bad hashOID */
  24198. }
  24199. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz),
  24200. BAD_FUNC_ARG);
  24201. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  24202. !defined(NO_RSA) && !defined(NO_SHA256)
  24203. /* test RSA sign raw digest callback, if using RSA and compiled in.
  24204. * Example callback assumes SHA-256, so only run test if compiled in. */
  24205. wc_PKCS7_Free(pkcs7);
  24206. pkcs7 = NULL;
  24207. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24208. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24209. if (pkcs7 != NULL) {
  24210. pkcs7->content = data;
  24211. pkcs7->contentSz = (word32)sizeof(data);
  24212. pkcs7->privateKey = key;
  24213. pkcs7->privateKeySz = (word32)sizeof(key);
  24214. pkcs7->encryptOID = RSAk;
  24215. pkcs7->hashOID = SHA256h;
  24216. pkcs7->rng = &rng;
  24217. }
  24218. ExpectIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  24219. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  24220. #endif
  24221. wc_PKCS7_Free(pkcs7);
  24222. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  24223. #endif
  24224. return EXPECT_RESULT();
  24225. } /* END test_wc_PKCS7_EncodeSignedData */
  24226. /*
  24227. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  24228. */
  24229. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  24230. {
  24231. EXPECT_DECLS;
  24232. #if defined(HAVE_PKCS7)
  24233. int i;
  24234. PKCS7* pkcs7 = NULL;
  24235. WC_RNG rng;
  24236. byte outputHead[FOURK_BUF/2];
  24237. byte outputFoot[FOURK_BUF/2];
  24238. word32 outputHeadSz = (word32)sizeof(outputHead);
  24239. word32 outputFootSz = (word32)sizeof(outputFoot);
  24240. byte data[FOURK_BUF];
  24241. wc_HashAlg hash;
  24242. #ifdef NO_SHA
  24243. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  24244. #else
  24245. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24246. #endif
  24247. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24248. word32 hashSz = wc_HashGetDigestSize(hashType);
  24249. #ifndef NO_RSA
  24250. #if defined(USE_CERT_BUFFERS_2048)
  24251. byte key[sizeof(client_key_der_2048)];
  24252. byte cert[sizeof(client_cert_der_2048)];
  24253. word32 keySz = (word32)sizeof(key);
  24254. word32 certSz = (word32)sizeof(cert);
  24255. XMEMSET(key, 0, keySz);
  24256. XMEMSET(cert, 0, certSz);
  24257. XMEMCPY(key, client_key_der_2048, keySz);
  24258. XMEMCPY(cert, client_cert_der_2048, certSz);
  24259. #elif defined(USE_CERT_BUFFERS_1024)
  24260. byte key[sizeof_client_key_der_1024];
  24261. byte cert[sizeof(sizeof_client_cert_der_1024)];
  24262. word32 keySz = (word32)sizeof(key);
  24263. word32 certSz = (word32)sizeof(cert);
  24264. XMEMSET(key, 0, keySz);
  24265. XMEMSET(cert, 0, certSz);
  24266. XMEMCPY(key, client_key_der_1024, keySz);
  24267. XMEMCPY(cert, client_cert_der_1024, certSz);
  24268. #else
  24269. unsigned char cert[ONEK_BUF];
  24270. unsigned char key[ONEK_BUF];
  24271. XFILE fp = XBADFILE;
  24272. int certSz;
  24273. int keySz;
  24274. ExpectTure((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  24275. XBADFILE);
  24276. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  24277. fp), 0);
  24278. if (fp != XBADFILE) {
  24279. XFCLOSE(fp);
  24280. fp = XBADFILE;
  24281. }
  24282. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  24283. XBADFILE);
  24284. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  24285. 0);
  24286. if (fp != XBADFILE)
  24287. XFCLOSE(fp);
  24288. #endif
  24289. #elif defined(HAVE_ECC)
  24290. #if defined(USE_CERT_BUFFERS_256)
  24291. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24292. unsigned char key[sizeof(ecc_clikey_der_256)];
  24293. int certSz = (int)sizeof(cert);
  24294. int keySz = (int)sizeof(key);
  24295. XMEMSET(cert, 0, certSz);
  24296. XMEMSET(key, 0, keySz);
  24297. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  24298. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  24299. #else
  24300. unsigned char cert[ONEK_BUF];
  24301. unsigned char key[ONEK_BUF];
  24302. XFILE fp = XBADFILE;
  24303. int certSz;
  24304. int keySz;
  24305. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  24306. XBADFILE);
  24307. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  24308. fp), 0);
  24309. if (fp != XBADFILE) {
  24310. XFCLOSE(fp);
  24311. fp = XBADFILE;
  24312. }
  24313. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  24314. XBADFILE);
  24315. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  24316. 0);
  24317. if (fp != XBADFILE)
  24318. XFCLOSE(fp);
  24319. #endif
  24320. #endif
  24321. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24322. /* initialize large data with sequence */
  24323. for (i=0; i<(int)sizeof(data); i++)
  24324. data[i] = i & 0xff;
  24325. XMEMSET(outputHead, 0, outputHeadSz);
  24326. XMEMSET(outputFoot, 0, outputFootSz);
  24327. ExpectIntEQ(wc_InitRng(&rng), 0);
  24328. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24329. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24330. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24331. if (pkcs7 != NULL) {
  24332. pkcs7->content = NULL; /* not used for ex */
  24333. pkcs7->contentSz = (word32)sizeof(data);
  24334. pkcs7->privateKey = key;
  24335. pkcs7->privateKeySz = (word32)sizeof(key);
  24336. pkcs7->encryptOID = RSAk;
  24337. #ifdef NO_SHA
  24338. pkcs7->hashOID = SHA256h;
  24339. #else
  24340. pkcs7->hashOID = SHAh;
  24341. #endif
  24342. pkcs7->rng = &rng;
  24343. }
  24344. /* calculate hash for content */
  24345. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  24346. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  24347. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  24348. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  24349. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  24350. /* Perform PKCS7 sign using hash directly */
  24351. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24352. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  24353. ExpectIntGT(outputHeadSz, 0);
  24354. ExpectIntGT(outputFootSz, 0);
  24355. wc_PKCS7_Free(pkcs7);
  24356. pkcs7 = NULL;
  24357. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24358. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24359. /* required parameter even on verify when using _ex, if using outputHead
  24360. * and outputFoot */
  24361. if (pkcs7 != NULL) {
  24362. pkcs7->contentSz = (word32)sizeof(data);
  24363. }
  24364. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24365. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  24366. wc_PKCS7_Free(pkcs7);
  24367. pkcs7 = NULL;
  24368. /* assembly complete PKCS7 sign and use normal verify */
  24369. {
  24370. byte* output = NULL;
  24371. word32 outputSz = 0;
  24372. #ifndef NO_PKCS7_STREAM
  24373. word32 z;
  24374. int ret;
  24375. #endif /* !NO_PKCS7_STREAM */
  24376. ExpectNotNull(output = (byte*)XMALLOC(
  24377. outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT,
  24378. DYNAMIC_TYPE_TMP_BUFFER));
  24379. if (output != NULL) {
  24380. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  24381. outputSz += outputHeadSz;
  24382. XMEMCPY(&output[outputSz], data, sizeof(data));
  24383. outputSz += sizeof(data);
  24384. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  24385. outputSz += outputFootSz;
  24386. }
  24387. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24388. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24389. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24390. #ifndef NO_PKCS7_STREAM
  24391. wc_PKCS7_Free(pkcs7);
  24392. pkcs7 = NULL;
  24393. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24394. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24395. /* test for streaming mode */
  24396. ret = -1;
  24397. for (z = 0; z < outputSz && ret != 0; z++) {
  24398. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24399. if (ret < 0){
  24400. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24401. }
  24402. }
  24403. ExpectIntEQ(ret, 0);
  24404. ExpectIntNE(pkcs7->contentSz, 0);
  24405. ExpectNotNull(pkcs7->contentDynamic);
  24406. wc_PKCS7_Free(pkcs7);
  24407. pkcs7 = NULL;
  24408. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24409. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24410. #endif /* !NO_PKCS7_STREAM */
  24411. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24412. }
  24413. /* Pass in bad args. */
  24414. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24415. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24416. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24417. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24418. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24419. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24420. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24421. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24422. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24423. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24424. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24425. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  24426. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24427. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  24428. if (pkcs7 != NULL) {
  24429. pkcs7->hashOID = 0; /* bad hashOID */
  24430. }
  24431. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24432. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24433. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24434. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24435. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24436. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24437. #ifndef NO_PKCS7_STREAM
  24438. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24439. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24440. #else
  24441. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24442. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  24443. #endif
  24444. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24445. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24446. #ifndef NO_PKCS7_STREAM
  24447. /* can pass in 0 buffer length with streaming API */
  24448. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24449. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24450. #else
  24451. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24452. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24453. #endif
  24454. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24455. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  24456. #ifndef NO_PKCS7_STREAM
  24457. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24458. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  24459. #else
  24460. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24461. outputHead, outputHeadSz, outputFoot, 0), BUFFER_E);
  24462. #endif
  24463. wc_PKCS7_Free(pkcs7);
  24464. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  24465. #endif
  24466. return EXPECT_RESULT();
  24467. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  24468. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  24469. /**
  24470. * Loads certs/keys from files or buffers into the argument buffers,
  24471. * helper function called by CreatePKCS7SignedData().
  24472. *
  24473. * Returns 0 on success, negative on error.
  24474. */
  24475. static int LoadPKCS7SignedDataCerts(
  24476. int useIntermediateCertChain, int pkAlgoType,
  24477. byte* intCARoot, word32* intCARootSz,
  24478. byte* intCA1, word32* intCA1Sz,
  24479. byte* intCA2, word32* intCA2Sz,
  24480. byte* cert, word32* certSz,
  24481. byte* key, word32* keySz)
  24482. {
  24483. EXPECT_DECLS;
  24484. int ret = 0;
  24485. XFILE fp = XBADFILE;
  24486. #ifndef NO_RSA
  24487. const char* intCARootRSA = "./certs/ca-cert.der";
  24488. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  24489. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  24490. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  24491. const char* intServKeyRSA = "./certs/server-key.der";
  24492. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  24493. const char* cli1024Cert = "./certs/1024/client-cert.der";
  24494. const char* cli1024Key = "./certs/1024/client-key.der";
  24495. #endif
  24496. #endif
  24497. #ifdef HAVE_ECC
  24498. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  24499. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  24500. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  24501. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  24502. const char* intServKeyECC = "./certs/ecc-key.der";
  24503. #ifndef USE_CERT_BUFFERS_256
  24504. const char* cliEccCert = "./certs/client-ecc-cert.der";
  24505. const char* cliEccKey = "./certs/client-ecc-key.der";
  24506. #endif
  24507. #endif
  24508. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  24509. ((useIntermediateCertChain == 1) &&
  24510. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  24511. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  24512. return BAD_FUNC_ARG;
  24513. }
  24514. /* Read/load certs and keys to use for signing based on PK type and chain */
  24515. switch (pkAlgoType) {
  24516. #ifndef NO_RSA
  24517. case RSA_TYPE:
  24518. if (useIntermediateCertChain == 1) {
  24519. ExpectTrue((fp = XFOPEN(intCARootRSA, "rb")) != XBADFILE);
  24520. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  24521. if (fp != XBADFILE) {
  24522. XFCLOSE(fp);
  24523. fp = XBADFILE;
  24524. }
  24525. ExpectIntGT(*intCARootSz, 0);
  24526. ExpectTrue((fp = XFOPEN(intCA1RSA, "rb")) != XBADFILE);
  24527. if (fp != XBADFILE) {
  24528. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24529. XFCLOSE(fp);
  24530. fp = XBADFILE;
  24531. }
  24532. ExpectIntGT(*intCA1Sz, 0);
  24533. ExpectTrue((fp = XFOPEN(intCA2RSA, "rb")) != XBADFILE);
  24534. if (fp != XBADFILE) {
  24535. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24536. XFCLOSE(fp);
  24537. fp = XBADFILE;
  24538. }
  24539. ExpectIntGT(*intCA2Sz, 0);
  24540. ExpectTrue((fp = XFOPEN(intServCertRSA, "rb")) != XBADFILE);
  24541. if (fp != XBADFILE) {
  24542. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24543. XFCLOSE(fp);
  24544. fp = XBADFILE;
  24545. }
  24546. ExpectIntGT(*certSz, 0);
  24547. ExpectTrue((fp = XFOPEN(intServKeyRSA, "rb")) != XBADFILE);
  24548. if (fp != XBADFILE) {
  24549. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24550. XFCLOSE(fp);
  24551. fp = XBADFILE;
  24552. }
  24553. ExpectIntGT(*keySz, 0);
  24554. }
  24555. else {
  24556. #if defined(USE_CERT_BUFFERS_2048)
  24557. *keySz = sizeof_client_key_der_2048;
  24558. *certSz = sizeof_client_cert_der_2048;
  24559. XMEMCPY(key, client_key_der_2048, *keySz);
  24560. XMEMCPY(cert, client_cert_der_2048, *certSz);
  24561. #elif defined(USE_CERT_BUFFERS_1024)
  24562. *keySz = sizeof_client_key_der_1024;
  24563. *certSz = sizeof_client_cert_der_1024;
  24564. XMEMCPY(key, client_key_der_1024, *keySz);
  24565. XMEMCPY(cert, client_cert_der_1024, *certSz);
  24566. #else
  24567. ExpectTrue((fp = XFOPEN(cli1024Key, "rb")) != XBADFILE);
  24568. if (fp != XBADFILE) {
  24569. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24570. XFCLOSE(fp);
  24571. fp = XBADFILE;
  24572. }
  24573. ExpectIntGT(*keySz, 0);
  24574. ExpectTrue((fp = XFOPEN(cli1024Cert, "rb")) != XBADFILE);
  24575. if (fp != XBADFILE) {
  24576. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24577. XFCLOSE(fp);
  24578. fp = XBADFILE;
  24579. }
  24580. ExpectIntGT(*certSz, 0);
  24581. #endif /* USE_CERT_BUFFERS_2048 */
  24582. }
  24583. break;
  24584. #endif /* !NO_RSA */
  24585. #ifdef HAVE_ECC
  24586. case ECC_TYPE:
  24587. if (useIntermediateCertChain == 1) {
  24588. ExpectTrue((fp = XFOPEN(intCARootECC, "rb")) != XBADFILE);
  24589. if (fp != XBADFILE) {
  24590. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz,
  24591. fp);
  24592. XFCLOSE(fp);
  24593. fp = XBADFILE;
  24594. }
  24595. ExpectIntGT(*intCARootSz, 0);
  24596. ExpectTrue((fp = XFOPEN(intCA1ECC, "rb")) != XBADFILE);
  24597. if (fp != XBADFILE) {
  24598. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24599. XFCLOSE(fp);
  24600. fp = XBADFILE;
  24601. }
  24602. ExpectIntGT(*intCA1Sz, 0);
  24603. ExpectTrue((fp = XFOPEN(intCA2ECC, "rb")) != XBADFILE);
  24604. if (fp != XBADFILE) {
  24605. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24606. XFCLOSE(fp);
  24607. fp = XBADFILE;
  24608. }
  24609. ExpectIntGT(*intCA2Sz, 0);
  24610. ExpectTrue((fp = XFOPEN(intServCertECC, "rb")) != XBADFILE);
  24611. if (fp != XBADFILE) {
  24612. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24613. XFCLOSE(fp);
  24614. fp = XBADFILE;
  24615. }
  24616. ExpectIntGT(*certSz, 0);
  24617. ExpectTrue((fp = XFOPEN(intServKeyECC, "rb")) != XBADFILE);
  24618. if (fp != XBADFILE) {
  24619. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24620. XFCLOSE(fp);
  24621. fp = XBADFILE;
  24622. }
  24623. ExpectIntGT(*keySz, 0);
  24624. }
  24625. else {
  24626. #if defined(USE_CERT_BUFFERS_256)
  24627. *keySz = sizeof_ecc_clikey_der_256;
  24628. *certSz = sizeof_cliecc_cert_der_256;
  24629. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  24630. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  24631. #else
  24632. ExpectTrue((fp = XFOPEN(cliEccKey, "rb")) != XBADFILE);
  24633. if (fp != XBADFILE) {
  24634. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24635. XFCLOSE(fp);
  24636. fp = XBADFILE;
  24637. }
  24638. ExpectIntGT(*keySz, 0);
  24639. ExpectTrue((fp = XFOPEN(cliEccCert, "rb")) != XBADFILE);
  24640. if (fp != XBADFILE) {
  24641. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24642. XFCLOSE(fp);
  24643. fp = XBADFILE;
  24644. }
  24645. ExpectIntGT(*certSz, 0);
  24646. #endif /* USE_CERT_BUFFERS_256 */
  24647. }
  24648. break;
  24649. #endif /* HAVE_ECC */
  24650. default:
  24651. WOLFSSL_MSG("Unsupported SignedData PK type");
  24652. ret = BAD_FUNC_ARG;
  24653. break;
  24654. }
  24655. if (EXPECT_FAIL() && (ret == 0)) {
  24656. ret = BAD_FUNC_ARG;
  24657. }
  24658. return ret;
  24659. }
  24660. /**
  24661. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  24662. *
  24663. * output output buffer to place SignedData
  24664. * outputSz size of output buffer
  24665. * data data buffer to be signed
  24666. * dataSz size of data buffer
  24667. * withAttribs [1/0] include attributes in SignedData message
  24668. * detachedSig [1/0] create detached signature, no content
  24669. * useIntCertChain [1/0] use certificate chain and include intermediate and
  24670. * root CAs in bundle
  24671. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  24672. *
  24673. * Return size of bundle created on success, negative on error */
  24674. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  24675. byte* data, word32 dataSz,
  24676. int withAttribs, int detachedSig,
  24677. int useIntermediateCertChain,
  24678. int pkAlgoType)
  24679. {
  24680. EXPECT_DECLS;
  24681. int ret = 0;
  24682. WC_RNG rng;
  24683. PKCS7* pkcs7 = NULL;
  24684. static byte messageTypeOid[] =
  24685. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  24686. 0x09, 0x02 };
  24687. static byte messageType[] = { 0x13, 2, '1', '9' };
  24688. PKCS7Attrib attribs[] =
  24689. {
  24690. { messageTypeOid, sizeof(messageTypeOid), messageType,
  24691. sizeof(messageType) }
  24692. };
  24693. byte intCARoot[TWOK_BUF];
  24694. byte intCA1[TWOK_BUF];
  24695. byte intCA2[TWOK_BUF];
  24696. byte cert[TWOK_BUF];
  24697. byte key[TWOK_BUF];
  24698. word32 intCARootSz = sizeof(intCARoot);
  24699. word32 intCA1Sz = sizeof(intCA1);
  24700. word32 intCA2Sz = sizeof(intCA2);
  24701. word32 certSz = sizeof(cert);
  24702. word32 keySz = sizeof(key);
  24703. XMEMSET(intCARoot, 0, intCARootSz);
  24704. XMEMSET(intCA1, 0, intCA1Sz);
  24705. XMEMSET(intCA2, 0, intCA2Sz);
  24706. XMEMSET(cert, 0, certSz);
  24707. XMEMSET(key, 0, keySz);
  24708. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  24709. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  24710. cert, &certSz, key, &keySz);
  24711. ExpectIntEQ(ret, 0);
  24712. XMEMSET(output, 0, outputSz);
  24713. ExpectIntEQ(wc_InitRng(&rng), 0);
  24714. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24715. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24716. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24717. if (useIntermediateCertChain == 1) {
  24718. /* Add intermediate and root CA certs into SignedData Certs SET */
  24719. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  24720. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  24721. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  24722. }
  24723. if (pkcs7 != NULL) {
  24724. pkcs7->content = data;
  24725. pkcs7->contentSz = dataSz;
  24726. pkcs7->privateKey = key;
  24727. pkcs7->privateKeySz = (word32)sizeof(key);
  24728. if (pkAlgoType == RSA_TYPE) {
  24729. pkcs7->encryptOID = RSAk;
  24730. }
  24731. else {
  24732. pkcs7->encryptOID = ECDSAk;
  24733. }
  24734. #ifdef NO_SHA
  24735. pkcs7->hashOID = SHA256h;
  24736. #else
  24737. pkcs7->hashOID = SHAh;
  24738. #endif
  24739. pkcs7->rng = &rng;
  24740. if (withAttribs) {
  24741. /* include a signed attribute */
  24742. pkcs7->signedAttribs = attribs;
  24743. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  24744. }
  24745. }
  24746. if (detachedSig) {
  24747. ExpectIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  24748. }
  24749. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz);
  24750. ExpectIntGT(outputSz, 0);
  24751. wc_PKCS7_Free(pkcs7);
  24752. pkcs7 = NULL;
  24753. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24754. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24755. if (detachedSig && (pkcs7 != NULL)) {
  24756. pkcs7->content = data;
  24757. pkcs7->contentSz = dataSz;
  24758. }
  24759. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24760. wc_PKCS7_Free(pkcs7);
  24761. wc_FreeRng(&rng);
  24762. if (EXPECT_FAIL()) {
  24763. outputSz = 0;
  24764. }
  24765. return outputSz;
  24766. }
  24767. #endif
  24768. /*
  24769. * Testing wc_PKCS_VerifySignedData()
  24770. */
  24771. static int test_wc_PKCS7_VerifySignedData_RSA(void)
  24772. {
  24773. EXPECT_DECLS;
  24774. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  24775. PKCS7* pkcs7 = NULL;
  24776. byte output[6000]; /* Large size needed for bundles with int CA certs */
  24777. word32 outputSz = sizeof(output);
  24778. byte data[] = "Test data to encode.";
  24779. byte badOut[1];
  24780. word32 badOutSz = 0;
  24781. byte badContent[] = "This is different content than was signed";
  24782. wc_HashAlg hash;
  24783. #ifdef NO_SHA
  24784. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  24785. #else
  24786. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24787. #endif
  24788. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24789. word32 hashSz = wc_HashGetDigestSize(hashType);
  24790. #ifndef NO_RSA
  24791. PKCS7DecodedAttrib* decodedAttrib = NULL;
  24792. /* contentType OID (1.2.840.113549.1.9.3) */
  24793. static const byte contentTypeOid[] =
  24794. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01, 0x09, 0x03 };
  24795. /* PKCS#7 DATA content type (contentType defaults to DATA) */
  24796. static const byte dataType[] =
  24797. { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01 };
  24798. /* messageDigest OID (1.2.840.113549.1.9.4) */
  24799. static const byte messageDigestOid[] =
  24800. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04 };
  24801. #ifndef NO_ASN_TIME
  24802. /* signingTime OID () */
  24803. static const byte signingTimeOid[] =
  24804. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05};
  24805. #endif
  24806. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  24807. int dateLength = 0;
  24808. byte dateFormat;
  24809. const byte* datePart = NULL;
  24810. struct tm timearg;
  24811. time_t now;
  24812. struct tm* nowTm = NULL;
  24813. #ifdef NEED_TMP_TIME
  24814. struct tm tmpTimeStorage;
  24815. struct tm* tmpTime = &tmpTimeStorage;
  24816. #endif
  24817. #endif /* !NO_ASN && !NO_ASN_TIME */
  24818. #ifndef NO_PKCS7_STREAM
  24819. word32 z;
  24820. int ret;
  24821. #endif /* !NO_PKCS7_STREAM */
  24822. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  24823. /* Success test with RSA certs/key */
  24824. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24825. (word32)sizeof(data), 0, 0, 0, RSA_TYPE)), 0);
  24826. /* calculate hash for content, used later */
  24827. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  24828. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  24829. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  24830. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  24831. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24832. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24833. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24834. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24835. #ifndef NO_PKCS7_STREAM
  24836. wc_PKCS7_Free(pkcs7);
  24837. pkcs7 = NULL;
  24838. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24839. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24840. /* test for streaming */
  24841. ret = -1;
  24842. for (z = 0; z < outputSz && ret != 0; z++) {
  24843. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24844. if (ret < 0){
  24845. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24846. }
  24847. }
  24848. ExpectIntEQ(ret, 0);
  24849. ExpectIntNE(pkcs7->contentSz, 0);
  24850. ExpectNotNull(pkcs7->contentDynamic);
  24851. #endif /* !NO_PKCS7_STREAM */
  24852. /* Check that decoded signed attributes are correct */
  24853. /* messageDigest should be first */
  24854. if (pkcs7 != NULL) {
  24855. decodedAttrib = pkcs7->decodedAttrib;
  24856. }
  24857. ExpectNotNull(decodedAttrib);
  24858. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(messageDigestOid));
  24859. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, messageDigestOid,
  24860. decodedAttrib->oidSz), 0);
  24861. /* + 2 for OCTET STRING and length bytes */
  24862. ExpectIntEQ(decodedAttrib->valueSz, hashSz + 2);
  24863. ExpectNotNull(decodedAttrib->value);
  24864. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, hashBuf, hashSz), 0);
  24865. #ifndef NO_ASN_TIME
  24866. /* signingTime should be second */
  24867. if (decodedAttrib != NULL) {
  24868. decodedAttrib = decodedAttrib->next;
  24869. }
  24870. ExpectNotNull(decodedAttrib);
  24871. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(signingTimeOid));
  24872. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, signingTimeOid,
  24873. decodedAttrib->oidSz), 0);
  24874. ExpectIntGT(decodedAttrib->valueSz, 0);
  24875. ExpectNotNull(decodedAttrib->value);
  24876. #endif
  24877. /* Verify signingTime if ASN and time are available */
  24878. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  24879. ExpectIntEQ(wc_GetDateInfo(decodedAttrib->value, decodedAttrib->valueSz,
  24880. &datePart, &dateFormat, &dateLength), 0);
  24881. ExpectNotNull(datePart);
  24882. ExpectIntGT(dateLength, 0);
  24883. XMEMSET(&timearg, 0, sizeof(timearg));
  24884. ExpectIntEQ(wc_GetDateAsCalendarTime(datePart, dateLength, dateFormat,
  24885. &timearg), 0);
  24886. /* Get current time and compare year/month/day against attribute value */
  24887. ExpectIntEQ(wc_GetTime(&now, sizeof(now)), 0);
  24888. nowTm = (struct tm*)XGMTIME((time_t*)&now, tmpTime);
  24889. ExpectNotNull(nowTm);
  24890. ExpectIntEQ(timearg.tm_year, nowTm->tm_year);
  24891. ExpectIntEQ(timearg.tm_mon, nowTm->tm_mon);
  24892. ExpectIntEQ(timearg.tm_mday, nowTm->tm_mday);
  24893. #endif /* !NO_ASN && !NO_ASN_TIME */
  24894. /* contentType should be third */
  24895. if (decodedAttrib != NULL) {
  24896. decodedAttrib = decodedAttrib->next;
  24897. }
  24898. ExpectNotNull(decodedAttrib);
  24899. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(contentTypeOid));
  24900. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, contentTypeOid,
  24901. decodedAttrib->oidSz), 0);
  24902. ExpectIntEQ(decodedAttrib->valueSz, (int)sizeof(dataType) + 2);
  24903. ExpectNotNull(decodedAttrib->value);
  24904. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, dataType, sizeof(dataType)),
  24905. 0);
  24906. #endif /* !NO_RSA */
  24907. /* Test bad args. */
  24908. ExpectIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  24909. BAD_FUNC_ARG);
  24910. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  24911. BAD_FUNC_ARG);
  24912. #ifndef NO_PKCS7_STREAM
  24913. /* can pass in 0 buffer length with streaming API */
  24914. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  24915. badOutSz), WC_PKCS7_WANT_READ_E);
  24916. #else
  24917. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  24918. badOutSz), BAD_FUNC_ARG);
  24919. #endif
  24920. wc_PKCS7_Free(pkcs7);
  24921. pkcs7 = NULL;
  24922. #ifndef NO_RSA
  24923. /* Try RSA certs/key/sig first */
  24924. outputSz = sizeof(output);
  24925. XMEMSET(output, 0, outputSz);
  24926. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24927. (word32)sizeof(data),
  24928. 1, 1, 0, RSA_TYPE)), 0);
  24929. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24930. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24931. if (pkcs7 != NULL) {
  24932. pkcs7->content = badContent;
  24933. pkcs7->contentSz = sizeof(badContent);
  24934. }
  24935. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  24936. SIG_VERIFY_E);
  24937. wc_PKCS7_Free(pkcs7);
  24938. pkcs7 = NULL;
  24939. #ifndef NO_PKCS7_STREAM
  24940. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24941. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24942. if (pkcs7 != NULL) {
  24943. pkcs7->content = badContent;
  24944. pkcs7->contentSz = sizeof(badContent);
  24945. }
  24946. /* test for streaming */
  24947. ret = -1;
  24948. for (z = 0; z < outputSz && ret != 0; z++) {
  24949. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24950. if (ret == WC_PKCS7_WANT_READ_E){
  24951. continue;
  24952. }
  24953. else if (ret < 0) {
  24954. break;
  24955. }
  24956. }
  24957. ExpectIntEQ(ret, SIG_VERIFY_E);
  24958. ExpectIntNE(pkcs7->contentSz, 0);
  24959. ExpectNotNull(pkcs7->contentDynamic);
  24960. wc_PKCS7_Free(pkcs7);
  24961. pkcs7 = NULL;
  24962. #endif /* !NO_PKCS7_STREAM */
  24963. /* Test success case with detached signature and valid content */
  24964. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24965. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24966. if (pkcs7 != NULL) {
  24967. pkcs7->content = data;
  24968. pkcs7->contentSz = sizeof(data);
  24969. }
  24970. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24971. wc_PKCS7_Free(pkcs7);
  24972. pkcs7 = NULL;
  24973. #ifndef NO_PKCS7_STREAM
  24974. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24975. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24976. if (pkcs7 != NULL) {
  24977. pkcs7->content = data;
  24978. pkcs7->contentSz = sizeof(data);
  24979. }
  24980. /* test for streaming */
  24981. ret = -1;
  24982. for (z = 0; z < outputSz && ret != 0; z++) {
  24983. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24984. if (ret < 0){
  24985. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24986. }
  24987. }
  24988. ExpectIntEQ(ret, 0);
  24989. ExpectIntNE(pkcs7->contentSz, 0);
  24990. ExpectNotNull(pkcs7->contentDynamic);
  24991. wc_PKCS7_Free(pkcs7);
  24992. pkcs7 = NULL;
  24993. #endif /* !NO_PKCS7_STREAM */
  24994. /* verify using pre-computed content digest only (no content) */
  24995. {
  24996. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24997. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  24998. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24999. output, outputSz, NULL, 0), 0);
  25000. wc_PKCS7_Free(pkcs7);
  25001. pkcs7 = NULL;
  25002. }
  25003. #endif /* !NO_RSA */
  25004. /* Test verify on signedData containing intermediate/root CA certs */
  25005. #ifndef NO_RSA
  25006. outputSz = sizeof(output);
  25007. XMEMSET(output, 0, outputSz);
  25008. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25009. (word32)sizeof(data),
  25010. 0, 0, 1, RSA_TYPE)), 0);
  25011. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25012. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25013. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25014. wc_PKCS7_Free(pkcs7);
  25015. pkcs7 = NULL;
  25016. #ifndef NO_PKCS7_STREAM
  25017. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25018. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25019. /* test for streaming */
  25020. ret = -1;
  25021. for (z = 0; z < outputSz && ret != 0; z++) {
  25022. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25023. if (ret < 0){
  25024. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25025. }
  25026. }
  25027. ExpectIntEQ(ret, 0);
  25028. ExpectIntNE(pkcs7->contentSz, 0);
  25029. ExpectNotNull(pkcs7->contentDynamic);
  25030. wc_PKCS7_Free(pkcs7);
  25031. pkcs7 = NULL;
  25032. #endif /* !NO_PKCS7_STREAM */
  25033. #endif /* !NO_RSA */
  25034. #if defined(ASN_BER_TO_DER) && !defined(NO_PKCS7_STREAM) && \
  25035. !defined(NO_FILESYSTEM)
  25036. {
  25037. XFILE signedBundle = XBADFILE;
  25038. int signedBundleSz = 0;
  25039. int chunkSz = 1;
  25040. int i, rc = 0;
  25041. byte* buf = NULL;
  25042. ExpectTrue((signedBundle = XFOPEN("./certs/test-stream-sign.p7b",
  25043. "rb")) != XBADFILE);
  25044. ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_END) == 0);
  25045. ExpectIntGT(signedBundleSz = (int)XFTELL(signedBundle), 0);
  25046. ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_SET) == 0);
  25047. ExpectNotNull(buf = (byte*)XMALLOC(signedBundleSz, HEAP_HINT,
  25048. DYNAMIC_TYPE_FILE));
  25049. if (buf != NULL) {
  25050. ExpectIntEQ(XFREAD(buf, 1, signedBundleSz, signedBundle),
  25051. signedBundleSz);
  25052. }
  25053. if (signedBundle != XBADFILE) {
  25054. XFCLOSE(signedBundle);
  25055. signedBundle = XBADFILE;
  25056. }
  25057. if (buf != NULL) {
  25058. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25059. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25060. for (i = 0; i < signedBundleSz;) {
  25061. int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
  25062. chunkSz;
  25063. rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, sz);
  25064. if (rc < 0 ) {
  25065. if (rc == WC_PKCS7_WANT_READ_E) {
  25066. i += sz;
  25067. continue;
  25068. }
  25069. break;
  25070. }
  25071. else {
  25072. break;
  25073. }
  25074. }
  25075. ExpectIntEQ(rc, PKCS7_SIGNEEDS_CHECK);
  25076. wc_PKCS7_Free(pkcs7);
  25077. pkcs7 = NULL;
  25078. }
  25079. /* now try with malformed bundle */
  25080. if (buf != NULL) {
  25081. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25082. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25083. buf[signedBundleSz - 2] = buf[signedBundleSz - 2] + 1;
  25084. for (i = 0; i < signedBundleSz;) {
  25085. int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
  25086. chunkSz;
  25087. rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, sz);
  25088. if (rc < 0 ) {
  25089. if (rc == WC_PKCS7_WANT_READ_E) {
  25090. i += sz;
  25091. continue;
  25092. }
  25093. break;
  25094. }
  25095. else {
  25096. break;
  25097. }
  25098. }
  25099. ExpectIntEQ(rc, ASN_PARSE_E);
  25100. wc_PKCS7_Free(pkcs7);
  25101. pkcs7 = NULL;
  25102. }
  25103. if (buf != NULL)
  25104. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  25105. }
  25106. #endif /* BER and stream */
  25107. #endif
  25108. return EXPECT_RESULT();
  25109. } /* END test_wc_PKCS7_VerifySignedData()_RSA */
  25110. /*
  25111. * Testing wc_PKCS_VerifySignedData()
  25112. */
  25113. static int test_wc_PKCS7_VerifySignedData_ECC(void)
  25114. {
  25115. EXPECT_DECLS;
  25116. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  25117. PKCS7* pkcs7 = NULL;
  25118. byte output[6000]; /* Large size needed for bundles with int CA certs */
  25119. word32 outputSz = sizeof(output);
  25120. byte data[] = "Test data to encode.";
  25121. byte badContent[] = "This is different content than was signed";
  25122. wc_HashAlg hash;
  25123. #ifndef NO_PKCS7_STREAM
  25124. word32 z;
  25125. int ret;
  25126. #endif /* !NO_PKCS7_STREAM */
  25127. #ifdef NO_SHA
  25128. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  25129. #else
  25130. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  25131. #endif
  25132. byte hashBuf[WC_MAX_DIGEST_SIZE];
  25133. word32 hashSz = wc_HashGetDigestSize(hashType);
  25134. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  25135. /* Success test with ECC certs/key */
  25136. outputSz = sizeof(output);
  25137. XMEMSET(output, 0, outputSz);
  25138. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25139. (word32)sizeof(data), 0, 0, 0, ECC_TYPE)), 0);
  25140. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25141. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25142. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25143. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25144. wc_PKCS7_Free(pkcs7);
  25145. pkcs7 = NULL;
  25146. #ifndef NO_PKCS7_STREAM
  25147. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25148. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25149. /* test for streaming */
  25150. ret = -1;
  25151. for (z = 0; z < outputSz && ret != 0; z++) {
  25152. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25153. if (ret < 0){
  25154. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25155. }
  25156. }
  25157. ExpectIntEQ(ret, 0);
  25158. ExpectIntNE(pkcs7->contentSz, 0);
  25159. ExpectNotNull(pkcs7->contentDynamic);
  25160. wc_PKCS7_Free(pkcs7);
  25161. pkcs7 = NULL;
  25162. #endif /* !NO_PKCS7_STREAM */
  25163. /* Invalid content should error, use detached signature so we can
  25164. * easily change content */
  25165. outputSz = sizeof(output);
  25166. XMEMSET(output, 0, outputSz);
  25167. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25168. (word32)sizeof(data), 1, 1, 0, ECC_TYPE)), 0);
  25169. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25170. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25171. if (pkcs7 != NULL) {
  25172. pkcs7->content = badContent;
  25173. pkcs7->contentSz = sizeof(badContent);
  25174. }
  25175. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  25176. SIG_VERIFY_E);
  25177. wc_PKCS7_Free(pkcs7);
  25178. pkcs7 = NULL;
  25179. #ifndef NO_PKCS7_STREAM
  25180. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25181. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25182. if (pkcs7 != NULL) {
  25183. pkcs7->content = badContent;
  25184. pkcs7->contentSz = sizeof(badContent);
  25185. }
  25186. /* test for streaming */
  25187. ret = -1;
  25188. for (z = 0; z < outputSz && ret != 0; z++) {
  25189. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25190. if (ret == WC_PKCS7_WANT_READ_E){
  25191. continue;
  25192. }
  25193. else if (ret < 0) {
  25194. break;
  25195. }
  25196. }
  25197. ExpectIntEQ(ret, SIG_VERIFY_E);
  25198. ExpectIntNE(pkcs7->contentSz, 0);
  25199. ExpectNotNull(pkcs7->contentDynamic);
  25200. wc_PKCS7_Free(pkcs7);
  25201. pkcs7 = NULL;
  25202. #endif /* !NO_PKCS7_STREAM */
  25203. /* Test success case with detached signature and valid content */
  25204. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25205. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25206. if (pkcs7 != NULL) {
  25207. pkcs7->content = data;
  25208. pkcs7->contentSz = sizeof(data);
  25209. }
  25210. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25211. wc_PKCS7_Free(pkcs7);
  25212. pkcs7 = NULL;
  25213. #ifndef NO_PKCS7_STREAM
  25214. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25215. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25216. if (pkcs7 != NULL) {
  25217. pkcs7->content = data;
  25218. pkcs7->contentSz = sizeof(data);
  25219. }
  25220. /* test for streaming */
  25221. ret = -1;
  25222. for (z = 0; z < outputSz && ret != 0; z++) {
  25223. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25224. if (ret < 0){
  25225. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25226. }
  25227. }
  25228. ExpectIntEQ(ret, 0);
  25229. ExpectIntNE(pkcs7->contentSz, 0);
  25230. ExpectNotNull(pkcs7->contentDynamic);
  25231. wc_PKCS7_Free(pkcs7);
  25232. pkcs7 = NULL;
  25233. #endif /* !NO_PKCS7_STREAM */
  25234. /* verify using pre-computed content digest only (no content) */
  25235. {
  25236. /* calculate hash for content */
  25237. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  25238. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  25239. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  25240. ExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  25241. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25242. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  25243. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25244. output, outputSz, NULL, 0), 0);
  25245. wc_PKCS7_Free(pkcs7);
  25246. pkcs7 = NULL;
  25247. }
  25248. /* Test verify on signedData containing intermediate/root CA certs */
  25249. outputSz = sizeof(output);
  25250. XMEMSET(output, 0, outputSz);
  25251. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25252. (word32)sizeof(data), 0, 0, 1, ECC_TYPE)), 0);
  25253. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25254. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25255. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25256. wc_PKCS7_Free(pkcs7);
  25257. pkcs7 = NULL;
  25258. #ifndef NO_PKCS7_STREAM
  25259. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25260. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25261. /* test for streaming */
  25262. ret = -1;
  25263. for (z = 0; z < outputSz && ret != 0; z++) {
  25264. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25265. if (ret < 0){
  25266. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25267. }
  25268. }
  25269. ExpectIntEQ(ret, 0);
  25270. ExpectIntNE(pkcs7->contentSz, 0);
  25271. ExpectNotNull(pkcs7->contentDynamic);
  25272. wc_PKCS7_Free(pkcs7);
  25273. pkcs7 = NULL;
  25274. #endif /* !NO_PKCS7_STREAM */
  25275. #endif
  25276. return EXPECT_RESULT();
  25277. } /* END test_wc_PKCS7_VerifySignedData_ECC() */
  25278. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  25279. !defined(NO_AES_256)
  25280. static const byte defKey[] = {
  25281. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25282. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25283. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25284. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25285. };
  25286. static byte aesHandle[32]; /* simulated hardware key handle */
  25287. /* return 0 on success */
  25288. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  25289. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  25290. byte* in, int inSz, byte* out, void* usrCtx)
  25291. {
  25292. int ret;
  25293. Aes aes;
  25294. if (usrCtx == NULL) {
  25295. /* no simulated handle passed in */
  25296. return -1;
  25297. }
  25298. switch (encryptOID) {
  25299. case AES256CBCb:
  25300. if (ivSz != AES_BLOCK_SIZE)
  25301. return BAD_FUNC_ARG;
  25302. break;
  25303. default:
  25304. WOLFSSL_MSG("Unsupported content cipher type for test");
  25305. return ALGO_ID_E;
  25306. };
  25307. /* simulate using handle to get key */
  25308. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  25309. if (ret == 0) {
  25310. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  25311. if (ret == 0)
  25312. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  25313. wc_AesFree(&aes);
  25314. }
  25315. (void)aad;
  25316. (void)aadSz;
  25317. (void)authTag;
  25318. (void)authTagSz;
  25319. (void)pkcs7;
  25320. return ret;
  25321. }
  25322. /* returns key size on success */
  25323. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  25324. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  25325. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  25326. {
  25327. int ret = -1;
  25328. (void)cekSz;
  25329. (void)cek;
  25330. (void)outSz;
  25331. (void)keyIdSz;
  25332. (void)direction;
  25333. (void)orginKey; /* used with KAKRI */
  25334. (void)orginKeySz;
  25335. if (out == NULL)
  25336. return BAD_FUNC_ARG;
  25337. if (keyId[0] != 0x00) {
  25338. return -1;
  25339. }
  25340. if (type != (int)PKCS7_KEKRI) {
  25341. return -1;
  25342. }
  25343. switch (keyWrapAlgo) {
  25344. case AES256_WRAP:
  25345. /* simulate setting a handle for later decryption but use key
  25346. * as handle in the test case here */
  25347. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  25348. aesHandle, sizeof(aesHandle), NULL);
  25349. if (ret < 0)
  25350. return ret;
  25351. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  25352. if (ret < 0)
  25353. return ret;
  25354. /* return key size on success */
  25355. return sizeof(defKey);
  25356. default:
  25357. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  25358. return BAD_KEYWRAP_ALG_E;
  25359. };
  25360. }
  25361. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  25362. /*
  25363. * Testing wc_PKCS7_EncodeEnvelopedData()
  25364. */
  25365. static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
  25366. {
  25367. EXPECT_DECLS;
  25368. #if defined(HAVE_PKCS7)
  25369. PKCS7* pkcs7 = NULL;
  25370. #ifdef ASN_BER_TO_DER
  25371. int encodedSz = 0;
  25372. #endif
  25373. #ifdef ECC_TIMING_RESISTANT
  25374. WC_RNG rng;
  25375. #endif
  25376. word32 tempWrd32 = 0;
  25377. byte* tmpBytePtr = NULL;
  25378. const char input[] = "Test data to encode.";
  25379. int i;
  25380. int testSz = 0;
  25381. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  25382. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25383. byte* rsaCert = NULL;
  25384. byte* rsaPrivKey = NULL;
  25385. word32 rsaCertSz;
  25386. word32 rsaPrivKeySz;
  25387. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  25388. !defined(USE_CERT_BUFFERS_2048) )
  25389. static const char* rsaClientCert = "./certs/client-cert.der";
  25390. static const char* rsaClientKey = "./certs/client-key.der";
  25391. rsaCertSz = (word32)sizeof(rsaClientCert);
  25392. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  25393. #endif
  25394. #endif
  25395. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25396. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25397. byte* eccCert = NULL;
  25398. byte* eccPrivKey = NULL;
  25399. word32 eccCertSz;
  25400. word32 eccPrivKeySz;
  25401. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  25402. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  25403. static const char* eccClientKey = "./certs/ecc-client-key.der";
  25404. #endif
  25405. #endif
  25406. /* Generic buffer size. */
  25407. byte output[ONEK_BUF];
  25408. byte decoded[sizeof(input)/sizeof(char)];
  25409. int decodedSz = 0;
  25410. #ifndef NO_FILESYSTEM
  25411. XFILE certFile = XBADFILE;
  25412. XFILE keyFile = XBADFILE;
  25413. #endif
  25414. #ifdef ECC_TIMING_RESISTANT
  25415. XMEMSET(&rng, 0, sizeof(WC_RNG));
  25416. #endif
  25417. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25418. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25419. /* RSA certs and keys. */
  25420. #if defined(USE_CERT_BUFFERS_1024)
  25421. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  25422. /* Allocate buffer space. */
  25423. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  25424. DYNAMIC_TYPE_TMP_BUFFER));
  25425. /* Init buffer. */
  25426. if (rsaCert != NULL) {
  25427. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  25428. }
  25429. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  25430. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  25431. DYNAMIC_TYPE_TMP_BUFFER));
  25432. if (rsaPrivKey != NULL) {
  25433. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  25434. }
  25435. #elif defined(USE_CERT_BUFFERS_2048)
  25436. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  25437. /* Allocate buffer */
  25438. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  25439. DYNAMIC_TYPE_TMP_BUFFER));
  25440. /* Init buffer. */
  25441. if (rsaCert != NULL) {
  25442. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  25443. }
  25444. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  25445. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  25446. DYNAMIC_TYPE_TMP_BUFFER));
  25447. if (rsaPrivKey != NULL) {
  25448. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  25449. }
  25450. #else
  25451. /* File system. */
  25452. ExpectTrue((certFile = XFOPEN(rsaClientCert, "rb")) != XBADFILE);
  25453. rsaCertSz = (word32)FOURK_BUF;
  25454. ExpectNotNull(rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25455. DYNAMIC_TYPE_TMP_BUFFER));
  25456. ExpectTrue((rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz,
  25457. certFile)) > 0);
  25458. if (certFile != XBADFILE)
  25459. XFCLOSE(certFile);
  25460. ExpectTrue((keyFile = XFOPEN(rsaClientKey, "rb")) != XBADFILE);
  25461. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25462. DYNAMIC_TYPE_TMP_BUFFER));
  25463. rsaPrivKeySz = (word32)FOURK_BUF;
  25464. ExpectTrue((rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz,
  25465. keyFile)) > 0);
  25466. if (keyFile != XBADFILE)
  25467. XFCLOSE(keyFile);
  25468. #endif /* USE_CERT_BUFFERS */
  25469. #endif /* NO_RSA */
  25470. /* ECC */
  25471. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25472. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25473. #ifdef USE_CERT_BUFFERS_256
  25474. ExpectNotNull(eccCert = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  25475. DYNAMIC_TYPE_TMP_BUFFER));
  25476. /* Init buffer. */
  25477. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  25478. if (eccCert != NULL) {
  25479. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  25480. }
  25481. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  25482. DYNAMIC_TYPE_TMP_BUFFER));
  25483. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  25484. if (eccPrivKey != NULL) {
  25485. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  25486. }
  25487. #else /* File system. */
  25488. ExpectTrue((certFile = XFOPEN(eccClientCert, "rb")) != XBADFILE);
  25489. eccCertSz = (word32)FOURK_BUF;
  25490. ExpectNotNull(eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25491. DYNAMIC_TYPE_TMP_BUFFER));
  25492. ExpectTrue((eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz,
  25493. certFile)) > 0);
  25494. if (certFile != XBADFILE) {
  25495. XFCLOSE(certFile);
  25496. }
  25497. ExpectTrue((keyFile = XFOPEN(eccClientKey, "rb")) != XBADFILE);
  25498. eccPrivKeySz = (word32)FOURK_BUF;
  25499. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25500. DYNAMIC_TYPE_TMP_BUFFER));
  25501. ExpectTrue((eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz,
  25502. keyFile)) > 0);
  25503. if (keyFile != XBADFILE) {
  25504. XFCLOSE(keyFile);
  25505. }
  25506. #endif /* USE_CERT_BUFFERS_256 */
  25507. #endif /* END HAVE_ECC */
  25508. #ifndef NO_FILESYSTEM
  25509. /* Silence. */
  25510. (void)keyFile;
  25511. (void)certFile;
  25512. #endif
  25513. {
  25514. const pkcs7EnvelopedVector testVectors[] = {
  25515. /* DATA is a global variable defined in the makefile. */
  25516. #if !defined(NO_RSA)
  25517. #ifndef NO_DES3
  25518. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  25519. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25520. #endif /* NO_DES3 */
  25521. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25522. #ifndef NO_AES_128
  25523. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  25524. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25525. #endif
  25526. #ifndef NO_AES_192
  25527. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  25528. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25529. #endif
  25530. #ifndef NO_AES_256
  25531. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  25532. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25533. #endif
  25534. #endif /* NO_AES && HAVE_AES_CBC */
  25535. #endif /* NO_RSA */
  25536. #if defined(HAVE_ECC)
  25537. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25538. #if !defined(NO_SHA) && !defined(NO_AES_128)
  25539. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25540. AES128CBCb, AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme,
  25541. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25542. #endif
  25543. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  25544. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25545. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme,
  25546. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25547. #endif
  25548. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  25549. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25550. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme,
  25551. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25552. #endif
  25553. #endif /* NO_AES && HAVE_AES_CBC*/
  25554. #endif /* END HAVE_ECC */
  25555. }; /* END pkcs7EnvelopedVector */
  25556. #ifdef ECC_TIMING_RESISTANT
  25557. ExpectIntEQ(wc_InitRng(&rng), 0);
  25558. #endif
  25559. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25560. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25561. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  25562. for (i = 0; i < testSz; i++) {
  25563. #ifdef ASN_BER_TO_DER
  25564. encodeSignedDataStream strm;
  25565. /* test setting stream mode, the first one using IO callbacks */
  25566. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  25567. (word32)(testVectors + i)->certSz), 0);
  25568. if (pkcs7 != NULL) {
  25569. #ifdef ECC_TIMING_RESISTANT
  25570. pkcs7->rng = &rng;
  25571. #endif
  25572. if (i != 0)
  25573. pkcs7->content = (byte*)(testVectors + i)->content;
  25574. pkcs7->contentSz = (testVectors + i)->contentSz;
  25575. pkcs7->contentOID = (testVectors + i)->contentOID;
  25576. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  25577. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  25578. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  25579. pkcs7->privateKey = (testVectors + i)->privateKey;
  25580. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  25581. }
  25582. if (i == 0) {
  25583. XMEMSET(&strm, 0, sizeof(strm));
  25584. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
  25585. StreamOutputCB, (void*)&strm), 0);
  25586. encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL, 0);
  25587. }
  25588. else {
  25589. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL), 0);
  25590. encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25591. (word32)sizeof(output));
  25592. }
  25593. switch ((testVectors + i)->encryptOID) {
  25594. #ifndef NO_DES3
  25595. case DES3b:
  25596. case DESb:
  25597. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25598. break;
  25599. #endif
  25600. #ifdef HAVE_AESCCM
  25601. #ifdef WOLFSSL_AES_128
  25602. case AES128CCMb:
  25603. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25604. break;
  25605. #endif
  25606. #ifdef WOLFSSL_AES_192
  25607. case AES192CCMb:
  25608. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25609. break;
  25610. #endif
  25611. #ifdef WOLFSSL_AES_256
  25612. case AES256CCMb:
  25613. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25614. break;
  25615. #endif
  25616. #endif
  25617. default:
  25618. ExpectIntGE(encodedSz, 0);
  25619. }
  25620. if (encodedSz > 0) {
  25621. if (i == 0) {
  25622. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7,
  25623. strm.out, (word32)encodedSz, decoded,
  25624. (word32)sizeof(decoded));
  25625. }
  25626. else {
  25627. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25628. (word32)encodedSz, decoded, (word32)sizeof(decoded));
  25629. }
  25630. ExpectIntGE(decodedSz, 0);
  25631. /* Verify the size of each buffer. */
  25632. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  25633. }
  25634. wc_PKCS7_Free(pkcs7);
  25635. pkcs7 = NULL;
  25636. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25637. #endif
  25638. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  25639. (word32)(testVectors + i)->certSz), 0);
  25640. if (pkcs7 != NULL) {
  25641. #ifdef ECC_TIMING_RESISTANT
  25642. pkcs7->rng = &rng;
  25643. #endif
  25644. pkcs7->content = (byte*)(testVectors + i)->content;
  25645. pkcs7->contentSz = (testVectors + i)->contentSz;
  25646. pkcs7->contentOID = (testVectors + i)->contentOID;
  25647. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  25648. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  25649. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  25650. pkcs7->privateKey = (testVectors + i)->privateKey;
  25651. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  25652. }
  25653. #ifdef ASN_BER_TO_DER
  25654. /* test without setting stream mode */
  25655. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  25656. #endif
  25657. ExpectIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25658. (word32)sizeof(output)), 0);
  25659. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25660. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  25661. ExpectIntGE(decodedSz, 0);
  25662. /* Verify the size of each buffer. */
  25663. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  25664. /* Don't free the last time through the loop. */
  25665. if (i < testSz - 1) {
  25666. wc_PKCS7_Free(pkcs7);
  25667. pkcs7 = NULL;
  25668. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25669. }
  25670. } /* END test loop. */
  25671. }
  25672. /* Test bad args. */
  25673. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  25674. (word32)sizeof(output)), BAD_FUNC_ARG);
  25675. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  25676. (word32)sizeof(output)), BAD_FUNC_ARG);
  25677. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  25678. /* Decode. */
  25679. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  25680. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25681. BAD_FUNC_ARG);
  25682. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25683. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25684. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25685. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  25686. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  25687. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25688. BAD_FUNC_ARG);
  25689. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  25690. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25691. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  25692. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  25693. /* only a failure for KARI test cases */
  25694. if (pkcs7 != NULL) {
  25695. tempWrd32 = pkcs7->singleCertSz;
  25696. pkcs7->singleCertSz = 0;
  25697. }
  25698. #if defined(WOLFSSL_ASN_TEMPLATE)
  25699. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25700. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25701. BUFFER_E);
  25702. #else
  25703. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25704. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25705. ASN_PARSE_E);
  25706. #endif
  25707. if (pkcs7 != NULL) {
  25708. pkcs7->singleCertSz = tempWrd32;
  25709. tmpBytePtr = pkcs7->singleCert;
  25710. pkcs7->singleCert = NULL;
  25711. }
  25712. #ifndef NO_RSA
  25713. #if defined(NO_PKCS7_STREAM)
  25714. /* when none streaming mode is used and PKCS7 is in bad state buffer error
  25715. * is returned from kari parse which gets set to bad func arg */
  25716. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25717. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25718. BAD_FUNC_ARG);
  25719. #else
  25720. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25721. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25722. ASN_PARSE_E);
  25723. #endif
  25724. #endif /* !NO_RSA */
  25725. if (pkcs7 != NULL) {
  25726. pkcs7->singleCert = tmpBytePtr;
  25727. }
  25728. #endif
  25729. if (pkcs7 != NULL) {
  25730. tempWrd32 = pkcs7->privateKeySz;
  25731. pkcs7->privateKeySz = 0;
  25732. }
  25733. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25734. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25735. BAD_FUNC_ARG);
  25736. if (pkcs7 != NULL) {
  25737. pkcs7->privateKeySz = tempWrd32;
  25738. tmpBytePtr = pkcs7->privateKey;
  25739. pkcs7->privateKey = NULL;
  25740. }
  25741. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25742. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25743. BAD_FUNC_ARG);
  25744. if (pkcs7 != NULL) {
  25745. pkcs7->privateKey = tmpBytePtr;
  25746. }
  25747. wc_PKCS7_Free(pkcs7);
  25748. pkcs7 = NULL;
  25749. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  25750. /* test of decrypt callback with KEKRI enveloped data */
  25751. {
  25752. int envelopedSz = 0;
  25753. const byte keyId[] = { 0x00 };
  25754. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25755. if (pkcs7 != NULL) {
  25756. pkcs7->content = (byte*)input;
  25757. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  25758. pkcs7->contentOID = DATA;
  25759. pkcs7->encryptOID = AES256CBCb;
  25760. }
  25761. ExpectIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  25762. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  25763. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  25764. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  25765. ExpectIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25766. (word32)sizeof(output))), 0);
  25767. wc_PKCS7_Free(pkcs7);
  25768. pkcs7 = NULL;
  25769. /* decode envelopedData */
  25770. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25771. ExpectIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  25772. ExpectIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  25773. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25774. envelopedSz, decoded, sizeof(decoded))), 0);
  25775. wc_PKCS7_Free(pkcs7);
  25776. pkcs7 = NULL;
  25777. }
  25778. #endif /* !NO_AES && !NO_AES_256 */
  25779. #ifndef NO_RSA
  25780. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25781. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25782. #endif /* NO_RSA */
  25783. #ifdef HAVE_ECC
  25784. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25785. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25786. #endif /* HAVE_ECC */
  25787. #ifdef ECC_TIMING_RESISTANT
  25788. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  25789. #endif
  25790. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && \
  25791. !defined(NO_RSA) && !defined(NO_SHA)
  25792. {
  25793. byte out[7];
  25794. byte *cms = NULL;
  25795. word32 cmsSz;
  25796. XFILE cmsFile = XBADFILE;
  25797. XMEMSET(out, 0, sizeof(out));
  25798. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25799. ExpectTrue((cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb"))
  25800. != XBADFILE);
  25801. cmsSz = (word32)FOURK_BUF;
  25802. ExpectNotNull(cms = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25803. DYNAMIC_TYPE_TMP_BUFFER));
  25804. ExpectTrue((cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile)) > 0);
  25805. if (cmsFile != XBADFILE)
  25806. XFCLOSE(cmsFile);
  25807. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  25808. sizeof_client_cert_der_2048), 0);
  25809. if (pkcs7 != NULL) {
  25810. pkcs7->privateKey = (byte*)client_key_der_2048;
  25811. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  25812. }
  25813. ExpectIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25814. 2), 0);
  25815. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25816. sizeof(out)), 0);
  25817. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25818. ExpectIntEQ(XMEMCMP(out, "test", 4), 0);
  25819. wc_PKCS7_Free(pkcs7);
  25820. pkcs7 = NULL;
  25821. }
  25822. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 && !NO_RSA && !NO_SHA */
  25823. #endif /* HAVE_PKCS7 */
  25824. return EXPECT_RESULT();
  25825. } /* END test_wc_PKCS7_EncodeDecodeEnvelopedData() */
  25826. /*
  25827. * Testing wc_PKCS7_EncodeEncryptedData()
  25828. */
  25829. static int test_wc_PKCS7_EncodeEncryptedData(void)
  25830. {
  25831. EXPECT_DECLS;
  25832. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  25833. PKCS7* pkcs7 = NULL;
  25834. byte* tmpBytePtr = NULL;
  25835. byte encrypted[TWOK_BUF];
  25836. byte decoded[TWOK_BUF];
  25837. word32 tmpWrd32 = 0;
  25838. int tmpInt = 0;
  25839. int decodedSz = 0;
  25840. int encryptedSz = 0;
  25841. int testSz = 0;
  25842. int i = 0;
  25843. const byte data[] = { /* Hello World */
  25844. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  25845. 0x72,0x6c,0x64
  25846. };
  25847. #ifndef NO_DES3
  25848. byte desKey[] = {
  25849. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  25850. };
  25851. byte des3Key[] = {
  25852. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  25853. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  25854. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  25855. };
  25856. #endif
  25857. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25858. #ifndef NO_AES_128
  25859. byte aes128Key[] = {
  25860. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25861. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25862. };
  25863. #endif
  25864. #ifndef NO_AES_192
  25865. byte aes192Key[] = {
  25866. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25867. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25868. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25869. };
  25870. #endif
  25871. #ifndef NO_AES_256
  25872. byte aes256Key[] = {
  25873. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25874. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25875. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25876. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25877. };
  25878. #endif
  25879. #endif /* !NO_AES && HAVE_AES_CBC */
  25880. const pkcs7EncryptedVector testVectors[] =
  25881. {
  25882. #ifndef NO_DES3
  25883. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  25884. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  25885. #endif /* !NO_DES3 */
  25886. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25887. #ifndef NO_AES_128
  25888. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  25889. sizeof(aes128Key)},
  25890. #endif
  25891. #ifndef NO_AES_192
  25892. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  25893. sizeof(aes192Key)},
  25894. #endif
  25895. #ifndef NO_AES_256
  25896. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  25897. sizeof(aes256Key)},
  25898. #endif
  25899. #endif /* !NO_AES && HAVE_AES_CBC */
  25900. };
  25901. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  25902. for (i = 0; i < testSz; i++) {
  25903. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25904. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25905. if (pkcs7 != NULL) {
  25906. pkcs7->content = (byte*)testVectors[i].content;
  25907. pkcs7->contentSz = testVectors[i].contentSz;
  25908. pkcs7->contentOID = testVectors[i].contentOID;
  25909. pkcs7->encryptOID = testVectors[i].encryptOID;
  25910. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  25911. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  25912. pkcs7->heap = HEAP_HINT;
  25913. }
  25914. /* encode encryptedData */
  25915. ExpectIntGT(encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25916. sizeof(encrypted)), 0);
  25917. /* Decode encryptedData */
  25918. ExpectIntGT(decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted,
  25919. encryptedSz, decoded, sizeof(decoded)), 0);
  25920. ExpectIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  25921. /* Keep values for last itr. */
  25922. if (i < testSz - 1) {
  25923. wc_PKCS7_Free(pkcs7);
  25924. pkcs7 = NULL;
  25925. }
  25926. }
  25927. if (pkcs7 == NULL || testSz == 0) {
  25928. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25929. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25930. }
  25931. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  25932. sizeof(encrypted)),BAD_FUNC_ARG);
  25933. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  25934. sizeof(encrypted)), BAD_FUNC_ARG);
  25935. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25936. 0), BAD_FUNC_ARG);
  25937. /* Testing the struct. */
  25938. if (pkcs7 != NULL) {
  25939. tmpBytePtr = pkcs7->content;
  25940. pkcs7->content = NULL;
  25941. }
  25942. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25943. sizeof(encrypted)), BAD_FUNC_ARG);
  25944. if (pkcs7 != NULL) {
  25945. pkcs7->content = tmpBytePtr;
  25946. tmpWrd32 = pkcs7->contentSz;
  25947. pkcs7->contentSz = 0;
  25948. }
  25949. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25950. sizeof(encrypted)), BAD_FUNC_ARG);
  25951. if (pkcs7 != NULL) {
  25952. pkcs7->contentSz = tmpWrd32;
  25953. tmpInt = pkcs7->encryptOID;
  25954. pkcs7->encryptOID = 0;
  25955. }
  25956. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25957. sizeof(encrypted)), BAD_FUNC_ARG);
  25958. if (pkcs7 != NULL) {
  25959. pkcs7->encryptOID = tmpInt;
  25960. tmpBytePtr = pkcs7->encryptionKey;
  25961. pkcs7->encryptionKey = NULL;
  25962. }
  25963. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25964. sizeof(encrypted)), BAD_FUNC_ARG);
  25965. if (pkcs7 != NULL) {
  25966. pkcs7->encryptionKey = tmpBytePtr;
  25967. tmpWrd32 = pkcs7->encryptionKeySz;
  25968. pkcs7->encryptionKeySz = 0;
  25969. }
  25970. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25971. sizeof(encrypted)), BAD_FUNC_ARG);
  25972. if (pkcs7 != NULL) {
  25973. pkcs7->encryptionKeySz = tmpWrd32;
  25974. }
  25975. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  25976. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25977. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  25978. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25979. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  25980. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25981. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25982. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  25983. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25984. decoded, 0), BAD_FUNC_ARG);
  25985. /* Test struct fields */
  25986. if (pkcs7 != NULL) {
  25987. tmpBytePtr = pkcs7->encryptionKey;
  25988. pkcs7->encryptionKey = NULL;
  25989. }
  25990. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25991. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25992. if (pkcs7 != NULL) {
  25993. pkcs7->encryptionKey = tmpBytePtr;
  25994. pkcs7->encryptionKeySz = 0;
  25995. }
  25996. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25997. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25998. wc_PKCS7_Free(pkcs7);
  25999. #endif
  26000. return EXPECT_RESULT();
  26001. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  26002. /*
  26003. * Testing wc_PKCS7_Degenerate()
  26004. */
  26005. static int test_wc_PKCS7_Degenerate(void)
  26006. {
  26007. EXPECT_DECLS;
  26008. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  26009. PKCS7* pkcs7 = NULL;
  26010. char fName[] = "./certs/test-degenerate.p7b";
  26011. XFILE f = XBADFILE;
  26012. byte der[4096];
  26013. word32 derSz = 0;
  26014. #ifndef NO_PKCS7_STREAM
  26015. word32 z;
  26016. int ret;
  26017. #endif /* !NO_PKCS7_STREAM */
  26018. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  26019. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26020. if (f != XBADFILE)
  26021. XFCLOSE(f);
  26022. /* test degenerate success */
  26023. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26024. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26025. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26026. #ifndef NO_RSA
  26027. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26028. #ifndef NO_PKCS7_STREAM
  26029. wc_PKCS7_Free(pkcs7);
  26030. pkcs7 = NULL;
  26031. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26032. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26033. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26034. /* test for streaming */
  26035. ret = -1;
  26036. for (z = 0; z < derSz && ret != 0; z++) {
  26037. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  26038. if (ret < 0){
  26039. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26040. }
  26041. }
  26042. ExpectIntEQ(ret, 0);
  26043. #endif /* !NO_PKCS7_STREAM */
  26044. #else
  26045. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26046. #endif /* NO_RSA */
  26047. wc_PKCS7_Free(pkcs7);
  26048. pkcs7 = NULL;
  26049. /* test with turning off degenerate cases */
  26050. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26051. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26052. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26053. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  26054. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz),
  26055. PKCS7_NO_SIGNER_E);
  26056. #ifndef NO_PKCS7_STREAM
  26057. wc_PKCS7_Free(pkcs7);
  26058. pkcs7 = NULL;
  26059. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26060. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26061. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26062. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  26063. /* test for streaming */
  26064. ret = -1;
  26065. for (z = 0; z < derSz && ret != 0; z++) {
  26066. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  26067. if (ret == WC_PKCS7_WANT_READ_E){
  26068. continue;
  26069. }
  26070. else
  26071. break;
  26072. }
  26073. ExpectIntEQ(ret, PKCS7_NO_SIGNER_E);
  26074. #endif /* !NO_PKCS7_STREAM */
  26075. wc_PKCS7_Free(pkcs7);
  26076. #endif
  26077. return EXPECT_RESULT();
  26078. } /* END test_wc_PKCS7_Degenerate() */
  26079. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  26080. defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
  26081. static byte berContent[] = {
  26082. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  26083. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  26084. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  26085. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  26086. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  26087. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  26088. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  26089. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  26090. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  26091. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  26092. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  26093. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  26094. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  26095. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  26096. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  26097. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  26098. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  26099. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  26100. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  26101. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  26102. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  26103. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  26104. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  26105. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  26106. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  26107. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  26108. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  26109. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  26110. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  26111. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  26112. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  26113. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  26114. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  26115. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  26116. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  26117. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  26118. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  26119. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  26120. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  26121. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  26122. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  26123. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  26124. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  26125. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  26126. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  26127. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  26128. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  26129. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  26130. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  26131. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  26132. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  26133. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  26134. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  26135. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  26136. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  26137. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  26138. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  26139. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  26140. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  26141. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  26142. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  26143. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  26144. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  26145. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  26146. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  26147. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  26148. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  26149. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  26150. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  26151. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  26152. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  26153. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  26154. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  26155. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  26156. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  26157. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  26158. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  26159. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  26160. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  26161. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  26162. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  26163. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  26164. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  26165. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  26166. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  26167. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  26168. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  26169. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  26170. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  26171. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  26172. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  26173. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  26174. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  26175. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  26176. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  26177. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  26178. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  26179. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  26180. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  26181. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  26182. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  26183. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  26184. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  26185. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  26186. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  26187. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  26188. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  26189. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  26190. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  26191. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  26192. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  26193. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  26194. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  26195. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  26196. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  26197. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  26198. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  26199. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  26200. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  26201. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  26202. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  26203. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  26204. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  26205. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  26206. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  26207. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  26208. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  26209. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  26210. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  26211. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  26212. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  26213. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  26214. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  26215. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  26216. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  26217. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  26218. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  26219. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  26220. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  26221. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  26222. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  26223. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  26224. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  26225. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  26226. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  26227. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  26228. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  26229. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  26230. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  26231. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  26232. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  26233. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  26234. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  26235. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  26236. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  26237. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  26238. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  26239. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  26240. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  26241. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  26242. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  26243. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  26244. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  26245. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  26246. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  26247. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  26248. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  26249. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  26250. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  26251. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  26252. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  26253. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  26254. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  26255. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  26256. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  26257. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  26258. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  26259. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  26260. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  26261. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  26262. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  26263. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  26264. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  26265. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  26266. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  26267. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  26268. 0x00, 0x00, 0x00, 0x00, 0x00
  26269. };
  26270. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
  26271. * !NO_DES3 && !NO_SHA
  26272. */
  26273. /*
  26274. * Testing wc_PKCS7_BER()
  26275. */
  26276. static int test_wc_PKCS7_BER(void)
  26277. {
  26278. EXPECT_DECLS;
  26279. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  26280. !defined(NO_SHA) && defined(ASN_BER_TO_DER)
  26281. PKCS7* pkcs7 = NULL;
  26282. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  26283. XFILE f = XBADFILE;
  26284. byte der[4096];
  26285. #ifndef NO_DES3
  26286. byte decoded[2048];
  26287. #endif
  26288. word32 derSz = 0;
  26289. #ifndef NO_PKCS7_STREAM
  26290. word32 z;
  26291. int ret;
  26292. #endif /* !NO_PKCS7_STREAM */
  26293. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  26294. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26295. if (f != XBADFILE) {
  26296. XFCLOSE(f);
  26297. f = XBADFILE;
  26298. }
  26299. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26300. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26301. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26302. #ifndef NO_RSA
  26303. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26304. #ifndef NO_PKCS7_STREAM
  26305. wc_PKCS7_Free(pkcs7);
  26306. pkcs7 = NULL;
  26307. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26308. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26309. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26310. /* test for streaming */
  26311. ret = -1;
  26312. for (z = 0; z < derSz && ret != 0; z++) {
  26313. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  26314. if (ret < 0){
  26315. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26316. }
  26317. }
  26318. ExpectIntEQ(ret, 0);
  26319. #endif /* !NO_PKCS7_STREAM */
  26320. #else
  26321. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26322. #endif
  26323. wc_PKCS7_Free(pkcs7);
  26324. pkcs7 = NULL;
  26325. #ifndef NO_DES3
  26326. /* decode BER content */
  26327. ExpectTrue((f = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  26328. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26329. if (f != XBADFILE) {
  26330. XFCLOSE(f);
  26331. f = XBADFILE;
  26332. }
  26333. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26334. #ifndef NO_RSA
  26335. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  26336. #else
  26337. ExpectIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  26338. #endif
  26339. ExpectTrue((f = XFOPEN("./certs/1024/client-key.der", "rb")) != XBADFILE);
  26340. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26341. if (f != XBADFILE) {
  26342. XFCLOSE(f);
  26343. f = XBADFILE;
  26344. }
  26345. if (pkcs7 != NULL) {
  26346. pkcs7->privateKey = der;
  26347. pkcs7->privateKeySz = derSz;
  26348. }
  26349. #ifndef NO_RSA
  26350. #ifdef WOLFSSL_SP_MATH
  26351. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26352. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  26353. #else
  26354. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26355. sizeof(berContent), decoded, sizeof(decoded)), 0);
  26356. #endif
  26357. #else
  26358. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26359. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  26360. #endif
  26361. wc_PKCS7_Free(pkcs7);
  26362. #endif /* !NO_DES3 */
  26363. #endif
  26364. return EXPECT_RESULT();
  26365. } /* END test_wc_PKCS7_BER() */
  26366. static int test_wc_PKCS7_signed_enveloped(void)
  26367. {
  26368. EXPECT_DECLS;
  26369. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  26370. !defined(NO_FILESYSTEM)
  26371. XFILE f = XBADFILE;
  26372. PKCS7* pkcs7 = NULL;
  26373. #ifdef HAVE_AES_CBC
  26374. PKCS7* inner = NULL;
  26375. #endif
  26376. WC_RNG rng;
  26377. unsigned char key[FOURK_BUF/2];
  26378. unsigned char cert[FOURK_BUF/2];
  26379. unsigned char env[FOURK_BUF];
  26380. int envSz = FOURK_BUF;
  26381. int keySz = 0;
  26382. int certSz = 0;
  26383. unsigned char sig[FOURK_BUF * 2];
  26384. int sigSz = FOURK_BUF * 2;
  26385. #ifdef HAVE_AES_CBC
  26386. unsigned char decoded[FOURK_BUF];
  26387. int decodedSz = FOURK_BUF;
  26388. #endif
  26389. #ifndef NO_PKCS7_STREAM
  26390. int z;
  26391. int ret;
  26392. #endif /* !NO_PKCS7_STREAM */
  26393. XMEMSET(&rng, 0, sizeof(WC_RNG));
  26394. /* load cert */
  26395. ExpectTrue((f = XFOPEN(cliCertDerFile, "rb")) != XBADFILE);
  26396. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  26397. if (f != XBADFILE) {
  26398. XFCLOSE(f);
  26399. f = XBADFILE;
  26400. }
  26401. /* load key */
  26402. ExpectTrue((f = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  26403. ExpectIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  26404. if (f != XBADFILE) {
  26405. XFCLOSE(f);
  26406. f = XBADFILE;
  26407. }
  26408. ExpectIntGT(keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL), 0);
  26409. /* sign cert for envelope */
  26410. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26411. ExpectIntEQ(wc_InitRng(&rng), 0);
  26412. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26413. if (pkcs7 != NULL) {
  26414. pkcs7->content = cert;
  26415. pkcs7->contentSz = certSz;
  26416. pkcs7->contentOID = DATA;
  26417. pkcs7->privateKey = key;
  26418. pkcs7->privateKeySz = keySz;
  26419. pkcs7->encryptOID = RSAk;
  26420. pkcs7->hashOID = SHA256h;
  26421. pkcs7->rng = &rng;
  26422. }
  26423. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26424. wc_PKCS7_Free(pkcs7);
  26425. pkcs7 = NULL;
  26426. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  26427. #ifdef HAVE_AES_CBC
  26428. /* create envelope */
  26429. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26430. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26431. if (pkcs7 != NULL) {
  26432. pkcs7->content = sig;
  26433. pkcs7->contentSz = sigSz;
  26434. pkcs7->contentOID = DATA;
  26435. pkcs7->encryptOID = AES256CBCb;
  26436. pkcs7->privateKey = key;
  26437. pkcs7->privateKeySz = keySz;
  26438. }
  26439. ExpectIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  26440. ExpectIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  26441. wc_PKCS7_Free(pkcs7);
  26442. pkcs7 = NULL;
  26443. #endif
  26444. /* create bad signed enveloped data */
  26445. sigSz = FOURK_BUF * 2;
  26446. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26447. ExpectIntEQ(wc_InitRng(&rng), 0);
  26448. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26449. if (pkcs7 != NULL) {
  26450. pkcs7->content = env;
  26451. pkcs7->contentSz = envSz;
  26452. pkcs7->contentOID = DATA;
  26453. pkcs7->privateKey = key;
  26454. pkcs7->privateKeySz = keySz;
  26455. pkcs7->encryptOID = RSAk;
  26456. pkcs7->hashOID = SHA256h;
  26457. pkcs7->rng = &rng;
  26458. }
  26459. /* Set no certs in bundle for this test. */
  26460. if (pkcs7 != NULL) {
  26461. ExpectIntEQ(wc_PKCS7_SetNoCerts(pkcs7, 1), 0);
  26462. ExpectIntEQ(wc_PKCS7_SetNoCerts(NULL, 1), BAD_FUNC_ARG);
  26463. ExpectIntEQ(wc_PKCS7_GetNoCerts(pkcs7), 1);
  26464. }
  26465. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26466. wc_PKCS7_Free(pkcs7);
  26467. pkcs7 = NULL;
  26468. /* check verify fails */
  26469. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26470. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26471. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  26472. PKCS7_SIGNEEDS_CHECK);
  26473. /* try verifying the signature manually */
  26474. {
  26475. RsaKey rKey;
  26476. word32 idx = 0;
  26477. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  26478. WC_MAX_DIGEST_SIZE];
  26479. int digestSz = 0;
  26480. ExpectIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  26481. ExpectIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  26482. ExpectIntGT(digestSz = wc_RsaSSL_Verify(pkcs7->signature,
  26483. pkcs7->signatureSz, digest, sizeof(digest), &rKey), 0);
  26484. ExpectIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  26485. ExpectIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  26486. ExpectIntEQ(wc_FreeRsaKey(&rKey), 0);
  26487. /* verify was success */
  26488. }
  26489. wc_PKCS7_Free(pkcs7);
  26490. pkcs7 = NULL;
  26491. /* initializing the PKCS7 struct with the signing certificate should pass */
  26492. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26493. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26494. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  26495. #ifndef NO_PKCS7_STREAM
  26496. wc_PKCS7_Free(pkcs7);
  26497. pkcs7 = NULL;
  26498. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26499. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26500. /* test for streaming */
  26501. ret = -1;
  26502. for (z = 0; z < sigSz && ret != 0; z++) {
  26503. ret = wc_PKCS7_VerifySignedData(pkcs7, sig + z, 1);
  26504. if (ret < 0){
  26505. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26506. }
  26507. }
  26508. ExpectIntEQ(ret, 0);
  26509. #endif /* !NO_PKCS7_STREAM */
  26510. wc_PKCS7_Free(pkcs7);
  26511. pkcs7 = NULL;
  26512. /* create valid degenerate bundle */
  26513. sigSz = FOURK_BUF * 2;
  26514. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26515. if (pkcs7 != NULL) {
  26516. pkcs7->content = env;
  26517. pkcs7->contentSz = envSz;
  26518. pkcs7->contentOID = DATA;
  26519. pkcs7->privateKey = key;
  26520. pkcs7->privateKeySz = keySz;
  26521. pkcs7->encryptOID = RSAk;
  26522. pkcs7->hashOID = SHA256h;
  26523. pkcs7->rng = &rng;
  26524. }
  26525. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  26526. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26527. wc_PKCS7_Free(pkcs7);
  26528. pkcs7 = NULL;
  26529. wc_FreeRng(&rng);
  26530. /* check verify */
  26531. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26532. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26533. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  26534. ExpectNotNull(pkcs7->content);
  26535. #ifndef NO_PKCS7_STREAM
  26536. wc_PKCS7_Free(pkcs7);
  26537. pkcs7 = NULL;
  26538. /* create valid degenerate bundle */
  26539. sigSz = FOURK_BUF * 2;
  26540. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26541. if (pkcs7 != NULL) {
  26542. pkcs7->content = env;
  26543. pkcs7->contentSz = envSz;
  26544. pkcs7->contentOID = DATA;
  26545. pkcs7->privateKey = key;
  26546. pkcs7->privateKeySz = keySz;
  26547. pkcs7->encryptOID = RSAk;
  26548. pkcs7->hashOID = SHA256h;
  26549. pkcs7->rng = &rng;
  26550. }
  26551. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  26552. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26553. wc_PKCS7_Free(pkcs7);
  26554. pkcs7 = NULL;
  26555. wc_FreeRng(&rng);
  26556. /* check verify */
  26557. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26558. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26559. /* test for streaming */
  26560. ret = -1;
  26561. for (z = 0; z < sigSz && ret != 0; z++) {
  26562. ret = wc_PKCS7_VerifySignedData(pkcs7, sig + z, 1);
  26563. if (ret < 0){
  26564. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26565. }
  26566. }
  26567. ExpectIntEQ(ret, 0);
  26568. #endif /* !NO_PKCS7_STREAM */
  26569. #ifdef HAVE_AES_CBC
  26570. /* check decode */
  26571. ExpectNotNull(inner = wc_PKCS7_New(NULL, 0));
  26572. ExpectIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  26573. if (inner != NULL) {
  26574. inner->privateKey = key;
  26575. inner->privateKeySz = keySz;
  26576. }
  26577. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  26578. pkcs7->contentSz, decoded, decodedSz)), 0);
  26579. wc_PKCS7_Free(inner);
  26580. inner = NULL;
  26581. #endif
  26582. wc_PKCS7_Free(pkcs7);
  26583. pkcs7 = NULL;
  26584. #ifdef HAVE_AES_CBC
  26585. /* check cert set */
  26586. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26587. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26588. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  26589. ExpectNotNull(pkcs7->singleCert);
  26590. ExpectIntNE(pkcs7->singleCertSz, 0);
  26591. wc_PKCS7_Free(pkcs7);
  26592. pkcs7 = NULL;
  26593. #ifndef NO_PKCS7_STREAM
  26594. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26595. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26596. /* test for streaming */
  26597. ret = -1;
  26598. for (z = 0; z < decodedSz && ret != 0; z++) {
  26599. ret = wc_PKCS7_VerifySignedData(pkcs7, decoded + z, 1);
  26600. if (ret < 0){
  26601. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26602. }
  26603. }
  26604. ExpectIntEQ(ret, 0);
  26605. ExpectNotNull(pkcs7->singleCert);
  26606. ExpectIntNE(pkcs7->singleCertSz, 0);
  26607. wc_PKCS7_Free(pkcs7);
  26608. pkcs7 = NULL;
  26609. #endif /* !NO_PKCS7_STREAM */
  26610. #endif
  26611. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  26612. return EXPECT_RESULT();
  26613. }
  26614. static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
  26615. {
  26616. EXPECT_DECLS;
  26617. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26618. && !defined(NO_AES)
  26619. PKCS7* pkcs7 = NULL;
  26620. void* heap = NULL;
  26621. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26622. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26623. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  26624. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  26625. wc_PKCS7_Free(pkcs7);
  26626. #endif
  26627. return EXPECT_RESULT();
  26628. }
  26629. static int test_wc_PKCS7_SetOriEncryptCtx(void)
  26630. {
  26631. EXPECT_DECLS;
  26632. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26633. && !defined(NO_AES)
  26634. PKCS7* pkcs7 = NULL;
  26635. void* heap = NULL;
  26636. WOLFSSL_CTX* ctx = NULL;
  26637. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26638. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26639. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26640. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  26641. wc_PKCS7_Free(pkcs7);
  26642. #endif
  26643. return EXPECT_RESULT();
  26644. }
  26645. static int test_wc_PKCS7_SetOriDecryptCtx(void)
  26646. {
  26647. EXPECT_DECLS;
  26648. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26649. && !defined(NO_AES)
  26650. PKCS7* pkcs7 = NULL;
  26651. void* heap = NULL;
  26652. WOLFSSL_CTX* ctx = NULL;
  26653. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26654. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26655. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26656. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  26657. wc_PKCS7_Free(pkcs7);
  26658. #endif
  26659. return EXPECT_RESULT();
  26660. }
  26661. static int test_wc_PKCS7_DecodeCompressedData(void)
  26662. {
  26663. EXPECT_DECLS;
  26664. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26665. && !defined(NO_AES) && defined(HAVE_LIBZ)
  26666. PKCS7* pkcs7 = NULL;
  26667. void* heap = NULL;
  26668. byte out[4096];
  26669. byte* decompressed = NULL;
  26670. int outSz;
  26671. int decompressedSz;
  26672. const char* cert = "./certs/client-cert.pem";
  26673. byte* cert_buf = NULL;
  26674. size_t cert_sz = 0;
  26675. ExpectIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  26676. ExpectNotNull((decompressed = (byte*)XMALLOC(cert_sz, heap,
  26677. DYNAMIC_TYPE_TMP_BUFFER)));
  26678. decompressedSz = (int)cert_sz;
  26679. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26680. if (pkcs7 != NULL) {
  26681. pkcs7->content = (byte*)cert_buf;
  26682. pkcs7->contentSz = (word32)cert_sz;
  26683. pkcs7->contentOID = DATA;
  26684. }
  26685. ExpectIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  26686. sizeof(out))), 0);
  26687. wc_PKCS7_Free(pkcs7);
  26688. pkcs7 = NULL;
  26689. /* compressed key should be smaller than when started */
  26690. ExpectIntLT(outSz, cert_sz);
  26691. /* test decompression */
  26692. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26693. ExpectIntEQ(pkcs7->contentOID, 0);
  26694. /* fail case with out buffer too small */
  26695. ExpectIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26696. decompressed, outSz), 0);
  26697. /* success case */
  26698. ExpectIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26699. decompressed, decompressedSz), cert_sz);
  26700. ExpectIntEQ(pkcs7->contentOID, DATA);
  26701. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26702. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26703. decompressed = NULL;
  26704. /* test decompression function with different 'max' inputs */
  26705. outSz = sizeof(out);
  26706. ExpectIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  26707. 0);
  26708. ExpectIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  26709. out, outSz, 0, heap), 0);
  26710. ExpectNull(decompressed);
  26711. ExpectIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  26712. out, outSz, 0, heap), 0);
  26713. ExpectNotNull(decompressed);
  26714. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26715. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26716. decompressed = NULL;
  26717. ExpectIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  26718. out, outSz, 0, heap), 0);
  26719. ExpectNotNull(decompressed);
  26720. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26721. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26722. if (cert_buf != NULL)
  26723. free(cert_buf);
  26724. wc_PKCS7_Free(pkcs7);
  26725. #endif
  26726. return EXPECT_RESULT();
  26727. }
  26728. static int test_wc_i2d_PKCS12(void)
  26729. {
  26730. EXPECT_DECLS;
  26731. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  26732. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26733. && !defined(NO_AES) && !defined(NO_SHA)
  26734. WC_PKCS12* pkcs12 = NULL;
  26735. unsigned char der[FOURK_BUF * 2];
  26736. unsigned char* pt;
  26737. int derSz = 0;
  26738. unsigned char out[FOURK_BUF * 2];
  26739. int outSz = FOURK_BUF * 2;
  26740. const char p12_f[] = "./certs/test-servercert.p12";
  26741. XFILE f = XBADFILE;
  26742. ExpectTrue((f = XFOPEN(p12_f, "rb")) != XBADFILE);
  26743. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  26744. if (f != XBADFILE)
  26745. XFCLOSE(f);
  26746. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  26747. ExpectIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  26748. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26749. ExpectIntEQ(outSz, derSz);
  26750. outSz = derSz - 1;
  26751. pt = out;
  26752. ExpectIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  26753. outSz = derSz;
  26754. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  26755. ExpectIntEQ((pt == out), 0);
  26756. pt = NULL;
  26757. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  26758. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  26759. wc_PKCS12_free(pkcs12);
  26760. pkcs12 = NULL;
  26761. /* Run the same test but use wc_d2i_PKCS12_fp. */
  26762. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  26763. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26764. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26765. ExpectIntEQ(outSz, derSz);
  26766. wc_PKCS12_free(pkcs12);
  26767. pkcs12 = NULL;
  26768. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  26769. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26770. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26771. ExpectIntEQ(outSz, derSz);
  26772. wc_PKCS12_free(pkcs12);
  26773. pkcs12 = NULL;
  26774. #endif
  26775. return EXPECT_RESULT();
  26776. }
  26777. /* Testing wc_SignatureGetSize() for signature type ECC */
  26778. static int test_wc_SignatureGetSize_ecc(void)
  26779. {
  26780. EXPECT_DECLS;
  26781. #if !defined(NO_SIG_WRAPPER) && defined(HAVE_ECC) && !defined(NO_ECC256)
  26782. enum wc_SignatureType sig_type;
  26783. word32 key_len;
  26784. ecc_key ecc;
  26785. const char* qx =
  26786. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  26787. const char* qy =
  26788. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  26789. const char* d =
  26790. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  26791. XMEMSET(&ecc, 0, sizeof(ecc_key));
  26792. ExpectIntEQ(wc_ecc_init(&ecc), 0);
  26793. ExpectIntEQ(wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1"), 0);
  26794. /* Input for signature type ECC */
  26795. sig_type = WC_SIGNATURE_TYPE_ECC;
  26796. key_len = sizeof(ecc_key);
  26797. ExpectIntGT(wc_SignatureGetSize(sig_type, &ecc, key_len), 0);
  26798. /* Test bad args */
  26799. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26800. sig_type = (enum wc_SignatureType) 100;
  26801. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26802. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  26803. sig_type = WC_SIGNATURE_TYPE_ECC;
  26804. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
  26805. key_len = (word32)0;
  26806. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  26807. DoExpectIntEQ(wc_ecc_free(&ecc), 0);
  26808. #endif /* !NO_SIG_WRAPPER && HAVE_ECC && !NO_ECC256 */
  26809. return EXPECT_RESULT();
  26810. } /* END test_wc_SignatureGetSize_ecc() */
  26811. /* Testing wc_SignatureGetSize() for signature type rsa */
  26812. static int test_wc_SignatureGetSize_rsa(void)
  26813. {
  26814. EXPECT_DECLS;
  26815. #if !defined(NO_SIG_WRAPPER) && !defined(NO_RSA)
  26816. enum wc_SignatureType sig_type;
  26817. word32 key_len;
  26818. word32 idx = 0;
  26819. RsaKey rsa_key;
  26820. byte* tmp = NULL;
  26821. size_t bytes;
  26822. XMEMSET(&rsa_key, 0, sizeof(RsaKey));
  26823. #ifdef USE_CERT_BUFFERS_1024
  26824. bytes = (size_t)sizeof_client_key_der_1024;
  26825. if (bytes < (size_t)sizeof_client_key_der_1024)
  26826. bytes = (size_t)sizeof_client_cert_der_1024;
  26827. #elif defined(USE_CERT_BUFFERS_2048)
  26828. bytes = (size_t)sizeof_client_key_der_2048;
  26829. if (bytes < (size_t)sizeof_client_cert_der_2048)
  26830. bytes = (size_t)sizeof_client_cert_der_2048;
  26831. #else
  26832. bytes = FOURK_BUF;
  26833. #endif
  26834. ExpectNotNull(tmp = (byte*)XMALLOC(bytes, HEAP_HINT,
  26835. DYNAMIC_TYPE_TMP_BUFFER));
  26836. if (tmp != NULL) {
  26837. #ifdef USE_CERT_BUFFERS_1024
  26838. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  26839. #elif defined(USE_CERT_BUFFERS_2048)
  26840. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  26841. #elif !defined(NO_FILESYSTEM)
  26842. XFILE file = XBADFILE;
  26843. ExpectTrue((file = XFOPEN(clientKey, "rb")) != XBADFILE);
  26844. ExpectIntGT(bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file), 0);
  26845. if (file != XBADFILE)
  26846. XFCLOSE(file);
  26847. }
  26848. #else
  26849. ExpectFail();
  26850. #endif
  26851. }
  26852. ExpectIntEQ(wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId), 0);
  26853. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes), 0);
  26854. /* Input for signature type RSA */
  26855. sig_type = WC_SIGNATURE_TYPE_RSA;
  26856. key_len = sizeof(RsaKey);
  26857. ExpectIntGT(wc_SignatureGetSize(sig_type, &rsa_key, key_len), 0);
  26858. /* Test bad args */
  26859. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26860. sig_type = (enum wc_SignatureType)100;
  26861. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26862. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  26863. sig_type = WC_SIGNATURE_TYPE_RSA;
  26864. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), BAD_FUNC_ARG);
  26865. key_len = (word32)0;
  26866. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  26867. DoExpectIntEQ(wc_FreeRsaKey(&rsa_key), 0);
  26868. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26869. #endif /* !NO_SIG_WRAPPER && !NO_RSA */
  26870. return EXPECT_RESULT();
  26871. } /* END test_wc_SignatureGetSize_rsa(void) */
  26872. /*----------------------------------------------------------------------------*
  26873. | hash.h Tests
  26874. *----------------------------------------------------------------------------*/
  26875. static int test_wc_HashInit(void)
  26876. {
  26877. EXPECT_DECLS;
  26878. int i; /* 0 indicates tests passed, 1 indicates failure */
  26879. wc_HashAlg hash;
  26880. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26881. enum wc_HashType enumArray[] = {
  26882. #ifndef NO_MD5
  26883. WC_HASH_TYPE_MD5,
  26884. #endif
  26885. #ifndef NO_SHA
  26886. WC_HASH_TYPE_SHA,
  26887. #endif
  26888. #ifdef WOLFSSL_SHA224
  26889. WC_HASH_TYPE_SHA224,
  26890. #endif
  26891. #ifndef NO_SHA256
  26892. WC_HASH_TYPE_SHA256,
  26893. #endif
  26894. #ifdef WOLFSSL_SHA384
  26895. WC_HASH_TYPE_SHA384,
  26896. #endif
  26897. #ifdef WOLFSSL_SHA512
  26898. WC_HASH_TYPE_SHA512,
  26899. #endif
  26900. };
  26901. /* dynamically finds the length */
  26902. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26903. /* For loop to test various arguments... */
  26904. for (i = 0; i < enumlen; i++) {
  26905. /* check for bad args */
  26906. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  26907. wc_HashFree(&hash, enumArray[i]);
  26908. /* check for null ptr */
  26909. ExpectIntEQ(wc_HashInit(NULL, enumArray[i]), BAD_FUNC_ARG);
  26910. } /* end of for loop */
  26911. return EXPECT_RESULT();
  26912. } /* end of test_wc_HashInit */
  26913. /*
  26914. * Unit test function for wc_HashSetFlags()
  26915. */
  26916. static int test_wc_HashSetFlags(void)
  26917. {
  26918. EXPECT_DECLS;
  26919. #ifdef WOLFSSL_HASH_FLAGS
  26920. wc_HashAlg hash;
  26921. word32 flags = 0;
  26922. int i, j;
  26923. int notSupportedLen;
  26924. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26925. enum wc_HashType enumArray[] = {
  26926. #ifndef NO_MD5
  26927. WC_HASH_TYPE_MD5,
  26928. #endif
  26929. #ifndef NO_SHA
  26930. WC_HASH_TYPE_SHA,
  26931. #endif
  26932. #ifdef WOLFSSL_SHA224
  26933. WC_HASH_TYPE_SHA224,
  26934. #endif
  26935. #ifndef NO_SHA256
  26936. WC_HASH_TYPE_SHA256,
  26937. #endif
  26938. #ifdef WOLFSSL_SHA384
  26939. WC_HASH_TYPE_SHA384,
  26940. #endif
  26941. #ifdef WOLFSSL_SHA512
  26942. WC_HASH_TYPE_SHA512,
  26943. #endif
  26944. #ifdef WOLFSSL_SHA3
  26945. WC_HASH_TYPE_SHA3_224,
  26946. #endif
  26947. };
  26948. enum wc_HashType notSupported[] = {
  26949. WC_HASH_TYPE_MD5_SHA,
  26950. WC_HASH_TYPE_MD2,
  26951. WC_HASH_TYPE_MD4,
  26952. WC_HASH_TYPE_BLAKE2B,
  26953. WC_HASH_TYPE_BLAKE2S,
  26954. WC_HASH_TYPE_NONE,
  26955. };
  26956. /* dynamically finds the length */
  26957. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26958. /* For loop to test various arguments... */
  26959. for (i = 0; i < enumlen; i++) {
  26960. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  26961. ExpectIntEQ(wc_HashSetFlags(&hash, enumArray[i], flags), 0);
  26962. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  26963. ExpectIntEQ(wc_HashSetFlags(NULL, enumArray[i], flags), BAD_FUNC_ARG);
  26964. wc_HashFree(&hash, enumArray[i]);
  26965. }
  26966. /* For loop to test not supported cases */
  26967. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  26968. for (j = 0; j < notSupportedLen; j++) {
  26969. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  26970. ExpectIntEQ(wc_HashSetFlags(&hash, notSupported[j], flags),
  26971. BAD_FUNC_ARG);
  26972. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  26973. }
  26974. #endif
  26975. return EXPECT_RESULT();
  26976. } /* END test_wc_HashSetFlags */
  26977. /*
  26978. * Unit test function for wc_HashGetFlags()
  26979. */
  26980. static int test_wc_HashGetFlags(void)
  26981. {
  26982. EXPECT_DECLS;
  26983. #ifdef WOLFSSL_HASH_FLAGS
  26984. wc_HashAlg hash;
  26985. word32 flags = 0;
  26986. int i, j;
  26987. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26988. enum wc_HashType enumArray[] = {
  26989. #ifndef NO_MD5
  26990. WC_HASH_TYPE_MD5,
  26991. #endif
  26992. #ifndef NO_SHA
  26993. WC_HASH_TYPE_SHA,
  26994. #endif
  26995. #ifdef WOLFSSL_SHA224
  26996. WC_HASH_TYPE_SHA224,
  26997. #endif
  26998. #ifndef NO_SHA256
  26999. WC_HASH_TYPE_SHA256,
  27000. #endif
  27001. #ifdef WOLFSSL_SHA384
  27002. WC_HASH_TYPE_SHA384,
  27003. #endif
  27004. #ifdef WOLFSSL_SHA512
  27005. WC_HASH_TYPE_SHA512,
  27006. #endif
  27007. #ifdef WOLFSSL_SHA3
  27008. WC_HASH_TYPE_SHA3_224,
  27009. #endif
  27010. };
  27011. enum wc_HashType notSupported[] = {
  27012. WC_HASH_TYPE_MD5_SHA,
  27013. WC_HASH_TYPE_MD2,
  27014. WC_HASH_TYPE_MD4,
  27015. WC_HASH_TYPE_BLAKE2B,
  27016. WC_HASH_TYPE_BLAKE2S,
  27017. WC_HASH_TYPE_NONE,
  27018. };
  27019. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  27020. int notSupportedLen;
  27021. /* For loop to test various arguments... */
  27022. for (i = 0; i < enumlen; i++) {
  27023. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  27024. ExpectIntEQ(wc_HashGetFlags(&hash, enumArray[i], &flags), 0);
  27025. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  27026. ExpectIntEQ(wc_HashGetFlags(NULL, enumArray[i], &flags), BAD_FUNC_ARG);
  27027. wc_HashFree(&hash, enumArray[i]);
  27028. }
  27029. /* For loop to test not supported cases */
  27030. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  27031. for (j = 0; j < notSupportedLen; j++) {
  27032. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  27033. ExpectIntEQ(wc_HashGetFlags(&hash, notSupported[j], &flags),
  27034. BAD_FUNC_ARG);
  27035. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  27036. }
  27037. #endif
  27038. return EXPECT_RESULT();
  27039. } /* END test_wc_HashGetFlags */
  27040. /*----------------------------------------------------------------------------*
  27041. | Compatibility Tests
  27042. *----------------------------------------------------------------------------*/
  27043. /*----------------------------------------------------------------------------*
  27044. | ASN.1 Tests
  27045. *----------------------------------------------------------------------------*/
  27046. static int test_wolfSSL_ASN1_BIT_STRING(void)
  27047. {
  27048. EXPECT_DECLS;
  27049. #if !defined(NO_CERTS) && defined(OPENSSL_ALL)
  27050. ASN1_BIT_STRING* str = NULL;
  27051. ExpectNotNull(str = ASN1_BIT_STRING_new());
  27052. /* Empty data testing. */
  27053. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 1), 0);
  27054. ASN1_BIT_STRING_free(str);
  27055. str = NULL;
  27056. ExpectNotNull(str = ASN1_BIT_STRING_new());
  27057. /* Invalid parameter testing. */
  27058. ExpectIntEQ(ASN1_BIT_STRING_set_bit(NULL, 42, 1), 0);
  27059. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, -1, 1), 0);
  27060. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 2), 0);
  27061. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, -1), 0);
  27062. /* No bit string - bit is always 0. */
  27063. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, 42), 0);
  27064. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, -1), 0);
  27065. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  27066. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 0), 0);
  27067. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  27068. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  27069. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  27070. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  27071. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  27072. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  27073. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  27074. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 91, 0), 1);
  27075. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 91), 0);
  27076. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 89, 0), 1);
  27077. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 89), 0);
  27078. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 0), 1);
  27079. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 0);
  27080. ASN1_BIT_STRING_free(str);
  27081. ASN1_BIT_STRING_free(NULL);
  27082. #endif
  27083. return EXPECT_RESULT();
  27084. }
  27085. static int test_wolfSSL_ASN1_INTEGER(void)
  27086. {
  27087. EXPECT_DECLS;
  27088. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27089. ASN1_INTEGER* a = NULL;
  27090. ASN1_INTEGER* dup = NULL;
  27091. const unsigned char invalidLenDer[] = {
  27092. 0x02, 0x20, 0x00
  27093. };
  27094. const unsigned char longDer[] = {
  27095. 0x02, 0x20,
  27096. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27097. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27098. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27099. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
  27100. };
  27101. const unsigned char* p;
  27102. /* Invalid parameter testing. */
  27103. ASN1_INTEGER_free(NULL);
  27104. ExpectNull(wolfSSL_ASN1_INTEGER_dup(NULL));
  27105. ExpectNotNull(a = ASN1_INTEGER_new());
  27106. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  27107. ASN1_INTEGER_free(dup);
  27108. dup = NULL;
  27109. ASN1_INTEGER_free(a);
  27110. a = NULL;
  27111. p = longDer;
  27112. ExpectNull(d2i_ASN1_INTEGER(NULL, &p, sizeof(invalidLenDer)));
  27113. p = longDer;
  27114. ExpectNotNull(a = d2i_ASN1_INTEGER(NULL, &p, sizeof(longDer)));
  27115. ExpectPtrNE(p, longDer);
  27116. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  27117. ASN1_INTEGER_free(dup);
  27118. ASN1_INTEGER_free(a);
  27119. #endif
  27120. return EXPECT_RESULT();
  27121. }
  27122. static int test_wolfSSL_ASN1_INTEGER_cmp(void)
  27123. {
  27124. EXPECT_DECLS;
  27125. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27126. ASN1_INTEGER* a = NULL;
  27127. ASN1_INTEGER* b = NULL;
  27128. ExpectNotNull(a = ASN1_INTEGER_new());
  27129. ExpectNotNull(b = ASN1_INTEGER_new());
  27130. ExpectIntEQ(ASN1_INTEGER_set(a, 1), 1);
  27131. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  27132. /* Invalid parameter testing. */
  27133. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, NULL), -1);
  27134. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, NULL), -1);
  27135. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, b), -1);
  27136. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27137. ExpectIntEQ(ASN1_INTEGER_set(b, -1), 1);
  27138. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27139. ExpectIntEQ(ASN1_INTEGER_set(a, -2), 1);
  27140. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27141. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  27142. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27143. ExpectIntEQ(ASN1_INTEGER_set(a, 0x01), 1);
  27144. ExpectIntEQ(ASN1_INTEGER_set(b, 0x1000), 1);
  27145. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27146. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(b, a), 0);
  27147. ASN1_INTEGER_free(b);
  27148. ASN1_INTEGER_free(a);
  27149. #endif
  27150. return EXPECT_RESULT();
  27151. }
  27152. static int test_wolfSSL_ASN1_INTEGER_BN(void)
  27153. {
  27154. EXPECT_DECLS;
  27155. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27156. ASN1_INTEGER* ai = NULL;
  27157. ASN1_INTEGER* ai2 = NULL;
  27158. BIGNUM* bn = NULL;
  27159. BIGNUM* bn2 = NULL;
  27160. ExpectNotNull(ai = ASN1_INTEGER_new());
  27161. ExpectNotNull(bn2 = BN_new());
  27162. /* Invalid parameter testing. */
  27163. ExpectNull(bn = ASN1_INTEGER_to_BN(NULL, NULL));
  27164. ExpectNull(ai2 = BN_to_ASN1_INTEGER(NULL, NULL));
  27165. /* at the moment hard setting since no set function */
  27166. if (ai != NULL) {
  27167. ai->data[0] = 0xff; /* No DER encoding. */
  27168. ai->length = 1;
  27169. }
  27170. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27171. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27172. BN_free(bn);
  27173. bn = NULL;
  27174. #else
  27175. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  27176. #endif
  27177. if (ai != NULL) {
  27178. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27179. ai->data[1] = 0x04; /* bad length of integer */
  27180. ai->data[2] = 0x03;
  27181. ai->length = 3;
  27182. }
  27183. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27184. /* Interpreted as a number 0x020403. */
  27185. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27186. BN_free(bn);
  27187. bn = NULL;
  27188. #else
  27189. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  27190. #endif
  27191. if (ai != NULL) {
  27192. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27193. ai->data[1] = 0x01; /* length of integer */
  27194. ai->data[2] = 0x03;
  27195. ai->length = 3;
  27196. }
  27197. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27198. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, NULL));
  27199. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27200. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27201. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27202. if (ai != NULL) {
  27203. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27204. ai->data[1] = 0x02; /* length of integer */
  27205. ai->data[2] = 0x00; /* padding byte to ensure positive */
  27206. ai->data[3] = 0xff;
  27207. ai->length = 4;
  27208. }
  27209. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27210. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  27211. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27212. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27213. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27214. if (ai != NULL) {
  27215. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27216. ai->data[1] = 0x01; /* length of integer */
  27217. ai->data[2] = 0x00;
  27218. ai->length = 3;
  27219. }
  27220. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27221. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  27222. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27223. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27224. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27225. if (ai != NULL) {
  27226. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27227. ai->data[1] = 0x01; /* length of integer */
  27228. ai->data[2] = 0x01;
  27229. ai->length = 3;
  27230. ai->negative = 1;
  27231. }
  27232. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27233. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  27234. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27235. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27236. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27237. BN_free(bn2);
  27238. BN_free(bn);
  27239. ASN1_INTEGER_free(ai2);
  27240. ASN1_INTEGER_free(ai);
  27241. #endif
  27242. return EXPECT_RESULT();
  27243. }
  27244. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  27245. {
  27246. EXPECT_DECLS;
  27247. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27248. ASN1_INTEGER *a = NULL;
  27249. long val;
  27250. ExpectNotNull(a = ASN1_INTEGER_new());
  27251. /* Invalid parameter testing. */
  27252. ExpectIntEQ(ASN1_INTEGER_get(NULL), 0);
  27253. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27254. ExpectIntEQ(ASN1_INTEGER_get(a), 0);
  27255. #else
  27256. ExpectIntEQ(ASN1_INTEGER_get(a), -1);
  27257. #endif
  27258. ASN1_INTEGER_free(a);
  27259. a = NULL;
  27260. ExpectNotNull(a = ASN1_INTEGER_new());
  27261. val = 0;
  27262. ExpectIntEQ(ASN1_INTEGER_set(NULL, val), 0);
  27263. ASN1_INTEGER_free(a);
  27264. a = NULL;
  27265. /* 0 */
  27266. ExpectNotNull(a = ASN1_INTEGER_new());
  27267. val = 0;
  27268. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27269. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27270. ASN1_INTEGER_free(a);
  27271. a = NULL;
  27272. /* 40 */
  27273. ExpectNotNull(a = ASN1_INTEGER_new());
  27274. val = 40;
  27275. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27276. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27277. ASN1_INTEGER_free(a);
  27278. a = NULL;
  27279. /* -40 */
  27280. ExpectNotNull(a = ASN1_INTEGER_new());
  27281. val = -40;
  27282. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27283. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27284. ASN1_INTEGER_free(a);
  27285. a = NULL;
  27286. /* 128 */
  27287. ExpectNotNull(a = ASN1_INTEGER_new());
  27288. val = 128;
  27289. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27290. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27291. ASN1_INTEGER_free(a);
  27292. a = NULL;
  27293. /* -128 */
  27294. ExpectNotNull(a = ASN1_INTEGER_new());
  27295. val = -128;
  27296. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27297. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27298. ASN1_INTEGER_free(a);
  27299. a = NULL;
  27300. /* 200 */
  27301. ExpectNotNull(a = ASN1_INTEGER_new());
  27302. val = 200;
  27303. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27304. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27305. ASN1_INTEGER_free(a);
  27306. a = NULL;
  27307. /* int max (2147483647) */
  27308. ExpectNotNull(a = ASN1_INTEGER_new());
  27309. val = 2147483647;
  27310. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27311. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27312. ASN1_INTEGER_free(a);
  27313. a = NULL;
  27314. /* int min (-2147483648) */
  27315. ExpectNotNull(a = ASN1_INTEGER_new());
  27316. val = -2147483647 - 1;
  27317. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27318. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27319. ASN1_INTEGER_free(a);
  27320. a = NULL;
  27321. /* long max positive */
  27322. ExpectNotNull(a = ASN1_INTEGER_new());
  27323. val = (long)(((unsigned long)-1) >> 1);
  27324. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27325. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27326. ASN1_INTEGER_free(a);
  27327. #endif
  27328. return EXPECT_RESULT();
  27329. }
  27330. #if defined(OPENSSL_EXTRA)
  27331. typedef struct ASN1IntTestVector {
  27332. const byte* der;
  27333. const size_t derSz;
  27334. const long value;
  27335. } ASN1IntTestVector;
  27336. #endif
  27337. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  27338. {
  27339. EXPECT_DECLS;
  27340. #if defined(OPENSSL_EXTRA)
  27341. size_t i;
  27342. WOLFSSL_ASN1_INTEGER* a = NULL;
  27343. WOLFSSL_ASN1_INTEGER* b = NULL;
  27344. WOLFSSL_ASN1_INTEGER* c = NULL;
  27345. const byte* p = NULL;
  27346. byte* p2 = NULL;
  27347. byte* reEncoded = NULL;
  27348. int reEncodedSz = 0;
  27349. static const byte zeroDer[] = {
  27350. 0x02, 0x01, 0x00
  27351. };
  27352. static const byte oneDer[] = {
  27353. 0x02, 0x01, 0x01
  27354. };
  27355. static const byte negativeDer[] = {
  27356. 0x02, 0x03, 0xC1, 0x16, 0x0D
  27357. };
  27358. static const byte positiveDer[] = {
  27359. 0x02, 0x03, 0x01, 0x00, 0x01
  27360. };
  27361. static const byte primeDer[] = {
  27362. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  27363. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  27364. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  27365. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  27366. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  27367. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  27368. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  27369. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  27370. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  27371. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  27372. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  27373. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  27374. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  27375. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  27376. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  27377. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  27378. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  27379. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  27380. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  27381. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  27382. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  27383. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  27384. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  27385. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  27386. };
  27387. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  27388. static const ASN1IntTestVector testVectors[] = {
  27389. {zeroDer, sizeof(zeroDer), 0},
  27390. {oneDer, sizeof(oneDer), 1},
  27391. {negativeDer, sizeof(negativeDer), -4123123},
  27392. {positiveDer, sizeof(positiveDer), 65537},
  27393. {primeDer, sizeof(primeDer), 0}
  27394. };
  27395. static const size_t NUM_TEST_VECTORS =
  27396. sizeof(testVectors)/sizeof(testVectors[0]);
  27397. /* Check d2i error conditions */
  27398. /* NULL pointer to input. */
  27399. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  27400. ExpectNull(b);
  27401. /* NULL input. */
  27402. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  27403. ExpectNull(b);
  27404. /* 0 length. */
  27405. p = testVectors[0].der;
  27406. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  27407. ExpectNull(b);
  27408. /* Negative length. */
  27409. p = testVectors[0].der;
  27410. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  27411. ExpectNull(b);
  27412. /* Garbage DER input. */
  27413. p = garbageDer;
  27414. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  27415. ExpectNull(b);
  27416. /* Check i2d error conditions */
  27417. /* NULL input. */
  27418. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  27419. /* 0 length input data buffer (a->length == 0). */
  27420. ExpectNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  27421. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  27422. if (a != NULL)
  27423. a->data = NULL;
  27424. /* NULL input data buffer. */
  27425. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  27426. if (a != NULL) {
  27427. /* Reset a->data. */
  27428. a->data = a->intData;
  27429. }
  27430. /* Set a to valid value. */
  27431. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  27432. /* NULL output buffer. */
  27433. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  27434. wolfSSL_ASN1_INTEGER_free(a);
  27435. a = NULL;
  27436. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  27437. p = testVectors[i].der;
  27438. ExpectNotNull(a = wolfSSL_d2i_ASN1_INTEGER(&b, &p,
  27439. testVectors[i].derSz));
  27440. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27441. if (testVectors[i].derSz <= sizeof(long)) {
  27442. ExpectNotNull(c = wolfSSL_ASN1_INTEGER_new());
  27443. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value), 1);
  27444. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  27445. wolfSSL_ASN1_INTEGER_free(c);
  27446. c = NULL;
  27447. }
  27448. /* Convert to DER without a pre-allocated output buffer. */
  27449. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  27450. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  27451. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  27452. /* Convert to DER with a pre-allocated output buffer. In this case, the
  27453. * output buffer pointer should be incremented just past the end of the
  27454. * encoded data. */
  27455. p2 = reEncoded;
  27456. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &p2)), 0);
  27457. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  27458. ExpectPtrEq(reEncoded, p2 - reEncodedSz);
  27459. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  27460. XFREE(reEncoded, NULL, DYNAMIC_TYPE_ASN1);
  27461. reEncoded = NULL;
  27462. wolfSSL_ASN1_INTEGER_free(a);
  27463. a = NULL;
  27464. }
  27465. #endif /* OPENSSL_EXTRA */
  27466. return EXPECT_RESULT();
  27467. }
  27468. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  27469. {
  27470. EXPECT_DECLS;
  27471. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  27472. BIO* bio = NULL;
  27473. BIO* out = NULL;
  27474. BIO* fixed = NULL;
  27475. ASN1_INTEGER* ai = NULL;
  27476. char buf[] = "123456\n12345\n1123456789123456\\\n78901234567890 \r\n\n";
  27477. char tmp[1024];
  27478. int tmpSz;
  27479. const char expected1[] = "123456";
  27480. const char expected2[] = "112345678912345678901234567890";
  27481. char longStr[] = "123456781234567812345678123456781234567812345678\n"
  27482. "123456781234567812345678123456781234567812345678\\\n12345678\n";
  27483. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  27484. ExpectNotNull(ai = ASN1_INTEGER_new());
  27485. ExpectNotNull(bio = BIO_new_mem_buf(buf, -1));
  27486. /* Invalid parameter testing. */
  27487. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, -1), 0);
  27488. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, NULL, -1), 0);
  27489. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, NULL, -1), 0);
  27490. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, tmp, -1), 0);
  27491. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, 1024), 0);
  27492. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, tmp, 1024), 0);
  27493. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, tmp, 1024), 0);
  27494. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, NULL, 1024), 0);
  27495. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, -1), 0);
  27496. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, NULL), 0);
  27497. ExpectIntEQ(i2a_ASN1_INTEGER(bio, NULL), 0);
  27498. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, ai), 0);
  27499. /* No data to read from BIO. */
  27500. ExpectIntEQ(a2i_ASN1_INTEGER(out, ai, tmp, 1024), 0);
  27501. /* read first line */
  27502. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27503. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  27504. XMEMSET(tmp, 0, 1024);
  27505. tmpSz = BIO_read(out, tmp, 1024);
  27506. ExpectIntEQ(tmpSz, 6);
  27507. ExpectIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  27508. /* fail on second line (not % 2) */
  27509. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  27510. /* read 3rd long line */
  27511. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27512. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  27513. XMEMSET(tmp, 0, 1024);
  27514. tmpSz = BIO_read(out, tmp, 1024);
  27515. ExpectIntEQ(tmpSz, 30);
  27516. ExpectIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  27517. /* fail on empty line */
  27518. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  27519. BIO_free(bio);
  27520. bio = NULL;
  27521. /* Make long integer, requiring dynamic memory, even longer. */
  27522. ExpectNotNull(bio = BIO_new_mem_buf(longStr, -1));
  27523. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27524. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 48);
  27525. XMEMSET(tmp, 0, 1024);
  27526. tmpSz = BIO_read(out, tmp, 1024);
  27527. ExpectIntEQ(tmpSz, 48);
  27528. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27529. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 56);
  27530. XMEMSET(tmp, 0, 1024);
  27531. tmpSz = BIO_read(out, tmp, 1024);
  27532. ExpectIntEQ(tmpSz, 56);
  27533. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(ai, 1), 1);
  27534. BIO_free(bio);
  27535. BIO_free(out);
  27536. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  27537. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27538. /* Ensure there is 0 bytes available to write into. */
  27539. ExpectIntEQ(BIO_write(fixed, tmp, 1), 1);
  27540. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  27541. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27542. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  27543. BIO_free(fixed);
  27544. ASN1_INTEGER_free(ai);
  27545. #endif
  27546. return EXPECT_RESULT();
  27547. }
  27548. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  27549. {
  27550. EXPECT_DECLS;
  27551. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27552. ASN1_INTEGER *a = NULL;
  27553. unsigned char *pp = NULL,*tpp = NULL;
  27554. int ret = 0;
  27555. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  27556. /* Invalid parameter testing. */
  27557. /* Set pp to an invalid value. */
  27558. pp = NULL;
  27559. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, &pp), 0);
  27560. ExpectIntEQ(i2c_ASN1_INTEGER(a, &pp), 0);
  27561. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, NULL), 0);
  27562. /* 40 */
  27563. if (a != NULL) {
  27564. a->intData[0] = ASN_INTEGER;
  27565. a->intData[1] = 1;
  27566. a->intData[2] = 40;
  27567. }
  27568. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27569. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27570. DYNAMIC_TYPE_TMP_BUFFER));
  27571. tpp = pp;
  27572. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27573. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27574. tpp--;
  27575. ExpectIntEQ(*tpp, 40);
  27576. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27577. pp = NULL;
  27578. /* 128 */
  27579. if (a != NULL) {
  27580. a->intData[0] = ASN_INTEGER;
  27581. a->intData[1] = 1;
  27582. a->intData[2] = 128;
  27583. }
  27584. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27585. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27586. DYNAMIC_TYPE_TMP_BUFFER));
  27587. tpp = pp;
  27588. if (tpp != NULL) {
  27589. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27590. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27591. tpp--;
  27592. ExpectIntEQ(*(tpp--), 128);
  27593. ExpectIntEQ(*tpp, 0);
  27594. }
  27595. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27596. pp = NULL;
  27597. /* -40 */
  27598. if (a != NULL) {
  27599. a->intData[0] = ASN_INTEGER;
  27600. a->intData[1] = 1;
  27601. a->intData[2] = 40;
  27602. a->negative = 1;
  27603. }
  27604. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27605. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27606. DYNAMIC_TYPE_TMP_BUFFER));
  27607. tpp = pp;
  27608. if (tpp != NULL) {
  27609. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27610. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27611. tpp--;
  27612. ExpectIntEQ(*tpp, 216);
  27613. }
  27614. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27615. pp = NULL;
  27616. /* -128 */
  27617. if (a != NULL) {
  27618. a->intData[0] = ASN_INTEGER;
  27619. a->intData[1] = 1;
  27620. a->intData[2] = 128;
  27621. a->negative = 1;
  27622. }
  27623. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27624. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27625. DYNAMIC_TYPE_TMP_BUFFER));
  27626. tpp = pp;
  27627. if (tpp != NULL) {
  27628. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27629. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27630. tpp--;
  27631. ExpectIntEQ(*tpp, 128);
  27632. }
  27633. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27634. pp = NULL;
  27635. /* -200 */
  27636. if (a != NULL) {
  27637. a->intData[0] = ASN_INTEGER;
  27638. a->intData[1] = 1;
  27639. a->intData[2] = 200;
  27640. a->negative = 1;
  27641. }
  27642. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27643. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27644. DYNAMIC_TYPE_TMP_BUFFER));
  27645. tpp = pp;
  27646. if (tpp != NULL) {
  27647. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27648. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27649. tpp--;
  27650. ExpectIntEQ(*(tpp--), 56);
  27651. ExpectIntEQ(*tpp, 255);
  27652. }
  27653. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27654. pp = NULL;
  27655. /* Empty */
  27656. if (a != NULL) {
  27657. a->intData[0] = ASN_INTEGER;
  27658. a->intData[1] = 0;
  27659. a->negative = 0;
  27660. }
  27661. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27662. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27663. DYNAMIC_TYPE_TMP_BUFFER));
  27664. tpp = pp;
  27665. if (tpp != NULL) {
  27666. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27667. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27668. tpp--;
  27669. ExpectIntEQ(*tpp, 0);
  27670. }
  27671. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27672. pp = NULL;
  27673. /* 0 */
  27674. if (a != NULL) {
  27675. a->intData[0] = ASN_INTEGER;
  27676. a->intData[1] = 1;
  27677. a->intData[2] = 0;
  27678. a->negative = 1;
  27679. }
  27680. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27681. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27682. DYNAMIC_TYPE_TMP_BUFFER));
  27683. if (tpp != NULL) {
  27684. tpp = pp;
  27685. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27686. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27687. tpp--;
  27688. ExpectIntEQ(*tpp, 0);
  27689. }
  27690. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27691. pp = NULL;
  27692. /* 0x100 */
  27693. if (a != NULL) {
  27694. a->intData[0] = ASN_INTEGER;
  27695. a->intData[1] = 2;
  27696. a->intData[2] = 0x01;
  27697. a->intData[3] = 0x00;
  27698. a->negative = 0;
  27699. }
  27700. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27701. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27702. DYNAMIC_TYPE_TMP_BUFFER));
  27703. if (tpp != NULL) {
  27704. tpp = pp;
  27705. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27706. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27707. tpp -= 2;
  27708. ExpectIntEQ(tpp[0], 0x01);
  27709. ExpectIntEQ(tpp[1], 0x00);
  27710. }
  27711. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27712. pp = NULL;
  27713. /* -0x8000 => 0x8000 */
  27714. if (a != NULL) {
  27715. a->intData[0] = ASN_INTEGER;
  27716. a->intData[1] = 2;
  27717. a->intData[2] = 0x80;
  27718. a->intData[3] = 0x00;
  27719. a->negative = 1;
  27720. }
  27721. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27722. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27723. DYNAMIC_TYPE_TMP_BUFFER));
  27724. tpp = pp;
  27725. if (tpp != NULL) {
  27726. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27727. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27728. tpp -= 2;
  27729. ExpectIntEQ(tpp[0], 0x80);
  27730. ExpectIntEQ(tpp[1], 0x00);
  27731. }
  27732. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27733. pp = NULL;
  27734. /* -0x8001 => 0xFF7FFF */
  27735. if (a != NULL) {
  27736. a->intData[0] = ASN_INTEGER;
  27737. a->intData[1] = 2;
  27738. a->intData[2] = 0x80;
  27739. a->intData[3] = 0x01;
  27740. a->negative = 1;
  27741. }
  27742. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 3);
  27743. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27744. DYNAMIC_TYPE_TMP_BUFFER));
  27745. tpp = pp;
  27746. if (tpp != NULL) {
  27747. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27748. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 3);
  27749. tpp -= 3;
  27750. ExpectIntEQ(tpp[0], 0xFF);
  27751. ExpectIntEQ(tpp[1], 0x7F);
  27752. ExpectIntEQ(tpp[2], 0xFF);
  27753. }
  27754. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27755. wolfSSL_ASN1_INTEGER_free(a);
  27756. #endif /* OPENSSL_EXTRA && !NO_ASN */
  27757. return EXPECT_RESULT();
  27758. }
  27759. static int test_wolfSSL_ASN1_OBJECT(void)
  27760. {
  27761. EXPECT_DECLS;
  27762. #if defined(OPENSSL_EXTRA)
  27763. ASN1_OBJECT* a = NULL;
  27764. ASN1_OBJECT s;
  27765. const unsigned char der[] = { 0x06, 0x01, 0x00 };
  27766. /* Invalid parameter testing. */
  27767. ASN1_OBJECT_free(NULL);
  27768. ExpectNull(wolfSSL_ASN1_OBJECT_dup(NULL));
  27769. /* Test that a static ASN1_OBJECT can be freed. */
  27770. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  27771. ASN1_OBJECT_free(&s);
  27772. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  27773. ASN1_OBJECT_free(a);
  27774. a = NULL;
  27775. s.obj = der;
  27776. s.objSz = sizeof(der);
  27777. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  27778. ASN1_OBJECT_free(a);
  27779. ASN1_OBJECT_free(&s);
  27780. #endif /* OPENSSL_EXTRA */
  27781. return EXPECT_RESULT();
  27782. }
  27783. static int test_wolfSSL_ASN1_get_object(void)
  27784. {
  27785. EXPECT_DECLS;
  27786. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  27787. const unsigned char* derBuf = cliecc_cert_der_256;
  27788. const unsigned char* nullPtr = NULL;
  27789. const unsigned char objDerInvalidLen[] = { 0x30, 0x81 };
  27790. const unsigned char objDerBadLen[] = { 0x30, 0x04 };
  27791. const unsigned char objDerNotObj[] = { 0x02, 0x01, 0x00 };
  27792. const unsigned char objDerNoData[] = { 0x06, 0x00 };
  27793. const unsigned char* p;
  27794. unsigned char objDer[8];
  27795. unsigned char* der;
  27796. unsigned char* derPtr;
  27797. int len = sizeof_cliecc_cert_der_256;
  27798. long asnLen = 0;
  27799. int tag = 0;
  27800. int cls = 0;
  27801. ASN1_OBJECT* a = NULL;
  27802. ASN1_OBJECT s;
  27803. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  27804. /* Invalid encoding at length. */
  27805. p = objDerInvalidLen;
  27806. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  27807. 0x80);
  27808. p = objDerBadLen;
  27809. /* Error = 0x80, Constructed = 0x20 */
  27810. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  27811. 0x80 | 0x20);
  27812. /* Read a couple TLV triplets and make sure they match the expected values
  27813. */
  27814. /* SEQUENCE */
  27815. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  27816. ExpectIntEQ(asnLen, 861);
  27817. ExpectIntEQ(tag, 0x10);
  27818. ExpectIntEQ(cls, 0);
  27819. /* SEQUENCE */
  27820. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27821. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27822. ExpectIntEQ(asnLen, 772);
  27823. ExpectIntEQ(tag, 0x10);
  27824. ExpectIntEQ(cls, 0);
  27825. /* [0] */
  27826. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27827. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27828. ExpectIntEQ(asnLen, 3);
  27829. ExpectIntEQ(tag, 0);
  27830. ExpectIntEQ(cls, 0x80);
  27831. /* INTEGER */
  27832. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27833. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27834. ExpectIntEQ(asnLen, 1);
  27835. ExpectIntEQ(tag, 0x2);
  27836. ExpectIntEQ(cls, 0);
  27837. derBuf += asnLen;
  27838. /* INTEGER */
  27839. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27840. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27841. ExpectIntEQ(asnLen, 20);
  27842. ExpectIntEQ(tag, 0x2);
  27843. ExpectIntEQ(cls, 0);
  27844. derBuf += asnLen;
  27845. /* SEQUENCE */
  27846. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27847. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27848. ExpectIntEQ(asnLen, 10);
  27849. ExpectIntEQ(tag, 0x10);
  27850. ExpectIntEQ(cls, 0);
  27851. /* Found OBJECT_ID. */
  27852. /* Invalid parameter testing. */
  27853. ExpectIntEQ(ASN1_get_object(NULL, NULL, NULL, NULL, 0), 0x80);
  27854. ExpectIntEQ(ASN1_get_object(&nullPtr, NULL, NULL, NULL, 0), 0x80);
  27855. ExpectIntEQ(ASN1_get_object(NULL, &asnLen, &tag, &cls, len), 0x80);
  27856. ExpectIntEQ(ASN1_get_object(&nullPtr, &asnLen, &tag, &cls, len), 0x80);
  27857. ExpectIntEQ(ASN1_get_object(&derBuf, NULL, &tag, &cls, len), 0x80);
  27858. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, NULL, &cls, len), 0x80);
  27859. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, NULL, len), 0x80);
  27860. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, 0), 0x80);
  27861. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, -1), 0x80);
  27862. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, -1));
  27863. ExpectNull(d2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  27864. ExpectNull(d2i_ASN1_OBJECT(NULL, &derBuf, -1));
  27865. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, 0));
  27866. ExpectNull(d2i_ASN1_OBJECT(&a, NULL, len));
  27867. ExpectNull(d2i_ASN1_OBJECT(&a, &nullPtr, len));
  27868. ExpectNull(d2i_ASN1_OBJECT(&a, &derBuf, -1));
  27869. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, -1));
  27870. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  27871. ExpectNull(c2i_ASN1_OBJECT(NULL, &derBuf, -1));
  27872. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, 1));
  27873. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, 1));
  27874. /* Invalid encoding at length. */
  27875. p = objDerInvalidLen;
  27876. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerInvalidLen)));
  27877. p = objDerBadLen;
  27878. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerBadLen)));
  27879. p = objDerNotObj;
  27880. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNotObj)));
  27881. p = objDerNoData;
  27882. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNoData)));
  27883. /* Create an ASN OBJECT from content */
  27884. p = derBuf + 2;
  27885. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 8));
  27886. ASN1_OBJECT_free(a);
  27887. a = NULL;
  27888. /* Create an ASN OBJECT from DER */
  27889. ExpectNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  27890. /* Invalid parameter testing. */
  27891. ExpectIntEQ(i2d_ASN1_OBJECT(NULL, NULL), 0);
  27892. ExpectIntEQ(i2d_ASN1_OBJECT(&s, NULL), 0);
  27893. ExpectIntEQ(i2d_ASN1_OBJECT(a, NULL), 8);
  27894. der = NULL;
  27895. ExpectIntEQ(i2d_ASN1_OBJECT(a, &der), 8);
  27896. derPtr = objDer;
  27897. ExpectIntEQ(i2d_ASN1_OBJECT(a, &derPtr), 8);
  27898. ExpectPtrNE(derPtr, objDer);
  27899. ExpectIntEQ(XMEMCMP(der, objDer, 8), 0);
  27900. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  27901. ASN1_OBJECT_free(a);
  27902. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  27903. return EXPECT_RESULT();
  27904. }
  27905. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  27906. {
  27907. EXPECT_DECLS;
  27908. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  27909. ASN1_OBJECT* obj = NULL;
  27910. ASN1_OBJECT* a = NULL;
  27911. BIO *bio = NULL;
  27912. const unsigned char notObjDer[] = { 0x04, 0x01, 0xff };
  27913. const unsigned char badLenDer[] = { 0x06, 0x04, 0x01 };
  27914. const unsigned char goodDer[] = { 0x06, 0x01, 0x01 };
  27915. const unsigned char* p;
  27916. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  27917. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  27918. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  27919. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  27920. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  27921. /* No DER encoding in ASN1_OBJECT. */
  27922. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_new());
  27923. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  27924. ASN1_OBJECT_free(a);
  27925. a = NULL;
  27926. /* DER encoding - not OBJECT_ID */
  27927. p = notObjDer;
  27928. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  27929. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  27930. ASN1_OBJECT_free(a);
  27931. a = NULL;
  27932. /* Bad length encoding. */
  27933. p = badLenDer;
  27934. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  27935. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  27936. ASN1_OBJECT_free(a);
  27937. a = NULL;
  27938. /* Good encoding - but unknown. */
  27939. p = goodDer;
  27940. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  27941. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  27942. ASN1_OBJECT_free(a);
  27943. BIO_free(bio);
  27944. ASN1_OBJECT_free(obj);
  27945. #endif
  27946. return EXPECT_RESULT();
  27947. }
  27948. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  27949. {
  27950. EXPECT_DECLS;
  27951. #if defined(OPENSSL_EXTRA) && \
  27952. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  27953. char buf[50] = {0};
  27954. ASN1_OBJECT* obj;
  27955. const char* oid = "2.5.29.19";
  27956. const char* ln = "X509v3 Basic Constraints";
  27957. obj = NULL;
  27958. ExpectIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), 0);
  27959. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), 0);
  27960. ExpectIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), 0);
  27961. ExpectNotNull(obj = OBJ_txt2obj(oid, 0));
  27962. XMEMSET(buf, 0, sizeof(buf));
  27963. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  27964. ExpectIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  27965. ASN1_OBJECT_free(obj);
  27966. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  27967. return EXPECT_RESULT();
  27968. }
  27969. static int test_wolfSSL_sk_ASN1_OBJECT(void)
  27970. {
  27971. EXPECT_DECLS;
  27972. #if !defined(NO_ASN) && (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  27973. WOLFSSL_STACK* sk = NULL;
  27974. WOLFSSL_ASN1_OBJECT* obj;
  27975. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  27976. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  27977. wolfSSL_sk_ASN1_OBJECT_free(sk);
  27978. sk = NULL;
  27979. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  27980. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, NULL), 0);
  27981. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, NULL), 0);
  27982. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, obj), 0);
  27983. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  27984. wolfSSL_sk_ASN1_OBJECT_pop_free(sk, NULL);
  27985. sk = NULL;
  27986. /* obj freed in pop_free call. */
  27987. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  27988. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  27989. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  27990. ExpectPtrEq(obj, wolfSSL_sk_ASN1_OBJECT_pop(sk));
  27991. wolfSSL_sk_ASN1_OBJECT_free(sk);
  27992. wolfSSL_ASN1_OBJECT_free(obj);
  27993. #endif /* !NO_ASN && (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) */
  27994. return EXPECT_RESULT();
  27995. }
  27996. static int test_wolfSSL_ASN1_STRING(void)
  27997. {
  27998. EXPECT_DECLS;
  27999. #if defined(OPENSSL_EXTRA)
  28000. ASN1_STRING* str = NULL;
  28001. ASN1_STRING* c = NULL;
  28002. const char data[] = "hello wolfSSL";
  28003. const char data2[] = "Same len data";
  28004. const char longData[] =
  28005. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  28006. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28007. ASN1_STRING_free(str);
  28008. str = NULL;
  28009. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28010. ExpectIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  28011. ExpectIntEQ(ASN1_STRING_type(NULL), 0);
  28012. /* Check setting to NULL works. */
  28013. ExpectIntEQ(ASN1_STRING_set(str, NULL, 0), 1);
  28014. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  28015. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  28016. ExpectIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  28017. ExpectIntEQ(ASN1_STRING_set(NULL, NULL, 0), 0);
  28018. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, NULL), 0);
  28019. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(str, NULL), 0);
  28020. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, str), 0);
  28021. ExpectNull(wolfSSL_ASN1_STRING_dup(NULL));
  28022. ExpectNotNull(c = wolfSSL_ASN1_STRING_dup(str));
  28023. ExpectIntEQ(ASN1_STRING_cmp(NULL, NULL), -1);
  28024. ExpectIntEQ(ASN1_STRING_cmp(str, NULL), -1);
  28025. ExpectIntEQ(ASN1_STRING_cmp(NULL, c), -1);
  28026. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  28027. ExpectIntEQ(ASN1_STRING_set(c, (const void*)data2, -1), 1);
  28028. ExpectIntGT(ASN1_STRING_cmp(str, c), 0);
  28029. ExpectIntEQ(ASN1_STRING_set(str, (const void*)longData, -1), 1);
  28030. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(c, str), 1);
  28031. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  28032. /* Check setting back to smaller size frees dynamic data. */
  28033. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  28034. ExpectIntLT(ASN1_STRING_cmp(str, c), 0);
  28035. ExpectIntGT(ASN1_STRING_cmp(c, str), 0);
  28036. ExpectNull(ASN1_STRING_get0_data(NULL));
  28037. ExpectNotNull(ASN1_STRING_get0_data(str));
  28038. ExpectNull(ASN1_STRING_data(NULL));
  28039. ExpectNotNull(ASN1_STRING_data(str));
  28040. ExpectIntEQ(ASN1_STRING_length(NULL), 0);
  28041. ExpectIntGT(ASN1_STRING_length(str), 0);
  28042. ASN1_STRING_free(c);
  28043. ASN1_STRING_free(str);
  28044. ASN1_STRING_free(NULL);
  28045. #ifndef NO_WOLFSSL_STUB
  28046. ExpectNull(d2i_DISPLAYTEXT(NULL, NULL, 0));
  28047. #endif
  28048. #endif
  28049. return EXPECT_RESULT();
  28050. }
  28051. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  28052. {
  28053. EXPECT_DECLS;
  28054. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_RSA) && \
  28055. !defined(NO_FILESYSTEM)
  28056. WOLFSSL_X509* x509 = NULL;
  28057. WOLFSSL_X509_NAME* subject = NULL;
  28058. WOLFSSL_X509_NAME_ENTRY* e = NULL;
  28059. WOLFSSL_ASN1_STRING* a = NULL;
  28060. FILE* file = XBADFILE;
  28061. int idx = 0;
  28062. char targetOutput[16] = "www.wolfssl.com";
  28063. unsigned char* actual_output = NULL;
  28064. int len = 0;
  28065. ExpectNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  28066. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  28067. if (file != NULL)
  28068. fclose(file);
  28069. /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
  28070. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  28071. ExpectIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  28072. NID_commonName, -1)), 5);
  28073. ExpectNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  28074. ExpectNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  28075. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  28076. ExpectIntEQ(strncmp((const char*)actual_output, targetOutput, len), 0);
  28077. a = NULL;
  28078. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
  28079. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)), -1);
  28080. /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
  28081. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)), -1);
  28082. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
  28083. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)), -1);
  28084. wolfSSL_X509_free(x509);
  28085. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28086. ExpectNotNull(a = ASN1_STRING_new());
  28087. ExpectIntEQ(wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a), -1);
  28088. ASN1_STRING_free(a);
  28089. #endif
  28090. return EXPECT_RESULT();
  28091. }
  28092. static int test_wolfSSL_i2s_ASN1_STRING(void)
  28093. {
  28094. EXPECT_DECLS;
  28095. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  28096. WOLFSSL_ASN1_STRING* str = NULL;
  28097. const char* data = "test_wolfSSL_i2s_ASN1_STRING";
  28098. char* ret = NULL;
  28099. ExpectNotNull(str = ASN1_STRING_new());
  28100. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, NULL));
  28101. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28102. ret = NULL;
  28103. /* No data. */
  28104. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  28105. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28106. ret = NULL;
  28107. ExpectIntEQ(ASN1_STRING_set(str, data, 0), 1);
  28108. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  28109. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28110. ret = NULL;
  28111. ExpectIntEQ(ASN1_STRING_set(str, data, -1), 1);
  28112. /* No type. */
  28113. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  28114. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28115. ASN1_STRING_free(str);
  28116. #endif
  28117. return EXPECT_RESULT();
  28118. }
  28119. static int test_wolfSSL_ASN1_STRING_canon(void)
  28120. {
  28121. EXPECT_DECLS;
  28122. #if defined(WOLFSSL_TEST_STATIC_BUILD)
  28123. #if !defined(NO_CERTS) && (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
  28124. defined(OPENSSL_EXTRA_X509_SMALL))
  28125. WOLFSSL_ASN1_STRING* orig = NULL;
  28126. WOLFSSL_ASN1_STRING* canon = NULL;
  28127. const char* data = "test_wolfSSL_ASN1_STRING_canon";
  28128. const char* whitespaceOnly = "\t\r\n";
  28129. const char* modData = " \x01\f\t\x02\r\n\v\xff\nTt \n";
  28130. const char* canonData = "\x01 \x02 \xff tt";
  28131. const char longData[] =
  28132. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  28133. ExpectNotNull(orig = ASN1_STRING_new());
  28134. ExpectNotNull(canon = ASN1_STRING_new());
  28135. /* Invalid parameter testing. */
  28136. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, NULL), BAD_FUNC_ARG);
  28137. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, NULL), BAD_FUNC_ARG);
  28138. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, orig), BAD_FUNC_ARG);
  28139. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28140. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28141. ExpectIntEQ(ASN1_STRING_set(orig, longData, (int)XSTRLEN(data)), 1);
  28142. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28143. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28144. ExpectIntEQ(ASN1_STRING_set(orig, data, (int)XSTRLEN(data)), 1);
  28145. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28146. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28147. ASN1_STRING_free(orig);
  28148. orig = NULL;
  28149. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  28150. ExpectIntEQ(ASN1_STRING_set(orig, modData, 15), 1);
  28151. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28152. ExpectIntEQ(ASN1_STRING_set(orig, canonData, 8), 1);
  28153. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28154. ASN1_STRING_free(orig);
  28155. orig = NULL;
  28156. ExpectNotNull(orig = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  28157. ExpectIntEQ(ASN1_STRING_set(orig, whitespaceOnly, 3), 1);
  28158. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28159. ASN1_STRING_free(orig);
  28160. orig = NULL;
  28161. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  28162. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28163. ASN1_STRING_free(orig);
  28164. ASN1_STRING_free(canon);
  28165. #endif
  28166. #endif
  28167. return EXPECT_RESULT();
  28168. }
  28169. static int test_wolfSSL_ASN1_STRING_print(void)
  28170. {
  28171. EXPECT_DECLS;
  28172. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS) && \
  28173. !defined(NO_BIO)
  28174. ASN1_STRING* asnStr = NULL;
  28175. const char HELLO_DATA[]= \
  28176. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  28177. #define MAX_UNPRINTABLE_CHAR 32
  28178. #define MAX_BUF 255
  28179. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  28180. unsigned char expected[sizeof(unprintableData)+1];
  28181. unsigned char rbuf[MAX_BUF];
  28182. BIO *bio = NULL;
  28183. int p_len;
  28184. int i;
  28185. /* setup */
  28186. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  28187. unprintableData[i] = HELLO_DATA[i];
  28188. expected[i] = HELLO_DATA[i];
  28189. }
  28190. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  28191. unprintableData[sizeof(HELLO_DATA)+i] = i;
  28192. if (i == (int)'\n' || i == (int)'\r')
  28193. expected[sizeof(HELLO_DATA)+i] = i;
  28194. else
  28195. expected[sizeof(HELLO_DATA)+i] = '.';
  28196. }
  28197. unprintableData[sizeof(unprintableData)-1] = '\0';
  28198. expected[sizeof(expected)-1] = '\0';
  28199. XMEMSET(rbuf, 0, MAX_BUF);
  28200. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28201. ExpectIntEQ(BIO_set_write_buf_size(bio, MAX_BUF), 0);
  28202. ExpectNotNull(asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28203. ExpectIntEQ(ASN1_STRING_set(asnStr,(const void*)unprintableData,
  28204. (int)sizeof(unprintableData)), 1);
  28205. /* test */
  28206. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, NULL), 0);
  28207. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, NULL), 0);
  28208. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, asnStr), 0);
  28209. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print(bio, asnStr), 46);
  28210. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 46), 46);
  28211. ExpectStrEQ((char*)rbuf, (const char*)expected);
  28212. BIO_free(bio);
  28213. bio = NULL;
  28214. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28215. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28216. /* Ensure there is 0 bytes available to write into. */
  28217. ExpectIntEQ(BIO_write(bio, rbuf, 1), 1);
  28218. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28219. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28220. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28221. ExpectIntEQ(BIO_set_write_buf_size(bio, 45), 1);
  28222. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28223. BIO_free(bio);
  28224. ASN1_STRING_free(asnStr);
  28225. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS && !NO_BIO */
  28226. return EXPECT_RESULT();
  28227. }
  28228. static int test_wolfSSL_ASN1_STRING_print_ex(void)
  28229. {
  28230. EXPECT_DECLS;
  28231. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  28232. ASN1_STRING* asn_str = NULL;
  28233. const char data[] = "Hello wolfSSL!";
  28234. ASN1_STRING* esc_str = NULL;
  28235. const char esc_data[] = "a+;<>";
  28236. ASN1_STRING* neg_int = NULL;
  28237. const char neg_int_data[] = "\xff";
  28238. ASN1_STRING* neg_enum = NULL;
  28239. const char neg_enum_data[] = "\xff";
  28240. BIO *bio = NULL;
  28241. BIO *fixed = NULL;
  28242. unsigned long flags;
  28243. int p_len;
  28244. unsigned char rbuf[255];
  28245. /* setup */
  28246. XMEMSET(rbuf, 0, 255);
  28247. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28248. ExpectIntEQ(BIO_set_write_buf_size(bio, 255), 0);
  28249. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28250. ExpectNotNull(asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28251. ExpectIntEQ(ASN1_STRING_set(asn_str, (const void*)data, sizeof(data)), 1);
  28252. ExpectNotNull(esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28253. ExpectIntEQ(ASN1_STRING_set(esc_str, (const void*)esc_data,
  28254. sizeof(esc_data)), 1);
  28255. ExpectNotNull(neg_int = ASN1_STRING_type_new(V_ASN1_NEG_INTEGER));
  28256. ExpectIntEQ(ASN1_STRING_set(neg_int, (const void*)neg_int_data,
  28257. sizeof(neg_int_data) - 1), 1);
  28258. ExpectNotNull(neg_enum = ASN1_STRING_type_new(V_ASN1_NEG_ENUMERATED));
  28259. ExpectIntEQ(ASN1_STRING_set(neg_enum, (const void*)neg_enum_data,
  28260. sizeof(neg_enum_data) - 1), 1);
  28261. /* Invalid parameter testing. */
  28262. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, NULL, 0), 0);
  28263. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(bio, NULL, 0), 0);
  28264. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, asn_str, 0), 0);
  28265. /* no flags */
  28266. XMEMSET(rbuf, 0, 255);
  28267. flags = 0;
  28268. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 15);
  28269. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 15), 15);
  28270. ExpectStrEQ((char*)rbuf, "Hello wolfSSL!");
  28271. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28272. /* Ensure there is 0 bytes available to write into. */
  28273. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28274. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28275. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28276. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28277. ExpectIntEQ(BIO_set_write_buf_size(fixed, 14), 1);
  28278. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28279. /* RFC2253 Escape */
  28280. XMEMSET(rbuf, 0, 255);
  28281. flags = ASN1_STRFLGS_ESC_2253;
  28282. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags), 9);
  28283. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 9), 9);
  28284. ExpectStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  28285. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28286. /* Ensure there is 0 bytes available to write into. */
  28287. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28288. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28289. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28290. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28291. ExpectIntEQ(BIO_set_write_buf_size(fixed, 8), 1);
  28292. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28293. /* Show type */
  28294. XMEMSET(rbuf, 0, 255);
  28295. flags = ASN1_STRFLGS_SHOW_TYPE;
  28296. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 28);
  28297. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 28), 28);
  28298. ExpectStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  28299. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28300. /* Ensure there is 0 bytes available to write into. */
  28301. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28302. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28303. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28304. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28305. ExpectIntEQ(BIO_set_write_buf_size(fixed, 12), 1);
  28306. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28307. ExpectIntEQ(BIO_set_write_buf_size(fixed, 27), 1);
  28308. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28309. /* Dump All */
  28310. XMEMSET(rbuf, 0, 255);
  28311. flags = ASN1_STRFLGS_DUMP_ALL;
  28312. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 31);
  28313. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 31), 31);
  28314. ExpectStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  28315. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28316. /* Ensure there is 0 bytes available to write into. */
  28317. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28318. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28319. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28320. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28321. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  28322. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28323. /* Dump Der */
  28324. XMEMSET(rbuf, 0, 255);
  28325. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  28326. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 35);
  28327. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 35), 35);
  28328. ExpectStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  28329. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28330. /* Ensure there is 0 bytes available to write into. */
  28331. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28332. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28333. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28334. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28335. ExpectIntEQ(BIO_set_write_buf_size(fixed, 2), 1);
  28336. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28337. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  28338. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28339. /* Dump All + Show type */
  28340. XMEMSET(rbuf, 0, 255);
  28341. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28342. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 44);
  28343. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 44), 44);
  28344. ExpectStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  28345. /* Dump All + Show type - Negative Integer. */
  28346. XMEMSET(rbuf, 0, 255);
  28347. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28348. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_int, flags), 11);
  28349. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 11), 11);
  28350. ExpectStrEQ((char*)rbuf, "INTEGER:#FF");
  28351. /* Dump All + Show type - Negative Enumerated. */
  28352. XMEMSET(rbuf, 0, 255);
  28353. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28354. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_enum, flags), 14);
  28355. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 14), 14);
  28356. ExpectStrEQ((char*)rbuf, "ENUMERATED:#FF");
  28357. BIO_free(fixed);
  28358. BIO_free(bio);
  28359. ASN1_STRING_free(asn_str);
  28360. ASN1_STRING_free(esc_str);
  28361. ASN1_STRING_free(neg_int);
  28362. ASN1_STRING_free(neg_enum);
  28363. ExpectStrEQ(wolfSSL_ASN1_tag2str(-1), "(unknown)");
  28364. ExpectStrEQ(wolfSSL_ASN1_tag2str(31), "(unknown)");
  28365. #endif
  28366. return EXPECT_RESULT();
  28367. }
  28368. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  28369. {
  28370. EXPECT_DECLS;
  28371. #if defined(OPENSSL_ALL) && !defined(NO_ASN)
  28372. ASN1_STRING* asn1str_test = NULL;
  28373. ASN1_STRING* asn1str_answer = NULL;
  28374. /* Each character is encoded using 4 bytes */
  28375. char input[] = {
  28376. 0, 0, 0, 'T',
  28377. 0, 0, 0, 'e',
  28378. 0, 0, 0, 's',
  28379. 0, 0, 0, 't',
  28380. };
  28381. char output[] = "Test";
  28382. char badInput[] = {
  28383. 1, 0, 0, 'T',
  28384. 0, 1, 0, 'e',
  28385. 0, 0, 1, 's',
  28386. };
  28387. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(NULL), 0);
  28388. /* Test wrong type. */
  28389. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28390. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28391. ASN1_STRING_free(asn1str_test);
  28392. asn1str_test = NULL;
  28393. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  28394. /* Test bad length. */
  28395. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input) - 1), 1);
  28396. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28397. /* Test bad input. */
  28398. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 0, 4), 1);
  28399. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28400. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 4, 4), 1);
  28401. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28402. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 8, 4), 1);
  28403. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28404. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  28405. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  28406. ExpectNotNull(
  28407. asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  28408. ExpectIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  28409. ExpectIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  28410. ASN1_STRING_free(asn1str_test);
  28411. ASN1_STRING_free(asn1str_answer);
  28412. #endif /* OPENSSL_ALL && !NO_ASN */
  28413. return EXPECT_RESULT();
  28414. }
  28415. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  28416. {
  28417. EXPECT_DECLS;
  28418. #if defined(OPENSSL_EXTRA)
  28419. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime = NULL;
  28420. unsigned char nullstr[32];
  28421. XMEMSET(nullstr, 0, 32);
  28422. ExpectNotNull(asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  28423. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL, DYNAMIC_TYPE_TMP_BUFFER));
  28424. if (asn1_gtime != NULL) {
  28425. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  28426. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  28427. ExpectIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  28428. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28429. }
  28430. wolfSSL_ASN1_GENERALIZEDTIME_free(NULL);
  28431. #endif /* OPENSSL_EXTRA */
  28432. return EXPECT_RESULT();
  28433. }
  28434. static int test_wolfSSL_ASN1_GENERALIZEDTIME_print(void)
  28435. {
  28436. EXPECT_DECLS;
  28437. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  28438. WOLFSSL_ASN1_GENERALIZEDTIME gtime;
  28439. BIO* bio = NULL;
  28440. unsigned char buf[24];
  28441. int i;
  28442. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28443. BIO_set_write_buf_size(bio, 24);
  28444. XMEMSET(&gtime, 0, sizeof(WOLFSSL_ASN1_GENERALIZEDTIME));
  28445. XMEMCPY(gtime.data, "20180504123500Z", ASN_GENERALIZED_TIME_SIZE);
  28446. gtime.length = ASN_GENERALIZED_TIME_SIZE;
  28447. /* Type not set. */
  28448. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  28449. gtime.type = V_ASN1_GENERALIZEDTIME;
  28450. /* Invalid parameters testing. */
  28451. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, NULL), BAD_FUNC_ARG);
  28452. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, NULL), BAD_FUNC_ARG);
  28453. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, &gtime), BAD_FUNC_ARG);
  28454. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 1);
  28455. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 20);
  28456. ExpectIntEQ(XMEMCMP(buf, "May 04 12:35:00 2018", 20), 0);
  28457. BIO_free(bio);
  28458. bio = NULL;
  28459. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28460. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28461. /* Ensure there is 0 bytes available to write into. */
  28462. ExpectIntEQ(BIO_write(bio, buf, 1), 1);
  28463. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  28464. for (i = 1; i < 20; i++) {
  28465. ExpectIntEQ(BIO_set_write_buf_size(bio, i), 1);
  28466. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  28467. }
  28468. BIO_free(bio);
  28469. wolfSSL_ASN1_GENERALIZEDTIME_free(&gtime);
  28470. #endif /* OPENSSL_EXTRA */
  28471. return EXPECT_RESULT();
  28472. }
  28473. static int test_wolfSSL_ASN1_TIME(void)
  28474. {
  28475. EXPECT_DECLS;
  28476. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28477. WOLFSSL_ASN1_TIME* asn_time = NULL;
  28478. unsigned char *data;
  28479. ExpectNotNull(asn_time = ASN1_TIME_new());
  28480. #ifndef NO_WOLFSSL_STUB
  28481. ExpectNotNull(ASN1_TIME_set(asn_time, 1));
  28482. #endif
  28483. ExpectIntEQ(ASN1_TIME_set_string(NULL, NULL), 0);
  28484. ExpectIntEQ(ASN1_TIME_set_string(asn_time, NULL), 0);
  28485. ExpectIntEQ(ASN1_TIME_set_string(NULL,
  28486. "String longer than CTC_DATA_SIZE that is 32 bytes"), 0);
  28487. ExpectIntEQ(ASN1_TIME_set_string(NULL, "101219181011Z"), 1);
  28488. ExpectIntEQ(ASN1_TIME_set_string(asn_time, "101219181011Z"), 1);
  28489. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(NULL), 0);
  28490. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(asn_time), ASN_UTC_TIME_SIZE - 1);
  28491. ExpectNull(wolfSSL_ASN1_TIME_get_data(NULL));
  28492. ExpectNotNull(data = wolfSSL_ASN1_TIME_get_data(asn_time));
  28493. ExpectIntEQ(XMEMCMP(data, "101219181011Z", 14), 0);
  28494. ExpectIntEQ(ASN1_TIME_check(NULL), 0);
  28495. ExpectIntEQ(ASN1_TIME_check(asn_time), 1);
  28496. ASN1_TIME_free(asn_time);
  28497. ASN1_TIME_free(NULL);
  28498. #endif
  28499. return EXPECT_RESULT();
  28500. }
  28501. static int test_wolfSSL_ASN1_TIME_to_string(void)
  28502. {
  28503. EXPECT_DECLS;
  28504. #ifndef NO_ASN_TIME
  28505. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28506. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  28507. WOLFSSL_ASN1_TIME* t = NULL;
  28508. char buf[ASN_GENERALIZED_TIME_SIZE];
  28509. ExpectNotNull((t = ASN1_TIME_new()));
  28510. ExpectIntEQ(ASN1_TIME_set_string(t, "030222211515Z"), 1);
  28511. /* Invalid parameter testing. */
  28512. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 4));
  28513. ExpectNull(ASN1_TIME_to_string(t, NULL, 4));
  28514. ExpectNull(ASN1_TIME_to_string(NULL, buf, 4));
  28515. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 5));
  28516. ExpectNull(ASN1_TIME_to_string(NULL, buf, 5));
  28517. ExpectNull(ASN1_TIME_to_string(t, NULL, 5));
  28518. ExpectNull(ASN1_TIME_to_string(t, buf, 4));
  28519. /* Buffer needs to be longer than minimum of 5 characters. */
  28520. ExpectNull(ASN1_TIME_to_string(t, buf, 5));
  28521. ASN1_TIME_free(t);
  28522. #endif
  28523. #endif /* NO_ASN_TIME */
  28524. return EXPECT_RESULT();
  28525. }
  28526. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  28527. {
  28528. EXPECT_DECLS;
  28529. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28530. ASN1_TIME* fromTime = NULL;
  28531. ASN1_TIME* closeToTime = NULL;
  28532. ASN1_TIME* toTime = NULL;
  28533. ASN1_TIME* invalidTime = NULL;
  28534. int daysDiff = 0;
  28535. int secsDiff = 0;
  28536. ExpectNotNull((fromTime = ASN1_TIME_new()));
  28537. /* Feb 22, 2003, 21:15:15 */
  28538. ExpectIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), 1);
  28539. ExpectNotNull((closeToTime = ASN1_TIME_new()));
  28540. /* Feb 22, 2003, 21:16:15 */
  28541. ExpectIntEQ(ASN1_TIME_set_string(closeToTime, "030222211615Z"), 1);
  28542. ExpectNotNull((toTime = ASN1_TIME_new()));
  28543. /* Dec 19, 2010, 18:10:11 */
  28544. ExpectIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), 1);
  28545. ExpectNotNull((invalidTime = ASN1_TIME_new()));
  28546. /* Dec 19, 2010, 18:10:11 but 'U' instead of 'Z' which is invalid. */
  28547. ExpectIntEQ(ASN1_TIME_set_string(invalidTime, "102519181011U"), 1);
  28548. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, invalidTime), 0);
  28549. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, invalidTime, toTime), 0);
  28550. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28551. /* Error conditions. */
  28552. ExpectIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 0);
  28553. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime), 0);
  28554. /* If both times are NULL, difference is 0. */
  28555. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL), 1);
  28556. ExpectIntEQ(daysDiff, 0);
  28557. ExpectIntEQ(secsDiff, 0);
  28558. /* If one time is NULL, it defaults to the current time. */
  28559. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime), 1);
  28560. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL), 1);
  28561. /* Normal operation. Both times non-NULL. */
  28562. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28563. ExpectIntEQ(daysDiff, 2856);
  28564. ExpectIntEQ(secsDiff, 75296);
  28565. /* Swapping the times should return negative values. */
  28566. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime), 1);
  28567. ExpectIntEQ(daysDiff, -2856);
  28568. ExpectIntEQ(secsDiff, -75296);
  28569. /* Compare with invalid time string. */
  28570. ExpectIntEQ(ASN1_TIME_compare(fromTime, invalidTime), -2);
  28571. ExpectIntEQ(ASN1_TIME_compare(invalidTime, toTime), -2);
  28572. /* Compare with days difference of 0. */
  28573. ExpectIntEQ(ASN1_TIME_compare(fromTime, closeToTime), -1);
  28574. ExpectIntEQ(ASN1_TIME_compare(closeToTime, fromTime), 1);
  28575. /* Days and seconds differences not 0. */
  28576. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  28577. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  28578. /* Same time. */
  28579. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  28580. /* Compare regression test: No seconds difference, just difference in days.
  28581. */
  28582. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  28583. ASN1_TIME_set_string(toTime, "19800101000000Z");
  28584. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  28585. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  28586. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  28587. /* Edge case with Unix epoch. */
  28588. ExpectNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  28589. ExpectNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  28590. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28591. ExpectIntEQ(daysDiff, 3652);
  28592. ExpectIntEQ(secsDiff, 0);
  28593. /* Edge case with year > 2038 (year 2038 problem). */
  28594. ExpectNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  28595. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28596. ExpectIntEQ(daysDiff, 2932896);
  28597. ExpectIntEQ(secsDiff, 86399);
  28598. ASN1_TIME_free(fromTime);
  28599. ASN1_TIME_free(closeToTime);
  28600. ASN1_TIME_free(toTime);
  28601. ASN1_TIME_free(invalidTime);
  28602. #endif
  28603. return EXPECT_RESULT();
  28604. }
  28605. static int test_wolfSSL_ASN1_TIME_adj(void)
  28606. {
  28607. EXPECT_DECLS;
  28608. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  28609. !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  28610. const int year = 365*24*60*60;
  28611. const int day = 24*60*60;
  28612. const int hour = 60*60;
  28613. const int mini = 60;
  28614. const byte asn_utc_time = ASN_UTC_TIME;
  28615. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  28616. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  28617. #endif
  28618. WOLFSSL_ASN1_TIME* asn_time = NULL;
  28619. WOLFSSL_ASN1_TIME* s = NULL;
  28620. int offset_day;
  28621. long offset_sec;
  28622. char date_str[CTC_DATE_SIZE + 1];
  28623. time_t t;
  28624. ExpectNotNull(s = wolfSSL_ASN1_TIME_new());
  28625. /* UTC notation test */
  28626. /* 2000/2/15 20:30:00 */
  28627. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  28628. offset_day = 7;
  28629. offset_sec = 45 * mini;
  28630. /* offset_sec = -45 * min;*/
  28631. ExpectNotNull(asn_time =
  28632. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  28633. ExpectTrue(asn_time->type == asn_utc_time);
  28634. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28635. CTC_DATE_SIZE));
  28636. date_str[CTC_DATE_SIZE] = '\0';
  28637. ExpectIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  28638. /* negative offset */
  28639. offset_sec = -45 * mini;
  28640. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  28641. ExpectNotNull(asn_time);
  28642. ExpectTrue(asn_time->type == asn_utc_time);
  28643. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28644. CTC_DATE_SIZE));
  28645. date_str[CTC_DATE_SIZE] = '\0';
  28646. ExpectIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  28647. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  28648. s = NULL;
  28649. XMEMSET(date_str, 0, sizeof(date_str));
  28650. /* Generalized time will overflow time_t if not long */
  28651. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  28652. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  28653. DYNAMIC_TYPE_OPENSSL);
  28654. /* GeneralizedTime notation test */
  28655. /* 2055/03/01 09:00:00 */
  28656. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  28657. offset_day = 12;
  28658. offset_sec = 10 * mini;
  28659. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  28660. offset_sec));
  28661. ExpectTrue(asn_time->type == asn_gen_time);
  28662. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28663. CTC_DATE_SIZE));
  28664. date_str[CTC_DATE_SIZE] = '\0';
  28665. ExpectIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  28666. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  28667. s = NULL;
  28668. XMEMSET(date_str, 0, sizeof(date_str));
  28669. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  28670. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  28671. s = NULL;
  28672. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  28673. offset_day = 7;
  28674. offset_sec = 45 * mini;
  28675. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  28676. offset_sec));
  28677. ExpectTrue(asn_time->type == asn_utc_time);
  28678. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28679. CTC_DATE_SIZE));
  28680. date_str[CTC_DATE_SIZE] = '\0';
  28681. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  28682. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  28683. asn_time = NULL;
  28684. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day,
  28685. offset_sec));
  28686. ExpectTrue(asn_time->type == asn_utc_time);
  28687. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28688. CTC_DATE_SIZE));
  28689. date_str[CTC_DATE_SIZE] = '\0';
  28690. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  28691. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  28692. #endif
  28693. return EXPECT_RESULT();
  28694. }
  28695. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  28696. {
  28697. EXPECT_DECLS;
  28698. #if (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28699. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  28700. defined(OPENSSL_ALL)) && !defined(NO_ASN_TIME)
  28701. ASN1_TIME asnTime;
  28702. struct tm tm;
  28703. time_t testTime = 1683926567; /* Fri May 12 09:22:47 PM UTC 2023 */
  28704. XMEMSET(&tm, 0, sizeof(struct tm));
  28705. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  28706. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  28707. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, NULL), 1);
  28708. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28709. ExpectIntEQ(tm.tm_sec, 15);
  28710. ExpectIntEQ(tm.tm_min, 15);
  28711. ExpectIntEQ(tm.tm_hour, 21);
  28712. ExpectIntEQ(tm.tm_mday, 22);
  28713. ExpectIntEQ(tm.tm_mon, 1);
  28714. ExpectIntEQ(tm.tm_year, 100);
  28715. ExpectIntEQ(tm.tm_isdst, 0);
  28716. #ifdef XMKTIME
  28717. ExpectIntEQ(tm.tm_wday, 2);
  28718. ExpectIntEQ(tm.tm_yday, 52);
  28719. #endif
  28720. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "500222211515Z"), 1);
  28721. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28722. ExpectIntEQ(tm.tm_year, 50);
  28723. /* Get current time. */
  28724. ExpectIntEQ(ASN1_TIME_to_tm(NULL, NULL), 0);
  28725. ExpectIntEQ(ASN1_TIME_to_tm(NULL, &tm), 1);
  28726. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  28727. /* 0 length. */
  28728. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28729. /* No type. */
  28730. asnTime.length = 1;
  28731. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28732. /* Not UTCTIME length. */
  28733. asnTime.type = V_ASN1_UTCTIME;
  28734. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28735. /* Not GENERALIZEDTIME length. */
  28736. asnTime.type = V_ASN1_GENERALIZEDTIME;
  28737. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28738. /* Not Zulu timezone. */
  28739. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515U"), 1);
  28740. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28741. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "20000222211515U"), 1);
  28742. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28743. #ifdef XMKTIME
  28744. ExpectNotNull(ASN1_TIME_adj(&asnTime, testTime, 0, 0));
  28745. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28746. ExpectIntEQ(tm.tm_sec, 47);
  28747. ExpectIntEQ(tm.tm_min, 22);
  28748. ExpectIntEQ(tm.tm_hour, 21);
  28749. ExpectIntEQ(tm.tm_mday, 12);
  28750. ExpectIntEQ(tm.tm_mon, 4);
  28751. ExpectIntEQ(tm.tm_year, 123);
  28752. ExpectIntEQ(tm.tm_wday, 5);
  28753. ExpectIntEQ(tm.tm_yday, 131);
  28754. /* Confirm that when used with a tm struct from ASN1_TIME_adj, all other
  28755. fields are zeroed out as expected. */
  28756. ExpectIntEQ(tm.tm_isdst, 0);
  28757. #endif
  28758. #endif
  28759. return EXPECT_RESULT();
  28760. }
  28761. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
  28762. {
  28763. EXPECT_DECLS;
  28764. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28765. WOLFSSL_ASN1_TIME *t = NULL;
  28766. WOLFSSL_ASN1_TIME *out = NULL;
  28767. WOLFSSL_ASN1_TIME *gtime = NULL;
  28768. int tlen = 0;
  28769. unsigned char *data = NULL;
  28770. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  28771. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(NULL, &out));
  28772. /* type not set. */
  28773. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28774. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28775. t = NULL;
  28776. /* UTC Time test */
  28777. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  28778. if (t != NULL) {
  28779. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  28780. t->type = ASN_UTC_TIME;
  28781. t->length = ASN_UTC_TIME_SIZE;
  28782. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  28783. }
  28784. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t), ASN_UTC_TIME_SIZE);
  28785. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)), "050727123456Z");
  28786. out = NULL;
  28787. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28788. wolfSSL_ASN1_TIME_free(gtime);
  28789. gtime = NULL;
  28790. ExpectNotNull(out = wolfSSL_ASN1_TIME_new());
  28791. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28792. ExpectPtrEq(gtime, out);
  28793. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28794. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28795. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  28796. /* Generalized Time test */
  28797. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28798. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  28799. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  28800. if (t != NULL) {
  28801. t->type = ASN_GENERALIZED_TIME;
  28802. t->length = ASN_GENERALIZED_TIME_SIZE;
  28803. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  28804. }
  28805. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t),
  28806. ASN_GENERALIZED_TIME_SIZE);
  28807. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)),
  28808. "20050727123456Z");
  28809. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28810. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28811. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28812. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  28813. /* UTC Time to Generalized Time 1900's test */
  28814. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28815. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  28816. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  28817. if (t != NULL) {
  28818. t->type = ASN_UTC_TIME;
  28819. t->length = ASN_UTC_TIME_SIZE;
  28820. XMEMCPY(t->data, "500727123456Z", ASN_UTC_TIME_SIZE);
  28821. }
  28822. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28823. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28824. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28825. ExpectStrEQ((char*)gtime->data, "19500727123456Z");
  28826. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28827. /* Null parameter test */
  28828. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28829. gtime = NULL;
  28830. out = NULL;
  28831. if (t != NULL) {
  28832. t->type = ASN_UTC_TIME;
  28833. t->length = ASN_UTC_TIME_SIZE;
  28834. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  28835. }
  28836. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  28837. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28838. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28839. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  28840. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28841. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28842. #endif
  28843. return EXPECT_RESULT();
  28844. }
  28845. static int test_wolfSSL_ASN1_TIME_print(void)
  28846. {
  28847. EXPECT_DECLS;
  28848. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_BIO) && \
  28849. (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28850. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  28851. defined(OPENSSL_ALL)) && defined(USE_CERT_BUFFERS_2048) && \
  28852. !defined(NO_ASN_TIME)
  28853. BIO* bio = NULL;
  28854. BIO* fixed = NULL;
  28855. X509* x509 = NULL;
  28856. const unsigned char* der = client_cert_der_2048;
  28857. ASN1_TIME* notAfter = NULL;
  28858. ASN1_TIME* notBefore = NULL;
  28859. unsigned char buf[25];
  28860. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28861. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28862. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  28863. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  28864. ExpectNotNull(notBefore = X509_get_notBefore(x509));
  28865. ExpectIntEQ(ASN1_TIME_print(NULL, NULL), 0);
  28866. ExpectIntEQ(ASN1_TIME_print(bio, NULL), 0);
  28867. ExpectIntEQ(ASN1_TIME_print(NULL, notBefore), 0);
  28868. ExpectIntEQ(ASN1_TIME_print(bio, notBefore), 1);
  28869. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  28870. ExpectIntEQ(XMEMCMP(buf, "Dec 13 22:19:28 2023 GMT", sizeof(buf) - 1), 0);
  28871. /* Test BIO_write fails. */
  28872. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28873. /* Ensure there is 0 bytes available to write into. */
  28874. ExpectIntEQ(BIO_write(fixed, buf, 1), 1);
  28875. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  28876. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28877. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  28878. ExpectIntEQ(BIO_set_write_buf_size(fixed, 23), 1);
  28879. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  28880. /* create a bad time and test results */
  28881. ExpectNotNull(notAfter = X509_get_notAfter(x509));
  28882. ExpectIntEQ(ASN1_TIME_check(notAfter), 1);
  28883. if (EXPECT_SUCCESS()) {
  28884. notAfter->data[8] = 0;
  28885. notAfter->data[3] = 0;
  28886. }
  28887. ExpectIntNE(ASN1_TIME_print(bio, notAfter), 1);
  28888. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  28889. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  28890. ExpectIntEQ(ASN1_TIME_check(notAfter), 0);
  28891. BIO_free(bio);
  28892. BIO_free(fixed);
  28893. X509_free(x509);
  28894. #endif
  28895. return EXPECT_RESULT();
  28896. }
  28897. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  28898. {
  28899. EXPECT_DECLS;
  28900. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  28901. BIO* bio = NULL;
  28902. ASN1_UTCTIME* utc = NULL;
  28903. unsigned char buf[25];
  28904. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  28905. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  28906. const char* genDate = "20190424111501Z"; /* GEN = YYYYMMDDHHMMSSZ */
  28907. /* Valid date */
  28908. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28909. ExpectNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  28910. DYNAMIC_TYPE_ASN1));
  28911. if (utc != NULL) {
  28912. utc->type = ASN_UTC_TIME;
  28913. utc->length = ASN_UTC_TIME_SIZE;
  28914. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  28915. }
  28916. ExpectIntEQ(ASN1_UTCTIME_print(NULL, NULL), 0);
  28917. ExpectIntEQ(ASN1_UTCTIME_print(bio, NULL), 0);
  28918. ExpectIntEQ(ASN1_UTCTIME_print(NULL, utc), 0);
  28919. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  28920. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  28921. ExpectIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  28922. XMEMSET(buf, 0, sizeof(buf));
  28923. BIO_free(bio);
  28924. bio = NULL;
  28925. /* Invalid format */
  28926. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28927. if (utc != NULL) {
  28928. utc->type = ASN_UTC_TIME;
  28929. utc->length = ASN_UTC_TIME_SIZE;
  28930. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  28931. }
  28932. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  28933. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  28934. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  28935. /* Invalid type */
  28936. if (utc != NULL) {
  28937. utc->type = ASN_GENERALIZED_TIME;
  28938. utc->length = ASN_GENERALIZED_TIME_SIZE;
  28939. XMEMCPY(utc->data, (byte*)genDate, ASN_GENERALIZED_TIME_SIZE);
  28940. }
  28941. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  28942. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  28943. BIO_free(bio);
  28944. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  28945. return EXPECT_RESULT();
  28946. }
  28947. static int test_wolfSSL_ASN1_TYPE(void)
  28948. {
  28949. EXPECT_DECLS;
  28950. #if defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD) || \
  28951. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  28952. WOLFSSL_ASN1_TYPE* t = NULL;
  28953. WOLFSSL_ASN1_OBJECT* obj = NULL;
  28954. #ifndef NO_ASN_TIME
  28955. WOLFSSL_ASN1_TIME* time = NULL;
  28956. #endif
  28957. WOLFSSL_ASN1_STRING* str = NULL;
  28958. unsigned char data[] = { 0x00 };
  28959. ASN1_TYPE_set(NULL, V_ASN1_NULL, NULL);
  28960. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28961. ASN1_TYPE_set(t, V_ASN1_EOC, NULL);
  28962. wolfSSL_ASN1_TYPE_free(t);
  28963. t = NULL;
  28964. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28965. ASN1_TYPE_set(t, V_ASN1_NULL, NULL);
  28966. ASN1_TYPE_set(t, V_ASN1_NULL, data);
  28967. wolfSSL_ASN1_TYPE_free(t);
  28968. t = NULL;
  28969. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28970. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  28971. ASN1_TYPE_set(t, V_ASN1_OBJECT, obj);
  28972. wolfSSL_ASN1_TYPE_free(t);
  28973. t = NULL;
  28974. #ifndef NO_ASN_TIME
  28975. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28976. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  28977. ASN1_TYPE_set(t, V_ASN1_UTCTIME, time);
  28978. wolfSSL_ASN1_TYPE_free(t);
  28979. t = NULL;
  28980. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28981. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  28982. ASN1_TYPE_set(t, V_ASN1_GENERALIZEDTIME, time);
  28983. wolfSSL_ASN1_TYPE_free(t);
  28984. t = NULL;
  28985. #endif
  28986. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28987. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28988. ASN1_TYPE_set(t, V_ASN1_UTF8STRING, str);
  28989. wolfSSL_ASN1_TYPE_free(t);
  28990. t = NULL;
  28991. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28992. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28993. ASN1_TYPE_set(t, V_ASN1_PRINTABLESTRING, str);
  28994. wolfSSL_ASN1_TYPE_free(t);
  28995. t = NULL;
  28996. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28997. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28998. ASN1_TYPE_set(t, V_ASN1_T61STRING, str);
  28999. wolfSSL_ASN1_TYPE_free(t);
  29000. t = NULL;
  29001. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29002. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29003. ASN1_TYPE_set(t, V_ASN1_IA5STRING, str);
  29004. wolfSSL_ASN1_TYPE_free(t);
  29005. t = NULL;
  29006. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29007. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29008. ASN1_TYPE_set(t, V_ASN1_UNIVERSALSTRING, str);
  29009. wolfSSL_ASN1_TYPE_free(t);
  29010. t = NULL;
  29011. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29012. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29013. ASN1_TYPE_set(t, V_ASN1_SEQUENCE, str);
  29014. wolfSSL_ASN1_TYPE_free(t);
  29015. t = NULL;
  29016. #endif
  29017. return EXPECT_RESULT();
  29018. }
  29019. /* Testing code used in dpp.c in hostap */
  29020. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  29021. typedef struct {
  29022. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  29023. * as an OID identifying the curve */
  29024. X509_ALGOR *alg;
  29025. /* Compressed format public key per ANSI X9.63 */
  29026. ASN1_BIT_STRING *pub_key;
  29027. } DPP_BOOTSTRAPPING_KEY;
  29028. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  29029. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  29030. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  29031. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  29032. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  29033. typedef struct {
  29034. ASN1_INTEGER *integer;
  29035. } TEST_ASN1;
  29036. ASN1_SEQUENCE(TEST_ASN1) = {
  29037. ASN1_SIMPLE(TEST_ASN1, integer, ASN1_INTEGER),
  29038. } ASN1_SEQUENCE_END(TEST_ASN1)
  29039. IMPLEMENT_ASN1_FUNCTIONS(TEST_ASN1)
  29040. typedef struct {
  29041. ASN1_OCTET_STRING *octet_string;
  29042. } TEST_FAIL_ASN1;
  29043. #define WOLFSSL_ASN1_OCTET_STRING_ASN1 4
  29044. ASN1_SEQUENCE(TEST_FAIL_ASN1) = {
  29045. ASN1_SIMPLE(TEST_FAIL_ASN1, octet_string, ASN1_OCTET_STRING),
  29046. } ASN1_SEQUENCE_END(TEST_FAIL_ASN1)
  29047. IMPLEMENT_ASN1_FUNCTIONS(TEST_FAIL_ASN1)
  29048. #endif
  29049. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  29050. {
  29051. EXPECT_DECLS;
  29052. /* Testing code used in dpp.c in hostap */
  29053. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  29054. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  29055. EC_KEY *eckey = NULL;
  29056. EVP_PKEY *key = NULL;
  29057. size_t len = 0;
  29058. unsigned char *der = NULL;
  29059. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  29060. const unsigned char *in = ecc_clikey_der_256;
  29061. WOLFSSL_ASN1_OBJECT* ec_obj = NULL;
  29062. WOLFSSL_ASN1_OBJECT* group_obj = NULL;
  29063. const EC_GROUP *group = NULL;
  29064. const EC_POINT *point = NULL;
  29065. int nid;
  29066. TEST_ASN1 *test_asn1 = NULL;
  29067. TEST_FAIL_ASN1 test_fail_asn1;
  29068. const unsigned char badObjDer[] = { 0x06, 0x00 };
  29069. const unsigned char goodObjDer[] = {
  29070. 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
  29071. };
  29072. WOLFSSL_ASN1_ITEM emptyTemplate;
  29073. XMEMSET(&emptyTemplate, 0, sizeof(WOLFSSL_ASN1_ITEM));
  29074. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  29075. der = NULL;
  29076. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(NULL, &der), 0);
  29077. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, NULL), 0);
  29078. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29079. ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  29080. (long)sizeof_ecc_clikey_der_256));
  29081. ExpectNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  29082. ExpectNotNull(group = EC_KEY_get0_group(eckey));
  29083. ExpectNotNull(point = EC_KEY_get0_public_key(eckey));
  29084. nid = EC_GROUP_get_curve_name(group);
  29085. ec_obj = OBJ_nid2obj(EVP_PKEY_EC);
  29086. group_obj = OBJ_nid2obj(nid);
  29087. if ((ec_obj != NULL) && (group_obj != NULL)) {
  29088. ExpectIntEQ(X509_ALGOR_set0(bootstrap->alg, ec_obj, V_ASN1_OBJECT,
  29089. group_obj), 1);
  29090. if (EXPECT_SUCCESS()) {
  29091. ec_obj = NULL;
  29092. group_obj = NULL;
  29093. }
  29094. }
  29095. wolfSSL_ASN1_OBJECT_free(group_obj);
  29096. wolfSSL_ASN1_OBJECT_free(ec_obj);
  29097. ExpectIntEQ(EC_POINT_point2oct(group, point, 0, NULL, 0, NULL), 0);
  29098. #ifdef HAVE_COMP_KEY
  29099. ExpectIntGT((len = EC_POINT_point2oct(
  29100. group, point, POINT_CONVERSION_COMPRESSED,
  29101. NULL, 0, NULL)), 0);
  29102. #else
  29103. ExpectIntGT((len = EC_POINT_point2oct(
  29104. group, point, POINT_CONVERSION_UNCOMPRESSED,
  29105. NULL, 0, NULL)), 0);
  29106. #endif
  29107. ExpectNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  29108. #ifdef HAVE_COMP_KEY
  29109. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  29110. der, len-1, NULL), 0);
  29111. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  29112. der, len, NULL), len);
  29113. #else
  29114. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  29115. der, len-1, NULL), 0);
  29116. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  29117. der, len, NULL), len);
  29118. #endif
  29119. if (EXPECT_SUCCESS()) {
  29120. bootstrap->pub_key->data = der;
  29121. bootstrap->pub_key->length = (int)len;
  29122. /* Not actually used */
  29123. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  29124. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  29125. }
  29126. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, NULL), 0);
  29127. der = NULL;
  29128. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29129. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29130. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  29131. EVP_PKEY_free(key);
  29132. EC_KEY_free(eckey);
  29133. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  29134. bootstrap = NULL;
  29135. DPP_BOOTSTRAPPING_KEY_free(NULL);
  29136. /* Create bootstrap key with bad OBJECT_ID DER data, parameter that is
  29137. * a NULL and an empty BIT_STRING. */
  29138. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  29139. ExpectNotNull(bootstrap->alg->algorithm = wolfSSL_ASN1_OBJECT_new());
  29140. if (EXPECT_SUCCESS()) {
  29141. bootstrap->alg->algorithm->obj = badObjDer;
  29142. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(badObjDer);
  29143. }
  29144. ExpectNotNull(bootstrap->alg->parameter = wolfSSL_ASN1_TYPE_new());
  29145. if (EXPECT_SUCCESS()) {
  29146. bootstrap->alg->parameter->type = V_ASN1_NULL;
  29147. bootstrap->alg->parameter->value.ptr = NULL;
  29148. bootstrap->pub_key->data = NULL;
  29149. bootstrap->pub_key->length = 0;
  29150. /* Not actually used */
  29151. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  29152. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  29153. }
  29154. /* Encode with bad OBJECT_ID. */
  29155. der = NULL;
  29156. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29157. /* Fix OBJECT_ID and encode with empty BIT_STRING. */
  29158. if (EXPECT_SUCCESS()) {
  29159. bootstrap->alg->algorithm->obj = goodObjDer;
  29160. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(goodObjDer);
  29161. bootstrap->alg->algorithm->grp = 2;
  29162. }
  29163. der = NULL;
  29164. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 16);
  29165. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, &emptyTemplate), 0);
  29166. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  29167. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  29168. /* Test integer */
  29169. ExpectNotNull(test_asn1 = TEST_ASN1_new());
  29170. der = NULL;
  29171. ExpectIntEQ(ASN1_INTEGER_set(test_asn1->integer, 100), 1);
  29172. ExpectIntEQ(i2d_TEST_ASN1(test_asn1, &der), 5);
  29173. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  29174. TEST_ASN1_free(test_asn1);
  29175. /* Test integer cases. */
  29176. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  29177. TEST_ASN1_free(NULL);
  29178. /* Test error cases. */
  29179. ExpectNull(TEST_FAIL_ASN1_new());
  29180. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  29181. TEST_FAIL_ASN1_free(NULL);
  29182. XMEMSET(&test_fail_asn1, 0, sizeof(TEST_FAIL_ASN1));
  29183. ExpectIntEQ(i2d_TEST_FAIL_ASN1(&test_fail_asn1, &der), 0);
  29184. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  29185. #endif /* OPENSSL_ALL && HAVE_ECC && USE_CERT_BUFFERS_256 */
  29186. return EXPECT_RESULT();
  29187. }
  29188. static int test_wolfSSL_lhash(void)
  29189. {
  29190. EXPECT_DECLS;
  29191. #ifdef OPENSSL_ALL
  29192. const char testStr[] = "Like a true nature's child\n"
  29193. "We were born\n"
  29194. "Born to be wild";
  29195. #ifdef NO_SHA
  29196. ExpectIntEQ(lh_strhash(testStr), 0xf9dc8a43);
  29197. #else
  29198. ExpectIntEQ(lh_strhash(testStr), 0x5b7541dc);
  29199. #endif
  29200. #endif
  29201. return EXPECT_RESULT();
  29202. }
  29203. static int test_wolfSSL_X509_NAME(void)
  29204. {
  29205. EXPECT_DECLS;
  29206. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  29207. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29208. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  29209. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  29210. defined(OPENSSL_EXTRA))
  29211. X509* x509 = NULL;
  29212. const unsigned char* c = NULL;
  29213. unsigned char buf[4096];
  29214. int bytes = 0;
  29215. XFILE f = XBADFILE;
  29216. const X509_NAME* a = NULL;
  29217. const X509_NAME* b = NULL;
  29218. X509_NAME* d2i_name = NULL;
  29219. int sz = 0;
  29220. unsigned char* tmp = NULL;
  29221. char file[] = "./certs/ca-cert.der";
  29222. #ifndef OPENSSL_EXTRA_X509_SMALL
  29223. byte empty[] = { /* CN=empty emailAddress= */
  29224. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  29225. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  29226. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  29227. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  29228. 0x01, 0x16, 0x00
  29229. };
  29230. #endif
  29231. #ifndef OPENSSL_EXTRA_X509_SMALL
  29232. /* test compile of deprecated function, returns 0 */
  29233. ExpectIntEQ(CRYPTO_thread_id(), 0);
  29234. #endif
  29235. ExpectNotNull(a = X509_NAME_new());
  29236. X509_NAME_free((X509_NAME*)a);
  29237. a = NULL;
  29238. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  29239. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  29240. if (f != XBADFILE)
  29241. XFCLOSE(f);
  29242. c = buf;
  29243. ExpectNotNull(x509 = wolfSSL_X509_d2i_ex(NULL, c, bytes, HEAP_HINT));
  29244. /* test cmp function */
  29245. ExpectNotNull(a = X509_get_issuer_name(x509));
  29246. ExpectNotNull(b = X509_get_subject_name(x509));
  29247. #ifndef OPENSSL_EXTRA_X509_SMALL
  29248. ExpectIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  29249. #endif
  29250. tmp = buf;
  29251. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  29252. if (sz > 0 && tmp == buf) {
  29253. fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
  29254. __LINE__);
  29255. fprintf(stderr, " Expected pointer to be incremented\n");
  29256. abort();
  29257. }
  29258. #ifndef OPENSSL_EXTRA_X509_SMALL
  29259. tmp = buf;
  29260. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  29261. #endif
  29262. /* if output parameter is NULL, should still return required size. */
  29263. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  29264. /* retry but with the function creating a buffer */
  29265. tmp = NULL;
  29266. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  29267. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  29268. tmp = NULL;
  29269. #ifdef WOLFSSL_CERT_NAME_ALL
  29270. /* test for givenName and name */
  29271. {
  29272. WOLFSSL_X509_NAME_ENTRY* entry = NULL;
  29273. const byte gName[] = "test-given-name";
  29274. const byte name[] = "test-name";
  29275. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  29276. NID_givenName, ASN_UTF8STRING, gName, sizeof(gName)));
  29277. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  29278. 1);
  29279. wolfSSL_X509_NAME_ENTRY_free(entry);
  29280. entry = NULL;
  29281. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  29282. NID_name, ASN_UTF8STRING, name, sizeof(name)));
  29283. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  29284. 1);
  29285. wolfSSL_X509_NAME_ENTRY_free(entry);
  29286. tmp = NULL;
  29287. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  29288. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  29289. }
  29290. #endif
  29291. b = NULL;
  29292. ExpectNotNull(b = X509_NAME_dup((X509_NAME*)a));
  29293. #ifndef OPENSSL_EXTRA_X509_SMALL
  29294. ExpectIntEQ(X509_NAME_cmp(a, b), 0);
  29295. #endif
  29296. X509_NAME_free((X509_NAME*)b);
  29297. X509_NAME_free(d2i_name);
  29298. d2i_name = NULL;
  29299. X509_free(x509);
  29300. #ifndef OPENSSL_EXTRA_X509_SMALL
  29301. /* test with an empty domain component */
  29302. tmp = empty;
  29303. sz = sizeof(empty);
  29304. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  29305. ExpectIntEQ(X509_NAME_entry_count(d2i_name), 2);
  29306. /* size of empty emailAddress will be 0 */
  29307. tmp = buf;
  29308. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  29309. (char*)tmp, sizeof(buf)), 0);
  29310. /* should contain no organization name */
  29311. tmp = buf;
  29312. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  29313. (char*)tmp, sizeof(buf)), -1);
  29314. X509_NAME_free(d2i_name);
  29315. #endif
  29316. #endif
  29317. return EXPECT_RESULT();
  29318. }
  29319. static int test_wolfSSL_X509_NAME_hash(void)
  29320. {
  29321. EXPECT_DECLS;
  29322. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  29323. !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  29324. BIO* bio = NULL;
  29325. X509* x509 = NULL;
  29326. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  29327. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  29328. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  29329. ExpectIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  29330. ExpectIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  29331. X509_free(x509);
  29332. BIO_free(bio);
  29333. #endif
  29334. return EXPECT_RESULT();
  29335. }
  29336. static int test_wolfSSL_X509_NAME_print_ex(void)
  29337. {
  29338. EXPECT_DECLS;
  29339. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  29340. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  29341. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  29342. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  29343. !defined(NO_BIO) && !defined(NO_RSA)
  29344. int memSz = 0;
  29345. byte* mem = NULL;
  29346. BIO* bio = NULL;
  29347. BIO* membio = NULL;
  29348. X509* x509 = NULL;
  29349. X509_NAME* name = NULL;
  29350. const char* expNormal = "C=US, CN=wolfssl.com";
  29351. const char* expReverse = "CN=wolfssl.com, C=US";
  29352. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  29353. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  29354. const char* expRFC5523 =
  29355. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  29356. /* Test with real cert (svrCertFile) first */
  29357. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  29358. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  29359. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  29360. ExpectNotNull(name = X509_get_subject_name(x509));
  29361. /* Test without flags */
  29362. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29363. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29364. BIO_free(membio);
  29365. membio = NULL;
  29366. /* Test flag: XN_FLAG_RFC2253 */
  29367. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29368. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29369. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29370. BIO_free(membio);
  29371. membio = NULL;
  29372. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  29373. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29374. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29375. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29376. BIO_free(membio);
  29377. membio = NULL;
  29378. X509_free(x509);
  29379. BIO_free(bio);
  29380. name = NULL;
  29381. /* Test normal case without escaped characters */
  29382. {
  29383. /* Create name: "/C=US/CN=wolfssl.com" */
  29384. ExpectNotNull(name = X509_NAME_new());
  29385. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  29386. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  29387. WOLFSSL_SUCCESS);
  29388. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  29389. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  29390. WOLFSSL_SUCCESS);
  29391. /* Test without flags */
  29392. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29393. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29394. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29395. ExpectIntEQ(memSz, XSTRLEN(expNormal));
  29396. ExpectIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  29397. BIO_free(membio);
  29398. membio = NULL;
  29399. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  29400. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29401. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29402. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29403. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29404. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  29405. BIO_free(membio);
  29406. membio = NULL;
  29407. /* Test flags: XN_FLAG_DN_REV - reversed */
  29408. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29409. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29410. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29411. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29412. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  29413. ExpectIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  29414. BIO_free(membio);
  29415. membio = NULL;
  29416. X509_NAME_free(name);
  29417. name = NULL;
  29418. }
  29419. /* Test RFC2253 characters are escaped with backslashes */
  29420. {
  29421. ExpectNotNull(name = X509_NAME_new());
  29422. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  29423. /* space at beginning and end, and: ,+"\ */
  29424. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  29425. WOLFSSL_SUCCESS);
  29426. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  29427. /* # at beginning, and: <>;*/
  29428. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  29429. WOLFSSL_SUCCESS);
  29430. /* Test without flags */
  29431. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29432. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29433. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29434. ExpectIntEQ(memSz, XSTRLEN(expNotEscaped));
  29435. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  29436. XSTRLEN(expNotEscaped)), 0);
  29437. BIO_free(membio);
  29438. membio = NULL;
  29439. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  29440. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29441. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29442. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29443. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29444. ExpectIntEQ(memSz, XSTRLEN(expRFC5523));
  29445. ExpectIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  29446. BIO_free(membio);
  29447. membio = NULL;
  29448. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  29449. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29450. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29451. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29452. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29453. ExpectIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  29454. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  29455. XSTRLEN(expNotEscapedRev)), 0);
  29456. BIO_free(membio);
  29457. X509_NAME_free(name);
  29458. }
  29459. #endif
  29460. return EXPECT_RESULT();
  29461. }
  29462. #ifndef NO_BIO
  29463. static int test_wolfSSL_X509_INFO_multiple_info(void)
  29464. {
  29465. EXPECT_DECLS;
  29466. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  29467. STACK_OF(X509_INFO) *info_stack = NULL;
  29468. X509_INFO *info = NULL;
  29469. int len;
  29470. int i;
  29471. const char* files[] = {
  29472. cliCertFile,
  29473. cliKeyFile,
  29474. /* This needs to be the order as svrCertFile contains the
  29475. * intermediate cert as well. */
  29476. svrKeyFile,
  29477. svrCertFile,
  29478. NULL,
  29479. };
  29480. const char** curFile;
  29481. BIO *fileBIO = NULL;
  29482. BIO *concatBIO = NULL;
  29483. byte tmp[FOURK_BUF];
  29484. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  29485. * to group objects together. */
  29486. ExpectNotNull(concatBIO = BIO_new(BIO_s_mem()));
  29487. for (curFile = files; EXPECT_SUCCESS() && *curFile != NULL; curFile++) {
  29488. int fileLen = 0;
  29489. ExpectNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  29490. ExpectIntGT(fileLen = wolfSSL_BIO_get_len(fileBIO), 0);
  29491. if (EXPECT_SUCCESS()) {
  29492. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  29493. ExpectIntEQ(BIO_write(concatBIO, tmp, len), len);
  29494. fileLen -= len;
  29495. if (EXPECT_FAIL())
  29496. break;
  29497. }
  29498. /* Make sure we read the entire file */
  29499. ExpectIntEQ(fileLen, 0);
  29500. }
  29501. BIO_free(fileBIO);
  29502. fileBIO = NULL;
  29503. }
  29504. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  29505. NULL));
  29506. ExpectIntEQ(sk_X509_INFO_num(info_stack), 3);
  29507. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  29508. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  29509. ExpectNotNull(info->x509);
  29510. ExpectNull(info->crl);
  29511. if (i != 0) {
  29512. ExpectNotNull(info->x_pkey);
  29513. ExpectIntEQ(X509_check_private_key(info->x509,
  29514. info->x_pkey->dec_pkey), 1);
  29515. }
  29516. else {
  29517. ExpectNull(info->x_pkey);
  29518. }
  29519. }
  29520. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29521. BIO_free(concatBIO);
  29522. #endif
  29523. return EXPECT_RESULT();
  29524. }
  29525. #endif
  29526. #ifndef NO_BIO
  29527. static int test_wolfSSL_X509_INFO(void)
  29528. {
  29529. EXPECT_DECLS;
  29530. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  29531. STACK_OF(X509_INFO) *info_stack = NULL;
  29532. X509_INFO *info = NULL;
  29533. BIO *cert = NULL;
  29534. int i;
  29535. /* PEM in hex format to avoid null terminator */
  29536. byte data[] = {
  29537. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  29538. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  29539. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  29540. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  29541. 0x2d, 0x2d, 0x2d
  29542. };
  29543. /* PEM in hex format to avoid null terminator */
  29544. byte data2[] = {
  29545. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  29546. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  29547. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  29548. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  29549. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  29550. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  29551. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  29552. };
  29553. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  29554. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29555. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  29556. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  29557. ExpectNotNull(info->x509);
  29558. ExpectNull(info->crl);
  29559. ExpectNull(info->x_pkey);
  29560. }
  29561. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29562. info_stack = NULL;
  29563. BIO_free(cert);
  29564. cert = NULL;
  29565. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  29566. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29567. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29568. info_stack = NULL;
  29569. BIO_free(cert);
  29570. cert = NULL;
  29571. /* This case should fail due to invalid input. */
  29572. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  29573. ExpectIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  29574. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29575. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29576. info_stack = NULL;
  29577. BIO_free(cert);
  29578. cert = NULL;
  29579. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  29580. ExpectIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  29581. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29582. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29583. BIO_free(cert);
  29584. #endif
  29585. return EXPECT_RESULT();
  29586. }
  29587. #endif
  29588. static int test_wolfSSL_X509_subject_name_hash(void)
  29589. {
  29590. EXPECT_DECLS;
  29591. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29592. !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  29593. X509* x509 = NULL;
  29594. X509_NAME* subjectName = NULL;
  29595. unsigned long ret1 = 0;
  29596. unsigned long ret2 = 0;
  29597. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29598. SSL_FILETYPE_PEM));
  29599. ExpectNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  29600. /* These two
  29601. * - X509_subject_name_hash(x509)
  29602. * - X509_NAME_hash(X509_get_subject_name(x509))
  29603. * should give the same hash, if !defined(NO_SHA) is true. */
  29604. ret1 = X509_subject_name_hash(x509);
  29605. ExpectIntNE(ret1, 0);
  29606. #if !defined(NO_SHA)
  29607. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  29608. ExpectIntNE(ret2, 0);
  29609. ExpectIntEQ(ret1, ret2);
  29610. #else
  29611. (void) ret2;
  29612. #endif
  29613. X509_free(x509);
  29614. #endif
  29615. return EXPECT_RESULT();
  29616. }
  29617. static int test_wolfSSL_X509_issuer_name_hash(void)
  29618. {
  29619. EXPECT_DECLS;
  29620. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29621. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  29622. X509* x509 = NULL;
  29623. X509_NAME* issuertName = NULL;
  29624. unsigned long ret1 = 0;
  29625. unsigned long ret2 = 0;
  29626. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29627. SSL_FILETYPE_PEM));
  29628. ExpectNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  29629. /* These two
  29630. * - X509_issuer_name_hash(x509)
  29631. * - X509_NAME_hash(X509_get_issuer_name(x509))
  29632. * should give the same hash, if !defined(NO_SHA) is true. */
  29633. ret1 = X509_issuer_name_hash(x509);
  29634. ExpectIntNE(ret1, 0);
  29635. #if !defined(NO_SHA)
  29636. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  29637. ExpectIntNE(ret2, 0);
  29638. ExpectIntEQ(ret1, ret2);
  29639. #else
  29640. (void) ret2;
  29641. #endif
  29642. X509_free(x509);
  29643. #endif
  29644. return EXPECT_RESULT();
  29645. }
  29646. static int test_wolfSSL_X509_check_host(void)
  29647. {
  29648. EXPECT_DECLS;
  29649. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29650. && !defined(NO_SHA) && !defined(NO_RSA)
  29651. X509* x509 = NULL;
  29652. const char altName[] = "example.com";
  29653. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29654. SSL_FILETYPE_PEM));
  29655. ExpectIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  29656. WOLFSSL_SUCCESS);
  29657. ExpectIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  29658. WOLFSSL_FAILURE);
  29659. X509_free(x509);
  29660. ExpectIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  29661. WOLFSSL_FAILURE);
  29662. #endif
  29663. return EXPECT_RESULT();
  29664. }
  29665. static int test_wolfSSL_X509_check_email(void)
  29666. {
  29667. EXPECT_DECLS;
  29668. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  29669. X509* x509 = NULL;
  29670. const char goodEmail[] = "info@wolfssl.com";
  29671. const char badEmail[] = "disinfo@wolfssl.com";
  29672. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29673. SSL_FILETYPE_PEM));
  29674. /* Should fail on non-matching email address */
  29675. ExpectIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  29676. WOLFSSL_FAILURE);
  29677. /* Should succeed on matching email address */
  29678. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  29679. WOLFSSL_SUCCESS);
  29680. /* Should compute length internally when not provided */
  29681. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  29682. WOLFSSL_SUCCESS);
  29683. /* Should fail when email address is NULL */
  29684. ExpectIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  29685. WOLFSSL_FAILURE);
  29686. X509_free(x509);
  29687. /* Should fail when x509 is NULL */
  29688. ExpectIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  29689. WOLFSSL_FAILURE);
  29690. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  29691. return EXPECT_RESULT();
  29692. }
  29693. static int test_wc_PemToDer(void)
  29694. {
  29695. EXPECT_DECLS;
  29696. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  29697. int ret;
  29698. DerBuffer* pDer = NULL;
  29699. const char* ca_cert = "./certs/server-cert.pem";
  29700. byte* cert_buf = NULL;
  29701. size_t cert_sz = 0;
  29702. int eccKey = 0;
  29703. EncryptedInfo info;
  29704. XMEMSET(&info, 0, sizeof(info));
  29705. ExpectIntEQ(ret = load_file(ca_cert, &cert_buf, &cert_sz), 0);
  29706. ExpectIntEQ(ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE, &pDer, NULL,
  29707. &info, &eccKey), 0);
  29708. wc_FreeDer(&pDer);
  29709. pDer = NULL;
  29710. if (cert_buf != NULL) {
  29711. free(cert_buf);
  29712. cert_buf = NULL;
  29713. }
  29714. #ifdef HAVE_ECC
  29715. {
  29716. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  29717. byte key_buf[256] = {0};
  29718. /* Test fail of loading a key with cert type */
  29719. ExpectIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  29720. key_buf[0] = '\n';
  29721. ExpectNotNull(XMEMCPY(key_buf + 1, cert_buf, cert_sz));
  29722. ExpectIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  29723. &pDer, NULL, &info, &eccKey)), 0);
  29724. #ifdef OPENSSL_EXTRA
  29725. ExpectIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  29726. &pDer, NULL, &info, &eccKey)), 0);
  29727. #endif
  29728. wc_FreeDer(&pDer);
  29729. if (cert_buf != NULL)
  29730. free(cert_buf);
  29731. }
  29732. #endif
  29733. #endif
  29734. return EXPECT_RESULT();
  29735. }
  29736. static int test_wc_AllocDer(void)
  29737. {
  29738. EXPECT_DECLS;
  29739. #if !defined(NO_CERTS)
  29740. DerBuffer* pDer = NULL;
  29741. word32 testSize = 1024;
  29742. ExpectIntEQ(wc_AllocDer(NULL, testSize, CERT_TYPE, HEAP_HINT),
  29743. BAD_FUNC_ARG);
  29744. ExpectIntEQ(wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT), 0);
  29745. ExpectNotNull(pDer);
  29746. wc_FreeDer(&pDer);
  29747. #endif
  29748. return EXPECT_RESULT();
  29749. }
  29750. static int test_wc_CertPemToDer(void)
  29751. {
  29752. EXPECT_DECLS;
  29753. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  29754. const char* ca_cert = "./certs/ca-cert.pem";
  29755. byte* cert_buf = NULL;
  29756. size_t cert_sz = 0;
  29757. size_t cert_dersz = 0;
  29758. byte* cert_der = NULL;
  29759. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  29760. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  29761. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29762. ExpectIntGE(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  29763. (int)cert_dersz, CERT_TYPE), 0);
  29764. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, -1, CERT_TYPE),
  29765. BAD_FUNC_ARG);
  29766. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL, -1, CERT_TYPE),
  29767. BAD_FUNC_ARG);
  29768. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der, -1, CERT_TYPE),
  29769. BAD_FUNC_ARG);
  29770. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, (int)cert_dersz,
  29771. CERT_TYPE), BAD_FUNC_ARG);
  29772. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der,
  29773. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  29774. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL,
  29775. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  29776. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der, -1,
  29777. CERT_TYPE), BAD_FUNC_ARG);
  29778. if (cert_der != NULL)
  29779. free(cert_der);
  29780. if (cert_buf != NULL)
  29781. free(cert_buf);
  29782. #endif
  29783. return EXPECT_RESULT();
  29784. }
  29785. static int test_wc_KeyPemToDer(void)
  29786. {
  29787. EXPECT_DECLS;
  29788. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29789. int ret = 0;
  29790. const byte cert_buf[] = \
  29791. "-----BEGIN PRIVATE KEY-----\n"
  29792. "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDMG5KgWxP002pA\n"
  29793. "QJIdA4H5N0oM1Wf0LrHcos5RYUlrHDkC2b5p2BUpVRPmgDAFD2+8leim98x0BvcB\n"
  29794. "k48TNzrVynuwyVEY664+iQyzEBO5v27HPRydOddprbLCvRO036XINGIjauy1jHFi\n"
  29795. "HaDVx3bexSwgp9aefUGAszFXi4q1J4GacV7Cr2b/wBqUHqWv4ZXPu6R9/UYngTkD\n"
  29796. "UDJL5gLlLfcLzNyyodKPHPCIAKdWn6mSVdcHk8XVpK4y9lgz4E7YDWA6ohKZgWgG\n"
  29797. "2RDha8CMilFMDgYa0G0SiS9g3PQx0qh3AMXJJsKSVhScFCZufAE0kV6KvjP7jAqP\n"
  29798. "XBiSkRGPAgMBAAECggEAW7hmRyY2jRX2UMJThrM9VIs6fRLnYI0dQ0tsEJj536ay\n"
  29799. "nevQjArc05KWW0Yujg+WRDZPcry3RUqd9Djlmhp/F3Si6dpF1b+PMS3wJYVrf9Sd\n"
  29800. "SO5W7faArU4vnyBNe0HnY1Ta5xSVI65lg1RSIs88RTZwsooJwXYDGf0shq0/21CE\n"
  29801. "V8HOb27DDYNcEnm35lzaONjFnMqQQT2Vs9anRrPiSEXNleEvTgLVXZtGTyCGTz6v\n"
  29802. "x86Y8eSWL9YNHvPE1I+mDPuocfSR7eRNgRu7SK3mn94W5mqd7Ns072YKX/2XN1mO\n"
  29803. "66+ZFHO6v4dK1u7cSjuwrU1EhLHpUsgDz6Bna5InyQKBgQDv5l8RPy8UneKSADaf\n"
  29804. "M5L/5675I/5t4nqVjvbnQje00YveLTAEjlJBNR93Biln3sYgnvNamYDCxyEuUZ/I\n"
  29805. "S/vmBL9PoxfGZow4FcsIBOEbIn3E0SYJgCBNWthquUvGpKsYDnThJuhO+1cVmxAJ\n"
  29806. "BUOjLFnJYHM0a+Vmk9GexT2OBwKBgQDZzkUBOK7Im3eiYytFocUJyhqMH30d49X9\n"
  29807. "ujC7kGw4UWAqVe7YCSvlBa8nzWpRWK2kRpu3M0272RU0V4geyWqT+nr/SvRRPtNP\n"
  29808. "F5dY8l3yR7hjtSejqqjOfBcZT6ETJxI4tiG0+Nl5BlfM5M+0nxnkWpRcHuOR3j79\n"
  29809. "YUFERyN+OQKBgQCjlOKeUAc6d65W/+4/AFvsQ378Q57qLtSHxsR1TKHPmlNVXFqx\n"
  29810. "wJo1/JNIBduWCEHxXHF0BdfW+RGXE/FwEt/hKLuLAhrkHmjelX2sKieU6R/5ZOQa\n"
  29811. "9lMQbDHGFDOncAF6leD85hriQGBRSzrT69MDIOrYdfwYcroqCAGX0cb3YQKBgQC8\n"
  29812. "iIFQylj5SyHmjcMSNjKSA8CxFDzAV8yPIdE3Oo+CvGXqn5HsrRuy1hXE9VmXapR8\n"
  29813. "A6ackSszdHiXY0FvrNe1mfdH7wDHJwPQjdIzazCJHS3uGQxj7sDKY7226ie6pXJv\n"
  29814. "ZrCMr2/IBAaSVGm6ppHKCeIsT4ybYm7R85KEYLPHeQKBgBeJOMBinXQfWN/1jT9b\n"
  29815. "6Ywrutvp2zP8hVxQGSZJ0WG4iewZyFLsPUlbWRXOSYNPElHmdD0ZomdLVm+lSpAA\n"
  29816. "XSH5FJ/IFCwqq7Eft6Gf8NFRV+NjPMUny+PnjHe4oFP8YK/Ek22K3ttNG8Hw69Aw\n"
  29817. "AQue5o6oVfhgLiJzMdo/77gw\n"
  29818. "-----END PRIVATE KEY-----\n";
  29819. const int cert_sz = sizeof(cert_buf);
  29820. const char cert_pw[] = "password";
  29821. int cert_dersz = 0;
  29822. byte* cert_der = NULL;
  29823. /* Bad arg: Cert buffer is NULL */
  29824. ExpectIntEQ(wc_KeyPemToDer(NULL, cert_sz, cert_der, cert_dersz, ""),
  29825. BAD_FUNC_ARG);
  29826. /* Bad arg: Cert DER buffer non-NULL but size zero (or less) */
  29827. ExpectIntEQ(wc_KeyPemToDer(cert_buf, cert_sz, (byte*)&cert_der, 0, ""),
  29828. BAD_FUNC_ARG);
  29829. /* Test normal operation */
  29830. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  29831. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29832. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  29833. cert_pw), 0);
  29834. ExpectIntLE(ret, cert_sz);
  29835. if (cert_der != NULL) {
  29836. free(cert_der);
  29837. cert_der = NULL;
  29838. }
  29839. /* Test NULL for DER buffer to return needed DER buffer size */
  29840. ExpectIntGT(ret = wc_KeyPemToDer(cert_buf, cert_sz, NULL, 0, ""), 0);
  29841. ExpectIntLE(ret, cert_sz);
  29842. if (EXPECT_SUCCESS())
  29843. cert_dersz = ret;
  29844. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29845. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  29846. cert_pw), 0);
  29847. ExpectIntLE(ret, cert_sz);
  29848. if (cert_der != NULL)
  29849. free(cert_der);
  29850. #endif
  29851. return EXPECT_RESULT();
  29852. }
  29853. static int test_wc_PubKeyPemToDer(void)
  29854. {
  29855. EXPECT_DECLS;
  29856. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  29857. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  29858. int ret = 0;
  29859. const char* key = "./certs/ecc-client-keyPub.pem";
  29860. byte* cert_buf = NULL;
  29861. size_t cert_sz = 0, cert_dersz = 0;
  29862. byte* cert_der = NULL;
  29863. ExpectIntEQ(wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  29864. cert_der, (int)cert_dersz), BAD_FUNC_ARG);
  29865. ExpectIntEQ(load_file(key, &cert_buf, &cert_sz), 0);
  29866. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  29867. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29868. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  29869. (int)cert_dersz), 0);
  29870. if (cert_der != NULL) {
  29871. free(cert_der);
  29872. cert_der = NULL;
  29873. }
  29874. /* Test NULL for DER buffer to return needed DER buffer size */
  29875. ExpectIntGT(ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, NULL, 0), 0);
  29876. ExpectIntLE(ret, cert_sz);
  29877. cert_dersz = ret;
  29878. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29879. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  29880. (int)cert_dersz), 0);
  29881. if (cert_der != NULL) {
  29882. free(cert_der);
  29883. }
  29884. if (cert_buf != NULL) {
  29885. free(cert_buf);
  29886. }
  29887. #endif
  29888. return EXPECT_RESULT();
  29889. }
  29890. static int test_wc_PemPubKeyToDer(void)
  29891. {
  29892. EXPECT_DECLS;
  29893. #if !defined(NO_FILESYSTEM) && \
  29894. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  29895. const char* key = "./certs/ecc-client-keyPub.pem";
  29896. size_t cert_dersz = 1024;
  29897. byte* cert_der = NULL;
  29898. ExpectIntGE(wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz),
  29899. BAD_FUNC_ARG);
  29900. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29901. ExpectIntGE(wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz), 0);
  29902. if (cert_der != NULL) {
  29903. free(cert_der);
  29904. }
  29905. #endif
  29906. return EXPECT_RESULT();
  29907. }
  29908. static int test_wc_GetPubKeyDerFromCert(void)
  29909. {
  29910. EXPECT_DECLS;
  29911. #if !defined(NO_RSA) || defined(HAVE_ECC)
  29912. int ret;
  29913. word32 idx = 0;
  29914. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  29915. word32 keyDerSz = (word32)sizeof(keyDer);
  29916. DecodedCert decoded;
  29917. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  29918. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  29919. word32 certBufSz = sizeof(certBuf);
  29920. #endif
  29921. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  29922. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  29923. XFILE fp = XBADFILE;
  29924. #endif
  29925. #ifndef NO_RSA
  29926. RsaKey rsaKey;
  29927. #if defined(USE_CERT_BUFFERS_2048)
  29928. byte* rsaCertDer = (byte*)client_cert_der_2048;
  29929. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  29930. #elif defined(USE_CERT_BUFFERS_1024)
  29931. byte* rsaCertDer = (byte*)client_cert_der_1024;
  29932. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  29933. #else
  29934. unsigned char rsaCertDer[TWOK_BUF];
  29935. word32 rsaCertDerSz;
  29936. #endif
  29937. #endif
  29938. #ifdef HAVE_ECC
  29939. ecc_key eccKey;
  29940. #if defined(USE_CERT_BUFFERS_256)
  29941. byte* eccCert = (byte*)cliecc_cert_der_256;
  29942. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  29943. #else
  29944. unsigned char eccCert[ONEK_BUF];
  29945. word32 eccCertSz;
  29946. XFILE fp2 = XBADFILE;
  29947. #endif
  29948. #endif
  29949. #ifndef NO_RSA
  29950. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  29951. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  29952. ExpectIntGT(rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer),
  29953. fp), 0);
  29954. if (fp != XBADFILE) {
  29955. XFCLOSE(fp);
  29956. fp = XBADFILE;
  29957. }
  29958. #endif
  29959. /* good test case - RSA DER cert */
  29960. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  29961. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  29962. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  29963. ExpectIntGT(keyDerSz, 0);
  29964. /* sanity check, verify we can import DER public key */
  29965. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  29966. ExpectIntEQ(ret, 0);
  29967. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  29968. if (ret == 0) {
  29969. wc_FreeRsaKey(&rsaKey);
  29970. }
  29971. /* test LENGTH_ONLY_E case */
  29972. keyDerSz = 0;
  29973. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  29974. LENGTH_ONLY_E);
  29975. ExpectIntGT(keyDerSz, 0);
  29976. /* bad args: DecodedCert NULL */
  29977. ExpectIntEQ(wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz), BAD_FUNC_ARG);
  29978. /* bad args: output key buff size */
  29979. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL), BAD_FUNC_ARG);
  29980. /* bad args: zero size output key buffer */
  29981. keyDerSz = 0;
  29982. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  29983. BAD_FUNC_ARG);
  29984. wc_FreeDecodedCert(&decoded);
  29985. /* Certificate Request Tests */
  29986. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  29987. {
  29988. XMEMSET(certBuf, 0, sizeof(certBuf));
  29989. ExpectTrue((fp = XFOPEN("./certs/csr.signed.der", "rb")) != XBADFILE);
  29990. ExpectIntGT(certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp), 0);
  29991. if (fp != XBADFILE) {
  29992. XFCLOSE(fp);
  29993. }
  29994. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  29995. ExpectIntEQ(wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL), 0);
  29996. /* good test case - RSA DER certificate request */
  29997. keyDerSz = sizeof(keyDer);
  29998. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  29999. 0);
  30000. ExpectIntGT(keyDerSz, 0);
  30001. /* sanity check, verify we can import DER public key */
  30002. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  30003. ExpectIntEQ(ret, 0);
  30004. idx = 0;
  30005. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  30006. if (ret == 0) {
  30007. wc_FreeRsaKey(&rsaKey);
  30008. }
  30009. wc_FreeDecodedCert(&decoded);
  30010. }
  30011. #endif /* WOLFSSL_CERT_REQ */
  30012. #endif /* NO_RSA */
  30013. #ifdef HAVE_ECC
  30014. #ifndef USE_CERT_BUFFERS_256
  30015. ExpectTrue((fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  30016. XBADFILE);
  30017. ExpectIntGT(eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2), 0);
  30018. if (fp2 != XBADFILE) {
  30019. XFCLOSE(fp2);
  30020. }
  30021. #endif
  30022. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  30023. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  30024. /* good test case - ECC */
  30025. XMEMSET(keyDer, 0, sizeof(keyDer));
  30026. keyDerSz = sizeof(keyDer);
  30027. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  30028. ExpectIntGT(keyDerSz, 0);
  30029. /* sanity check, verify we can import DER public key */
  30030. ret = wc_ecc_init(&eccKey);
  30031. ExpectIntEQ(ret, 0);
  30032. idx = 0; /* reset idx to 0, used above in RSA case */
  30033. ExpectIntEQ(wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz), 0);
  30034. if (ret == 0) {
  30035. wc_ecc_free(&eccKey);
  30036. }
  30037. /* test LENGTH_ONLY_E case */
  30038. keyDerSz = 0;
  30039. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  30040. LENGTH_ONLY_E);
  30041. ExpectIntGT(keyDerSz, 0);
  30042. wc_FreeDecodedCert(&decoded);
  30043. #endif
  30044. #endif /* !NO_RSA || HAVE_ECC */
  30045. return EXPECT_RESULT();
  30046. }
  30047. static int test_wc_CheckCertSigPubKey(void)
  30048. {
  30049. EXPECT_DECLS;
  30050. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  30051. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  30052. int ret = 0;
  30053. const char* ca_cert = "./certs/ca-cert.pem";
  30054. byte* cert_buf = NULL;
  30055. size_t cert_sz = 0;
  30056. byte* cert_der = NULL;
  30057. word32 cert_dersz = 0;
  30058. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  30059. word32 keyDerSz = (word32)sizeof(keyDer);
  30060. DecodedCert decoded;
  30061. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  30062. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  30063. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  30064. ExpectIntGE(ret = wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  30065. (int)cert_dersz, CERT_TYPE), 0);
  30066. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  30067. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  30068. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  30069. ExpectIntGT(keyDerSz, 0);
  30070. /* Good test case. */
  30071. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  30072. keyDerSz, RSAk), 0);
  30073. /* No certificate. */
  30074. ExpectIntEQ(wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  30075. ECDSAk), BAD_FUNC_ARG);
  30076. /* Bad cert size. */
  30077. ExpectIntNE(ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  30078. RSAk), 0);
  30079. ExpectTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  30080. /* No public key. */
  30081. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL,
  30082. keyDerSz, RSAk), ASN_NO_SIGNER_E);
  30083. /* Bad public key size. */
  30084. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  30085. RSAk), BAD_FUNC_ARG);
  30086. /* Wrong aglo. */
  30087. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  30088. keyDerSz, ECDSAk), ASN_PARSE_E);
  30089. wc_FreeDecodedCert(&decoded);
  30090. if (cert_der != NULL)
  30091. free(cert_der);
  30092. if (cert_buf != NULL)
  30093. free(cert_buf);
  30094. #endif
  30095. return EXPECT_RESULT();
  30096. }
  30097. static int test_wolfSSL_certs(void)
  30098. {
  30099. EXPECT_DECLS;
  30100. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  30101. !defined(NO_RSA)
  30102. X509* x509ext = NULL;
  30103. #ifdef OPENSSL_ALL
  30104. X509* x509 = NULL;
  30105. WOLFSSL_X509_EXTENSION* ext = NULL;
  30106. ASN1_OBJECT* obj = NULL;
  30107. #endif
  30108. WOLFSSL* ssl = NULL;
  30109. WOLFSSL_CTX* ctx = NULL;
  30110. STACK_OF(ASN1_OBJECT)* sk = NULL;
  30111. ASN1_STRING* asn1_str = NULL;
  30112. AUTHORITY_KEYID* akey = NULL;
  30113. BASIC_CONSTRAINTS* bc = NULL;
  30114. int crit = 0;
  30115. #ifndef NO_WOLFSSL_SERVER
  30116. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  30117. #else
  30118. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  30119. #endif
  30120. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30121. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30122. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  30123. #if !defined(NO_CHECK_PRIVATE_KEY)
  30124. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  30125. #endif
  30126. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30127. #if !defined(NO_CHECK_PRIVATE_KEY)
  30128. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  30129. #endif
  30130. ExpectNotNull(ssl = SSL_new(ctx));
  30131. #if !defined(NO_CHECK_PRIVATE_KEY)
  30132. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30133. #endif
  30134. #ifdef HAVE_PK_CALLBACKS
  30135. ExpectIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  30136. #endif /* HAVE_PK_CALLBACKS */
  30137. /* create and use x509 */
  30138. #ifdef OPENSSL_ALL
  30139. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  30140. WOLFSSL_FILETYPE_PEM));
  30141. #endif
  30142. ExpectNotNull(x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  30143. WOLFSSL_FILETYPE_PEM));
  30144. ExpectIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  30145. #if !defined(NO_CHECK_PRIVATE_KEY)
  30146. /* with loading in a new cert the check on private key should now fail */
  30147. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30148. #endif
  30149. #if defined(USE_CERT_BUFFERS_2048)
  30150. ExpectIntEQ(SSL_use_certificate_ASN1(ssl,
  30151. (unsigned char*)server_cert_der_2048,
  30152. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  30153. #endif
  30154. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  30155. /************* Get Digest of Certificate ******************/
  30156. {
  30157. byte digest[64]; /* max digest size */
  30158. word32 digestSz;
  30159. XMEMSET(digest, 0, sizeof(digest));
  30160. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  30161. WOLFSSL_SUCCESS);
  30162. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  30163. WOLFSSL_SUCCESS);
  30164. ExpectIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  30165. WOLFSSL_FAILURE);
  30166. }
  30167. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  30168. /* test and checkout X509 extensions */
  30169. ExpectNotNull(bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext,
  30170. NID_basic_constraints, &crit, NULL));
  30171. ExpectIntEQ(crit, 0);
  30172. #ifdef OPENSSL_ALL
  30173. ExpectNotNull(ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc));
  30174. X509_EXTENSION_free(ext);
  30175. ext = NULL;
  30176. ExpectNotNull(ext = X509_EXTENSION_new());
  30177. X509_EXTENSION_set_critical(ext, 1);
  30178. ExpectNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  30179. ExpectIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  30180. ASN1_OBJECT_free(obj);
  30181. obj = NULL;
  30182. X509_EXTENSION_free(ext);
  30183. ext = NULL;
  30184. ExpectNotNull(ext = X509_EXTENSION_new());
  30185. X509_EXTENSION_set_critical(ext, 0);
  30186. ExpectIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  30187. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  30188. NULL);
  30189. ExpectIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  30190. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  30191. * and X509_get_ext_d2i has created new */
  30192. asn1_str = NULL;
  30193. X509_EXTENSION_free(ext);
  30194. ext = NULL;
  30195. #endif
  30196. BASIC_CONSTRAINTS_free(bc);
  30197. bc = NULL;
  30198. ExpectNotNull(asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext,
  30199. NID_key_usage, &crit, NULL));
  30200. ExpectIntEQ(crit, 1);
  30201. ExpectIntEQ(asn1_str->type, NID_key_usage);
  30202. #ifdef OPENSSL_ALL
  30203. ExpectNotNull(ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str));
  30204. X509_EXTENSION_free(ext);
  30205. ext = NULL;
  30206. #endif
  30207. ASN1_STRING_free(asn1_str);
  30208. asn1_str = NULL;
  30209. #ifdef OPENSSL_ALL
  30210. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  30211. NID_ext_key_usage, &crit, NULL));
  30212. ExpectNotNull(ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk));
  30213. X509_EXTENSION_free(ext);
  30214. ext = NULL;
  30215. EXTENDED_KEY_USAGE_free(sk);
  30216. sk = NULL;
  30217. #else
  30218. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  30219. &crit, NULL);
  30220. ExpectNull(sk);
  30221. #endif
  30222. ExpectNotNull(akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  30223. NID_authority_key_identifier, &crit, NULL));
  30224. #ifdef OPENSSL_ALL
  30225. ExpectNotNull(ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit,
  30226. akey));
  30227. X509_EXTENSION_free(ext);
  30228. ext = NULL;
  30229. #endif
  30230. wolfSSL_AUTHORITY_KEYID_free(akey);
  30231. akey = NULL;
  30232. /* NID not yet supported */
  30233. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30234. NID_private_key_usage_period, &crit, NULL));
  30235. ExpectIntEQ(crit, -1);
  30236. sk_ASN1_OBJECT_free(sk);
  30237. sk = NULL;
  30238. ExpectNotNull(sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext,
  30239. NID_subject_alt_name, &crit, NULL));
  30240. {
  30241. int i;
  30242. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  30243. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  30244. ExpectIntEQ(gen->type, GEN_DNS);
  30245. ExpectIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  30246. }
  30247. }
  30248. sk_GENERAL_NAME_free(sk);
  30249. sk = NULL;
  30250. /* NID not yet supported */
  30251. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30252. NID_issuer_alt_name, &crit, NULL));
  30253. ExpectIntEQ(crit, -1);
  30254. sk_ASN1_OBJECT_free(sk);
  30255. sk = NULL;
  30256. /* NID not yet supported */
  30257. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30258. NID_info_access, &crit, NULL));
  30259. sk_ASN1_OBJECT_free(sk);
  30260. sk = NULL;
  30261. /* NID not yet supported */
  30262. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30263. NID_sinfo_access, &crit, NULL));
  30264. ExpectIntEQ(crit, -1);
  30265. sk_ASN1_OBJECT_free(sk);
  30266. sk = NULL;
  30267. /* NID not yet supported */
  30268. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30269. NID_name_constraints, &crit, NULL));
  30270. ExpectIntEQ(crit, -1);
  30271. sk_ASN1_OBJECT_free(sk);
  30272. sk = NULL;
  30273. /* no cert policy set */
  30274. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30275. NID_certificate_policies, &crit, NULL));
  30276. sk_ASN1_OBJECT_free(sk);
  30277. sk = NULL;
  30278. /* NID not yet supported */
  30279. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30280. NID_policy_mappings, &crit, NULL));
  30281. ExpectIntEQ(crit, -1);
  30282. sk_ASN1_OBJECT_free(sk);
  30283. sk = NULL;
  30284. /* NID not yet supported */
  30285. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30286. NID_policy_constraints, &crit, NULL));
  30287. ExpectIntEQ(crit, -1);
  30288. sk_ASN1_OBJECT_free(sk);
  30289. sk = NULL;
  30290. /* NID not yet supported */
  30291. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30292. NID_inhibit_any_policy, &crit, NULL));
  30293. ExpectIntEQ(crit, -1);
  30294. sk_ASN1_OBJECT_free(sk);
  30295. sk = NULL;
  30296. /* NID not yet supported */
  30297. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30298. NID_tlsfeature, &crit, NULL));
  30299. ExpectIntEQ(crit, -1);
  30300. sk_ASN1_OBJECT_free(sk);
  30301. sk = NULL;
  30302. /* test invalid cases */
  30303. crit = 0;
  30304. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit,
  30305. NULL));
  30306. ExpectIntEQ(crit, -1);
  30307. /* NULL passed for criticality. */
  30308. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL,
  30309. NID_tlsfeature, NULL, NULL));
  30310. ExpectIntEQ(SSL_get_hit(ssl), 0);
  30311. #ifdef OPENSSL_ALL
  30312. X509_free(x509);
  30313. #endif
  30314. X509_free(x509ext);
  30315. SSL_free(ssl);
  30316. SSL_CTX_free(ctx);
  30317. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  30318. return EXPECT_RESULT();
  30319. }
  30320. static int test_wolfSSL_X509_check_private_key(void)
  30321. {
  30322. EXPECT_DECLS;
  30323. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  30324. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  30325. X509* x509 = NULL;
  30326. EVP_PKEY* pkey = NULL;
  30327. const byte* key;
  30328. /* Check with correct key */
  30329. ExpectNotNull((x509 = X509_load_certificate_file(cliCertFile,
  30330. SSL_FILETYPE_PEM)));
  30331. key = client_key_der_2048;
  30332. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  30333. (long)sizeof_client_key_der_2048));
  30334. ExpectIntEQ(X509_check_private_key(x509, pkey), 1);
  30335. EVP_PKEY_free(pkey);
  30336. pkey = NULL;
  30337. /* Check with wrong key */
  30338. key = server_key_der_2048;
  30339. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  30340. (long)sizeof_server_key_der_2048));
  30341. ExpectIntEQ(X509_check_private_key(x509, pkey), 0);
  30342. /* test for incorrect parameter */
  30343. ExpectIntEQ(X509_check_private_key(NULL, pkey), 0);
  30344. ExpectIntEQ(X509_check_private_key(x509, NULL), 0);
  30345. ExpectIntEQ(X509_check_private_key(NULL, NULL), 0);
  30346. EVP_PKEY_free(pkey);
  30347. X509_free(x509);
  30348. #endif
  30349. return EXPECT_RESULT();
  30350. }
  30351. static int test_wolfSSL_private_keys(void)
  30352. {
  30353. EXPECT_DECLS;
  30354. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30355. !defined(NO_FILESYSTEM)
  30356. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30357. WOLFSSL* ssl = NULL;
  30358. WOLFSSL_CTX* ctx = NULL;
  30359. EVP_PKEY* pkey = NULL;
  30360. OpenSSL_add_all_digests();
  30361. OpenSSL_add_all_algorithms();
  30362. #ifndef NO_RSA
  30363. #ifndef NO_WOLFSSL_SERVER
  30364. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30365. #else
  30366. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30367. #endif
  30368. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  30369. /* Have to load a cert before you can check the private key against that
  30370. * certificates public key! */
  30371. #if !defined(NO_CHECK_PRIVATE_KEY)
  30372. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  30373. #endif
  30374. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  30375. #if !defined(NO_CHECK_PRIVATE_KEY)
  30376. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30377. #endif
  30378. ExpectNotNull(ssl = SSL_new(ctx));
  30379. #if !defined(NO_CHECK_PRIVATE_KEY)
  30380. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30381. #endif
  30382. #ifdef USE_CERT_BUFFERS_2048
  30383. {
  30384. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  30385. unsigned char buf[FOURK_BUF];
  30386. word32 bufSz;
  30387. ExpectIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  30388. (unsigned char*)client_key_der_2048,
  30389. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  30390. #if !defined(NO_CHECK_PRIVATE_KEY)
  30391. /* Should mismatch now that a different private key loaded */
  30392. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30393. #endif
  30394. ExpectIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  30395. (unsigned char*)server_key,
  30396. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  30397. #if !defined(NO_CHECK_PRIVATE_KEY)
  30398. /* After loading back in DER format of original key, should match */
  30399. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30400. #endif
  30401. /* test loading private key to the WOLFSSL_CTX */
  30402. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  30403. (unsigned char*)client_key_der_2048,
  30404. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  30405. #if !defined(NO_CHECK_PRIVATE_KEY)
  30406. /* Should mismatch now that a different private key loaded */
  30407. ExpectIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30408. #endif
  30409. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  30410. (unsigned char*)server_key,
  30411. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  30412. #if !defined(NO_CHECK_PRIVATE_KEY)
  30413. /* After loading back in DER format of original key, should match */
  30414. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30415. #endif
  30416. /* pkey not set yet, expecting to fail */
  30417. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  30418. /* set PKEY and test again */
  30419. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  30420. &server_key, (long)sizeof_server_key_der_2048));
  30421. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  30422. /* reuse PKEY structure and test
  30423. * this should be checked with a memory management sanity checker */
  30424. ExpectFalse(server_key == (const unsigned char*)server_key_der_2048);
  30425. server_key = (const unsigned char*)server_key_der_2048;
  30426. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  30427. &server_key, (long)sizeof_server_key_der_2048));
  30428. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  30429. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  30430. bufSz = FOURK_BUF;
  30431. ExpectIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  30432. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  30433. RSAk, NULL, 0)), 0);
  30434. server_key = (const unsigned char*)buf;
  30435. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  30436. (long)bufSz));
  30437. }
  30438. #endif
  30439. EVP_PKEY_free(pkey);
  30440. pkey = NULL;
  30441. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  30442. ssl = NULL;
  30443. SSL_CTX_free(ctx);
  30444. ctx = NULL;
  30445. #endif /* end of RSA private key match tests */
  30446. #ifdef HAVE_ECC
  30447. #ifndef NO_WOLFSSL_SERVER
  30448. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30449. #else
  30450. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30451. #endif
  30452. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  30453. WOLFSSL_FILETYPE_PEM));
  30454. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  30455. WOLFSSL_FILETYPE_PEM));
  30456. ExpectNotNull(ssl = SSL_new(ctx));
  30457. #if !defined(NO_CHECK_PRIVATE_KEY)
  30458. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30459. #endif
  30460. SSL_free(ssl);
  30461. ssl = NULL;
  30462. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  30463. WOLFSSL_FILETYPE_PEM));
  30464. ExpectNotNull(ssl = SSL_new(ctx));
  30465. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  30466. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30467. #endif
  30468. SSL_free(ssl);
  30469. ssl = NULL;
  30470. SSL_CTX_free(ctx);
  30471. ctx = NULL;
  30472. #endif /* end of ECC private key match tests */
  30473. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  30474. #ifndef NO_WOLFSSL_SERVER
  30475. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30476. #else
  30477. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30478. #endif
  30479. ExpectTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  30480. WOLFSSL_FILETYPE_PEM));
  30481. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  30482. WOLFSSL_FILETYPE_PEM));
  30483. ExpectNotNull(ssl = SSL_new(ctx));
  30484. #if !defined(NO_CHECK_PRIVATE_KEY)
  30485. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30486. #endif
  30487. SSL_free(ssl);
  30488. ssl = NULL;
  30489. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  30490. WOLFSSL_FILETYPE_PEM));
  30491. ExpectNotNull(ssl = SSL_new(ctx));
  30492. #if !defined(NO_CHECK_PRIVATE_KEY)
  30493. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30494. #endif
  30495. SSL_free(ssl);
  30496. ssl = NULL;
  30497. SSL_CTX_free(ctx);
  30498. ctx = NULL;
  30499. #endif /* end of Ed25519 private key match tests */
  30500. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  30501. #ifndef NO_WOLFSSL_SERVER
  30502. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30503. #else
  30504. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30505. #endif
  30506. ExpectTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  30507. WOLFSSL_FILETYPE_PEM));
  30508. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  30509. WOLFSSL_FILETYPE_PEM));
  30510. ExpectNotNull(ssl = SSL_new(ctx));
  30511. #if !defined(NO_CHECK_PRIVATE_KEY)
  30512. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30513. #endif
  30514. SSL_free(ssl);
  30515. ssl = NULL;
  30516. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  30517. WOLFSSL_FILETYPE_PEM));
  30518. ExpectNotNull(ssl = SSL_new(ctx));
  30519. #if !defined(NO_CHECK_PRIVATE_KEY)
  30520. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30521. #endif
  30522. SSL_free(ssl);
  30523. ssl = NULL;
  30524. SSL_CTX_free(ctx);
  30525. ctx = NULL;
  30526. #endif /* end of Ed448 private key match tests */
  30527. EVP_cleanup();
  30528. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  30529. CONF_modules_free();
  30530. ENGINE_cleanup();
  30531. CONF_modules_unload();
  30532. (void)ssl;
  30533. (void)ctx;
  30534. (void)pkey;
  30535. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30536. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  30537. return EXPECT_RESULT();
  30538. }
  30539. static int test_wolfSSL_PEM_def_callback(void)
  30540. {
  30541. EXPECT_DECLS;
  30542. #ifdef OPENSSL_EXTRA
  30543. char buf[10];
  30544. const char* defpwd = "DEF PWD";
  30545. int defpwdLen = (int)XSTRLEN(defpwd);
  30546. int smallLen = 1;
  30547. /* Bad parameters. */
  30548. ExpectIntEQ(wolfSSL_PEM_def_callback(NULL, sizeof(buf), 0, NULL), 0);
  30549. ExpectIntEQ(wolfSSL_PEM_def_callback(NULL, sizeof(buf), 0, (void*)defpwd),
  30550. 0);
  30551. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, sizeof(buf), 0, NULL), 0);
  30552. XMEMSET(buf, 0, sizeof(buf));
  30553. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, sizeof(buf), 0, (void*)defpwd),
  30554. defpwdLen);
  30555. ExpectIntEQ(XMEMCMP(buf, defpwd, defpwdLen), 0);
  30556. ExpectIntEQ(buf[defpwdLen], 0);
  30557. /* Size of buffer is smaller than default password. */
  30558. XMEMSET(buf, 0, sizeof(buf));
  30559. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, smallLen, 0, (void*)defpwd),
  30560. smallLen);
  30561. ExpectIntEQ(XMEMCMP(buf, defpwd, smallLen), 0);
  30562. ExpectIntEQ(buf[smallLen], 0);
  30563. #endif /* OPENSSL_EXTRA */
  30564. return EXPECT_RESULT();
  30565. }
  30566. static int test_wolfSSL_PEM_read_PrivateKey(void)
  30567. {
  30568. EXPECT_DECLS;
  30569. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || \
  30570. !defined(NO_DSA) || defined(HAVE_ECC) || !defined(NO_DH))
  30571. XFILE file = XBADFILE;
  30572. #if !defined(NO_RSA)
  30573. const char* fname_rsa = "./certs/server-key.pem";
  30574. RSA* rsa = NULL;
  30575. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  30576. unsigned char* sig = NULL;
  30577. size_t sigLen = 0;
  30578. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  30579. size_t tbsLen = sizeof(tbs);
  30580. #endif
  30581. #if !defined(NO_DSA)
  30582. const char* fname_dsa = "./certs/dsa2048.pem";
  30583. #endif
  30584. #if defined(HAVE_ECC)
  30585. const char* fname_ec = "./certs/ecc-key.pem";
  30586. #endif
  30587. #if !defined(NO_DH)
  30588. const char* fname_dh = "./certs/dh-priv-2048.pem";
  30589. #endif
  30590. EVP_PKEY* pkey = NULL;
  30591. /* Check error case. */
  30592. ExpectNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  30593. /* not a PEM key. */
  30594. ExpectTrue((file = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  30595. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30596. if (file != XBADFILE)
  30597. XFCLOSE(file);
  30598. file = XBADFILE;
  30599. #ifndef NO_RSA
  30600. /* Read in an RSA key. */
  30601. ExpectTrue((file = XFOPEN(fname_rsa, "rb")) != XBADFILE);
  30602. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30603. if (file != XBADFILE)
  30604. XFCLOSE(file);
  30605. file = XBADFILE;
  30606. /* Make sure the key is usable by signing some data with it. */
  30607. ExpectNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  30608. ExpectIntGT((sigLen = RSA_size(rsa)), 0);
  30609. ExpectNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  30610. DYNAMIC_TYPE_TMP_BUFFER));
  30611. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  30612. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  30613. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  30614. WOLFSSL_SUCCESS);
  30615. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30616. EVP_PKEY_CTX_free(ctx);
  30617. EVP_PKEY_free(pkey);
  30618. pkey = NULL;
  30619. #endif
  30620. #ifndef NO_DSA
  30621. /* Read in a DSA key. */
  30622. ExpectTrue((file = XFOPEN(fname_dsa, "rb")) != XBADFILE);
  30623. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH)
  30624. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30625. EVP_PKEY_free(pkey);
  30626. pkey = NULL;
  30627. #else
  30628. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30629. #endif
  30630. if (file != XBADFILE)
  30631. XFCLOSE(file);
  30632. file = XBADFILE;
  30633. #endif
  30634. #ifdef HAVE_ECC
  30635. /* Read in an EC key. */
  30636. ExpectTrue((file = XFOPEN(fname_ec, "rb")) != XBADFILE);
  30637. ExpectNotNull(pkey = EVP_PKEY_new());
  30638. ExpectPtrEq(PEM_read_PrivateKey(file, &pkey, NULL, NULL), pkey);
  30639. if (file != XBADFILE)
  30640. XFCLOSE(file);
  30641. file = XBADFILE;
  30642. EVP_PKEY_free(pkey);
  30643. pkey = NULL;
  30644. #endif
  30645. #ifndef NO_DH
  30646. /* Read in a DH key. */
  30647. ExpectTrue((file = XFOPEN(fname_dh, "rb")) != XBADFILE);
  30648. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  30649. defined(WOLFSSL_OPENSSH)) && (!defined(HAVE_FIPS) || \
  30650. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  30651. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30652. EVP_PKEY_free(pkey);
  30653. pkey = NULL;
  30654. #else
  30655. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30656. #endif
  30657. if (file != XBADFILE)
  30658. XFCLOSE(file);
  30659. file = XBADFILE;
  30660. #endif
  30661. #endif
  30662. return EXPECT_RESULT();
  30663. }
  30664. static int test_wolfSSL_PEM_read_PUBKEY(void)
  30665. {
  30666. EXPECT_DECLS;
  30667. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  30668. && !defined(NO_FILESYSTEM)
  30669. XFILE file = XBADFILE;
  30670. const char* fname = "./certs/client-keyPub.pem";
  30671. EVP_PKEY* pkey = NULL;
  30672. /* Check error case. */
  30673. ExpectNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  30674. /* Read in an RSA key. */
  30675. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30676. ExpectNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  30677. EVP_PKEY_free(pkey);
  30678. pkey = NULL;
  30679. if (file != XBADFILE)
  30680. XFCLOSE(file);
  30681. file = XBADFILE;
  30682. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30683. ExpectNotNull(pkey = EVP_PKEY_new());
  30684. ExpectPtrEq(PEM_read_PUBKEY(file, &pkey, NULL, NULL), pkey);
  30685. EVP_PKEY_free(pkey);
  30686. if (file != XBADFILE)
  30687. XFCLOSE(file);
  30688. #endif
  30689. return EXPECT_RESULT();
  30690. }
  30691. /* test loading RSA key using BIO */
  30692. static int test_wolfSSL_PEM_PrivateKey_rsa(void)
  30693. {
  30694. EXPECT_DECLS;
  30695. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  30696. defined(USE_CERT_BUFFERS_2048) && !defined(NO_FILESYSTEM) && \
  30697. !defined(NO_BIO)
  30698. BIO* bio = NULL;
  30699. XFILE file = XBADFILE;
  30700. const char* fname = "./certs/server-key.pem";
  30701. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  30702. EVP_PKEY* pkey = NULL;
  30703. size_t sz = 0;
  30704. byte* buf = NULL;
  30705. EVP_PKEY* pkey2 = NULL;
  30706. EVP_PKEY* pkey3 = NULL;
  30707. RSA* rsa_key = NULL;
  30708. #if defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)
  30709. unsigned char extra[10];
  30710. int i;
  30711. BIO* pub_bio = NULL;
  30712. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  30713. #endif
  30714. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30715. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30716. ExpectIntGT(sz = XFTELL(file), 0);
  30717. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30718. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30719. if (buf != NULL) {
  30720. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30721. }
  30722. if (file != XBADFILE) {
  30723. XFCLOSE(file);
  30724. file = XBADFILE;
  30725. }
  30726. /* Test using BIO new mem and loading PEM private key */
  30727. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30728. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30729. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30730. buf = NULL;
  30731. BIO_free(bio);
  30732. bio = NULL;
  30733. /* New empty EVP_PKEY */
  30734. ExpectNotNull(pkey2 = EVP_PKEY_new());
  30735. if (pkey2 != NULL) {
  30736. pkey2->type = EVP_PKEY_RSA;
  30737. }
  30738. /* Test parameter copy */
  30739. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  30740. EVP_PKEY_free(pkey2);
  30741. EVP_PKEY_free(pkey);
  30742. pkey = NULL;
  30743. /* Qt unit test case : rsa pkcs8 key */
  30744. ExpectTrue((file = XFOPEN(fname_rsa_p8, "rb")) != XBADFILE);
  30745. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30746. ExpectIntGT(sz = XFTELL(file), 0);
  30747. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30748. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30749. if (buf) {
  30750. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30751. }
  30752. if (file != XBADFILE) {
  30753. XFCLOSE(file);
  30754. file = XBADFILE;
  30755. }
  30756. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30757. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30758. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30759. buf = NULL;
  30760. BIO_free(bio);
  30761. bio = NULL;
  30762. ExpectNotNull(pkey3 = EVP_PKEY_new());
  30763. ExpectNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  30764. ExpectIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  30765. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30766. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30767. #else
  30768. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30769. #endif
  30770. RSA_free(rsa_key);
  30771. EVP_PKEY_free(pkey3);
  30772. EVP_PKEY_free(pkey);
  30773. pkey = NULL;
  30774. pkey2 = NULL;
  30775. #if defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)
  30776. #define BIO_PEM_TEST_CHAR 'a'
  30777. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  30778. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30779. ExpectIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  30780. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30781. ExpectIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  30782. ExpectNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key,
  30783. (long)sizeof_server_key_der_2048));
  30784. ExpectNull(pkey);
  30785. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  30786. (long)sizeof_server_key_der_2048));
  30787. ExpectIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL, NULL),
  30788. WOLFSSL_FAILURE);
  30789. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL, NULL),
  30790. WOLFSSL_FAILURE);
  30791. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30792. WOLFSSL_SUCCESS);
  30793. ExpectIntGT(BIO_pending(bio), 0);
  30794. ExpectIntEQ(BIO_pending(bio), 1679);
  30795. /* Check if the pubkey API writes only the public key */
  30796. #ifdef WOLFSSL_KEY_GEN
  30797. ExpectIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  30798. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  30799. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  30800. ExpectIntGT(BIO_pending(pub_bio), 0);
  30801. /* Previously both the private key and the pubkey calls would write
  30802. * out the private key and the PEM header was the only difference.
  30803. * The public PEM should be significantly shorter than the
  30804. * private key versison. */
  30805. ExpectIntEQ(BIO_pending(pub_bio), 451);
  30806. #else
  30807. /* Not supported. */
  30808. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), 0);
  30809. #endif
  30810. /* test creating new EVP_PKEY with good args */
  30811. ExpectNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30812. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  30813. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr,
  30814. pkey->pkey_sz), 0);
  30815. }
  30816. /* test of reuse of EVP_PKEY */
  30817. ExpectNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  30818. ExpectIntEQ(BIO_pending(bio), 0);
  30819. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30820. SSL_SUCCESS);
  30821. /* add 10 extra bytes after PEM */
  30822. ExpectIntEQ(BIO_write(bio, extra, 10), 10);
  30823. ExpectNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  30824. ExpectNotNull(pkey);
  30825. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  30826. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr,
  30827. pkey->pkey_sz), 0);
  30828. }
  30829. /* check 10 extra bytes still there */
  30830. ExpectIntEQ(BIO_pending(bio), 10);
  30831. ExpectIntEQ(BIO_read(bio, extra, 10), 10);
  30832. for (i = 0; i < 10; i++) {
  30833. ExpectIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  30834. }
  30835. BIO_free(pub_bio);
  30836. BIO_free(bio);
  30837. bio = NULL;
  30838. EVP_PKEY_free(pkey);
  30839. pkey = NULL;
  30840. EVP_PKEY_free(pkey2);
  30841. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  30842. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 &&
  30843. * !NO_FILESYSTEM && !NO_BIO */
  30844. return EXPECT_RESULT();
  30845. }
  30846. /* test loading ECC key using BIO */
  30847. static int test_wolfSSL_PEM_PrivateKey_ecc(void)
  30848. {
  30849. EXPECT_DECLS;
  30850. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_ECC) && \
  30851. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  30852. BIO* bio = NULL;
  30853. EVP_PKEY* pkey = NULL;
  30854. XFILE file = XBADFILE;
  30855. const char* fname = "./certs/ecc-key.pem";
  30856. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  30857. size_t sz = 0;
  30858. byte* buf = NULL;
  30859. EVP_PKEY* pkey2 = NULL;
  30860. EVP_PKEY* pkey3 = NULL;
  30861. EC_KEY* ec_key = NULL;
  30862. int nid = 0;
  30863. BIO* pub_bio = NULL;
  30864. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30865. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30866. ExpectIntGT(sz = XFTELL(file), 0);
  30867. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30868. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30869. if (buf) {
  30870. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30871. }
  30872. if (file != XBADFILE) {
  30873. XFCLOSE(file);
  30874. file = XBADFILE;
  30875. }
  30876. /* Test using BIO new mem and loading PEM private key */
  30877. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30878. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30879. BIO_free(bio);
  30880. bio = NULL;
  30881. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30882. buf = NULL;
  30883. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30884. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30885. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30886. WOLFSSL_SUCCESS);
  30887. ExpectIntGT(BIO_pending(bio), 0);
  30888. /* No parmeters. */
  30889. ExpectIntEQ(BIO_pending(bio), 227);
  30890. /* Check if the pubkey API writes only the public key */
  30891. #ifdef WOLFSSL_KEY_GEN
  30892. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  30893. ExpectIntGT(BIO_pending(pub_bio), 0);
  30894. /* Previously both the private key and the pubkey calls would write
  30895. * out the private key and the PEM header was the only difference.
  30896. * The public PEM should be significantly shorter than the
  30897. * private key versison. */
  30898. ExpectIntEQ(BIO_pending(pub_bio), 178);
  30899. #endif
  30900. BIO_free(pub_bio);
  30901. BIO_free(bio);
  30902. bio = NULL;
  30903. ExpectNotNull(pkey2 = EVP_PKEY_new());
  30904. ExpectNotNull(pkey3 = EVP_PKEY_new());
  30905. if (pkey2 != NULL) {
  30906. pkey2->type = EVP_PKEY_EC;
  30907. }
  30908. /* Test parameter copy */
  30909. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  30910. /* Qt unit test case 1*/
  30911. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  30912. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  30913. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30914. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30915. #else
  30916. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30917. #endif
  30918. /* Test default digest */
  30919. ExpectIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  30920. ExpectIntEQ(nid, NID_sha256);
  30921. EC_KEY_free(ec_key);
  30922. ec_key = NULL;
  30923. EVP_PKEY_free(pkey3);
  30924. pkey3 = NULL;
  30925. EVP_PKEY_free(pkey2);
  30926. pkey2 = NULL;
  30927. EVP_PKEY_free(pkey);
  30928. pkey = NULL;
  30929. /* Qt unit test case ec pkcs8 key */
  30930. ExpectTrue((file = XFOPEN(fname_ecc_p8, "rb")) != XBADFILE);
  30931. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30932. ExpectIntGT(sz = XFTELL(file), 0);
  30933. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30934. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30935. if (buf) {
  30936. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30937. }
  30938. if (file != XBADFILE) {
  30939. XFCLOSE(file);
  30940. file = XBADFILE;
  30941. }
  30942. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30943. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30944. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30945. buf = NULL;
  30946. BIO_free(bio);
  30947. bio = NULL;
  30948. ExpectNotNull(pkey3 = EVP_PKEY_new());
  30949. /* Qt unit test case */
  30950. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  30951. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  30952. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30953. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30954. #else
  30955. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30956. #endif
  30957. EC_KEY_free(ec_key);
  30958. EVP_PKEY_free(pkey3);
  30959. EVP_PKEY_free(pkey);
  30960. pkey = NULL;
  30961. #endif
  30962. return EXPECT_RESULT();
  30963. }
  30964. /* test loading DSA key using BIO */
  30965. static int test_wolfSSL_PEM_PrivateKey_dsa(void)
  30966. {
  30967. EXPECT_DECLS;
  30968. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_DSA) && \
  30969. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  30970. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
  30971. BIO* bio = NULL;
  30972. EVP_PKEY* pkey = NULL;
  30973. ExpectNotNull(bio = BIO_new_file("./certs/dsa2048.pem", "rb"));
  30974. /* Private DSA EVP_PKEY */
  30975. ExpectNotNull(pkey = wolfSSL_PEM_read_bio_PrivateKey(bio, NULL, NULL,
  30976. NULL));
  30977. BIO_free(bio);
  30978. bio = NULL;
  30979. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30980. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  30981. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  30982. NULL), 0);
  30983. #endif
  30984. #ifdef WOLFSSL_KEY_GEN
  30985. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), 1);
  30986. ExpectIntEQ(BIO_pending(bio), 1178);
  30987. BIO_reset(bio);
  30988. #endif
  30989. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30990. 1);
  30991. ExpectIntEQ(BIO_pending(bio), 1196);
  30992. BIO_free(bio);
  30993. bio = NULL;
  30994. EVP_PKEY_free(pkey);
  30995. pkey = NULL;
  30996. #endif
  30997. #endif
  30998. return EXPECT_RESULT();
  30999. }
  31000. /* test loading DH key using BIO */
  31001. static int test_wolfSSL_PEM_PrivateKey_dh(void)
  31002. {
  31003. EXPECT_DECLS;
  31004. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_DH) && \
  31005. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  31006. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  31007. defined(WOLFSSL_OPENSSH)) && (!defined(HAVE_FIPS) || \
  31008. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  31009. BIO* bio = NULL;
  31010. EVP_PKEY* pkey = NULL;
  31011. ExpectNotNull(bio = BIO_new_file("./certs/dh-priv-2048.pem", "rb"));
  31012. /* Private DH EVP_PKEY */
  31013. ExpectNotNull(pkey = wolfSSL_PEM_read_bio_PrivateKey(bio, NULL, NULL,
  31014. NULL));
  31015. BIO_free(bio);
  31016. bio = NULL;
  31017. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31018. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  31019. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  31020. NULL), 0);
  31021. #endif
  31022. #ifdef WOLFSSL_KEY_GEN
  31023. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), 0);
  31024. #endif
  31025. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  31026. 1);
  31027. ExpectIntEQ(BIO_pending(bio), 806);
  31028. BIO_free(bio);
  31029. bio = NULL;
  31030. EVP_PKEY_free(pkey);
  31031. pkey = NULL;
  31032. #endif
  31033. #endif
  31034. return EXPECT_RESULT();
  31035. }
  31036. static int test_wolfSSL_PEM_PrivateKey(void)
  31037. {
  31038. EXPECT_DECLS;
  31039. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31040. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  31041. #ifndef NO_BIO
  31042. BIO* bio = NULL;
  31043. #endif
  31044. EVP_PKEY* pkey = NULL;
  31045. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  31046. #ifndef NO_BIO
  31047. /* test creating new EVP_PKEY with bad arg */
  31048. ExpectNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  31049. /* Test bad EVP_PKEY type. */
  31050. /* New HMAC EVP_PKEY */
  31051. ExpectNotNull(bio = BIO_new_mem_buf("", 1));
  31052. ExpectNotNull(pkey = EVP_PKEY_new());
  31053. if (pkey != NULL) {
  31054. pkey->type = EVP_PKEY_HMAC;
  31055. }
  31056. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  31057. 0);
  31058. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  31059. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  31060. NULL), 0);
  31061. #endif
  31062. #ifdef WOLFSSL_KEY_GEN
  31063. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), WOLFSSL_FAILURE);
  31064. #endif
  31065. EVP_PKEY_free(pkey);
  31066. pkey = NULL;
  31067. BIO_free(bio);
  31068. bio = NULL;
  31069. /* key is DES encrypted */
  31070. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  31071. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  31072. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  31073. {
  31074. XFILE f = XBADFILE;
  31075. wc_pem_password_cb* passwd_cb = NULL;
  31076. void* passwd_cb_userdata;
  31077. SSL_CTX* ctx = NULL;
  31078. char passwd[] = "bad password";
  31079. #ifndef WOLFSSL_NO_TLS12
  31080. #ifndef NO_WOLFSSL_SERVER
  31081. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  31082. #else
  31083. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  31084. #endif
  31085. #else
  31086. #ifndef NO_WOLFSSL_SERVER
  31087. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  31088. #else
  31089. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  31090. #endif
  31091. #endif
  31092. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  31093. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  31094. ExpectNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  31095. ExpectNull(passwd_cb_userdata =
  31096. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  31097. /* fail case with password call back */
  31098. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  31099. (void*)passwd));
  31100. BIO_free(bio);
  31101. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  31102. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  31103. (void*)passwd));
  31104. BIO_free(bio);
  31105. ExpectTrue((f = XFOPEN("./certs/server-keyEnc.pem", "rb")) != XBADFILE);
  31106. ExpectNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  31107. if ((bio == NULL) && (f != XBADFILE)) {
  31108. XFCLOSE(f);
  31109. }
  31110. /* use callback that works */
  31111. ExpectNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  31112. (void*)"yassl123"));
  31113. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  31114. EVP_PKEY_free(pkey);
  31115. pkey = NULL;
  31116. BIO_free(bio);
  31117. bio = NULL;
  31118. SSL_CTX_free(ctx);
  31119. }
  31120. #endif /* !defined(NO_DES3) */
  31121. #endif /* !NO_BIO */
  31122. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  31123. {
  31124. unsigned char buf[2048];
  31125. size_t bytes = 0;
  31126. XFILE f = XBADFILE;
  31127. SSL_CTX* ctx = NULL;
  31128. #ifndef WOLFSSL_NO_TLS12
  31129. #ifndef NO_WOLFSSL_SERVER
  31130. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  31131. #else
  31132. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  31133. #endif
  31134. #else
  31135. #ifndef NO_WOLFSSL_SERVER
  31136. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  31137. #else
  31138. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  31139. #endif
  31140. #endif
  31141. ExpectTrue((f = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  31142. ExpectIntGT(bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f), 0);
  31143. if (f != XBADFILE)
  31144. XFCLOSE(f);
  31145. server_key = buf;
  31146. pkey = NULL;
  31147. ExpectNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  31148. ExpectNull(pkey);
  31149. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  31150. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  31151. EVP_PKEY_free(pkey);
  31152. pkey = NULL;
  31153. SSL_CTX_free(ctx);
  31154. server_key = NULL;
  31155. }
  31156. #endif
  31157. #ifndef NO_BIO
  31158. (void)bio;
  31159. #endif
  31160. (void)pkey;
  31161. (void)server_key;
  31162. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  31163. return EXPECT_RESULT();
  31164. }
  31165. static int test_wolfSSL_PEM_file_RSAKey(void)
  31166. {
  31167. EXPECT_DECLS;
  31168. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31169. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  31170. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  31171. RSA* rsa = NULL;
  31172. XFILE fp = XBADFILE;
  31173. ExpectTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  31174. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  31175. if (fp != XBADFILE)
  31176. XFCLOSE(fp);
  31177. ExpectIntEQ(RSA_size(rsa), 256);
  31178. ExpectIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  31179. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
  31180. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
  31181. ExpectIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  31182. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  31183. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
  31184. RSA_free(rsa);
  31185. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31186. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  31187. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  31188. return EXPECT_RESULT();
  31189. }
  31190. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  31191. {
  31192. EXPECT_DECLS;
  31193. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  31194. !defined(NO_FILESYSTEM) && \
  31195. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  31196. RSA* rsa = NULL;
  31197. XFILE f = NULL;
  31198. ExpectTrue((f = XFOPEN(svrKeyFile, "r")) != XBADFILE);
  31199. ExpectNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  31200. ExpectIntEQ(RSA_size(rsa), 256);
  31201. if (f != XBADFILE) {
  31202. XFCLOSE(f);
  31203. f = XBADFILE;
  31204. }
  31205. ExpectIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  31206. NULL), WOLFSSL_FAILURE);
  31207. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  31208. NULL), WOLFSSL_FAILURE);
  31209. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
  31210. WOLFSSL_SUCCESS);
  31211. RSA_free(rsa);
  31212. #ifdef HAVE_ECC
  31213. ExpectTrue((f = XFOPEN(eccKeyFile, "r")) != XBADFILE);
  31214. ExpectNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  31215. if (f != XBADFILE)
  31216. XFCLOSE(f);
  31217. #endif /* HAVE_ECC */
  31218. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31219. return EXPECT_RESULT();
  31220. }
  31221. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  31222. {
  31223. EXPECT_DECLS;
  31224. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31225. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31226. XFILE file = XBADFILE;
  31227. const char* fname = "./certs/client-keyPub.pem";
  31228. RSA *rsa = NULL;
  31229. ExpectNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  31230. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  31231. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  31232. ExpectIntEQ(RSA_size(rsa), 256);
  31233. RSA_free(rsa);
  31234. if (file != XBADFILE)
  31235. XFCLOSE(file);
  31236. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31237. return EXPECT_RESULT();
  31238. }
  31239. #ifndef NO_BIO
  31240. static int test_wolfSSL_PEM_bio_RSAKey(void)
  31241. {
  31242. EXPECT_DECLS;
  31243. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31244. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  31245. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  31246. RSA* rsa = NULL;
  31247. BIO* bio = NULL;
  31248. /* PrivateKey */
  31249. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  31250. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  31251. ExpectNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  31252. ExpectNotNull(rsa);
  31253. ExpectIntEQ(RSA_size(rsa), 256);
  31254. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  31255. NULL), WOLFSSL_FAILURE);
  31256. BIO_free(bio);
  31257. bio = NULL;
  31258. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31259. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  31260. NULL), WOLFSSL_SUCCESS);
  31261. BIO_free(bio);
  31262. bio = NULL;
  31263. RSA_free(rsa);
  31264. rsa = NULL;
  31265. /* PUBKEY */
  31266. ExpectNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  31267. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  31268. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31269. ExpectIntEQ(RSA_size(rsa), 256);
  31270. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31271. BIO_free(bio);
  31272. bio = NULL;
  31273. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31274. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  31275. BIO_free(bio);
  31276. bio = NULL;
  31277. RSA_free(rsa);
  31278. rsa = NULL;
  31279. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  31280. ExpectNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  31281. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31282. BIO_free(bio);
  31283. bio = NULL;
  31284. RSA_free(rsa);
  31285. rsa = NULL;
  31286. #ifdef HAVE_ECC
  31287. /* ensure that non-rsa keys do not work */
  31288. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  31289. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31290. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31291. BIO_free(bio);
  31292. bio = NULL;
  31293. RSA_free(rsa);
  31294. rsa = NULL;
  31295. #endif /* HAVE_ECC */
  31296. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31297. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  31298. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  31299. return EXPECT_RESULT();
  31300. }
  31301. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  31302. {
  31303. EXPECT_DECLS;
  31304. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31305. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31306. RSA* rsa = NULL;
  31307. RSA* rsa_dup = NULL;
  31308. BIO* bio = NULL;
  31309. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  31310. ExpectNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31311. ExpectIntEQ(RSA_size(rsa), 256);
  31312. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  31313. ExpectNull(rsa_dup = RSAPublicKey_dup(NULL));
  31314. /* Test duplicating empty key. */
  31315. ExpectNotNull(rsa_dup = RSA_new());
  31316. ExpectNull(RSAPublicKey_dup(rsa_dup));
  31317. RSA_free(rsa_dup);
  31318. rsa_dup = NULL;
  31319. ExpectNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  31320. ExpectPtrNE(rsa_dup, rsa);
  31321. #endif
  31322. /* test if valgrind complains about unreleased memory */
  31323. RSA_up_ref(rsa);
  31324. RSA_free(rsa);
  31325. BIO_free(bio);
  31326. bio = NULL;
  31327. RSA_free(rsa);
  31328. rsa = NULL;
  31329. RSA_free(rsa_dup);
  31330. rsa_dup = NULL;
  31331. #ifdef HAVE_ECC
  31332. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  31333. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31334. BIO_free(bio);
  31335. #endif /* HAVE_ECC */
  31336. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31337. return EXPECT_RESULT();
  31338. }
  31339. static int test_wolfSSL_PEM_bio_DSAKey(void)
  31340. {
  31341. EXPECT_DECLS;
  31342. #ifndef HAVE_SELFTEST
  31343. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  31344. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  31345. DSA* dsa = NULL;
  31346. BIO* bio = NULL;
  31347. /* PrivateKey */
  31348. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  31349. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  31350. ExpectNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  31351. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  31352. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  31353. NULL), WOLFSSL_FAILURE);
  31354. BIO_free(bio);
  31355. bio = NULL;
  31356. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31357. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL,
  31358. NULL), WOLFSSL_SUCCESS);
  31359. BIO_free(bio);
  31360. bio = NULL;
  31361. DSA_free(dsa);
  31362. dsa = NULL;
  31363. /* PUBKEY */
  31364. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  31365. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  31366. ExpectNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  31367. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  31368. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31369. BIO_free(bio);
  31370. bio = NULL;
  31371. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31372. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  31373. BIO_free(bio);
  31374. bio = NULL;
  31375. DSA_free(dsa);
  31376. dsa = NULL;
  31377. #ifdef HAVE_ECC
  31378. /* ensure that non-dsa keys do not work */
  31379. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  31380. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  31381. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  31382. BIO_free(bio);
  31383. bio = NULL;
  31384. DSA_free(dsa);
  31385. dsa = NULL;
  31386. #endif /* HAVE_ECC */
  31387. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  31388. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  31389. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  31390. #endif /* HAVE_SELFTEST */
  31391. return EXPECT_RESULT();
  31392. }
  31393. static int test_wolfSSL_PEM_bio_ECKey(void)
  31394. {
  31395. EXPECT_DECLS;
  31396. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31397. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  31398. EC_KEY* ec = NULL;
  31399. EC_KEY* ec2;
  31400. BIO* bio = NULL;
  31401. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  31402. unsigned char* pem = NULL;
  31403. int pLen;
  31404. #endif
  31405. static char ec_key_bad_1[] = "-----BEGIN PUBLIC KEY-----\n"
  31406. "MAA=\n"
  31407. "-----END PUBLIC KEY-----";
  31408. static char ec_priv_key_bad_1[] = "-----BEGIN EC PRIVATE KEY-----\n"
  31409. "MAA=\n"
  31410. "-----END EC PRIVATE KEY-----";
  31411. /* PrivateKey */
  31412. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  31413. ExpectNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  31414. ec2 = NULL;
  31415. ExpectNotNull((ec = PEM_read_bio_ECPrivateKey(bio, &ec2, NULL, NULL)));
  31416. ExpectIntEQ(ec == ec2, 1);
  31417. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  31418. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  31419. NULL), WOLFSSL_FAILURE);
  31420. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  31421. NULL), WOLFSSL_FAILURE);
  31422. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, ec, NULL, NULL, 0, NULL, NULL),
  31423. WOLFSSL_FAILURE);
  31424. BIO_free(bio);
  31425. bio = NULL;
  31426. /* Public key data - fail. */
  31427. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  31428. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  31429. BIO_free(bio);
  31430. bio = NULL;
  31431. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31432. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  31433. NULL), WOLFSSL_SUCCESS);
  31434. BIO_free(bio);
  31435. bio = NULL;
  31436. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, NULL, NULL, NULL, 0, NULL,
  31437. NULL),WOLFSSL_FAILURE);
  31438. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, NULL, NULL, NULL, 0, NULL, NULL),
  31439. WOLFSSL_FAILURE);
  31440. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, ec, NULL, NULL, 0, NULL, NULL),
  31441. WOLFSSL_FAILURE);
  31442. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, ec, NULL, NULL, 0, NULL, NULL),
  31443. WOLFSSL_SUCCESS);
  31444. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  31445. NULL), 0);
  31446. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  31447. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  31448. NULL), 0);
  31449. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  31450. NULL), 0);
  31451. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  31452. &pLen), 0);
  31453. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  31454. &pLen), 0);
  31455. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  31456. &pLen), 0);
  31457. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  31458. NULL), 0);
  31459. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  31460. &pLen), 1);
  31461. ExpectIntGT(pLen, 0);
  31462. XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  31463. #endif
  31464. EC_KEY_free(ec);
  31465. ec = NULL;
  31466. /* PUBKEY */
  31467. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  31468. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  31469. ec2 = NULL;
  31470. ExpectNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, &ec2, NULL, NULL)));
  31471. ExpectIntEQ(ec == ec2, 1);
  31472. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  31473. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31474. BIO_free(bio);
  31475. bio = NULL;
  31476. /* Test 0x30, 0x00 fails. */
  31477. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_key_bad_1,
  31478. sizeof(ec_key_bad_1)));
  31479. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  31480. BIO_free(bio);
  31481. bio = NULL;
  31482. /* Private key data - fail. */
  31483. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  31484. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  31485. BIO_free(bio);
  31486. bio = NULL;
  31487. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31488. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  31489. BIO_free(bio);
  31490. bio = NULL;
  31491. /* Same test as above, but with a file pointer rather than a BIO. */
  31492. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31493. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  31494. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  31495. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
  31496. EC_KEY_free(ec);
  31497. ec = NULL;
  31498. #ifndef NO_RSA
  31499. /* ensure that non-ec keys do not work */
  31500. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  31501. ExpectNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  31502. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  31503. BIO_free(bio);
  31504. bio = NULL;
  31505. EC_KEY_free(ec);
  31506. ec = NULL;
  31507. #endif /* !NO_RSA */
  31508. /* Test 0x30, 0x00 fails. */
  31509. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_priv_key_bad_1,
  31510. sizeof(ec_priv_key_bad_1)));
  31511. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  31512. BIO_free(bio);
  31513. bio = NULL;
  31514. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31515. return EXPECT_RESULT();
  31516. }
  31517. static int test_wolfSSL_PEM_PUBKEY(void)
  31518. {
  31519. EXPECT_DECLS;
  31520. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  31521. BIO* bio = NULL;
  31522. EVP_PKEY* pkey = NULL;
  31523. /* test creating new EVP_PKEY with bad arg */
  31524. ExpectNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  31525. /* test loading ECC key using BIO */
  31526. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  31527. {
  31528. XFILE file = XBADFILE;
  31529. const char* fname = "./certs/ecc-client-keyPub.pem";
  31530. size_t sz = 0;
  31531. byte* buf = NULL;
  31532. EVP_PKEY* pkey2 = NULL;
  31533. EC_KEY* ec_key = NULL;
  31534. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  31535. ExpectIntEQ(XFSEEK(file, 0, XSEEK_END), 0);
  31536. ExpectIntGT(sz = XFTELL(file), 0);
  31537. ExpectIntEQ(XFSEEK(file, 0, XSEEK_SET), 0);
  31538. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  31539. if (buf != NULL) {
  31540. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  31541. }
  31542. if (file != XBADFILE) {
  31543. XFCLOSE(file);
  31544. }
  31545. /* Test using BIO new mem and loading PEM private key */
  31546. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31547. ExpectNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  31548. BIO_free(bio);
  31549. bio = NULL;
  31550. EVP_PKEY_free(pkey);
  31551. pkey = NULL;
  31552. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31553. ExpectNotNull(pkey = EVP_PKEY_new());
  31554. ExpectPtrEq(PEM_read_bio_PUBKEY(bio, &pkey, NULL, NULL), pkey);
  31555. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  31556. BIO_free(bio);
  31557. bio = NULL;
  31558. /* Qt unit test case*/
  31559. ExpectNotNull(pkey2 = EVP_PKEY_new());
  31560. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  31561. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  31562. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31563. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  31564. #else
  31565. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  31566. #endif
  31567. EC_KEY_free(ec_key);
  31568. EVP_PKEY_free(pkey2);
  31569. EVP_PKEY_free(pkey);
  31570. pkey = NULL;
  31571. }
  31572. #endif
  31573. (void)bio;
  31574. (void)pkey;
  31575. #endif
  31576. return EXPECT_RESULT();
  31577. }
  31578. #endif /* !NO_BIO */
  31579. static int test_DSA_do_sign_verify(void)
  31580. {
  31581. EXPECT_DECLS;
  31582. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  31583. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  31584. !defined(NO_DSA)
  31585. unsigned char digest[WC_SHA_DIGEST_SIZE];
  31586. DSA_SIG* sig = NULL;
  31587. DSA* dsa = NULL;
  31588. word32 bytes;
  31589. byte sigBin[DSA_SIG_SIZE];
  31590. int dsacheck;
  31591. #ifdef USE_CERT_BUFFERS_1024
  31592. byte tmp[ONEK_BUF];
  31593. XMEMSET(tmp, 0, sizeof(tmp));
  31594. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  31595. bytes = sizeof_dsa_key_der_1024;
  31596. #elif defined(USE_CERT_BUFFERS_2048)
  31597. byte tmp[TWOK_BUF];
  31598. XMEMSET(tmp, 0, sizeof(tmp));
  31599. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  31600. bytes = sizeof_dsa_key_der_2048;
  31601. #else
  31602. byte tmp[TWOK_BUF];
  31603. XFILE fp = XBADFILE;
  31604. XMEMSET(tmp, 0, sizeof(tmp));
  31605. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb") != XBADFILE);
  31606. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  31607. if (fp != XBADFILE)
  31608. XFCLOSE(fp);
  31609. #endif /* END USE_CERT_BUFFERS_1024 */
  31610. XMEMSET(digest, 202, sizeof(digest));
  31611. ExpectNotNull(dsa = DSA_new());
  31612. ExpectIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  31613. ExpectIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  31614. ExpectIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  31615. ExpectNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  31616. ExpectIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  31617. DSA_SIG_free(sig);
  31618. DSA_free(dsa);
  31619. #endif
  31620. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  31621. return EXPECT_RESULT();
  31622. }
  31623. static int test_wolfSSL_tmp_dh(void)
  31624. {
  31625. EXPECT_DECLS;
  31626. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  31627. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  31628. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31629. byte buff[6000];
  31630. char file[] = "./certs/dsaparams.pem";
  31631. XFILE f = XBADFILE;
  31632. int bytes = 0;
  31633. DSA* dsa = NULL;
  31634. DH* dh = NULL;
  31635. #if defined(WOLFSSL_DH_EXTRA) && \
  31636. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31637. DH* dh2 = NULL;
  31638. #endif
  31639. BIO* bio = NULL;
  31640. SSL* ssl = NULL;
  31641. SSL_CTX* ctx = NULL;
  31642. #ifndef NO_WOLFSSL_SERVER
  31643. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31644. #else
  31645. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31646. #endif
  31647. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31648. WOLFSSL_FILETYPE_PEM));
  31649. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  31650. WOLFSSL_FILETYPE_PEM));
  31651. ExpectNotNull(ssl = SSL_new(ctx));
  31652. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  31653. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  31654. if (f != XBADFILE)
  31655. XFCLOSE(f);
  31656. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  31657. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  31658. ExpectNotNull(dsa);
  31659. dh = wolfSSL_DSA_dup_DH(dsa);
  31660. ExpectNotNull(dh);
  31661. #if defined(WOLFSSL_DH_EXTRA) && \
  31662. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31663. ExpectNotNull(dh2 = wolfSSL_DH_dup(dh));
  31664. #endif
  31665. ExpectIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  31666. #ifndef NO_WOLFSSL_SERVER
  31667. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  31668. #else
  31669. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  31670. #endif
  31671. BIO_free(bio);
  31672. DSA_free(dsa);
  31673. DH_free(dh);
  31674. dh = NULL;
  31675. #if defined(WOLFSSL_DH_EXTRA) && \
  31676. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31677. DH_free(dh2);
  31678. dh2 = NULL;
  31679. #endif
  31680. SSL_free(ssl);
  31681. SSL_CTX_free(ctx);
  31682. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31683. #endif
  31684. return EXPECT_RESULT();
  31685. }
  31686. static int test_wolfSSL_ctrl(void)
  31687. {
  31688. EXPECT_DECLS;
  31689. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  31690. byte buff[6000];
  31691. BIO* bio = NULL;
  31692. int bytes;
  31693. BUF_MEM* ptr = NULL;
  31694. XMEMSET(buff, 0, sizeof(buff));
  31695. bytes = sizeof(buff);
  31696. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  31697. ExpectNotNull(BIO_s_socket());
  31698. ExpectIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  31699. /* needs tested after stubs filled out @TODO
  31700. SSL_ctrl
  31701. SSL_CTX_ctrl
  31702. */
  31703. BIO_free(bio);
  31704. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  31705. return EXPECT_RESULT();
  31706. }
  31707. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  31708. {
  31709. EXPECT_DECLS;
  31710. #ifdef OPENSSL_EXTRA
  31711. static const unsigned char pw[] = "password";
  31712. static const int pwSz = sizeof(pw) - 1;
  31713. size_t checkPwSz = 0;
  31714. const unsigned char* checkPw = NULL;
  31715. WOLFSSL_EVP_PKEY* key = NULL;
  31716. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  31717. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  31718. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  31719. pwSz));
  31720. if (key != NULL) {
  31721. ExpectIntEQ(key->type, EVP_PKEY_HMAC);
  31722. ExpectIntEQ(key->save_type, EVP_PKEY_HMAC);
  31723. ExpectIntEQ(key->pkey_sz, pwSz);
  31724. ExpectIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  31725. }
  31726. ExpectNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  31727. ExpectIntEQ((int)checkPwSz, pwSz);
  31728. ExpectIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  31729. wolfSSL_EVP_PKEY_free(key);
  31730. key = NULL;
  31731. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  31732. 0));
  31733. ExpectIntEQ(key->pkey_sz, 0);
  31734. if (EXPECT_SUCCESS()) {
  31735. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  31736. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  31737. }
  31738. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  31739. ExpectIntEQ((int)checkPwSz, 0);
  31740. wolfSSL_EVP_PKEY_free(key);
  31741. key = NULL;
  31742. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL,
  31743. 0));
  31744. ExpectIntEQ(key->pkey_sz, 0);
  31745. if (EXPECT_SUCCESS()) {
  31746. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  31747. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  31748. }
  31749. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  31750. ExpectIntEQ((int)checkPwSz, 0);
  31751. wolfSSL_EVP_PKEY_free(key);
  31752. key = NULL;
  31753. #endif /* OPENSSL_EXTRA */
  31754. return EXPECT_RESULT();
  31755. }
  31756. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  31757. {
  31758. EXPECT_DECLS;
  31759. #ifdef OPENSSL_EXTRA
  31760. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  31761. const char *priv = "ABCDEFGHIJKLMNOP";
  31762. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  31763. WOLFSSL_EVP_PKEY* key = NULL;
  31764. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31765. NULL, NULL, AES_128_KEY_SIZE, cipher));
  31766. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31767. NULL, (const unsigned char *)priv, 0, cipher));
  31768. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31769. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  31770. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31771. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  31772. wolfSSL_EVP_PKEY_free(key);
  31773. #endif /* WOLFSSL_CMAC && !NO_AES && WOLFSSL_AES_DIRECT */
  31774. #endif /* OPENSSL_EXTRA */
  31775. return EXPECT_RESULT();
  31776. }
  31777. static int test_wolfSSL_EVP_Digest(void)
  31778. {
  31779. EXPECT_DECLS;
  31780. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  31781. const char* in = "abc";
  31782. int inLen = (int)XSTRLEN(in);
  31783. byte out[WC_SHA256_DIGEST_SIZE];
  31784. unsigned int outLen;
  31785. const char* expOut =
  31786. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  31787. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  31788. "\x15\xAD";
  31789. ExpectIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen,
  31790. "SHA256", NULL), 1);
  31791. ExpectIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  31792. ExpectIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  31793. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  31794. return EXPECT_RESULT();
  31795. }
  31796. static int test_wolfSSL_EVP_Digest_all(void)
  31797. {
  31798. EXPECT_DECLS;
  31799. #ifdef OPENSSL_EXTRA
  31800. const char* digests[] = {
  31801. #ifndef NO_MD5
  31802. "MD5",
  31803. #endif
  31804. #ifndef NO_SHA
  31805. "SHA",
  31806. #endif
  31807. #ifdef WOLFSSL_SHA224
  31808. "SHA224",
  31809. #endif
  31810. #ifndef NO_SHA256
  31811. "SHA256",
  31812. #endif
  31813. #ifdef WOLFSSL_SHA384
  31814. "SHA384",
  31815. #endif
  31816. #ifdef WOLFSSL_SHA512
  31817. "SHA512",
  31818. #endif
  31819. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  31820. "SHA512_224",
  31821. #endif
  31822. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  31823. "SHA512_256",
  31824. #endif
  31825. #ifdef WOLFSSL_SHA3
  31826. #ifndef WOLFSSL_NOSHA3_224
  31827. "SHA3_224",
  31828. #endif
  31829. #ifndef WOLFSSL_NOSHA3_256
  31830. "SHA3_256",
  31831. #endif
  31832. "SHA3_384",
  31833. #ifndef WOLFSSL_NOSHA3_512
  31834. "SHA3_512",
  31835. #endif
  31836. #endif /* WOLFSSL_SHA3 */
  31837. NULL
  31838. };
  31839. const char** d;
  31840. const unsigned char in[] = "abc";
  31841. int inLen = XSTR_SIZEOF(in);
  31842. byte out[WC_MAX_DIGEST_SIZE];
  31843. unsigned int outLen;
  31844. for (d = digests; *d != NULL; d++) {
  31845. ExpectIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  31846. ExpectIntGT(outLen, 0);
  31847. ExpectIntEQ(EVP_MD_size(*d), outLen);
  31848. }
  31849. #endif
  31850. return EXPECT_RESULT();
  31851. }
  31852. static int test_wolfSSL_EVP_MD_size(void)
  31853. {
  31854. EXPECT_DECLS;
  31855. #ifdef OPENSSL_EXTRA
  31856. WOLFSSL_EVP_MD_CTX mdCtx;
  31857. #ifdef WOLFSSL_SHA3
  31858. #ifndef WOLFSSL_NOSHA3_224
  31859. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31860. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  31861. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  31862. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  31863. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31864. #endif
  31865. #ifndef WOLFSSL_NOSHA3_256
  31866. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31867. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  31868. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  31869. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  31870. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31871. #endif
  31872. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31873. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  31874. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  31875. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  31876. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31877. #ifndef WOLFSSL_NOSHA3_512
  31878. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31879. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  31880. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  31881. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  31882. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31883. #endif
  31884. #endif /* WOLFSSL_SHA3 */
  31885. #ifndef NO_SHA256
  31886. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31887. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  31888. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31889. WC_SHA256_DIGEST_SIZE);
  31890. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31891. WC_SHA256_BLOCK_SIZE);
  31892. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  31893. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  31894. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31895. #endif
  31896. #ifndef NO_MD5
  31897. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31898. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  31899. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31900. WC_MD5_DIGEST_SIZE);
  31901. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31902. WC_MD5_BLOCK_SIZE);
  31903. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  31904. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  31905. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31906. #endif
  31907. #ifdef WOLFSSL_SHA224
  31908. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31909. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  31910. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31911. WC_SHA224_DIGEST_SIZE);
  31912. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31913. WC_SHA224_BLOCK_SIZE);
  31914. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  31915. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  31916. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31917. #endif
  31918. #ifdef WOLFSSL_SHA384
  31919. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31920. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  31921. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31922. WC_SHA384_DIGEST_SIZE);
  31923. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31924. WC_SHA384_BLOCK_SIZE);
  31925. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  31926. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  31927. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31928. #endif
  31929. #ifdef WOLFSSL_SHA512
  31930. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31931. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  31932. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31933. WC_SHA512_DIGEST_SIZE);
  31934. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31935. WC_SHA512_BLOCK_SIZE);
  31936. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  31937. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  31938. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31939. #endif
  31940. #ifndef NO_SHA
  31941. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31942. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  31943. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31944. WC_SHA_DIGEST_SIZE);
  31945. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31946. WC_SHA_BLOCK_SIZE);
  31947. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  31948. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  31949. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31950. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31951. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  31952. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31953. WC_SHA_DIGEST_SIZE);
  31954. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31955. WC_SHA_BLOCK_SIZE);
  31956. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  31957. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  31958. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31959. #endif
  31960. /* error case */
  31961. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31962. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  31963. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31964. BAD_FUNC_ARG);
  31965. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  31966. /* Cleanup is valid on uninit'ed struct */
  31967. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31968. #endif /* OPENSSL_EXTRA */
  31969. return EXPECT_RESULT();
  31970. }
  31971. static int test_wolfSSL_EVP_MD_pkey_type(void)
  31972. {
  31973. EXPECT_DECLS;
  31974. #ifdef OPENSSL_EXTRA
  31975. const WOLFSSL_EVP_MD* md;
  31976. #ifndef NO_MD5
  31977. ExpectNotNull(md = EVP_md5());
  31978. ExpectIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  31979. #endif
  31980. #ifndef NO_SHA
  31981. ExpectNotNull(md = EVP_sha1());
  31982. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  31983. #endif
  31984. #ifdef WOLFSSL_SHA224
  31985. ExpectNotNull(md = EVP_sha224());
  31986. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  31987. #endif
  31988. ExpectNotNull(md = EVP_sha256());
  31989. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  31990. #ifdef WOLFSSL_SHA384
  31991. ExpectNotNull(md = EVP_sha384());
  31992. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  31993. #endif
  31994. #ifdef WOLFSSL_SHA512
  31995. ExpectNotNull(md = EVP_sha512());
  31996. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  31997. #endif
  31998. #endif
  31999. return EXPECT_RESULT();
  32000. }
  32001. #ifdef OPENSSL_EXTRA
  32002. static int test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  32003. size_t testKeySz, const char* testData, size_t testDataSz,
  32004. const byte* testResult, size_t testResultSz)
  32005. {
  32006. EXPECT_DECLS;
  32007. unsigned char check[WC_MAX_DIGEST_SIZE];
  32008. size_t checkSz = -1;
  32009. WOLFSSL_EVP_PKEY* key = NULL;
  32010. WOLFSSL_EVP_MD_CTX mdCtx;
  32011. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  32012. testKey, (int)testKeySz));
  32013. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32014. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  32015. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32016. (unsigned int)testDataSz), 1);
  32017. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32018. ExpectIntEQ((int)checkSz, (int)testResultSz);
  32019. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32020. ExpectIntEQ((int)checkSz,(int)testResultSz);
  32021. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  32022. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32023. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  32024. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32025. (unsigned int)testDataSz), 1);
  32026. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  32027. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32028. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32029. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  32030. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  32031. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32032. ExpectIntEQ((int)checkSz, (int)testResultSz);
  32033. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32034. ExpectIntEQ((int)checkSz,(int)testResultSz);
  32035. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  32036. (unsigned int)testDataSz - 4), 1);
  32037. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32038. ExpectIntEQ((int)checkSz,(int)testResultSz);
  32039. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  32040. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32041. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  32042. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  32043. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  32044. (unsigned int)testDataSz - 4), 1);
  32045. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  32046. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32047. wolfSSL_EVP_PKEY_free(key);
  32048. return EXPECT_RESULT();
  32049. }
  32050. #endif
  32051. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  32052. {
  32053. EXPECT_DECLS;
  32054. #ifdef OPENSSL_EXTRA
  32055. static const unsigned char testKey[] =
  32056. {
  32057. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  32058. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  32059. 0x0b, 0x0b, 0x0b, 0x0b
  32060. };
  32061. static const char testData[] = "Hi There";
  32062. #ifdef WOLFSSL_SHA224
  32063. static const unsigned char testResultSha224[] =
  32064. {
  32065. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  32066. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  32067. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  32068. 0x53, 0x68, 0x4b, 0x22
  32069. };
  32070. #endif
  32071. #ifndef NO_SHA256
  32072. static const unsigned char testResultSha256[] =
  32073. {
  32074. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  32075. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  32076. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  32077. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  32078. };
  32079. #endif
  32080. #ifdef WOLFSSL_SHA384
  32081. static const unsigned char testResultSha384[] =
  32082. {
  32083. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  32084. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  32085. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  32086. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  32087. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  32088. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  32089. };
  32090. #endif
  32091. #ifdef WOLFSSL_SHA512
  32092. static const unsigned char testResultSha512[] =
  32093. {
  32094. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  32095. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  32096. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  32097. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  32098. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  32099. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  32100. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  32101. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  32102. };
  32103. #endif
  32104. #ifdef WOLFSSL_SHA3
  32105. #ifndef WOLFSSL_NOSHA3_224
  32106. static const unsigned char testResultSha3_224[] =
  32107. {
  32108. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  32109. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  32110. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  32111. 0xf3, 0xc8, 0x60, 0xf7
  32112. };
  32113. #endif
  32114. #ifndef WOLFSSL_NOSHA3_256
  32115. static const unsigned char testResultSha3_256[] =
  32116. {
  32117. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  32118. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  32119. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  32120. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  32121. };
  32122. #endif
  32123. #ifndef WOLFSSL_NOSHA3_384
  32124. static const unsigned char testResultSha3_384[] =
  32125. {
  32126. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  32127. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  32128. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  32129. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  32130. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  32131. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  32132. };
  32133. #endif
  32134. #ifndef WOLFSSL_NOSHA3_512
  32135. static const unsigned char testResultSha3_512[] =
  32136. {
  32137. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  32138. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  32139. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  32140. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  32141. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  32142. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  32143. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  32144. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  32145. };
  32146. #endif
  32147. #endif
  32148. #ifndef NO_SHA256
  32149. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha256(), testKey,
  32150. sizeof(testKey), testData, XSTRLEN(testData), testResultSha256,
  32151. sizeof(testResultSha256)), TEST_SUCCESS);
  32152. #endif
  32153. #ifdef WOLFSSL_SHA224
  32154. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha224(), testKey,
  32155. sizeof(testKey), testData, XSTRLEN(testData), testResultSha224,
  32156. sizeof(testResultSha224)), TEST_SUCCESS);
  32157. #endif
  32158. #ifdef WOLFSSL_SHA384
  32159. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha384(), testKey,
  32160. sizeof(testKey), testData, XSTRLEN(testData), testResultSha384,
  32161. sizeof(testResultSha384)), TEST_SUCCESS);
  32162. #endif
  32163. #ifdef WOLFSSL_SHA512
  32164. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha512(), testKey,
  32165. sizeof(testKey), testData, XSTRLEN(testData), testResultSha512,
  32166. sizeof(testResultSha512)), TEST_SUCCESS);
  32167. #endif
  32168. #ifdef WOLFSSL_SHA3
  32169. #ifndef WOLFSSL_NOSHA3_224
  32170. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey,
  32171. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_224,
  32172. sizeof(testResultSha3_224)), TEST_SUCCESS);
  32173. #endif
  32174. #ifndef WOLFSSL_NOSHA3_256
  32175. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey,
  32176. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_256,
  32177. sizeof(testResultSha3_256)), TEST_SUCCESS);
  32178. #endif
  32179. #ifndef WOLFSSL_NOSHA3_384
  32180. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey,
  32181. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_384,
  32182. sizeof(testResultSha3_384)), TEST_SUCCESS);
  32183. #endif
  32184. #ifndef WOLFSSL_NOSHA3_512
  32185. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey,
  32186. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_512,
  32187. sizeof(testResultSha3_512)), TEST_SUCCESS);
  32188. #endif
  32189. #endif
  32190. #endif /* OPENSSL_EXTRA */
  32191. return EXPECT_RESULT();
  32192. }
  32193. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  32194. {
  32195. EXPECT_DECLS;
  32196. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  32197. WOLFSSL_EVP_PKEY* privKey = NULL;
  32198. WOLFSSL_EVP_PKEY* pubKey = NULL;
  32199. WOLFSSL_EVP_PKEY_CTX* keyCtx = NULL;
  32200. const char testData[] = "Hi There";
  32201. WOLFSSL_EVP_MD_CTX mdCtx;
  32202. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  32203. int ret;
  32204. size_t checkSz = -1;
  32205. int sz = 2048 / 8;
  32206. const unsigned char* cp;
  32207. const unsigned char* p;
  32208. unsigned char check[2048/8];
  32209. size_t i;
  32210. int paddings[] = {
  32211. RSA_PKCS1_PADDING,
  32212. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  32213. RSA_PKCS1_PSS_PADDING,
  32214. #endif
  32215. };
  32216. cp = client_key_der_2048;
  32217. ExpectNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  32218. sizeof_client_key_der_2048)));
  32219. p = client_keypub_der_2048;
  32220. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  32221. sizeof_client_keypub_der_2048)));
  32222. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32223. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  32224. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32225. NULL, privKey), 1);
  32226. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32227. (unsigned int)XSTRLEN(testData)), 1);
  32228. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32229. ExpectIntEQ((int)checkSz, sz);
  32230. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32231. ExpectIntEQ((int)checkSz,sz);
  32232. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  32233. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  32234. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy);
  32235. ExpectIntEQ(ret, 1);
  32236. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32237. ExpectIntEQ(ret, 1);
  32238. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32239. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32240. NULL, pubKey), 1);
  32241. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32242. (unsigned int)XSTRLEN(testData)),
  32243. 1);
  32244. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32245. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32246. ExpectIntEQ(ret, 1);
  32247. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32248. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32249. NULL, privKey), 1);
  32250. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  32251. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32252. ExpectIntEQ((int)checkSz, sz);
  32253. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32254. ExpectIntEQ((int)checkSz, sz);
  32255. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  32256. (unsigned int)XSTRLEN(testData) - 4), 1);
  32257. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32258. ExpectIntEQ((int)checkSz, sz);
  32259. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32260. ExpectIntEQ(ret, 1);
  32261. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32262. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32263. NULL, pubKey), 1);
  32264. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  32265. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  32266. (unsigned int)XSTRLEN(testData) - 4),
  32267. 1);
  32268. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32269. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32270. ExpectIntEQ(ret, 1);
  32271. /* Check all signing padding types */
  32272. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  32273. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32274. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  32275. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  32276. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  32277. paddings[i]), 1);
  32278. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32279. (unsigned int)XSTRLEN(testData)), 1);
  32280. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32281. ExpectIntEQ((int)checkSz, sz);
  32282. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32283. ExpectIntEQ((int)checkSz,sz);
  32284. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32285. ExpectIntEQ(ret, 1);
  32286. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32287. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  32288. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  32289. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  32290. paddings[i]), 1);
  32291. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32292. (unsigned int)XSTRLEN(testData)), 1);
  32293. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32294. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32295. ExpectIntEQ(ret, 1);
  32296. }
  32297. wolfSSL_EVP_PKEY_free(pubKey);
  32298. wolfSSL_EVP_PKEY_free(privKey);
  32299. #endif
  32300. return EXPECT_RESULT();
  32301. }
  32302. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  32303. {
  32304. EXPECT_DECLS;
  32305. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  32306. WOLFSSL_EVP_PKEY* privKey = NULL;
  32307. WOLFSSL_EVP_PKEY* pubKey = NULL;
  32308. const char testData[] = "Hi There";
  32309. WOLFSSL_EVP_MD_CTX mdCtx;
  32310. int ret;
  32311. size_t checkSz = -1;
  32312. const unsigned char* cp;
  32313. const unsigned char* p;
  32314. unsigned char check[2048/8];
  32315. cp = ecc_clikey_der_256;
  32316. ExpectNotNull(privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  32317. sizeof_ecc_clikey_der_256));
  32318. p = ecc_clikeypub_der_256;
  32319. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  32320. sizeof_ecc_clikeypub_der_256)));
  32321. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32322. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32323. NULL, privKey), 1);
  32324. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32325. (unsigned int)XSTRLEN(testData)), 1);
  32326. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32327. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32328. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32329. ExpectIntEQ(ret, 1);
  32330. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32331. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32332. NULL, pubKey), 1);
  32333. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32334. (unsigned int)XSTRLEN(testData)),
  32335. 1);
  32336. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32337. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32338. ExpectIntEQ(ret, 1);
  32339. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32340. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32341. NULL, privKey), 1);
  32342. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  32343. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32344. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32345. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  32346. (unsigned int)XSTRLEN(testData) - 4), 1);
  32347. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32348. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32349. ExpectIntEQ(ret, 1);
  32350. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32351. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32352. NULL, pubKey), 1);
  32353. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  32354. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  32355. (unsigned int)XSTRLEN(testData) - 4),
  32356. 1);
  32357. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32358. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32359. ExpectIntEQ(ret, 1);
  32360. wolfSSL_EVP_PKEY_free(pubKey);
  32361. wolfSSL_EVP_PKEY_free(privKey);
  32362. #endif
  32363. return EXPECT_RESULT();
  32364. }
  32365. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  32366. {
  32367. EXPECT_DECLS;
  32368. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32369. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  32370. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32371. char caFile[] = "./certs/client-ca.pem";
  32372. char clientFile[] = "./certs/client-cert.pem";
  32373. SSL_CTX* ctx = NULL;
  32374. X509* x509 = NULL;
  32375. BIO *bio = NULL;
  32376. X509 *cert = NULL;
  32377. X509 *ca = NULL;
  32378. STACK_OF(X509) *chain = NULL;
  32379. STACK_OF(X509) *chain2 = NULL;
  32380. #ifndef NO_WOLFSSL_SERVER
  32381. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32382. #else
  32383. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32384. #endif
  32385. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  32386. WOLFSSL_FILETYPE_PEM));
  32387. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  32388. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  32389. WOLFSSL_FILETYPE_PEM));
  32390. /* additional test of getting EVP_PKEY key size from X509
  32391. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  32392. * allowed with user RSA */
  32393. {
  32394. EVP_PKEY* pkey = NULL;
  32395. #if defined(HAVE_ECC)
  32396. X509* ecX509 = NULL;
  32397. #endif /* HAVE_ECC */
  32398. ExpectNotNull(pkey = X509_get_pubkey(x509));
  32399. /* current RSA key is 2048 bit (256 bytes) */
  32400. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  32401. EVP_PKEY_free(pkey);
  32402. pkey = NULL;
  32403. #if defined(HAVE_ECC)
  32404. #if defined(USE_CERT_BUFFERS_256)
  32405. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  32406. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  32407. SSL_FILETYPE_ASN1));
  32408. #else
  32409. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  32410. cliEccCertFile, SSL_FILETYPE_PEM));
  32411. #endif
  32412. pkey = X509_get_pubkey(ecX509);
  32413. ExpectNotNull(pkey);
  32414. /* current ECC key is 256 bit (32 bytes) */
  32415. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  32416. X509_free(ecX509);
  32417. ecX509 = NULL;
  32418. EVP_PKEY_free(pkey);
  32419. pkey = NULL;
  32420. #endif /* HAVE_ECC */
  32421. }
  32422. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  32423. if (EXPECT_SUCCESS()) {
  32424. x509 = NULL;
  32425. }
  32426. #ifdef WOLFSSL_ENCRYPTED_KEYS
  32427. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  32428. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  32429. #endif
  32430. SSL_CTX_free(ctx);
  32431. ctx = NULL;
  32432. #ifndef NO_WOLFSSL_SERVER
  32433. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32434. #else
  32435. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32436. #endif
  32437. /* Test haproxy use case */
  32438. ExpectNotNull(bio = BIO_new_file(svrCertFile, "r"));
  32439. /* Read Certificate */
  32440. ExpectNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  32441. ExpectNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  32442. ExpectNotNull(chain = sk_X509_new_null());
  32443. ExpectIntEQ(sk_X509_push(chain, ca), 1);
  32444. if (EXPECT_SUCCESS()) {
  32445. ca = NULL;
  32446. }
  32447. ExpectNotNull(chain2 = X509_chain_up_ref(chain));
  32448. ExpectNotNull(ca = sk_X509_shift(chain2));
  32449. ExpectIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  32450. ExpectIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  32451. if (EXPECT_SUCCESS()) {
  32452. ca = NULL;
  32453. }
  32454. BIO_free(bio);
  32455. X509_free(cert);
  32456. X509_free(ca);
  32457. X509_free(x509);
  32458. sk_X509_pop_free(chain, X509_free);
  32459. sk_X509_pop_free(chain2, X509_free);
  32460. SSL_CTX_free(ctx);
  32461. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32462. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32463. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  32464. return EXPECT_RESULT();
  32465. }
  32466. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  32467. static int test_wolfSSL_ERR_peek_last_error_line(void)
  32468. {
  32469. EXPECT_DECLS;
  32470. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32471. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  32472. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  32473. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  32474. callback_functions client_cb;
  32475. callback_functions server_cb;
  32476. int line = 0;
  32477. int flag = ERR_TXT_STRING;
  32478. const char* file = NULL;
  32479. const char* data = NULL;
  32480. /* create a failed connection and inspect the error */
  32481. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  32482. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  32483. client_cb.method = wolfTLSv1_1_client_method;
  32484. server_cb.method = wolfTLSv1_2_server_method;
  32485. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  32486. ExpectIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  32487. ExpectNotNull(data);
  32488. /* check clearing error state */
  32489. ERR_remove_state(0);
  32490. ExpectIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  32491. ERR_peek_last_error_line(NULL, &line);
  32492. ExpectIntEQ(line, 0);
  32493. ERR_peek_last_error_line(&file, NULL);
  32494. ExpectNull(file);
  32495. /* retry connection to fill error queue */
  32496. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  32497. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  32498. client_cb.method = wolfTLSv1_1_client_method;
  32499. server_cb.method = wolfTLSv1_2_server_method;
  32500. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  32501. /* check that error code was stored */
  32502. ExpectIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  32503. ERR_peek_last_error_line(NULL, &line);
  32504. ExpectIntNE(line, 0);
  32505. ERR_peek_last_error_line(&file, NULL);
  32506. ExpectNotNull(file);
  32507. fprintf(stderr, "\nTesting error print out\n");
  32508. ERR_print_errors_fp(stderr);
  32509. fprintf(stderr, "Done testing print out\n\n");
  32510. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  32511. * !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  32512. return EXPECT_RESULT();
  32513. }
  32514. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  32515. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32516. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32517. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  32518. {
  32519. (void) ok;
  32520. (void) ctx;
  32521. fprintf(stderr, "ENTER verify_cb\n");
  32522. return SSL_SUCCESS;
  32523. }
  32524. #endif
  32525. static int test_wolfSSL_X509_Name_canon(void)
  32526. {
  32527. EXPECT_DECLS;
  32528. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32529. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  32530. defined(WOLFSSL_CERT_GEN) && \
  32531. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  32532. const long ex_hash1 = 0x0fdb2da4;
  32533. const long ex_hash2 = 0x9f3e8c9e;
  32534. X509_NAME *name = NULL;
  32535. X509 *x509 = NULL;
  32536. XFILE file = XBADFILE;
  32537. unsigned long hash = 0;
  32538. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  32539. byte *pbuf = NULL;
  32540. word32 len = 0;
  32541. (void) ex_hash2;
  32542. ExpectTrue((file = XFOPEN(caCertFile, "rb")) != XBADFILE);
  32543. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  32544. ExpectNotNull(name = X509_get_issuer_name(x509));
  32545. /* When output buffer is NULL, should return necessary output buffer
  32546. * length.*/
  32547. ExpectIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  32548. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  32549. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  32550. hash = (((unsigned long)digest[3] << 24) |
  32551. ((unsigned long)digest[2] << 16) |
  32552. ((unsigned long)digest[1] << 8) |
  32553. ((unsigned long)digest[0]));
  32554. ExpectIntEQ(hash, ex_hash1);
  32555. if (file != XBADFILE) {
  32556. XFCLOSE(file);
  32557. file = XBADFILE;
  32558. }
  32559. X509_free(x509);
  32560. x509 = NULL;
  32561. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  32562. pbuf = NULL;
  32563. ExpectTrue((file = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  32564. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  32565. ExpectNotNull(name = X509_get_issuer_name(x509));
  32566. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  32567. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  32568. hash = (((unsigned long)digest[3] << 24) |
  32569. ((unsigned long)digest[2] << 16) |
  32570. ((unsigned long)digest[1] << 8) |
  32571. ((unsigned long)digest[0]));
  32572. ExpectIntEQ(hash, ex_hash2);
  32573. if (file != XBADFILE)
  32574. XFCLOSE(file);
  32575. X509_free(x509);
  32576. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  32577. #endif
  32578. return EXPECT_RESULT();
  32579. }
  32580. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  32581. {
  32582. EXPECT_DECLS;
  32583. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  32584. const int MAX_DIR = 4;
  32585. const char paths[][32] = {
  32586. "./certs/ed25519",
  32587. "./certs/ecc",
  32588. "./certs/crl",
  32589. "./certs/",
  32590. };
  32591. char CertCrl_path[MAX_FILENAME_SZ];
  32592. char *p;
  32593. X509_STORE* str = NULL;
  32594. X509_LOOKUP* lookup = NULL;
  32595. WOLFSSL_STACK* sk = NULL;
  32596. int len, total_len, i;
  32597. (void)sk;
  32598. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  32599. /* illegal string */
  32600. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32601. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32602. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  32603. SSL_FILETYPE_PEM,NULL), 0);
  32604. /* free store */
  32605. X509_STORE_free(str);
  32606. str = NULL;
  32607. /* short folder string */
  32608. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32609. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32610. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  32611. SSL_FILETYPE_PEM,NULL), 1);
  32612. #if defined(WOLFSSL_INT_H)
  32613. /* only available when including internal.h */
  32614. ExpectNotNull(sk = lookup->dirs->dir_entry);
  32615. #endif
  32616. /* free store */
  32617. X509_STORE_free(str);
  32618. str = NULL;
  32619. /* typical function check */
  32620. p = &CertCrl_path[0];
  32621. total_len = 0;
  32622. for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  32623. len = (int)XSTRLEN((const char*)&paths[i]);
  32624. total_len += len;
  32625. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  32626. p += len;
  32627. if (i != 0) *(p++) = SEPARATOR_CHAR;
  32628. }
  32629. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32630. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32631. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  32632. SSL_FILETYPE_PEM,NULL), 1);
  32633. #if defined(WOLFSSL_INT_H)
  32634. /* only available when including internal.h */
  32635. ExpectNotNull(sk = lookup->dirs->dir_entry);
  32636. #endif
  32637. X509_STORE_free(str);
  32638. #endif
  32639. return EXPECT_RESULT();
  32640. }
  32641. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  32642. {
  32643. EXPECT_DECLS;
  32644. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32645. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  32646. defined(WOLFSSL_SIGNER_DER_CERT)
  32647. X509_STORE_CTX* ctx = NULL;
  32648. X509_STORE* str = NULL;
  32649. X509_LOOKUP* lookup = NULL;
  32650. X509* cert1 = NULL;
  32651. X509* x509Ca = NULL;
  32652. X509* x509Svr = NULL;
  32653. X509* issuer = NULL;
  32654. WOLFSSL_STACK* sk = NULL;
  32655. X509_NAME* caName = NULL;
  32656. X509_NAME* issuerName = NULL;
  32657. XFILE file1 = XBADFILE;
  32658. int i;
  32659. int cert_count = 0;
  32660. int cmp;
  32661. char der[] = "certs/ca-cert.der";
  32662. #ifdef HAVE_CRL
  32663. char pem[][100] = {
  32664. "./certs/crl/crl.pem",
  32665. "./certs/crl/crl2.pem",
  32666. "./certs/crl/caEccCrl.pem",
  32667. "./certs/crl/eccCliCRL.pem",
  32668. "./certs/crl/eccSrvCRL.pem",
  32669. ""
  32670. };
  32671. #endif
  32672. ExpectTrue((file1 = XFOPEN("./certs/ca-cert.pem", "rb")) != XBADFILE);
  32673. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  32674. if (file1 != XBADFILE)
  32675. XFCLOSE(file1);
  32676. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32677. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32678. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32679. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  32680. SSL_FILETYPE_PEM,NULL), 1);
  32681. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  32682. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  32683. /* check if CA cert is loaded into the store */
  32684. for (i = 0; i < cert_count; i++) {
  32685. x509Ca = sk_X509_value(sk, i);
  32686. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  32687. }
  32688. ExpectNotNull((x509Svr =
  32689. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32690. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  32691. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  32692. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  32693. ExpectNotNull(issuer);
  32694. caName = X509_get_subject_name(x509Ca);
  32695. ExpectNotNull(caName);
  32696. issuerName = X509_get_subject_name(issuer);
  32697. ExpectNotNull(issuerName);
  32698. cmp = X509_NAME_cmp(caName, issuerName);
  32699. ExpectIntEQ(cmp, 0);
  32700. /* load der format */
  32701. X509_free(issuer);
  32702. issuer = NULL;
  32703. X509_STORE_CTX_free(ctx);
  32704. ctx = NULL;
  32705. X509_STORE_free(str);
  32706. str = NULL;
  32707. sk_X509_pop_free(sk, NULL);
  32708. sk = NULL;
  32709. X509_free(x509Svr);
  32710. x509Svr = NULL;
  32711. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32712. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32713. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  32714. SSL_FILETYPE_ASN1,NULL), 1);
  32715. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  32716. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  32717. /* check if CA cert is loaded into the store */
  32718. for (i = 0; i < cert_count; i++) {
  32719. x509Ca = sk_X509_value(sk, i);
  32720. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  32721. }
  32722. X509_STORE_free(str);
  32723. str = NULL;
  32724. sk_X509_pop_free(sk, NULL);
  32725. sk = NULL;
  32726. X509_free(cert1);
  32727. cert1 = NULL;
  32728. #ifdef HAVE_CRL
  32729. ExpectNotNull(str = wolfSSL_X509_STORE_new());
  32730. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32731. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  32732. SSL_FILETYPE_PEM,NULL), 1);
  32733. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  32734. "certs/server-revoked-cert.pem",
  32735. SSL_FILETYPE_PEM,NULL), 1);
  32736. if (str) {
  32737. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  32738. WOLFSSL_FILETYPE_PEM), 1);
  32739. /* since store hasn't yet known the revoked cert*/
  32740. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  32741. "certs/server-revoked-cert.pem",
  32742. WOLFSSL_FILETYPE_PEM), 1);
  32743. }
  32744. for (i = 0; pem[i][0] != '\0'; i++)
  32745. {
  32746. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  32747. SSL_FILETYPE_PEM, NULL), 1);
  32748. }
  32749. if (str) {
  32750. /* since store knows crl list */
  32751. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  32752. "certs/server-revoked-cert.pem",
  32753. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  32754. }
  32755. ExpectIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  32756. X509_STORE_free(str);
  32757. #endif
  32758. #endif
  32759. return EXPECT_RESULT();
  32760. }
  32761. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  32762. {
  32763. int res = TEST_SKIPPED;
  32764. #if defined(OPENSSL_EXTRA)
  32765. X509_STORE_CTX_cleanup(NULL);
  32766. X509_STORE_CTX_trusted_stack(NULL, NULL);
  32767. res = TEST_SUCCESS;
  32768. #endif
  32769. return res;
  32770. }
  32771. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  32772. {
  32773. EXPECT_DECLS;
  32774. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32775. X509_STORE_CTX* ctx = NULL;
  32776. X509_STORE* str = NULL;
  32777. X509* x509Ca = NULL;
  32778. X509* x509Svr = NULL;
  32779. X509* issuer = NULL;
  32780. X509_NAME* caName = NULL;
  32781. X509_NAME* issuerName = NULL;
  32782. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32783. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32784. ExpectNotNull((x509Ca =
  32785. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  32786. ExpectIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  32787. ExpectNotNull((x509Svr =
  32788. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32789. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  32790. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  32791. ExpectNotNull(issuer = X509_STORE_CTX_get0_current_issuer(ctx));
  32792. ExpectNotNull(caName = X509_get_subject_name(x509Ca));
  32793. ExpectNotNull(issuerName = X509_get_subject_name(issuer));
  32794. #ifdef WOLFSSL_SIGNER_DER_CERT
  32795. ExpectIntEQ(X509_NAME_cmp(caName, issuerName), 0);
  32796. #endif
  32797. X509_free(issuer);
  32798. X509_STORE_CTX_free(ctx);
  32799. X509_free(x509Svr);
  32800. X509_STORE_free(str);
  32801. X509_free(x509Ca);
  32802. #endif
  32803. return EXPECT_RESULT();
  32804. }
  32805. static int test_wolfSSL_PKCS7_certs(void)
  32806. {
  32807. EXPECT_DECLS;
  32808. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  32809. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  32810. STACK_OF(X509)* sk = NULL;
  32811. STACK_OF(X509_INFO)* info_sk = NULL;
  32812. PKCS7 *p7 = NULL;
  32813. BIO* bio = NULL;
  32814. const byte* p = NULL;
  32815. int buflen = 0;
  32816. int i;
  32817. /* Test twice. Once with d2i and once without to test
  32818. * that everything is free'd correctly. */
  32819. for (i = 0; i < 2; i++) {
  32820. ExpectNotNull(p7 = PKCS7_new());
  32821. if (p7 != NULL) {
  32822. p7->version = 1;
  32823. #ifdef NO_SHA
  32824. p7->hashOID = SHA256h;
  32825. #else
  32826. p7->hashOID = SHAh;
  32827. #endif
  32828. }
  32829. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  32830. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  32831. ExpectNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  32832. ExpectIntEQ(sk_X509_INFO_num(info_sk), 2);
  32833. ExpectNotNull(sk = sk_X509_new_null());
  32834. while (EXPECT_SUCCESS() && (sk_X509_INFO_num(info_sk) > 0)) {
  32835. X509_INFO* info = NULL;
  32836. ExpectNotNull(info = sk_X509_INFO_shift(info_sk));
  32837. ExpectIntEQ(sk_X509_push(sk, info->x509), 1);
  32838. if (EXPECT_SUCCESS() && (info != NULL)) {
  32839. info->x509 = NULL;
  32840. }
  32841. X509_INFO_free(info);
  32842. }
  32843. sk_X509_INFO_pop_free(info_sk, X509_INFO_free);
  32844. info_sk = NULL;
  32845. BIO_free(bio);
  32846. bio = NULL;
  32847. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  32848. ExpectIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  32849. if ((sk != NULL) && ((p7 == NULL) || (bio == NULL))) {
  32850. sk_X509_pop_free(sk, X509_free);
  32851. }
  32852. sk = NULL;
  32853. ExpectIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  32854. if (i == 0) {
  32855. PKCS7_free(p7);
  32856. p7 = NULL;
  32857. ExpectNotNull(d2i_PKCS7(&p7, &p, buflen));
  32858. if (p7 != NULL) {
  32859. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate
  32860. * them */
  32861. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  32862. }
  32863. /* PKCS7_free free's the certs */
  32864. ExpectNotNull(wolfSSL_PKCS7_to_stack(p7));
  32865. }
  32866. BIO_free(bio);
  32867. bio = NULL;
  32868. PKCS7_free(p7);
  32869. p7 = NULL;
  32870. }
  32871. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32872. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  32873. return EXPECT_RESULT();
  32874. }
  32875. static int test_wolfSSL_X509_STORE_CTX(void)
  32876. {
  32877. EXPECT_DECLS;
  32878. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32879. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32880. X509_STORE_CTX* ctx = NULL;
  32881. X509_STORE* str = NULL;
  32882. X509* x509 = NULL;
  32883. #ifdef OPENSSL_ALL
  32884. X509* x5092 = NULL;
  32885. STACK_OF(X509) *sk = NULL;
  32886. STACK_OF(X509) *sk2 = NULL;
  32887. STACK_OF(X509) *sk3 = NULL;
  32888. #endif
  32889. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32890. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32891. ExpectNotNull((x509 =
  32892. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32893. ExpectIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  32894. #ifdef OPENSSL_ALL
  32895. /* sk_X509_new only in OPENSSL_ALL */
  32896. sk = sk_X509_new_null();
  32897. ExpectNotNull(sk);
  32898. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  32899. #else
  32900. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  32901. #endif
  32902. ExpectIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  32903. X509_STORE_CTX_set_error(ctx, -5);
  32904. X509_STORE_CTX_set_error(NULL, -5);
  32905. X509_STORE_CTX_free(ctx);
  32906. ctx = NULL;
  32907. #ifdef OPENSSL_ALL
  32908. sk_X509_pop_free(sk, NULL);
  32909. sk = NULL;
  32910. #endif
  32911. X509_STORE_free(str);
  32912. str = NULL;
  32913. X509_free(x509);
  32914. x509 = NULL;
  32915. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32916. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  32917. X509_STORE_CTX_free(ctx);
  32918. ctx = NULL;
  32919. #ifdef OPENSSL_ALL
  32920. /* test X509_STORE_CTX_get(1)_chain */
  32921. ExpectNotNull((x509 = X509_load_certificate_file(svrCertFile,
  32922. SSL_FILETYPE_PEM)));
  32923. ExpectNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  32924. SSL_FILETYPE_PEM)));
  32925. ExpectNotNull((sk = sk_X509_new_null()));
  32926. ExpectIntEQ(sk_X509_push(sk, x509), 1);
  32927. if (EXPECT_FAIL()) {
  32928. X509_free(x509);
  32929. x509 = NULL;
  32930. }
  32931. ExpectNotNull((str = X509_STORE_new()));
  32932. ExpectNotNull((ctx = X509_STORE_CTX_new()));
  32933. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  32934. ExpectNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  32935. ExpectNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  32936. ExpectIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  32937. ExpectNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  32938. ExpectNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  32939. ExpectIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  32940. X509_STORE_CTX_free(ctx);
  32941. ctx = NULL;
  32942. X509_STORE_free(str);
  32943. str = NULL;
  32944. /* CTX certs not freed yet */
  32945. X509_free(x5092);
  32946. x5092 = NULL;
  32947. sk_X509_pop_free(sk, NULL);
  32948. sk = NULL;
  32949. /* sk3 is dup so free here */
  32950. sk_X509_pop_free(sk3, NULL);
  32951. sk3 = NULL;
  32952. #endif
  32953. /* test X509_STORE_CTX_get/set_ex_data */
  32954. {
  32955. int i = 0, tmpData = 5;
  32956. void* tmpDataRet;
  32957. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32958. #ifdef HAVE_EX_DATA
  32959. for (i = 0; i < MAX_EX_DATA; i++) {
  32960. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  32961. WOLFSSL_SUCCESS);
  32962. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  32963. ExpectNotNull(tmpDataRet);
  32964. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  32965. }
  32966. #else
  32967. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  32968. WOLFSSL_FAILURE);
  32969. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  32970. ExpectNull(tmpDataRet);
  32971. #endif
  32972. X509_STORE_CTX_free(ctx);
  32973. ctx = NULL;
  32974. }
  32975. /* test X509_STORE_get/set_ex_data */
  32976. {
  32977. int i = 0, tmpData = 99;
  32978. void* tmpDataRet;
  32979. ExpectNotNull(str = X509_STORE_new());
  32980. #ifdef HAVE_EX_DATA
  32981. for (i = 0; i < MAX_EX_DATA; i++) {
  32982. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  32983. WOLFSSL_SUCCESS);
  32984. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  32985. ExpectNotNull(tmpDataRet);
  32986. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  32987. }
  32988. #else
  32989. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  32990. WOLFSSL_FAILURE);
  32991. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  32992. ExpectNull(tmpDataRet);
  32993. #endif
  32994. X509_STORE_free(str);
  32995. str = NULL;
  32996. }
  32997. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32998. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32999. return EXPECT_RESULT();
  33000. }
  33001. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33002. static int test_X509_STORE_untrusted_load_cert_to_stack(const char* filename,
  33003. STACK_OF(X509)* chain)
  33004. {
  33005. EXPECT_DECLS;
  33006. XFILE fp = XBADFILE;
  33007. X509* cert = NULL;
  33008. ExpectTrue((fp = XFOPEN(filename, "rb"))
  33009. != XBADFILE);
  33010. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  33011. if (fp != XBADFILE) {
  33012. XFCLOSE(fp);
  33013. fp = XBADFILE;
  33014. }
  33015. ExpectIntEQ(sk_X509_push(chain, cert), 1);
  33016. if (EXPECT_FAIL())
  33017. X509_free(cert);
  33018. return EXPECT_RESULT();
  33019. }
  33020. static int test_X509_STORE_untrusted_certs(const char** filenames, int ret,
  33021. int err, int loadCA)
  33022. {
  33023. EXPECT_DECLS;
  33024. X509_STORE_CTX* ctx = NULL;
  33025. X509_STORE* str = NULL;
  33026. XFILE fp = XBADFILE;
  33027. X509* cert = NULL;
  33028. STACK_OF(X509)* untrusted = NULL;
  33029. ExpectTrue((fp = XFOPEN("./certs/intermediate/server-int-cert.pem", "rb"))
  33030. != XBADFILE);
  33031. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  33032. if (fp != XBADFILE) {
  33033. XFCLOSE(fp);
  33034. fp = XBADFILE;
  33035. }
  33036. ExpectNotNull(str = X509_STORE_new());
  33037. ExpectNotNull(ctx = X509_STORE_CTX_new());
  33038. ExpectNotNull(untrusted = sk_X509_new_null());
  33039. ExpectIntEQ(X509_STORE_set_flags(str, 0), 1);
  33040. if (loadCA) {
  33041. ExpectIntEQ(X509_STORE_load_locations(str, "./certs/ca-cert.pem", NULL),
  33042. 1);
  33043. }
  33044. for (; *filenames; filenames++) {
  33045. ExpectIntEQ(test_X509_STORE_untrusted_load_cert_to_stack(*filenames,
  33046. untrusted), TEST_SUCCESS);
  33047. }
  33048. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, cert, untrusted), 1);
  33049. ExpectIntEQ(X509_verify_cert(ctx), ret);
  33050. ExpectIntEQ(X509_STORE_CTX_get_error(ctx), err);
  33051. X509_free(cert);
  33052. X509_STORE_free(str);
  33053. X509_STORE_CTX_free(ctx);
  33054. sk_X509_pop_free(untrusted, NULL);
  33055. return EXPECT_RESULT();
  33056. }
  33057. #endif
  33058. static int test_X509_STORE_untrusted(void)
  33059. {
  33060. EXPECT_DECLS;
  33061. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33062. const char* untrusted1[] = {
  33063. "./certs/intermediate/ca-int2-cert.pem",
  33064. NULL
  33065. };
  33066. const char* untrusted2[] = {
  33067. "./certs/intermediate/ca-int-cert.pem",
  33068. "./certs/intermediate/ca-int2-cert.pem",
  33069. NULL
  33070. };
  33071. const char* untrusted3[] = {
  33072. "./certs/intermediate/ca-int-cert.pem",
  33073. "./certs/intermediate/ca-int2-cert.pem",
  33074. "./certs/ca-cert.pem",
  33075. NULL
  33076. };
  33077. /* Adding unrelated certs that should be ignored */
  33078. const char* untrusted4[] = {
  33079. "./certs/client-ca.pem",
  33080. "./certs/intermediate/ca-int-cert.pem",
  33081. "./certs/server-cert.pem",
  33082. "./certs/intermediate/ca-int2-cert.pem",
  33083. NULL
  33084. };
  33085. /* Only immediate issuer in untrusted chain. Fails since can't build chain
  33086. * to loaded CA. */
  33087. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted1, 0,
  33088. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 1), TEST_SUCCESS);
  33089. /* Succeeds because path to loaded CA is available. */
  33090. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted2, 1, 0, 1),
  33091. TEST_SUCCESS);
  33092. /* Fails because root CA is in the untrusted stack */
  33093. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted3, 0,
  33094. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 0), TEST_SUCCESS);
  33095. /* Succeeds because path to loaded CA is available. */
  33096. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted4, 1, 0, 1),
  33097. TEST_SUCCESS);
  33098. #endif
  33099. return EXPECT_RESULT();
  33100. }
  33101. static int test_wolfSSL_X509_STORE_set_flags(void)
  33102. {
  33103. EXPECT_DECLS;
  33104. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33105. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33106. X509_STORE* store = NULL;
  33107. X509* x509 = NULL;
  33108. ExpectNotNull((store = wolfSSL_X509_STORE_new()));
  33109. ExpectNotNull((x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  33110. WOLFSSL_FILETYPE_PEM)));
  33111. ExpectIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  33112. #ifdef HAVE_CRL
  33113. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  33114. WOLFSSL_SUCCESS);
  33115. #else
  33116. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  33117. NOT_COMPILED_IN);
  33118. #endif
  33119. wolfSSL_X509_free(x509);
  33120. wolfSSL_X509_STORE_free(store);
  33121. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  33122. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33123. return EXPECT_RESULT();
  33124. }
  33125. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  33126. {
  33127. EXPECT_DECLS;
  33128. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  33129. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  33130. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  33131. WOLFSSL_X509_STORE* store = NULL;
  33132. WOLFSSL_X509_LOOKUP* lookup = NULL;
  33133. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  33134. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  33135. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  33136. X509_FILETYPE_PEM), 1);
  33137. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  33138. X509_FILETYPE_PEM), 1);
  33139. if (store != NULL) {
  33140. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  33141. WOLFSSL_FILETYPE_PEM), 1);
  33142. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  33143. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  33144. }
  33145. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  33146. X509_FILETYPE_PEM), 1);
  33147. if (store != NULL) {
  33148. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  33149. WOLFSSL_FILETYPE_PEM), 1);
  33150. }
  33151. wolfSSL_X509_STORE_free(store);
  33152. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) &&
  33153. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33154. return EXPECT_RESULT();
  33155. }
  33156. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  33157. {
  33158. EXPECT_DECLS;
  33159. #if defined(OPENSSL_EXTRA)
  33160. WOLFSSL_X509_STORE_CTX* ctx = NULL;
  33161. time_t c_time;
  33162. ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  33163. c_time = 365*24*60*60;
  33164. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  33165. ExpectTrue((ctx->param->flags & WOLFSSL_USE_CHECK_TIME) ==
  33166. WOLFSSL_USE_CHECK_TIME);
  33167. ExpectTrue(ctx->param->check_time == c_time);
  33168. wolfSSL_X509_STORE_CTX_free(ctx);
  33169. #endif /* OPENSSL_EXTRA */
  33170. return EXPECT_RESULT();
  33171. }
  33172. static int test_wolfSSL_CTX_get0_set1_param(void)
  33173. {
  33174. EXPECT_DECLS;
  33175. #if defined(OPENSSL_EXTRA)
  33176. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33177. SSL_CTX* ctx = NULL;
  33178. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  33179. WOLFSSL_X509_VERIFY_PARAM* pvpm = NULL;
  33180. char testIPv4[] = "127.0.0.1";
  33181. char testhostName[] = "foo.hoge.com";
  33182. #ifndef NO_WOLFSSL_SERVER
  33183. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33184. #else
  33185. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33186. #endif
  33187. ExpectNull(SSL_CTX_get0_param(NULL));
  33188. ExpectNotNull(pParam = SSL_CTX_get0_param(ctx));
  33189. ExpectNotNull(pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  33190. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL));
  33191. ExpectNotNull(XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM)));
  33192. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  33193. (int)XSTRLEN(testhostName)), WOLFSSL_SUCCESS);
  33194. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4),
  33195. WOLFSSL_SUCCESS);
  33196. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  33197. ExpectIntEQ(SSL_CTX_set1_param(ctx, pvpm), 1);
  33198. ExpectIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  33199. (int)XSTRLEN(testhostName)));
  33200. ExpectIntEQ(0x01, pParam->hostFlags);
  33201. ExpectIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  33202. /* test for incorrect parameter */
  33203. ExpectIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  33204. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  33205. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  33206. SSL_CTX_free(ctx);
  33207. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  33208. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33209. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  33210. return EXPECT_RESULT();
  33211. }
  33212. static int test_wolfSSL_get0_param(void)
  33213. {
  33214. EXPECT_DECLS;
  33215. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33216. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33217. SSL_CTX* ctx = NULL;
  33218. SSL* ssl = NULL;
  33219. #ifndef NO_WOLFSSL_SERVER
  33220. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33221. #else
  33222. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33223. #endif
  33224. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33225. SSL_FILETYPE_PEM));
  33226. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33227. ExpectNotNull(ssl = SSL_new(ctx));
  33228. ExpectNotNull(SSL_get0_param(ssl));
  33229. SSL_free(ssl);
  33230. SSL_CTX_free(ctx);
  33231. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33232. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  33233. return EXPECT_RESULT();
  33234. }
  33235. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  33236. {
  33237. EXPECT_DECLS;
  33238. #if defined(OPENSSL_EXTRA)
  33239. const char host[] = "www.example.com";
  33240. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  33241. ExpectNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  33242. sizeof(WOLFSSL_X509_VERIFY_PARAM), HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  33243. if (pParam != NULL) {
  33244. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  33245. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  33246. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33247. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  33248. ExpectIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33249. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33250. }
  33251. #endif /* OPENSSL_EXTRA */
  33252. return EXPECT_RESULT();
  33253. }
  33254. static int test_wolfSSL_set1_host(void)
  33255. {
  33256. EXPECT_DECLS;
  33257. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33258. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33259. const char host[] = "www.test_wolfSSL_set1_host.com";
  33260. const char emptyStr[] = "";
  33261. SSL_CTX* ctx = NULL;
  33262. SSL* ssl = NULL;
  33263. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  33264. #ifndef NO_WOLFSSL_SERVER
  33265. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33266. #else
  33267. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33268. #endif
  33269. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33270. SSL_FILETYPE_PEM));
  33271. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33272. ExpectNotNull(ssl = SSL_new(ctx));
  33273. pParam = SSL_get0_param(ssl);
  33274. /* we should get back host string */
  33275. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  33276. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33277. /* we should get back empty string */
  33278. ExpectIntEQ(SSL_set1_host(ssl, emptyStr), WOLFSSL_SUCCESS);
  33279. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  33280. /* we should get back host string */
  33281. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  33282. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33283. /* we should get back empty string */
  33284. ExpectIntEQ(SSL_set1_host(ssl, NULL), WOLFSSL_SUCCESS);
  33285. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  33286. SSL_free(ssl);
  33287. SSL_CTX_free(ctx);
  33288. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33289. #endif /* OPENSSL_EXTRA */
  33290. return EXPECT_RESULT();
  33291. }
  33292. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  33293. {
  33294. EXPECT_DECLS;
  33295. #if defined(OPENSSL_EXTRA)
  33296. unsigned char buf[16] = {0};
  33297. WOLFSSL_X509_VERIFY_PARAM* param = NULL;
  33298. ExpectNotNull(param = X509_VERIFY_PARAM_new());
  33299. /* test 127.0.0.1 */
  33300. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  33301. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  33302. ExpectIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  33303. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  33304. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33305. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  33306. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  33307. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  33308. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33309. ExpectIntEQ(XSTRNCMP(param->ipasc,
  33310. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  33311. /* test 2001:db8:: */
  33312. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33313. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33314. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33315. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  33316. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33317. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  33318. /* test ::1234:5678 */
  33319. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  33320. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33321. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33322. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  33323. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33324. ExpectIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  33325. /* test 2001:db8::1234:5678 */
  33326. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33327. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33328. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33329. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  33330. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33331. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  33332. sizeof(param->ipasc)), 0);
  33333. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  33334. /* 2001:db8:1::ab9:c0a8:102 */
  33335. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33336. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  33337. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  33338. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  33339. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33340. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  33341. sizeof(param->ipasc)), 0);
  33342. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33343. #endif /* OPENSSL_EXTRA */
  33344. return EXPECT_RESULT();
  33345. }
  33346. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  33347. {
  33348. EXPECT_DECLS;
  33349. #if defined(OPENSSL_EXTRA)
  33350. X509_STORE* store = NULL;
  33351. X509_STORE_CTX* ctx = NULL;
  33352. X509_STORE_CTX* ctx_no_init = NULL;
  33353. ExpectNotNull((store = X509_STORE_new()));
  33354. ExpectNotNull(ctx = X509_STORE_CTX_new());
  33355. ExpectNotNull(ctx_no_init = X509_STORE_CTX_new());
  33356. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  33357. ExpectNull(X509_STORE_CTX_get0_store(NULL));
  33358. /* should return NULL if ctx has not bee initialized */
  33359. ExpectNull(X509_STORE_CTX_get0_store(ctx_no_init));
  33360. ExpectNotNull(X509_STORE_CTX_get0_store(ctx));
  33361. wolfSSL_X509_STORE_CTX_free(ctx);
  33362. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  33363. X509_STORE_free(store);
  33364. #endif /* OPENSSL_EXTRA */
  33365. return EXPECT_RESULT();
  33366. }
  33367. static int test_wolfSSL_CTX_set_client_CA_list(void)
  33368. {
  33369. EXPECT_DECLS;
  33370. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  33371. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  33372. WOLFSSL_CTX* ctx = NULL;
  33373. WOLFSSL* ssl = NULL;
  33374. X509_NAME* name = NULL;
  33375. STACK_OF(X509_NAME)* names = NULL;
  33376. STACK_OF(X509_NAME)* ca_list = NULL;
  33377. int names_len = 0;
  33378. int i;
  33379. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  33380. /* Send two X501 names in cert request */
  33381. names = SSL_load_client_CA_file(cliCertFile);
  33382. ExpectNotNull(names);
  33383. ca_list = SSL_load_client_CA_file(caCertFile);
  33384. ExpectNotNull(ca_list);
  33385. ExpectNotNull(name = sk_X509_NAME_value(ca_list, 0));
  33386. ExpectIntEQ(sk_X509_NAME_push(names, name), 1);
  33387. if (EXPECT_FAIL()) {
  33388. wolfSSL_X509_NAME_free(name);
  33389. name = NULL;
  33390. }
  33391. SSL_CTX_set_client_CA_list(ctx, names);
  33392. /* This should only free the stack structure */
  33393. sk_X509_NAME_free(ca_list);
  33394. ca_list = NULL;
  33395. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  33396. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  33397. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  33398. for (i = 0; i < names_len; i++) {
  33399. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  33400. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  33401. }
  33402. /* Needed to be able to create ssl object */
  33403. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33404. SSL_FILETYPE_PEM));
  33405. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33406. ExpectNotNull(ssl = wolfSSL_new(ctx));
  33407. /* load again as old names are responsibility of ctx to free*/
  33408. names = SSL_load_client_CA_file(cliCertFile);
  33409. ExpectNotNull(names);
  33410. SSL_set_client_CA_list(ssl, names);
  33411. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl));
  33412. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  33413. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  33414. for (i = 0; i < names_len; i++) {
  33415. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  33416. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  33417. }
  33418. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  33419. {
  33420. tcp_ready ready;
  33421. func_args server_args;
  33422. callback_functions server_cb;
  33423. THREAD_TYPE serverThread;
  33424. WOLFSSL* ssl_client = NULL;
  33425. WOLFSSL_CTX* ctx_client = NULL;
  33426. SOCKET_T sockfd = 0;
  33427. /* wolfSSL_get_client_CA_list() with handshake */
  33428. StartTCP();
  33429. InitTcpReady(&ready);
  33430. XMEMSET(&server_args, 0, sizeof(func_args));
  33431. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  33432. server_args.signal = &ready;
  33433. server_args.callbacks = &server_cb;
  33434. /* we are responsible for free'ing WOLFSSL_CTX */
  33435. server_cb.ctx = ctx;
  33436. server_cb.isSharedCtx = 1;
  33437. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  33438. cliCertFile, 0));
  33439. start_thread(test_server_nofail, &server_args, &serverThread);
  33440. wait_tcp_ready(&server_args);
  33441. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  33442. ExpectNotNull(ctx_client =
  33443. wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  33444. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(
  33445. ctx_client, caCertFile, 0));
  33446. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(
  33447. ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  33448. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(
  33449. ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  33450. ExpectNotNull(ssl_client = wolfSSL_new(ctx_client));
  33451. ExpectIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  33452. ExpectIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  33453. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  33454. /* We are expecting two cert names to be sent */
  33455. ExpectIntEQ(sk_X509_NAME_num(ca_list), 2);
  33456. ExpectNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  33457. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  33458. ExpectNotNull(name = sk_X509_NAME_value(ca_list, i));
  33459. ExpectIntGE(sk_X509_NAME_find(names, name), 0);
  33460. }
  33461. wolfSSL_shutdown(ssl_client);
  33462. wolfSSL_free(ssl_client);
  33463. wolfSSL_CTX_free(ctx_client);
  33464. CloseSocket(sockfd);
  33465. join_thread(serverThread);
  33466. FreeTcpReady(&ready);
  33467. }
  33468. #endif
  33469. wolfSSL_free(ssl);
  33470. wolfSSL_CTX_free(ctx);
  33471. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
  33472. * !NO_BIO */
  33473. return EXPECT_RESULT();
  33474. }
  33475. static int test_wolfSSL_CTX_add_client_CA(void)
  33476. {
  33477. EXPECT_DECLS;
  33478. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  33479. !defined(NO_WOLFSSL_CLIENT)
  33480. WOLFSSL_CTX* ctx = NULL;
  33481. WOLFSSL_X509* x509 = NULL;
  33482. WOLFSSL_X509* x509_a = NULL;
  33483. STACK_OF(X509_NAME)* ca_list = NULL;
  33484. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33485. /* Add client cert */
  33486. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  33487. SSL_FILETYPE_PEM));
  33488. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509), SSL_SUCCESS);
  33489. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  33490. /* Add another client cert */
  33491. ExpectNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  33492. SSL_FILETYPE_PEM));
  33493. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  33494. /* test for incorrect parameter */
  33495. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  33496. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  33497. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  33498. X509_free(x509);
  33499. X509_free(x509_a);
  33500. SSL_CTX_free(ctx);
  33501. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  33502. return EXPECT_RESULT();
  33503. }
  33504. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  33505. defined(HAVE_IO_TESTS_DEPENDENCIES)
  33506. static THREAD_RETURN WOLFSSL_THREAD server_task_ech(void* args)
  33507. {
  33508. callback_functions* callbacks = ((func_args*)args)->callbacks;
  33509. WOLFSSL_CTX* ctx = callbacks->ctx;
  33510. WOLFSSL* ssl = NULL;
  33511. SOCKET_T sfd = 0;
  33512. SOCKET_T cfd = 0;
  33513. word16 port;
  33514. char input[1024];
  33515. int idx;
  33516. int ret, err = 0;
  33517. const char* privateName = "ech-private-name.com";
  33518. int privateNameLen = (int)XSTRLEN(privateName);
  33519. ((func_args*)args)->return_code = TEST_FAIL;
  33520. port = ((func_args*)args)->signal->port;
  33521. AssertIntEQ(WOLFSSL_SUCCESS,
  33522. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  33523. AssertIntEQ(WOLFSSL_SUCCESS,
  33524. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  33525. WOLFSSL_FILETYPE_PEM));
  33526. AssertIntEQ(WOLFSSL_SUCCESS,
  33527. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  33528. WOLFSSL_FILETYPE_PEM));
  33529. if (callbacks->ctx_ready)
  33530. callbacks->ctx_ready(ctx);
  33531. ssl = wolfSSL_new(ctx);
  33532. /* set the sni for the server */
  33533. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, privateName, privateNameLen);
  33534. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  33535. CloseSocket(sfd);
  33536. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  33537. if (callbacks->ssl_ready)
  33538. callbacks->ssl_ready(ssl);
  33539. do {
  33540. err = 0; /* Reset error */
  33541. ret = wolfSSL_accept(ssl);
  33542. if (ret != WOLFSSL_SUCCESS) {
  33543. err = wolfSSL_get_error(ssl, 0);
  33544. }
  33545. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  33546. if (ret != WOLFSSL_SUCCESS) {
  33547. char buff[WOLFSSL_MAX_ERROR_SZ];
  33548. fprintf(stderr, "error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  33549. }
  33550. else {
  33551. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  33552. input[idx] = 0;
  33553. fprintf(stderr, "Client message: %s\n", input);
  33554. }
  33555. AssertIntEQ(privateNameLen, wolfSSL_write(ssl, privateName,
  33556. privateNameLen));
  33557. ((func_args*)args)->return_code = TEST_SUCCESS;
  33558. }
  33559. if (callbacks->on_result)
  33560. callbacks->on_result(ssl);
  33561. wolfSSL_shutdown(ssl);
  33562. wolfSSL_free(ssl);
  33563. wolfSSL_CTX_free(ctx);
  33564. CloseSocket(cfd);
  33565. #ifdef FP_ECC
  33566. wc_ecc_fp_free();
  33567. #endif
  33568. WOLFSSL_RETURN_FROM_THREAD(0);
  33569. }
  33570. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  33571. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  33572. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  33573. {
  33574. AssertNotNull(ssl);
  33575. AssertNotNull(line);
  33576. XFILE fp;
  33577. const byte lf = '\n';
  33578. fp = XFOPEN("./MyKeyLog.txt", "a");
  33579. XFWRITE( line, 1, strlen(line),fp);
  33580. XFWRITE( (void*)&lf,1,1,fp);
  33581. XFFLUSH(fp);
  33582. XFCLOSE(fp);
  33583. }
  33584. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  33585. static int test_wolfSSL_CTX_set_keylog_callback(void)
  33586. {
  33587. EXPECT_DECLS;
  33588. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  33589. !defined(NO_WOLFSSL_CLIENT)
  33590. SSL_CTX* ctx = NULL;
  33591. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33592. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  33593. SSL_CTX_free(ctx);
  33594. SSL_CTX_set_keylog_callback(NULL, NULL);
  33595. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  33596. return EXPECT_RESULT();
  33597. }
  33598. static int test_wolfSSL_CTX_get_keylog_callback(void)
  33599. {
  33600. EXPECT_DECLS;
  33601. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  33602. !defined(NO_WOLFSSL_CLIENT)
  33603. SSL_CTX* ctx = NULL;
  33604. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33605. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  33606. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  33607. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  33608. SSL_CTX_set_keylog_callback(ctx, NULL );
  33609. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  33610. SSL_CTX_free(ctx);
  33611. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  33612. return EXPECT_RESULT();
  33613. }
  33614. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  33615. static int test_wolfSSL_Tls12_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  33616. {
  33617. /* set keylog callback */
  33618. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  33619. return TEST_SUCCESS;
  33620. }
  33621. #endif
  33622. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  33623. {
  33624. EXPECT_DECLS;
  33625. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  33626. /* This test is intended for checking whether keylog callback is called
  33627. * in client during TLS handshake between the client and a server.
  33628. */
  33629. test_ssl_cbf server_cbf;
  33630. test_ssl_cbf client_cbf;
  33631. XFILE fp = XBADFILE;
  33632. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  33633. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  33634. server_cbf.method = wolfTLSv1_2_server_method;
  33635. client_cbf.ctx_ready = &test_wolfSSL_Tls12_Key_Logging_client_ctx_ready;
  33636. /* clean up keylog file */
  33637. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  33638. if (fp != XBADFILE) {
  33639. XFFLUSH(fp);
  33640. XFCLOSE(fp);
  33641. fp = XBADFILE;
  33642. }
  33643. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33644. &server_cbf, NULL), TEST_SUCCESS);
  33645. XSLEEP_MS(100);
  33646. /* check if the keylog file exists */
  33647. char buff[300] = {0};
  33648. int found = 0;
  33649. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  33650. XFFLUSH(fp); /* Just to make sure any buffers get flushed */
  33651. while (EXPECT_SUCCESS() && XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  33652. if (0 == strncmp(buff,"CLIENT_RANDOM ", sizeof("CLIENT_RANDOM ")-1)) {
  33653. found = 1;
  33654. break;
  33655. }
  33656. }
  33657. if (fp != XBADFILE) {
  33658. XFCLOSE(fp);
  33659. }
  33660. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  33661. ExpectIntEQ(found, 1);
  33662. /* clean up */
  33663. ExpectIntEQ(rem_file("./MyKeyLog.txt"), 0);
  33664. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  33665. return EXPECT_RESULT();
  33666. }
  33667. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  33668. defined(HAVE_SECRET_CALLBACK)
  33669. static int test_wolfSSL_Tls13_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  33670. {
  33671. /* set keylog callback */
  33672. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  33673. return TEST_SUCCESS;
  33674. }
  33675. #endif
  33676. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  33677. {
  33678. EXPECT_DECLS;
  33679. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  33680. defined(HAVE_SECRET_CALLBACK)
  33681. /* This test is intended for checking whether keylog callback is called
  33682. * in client during TLS handshake between the client and a server.
  33683. */
  33684. test_ssl_cbf server_cbf;
  33685. test_ssl_cbf client_cbf;
  33686. XFILE fp = XBADFILE;
  33687. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  33688. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  33689. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  33690. client_cbf.ctx_ready = &test_wolfSSL_Tls13_Key_Logging_client_ctx_ready;
  33691. /* clean up keylog file */
  33692. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  33693. if (fp != XBADFILE) {
  33694. XFCLOSE(fp);
  33695. fp = XBADFILE;
  33696. }
  33697. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33698. &server_cbf, NULL), TEST_SUCCESS);
  33699. /* check if the keylog file exists */
  33700. {
  33701. char buff[300] = {0};
  33702. int found[4] = {0};
  33703. int numfnd = 0;
  33704. int i;
  33705. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  33706. while (EXPECT_SUCCESS() &&
  33707. XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  33708. if (0 == strncmp(buff, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  33709. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  33710. found[0] = 1;
  33711. continue;
  33712. }
  33713. else if (0 == strncmp(buff, "SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  33714. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  33715. found[1] = 1;
  33716. continue;
  33717. }
  33718. else if (0 == strncmp(buff, "CLIENT_TRAFFIC_SECRET_0 ",
  33719. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  33720. found[2] = 1;
  33721. continue;
  33722. }
  33723. else if (0 == strncmp(buff, "SERVER_TRAFFIC_SECRET_0 ",
  33724. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  33725. found[3] = 1;
  33726. continue;
  33727. }
  33728. }
  33729. if (fp != XBADFILE)
  33730. XFCLOSE(fp);
  33731. for (i = 0; i < 4; i++) {
  33732. if (found[i] != 0)
  33733. numfnd++;
  33734. }
  33735. ExpectIntEQ(numfnd, 4);
  33736. }
  33737. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  33738. return EXPECT_RESULT();
  33739. }
  33740. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  33741. defined(HAVE_IO_TESTS_DEPENDENCIES)
  33742. static int test_wolfSSL_Tls13_ECH_params(void)
  33743. {
  33744. EXPECT_DECLS;
  33745. #if !defined(NO_WOLFSSL_CLIENT)
  33746. word32 outputLen = 0;
  33747. byte testBuf[72];
  33748. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  33749. WOLFSSL *ssl = wolfSSL_new(ctx);
  33750. ExpectNotNull(ctx);
  33751. ExpectNotNull(ssl);
  33752. /* invalid ctx */
  33753. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(NULL,
  33754. "ech-public-name.com", 0, 0, 0));
  33755. /* invalid public name */
  33756. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx, NULL, 0,
  33757. 0, 0));
  33758. /* invalid algorithms */
  33759. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx,
  33760. "ech-public-name.com", 1000, 1000, 1000));
  33761. /* invalid ctx */
  33762. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(NULL, NULL,
  33763. &outputLen));
  33764. /* invalid output len */
  33765. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(ctx, NULL, NULL));
  33766. /* invalid ssl */
  33767. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(NULL,
  33768. (char*)testBuf, sizeof(testBuf)));
  33769. /* invalid configs64 */
  33770. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl, NULL,
  33771. sizeof(testBuf)));
  33772. /* invalid size */
  33773. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl,
  33774. (char*)testBuf, 0));
  33775. /* invalid ssl */
  33776. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(NULL, testBuf,
  33777. sizeof(testBuf)));
  33778. /* invalid configs */
  33779. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, NULL,
  33780. sizeof(testBuf)));
  33781. /* invalid size */
  33782. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, testBuf, 0));
  33783. /* invalid ssl */
  33784. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(NULL, NULL, &outputLen));
  33785. /* invalid size */
  33786. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(ssl, NULL, NULL));
  33787. wolfSSL_free(ssl);
  33788. wolfSSL_CTX_free(ctx);
  33789. #endif /* !NO_WOLFSSL_CLIENT */
  33790. return EXPECT_RESULT();
  33791. }
  33792. static int test_wolfSSL_Tls13_ECH(void)
  33793. {
  33794. EXPECT_DECLS;
  33795. tcp_ready ready;
  33796. func_args client_args;
  33797. func_args server_args;
  33798. THREAD_TYPE serverThread;
  33799. callback_functions server_cbf;
  33800. callback_functions client_cbf;
  33801. SOCKET_T sockfd = 0;
  33802. WOLFSSL_CTX* ctx = NULL;
  33803. WOLFSSL* ssl = NULL;
  33804. const char* publicName = "ech-public-name.com";
  33805. const char* privateName = "ech-private-name.com";
  33806. int privateNameLen = 20;
  33807. char reply[1024];
  33808. int replyLen = 0;
  33809. byte rawEchConfig[128];
  33810. word32 rawEchConfigLen = sizeof(rawEchConfig);
  33811. InitTcpReady(&ready);
  33812. ready.port = 22222;
  33813. XMEMSET(&client_args, 0, sizeof(func_args));
  33814. XMEMSET(&server_args, 0, sizeof(func_args));
  33815. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  33816. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  33817. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  33818. /* create the server context here so we can get the ech config */
  33819. ExpectNotNull(server_cbf.ctx =
  33820. wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  33821. /* generate ech config */
  33822. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(server_cbf.ctx,
  33823. publicName, 0, 0, 0));
  33824. /* get the config for the client to use */
  33825. ExpectIntEQ(WOLFSSL_SUCCESS,
  33826. wolfSSL_CTX_GetEchConfigs(server_cbf.ctx, rawEchConfig,
  33827. &rawEchConfigLen));
  33828. server_args.callbacks = &server_cbf;
  33829. server_args.signal = &ready;
  33830. /* start server task */
  33831. start_thread(server_task_ech, &server_args, &serverThread);
  33832. wait_tcp_ready(&server_args);
  33833. /* run as a TLS1.3 client */
  33834. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  33835. ExpectIntEQ(WOLFSSL_SUCCESS,
  33836. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  33837. ExpectIntEQ(WOLFSSL_SUCCESS,
  33838. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  33839. ExpectIntEQ(WOLFSSL_SUCCESS,
  33840. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  33841. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  33842. /* get connected the server task */
  33843. ExpectNotNull(ssl = wolfSSL_new(ctx));
  33844. /* set the ech configs for the client */
  33845. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, rawEchConfig,
  33846. rawEchConfigLen));
  33847. /* set the sni for the client */
  33848. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
  33849. privateName, privateNameLen));
  33850. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  33851. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  33852. ExpectIntEQ(wolfSSL_write(ssl, privateName, privateNameLen),
  33853. privateNameLen);
  33854. ExpectIntGT((replyLen = wolfSSL_read(ssl, reply, sizeof(reply))), 0);
  33855. /* add th null terminator for string compare */
  33856. reply[replyLen] = 0;
  33857. /* check that the server replied with the private name */
  33858. ExpectStrEQ(privateName, reply);
  33859. wolfSSL_free(ssl);
  33860. wolfSSL_CTX_free(ctx);
  33861. CloseSocket(sockfd);
  33862. join_thread(serverThread);
  33863. FreeTcpReady(&ready);
  33864. return EXPECT_RESULT();
  33865. }
  33866. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  33867. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  33868. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33869. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  33870. static int post_auth_version_cb(WOLFSSL* ssl)
  33871. {
  33872. EXPECT_DECLS;
  33873. /* do handshake and then test version error */
  33874. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  33875. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  33876. return EXPECT_RESULT();
  33877. }
  33878. static int post_auth_version_client_cb(WOLFSSL* ssl)
  33879. {
  33880. EXPECT_DECLS;
  33881. /* do handshake and then test version error */
  33882. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  33883. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  33884. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  33885. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  33886. /* check was added to error queue */
  33887. ExpectIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  33888. /* check the string matches expected string */
  33889. ExpectStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  33890. "WRONG_SSL_VERSION");
  33891. #endif
  33892. return EXPECT_RESULT();
  33893. }
  33894. static int post_auth_cb(WOLFSSL* ssl)
  33895. {
  33896. EXPECT_DECLS;
  33897. WOLFSSL_X509* x509 = NULL;
  33898. /* do handshake and then test version error */
  33899. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  33900. ExpectStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  33901. ExpectNull(x509 = wolfSSL_get_peer_certificate(ssl));
  33902. wolfSSL_X509_free(x509);
  33903. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  33904. return EXPECT_RESULT();
  33905. }
  33906. static int set_post_auth_cb(WOLFSSL* ssl)
  33907. {
  33908. if (!wolfSSL_is_server(ssl)) {
  33909. EXPECT_DECLS;
  33910. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  33911. return EXPECT_RESULT();
  33912. }
  33913. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  33914. return TEST_SUCCESS;
  33915. }
  33916. #endif
  33917. static int test_wolfSSL_Tls13_postauth(void)
  33918. {
  33919. EXPECT_DECLS;
  33920. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  33921. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33922. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  33923. test_ssl_cbf server_cbf;
  33924. test_ssl_cbf client_cbf;
  33925. /* test version failure doing post auth with TLS 1.2 connection */
  33926. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  33927. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  33928. server_cbf.method = wolfTLSv1_2_server_method;
  33929. server_cbf.ssl_ready = set_post_auth_cb;
  33930. server_cbf.on_result = post_auth_version_cb;
  33931. client_cbf.ssl_ready = set_post_auth_cb;
  33932. client_cbf.on_result = post_auth_version_client_cb;
  33933. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33934. &server_cbf, NULL), TEST_SUCCESS);
  33935. /* tests on post auth with TLS 1.3 */
  33936. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  33937. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  33938. server_cbf.method = wolfTLSv1_3_server_method;
  33939. server_cbf.ssl_ready = set_post_auth_cb;
  33940. client_cbf.ssl_ready = set_post_auth_cb;
  33941. server_cbf.on_result = post_auth_cb;
  33942. client_cbf.on_result = NULL;
  33943. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33944. &server_cbf, NULL), TEST_SUCCESS);
  33945. #endif
  33946. return EXPECT_RESULT();
  33947. }
  33948. static int test_wolfSSL_X509_NID(void)
  33949. {
  33950. EXPECT_DECLS;
  33951. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  33952. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  33953. int sigType;
  33954. int nameSz;
  33955. X509* cert = NULL;
  33956. EVP_PKEY* pubKeyTmp = NULL;
  33957. X509_NAME* name = NULL;
  33958. char commonName[80];
  33959. char countryName[80];
  33960. char localityName[80];
  33961. char stateName[80];
  33962. char orgName[80];
  33963. char orgUnit[80];
  33964. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  33965. /* convert cert from DER to internal WOLFSSL_X509 struct */
  33966. ExpectNotNull(cert = wolfSSL_X509_d2i_ex(&cert, client_cert_der_2048,
  33967. sizeof_client_cert_der_2048, HEAP_HINT));
  33968. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  33969. /* extract PUBLIC KEY from cert */
  33970. ExpectNotNull(pubKeyTmp = X509_get_pubkey(cert));
  33971. /* extract signatureType */
  33972. ExpectIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  33973. /* extract subjectName info */
  33974. ExpectNotNull(name = X509_get_subject_name(cert));
  33975. ExpectIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  33976. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  33977. NULL, 0)), 0);
  33978. ExpectIntEQ(nameSz, 15);
  33979. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  33980. commonName, sizeof(commonName))), 0);
  33981. ExpectIntEQ(nameSz, 15);
  33982. ExpectIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  33983. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  33984. commonName, 9)), 0);
  33985. ExpectIntEQ(nameSz, 8);
  33986. ExpectIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  33987. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  33988. countryName, sizeof(countryName))), 0);
  33989. ExpectIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  33990. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  33991. localityName, sizeof(localityName))), 0);
  33992. ExpectIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  33993. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  33994. NID_stateOrProvinceName, stateName, sizeof(stateName))), 0);
  33995. ExpectIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  33996. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  33997. orgName, sizeof(orgName))), 0);
  33998. ExpectIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  33999. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  34000. NID_organizationalUnitName, orgUnit, sizeof(orgUnit))), 0);
  34001. ExpectIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  34002. EVP_PKEY_free(pubKeyTmp);
  34003. X509_free(cert);
  34004. #endif
  34005. return EXPECT_RESULT();
  34006. }
  34007. static int test_wolfSSL_CTX_set_srp_username(void)
  34008. {
  34009. EXPECT_DECLS;
  34010. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  34011. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  34012. WOLFSSL_CTX* ctx = NULL;
  34013. WOLFSSL* ssl = NULL;
  34014. const char *username = "TESTUSER";
  34015. const char *password = "TESTPASSWORD";
  34016. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34017. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  34018. SSL_SUCCESS);
  34019. wolfSSL_CTX_free(ctx);
  34020. ctx = NULL;
  34021. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34022. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  34023. SSL_SUCCESS);
  34024. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  34025. SSL_SUCCESS);
  34026. ExpectNotNull(ssl = SSL_new(ctx));
  34027. ExpectNotNull(SSL_get_srp_username(ssl));
  34028. ExpectStrEQ(SSL_get_srp_username(ssl), username);
  34029. wolfSSL_free(ssl);
  34030. wolfSSL_CTX_free(ctx);
  34031. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  34032. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  34033. return EXPECT_RESULT();
  34034. }
  34035. static int test_wolfSSL_CTX_set_srp_password(void)
  34036. {
  34037. EXPECT_DECLS;
  34038. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) && \
  34039. !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  34040. WOLFSSL_CTX* ctx = NULL;
  34041. const char *username = "TESTUSER";
  34042. const char *password = "TESTPASSWORD";
  34043. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34044. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  34045. SSL_SUCCESS);
  34046. wolfSSL_CTX_free(ctx);
  34047. ctx = NULL;
  34048. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34049. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  34050. SSL_SUCCESS);
  34051. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  34052. SSL_SUCCESS);
  34053. wolfSSL_CTX_free(ctx);
  34054. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  34055. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  34056. return EXPECT_RESULT();
  34057. }
  34058. static int test_wolfSSL_X509_STORE(void)
  34059. {
  34060. EXPECT_DECLS;
  34061. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  34062. X509_STORE *store = NULL;
  34063. #ifdef HAVE_CRL
  34064. X509_STORE_CTX *storeCtx = NULL;
  34065. X509_CRL *crl = NULL;
  34066. X509 *ca = NULL;
  34067. X509 *cert = NULL;
  34068. const char crlPem[] = "./certs/crl/crl.revoked";
  34069. const char srvCert[] = "./certs/server-revoked-cert.pem";
  34070. const char caCert[] = "./certs/ca-cert.pem";
  34071. XFILE fp = XBADFILE;
  34072. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34073. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  34074. SSL_FILETYPE_PEM)));
  34075. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  34076. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  34077. SSL_FILETYPE_PEM)));
  34078. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  34079. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  34080. ExpectIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  34081. X509_STORE_free(store);
  34082. store = NULL;
  34083. X509_STORE_CTX_free(storeCtx);
  34084. storeCtx = NULL;
  34085. X509_free(cert);
  34086. cert = NULL;
  34087. X509_free(ca);
  34088. ca = NULL;
  34089. /* should fail to verify now after adding in CRL */
  34090. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34091. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  34092. SSL_FILETYPE_PEM)));
  34093. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  34094. ExpectTrue((fp = XFOPEN(crlPem, "rb")) != XBADFILE);
  34095. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  34096. NULL, NULL));
  34097. if (fp != XBADFILE)
  34098. XFCLOSE(fp);
  34099. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  34100. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  34101. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  34102. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  34103. SSL_FILETYPE_PEM)));
  34104. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  34105. ExpectIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  34106. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx),
  34107. WOLFSSL_X509_V_ERR_CERT_REVOKED);
  34108. X509_CRL_free(crl);
  34109. crl = NULL;
  34110. X509_STORE_free(store);
  34111. store = NULL;
  34112. X509_STORE_CTX_free(storeCtx);
  34113. storeCtx = NULL;
  34114. X509_free(cert);
  34115. cert = NULL;
  34116. X509_free(ca);
  34117. ca = NULL;
  34118. #endif /* HAVE_CRL */
  34119. #ifndef WOLFCRYPT_ONLY
  34120. {
  34121. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34122. SSL_CTX* ctx = NULL;
  34123. SSL* ssl = NULL;
  34124. int i;
  34125. for (i = 0; i < 2; i++) {
  34126. #ifndef NO_WOLFSSL_SERVER
  34127. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34128. #else
  34129. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34130. #endif
  34131. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34132. SSL_CTX_set_cert_store(ctx, store);
  34133. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34134. SSL_CTX_set_cert_store(ctx, store);
  34135. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34136. ExpectIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  34137. SSL_FILETYPE_PEM), SSL_SUCCESS);
  34138. ExpectIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  34139. SSL_FILETYPE_PEM), SSL_SUCCESS);
  34140. ExpectNotNull(ssl = SSL_new(ctx));
  34141. if (i == 0) {
  34142. ExpectIntEQ(SSL_set0_verify_cert_store(ssl, store),
  34143. SSL_SUCCESS);
  34144. }
  34145. else {
  34146. ExpectIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  34147. #ifdef OPENSSL_ALL
  34148. ExpectIntEQ(SSL_CTX_set1_verify_cert_store(ctx, store), SSL_SUCCESS);
  34149. #endif
  34150. }
  34151. if (EXPECT_FAIL() || (i == 1)) {
  34152. X509_STORE_free(store);
  34153. store = NULL;
  34154. }
  34155. SSL_free(ssl);
  34156. ssl = NULL;
  34157. SSL_CTX_free(ctx);
  34158. ctx = NULL;
  34159. }
  34160. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34161. }
  34162. #endif
  34163. #endif
  34164. return EXPECT_RESULT();
  34165. }
  34166. static int test_wolfSSL_X509_STORE_load_locations(void)
  34167. {
  34168. EXPECT_DECLS;
  34169. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  34170. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  34171. SSL_CTX *ctx = NULL;
  34172. X509_STORE *store = NULL;
  34173. const char ca_file[] = "./certs/ca-cert.pem";
  34174. const char client_pem_file[] = "./certs/client-cert.pem";
  34175. const char client_der_file[] = "./certs/client-cert.der";
  34176. const char ecc_file[] = "./certs/ecc-key.pem";
  34177. const char certs_path[] = "./certs/";
  34178. const char bad_path[] = "./bad-path/";
  34179. #ifdef HAVE_CRL
  34180. const char crl_path[] = "./certs/crl/";
  34181. const char crl_file[] = "./certs/crl/crl.pem";
  34182. #endif
  34183. #ifndef NO_WOLFSSL_SERVER
  34184. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  34185. #else
  34186. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  34187. #endif
  34188. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  34189. ExpectIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL),
  34190. WOLFSSL_SUCCESS);
  34191. /* Test bad arguments */
  34192. ExpectIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL),
  34193. WOLFSSL_FAILURE);
  34194. ExpectIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  34195. ExpectIntEQ(X509_STORE_load_locations(store, client_der_file, NULL),
  34196. WOLFSSL_FAILURE);
  34197. ExpectIntEQ(X509_STORE_load_locations(store, ecc_file, NULL),
  34198. WOLFSSL_FAILURE);
  34199. ExpectIntEQ(X509_STORE_load_locations(store, NULL, bad_path),
  34200. WOLFSSL_FAILURE);
  34201. #ifdef HAVE_CRL
  34202. /* Test with CRL */
  34203. ExpectIntEQ(X509_STORE_load_locations(store, crl_file, NULL),
  34204. WOLFSSL_SUCCESS);
  34205. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crl_path),
  34206. WOLFSSL_SUCCESS);
  34207. #endif
  34208. /* Test with CA */
  34209. ExpectIntEQ(X509_STORE_load_locations(store, ca_file, NULL),
  34210. WOLFSSL_SUCCESS);
  34211. /* Test with client_cert and certs path */
  34212. ExpectIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL),
  34213. WOLFSSL_SUCCESS);
  34214. ExpectIntEQ(X509_STORE_load_locations(store, NULL, certs_path),
  34215. WOLFSSL_SUCCESS);
  34216. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  34217. /* Clear nodes */
  34218. ERR_clear_error();
  34219. #endif
  34220. SSL_CTX_free(ctx);
  34221. #endif
  34222. return EXPECT_RESULT();
  34223. }
  34224. static int test_X509_STORE_get0_objects(void)
  34225. {
  34226. EXPECT_DECLS;
  34227. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  34228. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  34229. X509_STORE *store = NULL;
  34230. X509_STORE *store_cpy = NULL;
  34231. SSL_CTX *ctx = NULL;
  34232. X509_OBJECT *obj = NULL;
  34233. STACK_OF(X509_OBJECT) *objs = NULL;
  34234. int i;
  34235. /* Setup store */
  34236. #ifndef NO_WOLFSSL_SERVER
  34237. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  34238. #else
  34239. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  34240. #endif
  34241. ExpectNotNull(store_cpy = X509_STORE_new());
  34242. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  34243. ExpectIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL),
  34244. WOLFSSL_SUCCESS);
  34245. ExpectIntEQ(X509_STORE_load_locations(store, caCertFile, NULL),
  34246. WOLFSSL_SUCCESS);
  34247. ExpectIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL),
  34248. WOLFSSL_SUCCESS);
  34249. #ifdef HAVE_CRL
  34250. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir),
  34251. WOLFSSL_SUCCESS);
  34252. #endif
  34253. /* Store ready */
  34254. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  34255. ExpectNotNull(objs = X509_STORE_get0_objects(store));
  34256. #ifdef HAVE_CRL
  34257. #ifdef WOLFSSL_SIGNER_DER_CERT
  34258. ExpectIntEQ(sk_X509_OBJECT_num(objs), 4);
  34259. #else
  34260. ExpectIntEQ(sk_X509_OBJECT_num(objs), 1);
  34261. #endif
  34262. #else
  34263. #ifdef WOLFSSL_SIGNER_DER_CERT
  34264. ExpectIntEQ(sk_X509_OBJECT_num(objs), 3);
  34265. #else
  34266. ExpectIntEQ(sk_X509_OBJECT_num(objs), 0);
  34267. #endif
  34268. #endif
  34269. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  34270. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  34271. switch (X509_OBJECT_get_type(obj)) {
  34272. case X509_LU_X509:
  34273. {
  34274. WOLFSSL_X509* x509;
  34275. ExpectNotNull(x509 = X509_OBJECT_get0_X509(obj));
  34276. ExpectIntEQ(X509_STORE_add_cert(store_cpy, x509), WOLFSSL_SUCCESS);
  34277. break;
  34278. }
  34279. case X509_LU_CRL:
  34280. #ifdef HAVE_CRL
  34281. {
  34282. WOLFSSL_CRL* crl = NULL;
  34283. ExpectNotNull(crl = X509_OBJECT_get0_X509_CRL(obj));
  34284. ExpectIntEQ(X509_STORE_add_crl(store_cpy, crl), WOLFSSL_SUCCESS);
  34285. break;
  34286. }
  34287. #endif
  34288. case X509_LU_NONE:
  34289. default:
  34290. Fail(("X509_OBJECT_get_type should return x509 or crl "
  34291. "(when built with crl support)"),
  34292. ("Unrecognized X509_OBJECT type or none"));
  34293. }
  34294. }
  34295. X509_STORE_free(store_cpy);
  34296. SSL_CTX_free(ctx);
  34297. #endif
  34298. return EXPECT_RESULT();
  34299. }
  34300. static int test_wolfSSL_BN_CTX(void)
  34301. {
  34302. EXPECT_DECLS;
  34303. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34304. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34305. WOLFSSL_BN_CTX* bn_ctx = NULL;
  34306. WOLFSSL_BIGNUM* t = NULL;
  34307. ExpectNotNull(bn_ctx = wolfSSL_BN_CTX_new());
  34308. /* No implementation. */
  34309. BN_CTX_init(NULL);
  34310. ExpectNotNull(t = BN_CTX_get(NULL));
  34311. BN_free(t);
  34312. ExpectNotNull(t = BN_CTX_get(bn_ctx));
  34313. BN_free(t);
  34314. #ifndef NO_WOLFSSL_STUB
  34315. /* No implementation. */
  34316. BN_CTX_start(NULL);
  34317. BN_CTX_start(bn_ctx);
  34318. #endif
  34319. BN_CTX_free(NULL);
  34320. BN_CTX_free(bn_ctx);
  34321. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34322. return EXPECT_RESULT();
  34323. }
  34324. static int test_wolfSSL_BN(void)
  34325. {
  34326. EXPECT_DECLS;
  34327. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34328. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34329. BIGNUM* a = NULL;
  34330. BIGNUM* b = NULL;
  34331. BIGNUM* c = NULL;
  34332. BIGNUM* d = NULL;
  34333. BIGNUM emptyBN;
  34334. /* Setup */
  34335. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34336. /* internal not set emptyBN. */
  34337. ExpectNotNull(a = BN_new());
  34338. ExpectNotNull(b = BN_new());
  34339. ExpectNotNull(c = BN_dup(b));
  34340. ExpectNotNull(d = BN_new());
  34341. /* Invalid parameter testing. */
  34342. BN_free(NULL);
  34343. ExpectNull(BN_dup(NULL));
  34344. ExpectNull(BN_dup(&emptyBN));
  34345. ExpectNull(BN_copy(NULL, NULL));
  34346. ExpectNull(BN_copy(b, NULL));
  34347. ExpectNull(BN_copy(NULL, c));
  34348. ExpectNull(BN_copy(b, &emptyBN));
  34349. ExpectNull(BN_copy(&emptyBN, c));
  34350. BN_clear(NULL);
  34351. BN_clear(&emptyBN);
  34352. ExpectIntEQ(BN_num_bytes(NULL), 0);
  34353. ExpectIntEQ(BN_num_bytes(&emptyBN), 0);
  34354. ExpectIntEQ(BN_num_bits(NULL), 0);
  34355. ExpectIntEQ(BN_num_bits(&emptyBN), 0);
  34356. ExpectIntEQ(BN_is_negative(NULL), 0);
  34357. ExpectIntEQ(BN_is_negative(&emptyBN), 0);
  34358. /* END Invalid Parameters */
  34359. ExpectIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  34360. ExpectIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  34361. ExpectIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  34362. ExpectIntEQ(BN_num_bits(a), 2);
  34363. ExpectIntEQ(BN_num_bytes(a), 1);
  34364. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  34365. defined(WOLFSSL_SP_INT_NEGATIVE))
  34366. ExpectIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  34367. ExpectIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  34368. ExpectIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  34369. ExpectIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  34370. ExpectIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  34371. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34372. {
  34373. /* Do additional tests on negative BN conversions. */
  34374. char* ret = NULL;
  34375. ASN1_INTEGER* asn1 = NULL;
  34376. BIGNUM* tmp = NULL;
  34377. /* Sanity check we have a negative BN. */
  34378. ExpectIntEQ(BN_is_negative(c), 1);
  34379. ExpectNotNull(ret = BN_bn2dec(c));
  34380. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  34381. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  34382. ret = NULL;
  34383. /* Convert to ASN1_INTEGER and back to BN. */
  34384. ExpectNotNull(asn1 = BN_to_ASN1_INTEGER(c, NULL));
  34385. ExpectNotNull(tmp = ASN1_INTEGER_to_BN(asn1, NULL));
  34386. /* After converting back BN should be negative and correct. */
  34387. ExpectIntEQ(BN_is_negative(tmp), 1);
  34388. ExpectNotNull(ret = BN_bn2dec(tmp));
  34389. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  34390. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  34391. ASN1_INTEGER_free(asn1);
  34392. BN_free(tmp);
  34393. }
  34394. #endif
  34395. ExpectIntEQ(BN_get_word(c), 4);
  34396. #endif
  34397. ExpectIntEQ(BN_set_word(a, 3), 1);
  34398. ExpectIntEQ(BN_set_word(b, 3), 1);
  34399. ExpectIntEQ(BN_set_word(c, 4), 1);
  34400. /* NULL == NULL, NULL < num, num > NULL */
  34401. ExpectIntEQ(BN_cmp(NULL, NULL), 0);
  34402. ExpectIntEQ(BN_cmp(&emptyBN, &emptyBN), 0);
  34403. ExpectIntLT(BN_cmp(NULL, b), 0);
  34404. ExpectIntLT(BN_cmp(&emptyBN, b), 0);
  34405. ExpectIntGT(BN_cmp(a, NULL), 0);
  34406. ExpectIntGT(BN_cmp(a, &emptyBN), 0);
  34407. ExpectIntEQ(BN_cmp(a, b), 0);
  34408. ExpectIntLT(BN_cmp(a, c), 0);
  34409. ExpectIntGT(BN_cmp(c, b), 0);
  34410. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  34411. ExpectIntEQ(BN_print_fp(XBADFILE, NULL), 0);
  34412. ExpectIntEQ(BN_print_fp(XBADFILE, &emptyBN), 0);
  34413. ExpectIntEQ(BN_print_fp(stderr, NULL), 0);
  34414. ExpectIntEQ(BN_print_fp(stderr, &emptyBN), 0);
  34415. ExpectIntEQ(BN_print_fp(XBADFILE, a), 0);
  34416. ExpectIntEQ(BN_print_fp(stderr, a), 1);
  34417. #endif
  34418. BN_clear(a);
  34419. BN_free(a);
  34420. BN_free(b);
  34421. BN_free(c);
  34422. BN_clear_free(d);
  34423. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34424. return EXPECT_RESULT();
  34425. }
  34426. static int test_wolfSSL_BN_init(void)
  34427. {
  34428. EXPECT_DECLS;
  34429. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34430. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34431. #if !defined(USE_INTEGER_HEAP_MATH) && !defined(HAVE_WOLF_BIGINT)
  34432. BIGNUM* ap = NULL;
  34433. BIGNUM bv;
  34434. BIGNUM cv;
  34435. BIGNUM dv;
  34436. ExpectNotNull(ap = BN_new());
  34437. BN_init(NULL);
  34438. XMEMSET(&bv, 0, sizeof(bv));
  34439. ExpectNull(BN_dup(&bv));
  34440. BN_init(&bv);
  34441. BN_init(&cv);
  34442. BN_init(&dv);
  34443. ExpectIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  34444. ExpectIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  34445. ExpectIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  34446. /* a^b mod c = */
  34447. ExpectIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  34448. ExpectIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  34449. /* check result 3^2 mod 5 */
  34450. ExpectIntEQ(BN_get_word(&dv), 4);
  34451. /* a*b mod c = */
  34452. ExpectIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  34453. ExpectIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  34454. /* check result 3*2 mod 5 */
  34455. ExpectIntEQ(BN_get_word(&dv), 1);
  34456. BN_free(ap);
  34457. #endif
  34458. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34459. return EXPECT_RESULT();
  34460. }
  34461. static int test_wolfSSL_BN_enc_dec(void)
  34462. {
  34463. EXPECT_DECLS;
  34464. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  34465. BIGNUM* a = NULL;
  34466. BIGNUM* b = NULL;
  34467. BIGNUM* c = NULL;
  34468. BIGNUM emptyBN;
  34469. char* str = NULL;
  34470. const char* emptyStr = "";
  34471. const char* numberStr = "12345";
  34472. const char* badStr = "g12345";
  34473. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34474. const char* twoStr = "2";
  34475. #endif
  34476. unsigned char binNum[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  34477. unsigned char outNum[5];
  34478. /* Setup */
  34479. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34480. ExpectNotNull(a = BN_new());
  34481. ExpectNotNull(b = BN_new());
  34482. ExpectIntEQ(BN_set_word(a, 2), 1);
  34483. /* Invalid parameters */
  34484. ExpectIntEQ(BN_bn2bin(NULL, NULL), -1);
  34485. ExpectIntEQ(BN_bn2bin(&emptyBN, NULL), -1);
  34486. ExpectIntEQ(BN_bn2bin(NULL, outNum), -1);
  34487. ExpectIntEQ(BN_bn2bin(&emptyBN, outNum), -1);
  34488. ExpectNull(BN_bn2hex(NULL));
  34489. ExpectNull(BN_bn2hex(&emptyBN));
  34490. ExpectNull(BN_bn2dec(NULL));
  34491. ExpectNull(BN_bn2dec(&emptyBN));
  34492. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), NULL));
  34493. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), a));
  34494. ExpectNull(BN_bin2bn(binNum, -1, a));
  34495. ExpectNull(BN_bin2bn(binNum, -1, NULL));
  34496. ExpectNull(BN_bin2bn(binNum, sizeof(binNum), &emptyBN));
  34497. ExpectIntEQ(BN_hex2bn(NULL, NULL), 0);
  34498. ExpectIntEQ(BN_hex2bn(NULL, numberStr), 0);
  34499. ExpectIntEQ(BN_hex2bn(&a, NULL), 0);
  34500. ExpectIntEQ(BN_hex2bn(&a, emptyStr), 0);
  34501. ExpectIntEQ(BN_hex2bn(&a, badStr), 0);
  34502. ExpectIntEQ(BN_hex2bn(&c, badStr), 0);
  34503. ExpectIntEQ(BN_dec2bn(NULL, NULL), 0);
  34504. ExpectIntEQ(BN_dec2bn(NULL, numberStr), 0);
  34505. ExpectIntEQ(BN_dec2bn(&a, NULL), 0);
  34506. ExpectIntEQ(BN_dec2bn(&a, emptyStr), 0);
  34507. ExpectIntEQ(BN_dec2bn(&a, badStr), 0);
  34508. ExpectIntEQ(BN_dec2bn(&c, badStr), 0);
  34509. ExpectIntEQ(BN_set_word(a, 2), 1);
  34510. ExpectIntEQ(BN_bn2bin(a, NULL), 1);
  34511. ExpectIntEQ(BN_bn2bin(a, outNum), 1);
  34512. ExpectNotNull(BN_bin2bn(outNum, 1, b));
  34513. ExpectIntEQ(BN_cmp(a, b), 0);
  34514. ExpectNotNull(BN_bin2bn(binNum, sizeof(binNum), b));
  34515. ExpectIntEQ(BN_cmp(a, b), -1);
  34516. ExpectNotNull(str = BN_bn2hex(a));
  34517. ExpectNotNull(BN_hex2bn(&b, str));
  34518. ExpectIntEQ(BN_cmp(a, b), 0);
  34519. ExpectNotNull(BN_hex2bn(&b, numberStr));
  34520. ExpectIntEQ(BN_cmp(a, b), -1);
  34521. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34522. str = NULL;
  34523. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34524. ExpectNotNull(str = BN_bn2dec(a));
  34525. ExpectStrEQ(str, twoStr);
  34526. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34527. str = NULL;
  34528. #ifndef NO_RSA
  34529. ExpectNotNull(str = BN_bn2dec(a));
  34530. ExpectNotNull(BN_dec2bn(&b, str));
  34531. ExpectIntEQ(BN_cmp(a, b), 0);
  34532. ExpectNotNull(BN_dec2bn(&b, numberStr));
  34533. ExpectIntEQ(BN_cmp(a, b), -1);
  34534. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34535. str = NULL;
  34536. #else
  34537. /* No implementation - fail with good parameters. */
  34538. ExpectIntEQ(BN_dec2bn(&a, numberStr), 0);
  34539. #endif
  34540. #endif
  34541. BN_free(b);
  34542. BN_free(a);
  34543. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34544. return EXPECT_RESULT();
  34545. }
  34546. static int test_wolfSSL_BN_word(void)
  34547. {
  34548. EXPECT_DECLS;
  34549. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  34550. BIGNUM* a = NULL;
  34551. BIGNUM* b = NULL;
  34552. BIGNUM* c = NULL;
  34553. BIGNUM av;
  34554. ExpectNotNull(a = BN_new());
  34555. ExpectNotNull(b = BN_new());
  34556. ExpectNotNull(c = BN_new());
  34557. XMEMSET(&av, 0, sizeof(av));
  34558. /* Invalid parameter. */
  34559. ExpectIntEQ(BN_add_word(NULL, 3), 0);
  34560. ExpectIntEQ(BN_add_word(&av, 3), 0);
  34561. ExpectIntEQ(BN_sub_word(NULL, 3), 0);
  34562. ExpectIntEQ(BN_sub_word(&av, 3), 0);
  34563. ExpectIntEQ(BN_set_word(NULL, 3), 0);
  34564. ExpectIntEQ(BN_set_word(&av, 3), 0);
  34565. ExpectIntEQ(BN_get_word(NULL), 0);
  34566. ExpectIntEQ(BN_get_word(&av), 0);
  34567. ExpectIntEQ(BN_is_word(NULL, 3), 0);
  34568. ExpectIntEQ(BN_is_word(&av, 3), 0);
  34569. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  34570. !defined(NO_DSA))
  34571. ExpectIntEQ(BN_mod_word(NULL, 3), -1);
  34572. ExpectIntEQ(BN_mod_word(&av, 3), -1);
  34573. #endif
  34574. ExpectIntEQ(BN_one(NULL), 0);
  34575. ExpectIntEQ(BN_one(&av), 0);
  34576. BN_zero(NULL);
  34577. BN_zero(&av);
  34578. ExpectIntEQ(BN_is_one(NULL), 0);
  34579. ExpectIntEQ(BN_is_one(&av), 0);
  34580. ExpectIntEQ(BN_is_zero(NULL), 0);
  34581. ExpectIntEQ(BN_is_zero(&av), 0);
  34582. ExpectIntEQ(BN_set_word(a, 3), 1);
  34583. ExpectIntEQ(BN_set_word(b, 2), 1);
  34584. ExpectIntEQ(BN_set_word(c, 5), 1);
  34585. /* a + 3 = */
  34586. ExpectIntEQ(BN_add_word(a, 3), 1);
  34587. /* check result 3 + 3*/
  34588. ExpectIntEQ(BN_get_word(a), 6);
  34589. ExpectIntEQ(BN_is_word(a, 6), 1);
  34590. ExpectIntEQ(BN_is_word(a, 5), 0);
  34591. /* set a back to 3 */
  34592. ExpectIntEQ(BN_set_word(a, 3), 1);
  34593. /* a - 3 = */
  34594. ExpectIntEQ(BN_sub_word(a, 3), 1);
  34595. /* check result 3 - 3*/
  34596. ExpectIntEQ(BN_get_word(a), 0);
  34597. ExpectIntEQ(BN_one(a), 1);
  34598. ExpectIntEQ(BN_is_word(a, 1), 1);
  34599. ExpectIntEQ(BN_is_word(a, 0), 0);
  34600. ExpectIntEQ(BN_is_one(a), 1);
  34601. ExpectIntEQ(BN_is_zero(a), 0);
  34602. BN_zero(a);
  34603. ExpectIntEQ(BN_is_word(a, 0), 1);
  34604. ExpectIntEQ(BN_is_word(a, 1), 0);
  34605. ExpectIntEQ(BN_is_zero(a), 1);
  34606. ExpectIntEQ(BN_is_one(a), 0);
  34607. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  34608. !defined(NO_DSA))
  34609. ExpectIntEQ(BN_set_word(a, 5), 1);
  34610. ExpectIntEQ(BN_mod_word(a, 3), 2);
  34611. ExpectIntEQ(BN_mod_word(a, 0), -1);
  34612. #endif
  34613. BN_free(c);
  34614. BN_free(b);
  34615. BN_free(a);
  34616. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34617. return EXPECT_RESULT();
  34618. }
  34619. static int test_wolfSSL_BN_bits(void)
  34620. {
  34621. EXPECT_DECLS;
  34622. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34623. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34624. BIGNUM* a = NULL;
  34625. BIGNUM emptyBN;
  34626. /* Setup */
  34627. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34628. ExpectNotNull(a = BN_new());
  34629. /* Invalid parameters. */
  34630. ExpectIntEQ(BN_set_bit(NULL, 1), 0);
  34631. ExpectIntEQ(BN_set_bit(&emptyBN, 1), 0);
  34632. ExpectIntEQ(BN_set_bit(a, -1), 0);
  34633. ExpectIntEQ(BN_clear_bit(NULL, 1), 0);
  34634. ExpectIntEQ(BN_clear_bit(&emptyBN, 1), 0);
  34635. ExpectIntEQ(BN_clear_bit(a, -1), 0);
  34636. ExpectIntEQ(BN_is_bit_set(NULL, 1), 0);
  34637. ExpectIntEQ(BN_is_bit_set(&emptyBN, 1), 0);
  34638. ExpectIntEQ(BN_is_bit_set(a, -1), 0);
  34639. ExpectIntEQ(BN_is_odd(NULL), 0);
  34640. ExpectIntEQ(BN_is_odd(&emptyBN), 0);
  34641. ExpectIntEQ(BN_set_word(a, 0), 1);
  34642. ExpectIntEQ(BN_is_zero(a), 1);
  34643. ExpectIntEQ(BN_set_bit(a, 0x45), 1);
  34644. ExpectIntEQ(BN_is_zero(a), 0);
  34645. ExpectIntEQ(BN_is_bit_set(a, 0x45), 1);
  34646. ExpectIntEQ(BN_clear_bit(a, 0x45), 1);
  34647. ExpectIntEQ(BN_is_bit_set(a, 0x45), 0);
  34648. ExpectIntEQ(BN_is_zero(a), 1);
  34649. ExpectIntEQ(BN_set_bit(a, 0), 1);
  34650. ExpectIntEQ(BN_is_odd(a), 1);
  34651. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  34652. ExpectIntEQ(BN_is_odd(a), 0);
  34653. ExpectIntEQ(BN_set_bit(a, 1), 1);
  34654. ExpectIntEQ(BN_is_odd(a), 0);
  34655. ExpectIntEQ(BN_set_bit(a, 129), 1);
  34656. ExpectIntEQ(BN_get_word(a), WOLFSSL_BN_MAX_VAL);
  34657. #ifndef NO_WOLFSSL_STUB
  34658. ExpectIntEQ(BN_mask_bits(a, 1), 0);
  34659. #endif
  34660. BN_free(a);
  34661. #endif
  34662. return EXPECT_RESULT();
  34663. }
  34664. static int test_wolfSSL_BN_shift(void)
  34665. {
  34666. EXPECT_DECLS;
  34667. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34668. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34669. BIGNUM* a = NULL;
  34670. BIGNUM* b = NULL;
  34671. BIGNUM emptyBN;
  34672. /* Setup */
  34673. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34674. ExpectNotNull(a = BN_new());
  34675. ExpectNotNull(b = BN_new());
  34676. /* Invalid parameters. */
  34677. ExpectIntEQ(BN_lshift(NULL, NULL, 1), 0);
  34678. ExpectIntEQ(BN_lshift(&emptyBN, NULL, 1), 0);
  34679. ExpectIntEQ(BN_lshift(NULL, &emptyBN, 1), 0);
  34680. ExpectIntEQ(BN_lshift(b, NULL, 1), 0);
  34681. ExpectIntEQ(BN_lshift(b, &emptyBN, 1), 0);
  34682. ExpectIntEQ(BN_lshift(NULL, a, 1), 0);
  34683. ExpectIntEQ(BN_lshift(&emptyBN, a, 1), 0);
  34684. ExpectIntEQ(BN_lshift(b, a, -1), 0);
  34685. ExpectIntEQ(BN_rshift(NULL, NULL, 1), 0);
  34686. ExpectIntEQ(BN_rshift(&emptyBN, NULL, 1), 0);
  34687. ExpectIntEQ(BN_rshift(NULL, &emptyBN, 1), 0);
  34688. ExpectIntEQ(BN_rshift(b, NULL, 1), 0);
  34689. ExpectIntEQ(BN_rshift(b, &emptyBN, 1), 0);
  34690. ExpectIntEQ(BN_rshift(NULL, a, 1), 0);
  34691. ExpectIntEQ(BN_rshift(&emptyBN, a, 1), 0);
  34692. ExpectIntEQ(BN_rshift(b, a, -1), 0);
  34693. ExpectIntEQ(BN_set_word(a, 1), 1);
  34694. ExpectIntEQ(BN_lshift(b, a, 1), 1);
  34695. ExpectIntEQ(BN_is_word(b, 2), 1);
  34696. ExpectIntEQ(BN_lshift(a, a, 1), 1);
  34697. ExpectIntEQ(BN_is_word(a, 2), 1);
  34698. ExpectIntEQ(BN_rshift(b, a, 1), 1);
  34699. ExpectIntEQ(BN_is_word(b, 1), 1);
  34700. ExpectIntEQ(BN_rshift(a, a, 1), 1);
  34701. ExpectIntEQ(BN_is_word(a, 1), 1);
  34702. BN_free(b);
  34703. BN_free(a);
  34704. #endif
  34705. return EXPECT_RESULT();
  34706. }
  34707. static int test_wolfSSL_BN_math(void)
  34708. {
  34709. EXPECT_DECLS;
  34710. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34711. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34712. BIGNUM* a = NULL;
  34713. BIGNUM* b = NULL;
  34714. BIGNUM* r = NULL;
  34715. BIGNUM* rem = NULL;
  34716. BIGNUM emptyBN;
  34717. BN_ULONG val1;
  34718. BN_ULONG val2;
  34719. /* Setup */
  34720. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34721. ExpectNotNull(a = BN_new());
  34722. ExpectNotNull(b = BN_new());
  34723. ExpectNotNull(r = BN_new());
  34724. ExpectNotNull(rem = BN_new());
  34725. /* Invalid parameters. */
  34726. ExpectIntEQ(BN_add(NULL, NULL, NULL), 0);
  34727. ExpectIntEQ(BN_add(r, NULL, NULL), 0);
  34728. ExpectIntEQ(BN_add(NULL, a, NULL), 0);
  34729. ExpectIntEQ(BN_add(NULL, NULL, b), 0);
  34730. ExpectIntEQ(BN_add(r, a, NULL), 0);
  34731. ExpectIntEQ(BN_add(r, NULL, b), 0);
  34732. ExpectIntEQ(BN_add(NULL, a, b), 0);
  34733. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, &emptyBN), 0);
  34734. ExpectIntEQ(BN_add(r, &emptyBN, &emptyBN), 0);
  34735. ExpectIntEQ(BN_add(&emptyBN, a, &emptyBN), 0);
  34736. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, b), 0);
  34737. ExpectIntEQ(BN_add(r, a, &emptyBN), 0);
  34738. ExpectIntEQ(BN_add(r, &emptyBN, b), 0);
  34739. ExpectIntEQ(BN_add(&emptyBN, a, b), 0);
  34740. ExpectIntEQ(BN_sub(NULL, NULL, NULL), 0);
  34741. ExpectIntEQ(BN_sub(r, NULL, NULL), 0);
  34742. ExpectIntEQ(BN_sub(NULL, a, NULL), 0);
  34743. ExpectIntEQ(BN_sub(NULL, NULL, b), 0);
  34744. ExpectIntEQ(BN_sub(r, a, NULL), 0);
  34745. ExpectIntEQ(BN_sub(r, NULL, b), 0);
  34746. ExpectIntEQ(BN_sub(NULL, a, b), 0);
  34747. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, &emptyBN), 0);
  34748. ExpectIntEQ(BN_sub(r, &emptyBN, &emptyBN), 0);
  34749. ExpectIntEQ(BN_sub(&emptyBN, a, &emptyBN), 0);
  34750. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, b), 0);
  34751. ExpectIntEQ(BN_sub(r, a, &emptyBN), 0);
  34752. ExpectIntEQ(BN_sub(r, &emptyBN, b), 0);
  34753. ExpectIntEQ(BN_sub(&emptyBN, a, b), 0);
  34754. ExpectIntEQ(BN_mul(NULL, NULL, NULL, NULL), 0);
  34755. ExpectIntEQ(BN_mul(r, NULL, NULL, NULL), 0);
  34756. ExpectIntEQ(BN_mul(NULL, a, NULL, NULL), 0);
  34757. ExpectIntEQ(BN_mul(NULL, NULL, b, NULL), 0);
  34758. ExpectIntEQ(BN_mul(r, a, NULL, NULL), 0);
  34759. ExpectIntEQ(BN_mul(r, NULL, b, NULL), 0);
  34760. ExpectIntEQ(BN_mul(NULL, a, b, NULL), 0);
  34761. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34762. ExpectIntEQ(BN_mul(r, &emptyBN, &emptyBN, NULL), 0);
  34763. ExpectIntEQ(BN_mul(&emptyBN, a, &emptyBN, NULL), 0);
  34764. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, b, NULL), 0);
  34765. ExpectIntEQ(BN_mul(r, a, &emptyBN, NULL), 0);
  34766. ExpectIntEQ(BN_mul(r, &emptyBN, b, NULL), 0);
  34767. ExpectIntEQ(BN_mul(&emptyBN, a, b, NULL), 0);
  34768. ExpectIntEQ(BN_div(NULL, NULL, NULL, NULL, NULL), 0);
  34769. ExpectIntEQ(BN_div(r, NULL, NULL, NULL, NULL), 0);
  34770. ExpectIntEQ(BN_div(NULL, rem, NULL, NULL, NULL), 0);
  34771. ExpectIntEQ(BN_div(NULL, NULL, a, NULL, NULL), 0);
  34772. ExpectIntEQ(BN_div(NULL, NULL, NULL, b, NULL), 0);
  34773. ExpectIntEQ(BN_div(NULL, rem, a, b, NULL), 0);
  34774. ExpectIntEQ(BN_div(r, NULL, a, b, NULL), 0);
  34775. ExpectIntEQ(BN_div(r, rem, NULL, b, NULL), 0);
  34776. ExpectIntEQ(BN_div(r, rem, a, NULL, NULL), 0);
  34777. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34778. ExpectIntEQ(BN_div(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34779. ExpectIntEQ(BN_div(&emptyBN, rem, &emptyBN, &emptyBN, NULL), 0);
  34780. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, a, &emptyBN, NULL), 0);
  34781. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, b, NULL), 0);
  34782. ExpectIntEQ(BN_div(&emptyBN, rem, a, b, NULL), 0);
  34783. ExpectIntEQ(BN_div(r, &emptyBN, a, b, NULL), 0);
  34784. ExpectIntEQ(BN_div(r, rem, &emptyBN, b, NULL), 0);
  34785. ExpectIntEQ(BN_div(r, rem, a, &emptyBN, NULL), 0);
  34786. ExpectIntEQ(BN_mod(NULL, NULL, NULL, NULL), 0);
  34787. ExpectIntEQ(BN_mod(r, NULL, NULL, NULL), 0);
  34788. ExpectIntEQ(BN_mod(NULL, a, NULL, NULL), 0);
  34789. ExpectIntEQ(BN_mod(NULL, NULL, b, NULL), 0);
  34790. ExpectIntEQ(BN_mod(r, a, NULL, NULL), 0);
  34791. ExpectIntEQ(BN_mod(r, NULL, b, NULL), 0);
  34792. ExpectIntEQ(BN_mod(NULL, a, b, NULL), 0);
  34793. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34794. ExpectIntEQ(BN_mod(r, &emptyBN, &emptyBN, NULL), 0);
  34795. ExpectIntEQ(BN_mod(&emptyBN, a, &emptyBN, NULL), 0);
  34796. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, b, NULL), 0);
  34797. ExpectIntEQ(BN_mod(r, a, &emptyBN, NULL), 0);
  34798. ExpectIntEQ(BN_mod(r, &emptyBN, b, NULL), 0);
  34799. ExpectIntEQ(BN_mod(&emptyBN, a, b, NULL), 0);
  34800. /* END Invalid parameters. */
  34801. val1 = 8;
  34802. val2 = 3;
  34803. ExpectIntEQ(BN_set_word(a, val1), 1);
  34804. ExpectIntEQ(BN_set_word(b, val2), 1);
  34805. ExpectIntEQ(BN_add(r, a, b), 1);
  34806. ExpectIntEQ(BN_is_word(r, val1 + val2), 1);
  34807. ExpectIntEQ(BN_sub(r, a, b), 1);
  34808. ExpectIntEQ(BN_is_word(r, val1 - val2), 1);
  34809. ExpectIntEQ(BN_mul(r, a, b, NULL), 1);
  34810. ExpectIntEQ(BN_is_word(r, val1 * val2), 1);
  34811. ExpectIntEQ(BN_div(r, rem, a, b, NULL), 1);
  34812. ExpectIntEQ(BN_is_word(r, val1 / val2), 1);
  34813. ExpectIntEQ(BN_is_word(rem, val1 % val2), 1);
  34814. ExpectIntEQ(BN_mod(r, a, b, NULL), 1);
  34815. ExpectIntEQ(BN_is_word(r, val1 % val2), 1);
  34816. BN_free(rem);
  34817. BN_free(r);
  34818. BN_free(b);
  34819. BN_free(a);
  34820. #endif
  34821. return EXPECT_RESULT();
  34822. }
  34823. static int test_wolfSSL_BN_math_mod(void)
  34824. {
  34825. EXPECT_DECLS;
  34826. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34827. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34828. BIGNUM* a = NULL;
  34829. BIGNUM* b = NULL;
  34830. BIGNUM* m = NULL;
  34831. BIGNUM* r = NULL;
  34832. BIGNUM* t = NULL;
  34833. BIGNUM emptyBN;
  34834. BN_ULONG val1;
  34835. BN_ULONG val2;
  34836. BN_ULONG val3;
  34837. /* Setup */
  34838. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34839. ExpectNotNull(a = BN_new());
  34840. ExpectNotNull(b = BN_new());
  34841. ExpectNotNull(m = BN_new());
  34842. ExpectNotNull(r = BN_new());
  34843. /* Invalid parameters. */
  34844. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, NULL, NULL), 0);
  34845. ExpectIntEQ(BN_mod_add(r, NULL, NULL, NULL, NULL), 0);
  34846. ExpectIntEQ(BN_mod_add(NULL, a, NULL, NULL, NULL), 0);
  34847. ExpectIntEQ(BN_mod_add(NULL, NULL, b, NULL, NULL), 0);
  34848. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, m, NULL), 0);
  34849. ExpectIntEQ(BN_mod_add(NULL, a, b, m, NULL), 0);
  34850. ExpectIntEQ(BN_mod_add(r, NULL, b, m, NULL), 0);
  34851. ExpectIntEQ(BN_mod_add(r, a, NULL, m, NULL), 0);
  34852. ExpectIntEQ(BN_mod_add(r, a, m, NULL, NULL), 0);
  34853. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34854. ExpectIntEQ(BN_mod_add(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34855. ExpectIntEQ(BN_mod_add(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34856. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34857. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34858. ExpectIntEQ(BN_mod_add(&emptyBN, a, b, m, NULL), 0);
  34859. ExpectIntEQ(BN_mod_add(r, &emptyBN, b, m, NULL), 0);
  34860. ExpectIntEQ(BN_mod_add(r, a, &emptyBN, m, NULL), 0);
  34861. ExpectIntEQ(BN_mod_add(r, a, m, &emptyBN, NULL), 0);
  34862. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, NULL, NULL), 0);
  34863. ExpectIntEQ(BN_mod_mul(r, NULL, NULL, NULL, NULL), 0);
  34864. ExpectIntEQ(BN_mod_mul(NULL, a, NULL, NULL, NULL), 0);
  34865. ExpectIntEQ(BN_mod_mul(NULL, NULL, b, NULL, NULL), 0);
  34866. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, m, NULL), 0);
  34867. ExpectIntEQ(BN_mod_mul(NULL, a, b, m, NULL), 0);
  34868. ExpectIntEQ(BN_mod_mul(r, NULL, b, m, NULL), 0);
  34869. ExpectIntEQ(BN_mod_mul(r, a, NULL, m, NULL), 0);
  34870. ExpectIntEQ(BN_mod_mul(r, a, m, NULL, NULL), 0);
  34871. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34872. ExpectIntEQ(BN_mod_mul(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34873. ExpectIntEQ(BN_mod_mul(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34874. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34875. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34876. ExpectIntEQ(BN_mod_mul(&emptyBN, a, b, m, NULL), 0);
  34877. ExpectIntEQ(BN_mod_mul(r, &emptyBN, b, m, NULL), 0);
  34878. ExpectIntEQ(BN_mod_mul(r, a, &emptyBN, m, NULL), 0);
  34879. ExpectIntEQ(BN_mod_mul(r, a, m, &emptyBN, NULL), 0);
  34880. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, NULL, NULL), 0);
  34881. ExpectIntEQ(BN_mod_exp(r, NULL, NULL, NULL, NULL), 0);
  34882. ExpectIntEQ(BN_mod_exp(NULL, a, NULL, NULL, NULL), 0);
  34883. ExpectIntEQ(BN_mod_exp(NULL, NULL, b, NULL, NULL), 0);
  34884. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, m, NULL), 0);
  34885. ExpectIntEQ(BN_mod_exp(NULL, a, b, m, NULL), 0);
  34886. ExpectIntEQ(BN_mod_exp(r, NULL, b, m, NULL), 0);
  34887. ExpectIntEQ(BN_mod_exp(r, a, NULL, m, NULL), 0);
  34888. ExpectIntEQ(BN_mod_exp(r, a, m, NULL, NULL), 0);
  34889. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34890. ExpectIntEQ(BN_mod_exp(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34891. ExpectIntEQ(BN_mod_exp(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34892. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34893. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34894. ExpectIntEQ(BN_mod_exp(&emptyBN, a, b, m, NULL), 0);
  34895. ExpectIntEQ(BN_mod_exp(r, &emptyBN, b, m, NULL), 0);
  34896. ExpectIntEQ(BN_mod_exp(r, a, &emptyBN, m, NULL), 0);
  34897. ExpectIntEQ(BN_mod_exp(r, a, m, &emptyBN, NULL), 0);
  34898. ExpectNull(BN_mod_inverse(r, NULL, NULL, NULL));
  34899. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  34900. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  34901. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  34902. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  34903. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, &emptyBN, NULL));
  34904. ExpectNull(BN_mod_inverse(r, &emptyBN, &emptyBN, NULL));
  34905. ExpectNull(BN_mod_inverse(&emptyBN, a, &emptyBN, NULL));
  34906. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, m, NULL));
  34907. ExpectNull(BN_mod_inverse(&emptyBN, a, m, NULL));
  34908. ExpectNull(BN_mod_inverse(r, &emptyBN, m, NULL));
  34909. ExpectNull(BN_mod_inverse(r, a, &emptyBN, NULL));
  34910. /* END Invalid parameters. */
  34911. val1 = 9;
  34912. val2 = 13;
  34913. val3 = 5;
  34914. ExpectIntEQ(BN_set_word(a, val1), 1);
  34915. ExpectIntEQ(BN_set_word(b, val2), 1);
  34916. ExpectIntEQ(BN_set_word(m, val3), 1);
  34917. ExpectIntEQ(BN_mod_add(r, a, b, m, NULL), 1);
  34918. ExpectIntEQ(BN_is_word(r, (val1 + val2) % val3), 1);
  34919. ExpectIntEQ(BN_mod_mul(r, a, b, m, NULL), 1);
  34920. ExpectIntEQ(BN_is_word(r, (val1 * val2) % val3), 1);
  34921. ExpectIntEQ(BN_set_word(a, 2), 1);
  34922. ExpectIntEQ(BN_set_word(b, 3), 1);
  34923. ExpectIntEQ(BN_set_word(m, 5), 1);
  34924. /* (2 ^ 3) % 5 = 8 % 5 = 3 */
  34925. ExpectIntEQ(BN_mod_exp(r, a, b, m, NULL), 1);
  34926. ExpectIntEQ(BN_is_word(r, 3), 1);
  34927. /* (2 * 3) % 5 = 6 % 5 = 1 => inv = 3 */
  34928. ExpectNotNull(BN_mod_inverse(r, a, m, NULL));
  34929. ExpectIntEQ(BN_is_word(r, 3), 1);
  34930. ExpectNotNull(t = BN_mod_inverse(NULL, a, m, NULL));
  34931. ExpectIntEQ(BN_is_word(t, 3), 1);
  34932. BN_free(t);
  34933. /* No inverse case. No inverse when a divides b. */
  34934. ExpectIntEQ(BN_set_word(a, 3), 1);
  34935. ExpectIntEQ(BN_set_word(m, 9), 1);
  34936. ExpectNull(BN_mod_inverse(r, a, m, NULL));
  34937. BN_free(r);
  34938. BN_free(m);
  34939. BN_free(b);
  34940. BN_free(a);
  34941. #endif
  34942. return EXPECT_RESULT();
  34943. }
  34944. static int test_wolfSSL_BN_math_other(void)
  34945. {
  34946. EXPECT_DECLS;
  34947. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34948. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34949. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  34950. BIGNUM* a = NULL;
  34951. BIGNUM* b = NULL;
  34952. BIGNUM* r = NULL;
  34953. BIGNUM emptyBN;
  34954. /* Setup */
  34955. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34956. ExpectNotNull(a = BN_new());
  34957. ExpectNotNull(b = BN_new());
  34958. ExpectNotNull(r = BN_new());
  34959. /* Invalid parameters. */
  34960. ExpectIntEQ(BN_gcd(NULL, NULL, NULL, NULL), 0);
  34961. ExpectIntEQ(BN_gcd(r, NULL, NULL, NULL), 0);
  34962. ExpectIntEQ(BN_gcd(NULL, a, NULL, NULL), 0);
  34963. ExpectIntEQ(BN_gcd(NULL, NULL, b, NULL), 0);
  34964. ExpectIntEQ(BN_gcd(NULL, a, b, NULL), 0);
  34965. ExpectIntEQ(BN_gcd(r, NULL, b, NULL), 0);
  34966. ExpectIntEQ(BN_gcd(r, a, NULL, NULL), 0);
  34967. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34968. ExpectIntEQ(BN_gcd(r, &emptyBN, &emptyBN, NULL), 0);
  34969. ExpectIntEQ(BN_gcd(&emptyBN, a, &emptyBN, NULL), 0);
  34970. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, b, NULL), 0);
  34971. ExpectIntEQ(BN_gcd(&emptyBN, a, b, NULL), 0);
  34972. ExpectIntEQ(BN_gcd(r, &emptyBN, b, NULL), 0);
  34973. ExpectIntEQ(BN_gcd(r, a, &emptyBN, NULL), 0);
  34974. /* END Invalid parameters. */
  34975. /* No common factors between 2 and 3. */
  34976. ExpectIntEQ(BN_set_word(a, 2), 1);
  34977. ExpectIntEQ(BN_set_word(b, 3), 1);
  34978. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  34979. ExpectIntEQ(BN_is_word(r, 1), 1);
  34980. /* 3 is largest value that divides both 6 and 9. */
  34981. ExpectIntEQ(BN_set_word(a, 6), 1);
  34982. ExpectIntEQ(BN_set_word(b, 9), 1);
  34983. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  34984. ExpectIntEQ(BN_is_word(r, 3), 1);
  34985. /* GCD of 0 and 0 is undefined. */
  34986. ExpectIntEQ(BN_set_word(a, 0), 1);
  34987. ExpectIntEQ(BN_set_word(b, 0), 1);
  34988. ExpectIntEQ(BN_gcd(r, a, b, NULL), 0);
  34989. /* Teardown */
  34990. BN_free(r);
  34991. BN_free(b);
  34992. BN_free(a);
  34993. #endif
  34994. #endif
  34995. return EXPECT_RESULT();
  34996. }
  34997. static int test_wolfSSL_BN_rand(void)
  34998. {
  34999. EXPECT_DECLS;
  35000. #if defined(OPENSSL_EXTRA) && !defined(OPENSSL_EXTRA_NO_BN)
  35001. BIGNUM* bn = NULL;
  35002. BIGNUM* range = NULL;
  35003. BIGNUM emptyBN;
  35004. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35005. ExpectNotNull(bn = BN_new());
  35006. ExpectNotNull(range = BN_new());
  35007. /* Invalid parameters. */
  35008. ExpectIntEQ(BN_rand(NULL, -1, 0, 0), 0);
  35009. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  35010. ExpectIntEQ(BN_rand(NULL, 1, 0, 0), 0);
  35011. ExpectIntEQ(BN_rand(&emptyBN, -1, 0, 0), 0);
  35012. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  35013. ExpectIntEQ(BN_rand(&emptyBN, 1, 0, 0), 0);
  35014. ExpectIntEQ(BN_pseudo_rand(NULL, -1, 0, 0), 0);
  35015. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  35016. ExpectIntEQ(BN_pseudo_rand(NULL, 1, 0, 0), 0);
  35017. ExpectIntEQ(BN_pseudo_rand(&emptyBN, -1, 0, 0), 0);
  35018. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  35019. ExpectIntEQ(BN_pseudo_rand(&emptyBN, 1, 0, 0), 0);
  35020. ExpectIntEQ(BN_rand_range(NULL, NULL), 0);
  35021. ExpectIntEQ(BN_rand_range(bn, NULL), 0);
  35022. ExpectIntEQ(BN_rand_range(NULL, range), 0);
  35023. ExpectIntEQ(BN_rand_range(&emptyBN, &emptyBN), 0);
  35024. ExpectIntEQ(BN_rand_range(bn, &emptyBN), 0);
  35025. ExpectIntEQ(BN_rand_range(&emptyBN, range), 0);
  35026. /* 0 bit random value must be 0 and so cannot set bit in any position. */
  35027. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35028. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35029. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35030. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35031. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35032. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35033. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35034. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35035. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35036. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35037. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35038. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35039. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35040. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35041. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35042. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35043. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35044. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35045. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35046. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35047. /* 1 bit random value must have no more than one top bit set. */
  35048. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35049. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35050. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35051. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35052. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35053. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35054. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35055. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35056. /* END Invalid parameters. */
  35057. /* 0 bit random: 0. */
  35058. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35059. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35060. ExpectIntEQ(BN_is_zero(bn), 1);
  35061. ExpectIntEQ(BN_set_word(bn, 2), 1); /* Make sure not zero. */
  35062. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35063. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35064. ExpectIntEQ(BN_is_zero(bn), 1);
  35065. /* 1 bit random: 0 or 1. */
  35066. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  35067. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35068. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  35069. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35070. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35071. ExpectIntEQ(BN_get_word(bn), 1);
  35072. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35073. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35074. ExpectIntEQ(BN_get_word(bn), 1);
  35075. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  35076. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35077. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  35078. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35079. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35080. ExpectIntEQ(BN_get_word(bn), 1);
  35081. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35082. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35083. ExpectIntEQ(BN_get_word(bn), 1);
  35084. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35085. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35086. ExpectIntEQ(BN_num_bits(bn), 8);
  35087. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35088. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35089. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35090. ExpectIntEQ(BN_num_bits(bn), 8);
  35091. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35092. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  35093. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35094. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35095. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  35096. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  35097. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35098. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35099. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  35100. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35101. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35102. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  35103. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35104. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35105. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  35106. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  35107. * requested number of bits up to the nearest multiple of 8. E.g. in this
  35108. * case, requesting a 13-bit random number would actually return a 16-bit
  35109. * random number. */
  35110. ExpectIntEQ(BN_rand(bn, 13, WOLFSSL_BN_RAND_TOP_ONE,
  35111. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35112. ExpectIntEQ(BN_num_bits(bn), 13);
  35113. ExpectIntEQ(BN_rand(range, 64, WOLFSSL_BN_RAND_TOP_ONE,
  35114. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35115. ExpectIntEQ(BN_rand_range(bn, range), 1);
  35116. ExpectIntEQ(BN_set_word(range, 0), 1);
  35117. ExpectIntEQ(BN_rand_range(bn, range), 1);
  35118. ExpectIntEQ(BN_set_word(range, 1), 1);
  35119. ExpectIntEQ(BN_rand_range(bn, range), 1);
  35120. BN_free(bn);
  35121. BN_free(range);
  35122. #endif
  35123. return EXPECT_RESULT();
  35124. }
  35125. static int test_wolfSSL_BN_prime(void)
  35126. {
  35127. EXPECT_DECLS;
  35128. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  35129. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  35130. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  35131. BIGNUM* a = NULL;
  35132. BIGNUM* add = NULL;
  35133. BIGNUM* rem = NULL;
  35134. BIGNUM emptyBN;
  35135. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35136. ExpectNotNull(a = BN_new());
  35137. ExpectNotNull(add = BN_new());
  35138. ExpectNotNull(rem = BN_new());
  35139. /* Invalid parameters. */
  35140. /* BN_generate_prime_ex()
  35141. * prime - must have valid BIGNUM
  35142. * bits - Greater then 0
  35143. * safe - not supported, must be 0
  35144. * add - not supported, must be NULL
  35145. * rem - not supported, must be NULL
  35146. * cb - anything
  35147. */
  35148. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, rem, NULL), 0);
  35149. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, rem, NULL), 0);
  35150. ExpectIntEQ(BN_generate_prime_ex(a, -1, 1, add, rem, NULL), 0);
  35151. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 1, add, rem, NULL), 0);
  35152. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 1, add, rem, NULL), 0);
  35153. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 0, add, rem, NULL), 0);
  35154. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 0, add, rem, NULL), 0);
  35155. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, NULL, rem, NULL), 0);
  35156. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, NULL, rem, NULL), 0);
  35157. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, NULL, NULL), 0);
  35158. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, NULL, NULL), 0);
  35159. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 0, NULL, NULL, NULL), 0);
  35160. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 0, NULL, NULL, NULL), 0);
  35161. ExpectIntEQ(BN_generate_prime_ex(a, -1, 0, NULL, NULL, NULL), 0);
  35162. ExpectIntEQ(BN_generate_prime_ex(a, 0, 0, NULL, NULL, NULL), 0);
  35163. ExpectIntEQ(BN_generate_prime_ex(a, 2, 1, NULL, NULL, NULL), 0);
  35164. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, add, NULL, NULL), 0);
  35165. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, NULL, rem, NULL), 0);
  35166. ExpectIntEQ(BN_is_prime_ex(NULL, -1, NULL, NULL), -1);
  35167. ExpectIntEQ(BN_is_prime_ex(&emptyBN, -1, NULL, NULL), -1);
  35168. ExpectIntEQ(BN_is_prime_ex(a, -1, NULL, NULL), -1);
  35169. ExpectIntEQ(BN_is_prime_ex(a, 2048, NULL, NULL), -1);
  35170. ExpectIntEQ(BN_is_prime_ex(NULL, 1, NULL, NULL), -1);
  35171. ExpectIntEQ(BN_is_prime_ex(&emptyBN, 1, NULL, NULL), -1);
  35172. /* END Invalid parameters. */
  35173. ExpectIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL), 1);
  35174. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 1);
  35175. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  35176. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 0);
  35177. BN_free(rem);
  35178. BN_free(add);
  35179. BN_free(a);
  35180. #endif
  35181. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  35182. return EXPECT_RESULT();
  35183. }
  35184. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35185. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35186. #define TEST_ARG 0x1234
  35187. static void msg_cb(int write_p, int version, int content_type,
  35188. const void *buf, size_t len, SSL *ssl, void *arg)
  35189. {
  35190. (void)write_p;
  35191. (void)version;
  35192. (void)content_type;
  35193. (void)buf;
  35194. (void)len;
  35195. (void)ssl;
  35196. AssertTrue(arg == (void*)TEST_ARG);
  35197. }
  35198. #endif
  35199. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  35200. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35201. #if defined(SESSION_CERTS)
  35202. #include "wolfssl/internal.h"
  35203. #endif
  35204. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  35205. {
  35206. EXPECT_DECLS;
  35207. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  35208. STACK_OF(X509)* sk = NULL;
  35209. X509* x509 = NULL;
  35210. int i, num;
  35211. BIO* bio = NULL;
  35212. #endif
  35213. ExpectNotNull(ctx);
  35214. ExpectNotNull(ssl);
  35215. fprintf(stderr, "\n===== msgcb called ====\n");
  35216. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  35217. ExpectTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  35218. ExpectIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  35219. ExpectNotNull(SSL_get0_verified_chain(ssl));
  35220. #endif
  35221. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  35222. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  35223. ExpectNotNull(sk = SSL_get_peer_cert_chain(ssl));
  35224. if (sk == NULL) {
  35225. BIO_free(bio);
  35226. return TEST_FAIL;
  35227. }
  35228. num = sk_X509_num(sk);
  35229. ExpectTrue(num > 0);
  35230. for (i = 0; i < num; i++) {
  35231. ExpectNotNull(x509 = sk_X509_value(sk,i));
  35232. if (x509 == NULL)
  35233. break;
  35234. fprintf(stderr, "Certificate at index [%d] = :\n",i);
  35235. X509_print(bio,x509);
  35236. fprintf(stderr, "\n\n");
  35237. }
  35238. BIO_free(bio);
  35239. #endif
  35240. return EXPECT_RESULT();
  35241. }
  35242. #endif
  35243. static int test_wolfSSL_msgCb(void)
  35244. {
  35245. EXPECT_DECLS;
  35246. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  35247. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35248. test_ssl_cbf client_cb;
  35249. test_ssl_cbf server_cb;
  35250. XMEMSET(&client_cb, 0, sizeof(client_cb));
  35251. XMEMSET(&server_cb, 0, sizeof(server_cb));
  35252. #ifndef WOLFSSL_NO_TLS12
  35253. client_cb.method = wolfTLSv1_2_client_method;
  35254. server_cb.method = wolfTLSv1_2_server_method;
  35255. #else
  35256. client_cb.method = wolfTLSv1_3_client_method;
  35257. server_cb.method = wolfTLSv1_3_server_method;
  35258. #endif
  35259. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  35260. &server_cb, msgCb), TEST_SUCCESS);
  35261. #endif
  35262. return EXPECT_RESULT();
  35263. }
  35264. static int test_wolfSSL_either_side(void)
  35265. {
  35266. EXPECT_DECLS;
  35267. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  35268. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35269. test_ssl_cbf client_cb;
  35270. test_ssl_cbf server_cb;
  35271. XMEMSET(&client_cb, 0, sizeof(client_cb));
  35272. XMEMSET(&server_cb, 0, sizeof(server_cb));
  35273. /* Use different CTX for client and server */
  35274. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  35275. ExpectNotNull(client_cb.ctx);
  35276. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  35277. ExpectNotNull(server_cb.ctx);
  35278. /* we are responsible for free'ing WOLFSSL_CTX */
  35279. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  35280. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  35281. &server_cb, NULL), TEST_SUCCESS);
  35282. wolfSSL_CTX_free(client_cb.ctx);
  35283. wolfSSL_CTX_free(server_cb.ctx);
  35284. #endif
  35285. return EXPECT_RESULT();
  35286. }
  35287. static int test_wolfSSL_DTLS_either_side(void)
  35288. {
  35289. EXPECT_DECLS;
  35290. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  35291. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  35292. test_ssl_cbf client_cb;
  35293. test_ssl_cbf server_cb;
  35294. XMEMSET(&client_cb, 0, sizeof(client_cb));
  35295. XMEMSET(&server_cb, 0, sizeof(server_cb));
  35296. /* Use different CTX for client and server */
  35297. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  35298. ExpectNotNull(client_cb.ctx);
  35299. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  35300. ExpectNotNull(server_cb.ctx);
  35301. /* we are responsible for free'ing WOLFSSL_CTX */
  35302. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  35303. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  35304. &server_cb, NULL), TEST_SUCCESS);
  35305. wolfSSL_CTX_free(client_cb.ctx);
  35306. wolfSSL_CTX_free(server_cb.ctx);
  35307. #endif
  35308. return EXPECT_RESULT();
  35309. }
  35310. static int test_generate_cookie(void)
  35311. {
  35312. EXPECT_DECLS;
  35313. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  35314. SSL_CTX* ctx = NULL;
  35315. SSL* ssl = NULL;
  35316. byte buf[FOURK_BUF] = {0};
  35317. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  35318. ExpectNotNull(ssl = SSL_new(ctx));
  35319. /* Test unconnected */
  35320. ExpectIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  35321. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  35322. wolfSSL_SetCookieCtx(ssl, ctx);
  35323. ExpectNotNull(wolfSSL_GetCookieCtx(ssl));
  35324. ExpectNull(wolfSSL_GetCookieCtx(NULL));
  35325. SSL_free(ssl);
  35326. SSL_CTX_free(ctx);
  35327. #endif
  35328. return EXPECT_RESULT();
  35329. }
  35330. static int test_wolfSSL_set_options(void)
  35331. {
  35332. EXPECT_DECLS;
  35333. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35334. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35335. WOLFSSL* ssl = NULL;
  35336. WOLFSSL_CTX* ctx = NULL;
  35337. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  35338. char appData[] = "extra msg";
  35339. #endif
  35340. #ifdef OPENSSL_EXTRA
  35341. unsigned char protos[] = {
  35342. 7, 't', 'l', 's', '/', '1', '.', '2',
  35343. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  35344. };
  35345. unsigned int len = sizeof(protos);
  35346. void *arg = (void *)TEST_ARG;
  35347. #endif
  35348. #ifndef NO_WOLFSSL_SERVER
  35349. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  35350. #else
  35351. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35352. #endif
  35353. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  35354. WOLFSSL_FILETYPE_PEM));
  35355. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  35356. WOLFSSL_FILETYPE_PEM));
  35357. ExpectTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  35358. == WOLFSSL_OP_NO_TLSv1);
  35359. ExpectTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  35360. ExpectIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  35361. WOLFSSL_OP_NO_SSLv2)), 0);
  35362. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  35363. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  35364. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  35365. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  35366. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  35367. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  35368. ExpectFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  35369. WOLFSSL_OP_NO_COMPRESSION));
  35370. wolfSSL_CTX_free(ctx);
  35371. ctx = NULL;
  35372. #ifndef NO_WOLFSSL_SERVER
  35373. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  35374. #else
  35375. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35376. #endif
  35377. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  35378. WOLFSSL_FILETYPE_PEM));
  35379. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  35380. WOLFSSL_FILETYPE_PEM));
  35381. #ifdef OPENSSL_EXTRA
  35382. ExpectTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  35383. #endif
  35384. ExpectNotNull(ssl = wolfSSL_new(ctx));
  35385. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  35386. #ifdef HAVE_EX_DATA
  35387. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  35388. ExpectNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  35389. if (ssl != NULL) {
  35390. ExpectIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  35391. appData, sizeof(appData)), 0);
  35392. }
  35393. #else
  35394. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  35395. ExpectNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  35396. #endif
  35397. #endif
  35398. ExpectTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  35399. WOLFSSL_OP_NO_TLSv1);
  35400. ExpectTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  35401. ExpectIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  35402. WOLFSSL_OP_NO_SSLv2)), 0);
  35403. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  35404. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  35405. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  35406. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  35407. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  35408. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  35409. #ifdef OPENSSL_EXTRA
  35410. ExpectFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  35411. WOLFSSL_OP_NO_COMPRESSION));
  35412. #endif
  35413. #ifdef OPENSSL_EXTRA
  35414. ExpectTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  35415. wolfSSL_set_msg_callback_arg(ssl, arg);
  35416. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  35417. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  35418. #else
  35419. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  35420. #endif
  35421. #endif
  35422. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  35423. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  35424. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  35425. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  35426. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  35427. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  35428. #else
  35429. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  35430. #endif
  35431. #endif /* HAVE_ALPN && !NO_BIO */
  35432. #endif
  35433. wolfSSL_free(ssl);
  35434. wolfSSL_CTX_free(ctx);
  35435. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35436. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35437. return EXPECT_RESULT();
  35438. }
  35439. static int test_wolfSSL_sk_SSL_CIPHER(void)
  35440. {
  35441. EXPECT_DECLS;
  35442. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  35443. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35444. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35445. SSL* ssl = NULL;
  35446. SSL_CTX* ctx = NULL;
  35447. STACK_OF(SSL_CIPHER) *sk = NULL;
  35448. STACK_OF(SSL_CIPHER) *dupSk = NULL;
  35449. #ifndef NO_WOLFSSL_SERVER
  35450. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35451. #else
  35452. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35453. #endif
  35454. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  35455. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35456. ExpectNotNull(ssl = SSL_new(ctx));
  35457. ExpectNotNull(sk = SSL_get_ciphers(ssl));
  35458. ExpectNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  35459. ExpectIntGT(sk_SSL_CIPHER_num(sk), 0);
  35460. ExpectIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  35461. /* error case because connection has not been established yet */
  35462. ExpectIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  35463. sk_SSL_CIPHER_free(dupSk);
  35464. /* sk is pointer to internal struct that should be free'd in SSL_free */
  35465. SSL_free(ssl);
  35466. SSL_CTX_free(ctx);
  35467. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35468. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35469. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35470. return EXPECT_RESULT();
  35471. }
  35472. static int test_wolfSSL_set1_curves_list(void)
  35473. {
  35474. EXPECT_DECLS;
  35475. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  35476. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35477. SSL* ssl = NULL;
  35478. SSL_CTX* ctx = NULL;
  35479. #ifndef NO_WOLFSSL_SERVER
  35480. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35481. #else
  35482. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35483. #endif
  35484. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  35485. SSL_FILETYPE_PEM));
  35486. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  35487. ExpectNotNull(ssl = SSL_new(ctx));
  35488. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  35489. #ifdef HAVE_ECC
  35490. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  35491. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  35492. #endif
  35493. #ifdef HAVE_CURVE25519
  35494. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  35495. #else
  35496. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  35497. #endif
  35498. #ifdef HAVE_CURVE448
  35499. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  35500. #else
  35501. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  35502. #endif
  35503. ExpectIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  35504. #ifdef HAVE_ECC
  35505. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  35506. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  35507. #endif
  35508. #ifdef HAVE_CURVE25519
  35509. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  35510. #else
  35511. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  35512. #endif
  35513. #ifdef HAVE_CURVE448
  35514. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  35515. #else
  35516. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  35517. #endif
  35518. SSL_free(ssl);
  35519. SSL_CTX_free(ctx);
  35520. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35521. #endif
  35522. return EXPECT_RESULT();
  35523. }
  35524. static int test_wolfSSL_set1_sigalgs_list(void)
  35525. {
  35526. EXPECT_DECLS;
  35527. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  35528. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35529. SSL* ssl = NULL;
  35530. SSL_CTX* ctx = NULL;
  35531. #ifndef NO_WOLFSSL_SERVER
  35532. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35533. #else
  35534. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35535. #endif
  35536. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  35537. SSL_FILETYPE_PEM));
  35538. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35539. ExpectNotNull(ssl = SSL_new(ctx));
  35540. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  35541. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  35542. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  35543. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  35544. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  35545. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  35546. #ifndef NO_RSA
  35547. #ifndef NO_SHA256
  35548. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  35549. WOLFSSL_FAILURE);
  35550. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  35551. WOLFSSL_FAILURE);
  35552. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  35553. WOLFSSL_SUCCESS);
  35554. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  35555. WOLFSSL_SUCCESS);
  35556. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  35557. WOLFSSL_FAILURE);
  35558. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  35559. WOLFSSL_FAILURE);
  35560. #ifdef WC_RSA_PSS
  35561. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  35562. WOLFSSL_SUCCESS);
  35563. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  35564. WOLFSSL_SUCCESS);
  35565. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  35566. WOLFSSL_SUCCESS);
  35567. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  35568. WOLFSSL_SUCCESS);
  35569. #endif
  35570. #ifdef WOLFSSL_SHA512
  35571. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35572. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  35573. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35574. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  35575. #elif defined(WOLFSSL_SHA384)
  35576. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35577. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  35578. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35579. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  35580. #endif
  35581. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  35582. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  35583. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  35584. WOLFSSL_FAILURE);
  35585. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  35586. WOLFSSL_FAILURE);
  35587. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  35588. WOLFSSL_FAILURE);
  35589. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  35590. WOLFSSL_FAILURE);
  35591. #endif
  35592. #endif
  35593. #ifdef HAVE_ECC
  35594. #ifndef NO_SHA256
  35595. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  35596. WOLFSSL_SUCCESS);
  35597. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"),
  35598. WOLFSSL_SUCCESS);
  35599. #ifdef WOLFSSL_SHA512
  35600. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35601. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  35602. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35603. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  35604. #elif defined(WOLFSSL_SHA384)
  35605. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35606. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  35607. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35608. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  35609. #endif
  35610. #endif
  35611. #endif
  35612. #ifdef HAVE_ED25519
  35613. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  35614. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  35615. #endif
  35616. #ifdef HAVE_ED448
  35617. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  35618. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  35619. #endif
  35620. #ifndef NO_DSA
  35621. #ifndef NO_SHA256
  35622. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  35623. WOLFSSL_SUCCESS);
  35624. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  35625. WOLFSSL_SUCCESS);
  35626. #endif
  35627. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  35628. defined(WOLFSSL_ALLOW_TLS_SHA1))
  35629. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  35630. WOLFSSL_SUCCESS);
  35631. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  35632. WOLFSSL_SUCCESS);
  35633. #endif
  35634. #endif
  35635. SSL_free(ssl);
  35636. SSL_CTX_free(ctx);
  35637. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35638. #endif
  35639. return EXPECT_RESULT();
  35640. }
  35641. /* Testing wolfSSL_set_tlsext_status_type function.
  35642. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  35643. */
  35644. static int test_wolfSSL_set_tlsext_status_type(void)
  35645. {
  35646. EXPECT_DECLS;
  35647. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  35648. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  35649. SSL* ssl = NULL;
  35650. SSL_CTX* ctx = NULL;
  35651. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35652. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  35653. SSL_FILETYPE_PEM));
  35654. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35655. ExpectNotNull(ssl = SSL_new(ctx));
  35656. ExpectIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  35657. SSL_SUCCESS);
  35658. ExpectIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  35659. SSL_free(ssl);
  35660. SSL_CTX_free(ctx);
  35661. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  35662. return EXPECT_RESULT();
  35663. }
  35664. #ifndef NO_BIO
  35665. static int test_wolfSSL_PEM_read_bio(void)
  35666. {
  35667. EXPECT_DECLS;
  35668. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35669. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35670. byte buff[6000];
  35671. XFILE f = XBADFILE;
  35672. int bytes;
  35673. X509* x509 = NULL;
  35674. BIO* bio = NULL;
  35675. BUF_MEM* buf = NULL;
  35676. ExpectTrue((f = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  35677. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  35678. if (f != XBADFILE)
  35679. XFCLOSE(f);
  35680. ExpectNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  35681. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  35682. ExpectIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  35683. ExpectNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  35684. ExpectIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  35685. /* BIO should return the set EOF value */
  35686. ExpectIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  35687. ExpectIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  35688. ExpectIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  35689. ExpectIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  35690. BIO_free(bio);
  35691. BUF_MEM_free(buf);
  35692. X509_free(x509);
  35693. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  35694. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35695. return EXPECT_RESULT();
  35696. }
  35697. #if defined(OPENSSL_EXTRA)
  35698. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  35699. long argl, long ret)
  35700. {
  35701. (void)bio;
  35702. (void)cmd;
  35703. (void)argp;
  35704. (void)argi;
  35705. (void)argl;
  35706. return ret;
  35707. }
  35708. #endif
  35709. static int test_wolfSSL_BIO(void)
  35710. {
  35711. EXPECT_DECLS;
  35712. #if defined(OPENSSL_EXTRA)
  35713. const unsigned char* p = NULL;
  35714. byte buff[20];
  35715. BIO* bio1 = NULL;
  35716. BIO* bio2 = NULL;
  35717. BIO* bio3 = NULL;
  35718. char* bufPt = NULL;
  35719. int i;
  35720. for (i = 0; i < 20; i++) {
  35721. buff[i] = i;
  35722. }
  35723. /* test BIO_free with NULL */
  35724. ExpectIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  35725. /* Creating and testing type BIO_s_bio */
  35726. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  35727. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  35728. ExpectNotNull(bio3 = BIO_new(BIO_s_bio()));
  35729. /* read/write before set up */
  35730. ExpectIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  35731. ExpectIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  35732. ExpectIntEQ(BIO_set_nbio(bio1, 1), 1);
  35733. ExpectIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  35734. ExpectIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  35735. ExpectIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  35736. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  35737. ExpectNotNull(XMEMCPY(bufPt, buff, 10));
  35738. ExpectIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  35739. /* write buffer full */
  35740. ExpectIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  35741. ExpectIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  35742. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 0);
  35743. /* write the other direction with pair */
  35744. ExpectIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  35745. ExpectNotNull(XMEMCPY(bufPt, buff, 8));
  35746. ExpectIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  35747. /* try read */
  35748. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 8);
  35749. ExpectIntEQ((int)BIO_ctrl_pending(bio2), 20);
  35750. /* try read using ctrl function */
  35751. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  35752. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  35753. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  35754. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  35755. ExpectIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  35756. for (i = 0; i < 20; i++) {
  35757. ExpectIntEQ((int)bufPt[i], i);
  35758. }
  35759. ExpectIntEQ(BIO_nread(bio2, &bufPt, 1), 0);
  35760. ExpectIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  35761. for (i = 0; i < 8; i++) {
  35762. ExpectIntEQ((int)bufPt[i], i);
  35763. }
  35764. ExpectIntEQ(BIO_nread(bio1, &bufPt, 1), 0);
  35765. ExpectIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  35766. /* new pair */
  35767. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  35768. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  35769. bio2 = NULL;
  35770. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  35771. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  35772. ExpectIntEQ(BIO_nread(bio3, &bufPt, 10), 0);
  35773. /* test wrap around... */
  35774. ExpectIntEQ(BIO_reset(bio1), 0);
  35775. ExpectIntEQ(BIO_reset(bio3), 0);
  35776. /* fill write buffer, read only small amount then write again */
  35777. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35778. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  35779. ExpectIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  35780. for (i = 0; i < 4; i++) {
  35781. ExpectIntEQ(bufPt[i], i);
  35782. }
  35783. /* try writing over read index */
  35784. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  35785. ExpectNotNull(XMEMSET(bufPt, 0, 4));
  35786. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 20);
  35787. /* read and write 0 bytes */
  35788. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  35789. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  35790. /* should read only to end of write buffer then need to read again */
  35791. ExpectIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  35792. for (i = 0; i < 16; i++) {
  35793. ExpectIntEQ(bufPt[i], buff[4 + i]);
  35794. }
  35795. ExpectIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  35796. ExpectIntEQ(BIO_nread0(bio3, &bufPt), 4);
  35797. for (i = 0; i < 4; i++) {
  35798. ExpectIntEQ(bufPt[i], 0);
  35799. }
  35800. /* read index should not have advanced with nread0 */
  35801. ExpectIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  35802. for (i = 0; i < 4; i++) {
  35803. ExpectIntEQ(bufPt[i], 0);
  35804. }
  35805. /* write and fill up buffer checking reset of index state */
  35806. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35807. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  35808. /* test reset on data in bio1 write buffer */
  35809. ExpectIntEQ(BIO_reset(bio1), 0);
  35810. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  35811. ExpectIntEQ(BIO_nread(bio3, &bufPt, 3), 0);
  35812. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35813. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  35814. ExpectNotNull(p);
  35815. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  35816. ExpectIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  35817. for (i = 0; i < 6; i++) {
  35818. ExpectIntEQ(bufPt[i], i);
  35819. }
  35820. /* test case of writing twice with offset read index */
  35821. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  35822. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  35823. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35824. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  35825. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35826. ExpectIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  35827. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  35828. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35829. BIO_free(bio1);
  35830. bio1 = NULL;
  35831. BIO_free(bio3);
  35832. bio3 = NULL;
  35833. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  35834. {
  35835. BIO* bioA = NULL;
  35836. BIO* bioB = NULL;
  35837. ExpectIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  35838. ExpectIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  35839. BIO_free(bioA);
  35840. bioA = NULL;
  35841. BIO_free(bioB);
  35842. bioB = NULL;
  35843. }
  35844. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  35845. /* BIOs with file pointers */
  35846. #if !defined(NO_FILESYSTEM)
  35847. {
  35848. XFILE f1 = XBADFILE;
  35849. XFILE f2 = XBADFILE;
  35850. BIO* f_bio1 = NULL;
  35851. BIO* f_bio2 = NULL;
  35852. unsigned char cert[300];
  35853. char testFile[] = "tests/bio_write_test.txt";
  35854. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  35855. ExpectNotNull(f_bio1 = BIO_new(BIO_s_file()));
  35856. ExpectNotNull(f_bio2 = BIO_new(BIO_s_file()));
  35857. /* Failure due to wrong BIO type */
  35858. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  35859. ExpectIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  35860. ExpectTrue((f1 = XFOPEN(svrCertFile, "rwb")) != XBADFILE);
  35861. ExpectIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  35862. ExpectIntEQ(BIO_write_filename(f_bio2, testFile),
  35863. WOLFSSL_SUCCESS);
  35864. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  35865. ExpectIntEQ(BIO_tell(f_bio1),sizeof(cert));
  35866. ExpectIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  35867. ExpectIntEQ(BIO_tell(f_bio2),sizeof(msg));
  35868. ExpectIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  35869. ExpectIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  35870. ExpectIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  35871. ExpectIntEQ(BIO_reset(f_bio2), 0);
  35872. ExpectIntEQ(BIO_tell(NULL),-1);
  35873. ExpectIntEQ(BIO_tell(f_bio2),0);
  35874. ExpectIntEQ(BIO_seek(f_bio2, 4), 0);
  35875. ExpectIntEQ(BIO_tell(f_bio2),4);
  35876. BIO_free(f_bio1);
  35877. f_bio1 = NULL;
  35878. BIO_free(f_bio2);
  35879. f_bio2 = NULL;
  35880. ExpectNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  35881. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  35882. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  35883. BIO_free(f_bio1);
  35884. f_bio1 = NULL;
  35885. }
  35886. #endif /* !defined(NO_FILESYSTEM) */
  35887. /* BIO info callback */
  35888. {
  35889. const char* testArg = "test";
  35890. BIO* cb_bio = NULL;
  35891. ExpectNotNull(cb_bio = BIO_new(BIO_s_mem()));
  35892. BIO_set_callback(cb_bio, bioCallback);
  35893. ExpectNotNull(BIO_get_callback(cb_bio));
  35894. BIO_set_callback(cb_bio, NULL);
  35895. ExpectNull(BIO_get_callback(cb_bio));
  35896. BIO_set_callback_arg(cb_bio, (char*)testArg);
  35897. ExpectStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  35898. ExpectNull(BIO_get_callback_arg(NULL));
  35899. BIO_free(cb_bio);
  35900. cb_bio = NULL;
  35901. }
  35902. /* BIO_vfree */
  35903. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  35904. BIO_vfree(NULL);
  35905. BIO_vfree(bio1);
  35906. #endif
  35907. return EXPECT_RESULT();
  35908. }
  35909. static int test_wolfSSL_BIO_BIO_ring_read(void)
  35910. {
  35911. EXPECT_DECLS;
  35912. #if defined(OPENSSL_ALL)
  35913. BIO* bio1 = NULL;
  35914. BIO* bio2 = NULL;
  35915. byte data[50];
  35916. byte tmp[50];
  35917. XMEMSET(data, 42, sizeof(data));
  35918. ExpectIntEQ(BIO_new_bio_pair(&bio1, sizeof(data), &bio2, sizeof(data)),
  35919. SSL_SUCCESS);
  35920. ExpectIntEQ(BIO_write(bio1, data, 40), 40);
  35921. ExpectIntEQ(BIO_read(bio1, tmp, 20), -1);
  35922. ExpectIntEQ(BIO_read(bio2, tmp, 20), 20);
  35923. ExpectBufEQ(tmp, data, 20);
  35924. ExpectIntEQ(BIO_write(bio1, data, 20), 20);
  35925. ExpectIntEQ(BIO_read(bio2, tmp, 40), 40);
  35926. ExpectBufEQ(tmp, data, 40);
  35927. BIO_free(bio1);
  35928. BIO_free(bio2);
  35929. #endif
  35930. return EXPECT_RESULT();
  35931. }
  35932. #endif /* !NO_BIO */
  35933. static int test_wolfSSL_a2i_IPADDRESS(void)
  35934. {
  35935. EXPECT_DECLS;
  35936. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  35937. const unsigned char* data = NULL;
  35938. int dataSz = 0;
  35939. ASN1_OCTET_STRING *st = NULL;
  35940. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  35941. const unsigned char ipv6_exp[] = {
  35942. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  35943. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  35944. };
  35945. const unsigned char ipv6_home[] = {
  35946. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  35947. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  35948. };
  35949. ExpectNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  35950. ExpectNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  35951. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  35952. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP4_ADDR_LEN);
  35953. ExpectIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  35954. ASN1_STRING_free(st);
  35955. st = NULL;
  35956. ExpectNotNull(st = a2i_IPADDRESS("::1"));
  35957. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  35958. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  35959. ExpectIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  35960. ASN1_STRING_free(st);
  35961. st = NULL;
  35962. ExpectNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  35963. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  35964. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  35965. ExpectIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  35966. ASN1_STRING_free(st);
  35967. #endif
  35968. return EXPECT_RESULT();
  35969. }
  35970. static int test_wolfSSL_X509_cmp_time(void)
  35971. {
  35972. EXPECT_DECLS;
  35973. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  35974. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  35975. WOLFSSL_ASN1_TIME asn_time;
  35976. time_t t;
  35977. ExpectIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  35978. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  35979. ExpectIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  35980. ExpectIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  35981. ExpectIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  35982. #endif
  35983. return EXPECT_RESULT();
  35984. }
  35985. static int test_wolfSSL_X509_time_adj(void)
  35986. {
  35987. EXPECT_DECLS;
  35988. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  35989. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  35990. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  35991. !defined(NO_ASN_TIME)
  35992. X509* x509 = NULL;
  35993. time_t t;
  35994. time_t not_before;
  35995. time_t not_after;
  35996. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  35997. client_cert_der_2048, sizeof_client_cert_der_2048,
  35998. WOLFSSL_FILETYPE_ASN1));
  35999. t = 0;
  36000. not_before = wc_Time(0);
  36001. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  36002. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  36003. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  36004. /* Check X509_gmtime_adj, too. */
  36005. ExpectNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  36006. X509_free(x509);
  36007. #endif
  36008. return EXPECT_RESULT();
  36009. }
  36010. static int test_wolfSSL_X509(void)
  36011. {
  36012. EXPECT_DECLS;
  36013. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  36014. !defined(NO_RSA)
  36015. X509* x509 = NULL;
  36016. #ifndef NO_BIO
  36017. BIO* bio = NULL;
  36018. X509_STORE_CTX* ctx = NULL;
  36019. X509_STORE* store = NULL;
  36020. #endif
  36021. char der[] = "certs/ca-cert.der";
  36022. XFILE fp = XBADFILE;
  36023. ExpectNotNull(x509 = X509_new());
  36024. X509_free(x509);
  36025. x509 = NULL;
  36026. #ifndef NO_BIO
  36027. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  36028. SSL_FILETYPE_PEM));
  36029. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36030. #ifdef WOLFSSL_CERT_GEN
  36031. ExpectIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  36032. #endif
  36033. ExpectNotNull(ctx = X509_STORE_CTX_new());
  36034. ExpectIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  36035. ExpectNotNull(store = X509_STORE_new());
  36036. ExpectIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  36037. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  36038. ExpectIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  36039. X509_STORE_CTX_free(ctx);
  36040. X509_STORE_free(store);
  36041. X509_free(x509);
  36042. x509 = NULL;
  36043. BIO_free(bio);
  36044. #endif
  36045. /** d2i_X509_fp test **/
  36046. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  36047. ExpectNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  36048. ExpectNotNull(x509);
  36049. X509_free(x509);
  36050. x509 = NULL;
  36051. if (fp != XBADFILE) {
  36052. XFCLOSE(fp);
  36053. fp = XBADFILE;
  36054. }
  36055. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  36056. ExpectNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  36057. ExpectNotNull(x509);
  36058. X509_free(x509);
  36059. if (fp != XBADFILE)
  36060. XFCLOSE(fp);
  36061. /* X509_up_ref test */
  36062. ExpectIntEQ(X509_up_ref(NULL), 0);
  36063. ExpectNotNull(x509 = X509_new()); /* refCount = 1 */
  36064. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  36065. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  36066. X509_free(x509); /* refCount = 2 */
  36067. X509_free(x509); /* refCount = 1 */
  36068. X509_free(x509); /* refCount = 0, free */
  36069. #endif
  36070. return EXPECT_RESULT();
  36071. }
  36072. static int test_wolfSSL_X509_get_ext_count(void)
  36073. {
  36074. EXPECT_DECLS;
  36075. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  36076. !defined(NO_RSA)
  36077. int ret = 0;
  36078. WOLFSSL_X509* x509 = NULL;
  36079. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  36080. XFILE f = XBADFILE;
  36081. /* NULL parameter check */
  36082. ExpectIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  36083. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  36084. SSL_FILETYPE_PEM));
  36085. ExpectIntEQ(X509_get_ext_count(x509), 5);
  36086. wolfSSL_X509_free(x509);
  36087. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  36088. SSL_FILETYPE_PEM));
  36089. ExpectIntEQ(X509_get_ext_count(x509), 5);
  36090. wolfSSL_X509_free(x509);
  36091. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  36092. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  36093. if (f != XBADFILE)
  36094. XFCLOSE(f);
  36095. /* wolfSSL_X509_get_ext_count() valid input */
  36096. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  36097. /* wolfSSL_X509_get_ext_count() NULL argument */
  36098. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  36099. wolfSSL_X509_free(x509);
  36100. #endif
  36101. return EXPECT_RESULT();
  36102. }
  36103. static int test_wolfSSL_X509_sign2(void)
  36104. {
  36105. EXPECT_DECLS;
  36106. /* test requires WOLFSSL_AKID_NAME to match expected output */
  36107. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  36108. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  36109. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_AKID_NAME) && \
  36110. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  36111. defined(WOLFSSL_IP_ALT_NAME))
  36112. WOLFSSL_X509 *x509 = NULL;
  36113. WOLFSSL_X509 *ca = NULL;
  36114. const unsigned char *der = NULL;
  36115. const unsigned char *pt = NULL;
  36116. WOLFSSL_EVP_PKEY *priv = NULL;
  36117. WOLFSSL_X509_NAME *name = NULL;
  36118. int derSz;
  36119. #ifndef NO_ASN_TIME
  36120. WOLFSSL_ASN1_TIME *notBefore = NULL;
  36121. WOLFSSL_ASN1_TIME *notAfter = NULL;
  36122. const int year = 365*24*60*60;
  36123. const int day = 24*60*60;
  36124. const int hour = 60*60;
  36125. const int mini = 60;
  36126. time_t t;
  36127. #endif
  36128. const unsigned char expected[] = {
  36129. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xFB, 0xA0, 0x03, 0x02, 0x01,
  36130. 0x02, 0x02, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55, 0x8A,
  36131. 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8, 0x30,
  36132. 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
  36133. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
  36134. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  36135. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
  36136. 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  36137. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03,
  36138. 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6F, 0x6F, 0x74,
  36139. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x0A,
  36140. 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x18,
  36141. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77,
  36142. 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  36143. 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  36144. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F,
  36145. 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, 0x17,
  36146. 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  36147. 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  36148. 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30, 0x09,
  36149. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  36150. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74,
  36151. 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07,
  36152. 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15, 0x30,
  36153. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C, 0x66,
  36154. 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  36155. 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67, 0x72,
  36156. 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32, 0x30, 0x34, 0x38, 0x31,
  36157. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  36158. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  36159. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  36160. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  36161. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x82,
  36162. 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  36163. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
  36164. 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE,
  36165. 0x39, 0xA4, 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74,
  36166. 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07,
  36167. 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4, 0x81,
  36168. 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, 0xBB, 0xA1, 0x75, 0xC8, 0x36,
  36169. 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC,
  36170. 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F,
  36171. 0x65, 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, 0xC5,
  36172. 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 0x7A, 0x78, 0xE1, 0x01, 0x56,
  36173. 0x56, 0x91, 0xA6, 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C, 0xEF,
  36174. 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C, 0xA1, 0x3B, 0xF5, 0xF1, 0xA3,
  36175. 0x4A, 0x35, 0xE4, 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E, 0x97,
  36176. 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81, 0xAF, 0x20, 0x0B, 0x43, 0x14,
  36177. 0xC5, 0x74, 0x67, 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88, 0x40,
  36178. 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72, 0x22, 0x17, 0xD7, 0x52, 0x65,
  36179. 0x24, 0x73, 0xB0, 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C, 0x7B,
  36180. 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D, 0x50, 0x6D, 0x3B, 0xA3, 0x3B,
  36181. 0xA3, 0x99, 0x5E, 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A, 0xD9,
  36182. 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB, 0xFF, 0x25, 0x4C, 0xC4, 0xD1,
  36183. 0x79, 0xF4, 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5, 0x72,
  36184. 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7,
  36185. 0x7F, 0xC0, 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3, 0x02,
  36186. 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x4F, 0x30, 0x82, 0x01, 0x4B,
  36187. 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  36188. 0x01, 0xFF, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30,
  36189. 0x13, 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63,
  36190. 0x6F, 0x6D, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03,
  36191. 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  36192. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  36193. 0x85, 0x65, 0xC0, 0x30, 0x81, 0xDE, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04,
  36194. 0x81, 0xD6, 0x30, 0x81, 0xD3, 0x80, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  36195. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  36196. 0x85, 0x65, 0xC0, 0xA1, 0x81, 0xA4, 0xA4, 0x81, 0xA1, 0x30, 0x81, 0x9E,
  36197. 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  36198. 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07,
  36199. 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06,
  36200. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61,
  36201. 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C,
  36202. 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38,
  36203. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50,
  36204. 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32,
  36205. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  36206. 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  36207. 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A,
  36208. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
  36209. 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  36210. 0x6F, 0x6D, 0x82, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55,
  36211. 0x8A, 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8,
  36212. 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  36213. 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2B,
  36214. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  36215. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
  36216. 0x01, 0x01, 0x00, 0x14, 0xFB, 0xD0, 0xCE, 0x31, 0x7F, 0xA5, 0x59, 0xFA,
  36217. 0x7C, 0x68, 0x26, 0xA7, 0xE8, 0x0D, 0x9F, 0x50, 0x57, 0xFA, 0x1C, 0x7C,
  36218. 0x5E, 0x43, 0xA4, 0x97, 0x47, 0xB6, 0x41, 0xAC, 0x63, 0xD3, 0x61, 0x8C,
  36219. 0x1F, 0x42, 0xEF, 0x53, 0xD0, 0xBA, 0x31, 0x4D, 0x99, 0x74, 0xA4, 0x60,
  36220. 0xDC, 0xC6, 0x6F, 0xCC, 0x1E, 0x25, 0x98, 0xE1, 0xA4, 0xA0, 0x67, 0x69,
  36221. 0x97, 0xE3, 0x97, 0x7C, 0x83, 0x28, 0xF1, 0xF4, 0x7D, 0x03, 0xA8, 0x31,
  36222. 0x77, 0xCC, 0xD1, 0x37, 0xEF, 0x7B, 0x4A, 0x71, 0x2D, 0x11, 0x7E, 0x92,
  36223. 0xF5, 0x67, 0xB7, 0x56, 0xBA, 0x28, 0xF8, 0xD6, 0xCE, 0x2A, 0x71, 0xE3,
  36224. 0x70, 0x6B, 0x09, 0x0F, 0x67, 0x6F, 0x7A, 0xE0, 0x89, 0xF6, 0x5E, 0x23,
  36225. 0x0C, 0x0A, 0x44, 0x4E, 0x65, 0x8E, 0x7B, 0x68, 0xD0, 0xAD, 0x76, 0x3E,
  36226. 0x2A, 0x0E, 0xA2, 0x05, 0x11, 0x74, 0x24, 0x08, 0x60, 0xED, 0x9F, 0x98,
  36227. 0x18, 0xE9, 0x91, 0x58, 0x36, 0xEC, 0xEC, 0x25, 0x6B, 0xBA, 0x9C, 0x87,
  36228. 0x38, 0x68, 0xDC, 0xDC, 0x15, 0x6F, 0x20, 0x68, 0xC4, 0xBF, 0x05, 0x5B,
  36229. 0x4A, 0x0C, 0x44, 0x2B, 0x92, 0x3F, 0x10, 0x99, 0xDC, 0xF6, 0x6C, 0x0E,
  36230. 0x34, 0x26, 0x6E, 0x6D, 0x4E, 0x12, 0xBC, 0x60, 0x8F, 0x27, 0x1D, 0x7A,
  36231. 0x00, 0x50, 0xBE, 0x23, 0xDE, 0x48, 0x47, 0x9F, 0xAD, 0x2F, 0x94, 0x3D,
  36232. 0x16, 0x73, 0x48, 0x6B, 0xC8, 0x97, 0xE6, 0xB4, 0xB3, 0x4B, 0xE1, 0x68,
  36233. 0x08, 0xC3, 0xE5, 0x34, 0x5F, 0x9B, 0xDA, 0xAB, 0xCA, 0x6D, 0x55, 0x32,
  36234. 0xEF, 0x6C, 0xEF, 0x9B, 0x8B, 0x5B, 0xC7, 0xF0, 0xC2, 0x0F, 0x8E, 0x93,
  36235. 0x09, 0x60, 0x3C, 0x0B, 0xDC, 0xBD, 0xDB, 0x4A, 0x2D, 0xD0, 0x98, 0xAA,
  36236. 0xAB, 0x6C, 0x6F, 0x6D, 0x6B, 0x6A, 0x5C, 0x33, 0xAC, 0xAD, 0xA8, 0x1B,
  36237. 0x38, 0x5D, 0x9F, 0xDA, 0xE7, 0x70, 0x07
  36238. };
  36239. pt = ca_key_der_2048;
  36240. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  36241. sizeof_ca_key_der_2048));
  36242. pt = client_cert_der_2048;
  36243. ExpectNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  36244. sizeof_client_cert_der_2048));
  36245. pt = ca_cert_der_2048;
  36246. ExpectNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  36247. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  36248. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  36249. #ifndef NO_ASN_TIME
  36250. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  36251. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  36252. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  36253. ExpectIntEQ(notAfter->length, 13);
  36254. ExpectTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  36255. ExpectTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  36256. #endif
  36257. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  36258. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  36259. ExpectIntEQ(derSz, sizeof(expected));
  36260. #ifndef NO_ASN_TIME
  36261. ExpectIntEQ(XMEMCMP(der, expected, derSz), 0);
  36262. #endif
  36263. wolfSSL_X509_free(ca);
  36264. wolfSSL_X509_free(x509);
  36265. wolfSSL_EVP_PKEY_free(priv);
  36266. #ifndef NO_ASN_TIME
  36267. wolfSSL_ASN1_TIME_free(notBefore);
  36268. wolfSSL_ASN1_TIME_free(notAfter);
  36269. #endif
  36270. #endif
  36271. return EXPECT_RESULT();
  36272. }
  36273. static int test_wolfSSL_X509_sign(void)
  36274. {
  36275. EXPECT_DECLS;
  36276. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_ASN_TIME) && \
  36277. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  36278. int ret = 0;
  36279. char *cn = NULL;
  36280. word32 cnSz = 0;
  36281. X509_NAME *name = NULL;
  36282. X509 *x509 = NULL;
  36283. X509 *ca = NULL;
  36284. DecodedCert dCert;
  36285. EVP_PKEY *pub = NULL;
  36286. EVP_PKEY *priv = NULL;
  36287. EVP_MD_CTX *mctx = NULL;
  36288. #if defined(USE_CERT_BUFFERS_1024)
  36289. const unsigned char* rsaPriv = client_key_der_1024;
  36290. const unsigned char* rsaPub = client_keypub_der_1024;
  36291. const unsigned char* certIssuer = client_cert_der_1024;
  36292. long clientKeySz = (long)sizeof_client_key_der_1024;
  36293. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  36294. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  36295. #elif defined(USE_CERT_BUFFERS_2048)
  36296. const unsigned char* rsaPriv = client_key_der_2048;
  36297. const unsigned char* rsaPub = client_keypub_der_2048;
  36298. const unsigned char* certIssuer = client_cert_der_2048;
  36299. long clientKeySz = (long)sizeof_client_key_der_2048;
  36300. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  36301. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  36302. #endif
  36303. byte sn[16];
  36304. int snSz = sizeof(sn);
  36305. /* Set X509_NAME fields */
  36306. ExpectNotNull(name = X509_NAME_new());
  36307. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  36308. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  36309. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  36310. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  36311. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  36312. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  36313. /* Get private and public keys */
  36314. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  36315. clientKeySz));
  36316. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  36317. ExpectNotNull(x509 = X509_new());
  36318. /* Set version 3 */
  36319. ExpectIntNE(X509_set_version(x509, 2L), 0);
  36320. /* Set subject name, add pubkey, and sign certificate */
  36321. ExpectIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  36322. X509_NAME_free(name);
  36323. name = NULL;
  36324. ExpectIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  36325. #ifdef WOLFSSL_ALT_NAMES
  36326. /* Add some subject alt names */
  36327. ExpectIntNE(wolfSSL_X509_add_altname(NULL,
  36328. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  36329. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  36330. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  36331. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  36332. "sphygmomanometer",
  36333. ASN_DNS_TYPE), SSL_SUCCESS);
  36334. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  36335. "supercalifragilisticexpialidocious",
  36336. ASN_DNS_TYPE), SSL_SUCCESS);
  36337. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  36338. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  36339. ASN_DNS_TYPE), SSL_SUCCESS);
  36340. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  36341. {
  36342. unsigned char ip4_type[] = {127,128,0,255};
  36343. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  36344. 0xff, 0xee, 0x99, 0x88,
  36345. 0x77, 0x66, 0x55, 0x44,
  36346. 0x00, 0x33, 0x22, 0x11};
  36347. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  36348. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  36349. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  36350. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  36351. }
  36352. #endif
  36353. #endif /* WOLFSSL_ALT_NAMES */
  36354. /* test valid sign case */
  36355. ExpectIntGT(ret = X509_sign(x509, priv, EVP_sha256()), 0);
  36356. /* test valid X509_sign_ctx case */
  36357. ExpectNotNull(mctx = EVP_MD_CTX_new());
  36358. ExpectIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  36359. ExpectIntGT(X509_sign_ctx(x509, mctx), 0);
  36360. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  36361. ExpectIntEQ(X509_get_ext_count(x509), 1);
  36362. #endif
  36363. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  36364. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  36365. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  36366. #endif
  36367. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  36368. WOLFSSL_SUCCESS);
  36369. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  36370. /* Variation in size depends on ASN.1 encoding when MSB is set.
  36371. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  36372. * with the MSB set. See GenerateInteger in asn.c */
  36373. #ifndef USE_CERT_BUFFERS_1024
  36374. #ifndef WOLFSSL_ALT_NAMES
  36375. /* Valid case - size should be 781-786 with 16 byte serial number */
  36376. ExpectTrue((781 + snSz <= ret) && (ret <= 781 + 5 + snSz));
  36377. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  36378. /* Valid case - size should be 955-960 with 16 byte serial number */
  36379. ExpectTrue((939 + snSz <= ret) && (ret <= 939 + 5 + snSz));
  36380. #else
  36381. /* Valid case - size should be 926-931 with 16 byte serial number */
  36382. ExpectTrue((910 + snSz <= ret) && (ret <= 910 + 5 + snSz));
  36383. #endif
  36384. #else
  36385. #ifndef WOLFSSL_ALT_NAMES
  36386. /* Valid case - size should be 537-542 with 16 byte serial number */
  36387. ExpectTrue((521 + snSz <= ret) && (ret <= 521 + 5 + snSz));
  36388. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  36389. /* Valid case - size should be 695-670 with 16 byte serial number */
  36390. ExpectTrue((679 + snSz <= ret) && (ret <= 679 + 5 + snSz));
  36391. #else
  36392. /* Valid case - size should be 666-671 with 16 byte serial number */
  36393. ExpectTrue((650 + snSz <= ret) && (ret <= 650 + 5 + snSz));
  36394. #endif
  36395. #endif
  36396. /* check that issuer name is as expected after signature */
  36397. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  36398. ExpectIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  36399. ExpectNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  36400. ExpectNotNull(name = X509_get_subject_name(ca));
  36401. cnSz = X509_NAME_get_sz(name);
  36402. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  36403. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  36404. ExpectIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  36405. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  36406. cn = NULL;
  36407. #ifdef WOLFSSL_MULTI_ATTRIB
  36408. /* test adding multiple OU's to the signer */
  36409. ExpectNotNull(name = X509_get_subject_name(ca));
  36410. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  36411. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  36412. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  36413. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  36414. ExpectIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  36415. #endif
  36416. ExpectNotNull(name = X509_get_subject_name(ca));
  36417. ExpectIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  36418. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  36419. ExpectNotNull(name = X509_get_issuer_name(x509));
  36420. cnSz = X509_NAME_get_sz(name);
  36421. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  36422. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  36423. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  36424. ExpectIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  36425. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  36426. cn = NULL;
  36427. FreeDecodedCert(&dCert);
  36428. /* Test invalid parameters */
  36429. ExpectIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  36430. ExpectIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  36431. ExpectIntEQ(X509_sign(x509, priv, NULL), 0);
  36432. ExpectIntEQ(X509_sign_ctx(NULL, mctx), 0);
  36433. EVP_MD_CTX_free(mctx);
  36434. mctx = NULL;
  36435. ExpectNotNull(mctx = EVP_MD_CTX_new());
  36436. ExpectIntEQ(X509_sign_ctx(x509, mctx), 0);
  36437. ExpectIntEQ(X509_sign_ctx(x509, NULL), 0);
  36438. /* test invalid version number */
  36439. #if defined(OPENSSL_ALL)
  36440. ExpectIntNE(X509_set_version(x509, 6L), 0);
  36441. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  36442. /* uses ParseCert which fails on bad version number */
  36443. ExpectIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  36444. #endif
  36445. EVP_MD_CTX_free(mctx);
  36446. EVP_PKEY_free(priv);
  36447. EVP_PKEY_free(pub);
  36448. X509_free(x509);
  36449. X509_free(ca);
  36450. #endif
  36451. return EXPECT_RESULT();
  36452. }
  36453. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  36454. {
  36455. EXPECT_DECLS;
  36456. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  36457. X509* x509 = NULL;
  36458. const X509_ALGOR* alg;
  36459. ExpectNotNull(x509 = X509_new());
  36460. ExpectNull(alg = X509_get0_tbs_sigalg(NULL));
  36461. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  36462. X509_free(x509);
  36463. #endif
  36464. return EXPECT_RESULT();
  36465. }
  36466. static int test_wolfSSL_X509_ALGOR_get0(void)
  36467. {
  36468. EXPECT_DECLS;
  36469. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  36470. !defined(NO_SHA256) && !defined(NO_RSA)
  36471. X509* x509 = NULL;
  36472. const ASN1_OBJECT* obj = NULL;
  36473. const X509_ALGOR* alg = NULL;
  36474. int pptype = 0;
  36475. const void *ppval = NULL;
  36476. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  36477. SSL_FILETYPE_PEM));
  36478. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  36479. /* Invalid case */
  36480. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  36481. ExpectNull(obj);
  36482. /* Valid case */
  36483. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  36484. ExpectNotNull(obj);
  36485. ExpectNull(ppval);
  36486. ExpectIntNE(pptype, 0);
  36487. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  36488. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  36489. X509_free(x509);
  36490. #endif
  36491. return EXPECT_RESULT();
  36492. }
  36493. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  36494. {
  36495. EXPECT_DECLS;
  36496. #if defined(OPENSSL_EXTRA)
  36497. X509_VERIFY_PARAM *paramTo = NULL;
  36498. X509_VERIFY_PARAM *paramFrom = NULL;
  36499. char testIPv4[] = "127.0.0.1";
  36500. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  36501. char testhostName1[] = "foo.hoge.com";
  36502. char testhostName2[] = "foobar.hoge.com";
  36503. ExpectNotNull(paramTo = X509_VERIFY_PARAM_new());
  36504. ExpectNotNull(XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM)));
  36505. ExpectNotNull(paramFrom = X509_VERIFY_PARAM_new());
  36506. ExpectNotNull(XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM)));
  36507. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  36508. (int)XSTRLEN(testhostName1)), 1);
  36509. ExpectIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  36510. (int)XSTRLEN(testhostName1)));
  36511. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  36512. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  36513. ExpectIntEQ(0x01, paramFrom->hostFlags);
  36514. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4), 0);
  36515. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4), 1);
  36516. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  36517. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL), 1);
  36518. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6), 1);
  36519. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36520. /* null pointer */
  36521. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, paramFrom), 0);
  36522. /* in the case of "from" null, returns success */
  36523. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, NULL), 1);
  36524. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, NULL), 0);
  36525. /* inherit flags test : VPARAM_DEFAULT */
  36526. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  36527. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  36528. (int)XSTRLEN(testhostName1)));
  36529. ExpectIntEQ(0x01, paramTo->hostFlags);
  36530. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36531. /* inherit flags test : VPARAM OVERWRITE */
  36532. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  36533. (int)XSTRLEN(testhostName2)), 1);
  36534. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  36535. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  36536. if (paramTo != NULL) {
  36537. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  36538. }
  36539. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  36540. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  36541. (int)XSTRLEN(testhostName1)));
  36542. ExpectIntEQ(0x01, paramTo->hostFlags);
  36543. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36544. /* inherit flags test : VPARAM_RESET_FLAGS */
  36545. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  36546. (int)XSTRLEN(testhostName2)), 1);
  36547. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  36548. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  36549. if (paramTo != NULL) {
  36550. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  36551. }
  36552. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  36553. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  36554. (int)XSTRLEN(testhostName1)));
  36555. ExpectIntEQ(0x01, paramTo->hostFlags);
  36556. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36557. /* inherit flags test : VPARAM_LOCKED */
  36558. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  36559. (int)XSTRLEN(testhostName2)), 1);
  36560. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  36561. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  36562. if (paramTo != NULL) {
  36563. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  36564. }
  36565. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  36566. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  36567. (int)XSTRLEN(testhostName2)));
  36568. ExpectIntEQ(0x00, paramTo->hostFlags);
  36569. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  36570. /* test for incorrect parameters */
  36571. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL),
  36572. 0);
  36573. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, 0), 0);
  36574. /* inherit flags test : VPARAM_ONCE, not testable yet */
  36575. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL),
  36576. 1);
  36577. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo),
  36578. X509_V_FLAG_CRL_CHECK_ALL);
  36579. ExpectIntEQ(X509_VERIFY_PARAM_clear_flags(paramTo,
  36580. X509_V_FLAG_CRL_CHECK_ALL), 1);
  36581. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo), 0);
  36582. X509_VERIFY_PARAM_free(paramTo);
  36583. X509_VERIFY_PARAM_free(paramFrom);
  36584. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  36585. #endif
  36586. return EXPECT_RESULT();
  36587. }
  36588. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  36589. static int test_wolfSSL_check_domain_verify_count = 0;
  36590. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  36591. WOLFSSL_X509_STORE_CTX* store)
  36592. {
  36593. EXPECT_DECLS;
  36594. ExpectIntEQ(X509_STORE_CTX_get_error(store), 0);
  36595. ExpectIntEQ(preverify, 1);
  36596. ExpectIntGT(++test_wolfSSL_check_domain_verify_count, 0);
  36597. return EXPECT_SUCCESS();
  36598. }
  36599. static int test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  36600. {
  36601. EXPECT_DECLS;
  36602. X509_VERIFY_PARAM *param = NULL;
  36603. ExpectNotNull(param = SSL_get0_param(ssl));
  36604. /* Domain check should only be done on the leaf cert */
  36605. X509_VERIFY_PARAM_set_hostflags(param,
  36606. X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  36607. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(param,
  36608. "wolfSSL Server Chain", 0), 1);
  36609. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  36610. test_wolfSSL_check_domain_verify_cb);
  36611. return EXPECT_RESULT();
  36612. }
  36613. static int test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  36614. {
  36615. EXPECT_DECLS;
  36616. /* Use a cert with different domains in chain */
  36617. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  36618. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  36619. return EXPECT_RESULT();
  36620. }
  36621. static int test_wolfSSL_check_domain(void)
  36622. {
  36623. EXPECT_DECLS;
  36624. test_ssl_cbf func_cb_client;
  36625. test_ssl_cbf func_cb_server;
  36626. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  36627. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  36628. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  36629. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  36630. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  36631. &func_cb_server, NULL), TEST_SUCCESS);
  36632. /* Should have been called once for each cert in sent chain */
  36633. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  36634. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  36635. #else
  36636. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  36637. #endif
  36638. return EXPECT_RESULT();
  36639. }
  36640. #endif /* OPENSSL_EXTRA && HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  36641. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  36642. {
  36643. EXPECT_DECLS;
  36644. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  36645. X509* x509 = NULL;
  36646. X509_PUBKEY* pubKey;
  36647. ExpectNotNull(x509 = X509_new());
  36648. ExpectNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  36649. ExpectNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  36650. X509_free(x509);
  36651. #endif
  36652. return EXPECT_RESULT();
  36653. }
  36654. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  36655. {
  36656. EXPECT_DECLS;
  36657. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  36658. !defined(NO_SHA256) && !defined(NO_RSA)
  36659. X509* x509 = NULL;
  36660. ASN1_OBJECT* obj = NULL;
  36661. const ASN1_OBJECT* pa_oid = NULL;
  36662. X509_PUBKEY* pubKey = NULL;
  36663. X509_PUBKEY* pubKey2 = NULL;
  36664. EVP_PKEY* evpKey = NULL;
  36665. const unsigned char *pk = NULL;
  36666. int ppklen;
  36667. int pptype;
  36668. X509_ALGOR *pa = NULL;
  36669. const void *pval;
  36670. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  36671. SSL_FILETYPE_PEM));
  36672. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  36673. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  36674. ExpectNotNull(pk);
  36675. ExpectNotNull(pa);
  36676. ExpectNotNull(pubKey);
  36677. ExpectIntGT(ppklen, 0);
  36678. ExpectIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  36679. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  36680. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  36681. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  36682. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  36683. ExpectNotNull(pk);
  36684. ExpectNotNull(pa);
  36685. ExpectIntGT(ppklen, 0);
  36686. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  36687. ExpectNotNull(pa_oid);
  36688. ExpectNull(pval);
  36689. ExpectIntEQ(pptype, V_ASN1_NULL);
  36690. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  36691. X509_PUBKEY_free(pubKey2);
  36692. X509_free(x509);
  36693. EVP_PKEY_free(evpKey);
  36694. #endif
  36695. return EXPECT_RESULT();
  36696. }
  36697. static int test_wolfSSL_X509_PUBKEY_EC(void)
  36698. {
  36699. EXPECT_DECLS;
  36700. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  36701. X509* x509 = NULL;
  36702. ASN1_OBJECT* obj = NULL;
  36703. ASN1_OBJECT* poid = NULL;
  36704. const ASN1_OBJECT* pa_oid = NULL;
  36705. X509_PUBKEY* pubKey = NULL;
  36706. X509_PUBKEY* pubKey2 = NULL;
  36707. EVP_PKEY* evpKey = NULL;
  36708. const unsigned char *pk = NULL;
  36709. int ppklen;
  36710. int pptype;
  36711. X509_ALGOR *pa = NULL;
  36712. const void *pval;
  36713. char buf[50];
  36714. ExpectNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  36715. SSL_FILETYPE_PEM));
  36716. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  36717. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  36718. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  36719. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  36720. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  36721. ExpectNotNull(pk);
  36722. ExpectNotNull(pa);
  36723. ExpectIntGT(ppklen, 0);
  36724. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  36725. ExpectNotNull(pa_oid);
  36726. ExpectNotNull(pval);
  36727. ExpectIntEQ(pptype, V_ASN1_OBJECT);
  36728. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  36729. poid = (ASN1_OBJECT *)pval;
  36730. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  36731. ExpectIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  36732. X509_PUBKEY_free(pubKey2);
  36733. X509_free(x509);
  36734. EVP_PKEY_free(evpKey);
  36735. #endif
  36736. return EXPECT_RESULT();
  36737. }
  36738. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  36739. {
  36740. EXPECT_DECLS;
  36741. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  36742. word32 bytes;
  36743. #ifdef USE_CERT_BUFFERS_1024
  36744. byte tmp[ONEK_BUF];
  36745. #elif defined(USE_CERT_BUFFERS_2048)
  36746. byte tmp[TWOK_BUF];
  36747. #else
  36748. byte tmp[TWOK_BUF];
  36749. #endif /* END USE_CERT_BUFFERS_1024 */
  36750. const unsigned char* dsaKeyDer = tmp;
  36751. ASN1_OBJECT* obj = NULL;
  36752. ASN1_STRING* str;
  36753. const ASN1_OBJECT* pa_oid = NULL;
  36754. X509_PUBKEY* pubKey = NULL;
  36755. EVP_PKEY* evpKey = NULL;
  36756. const unsigned char *pk = NULL;
  36757. int ppklen, pptype;
  36758. X509_ALGOR *pa = NULL;
  36759. const void *pval;
  36760. #ifdef USE_CERT_BUFFERS_1024
  36761. XMEMSET(tmp, 0, sizeof(tmp));
  36762. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  36763. bytes = sizeof_dsa_key_der_1024;
  36764. #elif defined(USE_CERT_BUFFERS_2048)
  36765. XMEMSET(tmp, 0, sizeof(tmp));
  36766. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  36767. bytes = sizeof_dsa_key_der_2048;
  36768. #else
  36769. {
  36770. XFILE fp = XBADFILE;
  36771. XMEMSET(tmp, 0, sizeof(tmp));
  36772. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  36773. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  36774. if (fp != XBADFILE)
  36775. XFCLOSE(fp);
  36776. }
  36777. #endif
  36778. /* Initialize pkey with der format dsa key */
  36779. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  36780. ExpectNotNull(pubKey = X509_PUBKEY_new());
  36781. ExpectIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  36782. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  36783. ExpectNotNull(pk);
  36784. ExpectNotNull(pa);
  36785. ExpectIntGT(ppklen, 0);
  36786. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  36787. ExpectNotNull(pa_oid);
  36788. ExpectNotNull(pval);
  36789. ExpectIntEQ(pptype, V_ASN1_SEQUENCE);
  36790. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  36791. str = (ASN1_STRING *)pval;
  36792. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  36793. #ifdef USE_CERT_BUFFERS_1024
  36794. ExpectIntEQ(ASN1_STRING_length(str), 291);
  36795. #else
  36796. ExpectIntEQ(ASN1_STRING_length(str), 549);
  36797. #endif /* END USE_CERT_BUFFERS_1024 */
  36798. X509_PUBKEY_free(pubKey);
  36799. EVP_PKEY_free(evpKey);
  36800. #endif
  36801. return EXPECT_RESULT();
  36802. }
  36803. static int test_wolfSSL_BUF(void)
  36804. {
  36805. EXPECT_DECLS;
  36806. #if defined(OPENSSL_EXTRA)
  36807. BUF_MEM* buf = NULL;
  36808. ExpectNotNull(buf = BUF_MEM_new());
  36809. ExpectIntEQ(BUF_MEM_grow(buf, 10), 10);
  36810. ExpectIntEQ(BUF_MEM_grow(buf, -1), 0);
  36811. BUF_MEM_free(buf);
  36812. #endif
  36813. return EXPECT_RESULT();
  36814. }
  36815. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  36816. static int stub_rand_seed(const void *buf, int num)
  36817. {
  36818. (void)buf;
  36819. (void)num;
  36820. return 123;
  36821. }
  36822. static int stub_rand_bytes(unsigned char *buf, int num)
  36823. {
  36824. (void)buf;
  36825. (void)num;
  36826. return 456;
  36827. }
  36828. static byte* was_stub_rand_cleanup_called(void)
  36829. {
  36830. static byte was_called = 0;
  36831. return &was_called;
  36832. }
  36833. static void stub_rand_cleanup(void)
  36834. {
  36835. byte* was_called = was_stub_rand_cleanup_called();
  36836. *was_called = 1;
  36837. return;
  36838. }
  36839. static byte* was_stub_rand_add_called(void)
  36840. {
  36841. static byte was_called = 0;
  36842. return &was_called;
  36843. }
  36844. static int stub_rand_add(const void *buf, int num, double entropy)
  36845. {
  36846. byte* was_called = was_stub_rand_add_called();
  36847. (void)buf;
  36848. (void)num;
  36849. (void)entropy;
  36850. *was_called = 1;
  36851. return 0;
  36852. }
  36853. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  36854. {
  36855. (void)buf;
  36856. (void)num;
  36857. return 9876;
  36858. }
  36859. static int stub_rand_status(void)
  36860. {
  36861. return 5432;
  36862. }
  36863. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  36864. static int test_wolfSSL_RAND_set_rand_method(void)
  36865. {
  36866. EXPECT_DECLS;
  36867. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  36868. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  36869. unsigned char* buf = NULL;
  36870. int num = 0;
  36871. double entropy = 0;
  36872. int ret;
  36873. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  36874. byte* was_add_called = was_stub_rand_add_called();
  36875. ExpectNotNull(buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  36876. DYNAMIC_TYPE_TMP_BUFFER));
  36877. ExpectIntNE(wolfSSL_RAND_status(), 5432);
  36878. ExpectIntEQ(*was_cleanup_called, 0);
  36879. RAND_cleanup();
  36880. ExpectIntEQ(*was_cleanup_called, 0);
  36881. rand_methods.seed = &stub_rand_seed;
  36882. rand_methods.bytes = &stub_rand_bytes;
  36883. rand_methods.cleanup = &stub_rand_cleanup;
  36884. rand_methods.add = &stub_rand_add;
  36885. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  36886. rand_methods.status = &stub_rand_status;
  36887. ExpectIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  36888. ExpectIntEQ(RAND_seed(buf, num), 123);
  36889. ExpectIntEQ(RAND_bytes(buf, num), 456);
  36890. ExpectIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  36891. ExpectIntEQ(RAND_status(), 5432);
  36892. ExpectIntEQ(*was_add_called, 0);
  36893. /* The function pointer for RAND_add returns int, but RAND_add itself
  36894. * returns void. */
  36895. RAND_add(buf, num, entropy);
  36896. ExpectIntEQ(*was_add_called, 1);
  36897. was_add_called = 0;
  36898. ExpectIntEQ(*was_cleanup_called, 0);
  36899. RAND_cleanup();
  36900. ExpectIntEQ(*was_cleanup_called, 1);
  36901. *was_cleanup_called = 0;
  36902. ret = RAND_set_rand_method(NULL);
  36903. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  36904. ExpectIntNE(RAND_status(), 5432);
  36905. ExpectIntEQ(*was_cleanup_called, 0);
  36906. RAND_cleanup();
  36907. ExpectIntEQ(*was_cleanup_called, 0);
  36908. RAND_set_rand_method(NULL);
  36909. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36910. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  36911. return EXPECT_RESULT();
  36912. }
  36913. static int test_wolfSSL_RAND_bytes(void)
  36914. {
  36915. EXPECT_DECLS;
  36916. #if defined(OPENSSL_EXTRA)
  36917. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  36918. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  36919. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  36920. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  36921. int max_bufsize;
  36922. byte *my_buf = NULL;
  36923. /* sanity check */
  36924. ExpectIntEQ(RAND_bytes(NULL, 16), 0);
  36925. ExpectIntEQ(RAND_bytes(NULL, 0), 0);
  36926. max_bufsize = size4;
  36927. ExpectNotNull(my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  36928. DYNAMIC_TYPE_TMP_BUFFER));
  36929. ExpectIntEQ(RAND_bytes(my_buf, 0), 1);
  36930. ExpectIntEQ(RAND_bytes(my_buf, -1), 0);
  36931. ExpectNotNull(XMEMSET(my_buf, 0, max_bufsize));
  36932. ExpectIntEQ(RAND_bytes(my_buf, size1), 1);
  36933. ExpectIntEQ(RAND_bytes(my_buf, size2), 1);
  36934. ExpectIntEQ(RAND_bytes(my_buf, size3), 1);
  36935. ExpectIntEQ(RAND_bytes(my_buf, size4), 1);
  36936. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36937. #endif
  36938. return EXPECT_RESULT();
  36939. }
  36940. static int test_wolfSSL_RAND(void)
  36941. {
  36942. EXPECT_DECLS;
  36943. #if defined(OPENSSL_EXTRA)
  36944. byte seed[16];
  36945. XMEMSET(seed, 0, sizeof(seed));
  36946. /* No global methods set. */
  36947. ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
  36948. ExpectIntEQ(RAND_poll(), 1);
  36949. RAND_cleanup();
  36950. ExpectIntEQ(RAND_egd(NULL), -1);
  36951. #ifndef NO_FILESYSTEM
  36952. {
  36953. char fname[100];
  36954. ExpectNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  36955. ExpectIntEQ(RAND_write_file(NULL), 0);
  36956. }
  36957. #endif
  36958. #endif
  36959. return EXPECT_RESULT();
  36960. }
  36961. static int test_wolfSSL_PKCS8_Compat(void)
  36962. {
  36963. EXPECT_DECLS;
  36964. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC) && \
  36965. !defined(NO_BIO)
  36966. PKCS8_PRIV_KEY_INFO* pt = NULL;
  36967. BIO* bio = NULL;
  36968. XFILE f = XBADFILE;
  36969. int bytes;
  36970. char pkcs8_buffer[512];
  36971. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  36972. EVP_PKEY *pkey = NULL;
  36973. #endif
  36974. /* file from wolfssl/certs/ directory */
  36975. ExpectTrue((f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb")) != XBADFILE);
  36976. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)),
  36977. 0);
  36978. if (f != XBADFILE)
  36979. XFCLOSE(f);
  36980. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  36981. ExpectNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  36982. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  36983. ExpectNotNull(pkey = EVP_PKCS82PKEY(pt));
  36984. ExpectIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  36985. /* gets PKCS8 pointer to pkey */
  36986. ExpectNotNull(EVP_PKEY2PKCS8(pkey));
  36987. EVP_PKEY_free(pkey);
  36988. #endif
  36989. BIO_free(bio);
  36990. PKCS8_PRIV_KEY_INFO_free(pt);
  36991. #endif
  36992. return EXPECT_RESULT();
  36993. }
  36994. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  36995. static int NoPasswordCallBack(char* passwd, int sz, int rw, void* userdata)
  36996. {
  36997. (void)passwd;
  36998. (void)sz;
  36999. (void)rw;
  37000. (void)userdata;
  37001. return -1;
  37002. }
  37003. #endif
  37004. static int test_wolfSSL_PKCS8_d2i(void)
  37005. {
  37006. EXPECT_DECLS;
  37007. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  37008. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  37009. * requires a 12 byte password in FIPS mode. This test ends up
  37010. * trying to use an 8 byte password. */
  37011. #ifndef NO_FILESYSTEM
  37012. unsigned char pkcs8_buffer[2048];
  37013. const unsigned char* p = NULL;
  37014. int bytes = 0;
  37015. XFILE file = XBADFILE;
  37016. WOLFSSL_EVP_PKEY* pkey = NULL;
  37017. #ifndef NO_BIO
  37018. BIO* bio = NULL;
  37019. #if defined(OPENSSL_ALL) && \
  37020. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  37021. defined(HAVE_ECC)) && \
  37022. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37023. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  37024. #endif
  37025. #endif
  37026. #ifndef NO_RSA
  37027. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  37028. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  37029. #ifndef NO_DES3
  37030. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  37031. #endif
  37032. #endif /* NO_RSA */
  37033. #ifdef HAVE_ECC
  37034. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  37035. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  37036. #ifndef NO_DES3
  37037. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  37038. #endif
  37039. #endif /* HAVE_ECC */
  37040. #endif /* !NO_FILESYSTEM */
  37041. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  37042. #ifndef NO_RSA
  37043. #ifdef USE_CERT_BUFFERS_1024
  37044. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  37045. int rsaSz = sizeof_server_key_der_1024;
  37046. #else
  37047. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  37048. int rsaSz = sizeof_server_key_der_2048;
  37049. #endif
  37050. #endif
  37051. #ifdef HAVE_ECC
  37052. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  37053. int ecSz = sizeof_ecc_key_der_256;
  37054. #endif
  37055. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  37056. #ifndef NO_FILESYSTEM
  37057. (void)pkcs8_buffer;
  37058. (void)p;
  37059. (void)bytes;
  37060. (void)file;
  37061. #ifndef NO_BIO
  37062. (void)bio;
  37063. #endif
  37064. #endif
  37065. #ifdef OPENSSL_ALL
  37066. #ifndef NO_RSA
  37067. /* Try to auto-detect normal RSA private key */
  37068. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  37069. EVP_PKEY_free(pkey);
  37070. pkey = NULL;
  37071. #endif
  37072. #ifdef HAVE_ECC
  37073. /* Try to auto-detect normal EC private key */
  37074. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  37075. EVP_PKEY_free(pkey);
  37076. pkey = NULL;
  37077. #endif
  37078. #endif /* OPENSSL_ALL */
  37079. #ifndef NO_FILESYSTEM
  37080. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37081. ExpectIntEQ(PEM_write_PKCS8PrivateKey(XBADFILE, pkey, NULL, NULL, 0, NULL,
  37082. NULL), 0);
  37083. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  37084. NULL), 0);
  37085. #endif
  37086. #ifndef NO_RSA
  37087. /* Get DER encoded RSA PKCS#8 data. */
  37088. ExpectTrue((file = XFOPEN(rsaDerPkcs8File, "rb")) != XBADFILE);
  37089. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37090. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37091. file)), 0);
  37092. if (file != XBADFILE) {
  37093. XFCLOSE(file);
  37094. file = XBADFILE;
  37095. }
  37096. p = pkcs8_buffer;
  37097. #ifdef OPENSSL_ALL
  37098. /* Try to decode - auto-detect key type. */
  37099. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  37100. #else
  37101. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  37102. #endif
  37103. /* Get PEM encoded RSA PKCS#8 data. */
  37104. ExpectTrue((file = XFOPEN(rsaPemPkcs8File, "rb")) != XBADFILE);
  37105. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37106. file)), 0);
  37107. if (file != XBADFILE) {
  37108. XFCLOSE(file);
  37109. file = XBADFILE;
  37110. }
  37111. #if defined(OPENSSL_ALL) && \
  37112. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37113. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37114. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  37115. NULL), 0);
  37116. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  37117. NULL), 0);
  37118. /* Write PKCS#8 PEM to BIO. */
  37119. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  37120. NULL), bytes);
  37121. /* Write PKCS#8 PEM to stderr. */
  37122. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, NULL, NULL, 0, NULL,
  37123. NULL), bytes);
  37124. /* Compare file and written data */
  37125. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  37126. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  37127. BIO_free(bio);
  37128. bio = NULL;
  37129. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  37130. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_gcm(),
  37131. NULL, 0, PasswordCallBack, (void*)"yassl123"), 0);
  37132. #endif
  37133. #if !defined(NO_DES3) && !defined(NO_SHA)
  37134. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37135. /* Write Encrypted PKCS#8 PEM to BIO. */
  37136. bytes = 1834;
  37137. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  37138. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37139. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_des_ede3_cbc(),
  37140. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37141. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  37142. (void*)"yassl123"));
  37143. EVP_PKEY_free(evpPkey);
  37144. evpPkey = NULL;
  37145. BIO_free(bio);
  37146. bio = NULL;
  37147. #endif /* !NO_DES3 && !NO_SHA */
  37148. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37149. EVP_PKEY_free(pkey);
  37150. pkey = NULL;
  37151. /* PKCS#8 encrypted RSA key */
  37152. #ifndef NO_DES3
  37153. ExpectTrue((file = XFOPEN(rsaDerPkcs8EncFile, "rb")) != XBADFILE);
  37154. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37155. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37156. file)), 0);
  37157. if (file != XBADFILE) {
  37158. XFCLOSE(file);
  37159. file = XBADFILE;
  37160. }
  37161. #if defined(OPENSSL_ALL) && \
  37162. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37163. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  37164. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  37165. (void*)"yassl123"));
  37166. EVP_PKEY_free(pkey);
  37167. pkey = NULL;
  37168. BIO_free(bio);
  37169. bio = NULL;
  37170. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37171. #endif /* !NO_DES3 */
  37172. #endif /* NO_RSA */
  37173. #ifdef HAVE_ECC
  37174. /* PKCS#8 encode EC key */
  37175. ExpectTrue((file = XFOPEN(ecDerPkcs8File, "rb")) != XBADFILE);
  37176. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37177. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37178. file)), 0);
  37179. if (file != XBADFILE) {
  37180. XFCLOSE(file);
  37181. file = XBADFILE;
  37182. }
  37183. p = pkcs8_buffer;
  37184. #ifdef OPENSSL_ALL
  37185. /* Try to decode - auto-detect key type. */
  37186. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  37187. #else
  37188. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  37189. #endif
  37190. /* Get PEM encoded RSA PKCS#8 data. */
  37191. ExpectTrue((file = XFOPEN(ecPemPkcs8File, "rb")) != XBADFILE);
  37192. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37193. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37194. file)), 0);
  37195. if (file != XBADFILE) {
  37196. XFCLOSE(file);
  37197. file = XBADFILE;
  37198. }
  37199. #if defined(OPENSSL_ALL) && \
  37200. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  37201. defined(HAVE_AES_CBC)
  37202. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37203. /* Write PKCS#8 PEM to BIO. */
  37204. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  37205. NULL), bytes);
  37206. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, NULL, NULL, 0, NULL,
  37207. NULL), bytes);
  37208. /* Compare file and written data */
  37209. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  37210. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  37211. BIO_free(bio);
  37212. bio = NULL;
  37213. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37214. /* Write Encrypted PKCS#8 PEM to BIO. */
  37215. bytes = 379;
  37216. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  37217. NULL, 0, NoPasswordCallBack, (void*)"yassl123"), 0);
  37218. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  37219. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37220. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_cbc(),
  37221. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37222. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_cbc(),
  37223. (char*)"yassl123", 8, PasswordCallBack, NULL), bytes);
  37224. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_256_cbc(),
  37225. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37226. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  37227. (void*)"yassl123"));
  37228. EVP_PKEY_free(evpPkey);
  37229. evpPkey = NULL;
  37230. BIO_free(bio);
  37231. bio = NULL;
  37232. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  37233. EVP_PKEY_free(pkey);
  37234. pkey = NULL;
  37235. /* PKCS#8 encrypted EC key */
  37236. #ifndef NO_DES3
  37237. ExpectTrue((file = XFOPEN(ecDerPkcs8EncFile, "rb")) != XBADFILE);
  37238. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37239. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37240. file)), 0);
  37241. if (file != XBADFILE) {
  37242. XFCLOSE(file);
  37243. file = XBADFILE;
  37244. }
  37245. #if defined(OPENSSL_ALL) && \
  37246. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37247. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  37248. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  37249. (void*)"yassl123"));
  37250. EVP_PKEY_free(pkey);
  37251. pkey = NULL;
  37252. BIO_free(bio);
  37253. bio = NULL;
  37254. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37255. #endif /* !NO_DES3 */
  37256. #endif /* HAVE_ECC */
  37257. #endif /* !NO_FILESYSTEM */
  37258. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  37259. return EXPECT_RESULT();
  37260. }
  37261. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  37262. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  37263. #define LOGGING_THREADS 5
  37264. #define ERROR_COUNT 10
  37265. /* copied from logging.c since this is not exposed otherwise */
  37266. #ifndef ERROR_QUEUE_MAX
  37267. #ifdef ERROR_QUEUE_PER_THREAD
  37268. #define ERROR_QUEUE_MAX 16
  37269. #else
  37270. /* this breaks from compat of unlimited error queue size */
  37271. #define ERROR_QUEUE_MAX 100
  37272. #endif
  37273. #endif
  37274. static volatile int loggingThreadsReady;
  37275. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  37276. {
  37277. const char* file;
  37278. int line;
  37279. unsigned long err;
  37280. int errorCount = 0;
  37281. int i;
  37282. (void)args;
  37283. while (!loggingThreadsReady);
  37284. for (i = 0; i < ERROR_COUNT; i++)
  37285. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  37286. while ((err = ERR_get_error_line(&file, &line))) {
  37287. AssertIntEQ(err, 990 + errorCount);
  37288. errorCount++;
  37289. }
  37290. AssertIntEQ(errorCount, ERROR_COUNT);
  37291. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  37292. ERR_clear_error(); /* ERR_get_error_line() does not remove */
  37293. errorCount = 0;
  37294. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  37295. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  37296. while ((err = ERR_get_error_line(&file, &line))) {
  37297. AssertIntEQ(err, 990 + errorCount);
  37298. errorCount++;
  37299. }
  37300. /* test that the 3 errors over the max were dropped */
  37301. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  37302. WOLFSSL_RETURN_FROM_THREAD(0);
  37303. }
  37304. #endif
  37305. static int test_error_queue_per_thread(void)
  37306. {
  37307. int res = TEST_SKIPPED;
  37308. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  37309. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  37310. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  37311. int i;
  37312. ERR_clear_error(); /* clear out any error nodes */
  37313. loggingThreadsReady = 0;
  37314. for (i = 0; i < LOGGING_THREADS; i++)
  37315. start_thread(test_logging, NULL, &loggingThreads[i]);
  37316. loggingThreadsReady = 1;
  37317. for (i = 0; i < LOGGING_THREADS; i++)
  37318. join_thread(loggingThreads[i]);
  37319. res = TEST_SUCCESS;
  37320. #endif
  37321. return res;
  37322. }
  37323. static int test_wolfSSL_ERR_put_error(void)
  37324. {
  37325. EXPECT_DECLS;
  37326. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37327. defined(DEBUG_WOLFSSL)
  37328. const char* file;
  37329. int line;
  37330. ERR_clear_error(); /* clear out any error nodes */
  37331. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  37332. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  37333. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  37334. ExpectIntEQ(ERR_get_error_line(&file, &line), 1);
  37335. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  37336. ExpectIntEQ(ERR_get_error_line(&file, &line), 2);
  37337. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  37338. ExpectIntEQ(ERR_get_error_line(&file, &line), 3);
  37339. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  37340. ExpectIntEQ(ERR_get_error_line(&file, &line), 4);
  37341. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  37342. ExpectIntEQ(ERR_get_error_line(&file, &line), 5);
  37343. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  37344. ExpectIntEQ(ERR_get_error_line(&file, &line), 6);
  37345. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  37346. ExpectIntEQ(ERR_get_error_line(&file, &line), 7);
  37347. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  37348. ExpectIntEQ(ERR_get_error_line(&file, &line), 8);
  37349. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  37350. ExpectIntEQ(ERR_get_error_line(&file, &line), 9);
  37351. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  37352. ExpectIntEQ(ERR_get_error_line(&file, &line), 10);
  37353. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  37354. ExpectIntEQ(ERR_get_error_line(&file, &line), 11);
  37355. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  37356. ExpectIntEQ(ERR_get_error_line(&file, &line), 12);
  37357. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  37358. ExpectIntEQ(ERR_get_error_line(&file, &line), 13);
  37359. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  37360. ExpectIntEQ(ERR_get_error_line(&file, &line), 14);
  37361. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  37362. ExpectIntEQ(ERR_get_error_line(&file, &line), 15);
  37363. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  37364. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  37365. "this file", 100);
  37366. ExpectIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  37367. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  37368. ExpectIntEQ(line, 100);
  37369. ExpectIntEQ(wolfSSL_ERR_peek_error(),
  37370. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  37371. ExpectIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  37372. #endif
  37373. /* try reading past end of error queue */
  37374. file = NULL;
  37375. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  37376. ExpectNull(file);
  37377. ExpectIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  37378. PEMerr(4,4);
  37379. ExpectIntEQ(ERR_get_error(), 4);
  37380. /* Empty and free up all error nodes */
  37381. ERR_clear_error();
  37382. /* Verify all nodes are cleared */
  37383. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  37384. ERR_clear_error();
  37385. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  37386. #endif
  37387. return EXPECT_RESULT();
  37388. }
  37389. /*
  37390. * This is a regression test for a bug where the peek/get error functions were
  37391. * drawing from the end of the queue rather than the front.
  37392. */
  37393. static int test_wolfSSL_ERR_get_error_order(void)
  37394. {
  37395. EXPECT_DECLS;
  37396. #if defined(WOLFSSL_HAVE_ERROR_QUEUE) && defined(OPENSSL_EXTRA)
  37397. /* Empty the queue. */
  37398. wolfSSL_ERR_clear_error();
  37399. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  37400. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  37401. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  37402. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  37403. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  37404. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  37405. #endif /* WOLFSSL_HAVE_ERROR_QUEUE && OPENSSL_EXTRA */
  37406. return EXPECT_RESULT();
  37407. }
  37408. #ifndef NO_BIO
  37409. static int test_wolfSSL_ERR_print_errors(void)
  37410. {
  37411. EXPECT_DECLS;
  37412. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37413. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  37414. BIO* bio = NULL;
  37415. char buf[1024];
  37416. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37417. ERR_clear_error(); /* clear out any error nodes */
  37418. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  37419. /* Choosing -600 as an unused errno. */
  37420. ERR_put_error(0,SYS_F_BIND, -600, "asn.c", 100);
  37421. ERR_print_errors(bio);
  37422. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  37423. ExpectIntEQ(XSTRNCMP(
  37424. "error:173:wolfSSL library:Bad function argument:ssl.c:0",
  37425. buf, 55), 0);
  37426. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  37427. ExpectIntEQ(XSTRNCMP(
  37428. "error:600:wolfSSL library:unknown error number:asn.c:100",
  37429. buf, 56), 0);
  37430. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  37431. ExpectIntEQ(buf[0], '\0');
  37432. ExpectIntEQ(ERR_get_error_line(NULL, NULL), 0);
  37433. BIO_free(bio);
  37434. #endif
  37435. return EXPECT_RESULT();
  37436. }
  37437. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37438. defined(DEBUG_WOLFSSL)
  37439. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  37440. {
  37441. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  37442. return 0;
  37443. }
  37444. #endif
  37445. static int test_wolfSSL_ERR_print_errors_cb(void)
  37446. {
  37447. EXPECT_DECLS;
  37448. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37449. defined(DEBUG_WOLFSSL)
  37450. BIO* bio = NULL;
  37451. char buf[1024];
  37452. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37453. ERR_clear_error(); /* clear out any error nodes */
  37454. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  37455. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  37456. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  37457. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  37458. ExpectIntEQ(XSTRNCMP(
  37459. "wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  37460. buf, 53), 0);
  37461. ExpectIntEQ(XSTRNCMP(
  37462. "wolfSSL error occurred, error = 275 line:100 file:asn.c",
  37463. buf + 53, 55), 0);
  37464. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  37465. BIO_free(bio);
  37466. #endif
  37467. return EXPECT_RESULT();
  37468. }
  37469. /*
  37470. * Testing WOLFSSL_ERROR_MSG
  37471. */
  37472. static int test_WOLFSSL_ERROR_MSG(void)
  37473. {
  37474. int res = TEST_SKIPPED;
  37475. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  37476. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  37477. const char* msg = TEST_STRING;
  37478. WOLFSSL_ERROR_MSG(msg);
  37479. res = TEST_SUCCESS;
  37480. #endif
  37481. return res;
  37482. } /* End test_WOLFSSL_ERROR_MSG */
  37483. /*
  37484. * Testing wc_ERR_remove_state
  37485. */
  37486. static int test_wc_ERR_remove_state(void)
  37487. {
  37488. int res = TEST_SKIPPED;
  37489. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  37490. wc_ERR_remove_state();
  37491. res = TEST_SUCCESS;
  37492. #endif
  37493. return res;
  37494. } /* End test_wc_ERR_remove_state */
  37495. /*
  37496. * Testing wc_ERR_print_errors_fp
  37497. */
  37498. static int test_wc_ERR_print_errors_fp(void)
  37499. {
  37500. EXPECT_DECLS;
  37501. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  37502. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  37503. long sz;
  37504. XFILE fp = XBADFILE;
  37505. WOLFSSL_ERROR(BAD_FUNC_ARG);
  37506. ExpectTrue((fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar")) !=
  37507. XBADFILE);
  37508. wc_ERR_print_errors_fp(fp);
  37509. #if defined(DEBUG_WOLFSSL)
  37510. ExpectTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  37511. #ifdef NO_ERROR_QUEUE
  37512. ExpectIntEQ(sz = XFTELL(fp), 0);
  37513. #else
  37514. ExpectIntNE(sz = XFTELL(fp), 0);
  37515. #endif
  37516. #endif
  37517. if (fp != XBADFILE)
  37518. XFCLOSE(fp);
  37519. (void)sz;
  37520. #endif
  37521. return EXPECT_RESULT();
  37522. } /* End test_wc_ERR_print_errors_fp */
  37523. #ifdef DEBUG_WOLFSSL
  37524. static void Logging_cb(const int logLevel, const char *const logMessage)
  37525. {
  37526. (void)logLevel;
  37527. (void)logMessage;
  37528. }
  37529. #endif
  37530. /*
  37531. * Testing wolfSSL_GetLoggingCb
  37532. */
  37533. static int test_wolfSSL_GetLoggingCb(void)
  37534. {
  37535. EXPECT_DECLS;
  37536. #ifdef DEBUG_WOLFSSL
  37537. /* Testing without wolfSSL_SetLoggingCb() */
  37538. ExpectNull(wolfSSL_GetLoggingCb());
  37539. /* Testing with wolfSSL_SetLoggingCb() */
  37540. ExpectIntEQ(wolfSSL_SetLoggingCb(Logging_cb), 0);
  37541. ExpectNotNull(wolfSSL_GetLoggingCb());
  37542. ExpectIntEQ(wolfSSL_SetLoggingCb(NULL), 0);
  37543. #endif
  37544. ExpectNull(wolfSSL_GetLoggingCb());
  37545. return EXPECT_RESULT();
  37546. } /* End test_wolfSSL_GetLoggingCb */
  37547. #endif /* !NO_BIO */
  37548. static int test_wolfSSL_MD4(void)
  37549. {
  37550. EXPECT_DECLS;
  37551. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  37552. MD4_CTX md4;
  37553. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  37554. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  37555. "789012345678901234567890";
  37556. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  37557. "\xcc\x05\x36";
  37558. int msgSz = (int)XSTRLEN(msg);
  37559. XMEMSET(out, 0, sizeof(out));
  37560. MD4_Init(&md4);
  37561. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  37562. MD4_Final(out, &md4);
  37563. ExpectIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  37564. #endif
  37565. return EXPECT_RESULT();
  37566. }
  37567. static int test_wolfSSL_MD5(void)
  37568. {
  37569. EXPECT_DECLS;
  37570. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  37571. byte input1[] = "";
  37572. byte input2[] = "message digest";
  37573. byte hash[WC_MD5_DIGEST_SIZE];
  37574. unsigned char output1[] =
  37575. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  37576. unsigned char output2[] =
  37577. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  37578. WOLFSSL_MD5_CTX md5;
  37579. XMEMSET(&md5, 0, sizeof(md5));
  37580. /* Test cases for illegal parameters */
  37581. ExpectIntEQ(MD5_Init(NULL), 0);
  37582. ExpectIntEQ(MD5_Init(&md5), 1);
  37583. ExpectIntEQ(MD5_Update(NULL, input1, 0), 0);
  37584. ExpectIntEQ(MD5_Update(NULL, NULL, 0), 0);
  37585. ExpectIntEQ(MD5_Update(&md5, NULL, 1), 0);
  37586. ExpectIntEQ(MD5_Final(NULL, &md5), 0);
  37587. ExpectIntEQ(MD5_Final(hash, NULL), 0);
  37588. ExpectIntEQ(MD5_Final(NULL, NULL), 0);
  37589. /* Init MD5 CTX */
  37590. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  37591. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input1, XSTRLEN((const char*)&input1)),
  37592. 1);
  37593. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  37594. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  37595. /* Init MD5 CTX */
  37596. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  37597. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input2,
  37598. (int)XSTRLEN((const char*)input2)), 1);
  37599. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  37600. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  37601. #if !defined(NO_OLD_NAMES) && \
  37602. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  37603. ExpectPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  37604. ExpectPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  37605. ExpectPtrNE(MD5(input1, 1, NULL), NULL);
  37606. ExpectPtrNE(MD5(NULL, 0, NULL), NULL);
  37607. ExpectPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash),
  37608. &hash);
  37609. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  37610. ExpectPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash),
  37611. &hash);
  37612. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  37613. {
  37614. byte data[] = "Data to be hashed.";
  37615. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  37616. ExpectNotNull(MD5(data, sizeof(data), NULL));
  37617. ExpectNotNull(MD5(data, sizeof(data), hash));
  37618. ExpectNotNull(MD5(NULL, 0, hash));
  37619. ExpectNull(MD5(NULL, sizeof(data), hash));
  37620. }
  37621. #endif
  37622. #endif
  37623. return EXPECT_RESULT();
  37624. }
  37625. static int test_wolfSSL_MD5_Transform(void)
  37626. {
  37627. EXPECT_DECLS;
  37628. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  37629. byte input1[] = "";
  37630. byte input2[] = "abc";
  37631. byte local[WC_MD5_BLOCK_SIZE];
  37632. word32 sLen = 0;
  37633. #ifdef BIG_ENDIAN_ORDER
  37634. unsigned char output1[] =
  37635. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  37636. unsigned char output2[] =
  37637. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  37638. #else
  37639. unsigned char output1[] =
  37640. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  37641. unsigned char output2[] =
  37642. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  37643. #endif
  37644. union {
  37645. wc_Md5 native;
  37646. MD5_CTX compat;
  37647. } md5;
  37648. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  37649. XMEMSET(&local, 0, sizeof(local));
  37650. /* sanity check */
  37651. ExpectIntEQ(MD5_Transform(NULL, NULL), 0);
  37652. ExpectIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  37653. ExpectIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  37654. ExpectIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  37655. ExpectIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37656. ExpectIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  37657. /* Init MD5 CTX */
  37658. ExpectIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  37659. /* Do Transform*/
  37660. sLen = (word32)XSTRLEN((char*)input1);
  37661. XMEMCPY(local, input1, sLen);
  37662. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  37663. ExpectIntEQ(XMEMCMP(md5.native.digest, output1, WC_MD5_DIGEST_SIZE), 0);
  37664. /* Init MD5 CTX */
  37665. ExpectIntEQ(MD5_Init(&md5.compat), 1);
  37666. sLen = (word32)XSTRLEN((char*)input2);
  37667. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  37668. XMEMCPY(local, input2, sLen);
  37669. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  37670. ExpectIntEQ(XMEMCMP(md5.native.digest, output2, WC_MD5_DIGEST_SIZE), 0);
  37671. #endif
  37672. return EXPECT_RESULT();
  37673. }
  37674. static int test_wolfSSL_SHA(void)
  37675. {
  37676. EXPECT_DECLS;
  37677. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  37678. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  37679. (!defined(HAVE_FIPS) || \
  37680. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  37681. {
  37682. const unsigned char in[] = "abc";
  37683. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  37684. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  37685. unsigned char out[WC_SHA_DIGEST_SIZE];
  37686. unsigned char* p = NULL;
  37687. WOLFSSL_SHA_CTX sha;
  37688. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  37689. ExpectNotNull(SHA1(in, XSTRLEN((char*)in), out));
  37690. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37691. /* SHA interface test */
  37692. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  37693. ExpectNull(SHA(NULL, XSTRLEN((char*)in), out));
  37694. ExpectNotNull(SHA(in, 0, out));
  37695. ExpectNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  37696. ExpectNotNull(SHA(NULL, 0, out));
  37697. ExpectNotNull(SHA(NULL, 0, NULL));
  37698. ExpectNotNull(SHA(in, XSTRLEN((char*)in), out));
  37699. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37700. ExpectNotNull(p = SHA(in, XSTRLEN((char*)in), NULL));
  37701. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA_DIGEST_SIZE), 0);
  37702. ExpectIntEQ(wolfSSL_SHA_Init(&sha), 1);
  37703. ExpectIntEQ(wolfSSL_SHA_Update(&sha, in, XSTRLEN((char*)in)), 1);
  37704. ExpectIntEQ(wolfSSL_SHA_Final(out, &sha), 1);
  37705. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37706. ExpectIntEQ(wolfSSL_SHA1_Init(&sha), 1);
  37707. ExpectIntEQ(wolfSSL_SHA1_Update(&sha, in, XSTRLEN((char*)in)), 1);
  37708. ExpectIntEQ(wolfSSL_SHA1_Final(out, &sha), 1);
  37709. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37710. }
  37711. #endif
  37712. #if !defined(NO_SHA256)
  37713. {
  37714. const unsigned char in[] = "abc";
  37715. unsigned char expected[] =
  37716. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  37717. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  37718. "\x15\xAD";
  37719. unsigned char out[WC_SHA256_DIGEST_SIZE];
  37720. unsigned char* p = NULL;
  37721. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  37722. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37723. ExpectNotNull(SHA256(in, XSTRLEN((char*)in), out));
  37724. #else
  37725. ExpectNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  37726. #endif
  37727. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  37728. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37729. ExpectNotNull(p = SHA256(in, XSTRLEN((char*)in), NULL));
  37730. #else
  37731. ExpectNotNull(p = wolfSSL_SHA256(in, XSTRLEN((char*)in), NULL));
  37732. #endif
  37733. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA256_DIGEST_SIZE), 0);
  37734. }
  37735. #endif
  37736. #if defined(WOLFSSL_SHA384)
  37737. {
  37738. const unsigned char in[] = "abc";
  37739. unsigned char expected[] =
  37740. "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  37741. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  37742. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  37743. "\xc8\x25\xa7";
  37744. unsigned char out[WC_SHA384_DIGEST_SIZE];
  37745. unsigned char* p = NULL;
  37746. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  37747. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37748. ExpectNotNull(SHA384(in, XSTRLEN((char*)in), out));
  37749. #else
  37750. ExpectNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  37751. #endif
  37752. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  37753. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37754. ExpectNotNull(p = SHA384(in, XSTRLEN((char*)in), NULL));
  37755. #else
  37756. ExpectNotNull(p = wolfSSL_SHA384(in, XSTRLEN((char*)in), NULL));
  37757. #endif
  37758. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA384_DIGEST_SIZE), 0);
  37759. }
  37760. #endif
  37761. #if defined(WOLFSSL_SHA512)
  37762. {
  37763. const unsigned char in[] = "abc";
  37764. unsigned char expected[] =
  37765. "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  37766. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  37767. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  37768. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  37769. "\xa5\x4c\xa4\x9f";
  37770. unsigned char out[WC_SHA512_DIGEST_SIZE];
  37771. unsigned char* p = NULL;
  37772. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  37773. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37774. ExpectNotNull(SHA512(in, XSTRLEN((char*)in), out));
  37775. #else
  37776. ExpectNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  37777. #endif
  37778. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  37779. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37780. ExpectNotNull(p = SHA512(in, XSTRLEN((char*)in), NULL));
  37781. #else
  37782. ExpectNotNull(p = wolfSSL_SHA512(in, XSTRLEN((char*)in), NULL));
  37783. #endif
  37784. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA512_DIGEST_SIZE), 0);
  37785. }
  37786. #endif
  37787. #endif
  37788. return EXPECT_RESULT();
  37789. }
  37790. static int test_wolfSSL_SHA_Transform(void)
  37791. {
  37792. EXPECT_DECLS;
  37793. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  37794. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37795. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  37796. byte input1[] = "";
  37797. byte input2[] = "abc";
  37798. byte local[WC_SHA_BLOCK_SIZE];
  37799. word32 sLen = 0;
  37800. #ifdef BIG_ENDIAN_ORDER
  37801. unsigned char output1[] =
  37802. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  37803. "\x09\xf7\x3a\x93";
  37804. unsigned char output2[] =
  37805. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  37806. "\x7c\x6e\x08\xb8";
  37807. #else
  37808. unsigned char output1[] =
  37809. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  37810. "\x93\x3a\xf7\x09";
  37811. unsigned char output2[] =
  37812. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  37813. "\xb8\x08\x6e\x7c";
  37814. #endif
  37815. union {
  37816. wc_Sha native;
  37817. SHA_CTX compat;
  37818. } sha;
  37819. union {
  37820. wc_Sha native;
  37821. SHA_CTX compat;
  37822. } sha1;
  37823. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  37824. XMEMSET(&local, 0, sizeof(local));
  37825. /* sanity check */
  37826. ExpectIntEQ(SHA_Transform(NULL, NULL), 0);
  37827. ExpectIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  37828. ExpectIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  37829. ExpectIntEQ(SHA1_Transform(NULL, NULL), 0);
  37830. ExpectIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  37831. ExpectIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  37832. ExpectIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  37833. ExpectIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37834. ExpectIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  37835. /* Init SHA CTX */
  37836. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  37837. /* Do Transform*/
  37838. sLen = (word32)XSTRLEN((char*)input1);
  37839. XMEMCPY(local, input1, sLen);
  37840. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  37841. ExpectIntEQ(XMEMCMP(sha.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  37842. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  37843. /* Init SHA CTX */
  37844. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  37845. sLen = (word32)XSTRLEN((char*)input2);
  37846. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37847. XMEMCPY(local, input2, sLen);
  37848. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  37849. ExpectIntEQ(XMEMCMP(sha.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  37850. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  37851. /* SHA1 */
  37852. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37853. /* Init SHA CTX */
  37854. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  37855. /* Do Transform*/
  37856. sLen = (word32)XSTRLEN((char*)input1);
  37857. XMEMCPY(local, input1, sLen);
  37858. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  37859. ExpectIntEQ(XMEMCMP(sha1.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  37860. ExpectIntEQ(SHA1_Final(local, &sha1.compat), 1); /* frees resources */
  37861. /* Init SHA CTX */
  37862. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  37863. sLen = (word32)XSTRLEN((char*)input2);
  37864. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37865. XMEMCPY(local, input2, sLen);
  37866. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  37867. ExpectIntEQ(XMEMCMP(sha1.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  37868. ExpectIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  37869. #endif
  37870. #endif
  37871. return EXPECT_RESULT();
  37872. }
  37873. static int test_wolfSSL_SHA224(void)
  37874. {
  37875. EXPECT_DECLS;
  37876. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  37877. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37878. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  37879. unsigned char input[] =
  37880. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  37881. unsigned char output[] =
  37882. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  37883. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  37884. size_t inLen;
  37885. byte hash[WC_SHA224_DIGEST_SIZE];
  37886. unsigned char* p;
  37887. inLen = XSTRLEN((char*)input);
  37888. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  37889. ExpectNull(SHA224(NULL, inLen, hash));
  37890. ExpectNotNull(SHA224(input, 0, hash));
  37891. ExpectNotNull(SHA224(input, inLen, NULL));
  37892. ExpectNotNull(SHA224(NULL, 0, hash));
  37893. ExpectNotNull(SHA224(NULL, 0, NULL));
  37894. ExpectNotNull(SHA224(input, inLen, hash));
  37895. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  37896. ExpectNotNull(p = SHA224(input, inLen, NULL));
  37897. ExpectIntEQ(XMEMCMP(p, output, WC_SHA224_DIGEST_SIZE), 0);
  37898. #endif
  37899. return EXPECT_RESULT();
  37900. }
  37901. static int test_wolfSSL_SHA256(void)
  37902. {
  37903. EXPECT_DECLS;
  37904. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  37905. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  37906. unsigned char input[] =
  37907. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  37908. unsigned char output[] =
  37909. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  37910. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  37911. "\x06\xC1";
  37912. size_t inLen;
  37913. byte hash[WC_SHA256_DIGEST_SIZE];
  37914. inLen = XSTRLEN((char*)input);
  37915. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  37916. ExpectNotNull(SHA256(input, inLen, hash));
  37917. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  37918. #endif
  37919. return EXPECT_RESULT();
  37920. }
  37921. static int test_wolfSSL_SHA256_Transform(void)
  37922. {
  37923. EXPECT_DECLS;
  37924. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  37925. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37926. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37927. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH) && \
  37928. !defined(WOLFSSL_KCAPI_HASH)
  37929. byte input1[] = "";
  37930. byte input2[] = "abc";
  37931. byte local[WC_SHA256_BLOCK_SIZE];
  37932. word32 sLen = 0;
  37933. #ifdef BIG_ENDIAN_ORDER
  37934. unsigned char output1[] =
  37935. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  37936. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  37937. unsigned char output2[] =
  37938. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  37939. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  37940. #else
  37941. unsigned char output1[] =
  37942. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  37943. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  37944. unsigned char output2[] =
  37945. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  37946. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  37947. #endif
  37948. union {
  37949. wc_Sha256 native;
  37950. SHA256_CTX compat;
  37951. } sha256;
  37952. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  37953. XMEMSET(&local, 0, sizeof(local));
  37954. /* sanity check */
  37955. ExpectIntEQ(SHA256_Transform(NULL, NULL), 0);
  37956. ExpectIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  37957. ExpectIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  37958. ExpectIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  37959. ExpectIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37960. ExpectIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  37961. /* Init SHA256 CTX */
  37962. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  37963. /* Do Transform*/
  37964. sLen = (word32)XSTRLEN((char*)input1);
  37965. XMEMCPY(local, input1, sLen);
  37966. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  37967. ExpectIntEQ(XMEMCMP(sha256.native.digest, output1, WC_SHA256_DIGEST_SIZE),
  37968. 0);
  37969. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  37970. /* Init SHA256 CTX */
  37971. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  37972. sLen = (word32)XSTRLEN((char*)input2);
  37973. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  37974. XMEMCPY(local, input2, sLen);
  37975. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  37976. ExpectIntEQ(XMEMCMP(sha256.native.digest, output2, WC_SHA256_DIGEST_SIZE),
  37977. 0);
  37978. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  37979. #endif
  37980. #endif
  37981. return EXPECT_RESULT();
  37982. }
  37983. static int test_wolfSSL_SHA512_Transform(void)
  37984. {
  37985. EXPECT_DECLS;
  37986. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  37987. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37988. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37989. !defined(WOLFSSL_KCAPI_HASH)
  37990. byte input1[] = "";
  37991. byte input2[] = "abc";
  37992. byte local[WC_SHA512_BLOCK_SIZE];
  37993. word32 sLen = 0;
  37994. #ifdef BIG_ENDIAN_ORDER
  37995. unsigned char output1[] =
  37996. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  37997. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  37998. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  37999. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  38000. unsigned char output2[] =
  38001. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  38002. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  38003. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  38004. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  38005. #else
  38006. unsigned char output1[] =
  38007. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  38008. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  38009. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  38010. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  38011. unsigned char output2[] =
  38012. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  38013. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  38014. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  38015. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  38016. #endif
  38017. union {
  38018. wc_Sha512 native;
  38019. SHA512_CTX compat;
  38020. } sha512;
  38021. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  38022. XMEMSET(&local, 0, sizeof(local));
  38023. /* sanity check */
  38024. ExpectIntEQ(SHA512_Transform(NULL, NULL), 0);
  38025. ExpectIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  38026. ExpectIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  38027. ExpectIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  38028. ExpectIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  38029. ExpectIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  38030. /* Init SHA512 CTX */
  38031. ExpectIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  38032. /* Do Transform*/
  38033. sLen = (word32)XSTRLEN((char*)input1);
  38034. XMEMCPY(local, input1, sLen);
  38035. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  38036. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  38037. WC_SHA512_DIGEST_SIZE), 0);
  38038. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  38039. /* Init SHA512 CTX */
  38040. ExpectIntEQ(SHA512_Init(&sha512.compat), 1);
  38041. sLen = (word32)XSTRLEN((char*)input2);
  38042. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  38043. XMEMCPY(local, input2, sLen);
  38044. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  38045. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  38046. WC_SHA512_DIGEST_SIZE), 0);
  38047. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  38048. (void)input1;
  38049. #endif
  38050. #endif
  38051. return EXPECT_RESULT();
  38052. }
  38053. static int test_wolfSSL_SHA512_224_Transform(void)
  38054. {
  38055. EXPECT_DECLS;
  38056. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  38057. !defined(WOLFSSL_NOSHA512_224)
  38058. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  38059. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  38060. !defined(WOLFSSL_KCAPI_HASH)
  38061. byte input1[] = "";
  38062. byte input2[] = "abc";
  38063. byte local[WC_SHA512_BLOCK_SIZE];
  38064. word32 sLen = 0;
  38065. unsigned char output1[] =
  38066. "\x94\x24\x66\xd4\x60\x3a\xeb\x23\x1d\xa8\x69\x31\x3c\xd2\xde\x11"
  38067. "\x48\x0f\x4a\x5a\xdf\x3a\x8d\x87\xcf\xcd\xbf\xa5\x03\x21\x50\xf1"
  38068. "\x8a\x0d\x0f\x0d\x3c\x07\xba\x52\xe0\xaa\x3c\xbb\xf1\xd3\x3f\xca"
  38069. "\x12\xa7\x61\xf8\x47\xda\x0d\x1b\x79\xc2\x65\x13\x92\xc1\x9c\xa5";
  38070. unsigned char output2[] =
  38071. "\x51\x28\xe7\x0b\xca\x1e\xbc\x5f\xd7\x34\x0b\x48\x30\xd7\xc2\x75"
  38072. "\x6d\x8d\x48\x2c\x1f\xc7\x9e\x2b\x20\x5e\xbb\x0f\x0e\x4d\xb7\x61"
  38073. "\x31\x76\x33\xa0\xb4\x3d\x5f\x93\xc1\x73\xac\xf7\x21\xff\x69\x17"
  38074. "\xce\x66\xe5\x1e\x31\xe7\xf3\x22\x0f\x0b\x34\xd7\x5a\x57\xeb\xbf";
  38075. union {
  38076. wc_Sha512 native;
  38077. SHA512_CTX compat;
  38078. } sha512;
  38079. #ifdef BIG_ENDIAN_ORDER
  38080. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  38081. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  38082. #endif
  38083. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  38084. XMEMSET(&local, 0, sizeof(local));
  38085. /* sanity check */
  38086. ExpectIntEQ(SHA512_224_Transform(NULL, NULL), 0);
  38087. ExpectIntEQ(SHA512_224_Transform(NULL, (const byte*)&input1), 0);
  38088. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, NULL), 0);
  38089. ExpectIntEQ(wc_Sha512_224Transform(NULL, NULL), BAD_FUNC_ARG);
  38090. ExpectIntEQ(wc_Sha512_224Transform(NULL, (const byte*)&input1),
  38091. BAD_FUNC_ARG);
  38092. ExpectIntEQ(wc_Sha512_224Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  38093. /* Init SHA512 CTX */
  38094. ExpectIntEQ(wolfSSL_SHA512_224_Init(&sha512.compat), 1);
  38095. /* Do Transform*/
  38096. sLen = (word32)XSTRLEN((char*)input1);
  38097. XMEMCPY(local, input1, sLen);
  38098. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  38099. 1);
  38100. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  38101. WC_SHA512_DIGEST_SIZE), 0);
  38102. /* frees resources */
  38103. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  38104. /* Init SHA512 CTX */
  38105. ExpectIntEQ(SHA512_224_Init(&sha512.compat), 1);
  38106. sLen = (word32)XSTRLEN((char*)input2);
  38107. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  38108. XMEMCPY(local, input2, sLen);
  38109. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  38110. 1);
  38111. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  38112. WC_SHA512_DIGEST_SIZE), 0);
  38113. /* frees resources */
  38114. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  38115. #endif
  38116. #endif
  38117. return EXPECT_RESULT();
  38118. }
  38119. static int test_wolfSSL_SHA512_256_Transform(void)
  38120. {
  38121. EXPECT_DECLS;
  38122. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  38123. !defined(WOLFSSL_NOSHA512_256)
  38124. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  38125. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  38126. !defined(WOLFSSL_KCAPI_HASH)
  38127. byte input1[] = "";
  38128. byte input2[] = "abc";
  38129. byte local[WC_SHA512_BLOCK_SIZE];
  38130. word32 sLen = 0;
  38131. unsigned char output1[] =
  38132. "\xf8\x37\x37\x5a\xd7\x2e\x56\xec\xe2\x51\xa8\x31\x3a\xa0\x63\x2b"
  38133. "\x7e\x7c\x64\xcc\xd9\xff\x2b\x6b\xeb\xc3\xd4\x4d\x7f\x8a\x3a\xb5"
  38134. "\x61\x85\x0b\x37\x30\x9f\x3b\x08\x5e\x7b\xd3\xbc\x6d\x00\x61\xc0"
  38135. "\x65\x9a\xd7\x73\xda\x40\xbe\xc1\xe5\x2f\xc6\x5d\xb7\x9f\xbe\x60";
  38136. unsigned char output2[] =
  38137. "\x22\xad\xc0\x30\xee\xd4\x6a\xef\x13\xee\x5a\x95\x8b\x1f\xb7\xb6"
  38138. "\xb6\xba\xc0\x44\xb8\x18\x3b\xf0\xf6\x4b\x70\x9f\x03\xba\x64\xa1"
  38139. "\xe1\xe3\x45\x15\x91\x7d\xcb\x0b\x9a\xf0\xd2\x8e\x47\x8b\x37\x78"
  38140. "\x91\x41\xa6\xc4\xb0\x29\x8f\x8b\xdd\x78\x5c\xf2\x73\x3f\x21\x31";
  38141. union {
  38142. wc_Sha512 native;
  38143. SHA512_CTX compat;
  38144. } sha512;
  38145. #ifdef BIG_ENDIAN_ORDER
  38146. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  38147. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  38148. #endif
  38149. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  38150. XMEMSET(&local, 0, sizeof(local));
  38151. /* sanity check */
  38152. ExpectIntEQ(SHA512_256_Transform(NULL, NULL), 0);
  38153. ExpectIntEQ(SHA512_256_Transform(NULL, (const byte*)&input1), 0);
  38154. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, NULL), 0);
  38155. ExpectIntEQ(wc_Sha512_256Transform(NULL, NULL), BAD_FUNC_ARG);
  38156. ExpectIntEQ(wc_Sha512_256Transform(NULL, (const byte*)&input1),
  38157. BAD_FUNC_ARG);
  38158. ExpectIntEQ(wc_Sha512_256Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  38159. /* Init SHA512 CTX */
  38160. ExpectIntEQ(wolfSSL_SHA512_256_Init(&sha512.compat), 1);
  38161. /* Do Transform*/
  38162. sLen = (word32)XSTRLEN((char*)input1);
  38163. XMEMCPY(local, input1, sLen);
  38164. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  38165. 1);
  38166. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  38167. WC_SHA512_DIGEST_SIZE), 0);
  38168. /* frees resources */
  38169. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  38170. /* Init SHA512 CTX */
  38171. ExpectIntEQ(SHA512_256_Init(&sha512.compat), 1);
  38172. sLen = (word32)XSTRLEN((char*)input2);
  38173. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  38174. XMEMCPY(local, input2, sLen);
  38175. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  38176. 1);
  38177. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  38178. WC_SHA512_DIGEST_SIZE), 0);
  38179. /* frees resources */
  38180. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  38181. #endif
  38182. #endif
  38183. return EXPECT_RESULT();
  38184. }
  38185. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  38186. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  38187. * buffer of 64 bytes.
  38188. *
  38189. * returns the size of the digest buffer on success and a negative value on
  38190. * failure.
  38191. */
  38192. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest,
  38193. int* sz)
  38194. {
  38195. EXPECT_DECLS;
  38196. HMAC_CTX ctx1;
  38197. HMAC_CTX ctx2;
  38198. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  38199. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  38200. unsigned char long_key[] =
  38201. "0123456789012345678901234567890123456789"
  38202. "0123456789012345678901234567890123456789"
  38203. "0123456789012345678901234567890123456789"
  38204. "0123456789012345678901234567890123456789";
  38205. unsigned char msg[] = "message to hash";
  38206. unsigned int digestSz = 64;
  38207. int keySz = sizeof(key);
  38208. int long_keySz = sizeof(long_key);
  38209. int msgSz = sizeof(msg);
  38210. unsigned char digest2[64];
  38211. unsigned int digestSz2 = 64;
  38212. HMAC_CTX_init(&ctx1);
  38213. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  38214. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38215. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38216. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38217. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38218. HMAC_CTX_cleanup(&ctx1);
  38219. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38220. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  38221. HMAC_CTX_cleanup(&ctx2);
  38222. ExpectIntEQ(digestSz, digestSz2);
  38223. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38224. /* test HMAC_Init with NULL key */
  38225. /* init after copy */
  38226. HMAC_CTX_init(&ctx1);
  38227. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  38228. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38229. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38230. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  38231. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38232. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38233. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38234. HMAC_CTX_cleanup(&ctx1);
  38235. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  38236. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38237. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38238. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  38239. HMAC_CTX_cleanup(&ctx2);
  38240. ExpectIntEQ(digestSz, digestSz2);
  38241. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38242. /* long key */
  38243. HMAC_CTX_init(&ctx1);
  38244. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type),
  38245. SSL_SUCCESS);
  38246. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38247. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38248. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  38249. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38250. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38251. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38252. HMAC_CTX_cleanup(&ctx1);
  38253. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  38254. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38255. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38256. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  38257. HMAC_CTX_cleanup(&ctx2);
  38258. ExpectIntEQ(digestSz, digestSz2);
  38259. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38260. /* init before copy */
  38261. HMAC_CTX_init(&ctx1);
  38262. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  38263. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38264. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  38265. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38266. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38267. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38268. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38269. HMAC_CTX_cleanup(&ctx1);
  38270. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38271. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38272. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  38273. HMAC_CTX_cleanup(&ctx2);
  38274. ExpectIntEQ(digestSz, digestSz2);
  38275. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38276. *sz = digestSz;
  38277. return EXPECT_RESULT();
  38278. }
  38279. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  38280. static int test_wolfSSL_HMAC_CTX(void)
  38281. {
  38282. EXPECT_DECLS;
  38283. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  38284. unsigned char digest[64];
  38285. int digestSz;
  38286. WOLFSSL_HMAC_CTX* hmac_ctx = NULL;
  38287. WOLFSSL_HMAC_CTX ctx1;
  38288. WOLFSSL_HMAC_CTX ctx2;
  38289. ExpectNotNull(hmac_ctx = wolfSSL_HMAC_CTX_new());
  38290. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(NULL), 1);
  38291. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(hmac_ctx), 1);
  38292. wolfSSL_HMAC_CTX_free(NULL);
  38293. wolfSSL_HMAC_CTX_free(hmac_ctx);
  38294. XMEMSET(&ctx2, 0, sizeof(WOLFSSL_HMAC_CTX));
  38295. ExpectIntEQ(HMAC_CTX_init(NULL), 1);
  38296. ExpectIntEQ(HMAC_CTX_init(&ctx2), 1);
  38297. ExpectIntEQ(HMAC_CTX_copy(NULL, NULL), 0);
  38298. ExpectIntEQ(HMAC_CTX_copy(NULL, &ctx2), 0);
  38299. ExpectIntEQ(HMAC_CTX_copy(&ctx2, NULL), 0);
  38300. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  38301. ((! defined(HAVE_FIPS_VERSION)) || \
  38302. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  38303. /* Copy object that hasn't had a digest set - MD5. */
  38304. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 1);
  38305. #else
  38306. /* Copy object that hasn't had a digest set. */
  38307. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 0);
  38308. #endif
  38309. HMAC_CTX_cleanup(NULL);
  38310. HMAC_CTX_cleanup(&ctx2);
  38311. ExpectNull(HMAC_CTX_get_md(NULL));
  38312. #ifndef NO_SHA
  38313. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha1(), digest, &digestSz)),
  38314. TEST_SUCCESS);
  38315. ExpectIntEQ(digestSz, 20);
  38316. ExpectIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  38317. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  38318. #endif /* !NO_SHA */
  38319. #ifdef WOLFSSL_SHA224
  38320. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha224(), digest, &digestSz)),
  38321. TEST_SUCCESS);
  38322. ExpectIntEQ(digestSz, 28);
  38323. ExpectIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  38324. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  38325. "\x02\x0E", digest, digestSz), 0);
  38326. #endif /* WOLFSSL_SHA224 */
  38327. #ifndef NO_SHA256
  38328. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha256(), digest, &digestSz)),
  38329. TEST_SUCCESS);
  38330. ExpectIntEQ(digestSz, 32);
  38331. ExpectIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  38332. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  38333. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  38334. #endif /* !NO_SHA256 */
  38335. #ifdef WOLFSSL_SHA384
  38336. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha384(), digest, &digestSz)),
  38337. TEST_SUCCESS);
  38338. ExpectIntEQ(digestSz, 48);
  38339. ExpectIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  38340. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  38341. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  38342. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  38343. digest, digestSz), 0);
  38344. #endif /* WOLFSSL_SHA384 */
  38345. #ifdef WOLFSSL_SHA512
  38346. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha512(), digest, &digestSz)),
  38347. TEST_SUCCESS);
  38348. ExpectIntEQ(digestSz, 64);
  38349. ExpectIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  38350. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  38351. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  38352. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  38353. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  38354. digest, digestSz), 0);
  38355. #endif /* WOLFSSL_SHA512 */
  38356. #ifdef WOLFSSL_SHA3
  38357. #ifndef WOLFSSL_NOSHA3_224
  38358. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_224(), digest, &digestSz)),
  38359. TEST_SUCCESS);
  38360. ExpectIntEQ(digestSz, 28);
  38361. ExpectIntEQ(XMEMCMP("\xdc\x53\x25\x3f\xc0\x9d\x2b\x0c\x7f\x59\x11\x17\x08"
  38362. "\x5c\xe8\x43\x31\x01\x5a\xb3\xe3\x08\x37\x71\x26\x0b"
  38363. "\x29\x0f", digest, digestSz), 0);
  38364. #endif
  38365. #ifndef WOLFSSL_NOSHA3_256
  38366. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_256(), digest, &digestSz)),
  38367. TEST_SUCCESS);
  38368. ExpectIntEQ(digestSz, 32);
  38369. ExpectIntEQ(XMEMCMP("\x0f\x00\x89\x82\x15\xce\xd6\x45\x01\x83\xce\xc8\x35"
  38370. "\xab\x71\x07\xc9\xfe\x61\x22\x38\xf9\x09\xad\x35\x65"
  38371. "\x43\x77\x24\xd4\x1e\xf4", digest, digestSz), 0);
  38372. #endif
  38373. #ifndef WOLFSSL_NOSHA3_384
  38374. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_384(), digest, &digestSz)),
  38375. TEST_SUCCESS);
  38376. ExpectIntEQ(digestSz, 48);
  38377. ExpectIntEQ(XMEMCMP("\x0f\x6a\xc0\xfb\xc3\xf2\x80\xb1\xb4\x04\xb6\xc8\x45"
  38378. "\x23\x3b\xb4\xbe\xc6\xea\x85\x07\xca\x8c\x71\xbb\x6e"
  38379. "\x79\xf6\xf9\x2b\x98\xf5\xef\x11\x39\xd4\x5d\xd3\xca"
  38380. "\xc0\xe6\x81\xf7\x73\xf9\x85\x5d\x4f",
  38381. digest, digestSz), 0);
  38382. #endif
  38383. #ifndef WOLFSSL_NOSHA3_512
  38384. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_512(), digest, &digestSz)),
  38385. TEST_SUCCESS);
  38386. ExpectIntEQ(digestSz, 64);
  38387. ExpectIntEQ(XMEMCMP("\x3e\x77\xe3\x59\x42\x89\xed\xc3\xa4\x26\x3d\xa4\x75"
  38388. "\xd2\x84\x8c\xb2\xf3\x25\x04\x47\x61\xce\x1c\x42\x86"
  38389. "\xcd\xf4\x56\xaa\x2f\x84\xb1\x3b\x18\xed\xe6\xd6\x48"
  38390. "\x15\xb0\x29\xc5\x9d\x32\xef\xdd\x3e\x09\xf6\xed\x9e"
  38391. "\x70\xbc\x1c\x63\xf7\x3b\x3e\xe1\xdc\x84\x9c\x1c",
  38392. digest, digestSz), 0);
  38393. #endif
  38394. #endif
  38395. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || \
  38396. HAVE_FIPS_VERSION <= 2)
  38397. ExpectIntEQ((test_HMAC_CTX_helper(EVP_md5(), digest, &digestSz)),
  38398. TEST_SUCCESS);
  38399. ExpectIntEQ(digestSz, 16);
  38400. ExpectIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  38401. "\xE4\x98\xDD", digest, digestSz), 0);
  38402. #endif /* !NO_MD5 */
  38403. #endif
  38404. return EXPECT_RESULT();
  38405. }
  38406. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  38407. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  38408. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  38409. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  38410. {
  38411. EXPECT_DECLS;
  38412. static const unsigned char key[] = "simple test key";
  38413. HMAC_CTX* hmac = NULL;
  38414. ENGINE* e = NULL;
  38415. unsigned char hash[WC_MAX_DIGEST_SIZE];
  38416. unsigned int len;
  38417. ExpectNotNull(hmac = HMAC_CTX_new());
  38418. HMAC_CTX_init(hmac);
  38419. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  38420. ((! defined(HAVE_FIPS_VERSION)) || \
  38421. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  38422. /* Get size on object that hasn't had a digest set - MD5. */
  38423. ExpectIntEQ(HMAC_size(hmac), 16);
  38424. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 1);
  38425. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 1);
  38426. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 1);
  38427. #else
  38428. ExpectIntEQ(HMAC_size(hmac), BAD_FUNC_ARG);
  38429. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 0);
  38430. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 0);
  38431. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 0);
  38432. #endif
  38433. ExpectIntEQ(HMAC_Init_ex(NULL, (void*)key, (int)sizeof(key), md, e), 0);
  38434. ExpectIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e), 1);
  38435. /* re-using test key as data to hash */
  38436. ExpectIntEQ(HMAC_Update(NULL, key, (int)sizeof(key)), 0);
  38437. ExpectIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), 1);
  38438. ExpectIntEQ(HMAC_Update(hmac, key, 0), 1);
  38439. ExpectIntEQ(HMAC_Update(hmac, NULL, 0), 1);
  38440. ExpectIntEQ(HMAC_Update(hmac, NULL, (int)sizeof(key)), 1);
  38441. ExpectIntEQ(HMAC_Final(NULL, NULL, &len), 0);
  38442. ExpectIntEQ(HMAC_Final(hmac, NULL, &len), 0);
  38443. ExpectIntEQ(HMAC_Final(NULL, hash, &len), 0);
  38444. ExpectIntEQ(HMAC_Final(hmac, hash, &len), 1);
  38445. ExpectIntEQ(HMAC_Final(hmac, hash, NULL), 1);
  38446. ExpectIntEQ(len, md_len);
  38447. ExpectIntEQ(HMAC_size(NULL), 0);
  38448. ExpectIntEQ(HMAC_size(hmac), md_len);
  38449. ExpectStrEQ(HMAC_CTX_get_md(hmac), md);
  38450. HMAC_cleanup(NULL);
  38451. HMAC_cleanup(hmac);
  38452. HMAC_CTX_free(hmac);
  38453. len = 0;
  38454. ExpectNull(HMAC(NULL, key, (int)sizeof(key), NULL, 0, hash, &len));
  38455. ExpectNull(HMAC(md, NULL, (int)sizeof(key), NULL, 0, hash, &len));
  38456. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, 0, NULL, &len));
  38457. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  38458. ExpectIntEQ(len, md_len);
  38459. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, NULL));
  38460. /* With data. */
  38461. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, (int)sizeof(key), hash,
  38462. &len));
  38463. /* With NULL data. */
  38464. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, (int)sizeof(key), hash,
  38465. &len));
  38466. /* With zero length data. */
  38467. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, 0, hash, &len));
  38468. return EXPECT_RESULT();
  38469. }
  38470. #endif
  38471. static int test_wolfSSL_HMAC(void)
  38472. {
  38473. EXPECT_DECLS;
  38474. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  38475. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  38476. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  38477. #ifndef NO_SHA256
  38478. ExpectIntEQ(test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE),
  38479. TEST_SUCCESS);
  38480. #endif
  38481. #ifdef WOLFSSL_SHA224
  38482. ExpectIntEQ(test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE),
  38483. TEST_SUCCESS);
  38484. #endif
  38485. #ifdef WOLFSSL_SHA384
  38486. ExpectIntEQ(test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE),
  38487. TEST_SUCCESS);
  38488. #endif
  38489. #ifdef WOLFSSL_SHA512
  38490. ExpectIntEQ(test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE),
  38491. TEST_SUCCESS);
  38492. #endif
  38493. #ifdef WOLFSSL_SHA3
  38494. #ifndef WOLFSSL_NOSHA3_224
  38495. ExpectIntEQ(test_openssl_hmac(EVP_sha3_224(),
  38496. (int)WC_SHA3_224_DIGEST_SIZE), TEST_SUCCESS);
  38497. #endif
  38498. #ifndef WOLFSSL_NOSHA3_256
  38499. ExpectIntEQ(test_openssl_hmac(EVP_sha3_256(),
  38500. (int)WC_SHA3_256_DIGEST_SIZE), TEST_SUCCESS);
  38501. #endif
  38502. #ifndef WOLFSSL_NOSHA3_384
  38503. ExpectIntEQ(test_openssl_hmac(EVP_sha3_384(),
  38504. (int)WC_SHA3_384_DIGEST_SIZE), TEST_SUCCESS);
  38505. #endif
  38506. #ifndef WOLFSSL_NOSHA3_512
  38507. ExpectIntEQ(test_openssl_hmac(EVP_sha3_512(),
  38508. (int)WC_SHA3_512_DIGEST_SIZE), TEST_SUCCESS);
  38509. #endif
  38510. #endif
  38511. #ifndef NO_SHA
  38512. ExpectIntEQ(test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE),
  38513. TEST_SUCCESS);
  38514. #endif
  38515. #endif
  38516. return EXPECT_RESULT();
  38517. }
  38518. static int test_wolfSSL_CMAC(void)
  38519. {
  38520. EXPECT_DECLS;
  38521. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  38522. defined(WOLFSSL_AES_DIRECT)
  38523. int i;
  38524. byte key[AES_256_KEY_SIZE];
  38525. CMAC_CTX* cmacCtx = NULL;
  38526. byte out[AES_BLOCK_SIZE];
  38527. size_t outLen = AES_BLOCK_SIZE;
  38528. for (i=0; i < AES_256_KEY_SIZE; ++i) {
  38529. key[i] = i;
  38530. }
  38531. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38532. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  38533. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  38534. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  38535. NULL), 1);
  38536. /* re-using test key as data to hash */
  38537. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  38538. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  38539. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 1);
  38540. ExpectIntEQ(outLen, AES_BLOCK_SIZE);
  38541. /* No Update works. */
  38542. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  38543. NULL), 1);
  38544. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  38545. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  38546. NULL), 1);
  38547. /* Test parameters with CMAC_Update. */
  38548. ExpectIntEQ(CMAC_Update(NULL, NULL, 0), 0);
  38549. ExpectIntEQ(CMAC_Update(NULL, key, 0), 0);
  38550. ExpectIntEQ(CMAC_Update(NULL, NULL, AES_128_KEY_SIZE), 0);
  38551. ExpectIntEQ(CMAC_Update(NULL, key, AES_128_KEY_SIZE), 0);
  38552. ExpectIntEQ(CMAC_Update(cmacCtx, key, 0), 1);
  38553. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  38554. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, AES_128_KEY_SIZE), 1);
  38555. /* Test parameters with CMAC_Final. */
  38556. ExpectIntEQ(CMAC_Final(NULL, NULL, NULL), 0);
  38557. ExpectIntEQ(CMAC_Final(NULL, out, NULL), 0);
  38558. ExpectIntEQ(CMAC_Final(NULL, NULL, &outLen), 0);
  38559. ExpectIntEQ(CMAC_Final(NULL, out, &outLen), 0);
  38560. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, NULL), 1);
  38561. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, &outLen), 1);
  38562. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  38563. CMAC_CTX_free(cmacCtx);
  38564. /* Test parameters with CMAC Init. */
  38565. cmacCtx = NULL;
  38566. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38567. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  38568. ExpectIntEQ(CMAC_Init(NULL, NULL, 0, NULL, NULL), 0);
  38569. ExpectIntEQ(CMAC_Init(NULL, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  38570. NULL), 0);
  38571. ExpectIntEQ(CMAC_Init(cmacCtx, NULL, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  38572. NULL), 0);
  38573. /* give a key too small for the cipher, verify we get failure */
  38574. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_192_cbc(),
  38575. NULL), 0);
  38576. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, NULL, NULL), 0);
  38577. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128)
  38578. /* Only AES-CBC supported. */
  38579. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_gcm(),
  38580. NULL), 0);
  38581. #endif
  38582. CMAC_CTX_free(cmacCtx);
  38583. ExpectNull(CMAC_CTX_get0_cipher_ctx(NULL));
  38584. cmacCtx = NULL;
  38585. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38586. /* No Init. */
  38587. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 0);
  38588. CMAC_CTX_free(cmacCtx);
  38589. /* Test AES-256-CBC */
  38590. cmacCtx = NULL;
  38591. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38592. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_256_KEY_SIZE, EVP_aes_256_cbc(),
  38593. NULL), 1);
  38594. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  38595. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  38596. CMAC_CTX_free(cmacCtx);
  38597. /* Test AES-192-CBC */
  38598. cmacCtx = NULL;
  38599. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38600. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  38601. NULL), 1);
  38602. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  38603. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  38604. CMAC_CTX_free(cmacCtx);
  38605. cmacCtx = NULL;
  38606. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38607. CMAC_CTX_free(cmacCtx);
  38608. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  38609. return EXPECT_RESULT();
  38610. }
  38611. static int test_wolfSSL_DES(void)
  38612. {
  38613. EXPECT_DECLS;
  38614. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  38615. const_DES_cblock myDes;
  38616. DES_cblock iv;
  38617. DES_key_schedule key;
  38618. word32 i = 0;
  38619. DES_LONG dl = 0;
  38620. unsigned char msg[] = "hello wolfssl";
  38621. unsigned char weakKey[][8] = {
  38622. { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
  38623. { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
  38624. { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
  38625. { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E }
  38626. };
  38627. unsigned char semiWeakKey[][8] = {
  38628. { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
  38629. { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
  38630. { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
  38631. { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
  38632. { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
  38633. { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
  38634. { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
  38635. { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
  38636. { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
  38637. { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
  38638. { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
  38639. { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
  38640. };
  38641. DES_check_key(1);
  38642. DES_set_key(&myDes, &key);
  38643. /* check, check of odd parity */
  38644. XMEMSET(myDes, 4, sizeof(const_DES_cblock));
  38645. myDes[0] = 6; /* set even parity */
  38646. XMEMSET(key, 5, sizeof(DES_key_schedule));
  38647. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -1);
  38648. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  38649. ExpectIntEQ(DES_set_key_checked(NULL, NULL), -2);
  38650. ExpectIntEQ(DES_set_key_checked(&myDes, NULL), -2);
  38651. ExpectIntEQ(DES_set_key_checked(NULL, &key), -2);
  38652. /* set odd parity for success case */
  38653. DES_set_odd_parity(&myDes);
  38654. ExpectIntEQ(DES_check_key_parity(&myDes), 1);
  38655. fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
  38656. myDes[3]);
  38657. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  38658. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  38659. ExpectIntEQ(key[i], myDes[i]);
  38660. }
  38661. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  38662. /* check weak key */
  38663. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  38664. XMEMSET(key, 5, sizeof(DES_key_schedule));
  38665. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -2);
  38666. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  38667. DES_set_key_unchecked(NULL, NULL);
  38668. DES_set_key_unchecked(&myDes, NULL);
  38669. DES_set_key_unchecked(NULL, &key);
  38670. /* compare arrays, should be the same */
  38671. /* now do unchecked copy of a weak key over */
  38672. DES_set_key_unchecked(&myDes, &key);
  38673. /* compare arrays, should be the same */
  38674. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  38675. ExpectIntEQ(key[i], myDes[i]);
  38676. }
  38677. ExpectIntEQ(DES_is_weak_key(&myDes), 1);
  38678. myDes[7] = 2;
  38679. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  38680. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  38681. ExpectIntEQ(DES_is_weak_key(NULL), 1);
  38682. /* Test all weak keys. */
  38683. for (i = 0; i < sizeof(weakKey) / sizeof(*weakKey); i++) {
  38684. ExpectIntEQ(DES_set_key_checked(&weakKey[i], &key), -2);
  38685. }
  38686. /* Test all semi-weak keys. */
  38687. for (i = 0; i < sizeof(semiWeakKey) / sizeof(*semiWeakKey); i++) {
  38688. ExpectIntEQ(DES_set_key_checked(&semiWeakKey[i], &key), -2);
  38689. }
  38690. /* check DES_key_sched API */
  38691. XMEMSET(key, 1, sizeof(DES_key_schedule));
  38692. ExpectIntEQ(DES_key_sched(&myDes, NULL), 0);
  38693. ExpectIntEQ(DES_key_sched(NULL, &key), 0);
  38694. ExpectIntEQ(DES_key_sched(&myDes, &key), 0);
  38695. /* compare arrays, should be the same */
  38696. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  38697. ExpectIntEQ(key[i], myDes[i]);
  38698. }
  38699. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, NULL)), 0);
  38700. ExpectIntEQ((DES_cbc_cksum(msg, NULL, 0, NULL, NULL)), 0);
  38701. ExpectIntEQ((DES_cbc_cksum(NULL, &key, 0, NULL, NULL)), 0);
  38702. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, &myDes, NULL)), 0);
  38703. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, &iv)), 0);
  38704. ExpectIntEQ((DES_cbc_cksum(NULL, &key, sizeof(msg), &myDes, &iv)), 0);
  38705. ExpectIntEQ((DES_cbc_cksum(msg, NULL, sizeof(msg), &myDes, &iv)), 0);
  38706. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), NULL, &iv)), 0);
  38707. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, NULL)), 0);
  38708. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  38709. * DES_cbc_encrypt on the input */
  38710. XMEMSET(iv, 0, sizeof(DES_cblock));
  38711. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  38712. ExpectIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  38713. ExpectIntEQ(dl, 480052723);
  38714. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  38715. return EXPECT_RESULT();
  38716. }
  38717. static int test_wolfSSL_DES_ncbc(void)
  38718. {
  38719. EXPECT_DECLS;
  38720. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  38721. const_DES_cblock myDes;
  38722. DES_cblock iv = {1};
  38723. DES_key_schedule key = {0};
  38724. unsigned char msg[] = "hello wolfssl";
  38725. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  38726. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  38727. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  38728. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  38729. /* partial block test */
  38730. DES_set_key(&key, &myDes);
  38731. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  38732. ExpectIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  38733. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  38734. DES_set_key(&key, &myDes);
  38735. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38736. *((byte*)&iv) = 1;
  38737. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  38738. ExpectIntEQ(XMEMCMP(msg, pln, 3), 0);
  38739. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  38740. /* full block test */
  38741. DES_set_key(&key, &myDes);
  38742. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  38743. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38744. *((byte*)&iv) = 1;
  38745. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  38746. ExpectIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  38747. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  38748. DES_set_key(&key, &myDes);
  38749. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38750. *((byte*)&iv) = 1;
  38751. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  38752. ExpectIntEQ(XMEMCMP(msg, pln, 8), 0);
  38753. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  38754. #endif
  38755. return EXPECT_RESULT();
  38756. }
  38757. static int test_wolfSSL_DES_ecb_encrypt(void)
  38758. {
  38759. EXPECT_DECLS;
  38760. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  38761. WOLFSSL_DES_cblock input1, input2, output1, output2, back1, back2;
  38762. WOLFSSL_DES_key_schedule key;
  38763. XMEMCPY(key, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  38764. XMEMCPY(input1, "Iamhuman", sizeof(WOLFSSL_DES_cblock));
  38765. XMEMCPY(input2, "Whoisit?", sizeof(WOLFSSL_DES_cblock));
  38766. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  38767. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  38768. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  38769. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  38770. wolfSSL_DES_ecb_encrypt(NULL, NULL, NULL, DES_ENCRYPT);
  38771. wolfSSL_DES_ecb_encrypt(&input1, NULL, NULL, DES_ENCRYPT);
  38772. wolfSSL_DES_ecb_encrypt(NULL, &output1, NULL, DES_ENCRYPT);
  38773. wolfSSL_DES_ecb_encrypt(NULL, NULL, &key, DES_ENCRYPT);
  38774. wolfSSL_DES_ecb_encrypt(&input1, &output1, NULL, DES_ENCRYPT);
  38775. wolfSSL_DES_ecb_encrypt(&input1, NULL, &key, DES_ENCRYPT);
  38776. wolfSSL_DES_ecb_encrypt(NULL, &output1, &key, DES_ENCRYPT);
  38777. /* Encrypt messages */
  38778. wolfSSL_DES_ecb_encrypt(&input1, &output1, &key, DES_ENCRYPT);
  38779. wolfSSL_DES_ecb_encrypt(&input2, &output2, &key, DES_ENCRYPT);
  38780. {
  38781. /* Decrypt messages */
  38782. int ret1 = 0;
  38783. int ret2 = 0;
  38784. wolfSSL_DES_ecb_encrypt(&output1, &back1, &key, DES_DECRYPT);
  38785. ExpectIntEQ(ret1 = XMEMCMP((unsigned char *)back1,
  38786. (unsigned char *)input1, sizeof(WOLFSSL_DES_cblock)), 0);
  38787. wolfSSL_DES_ecb_encrypt(&output2, &back2, &key, DES_DECRYPT);
  38788. ExpectIntEQ(ret2 = XMEMCMP((unsigned char *)back2,
  38789. (unsigned char *)input2, sizeof(WOLFSSL_DES_cblock)), 0);
  38790. }
  38791. #endif
  38792. return EXPECT_RESULT();
  38793. }
  38794. static int test_wolfSSL_DES_ede3_cbc_encrypt(void)
  38795. {
  38796. EXPECT_DECLS;
  38797. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  38798. unsigned char input1[8], input2[8];
  38799. unsigned char output1[8], output2[8];
  38800. unsigned char back1[8], back2[8];
  38801. WOLFSSL_DES_cblock iv1, iv2;
  38802. WOLFSSL_DES_key_schedule key1, key2, key3;
  38803. int i;
  38804. XMEMCPY(key1, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  38805. XMEMCPY(key2, "23456781", sizeof(WOLFSSL_DES_key_schedule));
  38806. XMEMCPY(key3, "34567823", sizeof(WOLFSSL_DES_key_schedule));
  38807. XMEMCPY(input1, "Iamhuman", sizeof(input1));
  38808. XMEMCPY(input2, "Whoisit?", sizeof(input2));
  38809. XMEMSET(output1, 0, sizeof(output1));
  38810. XMEMSET(output2, 0, sizeof(output2));
  38811. XMEMSET(back1, 0, sizeof(back1));
  38812. XMEMSET(back2, 0, sizeof(back2));
  38813. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  38814. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  38815. /* Encrypt messages */
  38816. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, 8, &key1, &key2, &key3, &iv1,
  38817. DES_ENCRYPT);
  38818. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, 8, &key1, &key2, &key3, &iv2,
  38819. DES_ENCRYPT);
  38820. {
  38821. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  38822. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  38823. /* Decrypt messages */
  38824. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, 8, &key1, &key2, &key3,
  38825. &iv1, DES_DECRYPT);
  38826. ExpectIntEQ(XMEMCMP(back1, input1, sizeof(input1)), 0);
  38827. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, 8, &key1, &key2, &key3,
  38828. &iv2, DES_DECRYPT);
  38829. ExpectIntEQ(XMEMCMP(back2, input2, sizeof(input2)), 0);
  38830. }
  38831. for (i = 0; i < 8; i++) {
  38832. XMEMSET(output1, 0, sizeof(output1));
  38833. XMEMSET(output2, 0, sizeof(output2));
  38834. XMEMSET(back1, 0, sizeof(back1));
  38835. XMEMSET(back2, 0, sizeof(back2));
  38836. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  38837. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  38838. /* Encrypt partial messages */
  38839. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, i, &key1, &key2, &key3,
  38840. &iv1, DES_ENCRYPT);
  38841. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, i, &key1, &key2, &key3,
  38842. &iv2, DES_ENCRYPT);
  38843. {
  38844. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  38845. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  38846. /* Decrypt messages */
  38847. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, i, &key1, &key2,
  38848. &key3, &iv1, DES_DECRYPT);
  38849. ExpectIntEQ(XMEMCMP(back1, input1, i), 0);
  38850. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, i, &key1, &key2,
  38851. &key3, &iv2, DES_DECRYPT);
  38852. ExpectIntEQ(XMEMCMP(back2, input2, i), 0);
  38853. }
  38854. }
  38855. #endif
  38856. return EXPECT_RESULT();
  38857. }
  38858. static int test_wolfSSL_AES_encrypt(void)
  38859. {
  38860. EXPECT_DECLS;
  38861. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  38862. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  38863. AES_KEY enc;
  38864. AES_KEY dec;
  38865. const byte msg[] = {
  38866. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  38867. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  38868. };
  38869. const byte exp[] = {
  38870. 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c,
  38871. 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8,
  38872. };
  38873. const byte key[] = {
  38874. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  38875. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  38876. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  38877. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  38878. };
  38879. byte eout[sizeof(msg)];
  38880. byte dout[sizeof(msg)];
  38881. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &enc), 0);
  38882. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &dec), 0);
  38883. wolfSSL_AES_encrypt(NULL, NULL, NULL);
  38884. wolfSSL_AES_encrypt(msg, NULL, NULL);
  38885. wolfSSL_AES_encrypt(NULL, eout, NULL);
  38886. wolfSSL_AES_encrypt(NULL, NULL, &enc);
  38887. wolfSSL_AES_encrypt(msg, eout, NULL);
  38888. wolfSSL_AES_encrypt(msg, NULL, &enc);
  38889. wolfSSL_AES_encrypt(NULL, eout, &enc);
  38890. wolfSSL_AES_decrypt(NULL, NULL, NULL);
  38891. wolfSSL_AES_decrypt(eout, NULL, NULL);
  38892. wolfSSL_AES_decrypt(NULL, dout, NULL);
  38893. wolfSSL_AES_decrypt(NULL, NULL, &dec);
  38894. wolfSSL_AES_decrypt(eout, dout, NULL);
  38895. wolfSSL_AES_decrypt(eout, NULL, &dec);
  38896. wolfSSL_AES_decrypt(NULL, dout, &dec);
  38897. wolfSSL_AES_encrypt(msg, eout, &enc);
  38898. ExpectIntEQ(XMEMCMP(eout, exp, AES_BLOCK_SIZE), 0);
  38899. wolfSSL_AES_decrypt(eout, dout, &dec);
  38900. ExpectIntEQ(XMEMCMP(dout, msg, AES_BLOCK_SIZE), 0);
  38901. #endif
  38902. return EXPECT_RESULT();
  38903. }
  38904. static int test_wolfSSL_AES_ecb_encrypt(void)
  38905. {
  38906. EXPECT_DECLS;
  38907. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  38908. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  38909. AES_KEY aes;
  38910. const byte msg[] =
  38911. {
  38912. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38913. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  38914. };
  38915. const byte verify[] =
  38916. {
  38917. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  38918. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  38919. };
  38920. const byte key[] =
  38921. {
  38922. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  38923. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  38924. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  38925. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  38926. };
  38927. byte out[AES_BLOCK_SIZE];
  38928. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  38929. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38930. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  38931. ExpectIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  38932. #ifdef HAVE_AES_DECRYPT
  38933. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  38934. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38935. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  38936. ExpectIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  38937. #endif
  38938. /* test bad arguments */
  38939. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  38940. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  38941. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  38942. #endif
  38943. return EXPECT_RESULT();
  38944. }
  38945. static int test_wolfSSL_AES_cbc_encrypt(void)
  38946. {
  38947. EXPECT_DECLS;
  38948. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  38949. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  38950. AES_KEY aes;
  38951. AES_KEY* aesN = NULL;
  38952. size_t len = 0;
  38953. size_t lenB = 0;
  38954. int keySz0 = 0;
  38955. int keySzN = -1;
  38956. byte out[AES_BLOCK_SIZE] = {0};
  38957. byte* outN = NULL;
  38958. /* Test vectors retrieved from:
  38959. * <begin URL>
  38960. * https://csrc.nist.gov/
  38961. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  38962. * documents/aes/KAT_AES.zip
  38963. * </end URL>
  38964. */
  38965. const byte* pt128N = NULL;
  38966. byte* key128N = NULL;
  38967. byte* iv128N = NULL;
  38968. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  38969. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38970. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  38971. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  38972. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  38973. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38974. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  38975. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  38976. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  38977. len = sizeof(pt128);
  38978. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  38979. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  38980. ExpectIntNE(XMEMCMP(b, g, h), i)
  38981. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  38982. /* Stressing wolfSSL_AES_cbc_encrypt() */
  38983. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  38984. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  38985. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  38986. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38987. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  38988. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38989. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  38990. /* Stressing wolfSSL_AES_set_encrypt_key */
  38991. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  38992. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  38993. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  38994. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  38995. /* Stressing wolfSSL_AES_set_decrypt_key */
  38996. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  38997. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  38998. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  38999. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  39000. #ifdef WOLFSSL_AES_128
  39001. /* wolfSSL_AES_cbc_encrypt() 128-bit */
  39002. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39003. RESET_IV(iv128tmp, iv128);
  39004. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  39005. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  39006. ExpectIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  39007. wc_AesFree((Aes*)&aes);
  39008. #ifdef HAVE_AES_DECRYPT
  39009. /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
  39010. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39011. RESET_IV(iv128tmp, iv128);
  39012. len = sizeof(ct128);
  39013. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  39014. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  39015. ExpectIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  39016. wc_AesFree((Aes*)&aes);
  39017. #endif
  39018. #endif /* WOLFSSL_AES_128 */
  39019. #ifdef WOLFSSL_AES_192
  39020. {
  39021. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  39022. * Appendix F.2.3 */
  39023. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  39024. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  39025. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  39026. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  39027. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  39028. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  39029. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  39030. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  39031. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  39032. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  39033. len = sizeof(pt192);
  39034. /* wolfSSL_AES_cbc_encrypt() 192-bit */
  39035. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39036. RESET_IV(iv192tmp, iv192);
  39037. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  39038. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  39039. ExpectIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  39040. wc_AesFree((Aes*)&aes);
  39041. #ifdef HAVE_AES_DECRYPT
  39042. /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
  39043. len = sizeof(ct192);
  39044. RESET_IV(iv192tmp, iv192);
  39045. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39046. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  39047. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  39048. ExpectIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  39049. wc_AesFree((Aes*)&aes);
  39050. #endif
  39051. }
  39052. #endif /* WOLFSSL_AES_192 */
  39053. #ifdef WOLFSSL_AES_256
  39054. {
  39055. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  39056. * Appendix F.2.5 */
  39057. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  39058. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  39059. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  39060. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  39061. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  39062. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  39063. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  39064. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  39065. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  39066. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  39067. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  39068. len = sizeof(pt256);
  39069. /* wolfSSL_AES_cbc_encrypt() 256-bit */
  39070. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39071. RESET_IV(iv256tmp, iv256);
  39072. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39073. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  39074. ExpectIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  39075. wc_AesFree((Aes*)&aes);
  39076. #ifdef HAVE_AES_DECRYPT
  39077. /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
  39078. len = sizeof(ct256);
  39079. RESET_IV(iv256tmp, iv256);
  39080. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39081. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39082. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  39083. ExpectIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  39084. wc_AesFree((Aes*)&aes);
  39085. #endif
  39086. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && \
  39087. !defined(HAVE_SELFTEST)
  39088. {
  39089. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  39090. byte wrapPlain[sizeof(key256)] = { 0 };
  39091. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  39092. /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
  39093. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39094. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  39095. 15), WOLFSSL_FAILURE);
  39096. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  39097. sizeof(key256)), sizeof(wrapCipher));
  39098. wc_AesFree((Aes*)&aes);
  39099. /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
  39100. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39101. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  39102. 23), WOLFSSL_FAILURE);
  39103. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  39104. sizeof(wrapCipher)), sizeof(wrapPlain));
  39105. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  39106. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  39107. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  39108. wc_AesFree((Aes*)&aes);
  39109. /* wolfSSL_AES_wrap_key() 256-bit custom iv */
  39110. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39111. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  39112. sizeof(key256)), sizeof(wrapCipher));
  39113. wc_AesFree((Aes*)&aes);
  39114. /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
  39115. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39116. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  39117. sizeof(wrapCipher)), sizeof(wrapPlain));
  39118. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  39119. wc_AesFree((Aes*)&aes);
  39120. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, NULL, 0), 0);
  39121. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, NULL, NULL, 0), 0);
  39122. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  39123. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, wrapCipher, NULL, 0), 0);
  39124. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, key256, 0), 0);
  39125. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, wrapCipher, key256, 0), 0);
  39126. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256, 0), 0);
  39127. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, NULL, key256, 0), 0);
  39128. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, NULL, 0), 0);
  39129. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, NULL, 0), 0);
  39130. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, NULL, NULL, 0), 0);
  39131. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  39132. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, wrapPlain, NULL, 0), 0);
  39133. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, wrapCipher, 0), 0);
  39134. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, wrapPlain, wrapCipher, 0),
  39135. 0);
  39136. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher, 0),
  39137. 0);
  39138. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, NULL, wrapCipher, 0), 0);
  39139. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapPlain, NULL, 0), 0);
  39140. }
  39141. #endif /* HAVE_AES_KEYWRAP */
  39142. }
  39143. #endif /* WOLFSSL_AES_256 */
  39144. #endif
  39145. return EXPECT_RESULT();
  39146. }
  39147. static int test_wolfSSL_AES_cfb128_encrypt(void)
  39148. {
  39149. EXPECT_DECLS;
  39150. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(WOLFSSL_AES_CFB) && \
  39151. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  39152. AES_KEY aesEnc;
  39153. AES_KEY aesDec;
  39154. const byte msg[] = {
  39155. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  39156. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  39157. };
  39158. const byte exp[] = {
  39159. 0x16, 0xc9, 0x90, 0x6c, 0x04, 0x0c, 0xd1, 0x2f,
  39160. 0x84, 0x7b, 0x18, 0xed, 0xed, 0x6a, 0xb5, 0xfd
  39161. };
  39162. const byte key[] = {
  39163. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  39164. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  39165. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  39166. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  39167. };
  39168. const byte ivData[] = {
  39169. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  39170. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  39171. };
  39172. byte out[AES_BLOCK_SIZE];
  39173. byte iv[AES_BLOCK_SIZE];
  39174. word32 i;
  39175. int num;
  39176. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  39177. XMEMCPY(iv, ivData, sizeof(iv));
  39178. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39179. AES_cfb128_encrypt(msg, out, sizeof(msg), &aesEnc, iv, NULL, AES_ENCRYPT);
  39180. ExpectIntEQ(XMEMCMP(out, exp, sizeof(msg)), 0);
  39181. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39182. #ifdef HAVE_AES_DECRYPT
  39183. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  39184. XMEMCPY(iv, ivData, sizeof(iv));
  39185. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39186. AES_cfb128_encrypt(exp, out, sizeof(msg), &aesDec, iv, NULL, AES_DECRYPT);
  39187. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  39188. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39189. #endif
  39190. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(msg)); i++) {
  39191. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  39192. XMEMCPY(iv, ivData, sizeof(iv));
  39193. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39194. AES_cfb128_encrypt(msg, out, i, &aesEnc, iv, &num, AES_ENCRYPT);
  39195. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  39196. ExpectIntEQ(XMEMCMP(out, exp, i), 0);
  39197. if (i == 0) {
  39198. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39199. }
  39200. else {
  39201. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39202. }
  39203. #ifdef HAVE_AES_DECRYPT
  39204. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  39205. XMEMCPY(iv, ivData, sizeof(iv));
  39206. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39207. AES_cfb128_encrypt(exp, out, i, &aesDec, iv, &num, AES_DECRYPT);
  39208. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  39209. ExpectIntEQ(XMEMCMP(out, msg, i), 0);
  39210. if (i == 0) {
  39211. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39212. }
  39213. else {
  39214. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39215. }
  39216. #endif
  39217. }
  39218. if (EXPECT_SUCCESS()) {
  39219. /* test bad arguments */
  39220. AES_cfb128_encrypt(NULL, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  39221. AES_cfb128_encrypt(msg, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  39222. AES_cfb128_encrypt(NULL, out, 0, NULL, NULL, NULL, AES_DECRYPT);
  39223. AES_cfb128_encrypt(NULL, NULL, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  39224. AES_cfb128_encrypt(NULL, NULL, 0, NULL, iv, NULL, AES_DECRYPT);
  39225. AES_cfb128_encrypt(NULL, out, 0, &aesDec, iv, NULL, AES_DECRYPT);
  39226. AES_cfb128_encrypt(msg, NULL, 0, &aesDec, iv, NULL, AES_DECRYPT);
  39227. AES_cfb128_encrypt(msg, out, 0, NULL, iv, NULL, AES_DECRYPT);
  39228. AES_cfb128_encrypt(msg, out, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  39229. }
  39230. #endif
  39231. return EXPECT_RESULT();
  39232. }
  39233. static int test_wolfSSL_CRYPTO_cts128(void)
  39234. {
  39235. EXPECT_DECLS;
  39236. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  39237. defined(HAVE_CTS) && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  39238. byte tmp[64]; /* Largest vector size */
  39239. /* Test vectors taken form RFC3962 Appendix B */
  39240. const testVector vects[] = {
  39241. {
  39242. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39243. "\x20",
  39244. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  39245. "\x97",
  39246. 17, 17
  39247. },
  39248. {
  39249. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39250. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  39251. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  39252. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  39253. 31, 31
  39254. },
  39255. {
  39256. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39257. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  39258. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  39259. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  39260. 32, 32
  39261. },
  39262. {
  39263. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39264. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  39265. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  39266. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  39267. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  39268. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  39269. 47, 47
  39270. },
  39271. {
  39272. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39273. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  39274. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  39275. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  39276. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  39277. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  39278. 48, 48
  39279. },
  39280. {
  39281. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39282. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  39283. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  39284. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  39285. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  39286. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  39287. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  39288. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  39289. 64, 64
  39290. }
  39291. };
  39292. byte keyBytes[AES_128_KEY_SIZE] = {
  39293. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  39294. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  39295. };
  39296. size_t i;
  39297. AES_KEY encKey;
  39298. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  39299. XMEMSET(tmp, 0, sizeof(tmp));
  39300. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  39301. AES_KEY decKey;
  39302. ExpectIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  39303. &encKey), 0);
  39304. ExpectIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  39305. &decKey), 0);
  39306. XMEMSET(iv, 0, sizeof(iv));
  39307. ExpectIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  39308. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  39309. vects[i].outLen);
  39310. ExpectIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  39311. XMEMSET(iv, 0, sizeof(iv));
  39312. ExpectIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  39313. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  39314. vects[i].inLen);
  39315. ExpectIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  39316. }
  39317. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  39318. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  39319. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  39320. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  39321. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  39322. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL,
  39323. (cbc128_f)AES_cbc_encrypt), 0);
  39324. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, &encKey, iv,
  39325. (cbc128_f)AES_cbc_encrypt), 0);
  39326. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, &encKey, iv,
  39327. (cbc128_f)AES_cbc_encrypt), 0);
  39328. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, NULL, iv,
  39329. (cbc128_f)AES_cbc_encrypt), 0);
  39330. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, NULL,
  39331. (cbc128_f)AES_cbc_encrypt), 0);
  39332. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  39333. /* Length too small. */
  39334. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 0, &encKey, iv,
  39335. (cbc128_f)AES_cbc_encrypt), 0);
  39336. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  39337. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  39338. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  39339. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  39340. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  39341. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL,
  39342. (cbc128_f)AES_cbc_encrypt), 0);
  39343. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, &encKey, iv,
  39344. (cbc128_f)AES_cbc_encrypt), 0);
  39345. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, &encKey, iv,
  39346. (cbc128_f)AES_cbc_encrypt), 0);
  39347. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, NULL, iv,
  39348. (cbc128_f)AES_cbc_encrypt), 0);
  39349. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, NULL,
  39350. (cbc128_f)AES_cbc_encrypt), 0);
  39351. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  39352. /* Length too small. */
  39353. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 0, &encKey, iv,
  39354. (cbc128_f)AES_cbc_encrypt), 0);
  39355. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  39356. return EXPECT_RESULT();
  39357. }
  39358. static int test_wolfSSL_RC4(void)
  39359. {
  39360. EXPECT_DECLS;
  39361. #if !defined(NO_RC4) && defined(OPENSSL_EXTRA)
  39362. WOLFSSL_RC4_KEY rc4Key;
  39363. unsigned char key[] = {
  39364. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  39365. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  39366. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  39367. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  39368. };
  39369. unsigned char data[] = {
  39370. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  39371. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  39372. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  39373. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  39374. };
  39375. unsigned char enc[sizeof(data)];
  39376. unsigned char dec[sizeof(data)];
  39377. word32 i;
  39378. word32 j;
  39379. wolfSSL_RC4_set_key(NULL, -1, NULL);
  39380. wolfSSL_RC4_set_key(&rc4Key, -1, NULL);
  39381. wolfSSL_RC4_set_key(NULL, 0, NULL);
  39382. wolfSSL_RC4_set_key(NULL, -1, key);
  39383. wolfSSL_RC4_set_key(&rc4Key, 0, NULL);
  39384. wolfSSL_RC4_set_key(&rc4Key, -1, key);
  39385. wolfSSL_RC4_set_key(NULL, 0, key);
  39386. wolfSSL_RC4(NULL, 0, NULL, NULL);
  39387. wolfSSL_RC4(&rc4Key, 0, NULL, NULL);
  39388. wolfSSL_RC4(NULL, 0, data, NULL);
  39389. wolfSSL_RC4(NULL, 0, NULL, enc);
  39390. wolfSSL_RC4(&rc4Key, 0, data, NULL);
  39391. wolfSSL_RC4(&rc4Key, 0, NULL, enc);
  39392. wolfSSL_RC4(NULL, 0, data, enc);
  39393. ExpectIntEQ(1, 1);
  39394. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(key)); i++) {
  39395. for (j = 0; EXPECT_SUCCESS() && (j <= sizeof(data)); j++) {
  39396. XMEMSET(enc, 0, sizeof(enc));
  39397. XMEMSET(dec, 0, sizeof(dec));
  39398. /* Encrypt */
  39399. wolfSSL_RC4_set_key(&rc4Key, i, key);
  39400. wolfSSL_RC4(&rc4Key, j, data, enc);
  39401. /* Decrypt */
  39402. wolfSSL_RC4_set_key(&rc4Key, i, key);
  39403. wolfSSL_RC4(&rc4Key, j, enc, dec);
  39404. ExpectIntEQ(XMEMCMP(dec, data, j), 0);
  39405. }
  39406. }
  39407. #endif
  39408. return EXPECT_RESULT();
  39409. }
  39410. static int test_wolfSSL_OBJ(void)
  39411. {
  39412. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  39413. * mode
  39414. */
  39415. EXPECT_DECLS;
  39416. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  39417. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  39418. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && \
  39419. !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  39420. ASN1_OBJECT *obj = NULL;
  39421. ASN1_OBJECT *obj2 = NULL;
  39422. char buf[50];
  39423. XFILE fp = XBADFILE;
  39424. X509 *x509 = NULL;
  39425. X509_NAME *x509Name = NULL;
  39426. X509_NAME_ENTRY *x509NameEntry = NULL;
  39427. ASN1_OBJECT *asn1Name = NULL;
  39428. int numNames = 0;
  39429. BIO *bio = NULL;
  39430. int nid;
  39431. int i, j;
  39432. const char *f[] = {
  39433. #ifndef NO_RSA
  39434. "./certs/ca-cert.der",
  39435. #endif
  39436. #ifdef HAVE_ECC
  39437. "./certs/ca-ecc-cert.der",
  39438. "./certs/ca-ecc384-cert.der",
  39439. #endif
  39440. NULL};
  39441. ASN1_OBJECT *field_name_obj = NULL;
  39442. int lastpos = -1;
  39443. int tmp = -1;
  39444. ASN1_STRING *asn1 = NULL;
  39445. unsigned char *buf_dyn = NULL;
  39446. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  39447. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  39448. ExpectIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  39449. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  39450. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  39451. ASN1_OBJECT_free(obj);
  39452. obj = NULL;
  39453. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  39454. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256);
  39455. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  39456. #ifdef WOLFSSL_CERT_EXT
  39457. ExpectIntEQ(OBJ_txt2nid(buf), NID_sha256);
  39458. #endif
  39459. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  39460. ExpectNotNull(obj2 = OBJ_dup(obj));
  39461. ExpectIntEQ(OBJ_cmp(obj, obj2), 0);
  39462. ASN1_OBJECT_free(obj);
  39463. obj = NULL;
  39464. ASN1_OBJECT_free(obj2);
  39465. obj2 = NULL;
  39466. for (i = 0; f[i] != NULL; i++)
  39467. {
  39468. ExpectTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  39469. ExpectNotNull(x509 = d2i_X509_fp(fp, NULL));
  39470. if (fp != XBADFILE) {
  39471. XFCLOSE(fp);
  39472. fp = XBADFILE;
  39473. }
  39474. ExpectNotNull(x509Name = X509_get_issuer_name(x509));
  39475. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  39476. /* Get the Common Name by using OBJ_txt2obj */
  39477. ExpectNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  39478. do
  39479. {
  39480. lastpos = tmp;
  39481. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  39482. } while (tmp > -1);
  39483. ExpectIntNE(lastpos, -1);
  39484. ASN1_OBJECT_free(field_name_obj);
  39485. field_name_obj = NULL;
  39486. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  39487. ExpectNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  39488. ExpectIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  39489. /*
  39490. * All Common Names should be www.wolfssl.com
  39491. * This makes testing easier as we can test for the expected value.
  39492. */
  39493. ExpectStrEQ((char*)buf_dyn, "www.wolfssl.com");
  39494. OPENSSL_free(buf_dyn);
  39495. buf_dyn = NULL;
  39496. bio = BIO_new(BIO_s_mem());
  39497. ExpectTrue(bio != NULL);
  39498. for (j = 0; j < numNames; j++)
  39499. {
  39500. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  39501. ExpectNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  39502. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  39503. }
  39504. BIO_free(bio);
  39505. bio = NULL;
  39506. X509_free(x509);
  39507. x509 = NULL;
  39508. }
  39509. #ifdef HAVE_PKCS12
  39510. {
  39511. PKCS12 *p12 = NULL;
  39512. int boolRet;
  39513. EVP_PKEY *pkey = NULL;
  39514. const char *p12_f[] = {
  39515. #if !defined(NO_DES3) && !defined(NO_RSA)
  39516. "./certs/test-servercert.p12",
  39517. #endif
  39518. NULL};
  39519. for (i = 0; p12_f[i] != NULL; i++)
  39520. {
  39521. ExpectTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  39522. ExpectNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  39523. if (fp != XBADFILE) {
  39524. XFCLOSE(fp);
  39525. fp = XBADFILE;
  39526. }
  39527. ExpectTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  39528. &pkey, &x509, NULL)) > 0);
  39529. wc_PKCS12_free(p12);
  39530. p12 = NULL;
  39531. EVP_PKEY_free(pkey);
  39532. x509Name = X509_get_issuer_name(x509);
  39533. ExpectNotNull(x509Name);
  39534. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  39535. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  39536. for (j = 0; j < numNames; j++)
  39537. {
  39538. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  39539. ExpectNotNull(asn1Name =
  39540. X509_NAME_ENTRY_get_object(x509NameEntry));
  39541. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  39542. }
  39543. BIO_free(bio);
  39544. bio = NULL;
  39545. X509_free(x509);
  39546. x509 = NULL;
  39547. }
  39548. }
  39549. #endif /* HAVE_PKCS12 */
  39550. #endif
  39551. return EXPECT_RESULT();
  39552. }
  39553. static int test_wolfSSL_OBJ_cmp(void)
  39554. {
  39555. EXPECT_DECLS;
  39556. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  39557. ASN1_OBJECT *obj = NULL;
  39558. ASN1_OBJECT *obj2 = NULL;
  39559. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  39560. ExpectNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  39561. ExpectIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  39562. ExpectIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  39563. ExpectIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  39564. ExpectIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  39565. ExpectIntEQ(OBJ_cmp(obj, obj), 0);
  39566. ExpectIntEQ(OBJ_cmp(obj2, obj2), 0);
  39567. ASN1_OBJECT_free(obj);
  39568. ASN1_OBJECT_free(obj2);
  39569. #endif
  39570. return EXPECT_RESULT();
  39571. }
  39572. static int test_wolfSSL_OBJ_txt2nid(void)
  39573. {
  39574. EXPECT_DECLS;
  39575. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  39576. defined(WOLFSSL_APACHE_HTTPD)
  39577. int i;
  39578. static const struct {
  39579. const char* sn;
  39580. const char* ln;
  39581. const char* oid;
  39582. int nid;
  39583. } testVals[] = {
  39584. #ifdef WOLFSSL_APACHE_HTTPD
  39585. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  39586. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  39587. NID_id_on_dnsSRV },
  39588. { "msUPN", "Microsoft User Principal Name",
  39589. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  39590. #endif
  39591. { NULL, NULL, NULL, NID_undef }
  39592. };
  39593. /* Invalid cases */
  39594. ExpectIntEQ(OBJ_txt2nid(NULL), NID_undef);
  39595. ExpectIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  39596. /* Valid cases */
  39597. for (i = 0; testVals[i].sn != NULL; i++) {
  39598. ExpectIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  39599. ExpectIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  39600. ExpectIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  39601. }
  39602. #endif
  39603. return EXPECT_RESULT();
  39604. }
  39605. static int test_wolfSSL_OBJ_txt2obj(void)
  39606. {
  39607. EXPECT_DECLS;
  39608. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  39609. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  39610. int i;
  39611. char buf[50];
  39612. ASN1_OBJECT* obj = NULL;
  39613. static const struct {
  39614. const char* oidStr;
  39615. const char* sn;
  39616. const char* ln;
  39617. } objs_list[] = {
  39618. #if defined(WOLFSSL_APACHE_HTTPD)
  39619. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  39620. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  39621. #endif
  39622. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  39623. { NULL, NULL, NULL }
  39624. };
  39625. static const struct {
  39626. const char* numeric;
  39627. const char* name;
  39628. } objs_named[] = {
  39629. /* In dictionary but not in normal list. */
  39630. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  39631. /* Made up OID. */
  39632. { "1.3.5.7", "1.3.5.7" },
  39633. { NULL, NULL }
  39634. };
  39635. ExpectNull(obj = OBJ_txt2obj("Bad name", 0));
  39636. ASN1_OBJECT_free(obj);
  39637. obj = NULL;
  39638. ExpectNull(obj = OBJ_txt2obj(NULL, 0));
  39639. ASN1_OBJECT_free(obj);
  39640. obj = NULL;
  39641. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  39642. /* Test numerical value of oid (oidStr) */
  39643. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  39644. /* Convert object back to text to confirm oid is correct */
  39645. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  39646. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  39647. ASN1_OBJECT_free(obj);
  39648. obj = NULL;
  39649. XMEMSET(buf, 0, sizeof(buf));
  39650. /* Test short name (sn) */
  39651. ExpectNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  39652. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  39653. /* Convert object back to text to confirm oid is correct */
  39654. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  39655. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  39656. ASN1_OBJECT_free(obj);
  39657. obj = NULL;
  39658. XMEMSET(buf, 0, sizeof(buf));
  39659. /* Test long name (ln) - should fail when no_name = 1 */
  39660. ExpectNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  39661. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  39662. /* Convert object back to text to confirm oid is correct */
  39663. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  39664. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  39665. ASN1_OBJECT_free(obj);
  39666. obj = NULL;
  39667. XMEMSET(buf, 0, sizeof(buf));
  39668. }
  39669. for (i = 0; objs_named[i].numeric != NULL; i++) {
  39670. ExpectNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  39671. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  39672. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  39673. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  39674. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  39675. ASN1_OBJECT_free(obj);
  39676. obj = NULL;
  39677. }
  39678. #endif
  39679. return EXPECT_RESULT();
  39680. }
  39681. static int test_wolfSSL_PEM_write_bio_X509(void)
  39682. {
  39683. EXPECT_DECLS;
  39684. #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
  39685. defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
  39686. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
  39687. !defined(NO_FILESYSTEM)
  39688. /* This test contains the hard coded expected
  39689. * lengths. Update if necessary */
  39690. XFILE fp = XBADFILE;
  39691. WOLFSSL_EVP_PKEY *priv = NULL;
  39692. BIO* input = NULL;
  39693. BIO* output = NULL;
  39694. X509* x509a = NULL;
  39695. X509* x509b = NULL;
  39696. ASN1_TIME* notBeforeA = NULL;
  39697. ASN1_TIME* notAfterA = NULL;
  39698. #ifndef NO_ASN_TIME
  39699. ASN1_TIME* notBeforeB = NULL;
  39700. ASN1_TIME* notAfterB = NULL;
  39701. #endif
  39702. int expectedLen;
  39703. ExpectTrue((fp = XFOPEN("certs/server-key.pem", "rb")) != XBADFILE);
  39704. ExpectNotNull(priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL));
  39705. if (fp != XBADFILE) {
  39706. XFCLOSE(fp);
  39707. fp = XBADFILE;
  39708. }
  39709. ExpectNotNull(input = BIO_new_file("certs/test/cert-ext-multiple.pem",
  39710. "rb"));
  39711. ExpectIntEQ(wolfSSL_BIO_get_len(input), 2000);
  39712. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  39713. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  39714. ExpectNotNull(notBeforeA = X509_get_notBefore(x509a));
  39715. ExpectNotNull(notAfterA = X509_get_notAfter(x509a));
  39716. /* write X509 back to PEM BIO; no need to sign as nothing changed. */
  39717. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  39718. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  39719. /* compare length against expected */
  39720. expectedLen = 2000;
  39721. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  39722. #ifndef NO_ASN_TIME
  39723. /* read exported X509 PEM back into struct, sanity check on export,
  39724. * make sure notBefore/notAfter are the same and certs are identical. */
  39725. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  39726. ExpectNotNull(notBeforeB = X509_get_notBefore(x509b));
  39727. ExpectNotNull(notAfterB = X509_get_notAfter(x509b));
  39728. ExpectIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  39729. ExpectIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  39730. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  39731. X509_free(x509b);
  39732. x509b = NULL;
  39733. #endif
  39734. /* Reset output buffer */
  39735. BIO_free(output);
  39736. output = NULL;
  39737. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  39738. /* Test forcing the AKID to be generated just from KeyIdentifier */
  39739. if (EXPECT_SUCCESS() && x509a->authKeyIdSrc != NULL) {
  39740. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  39741. x509a->authKeyId = x509a->authKeyIdSrc;
  39742. x509a->authKeyIdSrc = NULL;
  39743. x509a->authKeyIdSrcSz = 0;
  39744. }
  39745. /* Resign to re-generate the der */
  39746. ExpectIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
  39747. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  39748. /* Check that we generate a smaller output since the AKID will
  39749. * only contain the KeyIdentifier without any additional
  39750. * information */
  39751. /* Here we copy the validity struct from the original */
  39752. expectedLen = 1688;
  39753. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  39754. /* Reset buffers and x509 */
  39755. BIO_free(input);
  39756. input = NULL;
  39757. BIO_free(output);
  39758. output = NULL;
  39759. X509_free(x509a);
  39760. x509a = NULL;
  39761. /* test CA and basicConstSet values are encoded when
  39762. * the cert is a CA */
  39763. ExpectNotNull(input = BIO_new_file("certs/server-cert.pem", "rb"));
  39764. /* read PEM into X509 struct */
  39765. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  39766. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  39767. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  39768. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  39769. /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
  39770. * values are maintained and certs are identical.*/
  39771. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  39772. ExpectIntEQ(x509b->isCa, 1);
  39773. ExpectIntEQ(x509b->basicConstSet, 1);
  39774. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  39775. X509_free(x509a);
  39776. x509a = NULL;
  39777. X509_free(x509b);
  39778. x509b = NULL;
  39779. BIO_free(input);
  39780. input = NULL;
  39781. BIO_free(output);
  39782. output = NULL;
  39783. /* test CA and basicConstSet values are encoded when
  39784. * the cert is not CA */
  39785. ExpectNotNull(input = BIO_new_file("certs/client-uri-cert.pem", "rb"));
  39786. /* read PEM into X509 struct */
  39787. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  39788. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  39789. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  39790. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  39791. /* read exported X509 PEM back into struct, ensure isCa and
  39792. * basicConstSet values are maintained and certs are identical */
  39793. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  39794. ExpectIntEQ(x509b->isCa, 0);
  39795. ExpectIntEQ(x509b->basicConstSet, 1);
  39796. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  39797. wolfSSL_EVP_PKEY_free(priv);
  39798. X509_free(x509a);
  39799. X509_free(x509b);
  39800. BIO_free(input);
  39801. BIO_free(output);
  39802. #endif
  39803. return EXPECT_RESULT();
  39804. }
  39805. static int test_wolfSSL_X509_NAME_ENTRY(void)
  39806. {
  39807. EXPECT_DECLS;
  39808. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  39809. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  39810. X509* x509 = NULL;
  39811. #ifndef NO_BIO
  39812. BIO* bio = NULL;
  39813. #endif
  39814. X509_NAME* nm = NULL;
  39815. X509_NAME_ENTRY* entry = NULL;
  39816. unsigned char cn[] = "another name to add";
  39817. #ifdef OPENSSL_ALL
  39818. int i;
  39819. int names_len = 0;
  39820. #endif
  39821. ExpectNotNull(x509 =
  39822. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  39823. #ifndef NO_BIO
  39824. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39825. ExpectIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  39826. #endif
  39827. #ifdef WOLFSSL_CERT_REQ
  39828. {
  39829. X509_REQ* req = NULL;
  39830. #ifndef NO_BIO
  39831. BIO* bReq = NULL;
  39832. #endif
  39833. ExpectNotNull(req =
  39834. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  39835. #ifndef NO_BIO
  39836. ExpectNotNull(bReq = BIO_new(BIO_s_mem()));
  39837. ExpectIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  39838. BIO_free(bReq);
  39839. #endif
  39840. X509_free(req);
  39841. }
  39842. #endif
  39843. ExpectNotNull(nm = X509_get_subject_name(x509));
  39844. /* Test add entry */
  39845. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  39846. 0x0c, cn, (int)sizeof(cn)));
  39847. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  39848. #ifdef WOLFSSL_CERT_EXT
  39849. ExpectIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  39850. (byte*)"support@wolfssl.com", 19, -1,
  39851. 1), WOLFSSL_SUCCESS);
  39852. #endif
  39853. X509_NAME_ENTRY_free(entry);
  39854. entry = NULL;
  39855. #ifdef WOLFSSL_CERT_REQ
  39856. {
  39857. unsigned char srv_pkcs9p[] = "Server";
  39858. unsigned char fvrtDrnk[] = "tequila";
  39859. unsigned char* der = NULL;
  39860. char* subject = NULL;
  39861. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  39862. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  39863. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_favouriteDrink,
  39864. MBSTRING_ASC, fvrtDrnk, -1, -1, 0), SSL_SUCCESS);
  39865. ExpectIntGT(wolfSSL_i2d_X509_NAME(nm, &der), 0);
  39866. ExpectNotNull(der);
  39867. ExpectNotNull(subject = X509_NAME_oneline(nm, 0, 0));
  39868. ExpectNotNull(XSTRSTR(subject, "favouriteDrink=tequila"));
  39869. ExpectNotNull(XSTRSTR(subject, "contentType=Server"));
  39870. #ifdef DEBUG_WOLFSSL
  39871. if (subject != NULL) {
  39872. fprintf(stderr, "\n\t%s\n", subject);
  39873. }
  39874. #endif
  39875. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  39876. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  39877. }
  39878. #endif
  39879. /* Test add entry by text */
  39880. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  39881. 0x0c, cn, (int)sizeof(cn)));
  39882. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  39883. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  39884. ExpectNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  39885. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  39886. #endif
  39887. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  39888. X509_NAME_ENTRY_free(entry);
  39889. entry = NULL;
  39890. /* Test add entry by NID */
  39891. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  39892. cn, -1, -1, 0), SSL_SUCCESS);
  39893. #ifdef OPENSSL_ALL
  39894. /* stack of name entry */
  39895. ExpectIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  39896. for (i = 0; i < names_len; i++) {
  39897. ExpectNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  39898. }
  39899. #endif
  39900. #ifndef NO_BIO
  39901. BIO_free(bio);
  39902. #endif
  39903. X509_free(x509); /* free's nm */
  39904. #endif
  39905. return EXPECT_RESULT();
  39906. }
  39907. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  39908. static int test_GENERAL_NAME_set0_othername(void) {
  39909. EXPECT_DECLS;
  39910. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  39911. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  39912. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  39913. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  39914. defined(WOLFSSL_FPKI)
  39915. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  39916. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  39917. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  39918. const char * cert_fname = "./certs/server-cert.der";
  39919. const char * key_fname = "./certs/server-key.der";
  39920. X509* x509 = NULL;
  39921. GENERAL_NAME* gn = NULL;
  39922. GENERAL_NAMES* gns = NULL;
  39923. ASN1_OBJECT* upn_oid = NULL;
  39924. ASN1_UTF8STRING *utf8str = NULL;
  39925. ASN1_TYPE *value = NULL;
  39926. X509_EXTENSION * ext = NULL;
  39927. byte* pt = NULL;
  39928. byte der[4096];
  39929. int derSz = 0;
  39930. EVP_PKEY* priv = NULL;
  39931. XFILE f = XBADFILE;
  39932. ExpectTrue((f = XFOPEN(cert_fname, "rb")) != XBADFILE);
  39933. ExpectNotNull(x509 = d2i_X509_fp(f, NULL));
  39934. if (f != XBADFILE) {
  39935. XFCLOSE(f);
  39936. f = XBADFILE;
  39937. }
  39938. ExpectNotNull(gn = GENERAL_NAME_new());
  39939. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  39940. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  39941. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  39942. ExpectNotNull(value = ASN1_TYPE_new());
  39943. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  39944. if ((value == NULL) || (value->value.ptr != (char*)utf8str)) {
  39945. wolfSSL_ASN1_STRING_free(utf8str);
  39946. }
  39947. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  39948. if (EXPECT_FAIL()) {
  39949. ASN1_TYPE_free(value);
  39950. }
  39951. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  39952. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  39953. if (EXPECT_FAIL()) {
  39954. GENERAL_NAME_free(gn);
  39955. gn = NULL;
  39956. }
  39957. ExpectNotNull(ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  39958. ExpectIntEQ(X509_add_ext(x509, ext, -1), 1);
  39959. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  39960. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  39961. if (f != XBADFILE) {
  39962. XFCLOSE(f);
  39963. f = XBADFILE;
  39964. }
  39965. pt = der;
  39966. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  39967. (const unsigned char**)&pt, derSz));
  39968. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  39969. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  39970. gns = NULL;
  39971. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  39972. NULL));
  39973. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 3);
  39974. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 2));
  39975. ExpectIntEQ(gn->type, 0);
  39976. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  39977. ASN1_OBJECT_free(upn_oid);
  39978. X509_EXTENSION_free(ext);
  39979. X509_free(x509);
  39980. EVP_PKEY_free(priv);
  39981. #endif
  39982. return EXPECT_RESULT();
  39983. }
  39984. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  39985. static int test_othername_and_SID_ext(void) {
  39986. EXPECT_DECLS;
  39987. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  39988. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  39989. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  39990. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  39991. defined(WOLFSSL_FPKI) && defined(WOLFSSL_ASN_TEMPLATE)
  39992. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  39993. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  39994. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  39995. const char* csr_fname = "./certs/csr.signed.der";
  39996. const char* key_fname = "./certs/server-key.der";
  39997. byte der[4096];
  39998. int derSz = 0;
  39999. X509_REQ* x509 = NULL;
  40000. STACK_OF(X509_EXTENSION) *exts = NULL;
  40001. X509_EXTENSION * san_ext = NULL;
  40002. X509_EXTENSION * ext = NULL;
  40003. GENERAL_NAME* gn = NULL;
  40004. GENERAL_NAMES* gns = NULL;
  40005. ASN1_OBJECT* upn_oid = NULL;
  40006. ASN1_UTF8STRING *utf8str = NULL;
  40007. ASN1_TYPE *value = NULL;
  40008. ASN1_STRING *extval = NULL;
  40009. /* SID extension. SID data format explained here:
  40010. * https://blog.qdsecurity.se/2022/05/27/manually-injecting-a-sid-in-a-certificate/
  40011. */
  40012. uint8_t SidExtension[] = {
  40013. 48, 64, 160, 62, 6, 10, 43, 6, 1, 4, 1, 130, 55, 25, 2, 1, 160,
  40014. 48, 4, 46, 83, 45, 49, 45, 53, 45, 50, 49, 45, 50, 56, 52, 51, 57,
  40015. 48, 55, 52, 49, 56, 45, 51, 57, 50, 54, 50, 55, 55, 52, 50, 49, 45,
  40016. 51, 56, 49, 53, 57, 57, 51, 57, 55, 50, 45, 52, 54, 48, 49};
  40017. uint8_t expectedAltName[] = {
  40018. 0x30, 0x27, 0xA0, 0x25, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82,
  40019. 0x37, 0x14, 0x02, 0x03, 0xA0, 0x17, 0x0C, 0x15, 0x6F, 0x74, 0x68, 0x65,
  40020. 0x72, 0x6E, 0x61, 0x6D, 0x65, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  40021. 0x6C, 0x2E, 0x63, 0x6F, 0x6D};
  40022. X509_EXTENSION *sid_ext = NULL;
  40023. ASN1_OBJECT* sid_oid = NULL;
  40024. ASN1_OCTET_STRING *sid_data = NULL;
  40025. EVP_PKEY* priv = NULL;
  40026. XFILE f = XBADFILE;
  40027. byte* pt = NULL;
  40028. BIO* bio = NULL;
  40029. ExpectTrue((f = XFOPEN(csr_fname, "rb")) != XBADFILE);
  40030. ExpectNotNull(x509 = d2i_X509_REQ_fp(f, NULL));
  40031. if (f != XBADFILE) {
  40032. XFCLOSE(f);
  40033. f = XBADFILE;
  40034. }
  40035. ExpectIntEQ(X509_REQ_set_version(x509, 2), 1);
  40036. ExpectNotNull(gn = GENERAL_NAME_new());
  40037. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  40038. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  40039. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  40040. ExpectNotNull(value = ASN1_TYPE_new());
  40041. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  40042. if (EXPECT_FAIL()) {
  40043. ASN1_UTF8STRING_free(utf8str);
  40044. }
  40045. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  40046. if (EXPECT_FAIL()) {
  40047. ASN1_TYPE_free(value);
  40048. GENERAL_NAME_free(gn);
  40049. gn = NULL;
  40050. }
  40051. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  40052. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  40053. if (EXPECT_FAIL()) {
  40054. GENERAL_NAME_free(gn);
  40055. }
  40056. ExpectNotNull(san_ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  40057. ExpectNotNull(sid_oid = OBJ_txt2obj("1.3.6.1.4.1.311.25.2", 1));
  40058. ExpectNotNull(sid_data = ASN1_OCTET_STRING_new());
  40059. ASN1_OCTET_STRING_set(sid_data, SidExtension, sizeof(SidExtension));
  40060. ExpectNotNull(sid_ext = X509_EXTENSION_create_by_OBJ(NULL, sid_oid, 0,
  40061. sid_data));
  40062. ExpectNotNull(exts = sk_X509_EXTENSION_new_null());
  40063. /* Ensure an empty stack doesn't raise an error. */
  40064. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  40065. ExpectIntEQ(sk_X509_EXTENSION_push(exts, san_ext), 1);
  40066. if (EXPECT_FAIL()) {
  40067. X509_EXTENSION_free(san_ext);
  40068. }
  40069. ExpectIntEQ(sk_X509_EXTENSION_push(exts, sid_ext), 2);
  40070. if (EXPECT_FAIL()) {
  40071. X509_EXTENSION_free(sid_ext);
  40072. }
  40073. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  40074. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  40075. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  40076. if (f != XBADFILE)
  40077. XFCLOSE(f);
  40078. pt = der;
  40079. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  40080. (const unsigned char**)&pt, derSz));
  40081. ExpectIntGT(X509_REQ_sign(x509, priv, EVP_sha256()), 0);
  40082. pt = der;
  40083. ExpectIntGT(derSz = i2d_X509_REQ(x509, &pt), 0);
  40084. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  40085. gns = NULL;
  40086. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  40087. exts = NULL;
  40088. ASN1_OBJECT_free(upn_oid);
  40089. ASN1_OBJECT_free(sid_oid);
  40090. ASN1_OCTET_STRING_free(sid_data);
  40091. X509_REQ_free(x509);
  40092. x509 = NULL;
  40093. EVP_PKEY_free(priv);
  40094. /* At this point everything used to generate what is in der is cleaned up.
  40095. * We now read back from der to confirm the extensions were inserted
  40096. * correctly. */
  40097. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
  40098. ExpectNotNull(bio);
  40099. ExpectIntEQ(BIO_write(bio, der, derSz), derSz); /* d2i consumes BIO */
  40100. ExpectNotNull(d2i_X509_REQ_bio(bio, &x509));
  40101. ExpectNotNull(x509);
  40102. BIO_free(bio);
  40103. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  40104. x509));
  40105. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  40106. /* Check the SID extension. */
  40107. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 0));
  40108. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  40109. ExpectIntEQ(extval->length, sizeof(SidExtension));
  40110. ExpectIntEQ(XMEMCMP(SidExtension, extval->data, sizeof(SidExtension)), 0);
  40111. /* Check the AltNames extension. */
  40112. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 1));
  40113. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  40114. ExpectIntEQ(extval->length, sizeof(expectedAltName));
  40115. ExpectIntEQ(XMEMCMP(expectedAltName, extval->data, sizeof(expectedAltName)),
  40116. 0);
  40117. /* Cleanup */
  40118. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  40119. NULL));
  40120. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  40121. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 0));
  40122. ExpectIntEQ(gn->type, 0);
  40123. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  40124. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  40125. X509_REQ_free(x509);
  40126. #endif
  40127. return EXPECT_RESULT();
  40128. }
  40129. static int test_wolfSSL_X509_set_name(void)
  40130. {
  40131. EXPECT_DECLS;
  40132. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  40133. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  40134. X509* x509 = NULL;
  40135. X509_NAME* name = NULL;
  40136. ExpectNotNull(name = X509_NAME_new());
  40137. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  40138. (byte*)"wolfssl.com", 11, 0, 1),
  40139. WOLFSSL_SUCCESS);
  40140. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  40141. (byte*)"support@wolfssl.com", 19, -1,
  40142. 1), WOLFSSL_SUCCESS);
  40143. ExpectNotNull(x509 = X509_new());
  40144. ExpectIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  40145. ExpectIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  40146. ExpectIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  40147. ExpectIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  40148. ExpectIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  40149. ExpectIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  40150. ExpectIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  40151. ExpectIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  40152. X509_free(x509);
  40153. X509_NAME_free(name);
  40154. #endif /* OPENSSL_ALL && !NO_CERTS */
  40155. return EXPECT_RESULT();
  40156. }
  40157. static int test_wolfSSL_X509_set_notAfter(void)
  40158. {
  40159. EXPECT_DECLS;
  40160. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  40161. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  40162. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  40163. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  40164. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  40165. /* Generalized time will overflow time_t if not long */
  40166. X509* x = NULL;
  40167. BIO* bio = NULL;
  40168. ASN1_TIME *asn_time = NULL;
  40169. ASN1_TIME *time_check = NULL;
  40170. const int year = 365*24*60*60;
  40171. const int day = 24*60*60;
  40172. const int hour = 60*60;
  40173. const int mini = 60;
  40174. int offset_day;
  40175. unsigned char buf[25];
  40176. time_t t;
  40177. /*
  40178. * Setup asn_time. APACHE HTTPD uses time(NULL)
  40179. */
  40180. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  40181. offset_day = 7;
  40182. /*
  40183. * Free these.
  40184. */
  40185. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  40186. ExpectNotNull(asn_time);
  40187. ExpectNotNull(x = X509_new());
  40188. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40189. /*
  40190. * Tests
  40191. */
  40192. ExpectTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  40193. /* time_check is simply (ANS1_TIME*)x->notAfter */
  40194. ExpectNotNull(time_check = X509_get_notAfter(x));
  40195. /* ANS1_TIME_check validates by checking if argument can be parsed */
  40196. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  40197. /* Convert to human readable format and compare to intended date */
  40198. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  40199. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  40200. ExpectIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  40201. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, NULL));
  40202. ExpectFalse(wolfSSL_X509_set_notAfter(x, NULL));
  40203. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, asn_time));
  40204. /*
  40205. * Cleanup
  40206. */
  40207. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  40208. X509_free(x);
  40209. BIO_free(bio);
  40210. #endif
  40211. return EXPECT_RESULT();
  40212. }
  40213. static int test_wolfSSL_X509_set_notBefore(void)
  40214. {
  40215. EXPECT_DECLS;
  40216. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  40217. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  40218. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  40219. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  40220. X509* x = NULL;
  40221. BIO* bio = NULL;
  40222. ASN1_TIME *asn_time = NULL;
  40223. ASN1_TIME *time_check = NULL;
  40224. const int year = 365*24*60*60;
  40225. const int day = 24*60*60;
  40226. const int hour = 60*60;
  40227. const int mini = 60;
  40228. int offset_day;
  40229. unsigned char buf[25];
  40230. time_t t;
  40231. /*
  40232. * Setup asn_time. APACHE HTTPD uses time(NULL)
  40233. */
  40234. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  40235. offset_day = 7;
  40236. /*
  40237. * Free these.
  40238. */
  40239. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  40240. ExpectNotNull(asn_time);
  40241. ExpectNotNull(x = X509_new());
  40242. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40243. ExpectIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  40244. /*
  40245. * Main Tests
  40246. */
  40247. ExpectTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  40248. /* time_check == (ANS1_TIME*)x->notBefore */
  40249. ExpectNotNull(time_check = X509_get_notBefore(x));
  40250. /* ANS1_TIME_check validates by checking if argument can be parsed */
  40251. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  40252. /* Convert to human readable format and compare to intended date */
  40253. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  40254. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  40255. ExpectIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  40256. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, NULL));
  40257. ExpectFalse(wolfSSL_X509_set_notBefore(x, NULL));
  40258. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, asn_time));
  40259. /*
  40260. * Cleanup
  40261. */
  40262. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  40263. X509_free(x);
  40264. BIO_free(bio);
  40265. #endif
  40266. return EXPECT_RESULT();
  40267. }
  40268. static int test_wolfSSL_X509_set_version(void)
  40269. {
  40270. EXPECT_DECLS;
  40271. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  40272. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  40273. X509* x509 = NULL;
  40274. long v = 2L;
  40275. long maxInt = INT_MAX;
  40276. ExpectNotNull(x509 = X509_new());
  40277. /* These should pass. */
  40278. ExpectTrue(wolfSSL_X509_set_version(x509, v));
  40279. ExpectIntEQ(v, wolfSSL_X509_get_version(x509));
  40280. /* Fail Case: When v(long) is greater than x509->version(int). */
  40281. v = maxInt+1;
  40282. ExpectFalse(wolfSSL_X509_set_version(x509, v));
  40283. ExpectFalse(wolfSSL_X509_set_version(NULL, 2L));
  40284. ExpectFalse(wolfSSL_X509_set_version(NULL, maxInt+1));
  40285. /* Cleanup */
  40286. X509_free(x509);
  40287. #endif
  40288. return EXPECT_RESULT();
  40289. }
  40290. #ifndef NO_BIO
  40291. static int test_wolfSSL_BIO_gets(void)
  40292. {
  40293. EXPECT_DECLS;
  40294. #if defined(OPENSSL_EXTRA)
  40295. BIO* bio = NULL;
  40296. BIO* bio2 = NULL;
  40297. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  40298. char emp[] = "";
  40299. char bio_buffer[20];
  40300. int bufferSz = 20;
  40301. #ifdef OPENSSL_ALL
  40302. BUF_MEM* emp_bm = NULL;
  40303. BUF_MEM* msg_bm = NULL;
  40304. #endif
  40305. /* try with bad args */
  40306. ExpectNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  40307. #ifdef OPENSSL_ALL
  40308. ExpectIntEQ(BIO_set_mem_buf(bio, NULL, BIO_NOCLOSE), BAD_FUNC_ARG);
  40309. #endif
  40310. /* try with real msg */
  40311. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  40312. XMEMSET(bio_buffer, 0, bufferSz);
  40313. ExpectNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  40314. ExpectNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  40315. ExpectNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  40316. ExpectFalse(bio2 != BIO_next(bio));
  40317. /* make buffer filled with no terminating characters */
  40318. XMEMSET(bio_buffer, 1, bufferSz);
  40319. /* BIO_gets reads a line of data */
  40320. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  40321. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  40322. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  40323. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  40324. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  40325. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  40326. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  40327. #ifdef OPENSSL_ALL
  40328. /* test setting the mem_buf manually */
  40329. BIO_free(bio);
  40330. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  40331. ExpectNotNull(emp_bm = BUF_MEM_new());
  40332. ExpectNotNull(msg_bm = BUF_MEM_new());
  40333. ExpectIntEQ(BUF_MEM_grow(msg_bm, sizeof(msg)), sizeof(msg));
  40334. if (EXPECT_SUCCESS()) {
  40335. XFREE(msg_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  40336. msg_bm->data = NULL;
  40337. }
  40338. /* emp size is 1 for terminator */
  40339. ExpectIntEQ(BUF_MEM_grow(emp_bm, sizeof(emp)), sizeof(emp));
  40340. if (EXPECT_SUCCESS()) {
  40341. XFREE(emp_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  40342. emp_bm->data = emp;
  40343. msg_bm->data = msg;
  40344. }
  40345. ExpectIntEQ(BIO_set_mem_buf(bio, emp_bm, BIO_CLOSE), WOLFSSL_SUCCESS);
  40346. /* check reading an empty string */
  40347. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  40348. ExpectStrEQ(emp, bio_buffer);
  40349. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  40350. /* BIO_gets reads a line of data */
  40351. ExpectIntEQ(BIO_set_mem_buf(bio, msg_bm, BIO_NOCLOSE), WOLFSSL_SUCCESS);
  40352. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  40353. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  40354. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  40355. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  40356. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  40357. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  40358. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  40359. if (EXPECT_SUCCESS())
  40360. emp_bm->data = NULL;
  40361. BUF_MEM_free(emp_bm);
  40362. if (EXPECT_SUCCESS())
  40363. msg_bm->data = NULL;
  40364. BUF_MEM_free(msg_bm);
  40365. #endif
  40366. /* check not null terminated string */
  40367. BIO_free(bio);
  40368. bio = NULL;
  40369. msg[0] = 0x33;
  40370. msg[1] = 0x33;
  40371. msg[2] = 0x33;
  40372. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  40373. ExpectIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  40374. ExpectIntEQ(bio_buffer[0], msg[0]);
  40375. ExpectIntEQ(bio_buffer[1], msg[1]);
  40376. ExpectIntNE(bio_buffer[2], msg[2]);
  40377. BIO_free(bio);
  40378. bio = NULL;
  40379. msg[3] = 0x33;
  40380. bio_buffer[3] = 0x33;
  40381. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  40382. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  40383. ExpectIntEQ(bio_buffer[0], msg[0]);
  40384. ExpectIntEQ(bio_buffer[1], msg[1]);
  40385. ExpectIntEQ(bio_buffer[2], msg[2]);
  40386. ExpectIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  40387. /* check reading an empty string */
  40388. BIO_free(bio);
  40389. bio = NULL;
  40390. ExpectNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  40391. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  40392. ExpectStrEQ(emp, bio_buffer);
  40393. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  40394. /* check error cases */
  40395. BIO_free(bio);
  40396. bio = NULL;
  40397. ExpectIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  40398. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40399. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  40400. #if !defined(NO_FILESYSTEM)
  40401. {
  40402. BIO* f_bio = NULL;
  40403. XFILE f = XBADFILE;
  40404. ExpectNotNull(f_bio = BIO_new(BIO_s_file()));
  40405. ExpectIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  40406. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  40407. ExpectIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  40408. if (EXPECT_FAIL() && (f != XBADFILE)) {
  40409. XFCLOSE(f);
  40410. }
  40411. ExpectIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  40412. BIO_free(f_bio);
  40413. f_bio = NULL;
  40414. }
  40415. #endif /* NO_FILESYSTEM */
  40416. BIO_free(bio);
  40417. bio = NULL;
  40418. BIO_free(bio2);
  40419. bio2 = NULL;
  40420. /* try with type BIO */
  40421. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  40422. sizeof(msg));
  40423. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  40424. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  40425. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  40426. ExpectIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  40427. ExpectIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  40428. ExpectIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  40429. ExpectIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  40430. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  40431. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  40432. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  40433. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  40434. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  40435. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  40436. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  40437. BIO_free(bio);
  40438. bio = NULL;
  40439. BIO_free(bio2);
  40440. bio2 = NULL;
  40441. /* check reading an empty string */
  40442. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  40443. ExpectIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  40444. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  40445. ExpectStrEQ(emp, bio_buffer);
  40446. BIO_free(bio);
  40447. bio = NULL;
  40448. #endif
  40449. return EXPECT_RESULT();
  40450. }
  40451. static int test_wolfSSL_BIO_puts(void)
  40452. {
  40453. EXPECT_DECLS;
  40454. #if defined(OPENSSL_EXTRA)
  40455. BIO* bio = NULL;
  40456. char input[] = "hello\0world\n.....ok\n\0";
  40457. char output[128];
  40458. XMEMSET(output, 0, sizeof(output));
  40459. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40460. ExpectIntEQ(BIO_puts(bio, input), 5);
  40461. ExpectIntEQ(BIO_pending(bio), 5);
  40462. ExpectIntEQ(BIO_puts(bio, input + 6), 14);
  40463. ExpectIntEQ(BIO_pending(bio), 19);
  40464. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  40465. ExpectStrEQ(output, "helloworld\n");
  40466. ExpectIntEQ(BIO_pending(bio), 8);
  40467. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  40468. ExpectStrEQ(output, ".....ok\n");
  40469. ExpectIntEQ(BIO_pending(bio), 0);
  40470. ExpectIntEQ(BIO_puts(bio, ""), -1);
  40471. BIO_free(bio);
  40472. #endif
  40473. return EXPECT_RESULT();
  40474. }
  40475. static int test_wolfSSL_BIO_dump(void)
  40476. {
  40477. EXPECT_DECLS;
  40478. #if defined(OPENSSL_EXTRA)
  40479. BIO* bio;
  40480. static const unsigned char data[] = {
  40481. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  40482. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  40483. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  40484. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  40485. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  40486. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  40487. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  40488. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  40489. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  40490. 0xB4
  40491. };
  40492. /* Generated with OpenSSL. */
  40493. static const char expected[] =
  40494. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  40495. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  40496. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  40497. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  40498. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  40499. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  40500. static const char expectedAll[] =
  40501. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  40502. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  40503. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  40504. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  40505. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  40506. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  40507. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  40508. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  40509. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  40510. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  40511. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  40512. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  40513. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  40514. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  40515. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  40516. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  40517. char output[16 * 80];
  40518. int i;
  40519. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40520. /* Example key dumped. */
  40521. ExpectIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  40522. sizeof(expected) - 1);
  40523. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  40524. ExpectIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  40525. /* Try every possible value for a character. */
  40526. for (i = 0; i < 256; i++)
  40527. output[i] = i;
  40528. ExpectIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  40529. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  40530. ExpectIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  40531. BIO_free(bio);
  40532. #endif
  40533. return EXPECT_RESULT();
  40534. }
  40535. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40536. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  40537. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  40538. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  40539. {
  40540. (void)ssl;
  40541. (void)buf;
  40542. (void)sz;
  40543. (void)ctx;
  40544. return WOLFSSL_CBIO_ERR_WANT_READ;
  40545. }
  40546. #endif
  40547. static int test_wolfSSL_BIO_should_retry(void)
  40548. {
  40549. EXPECT_DECLS;
  40550. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40551. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  40552. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  40553. tcp_ready ready;
  40554. func_args server_args;
  40555. THREAD_TYPE serverThread;
  40556. SOCKET_T sockfd = 0;
  40557. WOLFSSL_CTX* ctx = NULL;
  40558. WOLFSSL* ssl = NULL;
  40559. char msg[64] = "hello wolfssl!";
  40560. char reply[1024];
  40561. int msgSz = (int)XSTRLEN(msg);
  40562. int ret;
  40563. BIO* bio = NULL;
  40564. XMEMSET(&server_args, 0, sizeof(func_args));
  40565. #ifdef WOLFSSL_TIRTOS
  40566. fdOpenSession(Task_self());
  40567. #endif
  40568. StartTCP();
  40569. InitTcpReady(&ready);
  40570. #if defined(USE_WINDOWS_API)
  40571. /* use RNG to get random port if using windows */
  40572. ready.port = GetRandomPort();
  40573. #endif
  40574. server_args.signal = &ready;
  40575. start_thread(test_server_nofail, &server_args, &serverThread);
  40576. wait_tcp_ready(&server_args);
  40577. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40578. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  40579. ExpectIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  40580. #endif
  40581. ExpectIntEQ(WOLFSSL_SUCCESS,
  40582. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  40583. ExpectIntEQ(WOLFSSL_SUCCESS,
  40584. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  40585. ExpectIntEQ(WOLFSSL_SUCCESS,
  40586. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  40587. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  40588. /* force retry */
  40589. ExpectNotNull(bio = wolfSSL_BIO_new_ssl(ctx, 1));
  40590. ExpectIntEQ(BIO_get_ssl(bio, &ssl), 1);
  40591. ExpectNotNull(ssl);
  40592. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  40593. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  40594. if (EXPECT_FAIL()) {
  40595. wolfSSL_free(ssl);
  40596. ssl = NULL;
  40597. }
  40598. ExpectIntLE(BIO_write(bio, msg, msgSz), 0);
  40599. ExpectIntNE(BIO_should_retry(bio), 0);
  40600. ExpectIntEQ(BIO_should_read(bio), 0);
  40601. ExpectIntEQ(BIO_should_write(bio), 0);
  40602. /* now perform successful connection */
  40603. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  40604. ExpectIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  40605. ExpectIntNE(BIO_read(bio, reply, sizeof(reply)), 0);
  40606. ret = wolfSSL_get_error(ssl, -1);
  40607. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  40608. ExpectIntNE(BIO_should_retry(bio), 0);
  40609. if (ret == WOLFSSL_ERROR_WANT_READ)
  40610. ExpectIntEQ(BIO_should_read(bio), 1);
  40611. else
  40612. ExpectIntEQ(BIO_should_read(bio), 0);
  40613. if (ret == WOLFSSL_ERROR_WANT_WRITE)
  40614. ExpectIntEQ(BIO_should_write(bio), 1);
  40615. else
  40616. ExpectIntEQ(BIO_should_write(bio), 0);
  40617. }
  40618. else {
  40619. ExpectIntEQ(BIO_should_retry(bio), 0);
  40620. ExpectIntEQ(BIO_should_read(bio), 0);
  40621. ExpectIntEQ(BIO_should_write(bio), 0);
  40622. }
  40623. ExpectIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  40624. XSTRLEN("I hear you fa shizzle!")), 0);
  40625. BIO_free(bio);
  40626. wolfSSL_CTX_free(ctx);
  40627. CloseSocket(sockfd);
  40628. join_thread(serverThread);
  40629. FreeTcpReady(&ready);
  40630. #ifdef WOLFSSL_TIRTOS
  40631. fdOpenSession(Task_self());
  40632. #endif
  40633. #endif
  40634. return EXPECT_RESULT();
  40635. }
  40636. static int test_wolfSSL_BIO_connect(void)
  40637. {
  40638. EXPECT_DECLS;
  40639. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40640. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  40641. tcp_ready ready;
  40642. func_args server_args;
  40643. THREAD_TYPE serverThread;
  40644. BIO *tcpBio = NULL;
  40645. BIO *sslBio = NULL;
  40646. SSL_CTX* ctx = NULL;
  40647. SSL *ssl = NULL;
  40648. SSL *sslPtr;
  40649. char msg[] = "hello wolfssl!";
  40650. char reply[30];
  40651. char buff[10] = {0};
  40652. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40653. ExpectIntEQ(WOLFSSL_SUCCESS,
  40654. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  40655. ExpectIntEQ(WOLFSSL_SUCCESS,
  40656. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  40657. ExpectIntEQ(WOLFSSL_SUCCESS,
  40658. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  40659. /* Setup server */
  40660. XMEMSET(&server_args, 0, sizeof(func_args));
  40661. StartTCP();
  40662. InitTcpReady(&ready);
  40663. #if defined(USE_WINDOWS_API)
  40664. /* use RNG to get random port if using windows */
  40665. ready.port = GetRandomPort();
  40666. #endif
  40667. server_args.signal = &ready;
  40668. start_thread(test_server_nofail, &server_args, &serverThread);
  40669. wait_tcp_ready(&server_args);
  40670. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  40671. /* Start the test proper */
  40672. /* Setup the TCP BIO */
  40673. ExpectNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  40674. ExpectIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  40675. /* Setup the SSL object */
  40676. ExpectNotNull(ssl = SSL_new(ctx));
  40677. SSL_set_connect_state(ssl);
  40678. /* Setup the SSL BIO */
  40679. ExpectNotNull(sslBio = BIO_new(BIO_f_ssl()));
  40680. ExpectIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  40681. if (EXPECT_FAIL()) {
  40682. wolfSSL_free(ssl);
  40683. }
  40684. /* Verify that BIO_get_ssl works. */
  40685. ExpectIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  40686. ExpectPtrEq(ssl, sslPtr);
  40687. /* Link BIO's so that sslBio uses tcpBio for IO */
  40688. ExpectPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  40689. /* Do TCP connect */
  40690. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  40691. /* Do TLS handshake */
  40692. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  40693. /* Test writing */
  40694. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  40695. /* Expect length of default wolfSSL reply */
  40696. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  40697. /* Clean it all up */
  40698. BIO_free_all(sslBio);
  40699. /* Server clean up */
  40700. join_thread(serverThread);
  40701. FreeTcpReady(&ready);
  40702. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  40703. * after. */
  40704. XMEMSET(&server_args, 0, sizeof(func_args));
  40705. StartTCP();
  40706. InitTcpReady(&ready);
  40707. #if defined(USE_WINDOWS_API)
  40708. /* use RNG to get random port if using windows */
  40709. ready.port = GetRandomPort();
  40710. #endif
  40711. server_args.signal = &ready;
  40712. start_thread(test_server_nofail, &server_args, &serverThread);
  40713. wait_tcp_ready(&server_args);
  40714. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  40715. ExpectNotNull(sslBio = BIO_new_ssl_connect(ctx));
  40716. ExpectIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  40717. ExpectIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  40718. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  40719. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  40720. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  40721. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  40722. /* Attempt to close the TLS connection gracefully. */
  40723. BIO_ssl_shutdown(sslBio);
  40724. BIO_free_all(sslBio);
  40725. join_thread(serverThread);
  40726. FreeTcpReady(&ready);
  40727. SSL_CTX_free(ctx);
  40728. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  40729. wc_ecc_fp_free(); /* free per thread cache */
  40730. #endif
  40731. #endif
  40732. return EXPECT_RESULT();
  40733. }
  40734. static int test_wolfSSL_BIO_tls(void)
  40735. {
  40736. EXPECT_DECLS;
  40737. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  40738. SSL_CTX* ctx = NULL;
  40739. SSL *ssl = NULL;
  40740. BIO *readBio = NULL;
  40741. BIO *writeBio = NULL;
  40742. int ret;
  40743. int err = 0;
  40744. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  40745. ExpectNotNull(ssl = SSL_new(ctx));
  40746. ExpectNotNull(readBio = BIO_new(BIO_s_mem()));
  40747. ExpectNotNull(writeBio = BIO_new(BIO_s_mem()));
  40748. /* Qt reads data from write-bio,
  40749. * then writes the read data into plain packet.
  40750. * Qt reads data from plain packet,
  40751. * then writes the read data into read-bio.
  40752. */
  40753. SSL_set_bio(ssl, readBio, writeBio);
  40754. do {
  40755. #ifdef WOLFSSL_ASYNC_CRYPT
  40756. if (err == WC_PENDING_E) {
  40757. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  40758. if (ret < 0) { break; } else if (ret == 0) { continue; }
  40759. }
  40760. #endif
  40761. ret = SSL_connect(ssl);
  40762. err = SSL_get_error(ssl, 0);
  40763. } while (err == WC_PENDING_E);
  40764. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  40765. /* in this use case, should return WANT READ
  40766. * so that Qt will read the data from plain packet for next state.
  40767. */
  40768. ExpectIntEQ(err, SSL_ERROR_WANT_READ);
  40769. SSL_free(ssl);
  40770. SSL_CTX_free(ctx);
  40771. #endif
  40772. return EXPECT_RESULT();
  40773. }
  40774. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40775. defined(HAVE_HTTP_CLIENT)
  40776. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  40777. {
  40778. BIO* clientBio;
  40779. SSL* sslClient;
  40780. SSL_CTX* ctx;
  40781. char connectAddr[20]; /* IP + port */;
  40782. (void)args;
  40783. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  40784. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  40785. AssertIntEQ(BIO_do_connect(clientBio), 1);
  40786. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  40787. AssertNotNull(sslClient = SSL_new(ctx));
  40788. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  40789. SSL_set_bio(sslClient, clientBio, clientBio);
  40790. AssertIntEQ(SSL_connect(sslClient), 1);
  40791. SSL_free(sslClient);
  40792. SSL_CTX_free(ctx);
  40793. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  40794. wc_ecc_fp_free(); /* free per thread cache */
  40795. #endif
  40796. WOLFSSL_RETURN_FROM_THREAD(0);
  40797. }
  40798. #endif
  40799. static int test_wolfSSL_BIO_accept(void)
  40800. {
  40801. EXPECT_DECLS;
  40802. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40803. defined(HAVE_HTTP_CLIENT)
  40804. BIO* serverBindBio = NULL;
  40805. BIO* serverAcceptBio = NULL;
  40806. SSL* sslServer = NULL;
  40807. SSL_CTX* ctx = NULL;
  40808. func_args args;
  40809. THREAD_TYPE thread;
  40810. char port[10]; /* 10 bytes should be enough to store the string
  40811. * representation of the port */
  40812. ExpectIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  40813. ExpectNotNull(serverBindBio = BIO_new_accept(port));
  40814. /* First BIO_do_accept binds the port */
  40815. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  40816. XMEMSET(&args, 0, sizeof(func_args));
  40817. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  40818. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  40819. /* Let's plug it into SSL to test */
  40820. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  40821. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  40822. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  40823. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  40824. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  40825. ExpectNotNull(sslServer = SSL_new(ctx));
  40826. ExpectNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  40827. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  40828. ExpectIntEQ(SSL_accept(sslServer), 1);
  40829. join_thread(thread);
  40830. BIO_free(serverBindBio);
  40831. SSL_free(sslServer);
  40832. SSL_CTX_free(ctx);
  40833. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  40834. wc_ecc_fp_free(); /* free per thread cache */
  40835. #endif
  40836. #endif
  40837. return EXPECT_RESULT();
  40838. }
  40839. static int test_wolfSSL_BIO_write(void)
  40840. {
  40841. EXPECT_DECLS;
  40842. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  40843. BIO* bio = NULL;
  40844. BIO* bio64 = NULL;
  40845. BIO* bio_mem = NULL;
  40846. BIO* ptr = NULL;
  40847. int sz;
  40848. char msg[] = "conversion test";
  40849. char out[40];
  40850. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  40851. void* bufPtr = NULL;
  40852. BUF_MEM* buf = NULL;
  40853. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  40854. ExpectNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  40855. if (EXPECT_FAIL()) {
  40856. BIO_free(bio64);
  40857. }
  40858. /* now should convert to base64 then write to memory */
  40859. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40860. BIO_flush(bio);
  40861. /* test BIO chain */
  40862. ExpectIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  40863. ExpectNotNull(buf);
  40864. ExpectIntEQ(buf->length, 25);
  40865. ExpectIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  40866. ExpectPtrEq(buf->data, bufPtr);
  40867. ExpectNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  40868. sz = sizeof(out);
  40869. XMEMSET(out, 0, sz);
  40870. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  40871. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  40872. /* write then read should return the same message */
  40873. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40874. sz = sizeof(out);
  40875. XMEMSET(out, 0, sz);
  40876. ExpectIntEQ(BIO_read(bio, out, sz), 16);
  40877. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  40878. /* now try encoding with no line ending */
  40879. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  40880. #ifdef HAVE_EX_DATA
  40881. BIO_set_ex_data(bio64, 0, (void*) "data");
  40882. ExpectIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  40883. #endif
  40884. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40885. BIO_flush(bio);
  40886. sz = sizeof(out);
  40887. XMEMSET(out, 0, sz);
  40888. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  40889. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  40890. BIO_free_all(bio); /* frees bio64 also */
  40891. bio = NULL;
  40892. /* test with more than one bio64 in list */
  40893. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  40894. ExpectNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  40895. if (EXPECT_FAIL()) {
  40896. BIO_free(bio64);
  40897. bio64 = NULL;
  40898. }
  40899. ExpectNotNull(bio_mem = BIO_new(BIO_s_mem()));
  40900. ExpectNotNull(BIO_push(bio64, bio_mem));
  40901. if (EXPECT_FAIL()) {
  40902. BIO_free(bio_mem);
  40903. }
  40904. /* now should convert to base64 when stored and then decode with read */
  40905. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  40906. BIO_flush(bio);
  40907. sz = sizeof(out);
  40908. XMEMSET(out, 0, sz);
  40909. ExpectIntEQ((sz = BIO_read(bio, out, sz)), 16);
  40910. ExpectIntEQ(XMEMCMP(out, msg, sz), 0);
  40911. BIO_clear_flags(bio64, ~0);
  40912. BIO_set_retry_read(bio);
  40913. BIO_free_all(bio); /* frees bio64s also */
  40914. bio = NULL;
  40915. ExpectNotNull(bio = BIO_new_mem_buf(out, 0));
  40916. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40917. BIO_free(bio);
  40918. #endif
  40919. return EXPECT_RESULT();
  40920. }
  40921. static int test_wolfSSL_BIO_printf(void)
  40922. {
  40923. EXPECT_DECLS;
  40924. #if defined(OPENSSL_ALL)
  40925. BIO* bio = NULL;
  40926. int sz = 7;
  40927. char msg[] = "TLS 1.3 for the world";
  40928. char out[60];
  40929. char expected[] = "TLS 1.3 for the world : sz = 7";
  40930. XMEMSET(out, 0, sizeof(out));
  40931. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40932. ExpectIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  40933. ExpectIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  40934. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  40935. ExpectIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  40936. BIO_free(bio);
  40937. #endif
  40938. return EXPECT_RESULT();
  40939. }
  40940. static int test_wolfSSL_BIO_f_md(void)
  40941. {
  40942. EXPECT_DECLS;
  40943. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  40944. BIO* bio = NULL;
  40945. BIO* mem = NULL;
  40946. char msg[] = "message to hash";
  40947. char out[60];
  40948. EVP_MD_CTX* ctx = NULL;
  40949. const unsigned char testKey[] =
  40950. {
  40951. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  40952. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  40953. 0x0b, 0x0b, 0x0b, 0x0b
  40954. };
  40955. const char testData[] = "Hi There";
  40956. const unsigned char testResult[] =
  40957. {
  40958. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  40959. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  40960. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  40961. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  40962. };
  40963. const unsigned char expectedHash[] =
  40964. {
  40965. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  40966. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  40967. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  40968. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  40969. };
  40970. const unsigned char emptyHash[] =
  40971. {
  40972. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  40973. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  40974. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  40975. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  40976. };
  40977. unsigned char check[sizeof(testResult) + 1];
  40978. size_t checkSz = -1;
  40979. EVP_PKEY* key = NULL;
  40980. XMEMSET(out, 0, sizeof(out));
  40981. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  40982. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  40983. ExpectIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  40984. ExpectIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  40985. /* should not be able to write/read yet since just digest wrapper and no
  40986. * data is passing through the bio */
  40987. ExpectIntEQ(BIO_write(bio, msg, 0), 0);
  40988. ExpectIntEQ(BIO_pending(bio), 0);
  40989. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  40990. ExpectIntEQ(BIO_gets(bio, out, 3), 0);
  40991. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  40992. ExpectIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  40993. BIO_reset(bio);
  40994. /* append BIO mem to bio in order to read/write */
  40995. ExpectNotNull(bio = BIO_push(bio, mem));
  40996. XMEMSET(out, 0, sizeof(out));
  40997. ExpectIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  40998. ExpectIntEQ(BIO_pending(bio), 16);
  40999. /* this just reads the message and does not hash it (gets calls final) */
  41000. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  41001. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  41002. /* create a message digest using BIO */
  41003. XMEMSET(out, 0, sizeof(out));
  41004. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  41005. ExpectIntEQ(BIO_pending(mem), 16);
  41006. ExpectIntEQ(BIO_pending(bio), 16);
  41007. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  41008. ExpectIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  41009. BIO_free(bio);
  41010. bio = NULL;
  41011. BIO_free(mem);
  41012. mem = NULL;
  41013. /* test with HMAC */
  41014. XMEMSET(out, 0, sizeof(out));
  41015. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  41016. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  41017. BIO_get_md_ctx(bio, &ctx);
  41018. ExpectNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, testKey,
  41019. (int)sizeof(testKey)));
  41020. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  41021. ExpectNotNull(bio = BIO_push(bio, mem));
  41022. BIO_write(bio, testData, (int)strlen(testData));
  41023. ExpectIntEQ(EVP_DigestSignFinal(ctx, NULL, &checkSz), 1);
  41024. ExpectIntEQ(EVP_DigestSignFinal(ctx, check, &checkSz), 1);
  41025. ExpectIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  41026. EVP_PKEY_free(key);
  41027. BIO_free(bio);
  41028. BIO_free(mem);
  41029. #endif
  41030. return EXPECT_RESULT();
  41031. }
  41032. static int test_wolfSSL_BIO_up_ref(void)
  41033. {
  41034. EXPECT_DECLS;
  41035. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  41036. BIO* bio = NULL;
  41037. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  41038. ExpectIntEQ(BIO_up_ref(NULL), 0);
  41039. ExpectIntEQ(BIO_up_ref(bio), 1);
  41040. BIO_free(bio);
  41041. ExpectIntEQ(BIO_up_ref(bio), 1);
  41042. BIO_free(bio);
  41043. BIO_free(bio);
  41044. #endif
  41045. return EXPECT_RESULT();
  41046. }
  41047. static int test_wolfSSL_BIO_reset(void)
  41048. {
  41049. EXPECT_DECLS;
  41050. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  41051. BIO* bio = NULL;
  41052. byte buf[16];
  41053. ExpectNotNull(bio = BIO_new_mem_buf("secure your data",
  41054. (word32)XSTRLEN("secure your data")));
  41055. ExpectIntEQ(BIO_read(bio, buf, 6), 6);
  41056. ExpectIntEQ(XMEMCMP(buf, "secure", 6), 0);
  41057. XMEMSET(buf, 0, 16);
  41058. ExpectIntEQ(BIO_read(bio, buf, 16), 10);
  41059. ExpectIntEQ(XMEMCMP(buf, " your data", 10), 0);
  41060. /* You cannot write to MEM BIO with read-only mode. */
  41061. ExpectIntEQ(BIO_write(bio, "WriteToReadonly", 15), 0);
  41062. ExpectIntEQ(BIO_read(bio, buf, 16), -1);
  41063. XMEMSET(buf, 0, 16);
  41064. ExpectIntEQ(BIO_reset(bio), 0);
  41065. ExpectIntEQ(BIO_read(bio, buf, 16), 16);
  41066. ExpectIntEQ(XMEMCMP(buf, "secure your data", 16), 0);
  41067. BIO_free(bio);
  41068. #endif
  41069. return EXPECT_RESULT();
  41070. }
  41071. #endif /* !NO_BIO */
  41072. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  41073. /* test that the callback arg is correct */
  41074. static int certCbArg = 0;
  41075. static int certCb(WOLFSSL* ssl, void* arg)
  41076. {
  41077. if (ssl == NULL || arg != &certCbArg)
  41078. return 0;
  41079. if (wolfSSL_is_server(ssl)) {
  41080. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  41081. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41082. return 0;
  41083. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  41084. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41085. return 0;
  41086. }
  41087. else {
  41088. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  41089. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41090. return 0;
  41091. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  41092. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41093. return 0;
  41094. }
  41095. return 1;
  41096. }
  41097. static int certSetupCb(WOLFSSL_CTX* ctx)
  41098. {
  41099. SSL_CTX_set_cert_cb(ctx, certCb, &certCbArg);
  41100. return TEST_SUCCESS;
  41101. }
  41102. /**
  41103. * This is only done because test_wolfSSL_client_server_nofail_memio has no way
  41104. * to stop certificate and key loading
  41105. */
  41106. static int certClearCb(WOLFSSL* ssl)
  41107. {
  41108. /* Clear the loaded certs to force the callbacks to set them up */
  41109. SSL_certs_clear(ssl);
  41110. return TEST_SUCCESS;
  41111. }
  41112. #endif
  41113. static int test_wolfSSL_cert_cb(void)
  41114. {
  41115. EXPECT_DECLS;
  41116. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  41117. test_ssl_cbf func_cb_client;
  41118. test_ssl_cbf func_cb_server;
  41119. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  41120. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  41121. func_cb_client.ctx_ready = certSetupCb;
  41122. func_cb_client.ssl_ready = certClearCb;
  41123. func_cb_server.ctx_ready = certSetupCb;
  41124. func_cb_server.ssl_ready = certClearCb;
  41125. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  41126. &func_cb_server, NULL), TEST_SUCCESS);
  41127. #endif
  41128. return EXPECT_RESULT();
  41129. }
  41130. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  41131. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_cipher = NULL;
  41132. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs = NULL;
  41133. static int test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready(WOLFSSL_CTX* ctx)
  41134. {
  41135. EXPECT_DECLS;
  41136. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx,
  41137. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher), WOLFSSL_SUCCESS);
  41138. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  41139. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs), WOLFSSL_SUCCESS);
  41140. return EXPECT_RESULT();
  41141. }
  41142. static int test_wolfSSL_cert_cb_dyn_ciphers_certCB(WOLFSSL* ssl, void* arg)
  41143. {
  41144. const byte* suites = NULL;
  41145. word16 suiteSz = 0;
  41146. const byte* hashSigAlgo = NULL;
  41147. word16 hashSigAlgoSz = 0;
  41148. word16 idx = 0;
  41149. int haveRSA = 0;
  41150. int haveECC = 0;
  41151. (void)arg;
  41152. if (wolfSSL_get_client_suites_sigalgs(ssl, &suites, &suiteSz, &hashSigAlgo,
  41153. &hashSigAlgoSz) != WOLFSSL_SUCCESS)
  41154. return 0;
  41155. if (suites == NULL || suiteSz == 0 || hashSigAlgo == NULL ||
  41156. hashSigAlgoSz == 0)
  41157. return 0;
  41158. for (idx = 0; idx < suiteSz; idx += 2) {
  41159. WOLFSSL_CIPHERSUITE_INFO info =
  41160. wolfSSL_get_ciphersuite_info(suites[idx], suites[idx+1]);
  41161. if (info.rsaAuth)
  41162. haveRSA = 1;
  41163. else if (info.eccAuth)
  41164. haveECC = 1;
  41165. }
  41166. if (hashSigAlgoSz > 0) {
  41167. /* sigalgs extension takes precedence over ciphersuites */
  41168. haveRSA = 0;
  41169. haveECC = 0;
  41170. }
  41171. for (idx = 0; idx < hashSigAlgoSz; idx += 2) {
  41172. int hashAlgo = 0;
  41173. int sigAlgo = 0;
  41174. if (wolfSSL_get_sigalg_info(hashSigAlgo[idx+0], hashSigAlgo[idx+1],
  41175. &hashAlgo, &sigAlgo) != 0)
  41176. return 0;
  41177. if (sigAlgo == RSAk || sigAlgo == RSAPSSk)
  41178. haveRSA = 1;
  41179. else if (sigAlgo == ECDSAk)
  41180. haveECC = 1;
  41181. }
  41182. if (haveRSA) {
  41183. if (wolfSSL_use_certificate_file(ssl, svrCertFile, WOLFSSL_FILETYPE_PEM)
  41184. != WOLFSSL_SUCCESS)
  41185. return 0;
  41186. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile, WOLFSSL_FILETYPE_PEM)
  41187. != WOLFSSL_SUCCESS)
  41188. return 0;
  41189. }
  41190. else if (haveECC) {
  41191. if (wolfSSL_use_certificate_file(ssl, eccCertFile, WOLFSSL_FILETYPE_PEM)
  41192. != WOLFSSL_SUCCESS)
  41193. return 0;
  41194. if (wolfSSL_use_PrivateKey_file(ssl, eccKeyFile, WOLFSSL_FILETYPE_PEM)
  41195. != WOLFSSL_SUCCESS)
  41196. return 0;
  41197. }
  41198. return 1;
  41199. }
  41200. static int test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready(WOLFSSL_CTX* ctx)
  41201. {
  41202. SSL_CTX_set_cert_cb(ctx, test_wolfSSL_cert_cb_dyn_ciphers_certCB, NULL);
  41203. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, NULL);
  41204. return TEST_SUCCESS;
  41205. }
  41206. #endif
  41207. /* Testing dynamic ciphers offered by client */
  41208. static int test_wolfSSL_cert_cb_dyn_ciphers(void)
  41209. {
  41210. EXPECT_DECLS;
  41211. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  41212. test_ssl_cbf func_cb_client;
  41213. test_ssl_cbf func_cb_server;
  41214. struct {
  41215. method_provider client_meth;
  41216. const char* client_ciphers;
  41217. const char* client_sigalgs;
  41218. const char* client_ca;
  41219. method_provider server_meth;
  41220. } test_params[] = {
  41221. #if !defined(NO_SHA256) && defined(HAVE_AESGCM)
  41222. #ifdef WOLFSSL_TLS13
  41223. #if !defined(NO_RSA) && defined(WC_RSA_PSS)
  41224. {wolfTLSv1_3_client_method,
  41225. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  41226. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_3_server_method},
  41227. #endif
  41228. #ifdef HAVE_ECC
  41229. {wolfTLSv1_3_client_method,
  41230. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  41231. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_3_server_method},
  41232. #endif
  41233. #endif
  41234. #ifndef WOLFSSL_NO_TLS12
  41235. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_DH)
  41236. {wolfTLSv1_2_client_method,
  41237. "DHE-RSA-AES128-GCM-SHA256",
  41238. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_2_server_method},
  41239. #endif
  41240. #ifdef HAVE_ECC
  41241. {wolfTLSv1_2_client_method,
  41242. "ECDHE-ECDSA-AES128-GCM-SHA256",
  41243. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_2_server_method},
  41244. #endif
  41245. #endif
  41246. #endif
  41247. };
  41248. size_t i;
  41249. size_t testCount = sizeof(test_params)/sizeof(*test_params);
  41250. if (testCount > 0) {
  41251. for (i = 0; i < testCount; i++) {
  41252. printf("\tTesting %s ciphers with %s sigalgs\n",
  41253. test_params[i].client_ciphers,
  41254. test_params[i].client_sigalgs);
  41255. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  41256. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  41257. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher =
  41258. test_params[i].client_ciphers;
  41259. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs =
  41260. test_params[i].client_sigalgs;
  41261. func_cb_client.method = test_params[i].client_meth;
  41262. func_cb_client.caPemFile = test_params[i].client_ca;
  41263. func_cb_client.ctx_ready =
  41264. test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready;
  41265. func_cb_server.ctx_ready =
  41266. test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready;
  41267. func_cb_server.ssl_ready = certClearCb; /* Reuse from prev test */
  41268. func_cb_server.method = test_params[i].server_meth;
  41269. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  41270. &func_cb_server, NULL), TEST_SUCCESS);
  41271. }
  41272. }
  41273. #endif
  41274. return EXPECT_RESULT();
  41275. }
  41276. static int test_wolfSSL_ciphersuite_auth(void)
  41277. {
  41278. EXPECT_DECLS;
  41279. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  41280. WOLFSSL_CIPHERSUITE_INFO info;
  41281. (void)info;
  41282. #ifndef WOLFSSL_NO_TLS12
  41283. #ifdef HAVE_CHACHA
  41284. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  41285. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256);
  41286. ExpectIntEQ(info.rsaAuth, 1);
  41287. ExpectIntEQ(info.eccAuth, 0);
  41288. ExpectIntEQ(info.eccStatic, 0);
  41289. ExpectIntEQ(info.psk, 0);
  41290. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  41291. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256);
  41292. ExpectIntEQ(info.rsaAuth, 0);
  41293. ExpectIntEQ(info.eccAuth, 1);
  41294. ExpectIntEQ(info.eccStatic, 0);
  41295. ExpectIntEQ(info.psk, 0);
  41296. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  41297. TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256);
  41298. ExpectIntEQ(info.rsaAuth, 0);
  41299. ExpectIntEQ(info.eccAuth, 0);
  41300. ExpectIntEQ(info.eccStatic, 0);
  41301. ExpectIntEQ(info.psk, 1);
  41302. #endif
  41303. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  41304. #ifndef NO_RSA
  41305. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  41306. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA);
  41307. ExpectIntEQ(info.rsaAuth, 1);
  41308. ExpectIntEQ(info.eccAuth, 0);
  41309. ExpectIntEQ(info.eccStatic, 0);
  41310. ExpectIntEQ(info.psk, 0);
  41311. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  41312. TLS_ECDH_RSA_WITH_AES_128_CBC_SHA);
  41313. ExpectIntEQ(info.rsaAuth, 1);
  41314. ExpectIntEQ(info.eccAuth, 0);
  41315. ExpectIntEQ(info.eccStatic, 1);
  41316. ExpectIntEQ(info.psk, 0);
  41317. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  41318. TLS_ECDH_RSA_WITH_AES_256_CBC_SHA);
  41319. ExpectIntEQ(info.rsaAuth, 1);
  41320. ExpectIntEQ(info.eccAuth, 0);
  41321. ExpectIntEQ(info.eccStatic, 1);
  41322. ExpectIntEQ(info.psk, 0);
  41323. #endif
  41324. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  41325. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA);
  41326. ExpectIntEQ(info.rsaAuth, 0);
  41327. ExpectIntEQ(info.eccAuth, 1);
  41328. ExpectIntEQ(info.eccStatic, 0);
  41329. ExpectIntEQ(info.psk, 0);
  41330. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  41331. TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA);
  41332. ExpectIntEQ(info.rsaAuth, 0);
  41333. ExpectIntEQ(info.eccAuth, 1);
  41334. ExpectIntEQ(info.eccStatic, 1);
  41335. ExpectIntEQ(info.psk, 0);
  41336. info = wolfSSL_get_ciphersuite_info(ECDHE_PSK_BYTE,
  41337. TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256);
  41338. ExpectIntEQ(info.rsaAuth, 0);
  41339. ExpectIntEQ(info.eccAuth, 0);
  41340. ExpectIntEQ(info.eccStatic, 0);
  41341. ExpectIntEQ(info.psk, 1);
  41342. #endif
  41343. #endif
  41344. #ifdef WOLFSSL_TLS13
  41345. info = wolfSSL_get_ciphersuite_info(TLS13_BYTE,
  41346. TLS_AES_128_GCM_SHA256);
  41347. ExpectIntEQ(info.rsaAuth, 0);
  41348. ExpectIntEQ(info.eccAuth, 0);
  41349. ExpectIntEQ(info.eccStatic, 0);
  41350. ExpectIntEQ(info.psk, 0);
  41351. #endif
  41352. #endif
  41353. return EXPECT_RESULT();
  41354. }
  41355. static int test_wolfSSL_sigalg_info(void)
  41356. {
  41357. EXPECT_DECLS;
  41358. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  41359. byte hashSigAlgo[WOLFSSL_MAX_SIGALGO];
  41360. word16 len = 0;
  41361. word16 idx = 0;
  41362. int allSigAlgs = SIG_ECDSA | SIG_RSA | SIG_SM2 | SIG_FALCON | SIG_DILITHIUM;
  41363. InitSuitesHashSigAlgo_ex2(hashSigAlgo, allSigAlgs, 1, 0xFFFFFFFF, &len);
  41364. for (idx = 0; idx < len; idx += 2) {
  41365. int hashAlgo = 0;
  41366. int sigAlgo = 0;
  41367. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  41368. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  41369. ExpectIntNE(hashAlgo, 0);
  41370. ExpectIntNE(sigAlgo, 0);
  41371. }
  41372. InitSuitesHashSigAlgo_ex2(hashSigAlgo, allSigAlgs | SIG_ANON, 1,
  41373. 0xFFFFFFFF, &len);
  41374. for (idx = 0; idx < len; idx += 2) {
  41375. int hashAlgo = 0;
  41376. int sigAlgo = 0;
  41377. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  41378. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  41379. ExpectIntNE(hashAlgo, 0);
  41380. }
  41381. #endif
  41382. return EXPECT_RESULT();
  41383. }
  41384. static int test_wolfSSL_SESSION(void)
  41385. {
  41386. EXPECT_DECLS;
  41387. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41388. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  41389. !defined(NO_SESSION_CACHE)
  41390. WOLFSSL* ssl = NULL;
  41391. WOLFSSL_CTX* ctx = NULL;
  41392. WOLFSSL_SESSION* sess = NULL;
  41393. WOLFSSL_SESSION* sess_copy = NULL;
  41394. #ifdef OPENSSL_EXTRA
  41395. #ifdef HAVE_EXT_CACHE
  41396. unsigned char* sessDer = NULL;
  41397. unsigned char* ptr = NULL;
  41398. int sz = 0;
  41399. #endif
  41400. const unsigned char context[] = "user app context";
  41401. unsigned int contextSz = (unsigned int)sizeof(context);
  41402. #endif
  41403. int ret = 0, err = 0;
  41404. SOCKET_T sockfd;
  41405. tcp_ready ready;
  41406. func_args server_args;
  41407. THREAD_TYPE serverThread;
  41408. char msg[80];
  41409. const char* sendGET = "GET";
  41410. /* TLS v1.3 requires session tickets */
  41411. /* CHACHA and POLY1305 required for myTicketEncCb */
  41412. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  41413. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  41414. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  41415. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  41416. #else
  41417. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41418. #endif
  41419. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  41420. WOLFSSL_FILETYPE_PEM));
  41421. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  41422. WOLFSSL_FILETYPE_PEM));
  41423. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  41424. WOLFSSL_SUCCESS);
  41425. #ifdef WOLFSSL_ENCRYPTED_KEYS
  41426. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  41427. #endif
  41428. #ifdef HAVE_SESSION_TICKET
  41429. /* Use session tickets, for ticket tests below */
  41430. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  41431. #endif
  41432. XMEMSET(&server_args, 0, sizeof(func_args));
  41433. #ifdef WOLFSSL_TIRTOS
  41434. fdOpenSession(Task_self());
  41435. #endif
  41436. StartTCP();
  41437. InitTcpReady(&ready);
  41438. #if defined(USE_WINDOWS_API)
  41439. /* use RNG to get random port if using windows */
  41440. ready.port = GetRandomPort();
  41441. #endif
  41442. server_args.signal = &ready;
  41443. start_thread(test_server_nofail, &server_args, &serverThread);
  41444. wait_tcp_ready(&server_args);
  41445. /* client connection */
  41446. ExpectNotNull(ssl = wolfSSL_new(ctx));
  41447. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  41448. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  41449. #ifdef WOLFSSL_ASYNC_CRYPT
  41450. err = 0; /* Reset error */
  41451. #endif
  41452. do {
  41453. #ifdef WOLFSSL_ASYNC_CRYPT
  41454. if (err == WC_PENDING_E) {
  41455. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  41456. if (ret < 0) { break; } else if (ret == 0) { continue; }
  41457. }
  41458. #endif
  41459. ret = wolfSSL_connect(ssl);
  41460. err = wolfSSL_get_error(ssl, 0);
  41461. } while (err == WC_PENDING_E);
  41462. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  41463. #ifdef WOLFSSL_ASYNC_CRYPT
  41464. err = 0; /* Reset error */
  41465. #endif
  41466. do {
  41467. #ifdef WOLFSSL_ASYNC_CRYPT
  41468. if (err == WC_PENDING_E) {
  41469. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  41470. if (ret < 0) { break; } else if (ret == 0) { continue; }
  41471. }
  41472. #endif
  41473. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  41474. err = wolfSSL_get_error(ssl, 0);
  41475. } while (err == WC_PENDING_E);
  41476. ExpectIntEQ(ret, (int)XSTRLEN(sendGET));
  41477. #ifdef WOLFSSL_ASYNC_CRYPT
  41478. err = 0; /* Reset error */
  41479. #endif
  41480. do {
  41481. #ifdef WOLFSSL_ASYNC_CRYPT
  41482. if (err == WC_PENDING_E) {
  41483. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  41484. if (ret < 0) { break; } else if (ret == 0) { continue; }
  41485. }
  41486. #endif
  41487. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  41488. err = wolfSSL_get_error(ssl, 0);
  41489. } while (err == WC_PENDING_E);
  41490. ExpectIntEQ(ret, 23);
  41491. ExpectPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  41492. ExpectPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  41493. #ifdef HAVE_EXT_CACHE
  41494. ExpectPtrEq(sess, sess_copy); /* they should be the same pointer but without
  41495. * HAVE_EXT_CACHE we get new objects each time */
  41496. #endif
  41497. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  41498. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  41499. sess = wolfSSL_get_session(ssl);
  41500. #ifdef OPENSSL_EXTRA
  41501. ExpectIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  41502. ExpectIntEQ(SSL_SESSION_is_resumable(sess), 1);
  41503. ExpectIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  41504. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  41505. #ifdef HAVE_SESSION_TICKET
  41506. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  41507. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  41508. SESSION_TICKET_HINT_DEFAULT);
  41509. #else
  41510. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  41511. #endif
  41512. #else
  41513. (void)sess;
  41514. #endif /* OPENSSL_EXTRA */
  41515. /* Retain copy of the session for later testing */
  41516. ExpectNotNull(sess = wolfSSL_get1_session(ssl));
  41517. wolfSSL_shutdown(ssl);
  41518. wolfSSL_free(ssl); ssl = NULL;
  41519. CloseSocket(sockfd);
  41520. join_thread(serverThread);
  41521. FreeTcpReady(&ready);
  41522. #ifdef WOLFSSL_TIRTOS
  41523. fdOpenSession(Task_self());
  41524. #endif
  41525. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  41526. {
  41527. X509 *x509 = NULL;
  41528. char buf[30];
  41529. int bufSz = 0;
  41530. ExpectNotNull(x509 = SSL_SESSION_get0_peer(sess));
  41531. ExpectIntGT((bufSz = X509_NAME_get_text_by_NID(
  41532. X509_get_subject_name(x509), NID_organizationalUnitName, buf,
  41533. sizeof(buf))), 0);
  41534. ExpectIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  41535. if (bufSz == 7) {
  41536. ExpectIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  41537. }
  41538. if (bufSz == 16) {
  41539. ExpectIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  41540. }
  41541. }
  41542. #endif
  41543. #ifdef HAVE_EXT_CACHE
  41544. ExpectNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  41545. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  41546. sess_copy = NULL;
  41547. #endif
  41548. #if defined(OPENSSL_EXTRA) && defined(HAVE_EXT_CACHE)
  41549. /* get session from DER and update the timeout */
  41550. ExpectIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  41551. ExpectIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  41552. wolfSSL_SESSION_free(sess); sess = NULL;
  41553. sess = NULL;
  41554. ptr = sessDer;
  41555. ExpectNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  41556. ExpectNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  41557. (const unsigned char**)&ptr, sz));
  41558. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  41559. sessDer = NULL;
  41560. ExpectIntGT(wolfSSL_SESSION_get_time(sess), 0);
  41561. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  41562. #endif
  41563. /* successful set session test */
  41564. ExpectNotNull(ssl = wolfSSL_new(ctx));
  41565. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  41566. #ifdef HAVE_SESSION_TICKET
  41567. /* Test set/get session ticket */
  41568. {
  41569. const char* ticket = "This is a session ticket";
  41570. char buf[64] = {0};
  41571. word32 bufSz = (word32)sizeof(buf);
  41572. ExpectIntEQ(SSL_SUCCESS,
  41573. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  41574. (word32)XSTRLEN(ticket)));
  41575. ExpectIntEQ(SSL_SUCCESS,
  41576. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  41577. ExpectStrEQ(ticket, buf);
  41578. }
  41579. #endif
  41580. #ifdef OPENSSL_EXTRA
  41581. /* session timeout case */
  41582. /* make the session to be expired */
  41583. ExpectIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  41584. XSLEEP_MS(1200);
  41585. /* SSL_set_session should reject specified session but return success
  41586. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  41587. */
  41588. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  41589. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  41590. #else
  41591. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  41592. #endif
  41593. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  41594. #ifdef WOLFSSL_SESSION_ID_CTX
  41595. /* fail case with miss match session context IDs (use compatibility API) */
  41596. ExpectIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  41597. SSL_SUCCESS);
  41598. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  41599. wolfSSL_free(ssl); ssl = NULL;
  41600. ExpectIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  41601. SSL_FAILURE);
  41602. ExpectIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  41603. SSL_SUCCESS);
  41604. ExpectNotNull(ssl = wolfSSL_new(ctx));
  41605. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  41606. #endif
  41607. #endif /* OPENSSL_EXTRA */
  41608. wolfSSL_free(ssl);
  41609. wolfSSL_SESSION_free(sess);
  41610. wolfSSL_CTX_free(ctx);
  41611. #endif
  41612. return EXPECT_RESULT();
  41613. }
  41614. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41615. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  41616. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  41617. !defined(WOLFSSL_NO_TLS12)
  41618. static WOLFSSL_SESSION* test_wolfSSL_SESSION_expire_sess = NULL;
  41619. static void test_wolfSSL_SESSION_expire_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  41620. {
  41621. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  41622. /* returns previous timeout value */
  41623. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), 500);
  41624. #else
  41625. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), WOLFSSL_SUCCESS);
  41626. #endif
  41627. }
  41628. /* set the session to timeout in a second */
  41629. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready(WOLFSSL* ssl)
  41630. {
  41631. AssertIntEQ(wolfSSL_set_timeout(ssl, 2), 1);
  41632. }
  41633. /* store the client side session from the first successful connection */
  41634. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result(WOLFSSL* ssl)
  41635. {
  41636. AssertPtrNE((test_wolfSSL_SESSION_expire_sess = wolfSSL_get1_session(ssl)),
  41637. NULL); /* ref count 1 */
  41638. }
  41639. /* wait till session is expired then set it in the WOLFSSL struct for use */
  41640. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait(WOLFSSL* ssl)
  41641. {
  41642. AssertIntEQ(wolfSSL_set_timeout(ssl, 1), 1);
  41643. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  41644. WOLFSSL_SUCCESS);
  41645. XSLEEP_MS(2000); /* wait 2 seconds for session to expire */
  41646. }
  41647. /* set expired session in the WOLFSSL struct for use */
  41648. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set(WOLFSSL* ssl)
  41649. {
  41650. XSLEEP_MS(1200); /* wait a second for session to expire */
  41651. /* set the expired session, call to set session fails but continuing on
  41652. after failure should be handled here */
  41653. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL)
  41654. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  41655. WOLFSSL_SUCCESS);
  41656. #else
  41657. AssertIntNE(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  41658. WOLFSSL_SUCCESS);
  41659. #endif
  41660. }
  41661. /* check that the expired session was not reused */
  41662. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse(WOLFSSL* ssl)
  41663. {
  41664. /* since the session has expired it should not have been reused */
  41665. AssertIntEQ(wolfSSL_session_reused(ssl), 0);
  41666. }
  41667. #endif
  41668. static int test_wolfSSL_SESSION_expire_downgrade(void)
  41669. {
  41670. EXPECT_DECLS;
  41671. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41672. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  41673. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  41674. !defined(WOLFSSL_NO_TLS12)
  41675. WOLFSSL_CTX* ctx = NULL;
  41676. callback_functions server_cbf, client_cbf;
  41677. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  41678. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  41679. /* force server side to use TLS 1.2 */
  41680. server_cbf.ctx = ctx;
  41681. server_cbf.method = wolfTLSv1_2_server_method;
  41682. client_cbf.method = wolfSSLv23_client_method;
  41683. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  41684. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready;
  41685. client_cbf.on_result = test_wolfSSL_SESSION_expire_downgrade_ssl_result;
  41686. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  41687. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  41688. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  41689. /* set the previously created session and wait till expired */
  41690. server_cbf.ctx = ctx;
  41691. client_cbf.method = wolfSSLv23_client_method;
  41692. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  41693. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait;
  41694. client_cbf.on_result =
  41695. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  41696. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  41697. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  41698. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  41699. /* set the previously created expired session */
  41700. server_cbf.ctx = ctx;
  41701. client_cbf.method = wolfSSLv23_client_method;
  41702. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  41703. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set;
  41704. client_cbf.on_result =
  41705. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  41706. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  41707. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  41708. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  41709. wolfSSL_SESSION_free(test_wolfSSL_SESSION_expire_sess);
  41710. wolfSSL_CTX_free(ctx);
  41711. #endif
  41712. return EXPECT_RESULT();
  41713. }
  41714. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  41715. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  41716. static int clientSessRemCountMalloc = 0;
  41717. static int serverSessRemCountMalloc = 0;
  41718. static int clientSessRemCountFree = 0;
  41719. static int serverSessRemCountFree = 0;
  41720. static WOLFSSL_CTX* serverSessCtx = NULL;
  41721. static WOLFSSL_SESSION* serverSess = NULL;
  41722. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  41723. !defined(NO_SESSION_CACHE_REF)
  41724. static WOLFSSL_CTX* clientSessCtx = NULL;
  41725. static WOLFSSL_SESSION* clientSess = NULL;
  41726. #endif
  41727. static int serverSessRemIdx = 3;
  41728. static int sessRemCtx_Server = WOLFSSL_SERVER_END;
  41729. static int sessRemCtx_Client = WOLFSSL_CLIENT_END;
  41730. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  41731. {
  41732. int* side;
  41733. (void)ctx;
  41734. side = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  41735. if (side != NULL) {
  41736. if (*side == WOLFSSL_CLIENT_END)
  41737. clientSessRemCountFree++;
  41738. else
  41739. serverSessRemCountFree++;
  41740. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  41741. }
  41742. }
  41743. static int SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  41744. {
  41745. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  41746. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  41747. !defined(NO_SESSION_CACHE_REF)
  41748. {
  41749. EXPECT_DECLS;
  41750. /* Allow downgrade, set min version, and disable TLS 1.3.
  41751. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  41752. * reference to the session cache. But with WOLFSSL_TLS13 and without
  41753. * HAVE_SESSION_TICKET we won't have a session ID to be able to place
  41754. * the session in the cache. In this case we need to downgrade to
  41755. * previous versions to just use the legacy session ID field. */
  41756. ExpectIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  41757. SSL_SUCCESS);
  41758. ExpectIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  41759. SSL_SUCCESS);
  41760. return EXPECT_RESULT();
  41761. }
  41762. #else
  41763. return TEST_SUCCESS;
  41764. #endif
  41765. }
  41766. static int SessRemSslSetupCb(WOLFSSL* ssl)
  41767. {
  41768. EXPECT_DECLS;
  41769. int* side;
  41770. if (SSL_is_server(ssl)) {
  41771. side = &sessRemCtx_Server;
  41772. serverSessRemCountMalloc++;
  41773. ExpectNotNull(serverSess = SSL_get1_session(ssl));
  41774. ExpectIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  41775. SSL_SUCCESS);
  41776. }
  41777. else {
  41778. side = &sessRemCtx_Client;
  41779. clientSessRemCountMalloc++;
  41780. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  41781. !defined(NO_SESSION_CACHE_REF)
  41782. ExpectNotNull(clientSess = SSL_get1_session(ssl));
  41783. ExpectIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  41784. SSL_SUCCESS);
  41785. #endif
  41786. }
  41787. ExpectIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl),
  41788. serverSessRemIdx, side), SSL_SUCCESS);
  41789. return EXPECT_RESULT();
  41790. }
  41791. #endif
  41792. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  41793. {
  41794. EXPECT_DECLS;
  41795. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  41796. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  41797. /* Check that the remove callback gets called for external data in a
  41798. * session object */
  41799. test_ssl_cbf func_cb;
  41800. XMEMSET(&func_cb, 0, sizeof(func_cb));
  41801. func_cb.ctx_ready = SessRemCtxSetupCb;
  41802. func_cb.on_result = SessRemSslSetupCb;
  41803. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb, &func_cb,
  41804. NULL), TEST_SUCCESS);
  41805. /* Both should have been allocated */
  41806. ExpectIntEQ(clientSessRemCountMalloc, 1);
  41807. ExpectIntEQ(serverSessRemCountMalloc, 1);
  41808. /* This should not be called yet. Session wasn't evicted from cache yet. */
  41809. ExpectIntEQ(clientSessRemCountFree, 0);
  41810. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  41811. !defined(NO_SESSION_CACHE_REF)
  41812. /* Force a cache lookup */
  41813. ExpectNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  41814. /* Force a cache update */
  41815. ExpectNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  41816. /* This should set the timeout to 0 and call the remove callback from within
  41817. * the session cache. */
  41818. ExpectIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  41819. ExpectNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  41820. ExpectIntEQ(clientSessRemCountFree, 1);
  41821. #endif
  41822. /* Server session is in the cache so ex_data isn't free'd with the SSL
  41823. * object */
  41824. ExpectIntEQ(serverSessRemCountFree, 0);
  41825. /* Force a cache lookup */
  41826. ExpectNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  41827. /* Force a cache update */
  41828. ExpectNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  41829. /* This should set the timeout to 0 and call the remove callback from within
  41830. * the session cache. */
  41831. ExpectIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  41832. ExpectNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  41833. ExpectIntEQ(serverSessRemCountFree, 1);
  41834. /* Need to free the references that we kept */
  41835. SSL_CTX_free(serverSessCtx);
  41836. SSL_SESSION_free(serverSess);
  41837. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  41838. !defined(NO_SESSION_CACHE_REF)
  41839. SSL_CTX_free(clientSessCtx);
  41840. SSL_SESSION_free(clientSess);
  41841. #endif
  41842. #endif
  41843. return EXPECT_RESULT();
  41844. }
  41845. static int test_wolfSSL_ticket_keys(void)
  41846. {
  41847. EXPECT_DECLS;
  41848. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  41849. !defined(NO_WOLFSSL_SERVER)
  41850. WOLFSSL_CTX* ctx = NULL;
  41851. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  41852. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  41853. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  41854. WOLFSSL_FAILURE);
  41855. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  41856. WOLFSSL_FAILURE);
  41857. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  41858. WOLFSSL_FAILURE);
  41859. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  41860. WOLFSSL_FAILURE);
  41861. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  41862. WOLFSSL_FAILURE);
  41863. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  41864. WOLFSSL_FAILURE);
  41865. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  41866. WOLFSSL_FAILURE);
  41867. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  41868. WOLFSSL_FAILURE);
  41869. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  41870. WOLFSSL_FAILURE);
  41871. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  41872. WOLFSSL_FAILURE);
  41873. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  41874. WOLFSSL_FAILURE);
  41875. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  41876. WOLFSSL_FAILURE);
  41877. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  41878. WOLFSSL_FAILURE);
  41879. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  41880. WOLFSSL_FAILURE);
  41881. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  41882. WOLFSSL_SUCCESS);
  41883. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  41884. WOLFSSL_SUCCESS);
  41885. wolfSSL_CTX_free(ctx);
  41886. #endif
  41887. return EXPECT_RESULT();
  41888. }
  41889. #ifndef NO_BIO
  41890. static int test_wolfSSL_d2i_PUBKEY(void)
  41891. {
  41892. EXPECT_DECLS;
  41893. #if defined(OPENSSL_EXTRA)
  41894. BIO* bio = NULL;
  41895. EVP_PKEY* pkey = NULL;
  41896. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  41897. ExpectNull(d2i_PUBKEY_bio(NULL, NULL));
  41898. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  41899. /* RSA PUBKEY test */
  41900. ExpectIntGT(BIO_write(bio, client_keypub_der_2048,
  41901. sizeof_client_keypub_der_2048), 0);
  41902. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  41903. EVP_PKEY_free(pkey);
  41904. pkey = NULL;
  41905. #endif
  41906. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  41907. /* ECC PUBKEY test */
  41908. ExpectIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  41909. sizeof_ecc_clikeypub_der_256), 0);
  41910. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  41911. EVP_PKEY_free(pkey);
  41912. pkey = NULL;
  41913. #endif
  41914. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  41915. /* DSA PUBKEY test */
  41916. ExpectIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  41917. sizeof_dsa_pub_key_der_2048), 0);
  41918. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  41919. EVP_PKEY_free(pkey);
  41920. pkey = NULL;
  41921. #endif
  41922. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  41923. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  41924. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  41925. (HAVE_FIPS_VERSION > 2))
  41926. /* DH PUBKEY test */
  41927. ExpectIntGT(BIO_write(bio, dh_pub_key_der_2048,
  41928. sizeof_dh_pub_key_der_2048), 0);
  41929. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  41930. EVP_PKEY_free(pkey);
  41931. pkey = NULL;
  41932. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  41933. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  41934. BIO_free(bio);
  41935. (void)pkey;
  41936. #endif
  41937. return EXPECT_RESULT();
  41938. }
  41939. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  41940. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  41941. {
  41942. EXPECT_DECLS;
  41943. BIO* bio = NULL;
  41944. EVP_PKEY* pkey = NULL;
  41945. WOLFSSL_CTX* ctx = NULL;
  41946. #if defined(WOLFSSL_KEY_GEN)
  41947. unsigned char buff[4096];
  41948. unsigned char* bufPtr = buff;
  41949. #endif
  41950. /* test creating new EVP_PKEY with bad arg */
  41951. ExpectNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  41952. /* test loading RSA key using BIO */
  41953. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  41954. {
  41955. XFILE file = XBADFILE;
  41956. const char* fname = "./certs/server-key.der";
  41957. size_t sz = 0;
  41958. byte* buf = NULL;
  41959. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  41960. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  41961. ExpectTrue((sz = XFTELL(file)) != 0);
  41962. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  41963. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  41964. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  41965. if (file != XBADFILE) {
  41966. XFCLOSE(file);
  41967. }
  41968. /* Test using BIO new mem and loading DER private key */
  41969. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  41970. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  41971. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  41972. BIO_free(bio);
  41973. bio = NULL;
  41974. EVP_PKEY_free(pkey);
  41975. pkey = NULL;
  41976. }
  41977. #endif
  41978. /* test loading ECC key using BIO */
  41979. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  41980. {
  41981. XFILE file = XBADFILE;
  41982. const char* fname = "./certs/ecc-key.der";
  41983. size_t sz = 0;
  41984. byte* buf = NULL;
  41985. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  41986. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  41987. ExpectTrue((sz = XFTELL(file)) != 0);
  41988. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  41989. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  41990. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  41991. if (file != XBADFILE)
  41992. XFCLOSE(file);
  41993. /* Test using BIO new mem and loading DER private key */
  41994. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  41995. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  41996. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  41997. BIO_free(bio);
  41998. bio = NULL;
  41999. EVP_PKEY_free(pkey);
  42000. pkey = NULL;
  42001. }
  42002. #endif
  42003. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  42004. #ifndef NO_WOLFSSL_SERVER
  42005. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  42006. #else
  42007. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  42008. #endif
  42009. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  42010. {
  42011. RSA* rsa = NULL;
  42012. /* Tests bad parameters */
  42013. ExpectNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  42014. /* RSA not set yet, expecting to fail*/
  42015. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  42016. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  42017. /* set RSA using bio*/
  42018. ExpectIntGT(BIO_write(bio, client_key_der_2048,
  42019. sizeof_client_key_der_2048), 0);
  42020. ExpectNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  42021. ExpectNotNull(rsa);
  42022. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  42023. /* i2d RSAprivate key tests */
  42024. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  42025. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  42026. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  42027. sizeof_client_key_der_2048);
  42028. bufPtr -= sizeof_client_key_der_2048;
  42029. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  42030. sizeof_client_key_der_2048), 0);
  42031. bufPtr = NULL;
  42032. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  42033. sizeof_client_key_der_2048);
  42034. ExpectNotNull(bufPtr);
  42035. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  42036. sizeof_client_key_der_2048), 0);
  42037. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  42038. RSA_free(rsa);
  42039. rsa = RSA_new();
  42040. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  42041. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  42042. RSA_free(rsa);
  42043. }
  42044. #endif /* WOLFSSL_KEY_GEN && !NO_RSA */
  42045. SSL_CTX_free(ctx);
  42046. ctx = NULL;
  42047. BIO_free(bio);
  42048. bio = NULL;
  42049. return EXPECT_RESULT();
  42050. }
  42051. #endif /* OPENSSL_ALL || (WOLFSSL_ASIO && !NO_RSA) */
  42052. #endif /* !NO_BIO */
  42053. static int test_wolfSSL_sk_GENERAL_NAME(void)
  42054. {
  42055. EXPECT_DECLS;
  42056. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  42057. !defined(NO_RSA)
  42058. X509* x509 = NULL;
  42059. GENERAL_NAME* gn = NULL;
  42060. unsigned char buf[4096];
  42061. const unsigned char* bufPt = NULL;
  42062. int bytes = 0;
  42063. int i;
  42064. int j;
  42065. XFILE f = XBADFILE;
  42066. STACK_OF(GENERAL_NAME)* sk = NULL;
  42067. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  42068. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  42069. if (f != XBADFILE)
  42070. XFCLOSE(f);
  42071. for (j = 0; j < 2; ++j) {
  42072. bufPt = buf;
  42073. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  42074. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  42075. NID_subject_alt_name, NULL, NULL));
  42076. ExpectIntEQ(sk_GENERAL_NAME_num(sk), 1);
  42077. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  42078. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  42079. if (gn != NULL) {
  42080. switch (gn->type) {
  42081. case GEN_DNS:
  42082. fprintf(stderr, "found type GEN_DNS\n");
  42083. break;
  42084. case GEN_EMAIL:
  42085. fprintf(stderr, "found type GEN_EMAIL\n");
  42086. break;
  42087. case GEN_URI:
  42088. fprintf(stderr, "found type GEN_URI\n");
  42089. break;
  42090. }
  42091. }
  42092. }
  42093. X509_free(x509);
  42094. x509 = NULL;
  42095. if (j == 0) {
  42096. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  42097. }
  42098. else {
  42099. /*
  42100. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  42101. * it was supposed to. This is a regression test for that bug.
  42102. */
  42103. GENERAL_NAMES_free(sk);
  42104. }
  42105. sk = NULL;
  42106. }
  42107. #endif
  42108. return EXPECT_RESULT();
  42109. }
  42110. static int test_wolfSSL_GENERAL_NAME_print(void)
  42111. {
  42112. EXPECT_DECLS;
  42113. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  42114. X509* x509 = NULL;
  42115. GENERAL_NAME* gn = NULL;
  42116. unsigned char buf[4096];
  42117. const unsigned char* bufPt = NULL;
  42118. int bytes;
  42119. XFILE f = XBADFILE;
  42120. STACK_OF(GENERAL_NAME)* sk = NULL;
  42121. BIO* out = NULL;
  42122. unsigned char outbuf[128];
  42123. X509_EXTENSION* ext = NULL;
  42124. AUTHORITY_INFO_ACCESS* aia = NULL;
  42125. ACCESS_DESCRIPTION* ad = NULL;
  42126. ASN1_IA5STRING *dnsname = NULL;
  42127. const unsigned char v4Addr[] = {192,168,53,1};
  42128. const unsigned char v6Addr[] =
  42129. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  42130. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  42131. const unsigned char email[] =
  42132. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  42133. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  42134. const char* dnsStr = "DNS:example.com";
  42135. const char* uriStr = "URI:http://127.0.0.1:22220";
  42136. const char* v4addStr = "IP Address:192.168.53.1";
  42137. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  42138. const char* emailStr = "email:info@wolfssl.com";
  42139. const char* othrStr = "othername:<unsupported>";
  42140. const char* x400Str = "X400Name:<unsupported>";
  42141. const char* ediStr = "EdiPartyName:<unsupported>";
  42142. /* BIO to output */
  42143. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  42144. /* test for NULL param */
  42145. gn = NULL;
  42146. ExpectIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  42147. ExpectIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  42148. ExpectIntEQ(GENERAL_NAME_print(out, NULL), 0);
  42149. /* test for GEN_DNS */
  42150. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  42151. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  42152. if (f != XBADFILE) {
  42153. XFCLOSE(f);
  42154. f = XBADFILE;
  42155. }
  42156. bufPt = buf;
  42157. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  42158. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  42159. NID_subject_alt_name, NULL, NULL));
  42160. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  42161. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42162. XMEMSET(outbuf, 0, sizeof(outbuf));
  42163. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42164. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  42165. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  42166. gn = NULL;
  42167. sk = NULL;
  42168. X509_free(x509);
  42169. x509 = NULL;
  42170. /* Lets test for setting as well. */
  42171. ExpectNotNull(gn = GENERAL_NAME_new());
  42172. ExpectNotNull(dnsname = ASN1_IA5STRING_new());
  42173. ExpectIntEQ(ASN1_STRING_set(dnsname, "example.com", -1), 1);
  42174. GENERAL_NAME_set0_value(gn, GEN_DNS, dnsname);
  42175. dnsname = NULL;
  42176. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42177. XMEMSET(outbuf, 0, sizeof(outbuf));
  42178. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42179. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  42180. GENERAL_NAME_free(gn);
  42181. /* test for GEN_URI */
  42182. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  42183. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  42184. if (f != XBADFILE) {
  42185. XFCLOSE(f);
  42186. f = XBADFILE;
  42187. }
  42188. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  42189. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  42190. ext));
  42191. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0));
  42192. if (ad != NULL) {
  42193. gn = ad->location;
  42194. }
  42195. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42196. gn = NULL;
  42197. XMEMSET(outbuf,0,sizeof(outbuf));
  42198. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42199. ExpectIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  42200. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  42201. aia = NULL;
  42202. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  42203. ExpectNotNull(aia);
  42204. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  42205. aia = NULL;
  42206. X509_free(x509);
  42207. x509 = NULL;
  42208. /* test for GEN_IPADD */
  42209. /* ip v4 address */
  42210. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42211. if (gn != NULL) {
  42212. gn->type = GEN_IPADD;
  42213. if (gn->d.iPAddress != NULL) {
  42214. gn->d.iPAddress->length = sizeof(v4Addr);
  42215. }
  42216. }
  42217. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  42218. sizeof(v4Addr)), 1);
  42219. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42220. XMEMSET(outbuf,0,sizeof(outbuf));
  42221. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42222. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  42223. GENERAL_NAME_free(gn);
  42224. gn = NULL;
  42225. /* ip v6 address */
  42226. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42227. if (gn != NULL) {
  42228. gn->type = GEN_IPADD;
  42229. if (gn->d.iPAddress != NULL) {
  42230. gn->d.iPAddress->length = sizeof(v6Addr);
  42231. }
  42232. }
  42233. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  42234. sizeof(v6Addr)), 1);
  42235. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42236. XMEMSET(outbuf,0,sizeof(outbuf));
  42237. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42238. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  42239. GENERAL_NAME_free(gn);
  42240. gn = NULL;
  42241. /* test for GEN_EMAIL */
  42242. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42243. if (gn != NULL) {
  42244. gn->type = GEN_EMAIL;
  42245. if (gn->d.rfc822Name != NULL) {
  42246. gn->d.rfc822Name->length = sizeof(email);
  42247. }
  42248. }
  42249. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email, sizeof(email)),
  42250. 1);
  42251. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42252. XMEMSET(outbuf,0,sizeof(outbuf));
  42253. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42254. ExpectIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  42255. GENERAL_NAME_free(gn);
  42256. gn = NULL;
  42257. /* test for GEN_OTHERNAME */
  42258. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42259. if (gn != NULL) {
  42260. gn->type = GEN_OTHERNAME;
  42261. }
  42262. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42263. XMEMSET(outbuf,0,sizeof(outbuf));
  42264. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42265. ExpectIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  42266. GENERAL_NAME_free(gn);
  42267. gn = NULL;
  42268. /* test for GEN_X400 */
  42269. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42270. if (gn != NULL) {
  42271. gn->type = GEN_X400;
  42272. }
  42273. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42274. XMEMSET(outbuf,0,sizeof(outbuf));
  42275. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42276. ExpectIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  42277. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  42278. if (gn != NULL) {
  42279. gn->type = GEN_IA5;
  42280. }
  42281. GENERAL_NAME_free(gn);
  42282. gn = NULL;
  42283. /* test for GEN_EDIPARTY */
  42284. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42285. if (gn != NULL) {
  42286. gn->type = GEN_EDIPARTY;
  42287. }
  42288. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42289. XMEMSET(outbuf,0,sizeof(outbuf));
  42290. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42291. ExpectIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  42292. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  42293. if (gn != NULL) {
  42294. gn->type = GEN_IA5;
  42295. }
  42296. GENERAL_NAME_free(gn);
  42297. gn = NULL;
  42298. BIO_free(out);
  42299. #endif /* OPENSSL_ALL */
  42300. return EXPECT_RESULT();
  42301. }
  42302. static int test_wolfSSL_sk_DIST_POINT(void)
  42303. {
  42304. EXPECT_DECLS;
  42305. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  42306. !defined(NO_RSA)
  42307. X509* x509 = NULL;
  42308. unsigned char buf[4096];
  42309. const unsigned char* bufPt;
  42310. int bytes = 0;
  42311. int i = 0;
  42312. int j = 0;
  42313. XFILE f = XBADFILE;
  42314. DIST_POINT* dp = NULL;
  42315. DIST_POINT_NAME* dpn = NULL;
  42316. GENERAL_NAME* gn = NULL;
  42317. ASN1_IA5STRING* uri = NULL;
  42318. STACK_OF(DIST_POINT)* dps = NULL;
  42319. STACK_OF(GENERAL_NAME)* gns = NULL;
  42320. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  42321. ExpectTrue((f = XFOPEN(cliCertDerCrlDistPoint, "rb")) != XBADFILE);
  42322. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  42323. if (f != XBADFILE)
  42324. XFCLOSE(f);
  42325. bufPt = buf;
  42326. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  42327. ExpectNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  42328. NID_crl_distribution_points, NULL, NULL));
  42329. ExpectIntEQ(sk_DIST_POINT_num(dps), 1);
  42330. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  42331. ExpectNotNull(dp = sk_DIST_POINT_value(dps, i));
  42332. ExpectNotNull(dpn = dp->distpoint);
  42333. /* this should be type 0, fullname */
  42334. ExpectIntEQ(dpn->type, 0);
  42335. ExpectNotNull(gns = dp->distpoint->name.fullname);
  42336. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  42337. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  42338. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, j));
  42339. ExpectIntEQ(gn->type, GEN_URI);
  42340. ExpectNotNull(uri = gn->d.uniformResourceIdentifier);
  42341. ExpectNotNull(uri->data);
  42342. ExpectIntGT(uri->length, 0);
  42343. }
  42344. }
  42345. X509_free(x509);
  42346. CRL_DIST_POINTS_free(dps);
  42347. #endif
  42348. return EXPECT_RESULT();
  42349. }
  42350. static int test_wolfSSL_verify_mode(void)
  42351. {
  42352. EXPECT_DECLS;
  42353. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  42354. WOLFSSL* ssl = NULL;
  42355. WOLFSSL_CTX* ctx = NULL;
  42356. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42357. ExpectNotNull(ssl = SSL_new(ctx));
  42358. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  42359. SSL_free(ssl);
  42360. ssl = NULL;
  42361. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  42362. ExpectNotNull(ssl = SSL_new(ctx));
  42363. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  42364. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  42365. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  42366. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  42367. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  42368. SSL_free(ssl);
  42369. ssl = NULL;
  42370. wolfSSL_CTX_set_verify(ctx,
  42371. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  42372. ExpectNotNull(ssl = SSL_new(ctx));
  42373. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  42374. ExpectIntEQ(SSL_get_verify_mode(ssl),
  42375. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  42376. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  42377. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  42378. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  42379. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  42380. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  42381. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  42382. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  42383. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  42384. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  42385. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  42386. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  42387. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  42388. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  42389. #endif
  42390. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  42391. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  42392. SSL_free(ssl);
  42393. SSL_CTX_free(ctx);
  42394. #endif
  42395. return EXPECT_RESULT();
  42396. }
  42397. static int test_wolfSSL_verify_depth(void)
  42398. {
  42399. EXPECT_DECLS;
  42400. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  42401. WOLFSSL* ssl = NULL;
  42402. WOLFSSL_CTX* ctx = NULL;
  42403. long depth;
  42404. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42405. ExpectIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  42406. ExpectNotNull(ssl = SSL_new(ctx));
  42407. ExpectIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  42408. SSL_free(ssl);
  42409. ssl = NULL;
  42410. SSL_CTX_set_verify_depth(ctx, -1);
  42411. ExpectIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  42412. SSL_CTX_set_verify_depth(ctx, 2);
  42413. ExpectIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  42414. ExpectNotNull(ssl = SSL_new(ctx));
  42415. ExpectIntEQ(2, SSL_get_verify_depth(ssl));
  42416. SSL_free(ssl);
  42417. SSL_CTX_free(ctx);
  42418. #endif
  42419. return EXPECT_RESULT();
  42420. }
  42421. static int test_wolfSSL_verify_result(void)
  42422. {
  42423. EXPECT_DECLS;
  42424. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  42425. defined(OPENSSL_ALL)) && !defined(NO_WOLFSSL_CLIENT)
  42426. WOLFSSL* ssl = NULL;
  42427. WOLFSSL_CTX* ctx = NULL;
  42428. long result = 0xDEADBEEF;
  42429. ExpectIntEQ(WOLFSSL_FAILURE, wolfSSL_get_verify_result(ssl));
  42430. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42431. ExpectNotNull(ssl = SSL_new(ctx));
  42432. wolfSSL_set_verify_result(ssl, result);
  42433. ExpectIntEQ(result, wolfSSL_get_verify_result(ssl));
  42434. SSL_free(ssl);
  42435. SSL_CTX_free(ctx);
  42436. #endif
  42437. return EXPECT_RESULT();
  42438. }
  42439. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  42440. static void sslMsgCb(int w, int version, int type, const void* buf,
  42441. size_t sz, SSL* ssl, void* arg)
  42442. {
  42443. int i;
  42444. unsigned char* pt = (unsigned char*)buf;
  42445. fprintf(stderr, "%s %d bytes of version %d , type %d : ",
  42446. (w)?"Writing":"Reading", (int)sz, version, type);
  42447. for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
  42448. fprintf(stderr, "\n");
  42449. (void)ssl;
  42450. (void)arg;
  42451. }
  42452. #endif /* OPENSSL_EXTRA */
  42453. static int test_wolfSSL_msg_callback(void)
  42454. {
  42455. EXPECT_DECLS;
  42456. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  42457. WOLFSSL* ssl = NULL;
  42458. WOLFSSL_CTX* ctx = NULL;
  42459. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42460. ExpectNotNull(ssl = SSL_new(ctx));
  42461. ExpectIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  42462. ExpectIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  42463. ExpectIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  42464. SSL_free(ssl);
  42465. SSL_CTX_free(ctx);
  42466. #endif
  42467. return EXPECT_RESULT();
  42468. }
  42469. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  42470. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  42471. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  42472. static void binary_dump(void *ptr, int size)
  42473. {
  42474. #ifdef WOLFSSL_EVP_PRINT
  42475. int i = 0;
  42476. unsigned char *p = (unsigned char *) ptr;
  42477. fprintf(stderr, "{");
  42478. while ((p != NULL) && (i < size)) {
  42479. if ((i % 8) == 0) {
  42480. fprintf(stderr, "\n");
  42481. fprintf(stderr, " ");
  42482. }
  42483. fprintf(stderr, "0x%02x, ", p[i]);
  42484. i++;
  42485. }
  42486. fprintf(stderr, "\n};\n");
  42487. #else
  42488. (void) ptr;
  42489. (void) size;
  42490. #endif
  42491. }
  42492. static int last_val = 0x0f;
  42493. static int check_result(unsigned char *data, int len)
  42494. {
  42495. int i;
  42496. for ( ; len; ) {
  42497. last_val = (last_val + 1) % 16;
  42498. for (i = 0; i < 16; len--, i++, data++)
  42499. if (*data != last_val) {
  42500. return -1;
  42501. }
  42502. }
  42503. return 0;
  42504. }
  42505. static int r_offset;
  42506. static int w_offset;
  42507. static void init_offset(void)
  42508. {
  42509. r_offset = 0;
  42510. w_offset = 0;
  42511. }
  42512. static void get_record(unsigned char *data, unsigned char *buf, int len)
  42513. {
  42514. XMEMCPY(buf, data+r_offset, len);
  42515. r_offset += len;
  42516. }
  42517. static void set_record(unsigned char *data, unsigned char *buf, int len)
  42518. {
  42519. XMEMCPY(data+w_offset, buf, len);
  42520. w_offset += len;
  42521. }
  42522. static void set_plain(unsigned char *plain, int rec)
  42523. {
  42524. int i, j;
  42525. unsigned char *p = plain;
  42526. #define BLOCKSZ 16
  42527. for (i=0; i<(rec/BLOCKSZ); i++) {
  42528. for (j=0; j<BLOCKSZ; j++)
  42529. *p++ = (i % 16);
  42530. }
  42531. }
  42532. #endif
  42533. static int test_wolfSSL_EVP_Cipher_extra(void)
  42534. {
  42535. EXPECT_DECLS;
  42536. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  42537. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  42538. /* aes128-cbc, keylen=16, ivlen=16 */
  42539. byte aes128_cbc_key[] = {
  42540. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  42541. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  42542. };
  42543. byte aes128_cbc_iv[] = {
  42544. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  42545. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  42546. };
  42547. /* teset data size table */
  42548. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  42549. int test_drive2[] = {8, 3, 8, 512, 0};
  42550. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  42551. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  42552. int test_drive_len[100];
  42553. int ret = 0;
  42554. EVP_CIPHER_CTX *evp = NULL;
  42555. int ilen = 0;
  42556. int klen = 0;
  42557. int i, j;
  42558. const EVP_CIPHER *type;
  42559. byte *iv;
  42560. byte *key;
  42561. int ivlen;
  42562. int keylen;
  42563. #define RECORDS 16
  42564. #define BUFFSZ 512
  42565. byte plain [BUFFSZ * RECORDS];
  42566. byte cipher[BUFFSZ * RECORDS];
  42567. byte inb[BUFFSZ];
  42568. byte outb[BUFFSZ+16];
  42569. int outl = 0;
  42570. int inl;
  42571. iv = aes128_cbc_iv;
  42572. ivlen = sizeof(aes128_cbc_iv);
  42573. key = aes128_cbc_key;
  42574. keylen = sizeof(aes128_cbc_key);
  42575. type = EVP_aes_128_cbc();
  42576. set_plain(plain, BUFFSZ * RECORDS);
  42577. SSL_library_init();
  42578. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  42579. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  42580. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  42581. klen = EVP_CIPHER_CTX_key_length(evp);
  42582. if (klen > 0 && keylen != klen) {
  42583. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  42584. }
  42585. ilen = EVP_CIPHER_CTX_iv_length(evp);
  42586. if (ilen > 0 && ivlen != ilen) {
  42587. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  42588. }
  42589. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  42590. for (j = 0; j<RECORDS; j++)
  42591. {
  42592. inl = BUFFSZ;
  42593. get_record(plain, inb, inl);
  42594. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  42595. set_record(cipher, outb, outl);
  42596. }
  42597. for (i = 0; test_drive[i]; i++) {
  42598. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  42599. init_offset();
  42600. test_drive_len[i] = 0;
  42601. for (j = 0; test_drive[i][j]; j++)
  42602. {
  42603. inl = test_drive[i][j];
  42604. test_drive_len[i] += inl;
  42605. get_record(plain, inb, inl);
  42606. ExpectIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)),
  42607. 0);
  42608. /* output to cipher buffer, so that following Dec test can detect
  42609. if any error */
  42610. set_record(cipher, outb, outl);
  42611. }
  42612. EVP_CipherFinal(evp, outb, &outl);
  42613. if (outl > 0)
  42614. set_record(cipher, outb, outl);
  42615. }
  42616. for (i = 0; test_drive[i]; i++) {
  42617. last_val = 0x0f;
  42618. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  42619. init_offset();
  42620. for (j = 0; test_drive[i][j]; j++) {
  42621. inl = test_drive[i][j];
  42622. get_record(cipher, inb, inl);
  42623. ExpectIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)),
  42624. 0);
  42625. binary_dump(outb, outl);
  42626. ExpectIntEQ((ret = check_result(outb, outl)), 0);
  42627. ExpectFalse(outl > ((inl/16+1)*16) && outl > 16);
  42628. }
  42629. ret = EVP_CipherFinal(evp, outb, &outl);
  42630. binary_dump(outb, outl);
  42631. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  42632. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  42633. ExpectTrue(ret);
  42634. }
  42635. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(evp), WOLFSSL_SUCCESS);
  42636. EVP_CIPHER_CTX_free(evp);
  42637. evp = NULL;
  42638. /* Do an extra test to verify correct behavior with empty input. */
  42639. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  42640. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  42641. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  42642. klen = EVP_CIPHER_CTX_key_length(evp);
  42643. if (klen > 0 && keylen != klen) {
  42644. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  42645. }
  42646. ilen = EVP_CIPHER_CTX_iv_length(evp);
  42647. if (ilen > 0 && ivlen != ilen) {
  42648. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  42649. }
  42650. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  42651. /* outl should be set to 0 after passing NULL, 0 for input args. */
  42652. outl = -1;
  42653. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, NULL, 0)), 0);
  42654. ExpectIntEQ(outl, 0);
  42655. EVP_CIPHER_CTX_free(evp);
  42656. #endif /* test_EVP_Cipher */
  42657. return EXPECT_RESULT();
  42658. }
  42659. static int test_wolfSSL_PEM_read_DHparams(void)
  42660. {
  42661. EXPECT_DECLS;
  42662. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  42663. !defined(NO_FILESYSTEM)
  42664. DH* dh = NULL;
  42665. XFILE fp = XBADFILE;
  42666. unsigned char derOut[300];
  42667. unsigned char* derOutBuf = derOut;
  42668. int derOutSz = 0;
  42669. unsigned char derExpected[300];
  42670. int derExpectedSz = 0;
  42671. XMEMSET(derOut, 0, sizeof(derOut));
  42672. XMEMSET(derExpected, 0, sizeof(derExpected));
  42673. /* open DH param file, read into DH struct */
  42674. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  42675. /* bad args */
  42676. ExpectNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  42677. ExpectNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  42678. /* good args */
  42679. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  42680. if (fp != XBADFILE) {
  42681. XFCLOSE(fp);
  42682. fp = XBADFILE;
  42683. }
  42684. /* read in certs/dh2048.der for comparison against exported params */
  42685. ExpectTrue((fp = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  42686. ExpectIntGT(derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected),
  42687. fp), 0);
  42688. if (fp != XBADFILE) {
  42689. XFCLOSE(fp);
  42690. fp = XBADFILE;
  42691. }
  42692. /* export DH back to DER and compare */
  42693. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  42694. ExpectIntEQ(derOutSz, derExpectedSz);
  42695. ExpectIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  42696. DH_free(dh);
  42697. dh = NULL;
  42698. /* Test parsing with X9.42 header */
  42699. ExpectTrue((fp = XFOPEN("./certs/x942dh2048.pem", "rb")) != XBADFILE);
  42700. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  42701. if (fp != XBADFILE)
  42702. XFCLOSE(fp);
  42703. DH_free(dh);
  42704. dh = NULL;
  42705. #endif
  42706. return EXPECT_RESULT();
  42707. }
  42708. static int test_wolfSSL_X509_get_serialNumber(void)
  42709. {
  42710. EXPECT_DECLS;
  42711. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  42712. ASN1_INTEGER* a = NULL;
  42713. BIGNUM* bn = NULL;
  42714. X509* x509 = NULL;
  42715. char *serialHex = NULL;
  42716. byte serial[3];
  42717. int serialSz;
  42718. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42719. SSL_FILETYPE_PEM));
  42720. ExpectNotNull(a = X509_get_serialNumber(x509));
  42721. /* check on value of ASN1 Integer */
  42722. ExpectNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  42723. a = NULL;
  42724. /* test setting serial number and then retrieving it */
  42725. ExpectNotNull(a = ASN1_INTEGER_new());
  42726. ExpectIntEQ(ASN1_INTEGER_set(a, 3), 1);
  42727. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  42728. serialSz = sizeof(serial);
  42729. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  42730. WOLFSSL_SUCCESS);
  42731. ExpectIntEQ(serialSz, 1);
  42732. ExpectIntEQ(serial[0], 3);
  42733. ASN1_INTEGER_free(a);
  42734. a = NULL;
  42735. /* test setting serial number with 0's in it */
  42736. serial[0] = 0x01;
  42737. serial[1] = 0x00;
  42738. serial[2] = 0x02;
  42739. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  42740. if (a != NULL) {
  42741. a->data[0] = ASN_INTEGER;
  42742. a->data[1] = sizeof(serial);
  42743. XMEMCPY(&a->data[2], serial, sizeof(serial));
  42744. a->length = sizeof(serial) + 2;
  42745. }
  42746. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  42747. XMEMSET(serial, 0, sizeof(serial));
  42748. serialSz = sizeof(serial);
  42749. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  42750. WOLFSSL_SUCCESS);
  42751. ExpectIntEQ(serialSz, 3);
  42752. ExpectIntEQ(serial[0], 0x01);
  42753. ExpectIntEQ(serial[1], 0x00);
  42754. ExpectIntEQ(serial[2], 0x02);
  42755. ASN1_INTEGER_free(a);
  42756. a = NULL;
  42757. X509_free(x509); /* free's a */
  42758. ExpectNotNull(serialHex = BN_bn2hex(bn));
  42759. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  42760. ExpectStrEQ(serialHex, "01");
  42761. #else
  42762. ExpectStrEQ(serialHex, "1");
  42763. #endif
  42764. OPENSSL_free(serialHex);
  42765. ExpectIntEQ(BN_get_word(bn), 1);
  42766. BN_free(bn);
  42767. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  42768. ExpectNotNull(a = ASN1_INTEGER_new());
  42769. if (a != NULL) {
  42770. ExpectNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  42771. DYNAMIC_TYPE_OPENSSL));
  42772. a->isDynamic = 1;
  42773. ASN1_INTEGER_free(a);
  42774. }
  42775. #endif
  42776. return EXPECT_RESULT();
  42777. }
  42778. static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
  42779. {
  42780. EXPECT_DECLS;
  42781. #if defined(OPENSSL_EXTRA)
  42782. ExpectIntEQ(wolfSSL_add_all_algorithms(), WOLFSSL_SUCCESS);
  42783. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(), WOLFSSL_SUCCESS);
  42784. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(), WOLFSSL_SUCCESS);
  42785. #endif
  42786. return EXPECT_RESULT();
  42787. }
  42788. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  42789. {
  42790. EXPECT_DECLS;
  42791. #if defined(OPENSSL_EXTRA)
  42792. #define MAX_HEXSTR_BUFSZ 9
  42793. #define NUM_CASES 5
  42794. struct Output {
  42795. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  42796. long ret;
  42797. };
  42798. int i;
  42799. int j;
  42800. const char* inputs[NUM_CASES] = {
  42801. "aabcd1357e",
  42802. "01:12:23:34:a5:b6:c7:d8:e9",
  42803. ":01:02",
  42804. "012",
  42805. ":ab:ac:d"
  42806. };
  42807. struct Output expectedOutputs[NUM_CASES] = {
  42808. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  42809. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  42810. {{0x01, 0x02}, 2},
  42811. {{0x00}, 0},
  42812. {{0x00}, 0}
  42813. };
  42814. long len = 0;
  42815. unsigned char* returnedBuf = NULL;
  42816. for (i = 0; i < NUM_CASES && !EXPECT_FAIL(); ++i) {
  42817. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  42818. if (returnedBuf == NULL) {
  42819. ExpectIntEQ(expectedOutputs[i].ret, 0);
  42820. continue;
  42821. }
  42822. ExpectIntEQ(expectedOutputs[i].ret, len);
  42823. for (j = 0; j < len; ++j) {
  42824. ExpectIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  42825. }
  42826. OPENSSL_free(returnedBuf);
  42827. returnedBuf = NULL;
  42828. }
  42829. #endif
  42830. return EXPECT_RESULT();
  42831. }
  42832. static int test_wolfSSL_X509_CA_num(void)
  42833. {
  42834. EXPECT_DECLS;
  42835. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  42836. defined(HAVE_ECC) && !defined(NO_RSA)
  42837. WOLFSSL_X509_STORE *store = NULL;
  42838. WOLFSSL_X509 *x509_1 = NULL;
  42839. WOLFSSL_X509 *x509_2 = NULL;
  42840. int ca_num = 0;
  42841. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  42842. ExpectNotNull(x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42843. WOLFSSL_FILETYPE_PEM));
  42844. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_1), 1);
  42845. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 1);
  42846. ExpectNotNull(x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile,
  42847. WOLFSSL_FILETYPE_PEM));
  42848. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_2), 1);
  42849. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 2);
  42850. wolfSSL_X509_free(x509_1);
  42851. wolfSSL_X509_free(x509_2);
  42852. wolfSSL_X509_STORE_free(store);
  42853. #endif
  42854. return EXPECT_RESULT();
  42855. }
  42856. static int test_wolfSSL_X509_check_ca(void)
  42857. {
  42858. EXPECT_DECLS;
  42859. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  42860. WOLFSSL_X509 *x509 = NULL;
  42861. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42862. WOLFSSL_FILETYPE_PEM));
  42863. ExpectIntEQ(wolfSSL_X509_check_ca(x509), 1);
  42864. wolfSSL_X509_free(x509);
  42865. #endif
  42866. return EXPECT_RESULT();
  42867. }
  42868. static int test_wolfSSL_X509_check_ip_asc(void)
  42869. {
  42870. EXPECT_DECLS;
  42871. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  42872. WOLFSSL_X509 *x509 = NULL;
  42873. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  42874. WOLFSSL_FILETYPE_PEM));
  42875. #if 0
  42876. /* TODO: add cert gen for testing positive case */
  42877. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  42878. #endif
  42879. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  42880. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  42881. wolfSSL_X509_free(x509);
  42882. #endif
  42883. return EXPECT_RESULT();
  42884. }
  42885. static int test_wolfSSL_make_cert(void)
  42886. {
  42887. EXPECT_DECLS;
  42888. #if !defined(NO_RSA) && !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && \
  42889. defined(WOLFSSL_CERT_EXT)
  42890. int ret = 0;
  42891. Cert cert;
  42892. CertName name;
  42893. RsaKey key;
  42894. WC_RNG rng;
  42895. byte der[FOURK_BUF];
  42896. word32 idx = 0;
  42897. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  42898. #ifdef OPENSSL_EXTRA
  42899. const unsigned char* pt = NULL;
  42900. int certSz = 0;
  42901. X509* x509 = NULL;
  42902. X509_NAME* x509name = NULL;
  42903. X509_NAME_ENTRY* entry = NULL;
  42904. ASN1_STRING* entryValue = NULL;
  42905. #endif
  42906. XMEMSET(&name, 0, sizeof(CertName));
  42907. /* set up cert name */
  42908. XMEMCPY(name.country, "US", sizeof("US"));
  42909. name.countryEnc = CTC_PRINTABLE;
  42910. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  42911. name.stateEnc = CTC_UTF8;
  42912. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  42913. name.localityEnc = CTC_UTF8;
  42914. XMEMCPY(name.sur, "Test", sizeof("Test"));
  42915. name.surEnc = CTC_UTF8;
  42916. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  42917. name.orgEnc = CTC_UTF8;
  42918. XMEMCPY(name.unit, "Development", sizeof("Development"));
  42919. name.unitEnc = CTC_UTF8;
  42920. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  42921. name.commonNameEnc = CTC_UTF8;
  42922. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  42923. name.serialDevEnc = CTC_PRINTABLE;
  42924. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  42925. name.userIdEnc = CTC_PRINTABLE;
  42926. #ifdef WOLFSSL_MULTI_ATTRIB
  42927. #if CTC_MAX_ATTRIB > 2
  42928. {
  42929. NameAttrib* n;
  42930. n = &name.name[0];
  42931. n->id = ASN_DOMAIN_COMPONENT;
  42932. n->type = CTC_UTF8;
  42933. n->sz = sizeof("com");
  42934. XMEMCPY(n->value, "com", sizeof("com"));
  42935. n = &name.name[1];
  42936. n->id = ASN_DOMAIN_COMPONENT;
  42937. n->type = CTC_UTF8;
  42938. n->sz = sizeof("wolfssl");
  42939. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  42940. }
  42941. #endif
  42942. #endif /* WOLFSSL_MULTI_ATTRIB */
  42943. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  42944. #ifndef HAVE_FIPS
  42945. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  42946. #else
  42947. ExpectIntEQ(wc_InitRng(&rng), 0);
  42948. #endif
  42949. /* load test RSA key */
  42950. idx = 0;
  42951. #if defined(USE_CERT_BUFFERS_1024)
  42952. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  42953. sizeof_server_key_der_1024), 0);
  42954. #elif defined(USE_CERT_BUFFERS_2048)
  42955. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  42956. sizeof_server_key_der_2048), 0);
  42957. #else
  42958. /* error case, no RSA key loaded, happens later */
  42959. (void)idx;
  42960. #endif
  42961. XMEMSET(&cert, 0 , sizeof(Cert));
  42962. ExpectIntEQ(wc_InitCert(&cert), 0);
  42963. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  42964. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  42965. cert.serialSz = (int)sizeof(mySerial);
  42966. cert.isCA = 1;
  42967. #ifndef NO_SHA256
  42968. cert.sigType = CTC_SHA256wRSA;
  42969. #else
  42970. cert.sigType = CTC_SHAwRSA;
  42971. #endif
  42972. /* add SKID from the Public Key */
  42973. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  42974. /* add AKID from the Public Key */
  42975. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  42976. ret = 0;
  42977. do {
  42978. #if defined(WOLFSSL_ASYNC_CRYPT)
  42979. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  42980. #endif
  42981. if (ret >= 0) {
  42982. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  42983. }
  42984. } while (ret == WC_PENDING_E);
  42985. ExpectIntGT(ret, 0);
  42986. #ifdef OPENSSL_EXTRA
  42987. /* der holds a certificate with DC's now check X509 parsing of it */
  42988. certSz = ret;
  42989. pt = der;
  42990. ExpectNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  42991. ExpectNotNull(x509name = X509_get_subject_name(x509));
  42992. #ifdef WOLFSSL_MULTI_ATTRIB
  42993. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42994. -1)), 5);
  42995. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42996. idx)), 6);
  42997. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42998. idx)), -1);
  42999. #endif /* WOLFSSL_MULTI_ATTRIB */
  43000. /* compare DN at index 0 */
  43001. ExpectNotNull(entry = X509_NAME_get_entry(x509name, 0));
  43002. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  43003. ExpectIntEQ(ASN1_STRING_length(entryValue), 2);
  43004. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  43005. #ifndef WOLFSSL_MULTI_ATTRIB
  43006. /* compare Serial Number */
  43007. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_serialNumber,
  43008. -1)), 7);
  43009. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  43010. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  43011. ExpectIntEQ(ASN1_STRING_length(entryValue), XSTRLEN("wolfSSL12345"));
  43012. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "wolfSSL12345");
  43013. #endif
  43014. #ifdef WOLFSSL_MULTI_ATTRIB
  43015. /* get first and second DC and compare result */
  43016. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  43017. -1)), 5);
  43018. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  43019. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  43020. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  43021. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  43022. idx)), 6);
  43023. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  43024. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  43025. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  43026. #endif /* WOLFSSL_MULTI_ATTRIB */
  43027. /* try invalid index locations for regression test and sanity check */
  43028. ExpectNull(entry = X509_NAME_get_entry(x509name, 11));
  43029. ExpectNull(entry = X509_NAME_get_entry(x509name, 20));
  43030. X509_free(x509);
  43031. #endif /* OPENSSL_EXTRA */
  43032. wc_FreeRsaKey(&key);
  43033. wc_FreeRng(&rng);
  43034. #endif
  43035. return EXPECT_RESULT();
  43036. }
  43037. static int test_wolfSSL_X509_get_version(void)
  43038. {
  43039. EXPECT_DECLS;
  43040. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  43041. WOLFSSL_X509 *x509 = NULL;
  43042. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  43043. WOLFSSL_FILETYPE_PEM));
  43044. ExpectIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  43045. wolfSSL_X509_free(x509);
  43046. #endif
  43047. return EXPECT_RESULT();
  43048. }
  43049. #if defined(OPENSSL_ALL)
  43050. static int test_wolfSSL_sk_CIPHER_description(void)
  43051. {
  43052. EXPECT_DECLS;
  43053. #if !defined(NO_RSA)
  43054. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  43055. int i;
  43056. int numCiphers = 0;
  43057. const SSL_METHOD *method = NULL;
  43058. const SSL_CIPHER *cipher = NULL;
  43059. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  43060. SSL_CTX *ctx = NULL;
  43061. SSL *ssl = NULL;
  43062. char buf[256];
  43063. char test_str[9] = "0000000";
  43064. const char badStr[] = "unknown";
  43065. const char certPath[] = "./certs/client-cert.pem";
  43066. XMEMSET(buf, 0, sizeof(buf));
  43067. ExpectNotNull(method = TLSv1_2_client_method());
  43068. ExpectNotNull(ctx = SSL_CTX_new(method));
  43069. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  43070. SSL_CTX_set_verify_depth(ctx, 4);
  43071. SSL_CTX_set_options(ctx, flags);
  43072. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  43073. WOLFSSL_SUCCESS);
  43074. ExpectNotNull(ssl = SSL_new(ctx));
  43075. /* SSL_get_ciphers returns a stack of all configured ciphers
  43076. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  43077. */
  43078. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  43079. /* loop through the amount of supportedCiphers */
  43080. numCiphers = sk_num(supportedCiphers);
  43081. for (i = 0; i < numCiphers; ++i) {
  43082. int j;
  43083. /* sk_value increments "sk->data.cipher->cipherOffset".
  43084. * wolfSSL_sk_CIPHER_description sets the description for
  43085. * the cipher based on the provided offset.
  43086. */
  43087. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  43088. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  43089. }
  43090. /* Search cipher description string for "unknown" descriptor */
  43091. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  43092. int k = 0;
  43093. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  43094. test_str[k] = badStr[k];
  43095. j++;
  43096. k++;
  43097. }
  43098. }
  43099. /* Fail if test_str == badStr == "unknown" */
  43100. ExpectStrNE(test_str,badStr);
  43101. }
  43102. SSL_free(ssl);
  43103. SSL_CTX_free(ctx);
  43104. #endif
  43105. return EXPECT_RESULT();
  43106. }
  43107. static int test_wolfSSL_get_ciphers_compat(void)
  43108. {
  43109. EXPECT_DECLS;
  43110. #if !defined(NO_RSA)
  43111. const SSL_METHOD *method = NULL;
  43112. const char certPath[] = "./certs/client-cert.pem";
  43113. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  43114. SSL_CTX *ctx = NULL;
  43115. WOLFSSL *ssl = NULL;
  43116. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  43117. ExpectNotNull(method = SSLv23_client_method());
  43118. ExpectNotNull(ctx = SSL_CTX_new(method));
  43119. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  43120. SSL_CTX_set_verify_depth(ctx, 4);
  43121. SSL_CTX_set_options(ctx, flags);
  43122. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  43123. WOLFSSL_SUCCESS);
  43124. ExpectNotNull(ssl = SSL_new(ctx));
  43125. /* Test Bad NULL input */
  43126. ExpectNull(supportedCiphers = SSL_get_ciphers(NULL));
  43127. /* Test for Good input */
  43128. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  43129. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  43130. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  43131. SSL_free(ssl);
  43132. SSL_CTX_free(ctx);
  43133. #endif
  43134. return EXPECT_RESULT();
  43135. }
  43136. static int test_wolfSSL_X509_PUBKEY_get(void)
  43137. {
  43138. EXPECT_DECLS;
  43139. WOLFSSL_X509_PUBKEY pubkey;
  43140. WOLFSSL_X509_PUBKEY* key;
  43141. WOLFSSL_EVP_PKEY evpkey ;
  43142. WOLFSSL_EVP_PKEY* evpPkey;
  43143. WOLFSSL_EVP_PKEY* retEvpPkey;
  43144. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  43145. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  43146. key = &pubkey;
  43147. evpPkey = &evpkey;
  43148. evpPkey->type = WOLFSSL_SUCCESS;
  43149. key->pkey = evpPkey;
  43150. ExpectNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  43151. ExpectIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  43152. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  43153. key->pkey = NULL;
  43154. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  43155. return EXPECT_RESULT();
  43156. }
  43157. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  43158. {
  43159. EXPECT_DECLS;
  43160. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  43161. DSA *dsa = NULL;
  43162. DSA *setDsa = NULL;
  43163. EVP_PKEY *pkey = NULL;
  43164. EVP_PKEY *set1Pkey = NULL;
  43165. SHA_CTX sha;
  43166. byte signature[DSA_SIG_SIZE];
  43167. byte hash[WC_SHA_DIGEST_SIZE];
  43168. word32 bytes;
  43169. int answer;
  43170. #ifdef USE_CERT_BUFFERS_1024
  43171. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  43172. int dsaKeySz = sizeof_dsa_key_der_1024;
  43173. byte tmp[ONEK_BUF];
  43174. XMEMSET(tmp, 0, sizeof(tmp));
  43175. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  43176. bytes = dsaKeySz;
  43177. #elif defined(USE_CERT_BUFFERS_2048)
  43178. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  43179. int dsaKeySz = sizeof_dsa_key_der_2048;
  43180. byte tmp[TWOK_BUF];
  43181. XMEMSET(tmp, 0, sizeof(tmp));
  43182. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  43183. bytes = dsaKeySz;
  43184. #else
  43185. byte tmp[TWOK_BUF];
  43186. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  43187. int dsaKeySz;
  43188. XFILE fp = XBADFILE;
  43189. XMEMSET(tmp, 0, sizeof(tmp));
  43190. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  43191. ExpectIntGT(dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  43192. if (fp != XBADFILE)
  43193. XFCLOSE(fp);
  43194. #endif /* END USE_CERT_BUFFERS_1024 */
  43195. /* Create hash to later Sign and Verify */
  43196. ExpectIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  43197. ExpectIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  43198. ExpectIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  43199. /* Initialize pkey with der format dsa key */
  43200. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey, &dsaKeyDer,
  43201. (long)dsaKeySz));
  43202. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  43203. /* Should Fail: NULL argument */
  43204. ExpectNull(dsa = EVP_PKEY_get0_DSA(NULL));
  43205. ExpectNull(dsa = EVP_PKEY_get1_DSA(NULL));
  43206. /* Should Pass: Initialized pkey argument */
  43207. ExpectNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  43208. ExpectNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  43209. #ifdef USE_CERT_BUFFERS_1024
  43210. ExpectIntEQ(DSA_bits(dsa), 1024);
  43211. #else
  43212. ExpectIntEQ(DSA_bits(dsa), 2048);
  43213. #endif
  43214. /* Sign */
  43215. ExpectIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  43216. /* Verify. */
  43217. ExpectIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  43218. WOLFSSL_SUCCESS);
  43219. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  43220. /* Should Fail: set1Pkey not initialized */
  43221. ExpectIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  43222. /* Initialize set1Pkey */
  43223. set1Pkey = EVP_PKEY_new();
  43224. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  43225. ExpectIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  43226. WOLFSSL_SUCCESS);
  43227. /* Should Pass: set dsa into set1Pkey */
  43228. ExpectIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  43229. DSA_free(dsa);
  43230. DSA_free(setDsa);
  43231. EVP_PKEY_free(pkey);
  43232. EVP_PKEY_free(set1Pkey);
  43233. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  43234. return EXPECT_RESULT();
  43235. } /* END test_EVP_PKEY_set1_get1_DSA */
  43236. static int test_wolfSSL_DSA_generate_parameters(void)
  43237. {
  43238. EXPECT_DECLS;
  43239. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  43240. !defined(HAVE_FIPS)
  43241. DSA *dsa = NULL;
  43242. ExpectNotNull(dsa = DSA_generate_parameters(2048, NULL, 0, NULL, NULL, NULL,
  43243. NULL));
  43244. DSA_free(dsa);
  43245. #endif
  43246. return EXPECT_RESULT();
  43247. }
  43248. static int test_wolfSSL_DSA_SIG(void)
  43249. {
  43250. EXPECT_DECLS;
  43251. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  43252. !defined(HAVE_FIPS)
  43253. DSA *dsa = NULL;
  43254. DSA *dsa2 = NULL;
  43255. DSA_SIG *sig = NULL;
  43256. const BIGNUM *p = NULL;
  43257. const BIGNUM *q = NULL;
  43258. const BIGNUM *g = NULL;
  43259. const BIGNUM *pub = NULL;
  43260. const BIGNUM *priv = NULL;
  43261. BIGNUM *dup_p = NULL;
  43262. BIGNUM *dup_q = NULL;
  43263. BIGNUM *dup_g = NULL;
  43264. BIGNUM *dup_pub = NULL;
  43265. BIGNUM *dup_priv = NULL;
  43266. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  43267. ExpectNotNull(dsa = DSA_new());
  43268. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048, NULL, 0, NULL, NULL,
  43269. NULL), 1);
  43270. ExpectIntEQ(DSA_generate_key(dsa), 1);
  43271. DSA_get0_pqg(dsa, &p, &q, &g);
  43272. DSA_get0_key(dsa, &pub, &priv);
  43273. ExpectNotNull(dup_p = BN_dup(p));
  43274. ExpectNotNull(dup_q = BN_dup(q));
  43275. ExpectNotNull(dup_g = BN_dup(g));
  43276. ExpectNotNull(dup_pub = BN_dup(pub));
  43277. ExpectNotNull(dup_priv = BN_dup(priv));
  43278. ExpectNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  43279. ExpectNotNull(dsa2 = DSA_new());
  43280. ExpectIntEQ(DSA_set0_pqg(dsa2, dup_p, dup_q, dup_g), 1);
  43281. if (EXPECT_FAIL()) {
  43282. BN_free(dup_p);
  43283. BN_free(dup_q);
  43284. BN_free(dup_g);
  43285. }
  43286. ExpectIntEQ(DSA_set0_key(dsa2, dup_pub, dup_priv), 1);
  43287. if (EXPECT_FAIL()) {
  43288. BN_free(dup_pub);
  43289. BN_free(dup_priv);
  43290. }
  43291. ExpectIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  43292. DSA_free(dsa);
  43293. DSA_free(dsa2);
  43294. DSA_SIG_free(sig);
  43295. #endif
  43296. return EXPECT_RESULT();
  43297. }
  43298. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  43299. {
  43300. EXPECT_DECLS;
  43301. #ifdef HAVE_ECC
  43302. WOLFSSL_EC_KEY* ecKey = NULL;
  43303. WOLFSSL_EC_KEY* ecGet1 = NULL;
  43304. EVP_PKEY* pkey = NULL;
  43305. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  43306. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43307. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  43308. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  43309. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  43310. /* Should fail since ecKey is empty */
  43311. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  43312. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  43313. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  43314. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  43315. ExpectNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  43316. ExpectNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  43317. wolfSSL_EC_KEY_free(ecKey);
  43318. wolfSSL_EC_KEY_free(ecGet1);
  43319. EVP_PKEY_free(pkey);
  43320. #endif /* HAVE_ECC */
  43321. return EXPECT_RESULT();
  43322. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  43323. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  43324. {
  43325. EXPECT_DECLS;
  43326. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  43327. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  43328. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  43329. DH *dh = NULL;
  43330. DH *setDh = NULL;
  43331. EVP_PKEY *pkey = NULL;
  43332. XFILE f = XBADFILE;
  43333. unsigned char buf[4096];
  43334. const unsigned char* pt = buf;
  43335. const char* dh2048 = "./certs/dh2048.der";
  43336. long len = 0;
  43337. int code = -1;
  43338. XMEMSET(buf, 0, sizeof(buf));
  43339. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  43340. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  43341. if (f != XBADFILE)
  43342. XFCLOSE(f);
  43343. /* Load dh2048.der into DH with internal format */
  43344. ExpectNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  43345. ExpectIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  43346. ExpectIntEQ(code, 0);
  43347. code = -1;
  43348. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43349. /* Set DH into PKEY */
  43350. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  43351. /* Get DH from PKEY */
  43352. ExpectNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  43353. ExpectIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  43354. ExpectIntEQ(code, 0);
  43355. EVP_PKEY_free(pkey);
  43356. DH_free(setDh);
  43357. setDh = NULL;
  43358. DH_free(dh);
  43359. dh = NULL;
  43360. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  43361. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  43362. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  43363. return EXPECT_RESULT();
  43364. } /* END test_EVP_PKEY_set1_get1_DH */
  43365. static int test_wolfSSL_CTX_ctrl(void)
  43366. {
  43367. EXPECT_DECLS;
  43368. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  43369. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  43370. char caFile[] = "./certs/client-ca.pem";
  43371. char clientFile[] = "./certs/client-cert.pem";
  43372. SSL_CTX* ctx = NULL;
  43373. X509* x509 = NULL;
  43374. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  43375. byte buf[6000];
  43376. char file[] = "./certs/dsaparams.pem";
  43377. XFILE f = XBADFILE;
  43378. int bytes = 0;
  43379. BIO* bio = NULL;
  43380. DSA* dsa = NULL;
  43381. DH* dh = NULL;
  43382. #endif
  43383. #ifdef HAVE_ECC
  43384. WOLFSSL_EC_KEY* ecKey = NULL;
  43385. #endif
  43386. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  43387. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  43388. WOLFSSL_FILETYPE_PEM));
  43389. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  43390. if (EXPECT_FAIL()) {
  43391. wolfSSL_X509_free(x509);
  43392. }
  43393. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  43394. WOLFSSL_FILETYPE_PEM));
  43395. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  43396. /* Initialize DH */
  43397. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  43398. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  43399. if (f != XBADFILE)
  43400. XFCLOSE(f);
  43401. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  43402. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  43403. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  43404. #endif
  43405. #ifdef HAVE_ECC
  43406. /* Initialize WOLFSSL_EC_KEY */
  43407. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  43408. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  43409. #endif
  43410. /* additional test of getting EVP_PKEY key size from X509
  43411. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  43412. * allowed with user RSA */
  43413. {
  43414. EVP_PKEY* pkey = NULL;
  43415. #if defined(HAVE_ECC)
  43416. X509* ecX509 = NULL;
  43417. #endif /* HAVE_ECC */
  43418. ExpectNotNull(pkey = X509_get_pubkey(x509));
  43419. /* current RSA key is 2048 bit (256 bytes) */
  43420. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  43421. EVP_PKEY_free(pkey);
  43422. pkey = NULL;
  43423. #if defined(HAVE_ECC)
  43424. #if defined(USE_CERT_BUFFERS_256)
  43425. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  43426. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  43427. SSL_FILETYPE_ASN1));
  43428. #else
  43429. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  43430. cliEccCertFile, SSL_FILETYPE_PEM));
  43431. #endif
  43432. ExpectNotNull(pkey = X509_get_pubkey(ecX509));
  43433. /* current ECC key is 256 bit (32 bytes) */
  43434. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  43435. X509_free(ecX509);
  43436. EVP_PKEY_free(pkey);
  43437. #endif /* HAVE_ECC */
  43438. }
  43439. /* Tests should fail with passed in NULL pointer */
  43440. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, NULL),
  43441. SSL_FAILURE);
  43442. #if !defined(NO_DH) && !defined(NO_DSA)
  43443. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, NULL),
  43444. SSL_FAILURE);
  43445. #endif
  43446. #ifdef HAVE_ECC
  43447. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, NULL),
  43448. SSL_FAILURE);
  43449. #endif
  43450. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  43451. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  43452. */
  43453. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, x509),
  43454. SSL_SUCCESS);
  43455. if (EXPECT_FAIL()) {
  43456. wolfSSL_X509_free(x509);
  43457. }
  43458. /* Test with SSL_CTRL_OPTIONS
  43459. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  43460. */
  43461. ExpectTrue(wolfSSL_CTX_ctrl(ctx, SSL_CTRL_OPTIONS, SSL_OP_NO_TLSv1,
  43462. NULL) == SSL_OP_NO_TLSv1);
  43463. ExpectTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  43464. /* Test with SSL_CTRL_SET_TMP_DH
  43465. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  43466. */
  43467. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  43468. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, dh),
  43469. SSL_SUCCESS);
  43470. #endif
  43471. /* Test with SSL_CTRL_SET_TMP_ECDH
  43472. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  43473. */
  43474. #ifdef HAVE_ECC
  43475. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, ecKey),
  43476. SSL_SUCCESS);
  43477. #endif
  43478. #ifdef WOLFSSL_ENCRYPTED_KEYS
  43479. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  43480. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  43481. #endif
  43482. /* Test for min/max proto */
  43483. #ifndef WOLFSSL_NO_TLS12
  43484. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  43485. 0, NULL), SSL_SUCCESS);
  43486. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  43487. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  43488. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  43489. #endif
  43490. #ifdef WOLFSSL_TLS13
  43491. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  43492. 0, NULL), SSL_SUCCESS);
  43493. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  43494. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  43495. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  43496. #ifndef WOLFSSL_NO_TLS12
  43497. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  43498. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  43499. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  43500. #endif
  43501. #endif
  43502. /* Cleanup and Pass */
  43503. #if !defined(NO_DH) && !defined(NO_DSA)
  43504. #ifndef NO_BIO
  43505. BIO_free(bio);
  43506. DSA_free(dsa);
  43507. DH_free(dh);
  43508. dh = NULL;
  43509. #endif
  43510. #endif
  43511. #ifdef HAVE_ECC
  43512. wolfSSL_EC_KEY_free(ecKey);
  43513. #endif
  43514. SSL_CTX_free(ctx);
  43515. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  43516. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  43517. return EXPECT_RESULT();
  43518. }
  43519. static int test_wolfSSL_EVP_PKEY_assign(void)
  43520. {
  43521. EXPECT_DECLS;
  43522. #if !defined(NO_RSA) || !defined(NO_DSA) || defined(HAVE_ECC)
  43523. int type;
  43524. WOLFSSL_EVP_PKEY* pkey = NULL;
  43525. #ifndef NO_RSA
  43526. WOLFSSL_RSA* rsa = NULL;
  43527. #endif
  43528. #ifndef NO_DSA
  43529. WOLFSSL_DSA* dsa = NULL;
  43530. #endif
  43531. #ifdef HAVE_ECC
  43532. WOLFSSL_EC_KEY* ecKey = NULL;
  43533. #endif
  43534. #ifndef NO_RSA
  43535. type = EVP_PKEY_RSA;
  43536. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43537. ExpectNotNull(rsa = wolfSSL_RSA_new());
  43538. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, rsa), WOLFSSL_FAILURE);
  43539. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  43540. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, rsa), WOLFSSL_FAILURE);
  43541. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, rsa), WOLFSSL_SUCCESS);
  43542. if (EXPECT_FAIL()) {
  43543. wolfSSL_RSA_free(rsa);
  43544. }
  43545. wolfSSL_EVP_PKEY_free(pkey);
  43546. pkey = NULL;
  43547. #endif /* NO_RSA */
  43548. #ifndef NO_DSA
  43549. type = EVP_PKEY_DSA;
  43550. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43551. ExpectNotNull(dsa = wolfSSL_DSA_new());
  43552. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, dsa), WOLFSSL_FAILURE);
  43553. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  43554. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, dsa), WOLFSSL_FAILURE);
  43555. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, dsa), WOLFSSL_SUCCESS);
  43556. if (EXPECT_FAIL()) {
  43557. wolfSSL_DSA_free(dsa);
  43558. }
  43559. wolfSSL_EVP_PKEY_free(pkey);
  43560. pkey = NULL;
  43561. #endif /* NO_DSA */
  43562. #ifdef HAVE_ECC
  43563. type = EVP_PKEY_EC;
  43564. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43565. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  43566. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, ecKey), WOLFSSL_FAILURE);
  43567. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  43568. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, ecKey), WOLFSSL_FAILURE);
  43569. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_FAILURE);
  43570. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  43571. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_SUCCESS);
  43572. if (EXPECT_FAIL()) {
  43573. wolfSSL_EC_KEY_free(ecKey);
  43574. }
  43575. wolfSSL_EVP_PKEY_free(pkey);
  43576. pkey = NULL;
  43577. #endif /* HAVE_ECC */
  43578. #endif /* !NO_RSA || !NO_DSA || HAVE_ECC */
  43579. return EXPECT_RESULT();
  43580. }
  43581. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  43582. {
  43583. EXPECT_DECLS;
  43584. #if !defined(NO_DH) && \
  43585. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43586. XFILE f = XBADFILE;
  43587. unsigned char buf[4096];
  43588. const unsigned char* pt = buf;
  43589. const char* params1 = "./certs/dh2048.der";
  43590. long len = 0;
  43591. WOLFSSL_DH* dh = NULL;
  43592. WOLFSSL_EVP_PKEY* pkey = NULL;
  43593. XMEMSET(buf, 0, sizeof(buf));
  43594. /* Load DH parameters DER. */
  43595. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  43596. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  43597. if (f != XBADFILE)
  43598. XFCLOSE(f);
  43599. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  43600. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  43601. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43602. /* Bad cases */
  43603. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  43604. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  43605. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  43606. /* Good case */
  43607. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  43608. if (EXPECT_FAIL()) {
  43609. wolfSSL_DH_free(dh);
  43610. }
  43611. EVP_PKEY_free(pkey);
  43612. #endif
  43613. return EXPECT_RESULT();
  43614. }
  43615. static int test_wolfSSL_EVP_PKEY_base_id(void)
  43616. {
  43617. EXPECT_DECLS;
  43618. WOLFSSL_EVP_PKEY* pkey = NULL;
  43619. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43620. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  43621. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  43622. EVP_PKEY_free(pkey);
  43623. return EXPECT_RESULT();
  43624. }
  43625. static int test_wolfSSL_EVP_PKEY_id(void)
  43626. {
  43627. EXPECT_DECLS;
  43628. WOLFSSL_EVP_PKEY* pkey = NULL;
  43629. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43630. ExpectIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  43631. ExpectIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  43632. EVP_PKEY_free(pkey);
  43633. return EXPECT_RESULT();
  43634. }
  43635. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  43636. {
  43637. EXPECT_DECLS;
  43638. /* ECC check taken from ecc.c. It is the condition that defines ECC256 */
  43639. #if defined(OPENSSL_ALL) && !defined(NO_ECC_SECP) && \
  43640. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  43641. ECC_MIN_KEY_SZ <= 256)
  43642. EVP_PKEY_CTX* ctx = NULL;
  43643. EVP_PKEY* pkey = NULL;
  43644. /* Test error conditions. */
  43645. ExpectIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  43646. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  43647. ExpectIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  43648. #ifndef NO_RSA
  43649. EVP_PKEY_CTX_free(ctx);
  43650. /* Parameter generation for RSA not supported yet. */
  43651. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  43652. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  43653. #endif
  43654. #ifdef HAVE_ECC
  43655. EVP_PKEY_CTX_free(ctx);
  43656. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  43657. ExpectIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  43658. ExpectIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  43659. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  43660. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  43661. ExpectIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  43662. WOLFSSL_SUCCESS);
  43663. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  43664. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  43665. #endif
  43666. EVP_PKEY_CTX_free(ctx);
  43667. EVP_PKEY_free(pkey);
  43668. #endif
  43669. return EXPECT_RESULT();
  43670. }
  43671. static int test_wolfSSL_EVP_PKEY_keygen(void)
  43672. {
  43673. EXPECT_DECLS;
  43674. WOLFSSL_EVP_PKEY* pkey = NULL;
  43675. EVP_PKEY_CTX* ctx = NULL;
  43676. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  43677. WOLFSSL_EVP_PKEY* params = NULL;
  43678. DH* dh = NULL;
  43679. const BIGNUM* pubkey = NULL;
  43680. const BIGNUM* privkey = NULL;
  43681. ASN1_INTEGER* asn1int = NULL;
  43682. unsigned int length = 0;
  43683. byte* derBuffer = NULL;
  43684. #endif
  43685. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43686. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43687. /* Bad cases */
  43688. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  43689. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  43690. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  43691. /* Good case */
  43692. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  43693. EVP_PKEY_CTX_free(ctx);
  43694. ctx = NULL;
  43695. EVP_PKEY_free(pkey);
  43696. pkey = NULL;
  43697. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  43698. /* Test DH keygen */
  43699. {
  43700. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  43701. ExpectNotNull(dh = DH_get_2048_256());
  43702. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  43703. ExpectNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  43704. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  43705. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  43706. DH_free(dh);
  43707. dh = NULL;
  43708. EVP_PKEY_CTX_free(ctx);
  43709. EVP_PKEY_free(params);
  43710. /* try exporting generated key to DER, to verify */
  43711. ExpectNotNull(dh = EVP_PKEY_get1_DH(pkey));
  43712. DH_get0_key(dh, &pubkey, &privkey);
  43713. ExpectNotNull(pubkey);
  43714. ExpectNotNull(privkey);
  43715. ExpectNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  43716. ExpectIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  43717. ASN1_INTEGER_free(asn1int);
  43718. DH_free(dh);
  43719. dh = NULL;
  43720. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43721. EVP_PKEY_free(pkey);
  43722. }
  43723. #endif
  43724. return EXPECT_RESULT();
  43725. }
  43726. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  43727. {
  43728. EXPECT_DECLS;
  43729. WOLFSSL_EVP_PKEY* pkey = NULL;
  43730. EVP_PKEY_CTX *ctx = NULL;
  43731. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43732. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43733. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  43734. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(NULL), WOLFSSL_SUCCESS);
  43735. EVP_PKEY_CTX_free(ctx);
  43736. EVP_PKEY_free(pkey);
  43737. return EXPECT_RESULT();
  43738. }
  43739. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  43740. {
  43741. EXPECT_DECLS;
  43742. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  43743. WOLFSSL_EVP_PKEY* pkey = NULL;
  43744. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43745. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  43746. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(NULL), 0);
  43747. EVP_PKEY_free(pkey);
  43748. #endif
  43749. return EXPECT_RESULT();
  43750. }
  43751. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  43752. {
  43753. EXPECT_DECLS;
  43754. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  43755. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  43756. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  43757. !defined(NO_FILESYSTEM)
  43758. WOLFSSL_EVP_PKEY* params = NULL;
  43759. WOLFSSL_EVP_PKEY* copy = NULL;
  43760. DH* dh = NULL;
  43761. BIGNUM* p1;
  43762. BIGNUM* g1;
  43763. BIGNUM* q1;
  43764. BIGNUM* p2;
  43765. BIGNUM* g2;
  43766. BIGNUM* q2;
  43767. /* create DH with DH_get_2048_256 params */
  43768. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  43769. ExpectNotNull(dh = DH_get_2048_256());
  43770. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  43771. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  43772. (const BIGNUM**)&q1,
  43773. (const BIGNUM**)&g1);
  43774. DH_free(dh);
  43775. dh = NULL;
  43776. /* create DH with random generated DH params */
  43777. ExpectNotNull(copy = wolfSSL_EVP_PKEY_new());
  43778. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  43779. ExpectIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  43780. DH_free(dh);
  43781. dh = NULL;
  43782. ExpectIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  43783. ExpectNotNull(dh = EVP_PKEY_get1_DH(copy));
  43784. ExpectNotNull(dh->p);
  43785. ExpectNotNull(dh->g);
  43786. ExpectNotNull(dh->q);
  43787. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  43788. (const BIGNUM**)&q2,
  43789. (const BIGNUM**)&g2);
  43790. ExpectIntEQ(BN_cmp(p1, p2), 0);
  43791. ExpectIntEQ(BN_cmp(q1, q2), 0);
  43792. ExpectIntEQ(BN_cmp(g1, g2), 0);
  43793. DH_free(dh);
  43794. dh = NULL;
  43795. EVP_PKEY_free(copy);
  43796. EVP_PKEY_free(params);
  43797. #endif
  43798. return EXPECT_RESULT();
  43799. }
  43800. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  43801. {
  43802. EXPECT_DECLS;
  43803. WOLFSSL_EVP_PKEY* pkey = NULL;
  43804. EVP_PKEY_CTX* ctx = NULL;
  43805. int bits = 2048;
  43806. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43807. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43808. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  43809. WOLFSSL_SUCCESS);
  43810. EVP_PKEY_CTX_free(ctx);
  43811. EVP_PKEY_free(pkey);
  43812. return EXPECT_RESULT();
  43813. }
  43814. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  43815. {
  43816. EXPECT_DECLS;
  43817. /* This is large enough to be used for all key sizes */
  43818. byte key[AES_256_KEY_SIZE] = {0};
  43819. byte iv[AES_BLOCK_SIZE] = {0};
  43820. int i;
  43821. int nids[] = {
  43822. #ifdef HAVE_AES_CBC
  43823. NID_aes_128_cbc,
  43824. #endif
  43825. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43826. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43827. #ifdef HAVE_AESGCM
  43828. NID_aes_128_gcm,
  43829. #endif
  43830. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43831. #ifdef WOLFSSL_AES_COUNTER
  43832. NID_aes_128_ctr,
  43833. #endif
  43834. #ifndef NO_DES3
  43835. NID_des_cbc,
  43836. NID_des_ede3_cbc,
  43837. #endif
  43838. };
  43839. int iv_lengths[] = {
  43840. #ifdef HAVE_AES_CBC
  43841. AES_BLOCK_SIZE,
  43842. #endif
  43843. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43844. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43845. #ifdef HAVE_AESGCM
  43846. GCM_NONCE_MID_SZ,
  43847. #endif
  43848. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43849. #ifdef WOLFSSL_AES_COUNTER
  43850. AES_BLOCK_SIZE,
  43851. #endif
  43852. #ifndef NO_DES3
  43853. DES_BLOCK_SIZE,
  43854. DES_BLOCK_SIZE,
  43855. #endif
  43856. };
  43857. int nidsLen = (sizeof(nids)/sizeof(int));
  43858. for (i = 0; i < nidsLen; i++) {
  43859. const EVP_CIPHER* init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  43860. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  43861. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43862. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43863. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  43864. EVP_CIPHER_CTX_free(ctx);
  43865. }
  43866. return EXPECT_RESULT();
  43867. }
  43868. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  43869. {
  43870. EXPECT_DECLS;
  43871. byte key[AES_256_KEY_SIZE] = {0};
  43872. byte iv[AES_BLOCK_SIZE] = {0};
  43873. int i;
  43874. int nids[] = {
  43875. #ifdef HAVE_AES_CBC
  43876. NID_aes_128_cbc,
  43877. NID_aes_256_cbc,
  43878. #endif
  43879. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43880. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43881. #ifdef HAVE_AESGCM
  43882. NID_aes_128_gcm,
  43883. NID_aes_256_gcm,
  43884. #endif
  43885. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43886. #ifdef WOLFSSL_AES_COUNTER
  43887. NID_aes_128_ctr,
  43888. NID_aes_256_ctr,
  43889. #endif
  43890. #ifndef NO_DES3
  43891. NID_des_cbc,
  43892. NID_des_ede3_cbc,
  43893. #endif
  43894. };
  43895. int key_lengths[] = {
  43896. #ifdef HAVE_AES_CBC
  43897. AES_128_KEY_SIZE,
  43898. AES_256_KEY_SIZE,
  43899. #endif
  43900. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43901. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43902. #ifdef HAVE_AESGCM
  43903. AES_128_KEY_SIZE,
  43904. AES_256_KEY_SIZE,
  43905. #endif
  43906. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43907. #ifdef WOLFSSL_AES_COUNTER
  43908. AES_128_KEY_SIZE,
  43909. AES_256_KEY_SIZE,
  43910. #endif
  43911. #ifndef NO_DES3
  43912. DES_KEY_SIZE,
  43913. DES3_KEY_SIZE,
  43914. #endif
  43915. };
  43916. int nidsLen = (sizeof(nids)/sizeof(int));
  43917. for (i = 0; i < nidsLen; i++) {
  43918. const EVP_CIPHER *init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  43919. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  43920. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43921. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43922. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), key_lengths[i]);
  43923. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, key_lengths[i]),
  43924. WOLFSSL_SUCCESS);
  43925. EVP_CIPHER_CTX_free(ctx);
  43926. }
  43927. return EXPECT_RESULT();
  43928. }
  43929. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  43930. {
  43931. EXPECT_DECLS;
  43932. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  43933. int ivLen, keyLen;
  43934. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  43935. #ifdef HAVE_AESGCM
  43936. byte key[AES_128_KEY_SIZE] = {0};
  43937. byte iv[AES_BLOCK_SIZE] = {0};
  43938. const EVP_CIPHER *init = EVP_aes_128_gcm();
  43939. #else
  43940. byte key[DES3_KEY_SIZE] = {0};
  43941. byte iv[DES_BLOCK_SIZE] = {0};
  43942. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  43943. #endif
  43944. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43945. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43946. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  43947. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  43948. /* Bad cases */
  43949. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen),
  43950. WOLFSSL_FAILURE);
  43951. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen),
  43952. WOLFSSL_FAILURE);
  43953. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  43954. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  43955. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen),
  43956. WOLFSSL_FAILURE);
  43957. /* Good case */
  43958. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  43959. EVP_CIPHER_CTX_free(ctx);
  43960. #endif
  43961. return EXPECT_RESULT();
  43962. }
  43963. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  43964. {
  43965. EXPECT_DECLS;
  43966. WOLFSSL_ENGINE* e = NULL;
  43967. int id = 0;
  43968. EVP_PKEY_CTX *ctx = NULL;
  43969. ExpectNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  43970. EVP_PKEY_CTX_free(ctx);
  43971. return EXPECT_RESULT();
  43972. }
  43973. static int test_wolfSSL_EVP_rc4(void)
  43974. {
  43975. EXPECT_DECLS;
  43976. #if !defined(NO_RC4)
  43977. ExpectNotNull(wolfSSL_EVP_rc4());
  43978. #endif
  43979. return EXPECT_RESULT();
  43980. }
  43981. static int test_wolfSSL_EVP_enc_null(void)
  43982. {
  43983. EXPECT_DECLS;
  43984. ExpectNotNull(wolfSSL_EVP_enc_null());
  43985. return EXPECT_RESULT();
  43986. }
  43987. static int test_wolfSSL_EVP_rc2_cbc(void)
  43988. {
  43989. EXPECT_DECLS;
  43990. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  43991. ExpectNull(wolfSSL_EVP_rc2_cbc());
  43992. #endif
  43993. return EXPECT_RESULT();
  43994. }
  43995. static int test_wolfSSL_EVP_mdc2(void)
  43996. {
  43997. EXPECT_DECLS;
  43998. #if !defined(NO_WOLFSSL_STUB)
  43999. ExpectNull(wolfSSL_EVP_mdc2());
  44000. #endif
  44001. return EXPECT_RESULT();
  44002. }
  44003. static int test_wolfSSL_EVP_md4(void)
  44004. {
  44005. EXPECT_DECLS;
  44006. #if !defined(NO_MD4)
  44007. ExpectNotNull(wolfSSL_EVP_md4());
  44008. #endif
  44009. return EXPECT_RESULT();
  44010. }
  44011. static int test_wolfSSL_EVP_aes_256_gcm(void)
  44012. {
  44013. EXPECT_DECLS;
  44014. #ifdef HAVE_AESGCM
  44015. ExpectNotNull(wolfSSL_EVP_aes_256_gcm());
  44016. #endif
  44017. return EXPECT_RESULT();
  44018. }
  44019. static int test_wolfSSL_EVP_aes_192_gcm(void)
  44020. {
  44021. EXPECT_DECLS;
  44022. #ifdef HAVE_AESGCM
  44023. ExpectNotNull(wolfSSL_EVP_aes_192_gcm());
  44024. #endif
  44025. return EXPECT_RESULT();
  44026. }
  44027. static int test_wolfSSL_EVP_aes_256_ccm(void)
  44028. {
  44029. EXPECT_DECLS;
  44030. #ifdef HAVE_AESCCM
  44031. ExpectNotNull(wolfSSL_EVP_aes_256_ccm());
  44032. #endif
  44033. return EXPECT_RESULT();
  44034. }
  44035. static int test_wolfSSL_EVP_aes_192_ccm(void)
  44036. {
  44037. EXPECT_DECLS;
  44038. #ifdef HAVE_AESCCM
  44039. ExpectNotNull(wolfSSL_EVP_aes_192_ccm());
  44040. #endif
  44041. return EXPECT_RESULT();
  44042. }
  44043. static int test_wolfSSL_EVP_aes_128_ccm(void)
  44044. {
  44045. EXPECT_DECLS;
  44046. #ifdef HAVE_AESCCM
  44047. ExpectNotNull(wolfSSL_EVP_aes_128_ccm());
  44048. #endif
  44049. return EXPECT_RESULT();
  44050. }
  44051. static int test_wolfSSL_EVP_ripemd160(void)
  44052. {
  44053. EXPECT_DECLS;
  44054. #if !defined(NO_WOLFSSL_STUB)
  44055. ExpectNull(wolfSSL_EVP_ripemd160());
  44056. #endif
  44057. return EXPECT_RESULT();
  44058. }
  44059. static int test_wolfSSL_EVP_get_digestbynid(void)
  44060. {
  44061. EXPECT_DECLS;
  44062. #ifndef NO_MD5
  44063. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  44064. #endif
  44065. #ifndef NO_SHA
  44066. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  44067. #endif
  44068. #ifndef NO_SHA256
  44069. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
  44070. #endif
  44071. ExpectNull(wolfSSL_EVP_get_digestbynid(0));
  44072. return EXPECT_RESULT();
  44073. }
  44074. static int test_wolfSSL_EVP_MD_nid(void)
  44075. {
  44076. EXPECT_DECLS;
  44077. #ifndef NO_MD5
  44078. ExpectIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  44079. #endif
  44080. #ifndef NO_SHA
  44081. ExpectIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  44082. #endif
  44083. #ifndef NO_SHA256
  44084. ExpectIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  44085. #endif
  44086. ExpectIntEQ(EVP_MD_nid(NULL), NID_undef);
  44087. return EXPECT_RESULT();
  44088. }
  44089. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  44090. {
  44091. EXPECT_DECLS;
  44092. #if defined(HAVE_ECC)
  44093. WOLFSSL_EVP_PKEY* pkey = NULL;
  44094. ExpectNull(EVP_PKEY_get0_EC_KEY(NULL));
  44095. ExpectNotNull(pkey = EVP_PKEY_new());
  44096. ExpectNull(EVP_PKEY_get0_EC_KEY(pkey));
  44097. EVP_PKEY_free(pkey);
  44098. #endif
  44099. return EXPECT_RESULT();
  44100. }
  44101. static int test_wolfSSL_EVP_X_STATE(void)
  44102. {
  44103. EXPECT_DECLS;
  44104. #if !defined(NO_DES3) && !defined(NO_RC4)
  44105. byte key[DES3_KEY_SIZE] = {0};
  44106. byte iv[DES_IV_SIZE] = {0};
  44107. EVP_CIPHER_CTX *ctx = NULL;
  44108. const EVP_CIPHER *init = NULL;
  44109. /* Bad test cases */
  44110. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44111. ExpectNotNull(init = EVP_des_ede3_cbc());
  44112. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44113. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44114. ExpectNull(wolfSSL_EVP_X_STATE(NULL));
  44115. ExpectNull(wolfSSL_EVP_X_STATE(ctx));
  44116. EVP_CIPHER_CTX_free(ctx);
  44117. ctx = NULL;
  44118. /* Good test case */
  44119. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44120. ExpectNotNull(init = wolfSSL_EVP_rc4());
  44121. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44122. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44123. ExpectNotNull(wolfSSL_EVP_X_STATE(ctx));
  44124. EVP_CIPHER_CTX_free(ctx);
  44125. #endif
  44126. return EXPECT_RESULT();
  44127. }
  44128. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  44129. {
  44130. EXPECT_DECLS;
  44131. #if !defined(NO_DES3) && !defined(NO_RC4)
  44132. byte key[DES3_KEY_SIZE] = {0};
  44133. byte iv[DES_IV_SIZE] = {0};
  44134. EVP_CIPHER_CTX *ctx = NULL;
  44135. const EVP_CIPHER *init = NULL;
  44136. /* Bad test cases */
  44137. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44138. ExpectNotNull(init = EVP_des_ede3_cbc());
  44139. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44140. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44141. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  44142. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  44143. EVP_CIPHER_CTX_free(ctx);
  44144. ctx = NULL;
  44145. /* Good test case */
  44146. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44147. ExpectNotNull(init = wolfSSL_EVP_rc4());
  44148. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44149. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44150. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  44151. EVP_CIPHER_CTX_free(ctx);
  44152. #endif
  44153. return EXPECT_RESULT();
  44154. }
  44155. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  44156. {
  44157. EXPECT_DECLS;
  44158. #if defined(HAVE_AES_CBC) || defined(HAVE_AESGCM) || \
  44159. defined(WOLFSSL_AES_COUNTER) || defined(HAVE_AES_ECB) || \
  44160. defined(WOLFSSL_AES_OFB) || !defined(NO_RC4) || \
  44161. (defined(HAVE_CHACHA) && defined(HAVE_POLY1305))
  44162. #ifdef HAVE_AES_CBC
  44163. #ifdef WOLFSSL_AES_128
  44164. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  44165. #endif
  44166. #ifdef WOLFSSL_AES_192
  44167. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  44168. #endif
  44169. #ifdef WOLFSSL_AES_256
  44170. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  44171. #endif
  44172. #endif
  44173. #ifdef HAVE_AESGCM
  44174. #ifdef WOLFSSL_AES_128
  44175. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  44176. #endif
  44177. #ifdef WOLFSSL_AES_192
  44178. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  44179. #endif
  44180. #ifdef WOLFSSL_AES_256
  44181. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  44182. #endif
  44183. #endif
  44184. #ifdef HAVE_AESCCM
  44185. #ifdef WOLFSSL_AES_128
  44186. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ccm()), 1);
  44187. #endif
  44188. #ifdef WOLFSSL_AES_192
  44189. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ccm()), 1);
  44190. #endif
  44191. #ifdef WOLFSSL_AES_256
  44192. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ccm()), 1);
  44193. #endif
  44194. #endif
  44195. #ifdef WOLFSSL_AES_COUNTER
  44196. #ifdef WOLFSSL_AES_128
  44197. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  44198. #endif
  44199. #ifdef WOLFSSL_AES_192
  44200. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  44201. #endif
  44202. #ifdef WOLFSSL_AES_256
  44203. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  44204. #endif
  44205. #endif
  44206. #ifdef HAVE_AES_ECB
  44207. #ifdef WOLFSSL_AES_128
  44208. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  44209. #endif
  44210. #ifdef WOLFSSL_AES_192
  44211. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  44212. #endif
  44213. #ifdef WOLFSSL_AES_256
  44214. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  44215. #endif
  44216. #endif
  44217. #ifdef WOLFSSL_AES_OFB
  44218. #ifdef WOLFSSL_AES_128
  44219. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  44220. #endif
  44221. #ifdef WOLFSSL_AES_192
  44222. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  44223. #endif
  44224. #ifdef WOLFSSL_AES_256
  44225. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  44226. #endif
  44227. #endif
  44228. #ifndef NO_RC4
  44229. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  44230. #endif
  44231. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  44232. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  44233. #endif
  44234. #endif
  44235. #ifdef WOLFSSL_SM4_ECB
  44236. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ecb()), SM4_BLOCK_SIZE);
  44237. #endif
  44238. #ifdef WOLFSSL_SM4_CBC
  44239. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_cbc()), SM4_BLOCK_SIZE);
  44240. #endif
  44241. #ifdef WOLFSSL_SM4_CTR
  44242. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ctr()), 1);
  44243. #endif
  44244. #ifdef WOLFSSL_SM4_GCM
  44245. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_gcm()), 1);
  44246. #endif
  44247. #ifdef WOLFSSL_SM4_CCM
  44248. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ccm()), 1);
  44249. #endif
  44250. return EXPECT_RESULT();
  44251. }
  44252. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  44253. {
  44254. EXPECT_DECLS;
  44255. int nids[] = {
  44256. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  44257. #ifdef WOLFSSL_AES_128
  44258. NID_aes_128_cbc,
  44259. #endif
  44260. #ifdef WOLFSSL_AES_192
  44261. NID_aes_192_cbc,
  44262. #endif
  44263. #ifdef WOLFSSL_AES_256
  44264. NID_aes_256_cbc,
  44265. #endif
  44266. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  44267. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  44268. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44269. #ifdef HAVE_AESGCM
  44270. #ifdef WOLFSSL_AES_128
  44271. NID_aes_128_gcm,
  44272. #endif
  44273. #ifdef WOLFSSL_AES_192
  44274. NID_aes_192_gcm,
  44275. #endif
  44276. #ifdef WOLFSSL_AES_256
  44277. NID_aes_256_gcm,
  44278. #endif
  44279. #endif /* HAVE_AESGCM */
  44280. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  44281. #ifdef WOLFSSL_AES_COUNTER
  44282. #ifdef WOLFSSL_AES_128
  44283. NID_aes_128_ctr,
  44284. #endif
  44285. #ifdef WOLFSSL_AES_192
  44286. NID_aes_192_ctr,
  44287. #endif
  44288. #ifdef WOLFSSL_AES_256
  44289. NID_aes_256_ctr,
  44290. #endif
  44291. #endif
  44292. #ifndef NO_DES3
  44293. NID_des_cbc,
  44294. NID_des_ede3_cbc,
  44295. #endif
  44296. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  44297. NID_chacha20_poly1305,
  44298. #endif
  44299. };
  44300. int iv_lengths[] = {
  44301. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  44302. #ifdef WOLFSSL_AES_128
  44303. AES_BLOCK_SIZE,
  44304. #endif
  44305. #ifdef WOLFSSL_AES_192
  44306. AES_BLOCK_SIZE,
  44307. #endif
  44308. #ifdef WOLFSSL_AES_256
  44309. AES_BLOCK_SIZE,
  44310. #endif
  44311. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  44312. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  44313. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44314. #ifdef HAVE_AESGCM
  44315. #ifdef WOLFSSL_AES_128
  44316. GCM_NONCE_MID_SZ,
  44317. #endif
  44318. #ifdef WOLFSSL_AES_192
  44319. GCM_NONCE_MID_SZ,
  44320. #endif
  44321. #ifdef WOLFSSL_AES_256
  44322. GCM_NONCE_MID_SZ,
  44323. #endif
  44324. #endif /* HAVE_AESGCM */
  44325. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  44326. #ifdef WOLFSSL_AES_COUNTER
  44327. #ifdef WOLFSSL_AES_128
  44328. AES_BLOCK_SIZE,
  44329. #endif
  44330. #ifdef WOLFSSL_AES_192
  44331. AES_BLOCK_SIZE,
  44332. #endif
  44333. #ifdef WOLFSSL_AES_256
  44334. AES_BLOCK_SIZE,
  44335. #endif
  44336. #endif
  44337. #ifndef NO_DES3
  44338. DES_BLOCK_SIZE,
  44339. DES_BLOCK_SIZE,
  44340. #endif
  44341. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  44342. CHACHA20_POLY1305_AEAD_IV_SIZE,
  44343. #endif
  44344. };
  44345. int i;
  44346. int nidsLen = (sizeof(nids)/sizeof(int));
  44347. for (i = 0; i < nidsLen; i++) {
  44348. const EVP_CIPHER *c = EVP_get_cipherbynid(nids[i]);
  44349. ExpectIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  44350. }
  44351. return EXPECT_RESULT();
  44352. }
  44353. static int test_wolfSSL_EVP_SignInit_ex(void)
  44354. {
  44355. EXPECT_DECLS;
  44356. WOLFSSL_EVP_MD_CTX mdCtx;
  44357. WOLFSSL_ENGINE* e = 0;
  44358. const EVP_MD* md = EVP_sha256();
  44359. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  44360. ExpectIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  44361. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  44362. return EXPECT_RESULT();
  44363. }
  44364. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  44365. {
  44366. EXPECT_DECLS;
  44367. #if !defined(NO_SHA256)
  44368. WOLFSSL_EVP_MD_CTX mdCtx;
  44369. unsigned int s = 0;
  44370. unsigned char md[WC_SHA256_DIGEST_SIZE];
  44371. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  44372. /* Bad Case */
  44373. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  44374. (HAVE_FIPS_VERSION > 2))
  44375. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  44376. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  44377. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  44378. #else
  44379. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  44380. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  44381. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  44382. #endif
  44383. /* Good Case */
  44384. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  44385. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, EVP_sha256()), WOLFSSL_SUCCESS);
  44386. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  44387. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  44388. #endif
  44389. return EXPECT_RESULT();
  44390. }
  44391. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  44392. {
  44393. EXPECT_DECLS;
  44394. #if defined(OPENSSL_EXTRA)
  44395. EVP_PKEY* pkey = NULL;
  44396. EVP_PKEY_CTX* ctx = NULL;
  44397. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44398. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44399. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  44400. /* void */
  44401. EVP_PKEY_CTX_free(ctx);
  44402. #else
  44403. /* int */
  44404. ExpectIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  44405. #endif
  44406. EVP_PKEY_free(pkey);
  44407. #endif
  44408. return EXPECT_RESULT();
  44409. }
  44410. static int test_wolfSSL_EVP_PKEY_param_check(void)
  44411. {
  44412. EXPECT_DECLS;
  44413. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  44414. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  44415. DH *dh = NULL;
  44416. DH *setDh = NULL;
  44417. EVP_PKEY *pkey = NULL;
  44418. EVP_PKEY_CTX* ctx = NULL;
  44419. FILE* f = NULL;
  44420. unsigned char buf[512];
  44421. const unsigned char* pt = buf;
  44422. const char* dh2048 = "./certs/dh2048.der";
  44423. long len = 0;
  44424. int code = -1;
  44425. XMEMSET(buf, 0, sizeof(buf));
  44426. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  44427. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  44428. if (f != XBADFILE)
  44429. XFCLOSE(f);
  44430. /* Load dh2048.der into DH with internal format */
  44431. ExpectNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  44432. ExpectIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  44433. ExpectIntEQ(code, 0);
  44434. code = -1;
  44435. pkey = wolfSSL_EVP_PKEY_new();
  44436. /* Set DH into PKEY */
  44437. ExpectIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  44438. /* create ctx from pkey */
  44439. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44440. ExpectIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  44441. /* TODO: more invalid cases */
  44442. ExpectIntEQ(EVP_PKEY_param_check(NULL), 0);
  44443. EVP_PKEY_CTX_free(ctx);
  44444. EVP_PKEY_free(pkey);
  44445. DH_free(setDh);
  44446. setDh = NULL;
  44447. DH_free(dh);
  44448. dh = NULL;
  44449. #endif
  44450. #endif
  44451. return EXPECT_RESULT();
  44452. }
  44453. static int test_wolfSSL_EVP_BytesToKey(void)
  44454. {
  44455. EXPECT_DECLS;
  44456. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  44457. byte key[AES_BLOCK_SIZE] = {0};
  44458. byte iv[AES_BLOCK_SIZE] = {0};
  44459. int count = 0;
  44460. const EVP_MD* md = EVP_sha256();
  44461. const EVP_CIPHER *type;
  44462. const unsigned char *salt = (unsigned char *)"salt1234";
  44463. int sz = 5;
  44464. const byte data[] = {
  44465. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  44466. 0x72,0x6c,0x64
  44467. };
  44468. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  44469. /* Bad cases */
  44470. ExpectIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  44471. 0);
  44472. ExpectIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  44473. 16);
  44474. md = "2";
  44475. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  44476. WOLFSSL_FAILURE);
  44477. /* Good case */
  44478. md = EVP_sha256();
  44479. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  44480. 16);
  44481. #endif
  44482. return EXPECT_RESULT();
  44483. }
  44484. static int test_evp_cipher_aes_gcm(void)
  44485. {
  44486. EXPECT_DECLS;
  44487. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  44488. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  44489. (HAVE_FIPS_VERSION >= 2)))
  44490. /*
  44491. * This test checks data at various points in the encrypt/decrypt process
  44492. * against known values produced using the same test with OpenSSL. This
  44493. * interop testing is critical for verifying the correctness of our
  44494. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  44495. * a flow supported by OpenSSL that uses the control command
  44496. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  44497. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  44498. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  44499. * wolfSSL OpenSSH clients because there was a bug in this flow that
  44500. * happened to "cancel out" if both sides of the connection had the bug.
  44501. */
  44502. enum {
  44503. NUM_ENCRYPTIONS = 3,
  44504. AAD_SIZE = 4
  44505. };
  44506. byte plainText1[] = {
  44507. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  44508. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  44509. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  44510. };
  44511. byte plainText2[] = {
  44512. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  44513. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  44514. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  44515. };
  44516. byte plainText3[] = {
  44517. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  44518. 0x5b, 0x57, 0x10
  44519. };
  44520. byte* plainTexts[NUM_ENCRYPTIONS] = {
  44521. plainText1,
  44522. plainText2,
  44523. plainText3
  44524. };
  44525. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  44526. sizeof(plainText1),
  44527. sizeof(plainText2),
  44528. sizeof(plainText3)
  44529. };
  44530. byte aad1[AAD_SIZE] = {
  44531. 0x00, 0x00, 0x00, 0x01
  44532. };
  44533. byte aad2[AAD_SIZE] = {
  44534. 0x00, 0x00, 0x00, 0x10
  44535. };
  44536. byte aad3[AAD_SIZE] = {
  44537. 0x00, 0x00, 0x01, 0x00
  44538. };
  44539. byte* aads[NUM_ENCRYPTIONS] = {
  44540. aad1,
  44541. aad2,
  44542. aad3
  44543. };
  44544. const byte iv[GCM_NONCE_MID_SZ] = {
  44545. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  44546. };
  44547. byte currentIv[GCM_NONCE_MID_SZ];
  44548. const byte key[] = {
  44549. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  44550. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  44551. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  44552. };
  44553. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  44554. {
  44555. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  44556. 0xEF
  44557. },
  44558. {
  44559. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  44560. 0xF0
  44561. },
  44562. {
  44563. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  44564. 0xF1
  44565. }
  44566. };
  44567. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  44568. {
  44569. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  44570. 0x3D, 0x32, 0x18, 0x34, 0xA9
  44571. },
  44572. {
  44573. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  44574. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  44575. },
  44576. {
  44577. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  44578. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  44579. }
  44580. };
  44581. const byte expCipherText1[] = {
  44582. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  44583. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  44584. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  44585. };
  44586. const byte expCipherText2[] = {
  44587. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  44588. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  44589. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  44590. };
  44591. const byte expCipherText3[] = {
  44592. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  44593. 0x80, 0x5E, 0x6B,
  44594. };
  44595. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  44596. expCipherText1,
  44597. expCipherText2,
  44598. expCipherText3
  44599. };
  44600. byte* cipherText = NULL;
  44601. byte* calcPlainText = NULL;
  44602. byte tag[AES_BLOCK_SIZE];
  44603. EVP_CIPHER_CTX* encCtx = NULL;
  44604. EVP_CIPHER_CTX* decCtx = NULL;
  44605. int i, j, outl;
  44606. /****************************************************/
  44607. for (i = 0; i < 3; ++i) {
  44608. ExpectNotNull(encCtx = EVP_CIPHER_CTX_new());
  44609. ExpectNotNull(decCtx = EVP_CIPHER_CTX_new());
  44610. /* First iteration, set key before IV. */
  44611. if (i == 0) {
  44612. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  44613. SSL_SUCCESS);
  44614. /*
  44615. * The call to EVP_CipherInit below (with NULL key) should clear the
  44616. * authIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  44617. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  44618. * behavior.
  44619. */
  44620. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  44621. (void*)iv), SSL_SUCCESS);
  44622. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  44623. SSL_SUCCESS);
  44624. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  44625. currentIv), SSL_FAILURE);
  44626. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  44627. SSL_SUCCESS);
  44628. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  44629. SSL_SUCCESS);
  44630. }
  44631. /* Second iteration, IV before key. */
  44632. else {
  44633. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  44634. SSL_SUCCESS);
  44635. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  44636. SSL_SUCCESS);
  44637. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  44638. SSL_SUCCESS);
  44639. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  44640. SSL_SUCCESS);
  44641. }
  44642. /*
  44643. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  44644. * been issued first.
  44645. */
  44646. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  44647. currentIv), SSL_FAILURE);
  44648. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  44649. (void*)iv), SSL_SUCCESS);
  44650. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  44651. (void*)iv), SSL_SUCCESS);
  44652. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  44653. /*************** Encrypt ***************/
  44654. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  44655. currentIv), SSL_SUCCESS);
  44656. /* Check current IV against expected. */
  44657. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  44658. /* Add AAD. */
  44659. if (i == 2) {
  44660. /* Test streaming API. */
  44661. ExpectIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  44662. AAD_SIZE), SSL_SUCCESS);
  44663. }
  44664. else {
  44665. ExpectIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  44666. AAD_SIZE);
  44667. }
  44668. ExpectNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  44669. DYNAMIC_TYPE_TMP_BUFFER));
  44670. /* Encrypt plaintext. */
  44671. if (i == 2) {
  44672. ExpectIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  44673. plainTexts[j], plainTextSzs[j]),
  44674. SSL_SUCCESS);
  44675. }
  44676. else {
  44677. ExpectIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  44678. plainTextSzs[j]), plainTextSzs[j]);
  44679. }
  44680. if (i == 2) {
  44681. ExpectIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  44682. SSL_SUCCESS);
  44683. }
  44684. else {
  44685. /*
  44686. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  44687. * akin to calling EVP_CipherFinal.
  44688. */
  44689. ExpectIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  44690. }
  44691. /* Check ciphertext against expected. */
  44692. ExpectIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  44693. 0);
  44694. /* Get and check tag against expected. */
  44695. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  44696. sizeof(tag), tag), SSL_SUCCESS);
  44697. ExpectIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  44698. /*************** Decrypt ***************/
  44699. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  44700. currentIv), SSL_SUCCESS);
  44701. /* Check current IV against expected. */
  44702. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  44703. /* Add AAD. */
  44704. if (i == 2) {
  44705. /* Test streaming API. */
  44706. ExpectIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  44707. AAD_SIZE), SSL_SUCCESS);
  44708. }
  44709. else {
  44710. ExpectIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  44711. AAD_SIZE);
  44712. }
  44713. /* Set expected tag. */
  44714. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  44715. sizeof(tag), tag), SSL_SUCCESS);
  44716. /* Decrypt ciphertext. */
  44717. ExpectNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  44718. DYNAMIC_TYPE_TMP_BUFFER));
  44719. if (i == 2) {
  44720. ExpectIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  44721. cipherText, plainTextSzs[j]),
  44722. SSL_SUCCESS);
  44723. }
  44724. else {
  44725. /* This first EVP_Cipher call will check the tag, too. */
  44726. ExpectIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  44727. plainTextSzs[j]), plainTextSzs[j]);
  44728. }
  44729. if (i == 2) {
  44730. ExpectIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  44731. SSL_SUCCESS);
  44732. }
  44733. else {
  44734. ExpectIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  44735. }
  44736. /* Check plaintext against expected. */
  44737. ExpectIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  44738. 0);
  44739. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44740. cipherText = NULL;
  44741. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44742. calcPlainText = NULL;
  44743. }
  44744. EVP_CIPHER_CTX_free(encCtx);
  44745. encCtx = NULL;
  44746. EVP_CIPHER_CTX_free(decCtx);
  44747. decCtx = NULL;
  44748. }
  44749. #endif
  44750. return EXPECT_RESULT();
  44751. }
  44752. static int test_wolfSSL_OBJ_ln(void)
  44753. {
  44754. EXPECT_DECLS;
  44755. const int nid_set[] = {
  44756. NID_commonName,
  44757. NID_serialNumber,
  44758. NID_countryName,
  44759. NID_localityName,
  44760. NID_stateOrProvinceName,
  44761. NID_organizationName,
  44762. NID_organizationalUnitName,
  44763. NID_domainComponent,
  44764. NID_businessCategory,
  44765. NID_jurisdictionCountryName,
  44766. NID_jurisdictionStateOrProvinceName,
  44767. NID_emailAddress
  44768. };
  44769. const char* ln_set[] = {
  44770. "commonName",
  44771. "serialNumber",
  44772. "countryName",
  44773. "localityName",
  44774. "stateOrProvinceName",
  44775. "organizationName",
  44776. "organizationalUnitName",
  44777. "domainComponent",
  44778. "businessCategory",
  44779. "jurisdictionCountryName",
  44780. "jurisdictionStateOrProvinceName",
  44781. "emailAddress",
  44782. };
  44783. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  44784. ExpectIntEQ(OBJ_ln2nid(NULL), NID_undef);
  44785. #ifdef HAVE_ECC
  44786. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44787. {
  44788. EC_builtin_curve r[27];
  44789. size_t nCurves = sizeof(r) / sizeof(r[0]);
  44790. nCurves = EC_get_builtin_curves(r, nCurves);
  44791. for (i = 0; i < nCurves; i++) {
  44792. /* skip ECC_CURVE_INVALID */
  44793. if (r[i].nid != ECC_CURVE_INVALID) {
  44794. ExpectIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  44795. ExpectStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  44796. }
  44797. }
  44798. }
  44799. #endif
  44800. #endif
  44801. for (i = 0; i < maxIdx; i++) {
  44802. ExpectIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  44803. ExpectStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  44804. }
  44805. return EXPECT_RESULT();
  44806. }
  44807. static int test_wolfSSL_OBJ_sn(void)
  44808. {
  44809. EXPECT_DECLS;
  44810. int i = 0, maxIdx = 7;
  44811. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  44812. NID_stateOrProvinceName,NID_organizationName,
  44813. NID_organizationalUnitName,NID_emailAddress};
  44814. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  44815. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  44816. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  44817. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  44818. WOLFSSL_EMAIL_ADDR};
  44819. ExpectIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  44820. for (i = 0; i < maxIdx; i++) {
  44821. ExpectIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  44822. ExpectStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  44823. }
  44824. return EXPECT_RESULT();
  44825. }
  44826. #if !defined(NO_BIO)
  44827. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  44828. {
  44829. return lh_strhash(s[3]);
  44830. }
  44831. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  44832. {
  44833. return XSTRCMP(a[3], b[3]);
  44834. }
  44835. #endif
  44836. static int test_wolfSSL_TXT_DB(void)
  44837. {
  44838. EXPECT_DECLS;
  44839. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  44840. BIO *bio = NULL;
  44841. TXT_DB *db = NULL;
  44842. const int columns = 6;
  44843. const char *fields[6] = {
  44844. "V",
  44845. "320926161116Z",
  44846. "",
  44847. "12BD",
  44848. "unknown",
  44849. "/CN=rsa doe",
  44850. };
  44851. char** fields_copy = NULL;
  44852. /* Test read */
  44853. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  44854. ExpectIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  44855. ExpectNotNull(db = TXT_DB_read(bio, columns));
  44856. ExpectNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  44857. DYNAMIC_TYPE_OPENSSL));
  44858. if (fields_copy != NULL) {
  44859. XMEMCPY(fields_copy, fields, sizeof(fields));
  44860. }
  44861. ExpectIntEQ(TXT_DB_insert(db, fields_copy), 1);
  44862. if (EXPECT_FAIL()) {
  44863. XFREE(fields_copy, NULL, DYNAMIC_TYPE_OPENSSL);
  44864. }
  44865. BIO_free(bio);
  44866. bio = NULL;
  44867. /* Test write */
  44868. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  44869. ExpectIntEQ(TXT_DB_write(bio, db), 1484);
  44870. BIO_free(bio);
  44871. /* Test index */
  44872. ExpectIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  44873. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  44874. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44875. fields[3] = "12DA";
  44876. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44877. fields[3] = "FFFF";
  44878. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44879. fields[3] = "";
  44880. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44881. TXT_DB_free(db);
  44882. #endif
  44883. return EXPECT_RESULT();
  44884. }
  44885. static int test_wolfSSL_NCONF(void)
  44886. {
  44887. EXPECT_DECLS;
  44888. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  44889. const char* confFile = "./tests/NCONF_test.cnf";
  44890. CONF* conf = NULL;
  44891. long eline = 0;
  44892. long num = 0;
  44893. ExpectNotNull(conf = NCONF_new(NULL));
  44894. ExpectIntEQ(NCONF_load(conf, confFile, &eline), 1);
  44895. ExpectIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  44896. ExpectIntEQ(num, 1234);
  44897. ExpectIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  44898. ExpectIntEQ(num, 4321);
  44899. ExpectStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  44900. ExpectStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  44901. "./test-dir/file1");
  44902. ExpectStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  44903. "./test-dir/file1:./test-dir/file2:./section1:file2");
  44904. NCONF_free(conf);
  44905. #endif
  44906. return EXPECT_RESULT();
  44907. }
  44908. #endif /* OPENSSL_ALL */
  44909. static int test_wolfSSL_X509V3_EXT_get(void) {
  44910. EXPECT_DECLS;
  44911. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44912. XFILE f = XBADFILE;
  44913. int numOfExt =0;
  44914. int extNid = 0;
  44915. int i = 0;
  44916. WOLFSSL_X509* x509 = NULL;
  44917. WOLFSSL_X509_EXTENSION* ext = NULL;
  44918. const WOLFSSL_v3_ext_method* method = NULL;
  44919. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  44920. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  44921. if (f != XBADFILE)
  44922. XFCLOSE(f);
  44923. /* wolfSSL_X509V3_EXT_get() return struct and nid test */
  44924. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  44925. for (i = 0; i < numOfExt; i++) {
  44926. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44927. ExpectIntNE((extNid = ext->obj->nid), NID_undef);
  44928. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  44929. ExpectIntEQ(method->ext_nid, extNid);
  44930. }
  44931. /* wolfSSL_X509V3_EXT_get() NULL argument test */
  44932. ExpectNull(method = wolfSSL_X509V3_EXT_get(NULL));
  44933. wolfSSL_X509_free(x509);
  44934. #endif
  44935. return EXPECT_RESULT();
  44936. }
  44937. static int test_wolfSSL_X509V3_EXT_nconf(void)
  44938. {
  44939. EXPECT_DECLS;
  44940. #ifdef OPENSSL_ALL
  44941. const char *ext_names[] = {
  44942. "subjectKeyIdentifier",
  44943. "authorityKeyIdentifier",
  44944. "subjectAltName",
  44945. "keyUsage",
  44946. "extendedKeyUsage",
  44947. };
  44948. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  44949. int ext_nids[] = {
  44950. NID_subject_key_identifier,
  44951. NID_authority_key_identifier,
  44952. NID_subject_alt_name,
  44953. NID_key_usage,
  44954. NID_ext_key_usage,
  44955. };
  44956. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  44957. const char *ext_values[] = {
  44958. "hash",
  44959. "hash",
  44960. "DNS:example.com, IP:127.0.0.1",
  44961. "digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,"
  44962. "keyAgreement,keyCertSign,cRLSign,encipherOnly,decipherOnly",
  44963. "serverAuth,clientAuth,codeSigning,emailProtection,timeStamping,"
  44964. "OCSPSigning",
  44965. };
  44966. size_t i;
  44967. X509_EXTENSION* ext = NULL;
  44968. X509* x509 = NULL;
  44969. unsigned int keyUsageFlags;
  44970. unsigned int extKeyUsageFlags;
  44971. ExpectNotNull(x509 = X509_new());
  44972. /* keyUsage / extKeyUsage should match string above */
  44973. keyUsageFlags = KU_DIGITAL_SIGNATURE
  44974. | KU_NON_REPUDIATION
  44975. | KU_KEY_ENCIPHERMENT
  44976. | KU_DATA_ENCIPHERMENT
  44977. | KU_KEY_AGREEMENT
  44978. | KU_KEY_CERT_SIGN
  44979. | KU_CRL_SIGN
  44980. | KU_ENCIPHER_ONLY
  44981. | KU_DECIPHER_ONLY;
  44982. extKeyUsageFlags = XKU_SSL_CLIENT
  44983. | XKU_SSL_SERVER
  44984. | XKU_CODE_SIGN
  44985. | XKU_SMIME
  44986. | XKU_TIMESTAMP
  44987. | XKU_OCSP_SIGN;
  44988. for (i = 0; i < ext_names_count; i++) {
  44989. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  44990. ext_values[i]));
  44991. X509_EXTENSION_free(ext);
  44992. ext = NULL;
  44993. }
  44994. for (i = 0; i < ext_nids_count; i++) {
  44995. ExpectNotNull(ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i],
  44996. ext_values[i]));
  44997. X509_EXTENSION_free(ext);
  44998. ext = NULL;
  44999. }
  45000. /* Test adding extension to X509 */
  45001. for (i = 0; i < ext_nids_count; i++) {
  45002. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  45003. ext_values[i]));
  45004. ExpectIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  45005. if (ext_nids[i] == NID_key_usage) {
  45006. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  45007. }
  45008. else if (ext_nids[i] == NID_ext_key_usage) {
  45009. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  45010. }
  45011. X509_EXTENSION_free(ext);
  45012. ext = NULL;
  45013. }
  45014. X509_free(x509);
  45015. #endif
  45016. return EXPECT_RESULT();
  45017. }
  45018. static int test_wolfSSL_X509V3_EXT(void) {
  45019. EXPECT_DECLS;
  45020. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45021. XFILE f = XBADFILE;
  45022. int numOfExt = 0, nid = 0, i = 0, expected, actual = 0;
  45023. char* str = NULL;
  45024. unsigned char* data = NULL;
  45025. const WOLFSSL_v3_ext_method* method = NULL;
  45026. WOLFSSL_X509* x509 = NULL;
  45027. WOLFSSL_X509_EXTENSION* ext = NULL;
  45028. WOLFSSL_X509_EXTENSION* ext2 = NULL;
  45029. WOLFSSL_ASN1_OBJECT *obj = NULL;
  45030. WOLFSSL_ASN1_OBJECT *adObj = NULL;
  45031. WOLFSSL_ASN1_STRING* asn1str = NULL;
  45032. WOLFSSL_AUTHORITY_KEYID* aKeyId = NULL;
  45033. WOLFSSL_AUTHORITY_INFO_ACCESS* aia = NULL;
  45034. WOLFSSL_BASIC_CONSTRAINTS* bc = NULL;
  45035. WOLFSSL_ACCESS_DESCRIPTION* ad = NULL;
  45036. WOLFSSL_GENERAL_NAME* gn = NULL;
  45037. /* Check NULL argument */
  45038. ExpectNull(wolfSSL_X509V3_EXT_d2i(NULL));
  45039. /* Using OCSP cert with X509V3 extensions */
  45040. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  45041. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45042. if (f != XBADFILE)
  45043. XFCLOSE(f);
  45044. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  45045. /* Basic Constraints */
  45046. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45047. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45048. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  45049. ExpectNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  45050. ExpectIntEQ(bc->ca, 1);
  45051. ExpectNull(bc->pathlen);
  45052. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  45053. bc = NULL;
  45054. i++;
  45055. /* Subject Key Identifier */
  45056. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45057. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45058. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  45059. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  45060. ExpectNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  45061. asn1str));
  45062. X509_EXTENSION_free(ext2);
  45063. ext2 = NULL;
  45064. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  45065. ExpectNotNull(method->i2s);
  45066. ExpectNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  45067. wolfSSL_ASN1_STRING_free(asn1str);
  45068. asn1str = NULL;
  45069. if (str != NULL) {
  45070. actual = strcmp(str,
  45071. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  45072. }
  45073. ExpectIntEQ(actual, 0);
  45074. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45075. str = NULL;
  45076. i++;
  45077. /* Authority Key Identifier */
  45078. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45079. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45080. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  45081. ExpectNotNull(aKeyId = (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(
  45082. ext));
  45083. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  45084. ExpectNotNull(asn1str = aKeyId->keyid);
  45085. ExpectNotNull(str = wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method,
  45086. asn1str));
  45087. asn1str = NULL;
  45088. if (str != NULL) {
  45089. actual = strcmp(str,
  45090. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  45091. }
  45092. ExpectIntEQ(actual, 0);
  45093. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45094. str = NULL;
  45095. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  45096. aKeyId = NULL;
  45097. i++;
  45098. /* Key Usage */
  45099. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45100. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45101. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  45102. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  45103. #if defined(WOLFSSL_QT)
  45104. ExpectNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  45105. #else
  45106. ExpectNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  45107. #endif
  45108. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  45109. if (data != NULL) {
  45110. #ifdef BIG_ENDIAN_ORDER
  45111. actual = data[1];
  45112. #else
  45113. actual = data[0];
  45114. #endif
  45115. }
  45116. ExpectIntEQ(actual, expected);
  45117. wolfSSL_ASN1_STRING_free(asn1str);
  45118. asn1str = NULL;
  45119. #if 1
  45120. i++;
  45121. /* Authority Info Access */
  45122. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45123. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45124. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  45125. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  45126. ext));
  45127. #if defined(WOLFSSL_QT)
  45128. ExpectIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  45129. #else
  45130. ExpectIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  45131. #endif
  45132. /* URI entry is an ACCESS_DESCRIPTION type */
  45133. #if defined(WOLFSSL_QT)
  45134. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  45135. #else
  45136. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  45137. #endif
  45138. ExpectNotNull(adObj = ad->method);
  45139. /* Make sure nid is OCSP */
  45140. ExpectIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  45141. /* GENERAL_NAME stores URI as an ASN1_STRING */
  45142. ExpectNotNull(gn = ad->location);
  45143. ExpectIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  45144. ExpectNotNull(asn1str = gn->d.uniformResourceIdentifier);
  45145. ExpectIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  45146. #if defined(WOLFSSL_QT)
  45147. ExpectNotNull(str = (char*)ASN1_STRING_get0_data(asn1str));
  45148. #else
  45149. ExpectNotNull(str = (char*)wolfSSL_ASN1_STRING_data(asn1str));
  45150. #endif
  45151. if (str != NULL) {
  45152. actual = strcmp(str, "http://127.0.0.1:22220");
  45153. }
  45154. ExpectIntEQ(actual, 0);
  45155. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  45156. aia = NULL;
  45157. #else
  45158. (void) aia; (void) ad; (void) adObj; (void) gn;
  45159. #endif
  45160. wolfSSL_X509_free(x509);
  45161. #endif
  45162. return EXPECT_RESULT();
  45163. }
  45164. static int test_wolfSSL_X509_get_extension_flags(void)
  45165. {
  45166. EXPECT_DECLS;
  45167. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  45168. XFILE f = XBADFILE;
  45169. X509* x509 = NULL;
  45170. unsigned int extFlags;
  45171. unsigned int keyUsageFlags;
  45172. unsigned int extKeyUsageFlags;
  45173. /* client-int-cert.pem has the following extension flags. */
  45174. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  45175. /* and the following key usage flags. */
  45176. keyUsageFlags = KU_DIGITAL_SIGNATURE
  45177. | KU_NON_REPUDIATION
  45178. | KU_KEY_ENCIPHERMENT;
  45179. /* and the following extended key usage flags. */
  45180. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  45181. ExpectTrue((f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb")) !=
  45182. XBADFILE);
  45183. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  45184. if (f != XBADFILE) {
  45185. XFCLOSE(f);
  45186. f = XBADFILE;
  45187. }
  45188. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  45189. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  45190. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  45191. X509_free(x509);
  45192. x509 = NULL;
  45193. /* client-cert-ext.pem has the following extension flags. */
  45194. extFlags = EXFLAG_KUSAGE;
  45195. /* and the following key usage flags. */
  45196. keyUsageFlags = KU_DIGITAL_SIGNATURE
  45197. | KU_KEY_CERT_SIGN
  45198. | KU_CRL_SIGN;
  45199. ExpectTrue((f = fopen("./certs/client-cert-ext.pem", "rb")) != XBADFILE);
  45200. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  45201. if (f != XBADFILE)
  45202. XFCLOSE(f);
  45203. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  45204. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  45205. X509_free(x509);
  45206. #endif /* OPENSSL_ALL */
  45207. return EXPECT_RESULT();
  45208. }
  45209. static int test_wolfSSL_X509_get_ext(void)
  45210. {
  45211. EXPECT_DECLS;
  45212. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45213. int ret = 0;
  45214. XFILE f = XBADFILE;
  45215. WOLFSSL_X509* x509 = NULL;
  45216. WOLFSSL_X509_EXTENSION* foundExtension;
  45217. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45218. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45219. if (f != XBADFILE)
  45220. XFCLOSE(f);
  45221. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  45222. /* wolfSSL_X509_get_ext() valid input */
  45223. ExpectNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  45224. /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
  45225. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  45226. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  45227. /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
  45228. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  45229. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  45230. /* wolfSSL_X509_get_ext() NULL x509, valid idx */
  45231. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  45232. wolfSSL_X509_free(x509);
  45233. #endif
  45234. return EXPECT_RESULT();
  45235. }
  45236. static int test_wolfSSL_X509_get_ext_by_NID(void)
  45237. {
  45238. EXPECT_DECLS;
  45239. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  45240. int rc = 0;
  45241. XFILE f = XBADFILE;
  45242. WOLFSSL_X509* x509 = NULL;
  45243. ASN1_OBJECT* obj = NULL;
  45244. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45245. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45246. if (f != XBADFILE)
  45247. XFCLOSE(f);
  45248. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  45249. -1), 0);
  45250. /* Start search from last location (should fail) */
  45251. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  45252. rc), -1);
  45253. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  45254. -2), -1);
  45255. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints,
  45256. -1), -1);
  45257. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1), -1);
  45258. /* NID_ext_key_usage, check also its nid and oid */
  45259. ExpectIntGT(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1),
  45260. -1);
  45261. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(
  45262. x509, rc)));
  45263. ExpectIntEQ(obj->nid, NID_ext_key_usage);
  45264. ExpectIntEQ(obj->type, EXT_KEY_USAGE_OID);
  45265. wolfSSL_X509_free(x509);
  45266. #endif
  45267. return EXPECT_RESULT();
  45268. }
  45269. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  45270. {
  45271. EXPECT_DECLS;
  45272. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  45273. int rc = 0;
  45274. XFILE f = XBADFILE;
  45275. WOLFSSL_X509* x509 = NULL;
  45276. WOLFSSL_X509_EXTENSION* ext = NULL;
  45277. WOLFSSL_ASN1_STRING* sanString = NULL;
  45278. byte* sanDer = NULL;
  45279. const byte expectedDer[] = {
  45280. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  45281. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  45282. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45283. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  45284. if (f != XBADFILE)
  45285. XFCLOSE(f);
  45286. ExpectIntNE(rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1), -1);
  45287. ExpectNotNull(ext = X509_get_ext(x509, rc));
  45288. ExpectNotNull(sanString = X509_EXTENSION_get_data(ext));
  45289. ExpectIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  45290. ExpectNotNull(sanDer = ASN1_STRING_data(sanString));
  45291. ExpectIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  45292. X509_free(x509);
  45293. #endif
  45294. return EXPECT_RESULT();
  45295. }
  45296. static int test_wolfSSL_X509_EXTENSION_new(void)
  45297. {
  45298. EXPECT_DECLS;
  45299. #if defined (OPENSSL_ALL)
  45300. WOLFSSL_X509_EXTENSION* ext = NULL;
  45301. ExpectNotNull(ext = wolfSSL_X509_EXTENSION_new());
  45302. ExpectNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  45303. wolfSSL_X509_EXTENSION_free(ext);
  45304. #endif
  45305. return EXPECT_RESULT();
  45306. }
  45307. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  45308. {
  45309. EXPECT_DECLS;
  45310. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45311. WOLFSSL_X509* x509 = NULL;
  45312. WOLFSSL_X509_EXTENSION* ext = NULL;
  45313. WOLFSSL_ASN1_OBJECT* o = NULL;
  45314. XFILE file = XBADFILE;
  45315. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45316. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  45317. if (file != XBADFILE)
  45318. XFCLOSE(file);
  45319. /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
  45320. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  45321. ExpectNull(wolfSSL_X509_EXTENSION_get_object(NULL));
  45322. ExpectNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  45323. ExpectIntEQ(o->nid, 128);
  45324. /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
  45325. ExpectNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  45326. wolfSSL_X509_free(x509);
  45327. #endif
  45328. return EXPECT_RESULT();
  45329. }
  45330. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  45331. {
  45332. EXPECT_DECLS;
  45333. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45334. WOLFSSL_X509* x509 = NULL;
  45335. WOLFSSL_X509_EXTENSION* ext = NULL;
  45336. WOLFSSL_ASN1_STRING* str = NULL;
  45337. XFILE file = XBADFILE;
  45338. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45339. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  45340. if (file != XBADFILE)
  45341. XFCLOSE(file);
  45342. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  45343. ExpectNull(str = wolfSSL_X509_EXTENSION_get_data(NULL));
  45344. ExpectNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  45345. wolfSSL_X509_free(x509);
  45346. #endif
  45347. return EXPECT_RESULT();
  45348. }
  45349. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  45350. {
  45351. EXPECT_DECLS;
  45352. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45353. WOLFSSL_X509* x509 = NULL;
  45354. WOLFSSL_X509_EXTENSION* ext = NULL;
  45355. XFILE file = XBADFILE;
  45356. int crit = 0;
  45357. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45358. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  45359. if (file != XBADFILE)
  45360. XFCLOSE(file);
  45361. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  45362. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(NULL), BAD_FUNC_ARG);
  45363. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(ext), 0);
  45364. wolfSSL_X509_free(x509);
  45365. #endif
  45366. return EXPECT_RESULT();
  45367. }
  45368. static int test_wolfSSL_X509V3_EXT_print(void)
  45369. {
  45370. EXPECT_DECLS;
  45371. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  45372. !defined(NO_RSA)
  45373. {
  45374. XFILE f = XBADFILE;
  45375. WOLFSSL_X509* x509 = NULL;
  45376. X509_EXTENSION * ext = NULL;
  45377. int loc = 0;
  45378. BIO *bio = NULL;
  45379. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  45380. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45381. if (f != XBADFILE)
  45382. fclose(f);
  45383. ExpectNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  45384. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  45385. NID_basic_constraints, -1), -1);
  45386. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  45387. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  45388. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  45389. NID_subject_key_identifier, -1), -1);
  45390. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  45391. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  45392. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  45393. NID_authority_key_identifier, -1), -1);
  45394. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  45395. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  45396. wolfSSL_BIO_free(bio);
  45397. wolfSSL_X509_free(x509);
  45398. }
  45399. {
  45400. X509 *x509 = NULL;
  45401. BIO *bio = NULL;
  45402. X509_EXTENSION *ext = NULL;
  45403. unsigned int i = 0;
  45404. unsigned int idx = 0;
  45405. /* Some NIDs to test with */
  45406. int nids[] = {
  45407. /* NID_key_usage, currently X509_get_ext returns this as a bit
  45408. * string, which messes up X509V3_EXT_print */
  45409. /* NID_ext_key_usage, */
  45410. NID_subject_alt_name,
  45411. };
  45412. int* n = NULL;
  45413. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  45414. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  45415. WOLFSSL_FILETYPE_PEM));
  45416. ExpectIntGT(fprintf(stderr, "\nPrinting extension values:\n"), 0);
  45417. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  45418. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  45419. * update the index */
  45420. ExpectIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  45421. ExpectNotNull(ext = X509_get_ext(x509, idx));
  45422. ExpectIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  45423. ExpectIntGT(fprintf(stderr, "\n"), 0);
  45424. }
  45425. BIO_free(bio);
  45426. X509_free(x509);
  45427. }
  45428. #endif
  45429. return EXPECT_RESULT();
  45430. }
  45431. static int test_wolfSSL_X509_cmp(void)
  45432. {
  45433. EXPECT_DECLS;
  45434. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  45435. XFILE file1 = XBADFILE;
  45436. XFILE file2 = XBADFILE;
  45437. WOLFSSL_X509* cert1 = NULL;
  45438. WOLFSSL_X509* cert2 = NULL;
  45439. ExpectTrue((file1 = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45440. ExpectTrue((file2 = XFOPEN("./certs/3072/client-cert.pem", "rb")) !=
  45441. XBADFILE);
  45442. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  45443. ExpectNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  45444. if (file1 != XBADFILE)
  45445. fclose(file1);
  45446. if (file2 != XBADFILE)
  45447. fclose(file2);
  45448. /* wolfSSL_X509_cmp() testing matching certs */
  45449. ExpectIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  45450. /* wolfSSL_X509_cmp() testing mismatched certs */
  45451. ExpectIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  45452. /* wolfSSL_X509_cmp() testing NULL, valid args */
  45453. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  45454. /* wolfSSL_X509_cmp() testing valid, NULL args */
  45455. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  45456. /* wolfSSL_X509_cmp() testing NULL, NULL args */
  45457. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  45458. wolfSSL_X509_free(cert1);
  45459. wolfSSL_X509_free(cert2);
  45460. #endif
  45461. return EXPECT_RESULT();
  45462. }
  45463. static int test_wolfSSL_EVP_PKEY_up_ref(void)
  45464. {
  45465. EXPECT_DECLS;
  45466. #if defined(OPENSSL_ALL)
  45467. EVP_PKEY* pkey;
  45468. pkey = EVP_PKEY_new();
  45469. ExpectNotNull(pkey);
  45470. ExpectIntEQ(EVP_PKEY_up_ref(NULL), 0);
  45471. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  45472. EVP_PKEY_free(pkey);
  45473. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  45474. EVP_PKEY_free(pkey);
  45475. EVP_PKEY_free(pkey);
  45476. #endif
  45477. return EXPECT_RESULT();
  45478. }
  45479. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  45480. {
  45481. EXPECT_DECLS;
  45482. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  45483. EVP_PKEY* pkey = NULL;
  45484. const unsigned char* p;
  45485. unsigned char *der = NULL;
  45486. unsigned char *tmp = NULL;
  45487. int derLen;
  45488. p = client_keypub_der_2048;
  45489. /* Check that key can be successfully decoded. */
  45490. ExpectNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  45491. sizeof_client_keypub_der_2048));
  45492. /* Check that key can be successfully encoded. */
  45493. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  45494. /* Ensure that the encoded version matches the original. */
  45495. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  45496. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  45497. /* Do same test except with pre-allocated buffer to ensure the der pointer
  45498. * is advanced. */
  45499. tmp = der;
  45500. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  45501. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  45502. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  45503. ExpectTrue(der + derLen == tmp);
  45504. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  45505. EVP_PKEY_free(pkey);
  45506. #endif
  45507. return EXPECT_RESULT();
  45508. }
  45509. static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
  45510. {
  45511. EXPECT_DECLS;
  45512. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(NO_CERTS) && \
  45513. !defined(NO_ASN) && !defined(NO_PWDBASED)
  45514. EVP_PKEY* pkey = NULL;
  45515. const unsigned char* p;
  45516. unsigned char *der = NULL;
  45517. unsigned char *tmp = NULL;
  45518. int derLen;
  45519. unsigned char pub_buf[65];
  45520. const int pub_len = 65;
  45521. BN_CTX* ctx;
  45522. EC_GROUP* curve = NULL;
  45523. EC_KEY* ephemeral_key = NULL;
  45524. const EC_POINT* h;
  45525. /* Generate an x963 key pair and get public part into pub_buf */
  45526. ExpectNotNull(ctx = BN_CTX_new());
  45527. ExpectNotNull(curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  45528. ExpectNotNull(ephemeral_key = EC_KEY_new_by_curve_name(
  45529. NID_X9_62_prime256v1));
  45530. ExpectIntEQ(EC_KEY_generate_key(ephemeral_key), 1);
  45531. ExpectNotNull(h = EC_KEY_get0_public_key(ephemeral_key));
  45532. ExpectIntEQ(pub_len, EC_POINT_point2oct(curve, h,
  45533. POINT_CONVERSION_UNCOMPRESSED, pub_buf, pub_len, ctx));
  45534. /* Prepare the EVP_PKEY */
  45535. ExpectNotNull(pkey = EVP_PKEY_new());
  45536. p = pub_buf;
  45537. /* Check that key can be successfully decoded. */
  45538. ExpectNotNull(wolfSSL_d2i_PublicKey(EVP_PKEY_EC, &pkey, &p,
  45539. pub_len));
  45540. /* Check that key can be successfully encoded. */
  45541. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  45542. /* Ensure that the encoded version matches the original. */
  45543. ExpectIntEQ(derLen, pub_len);
  45544. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  45545. /* Do same test except with pre-allocated buffer to ensure the der pointer
  45546. * is advanced. */
  45547. tmp = der;
  45548. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  45549. ExpectIntEQ(derLen, pub_len);
  45550. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  45551. ExpectTrue(der + derLen == tmp);
  45552. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  45553. EVP_PKEY_free(pkey);
  45554. EC_KEY_free(ephemeral_key);
  45555. EC_GROUP_free(curve);
  45556. #endif
  45557. return EXPECT_RESULT();
  45558. }
  45559. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  45560. {
  45561. EXPECT_DECLS;
  45562. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  45563. DSA* dsa = NULL;
  45564. byte derIn[] = {
  45565. 0x30, 0x82, 0x01, 0x1f, 0x02, 0x81, 0x81, 0x00,
  45566. 0xcd, 0xde, 0x25, 0x68, 0x80, 0x53, 0x0d, 0xe5,
  45567. 0x77, 0xd6, 0xd2, 0x90, 0x39, 0x3f, 0x90, 0xa2,
  45568. 0x3f, 0x33, 0x94, 0x6e, 0xe8, 0x4f, 0x2b, 0x63,
  45569. 0xab, 0x30, 0xab, 0x15, 0xba, 0x11, 0xea, 0x8a,
  45570. 0x5d, 0x8d, 0xcc, 0xb8, 0xd4, 0xa1, 0xd5, 0xc1,
  45571. 0x47, 0x9d, 0x5a, 0x73, 0x6a, 0x62, 0x49, 0xd1,
  45572. 0x06, 0x07, 0x67, 0xf6, 0x2f, 0xa3, 0x39, 0xbd,
  45573. 0x4e, 0x0d, 0xb4, 0xd3, 0x22, 0x23, 0x84, 0xec,
  45574. 0x93, 0x26, 0x5a, 0x49, 0xee, 0x7c, 0x89, 0x48,
  45575. 0x66, 0x4d, 0xe8, 0xe8, 0xd8, 0x50, 0xfb, 0xa5,
  45576. 0x71, 0x9f, 0x22, 0x18, 0xe5, 0xe6, 0x0b, 0x46,
  45577. 0x87, 0x66, 0xee, 0x52, 0x8f, 0x46, 0x4f, 0xb5,
  45578. 0x03, 0xce, 0xed, 0xe3, 0xbe, 0xe5, 0xb5, 0x81,
  45579. 0xd2, 0x59, 0xe9, 0xc0, 0xad, 0x4d, 0xd0, 0x4d,
  45580. 0x26, 0xf7, 0xba, 0x50, 0xe8, 0xc9, 0x8f, 0xfe,
  45581. 0x24, 0x19, 0x3d, 0x2e, 0xa7, 0x52, 0x3c, 0x6d,
  45582. 0x02, 0x15, 0x00, 0xfb, 0x47, 0xfb, 0xec, 0x81,
  45583. 0x20, 0xc8, 0x1c, 0xe9, 0x4a, 0xba, 0x04, 0x6f,
  45584. 0x19, 0x9b, 0x94, 0xee, 0x82, 0x67, 0xd3, 0x02,
  45585. 0x81, 0x81, 0x00, 0x9b, 0x95, 0xbb, 0x85, 0xc5,
  45586. 0x58, 0x4a, 0x32, 0x9c, 0xaa, 0x44, 0x85, 0xd6,
  45587. 0x68, 0xdc, 0x3e, 0x14, 0xf4, 0xce, 0x6d, 0xa3,
  45588. 0x49, 0x38, 0xea, 0xd6, 0x61, 0x48, 0x92, 0x5a,
  45589. 0x40, 0x95, 0x49, 0x38, 0xaa, 0xe1, 0x39, 0x29,
  45590. 0x68, 0x58, 0x47, 0x8a, 0x4b, 0x01, 0xe1, 0x2e,
  45591. 0x8e, 0x6c, 0x63, 0x6f, 0x40, 0xca, 0x50, 0x3f,
  45592. 0x8c, 0x0b, 0x99, 0xe4, 0x72, 0x42, 0xb8, 0xb1,
  45593. 0xc2, 0x26, 0x48, 0xf1, 0x9c, 0x83, 0xc6, 0x37,
  45594. 0x2e, 0x5a, 0xae, 0x11, 0x09, 0xd9, 0xf3, 0xad,
  45595. 0x1f, 0x6f, 0xad, 0xad, 0x50, 0xe3, 0x78, 0x32,
  45596. 0xe6, 0xde, 0x8e, 0xaa, 0xbf, 0xd1, 0x00, 0x9f,
  45597. 0xb3, 0x02, 0x12, 0x19, 0xa2, 0x15, 0xec, 0x14,
  45598. 0x18, 0x5c, 0x0e, 0x26, 0xce, 0xf9, 0xae, 0xcc,
  45599. 0x7b, 0xb5, 0xd1, 0x26, 0xfc, 0x85, 0xfe, 0x14,
  45600. 0x93, 0xb6, 0x9d, 0x7d, 0x76, 0xe3, 0x35, 0x97,
  45601. 0x1e, 0xde, 0xc4
  45602. };
  45603. int derInLen = sizeof(derIn);
  45604. byte* derOut = NULL;
  45605. int derOutLen;
  45606. byte* p = derIn;
  45607. /* Check that params can be successfully decoded. */
  45608. ExpectNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&p, derInLen));
  45609. /* Check that params can be successfully encoded. */
  45610. ExpectIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  45611. /* Ensure that the encoded version matches the original. */
  45612. ExpectIntEQ(derInLen, derOutLen);
  45613. ExpectIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  45614. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  45615. DSA_free(dsa);
  45616. #endif
  45617. return EXPECT_RESULT();
  45618. }
  45619. static int test_wolfSSL_i2d_PrivateKey(void)
  45620. {
  45621. EXPECT_DECLS;
  45622. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && \
  45623. !defined(NO_ASN) && !defined(NO_PWDBASED)
  45624. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  45625. {
  45626. EVP_PKEY* pkey = NULL;
  45627. const unsigned char* server_key =
  45628. (const unsigned char*)server_key_der_2048;
  45629. unsigned char buf[FOURK_BUF];
  45630. unsigned char* pt = NULL;
  45631. int bufSz = 0;
  45632. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  45633. (long)sizeof_server_key_der_2048));
  45634. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  45635. pt = buf;
  45636. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  45637. ExpectIntNE((pt - buf), 0);
  45638. ExpectIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  45639. EVP_PKEY_free(pkey);
  45640. }
  45641. #endif
  45642. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45643. {
  45644. EVP_PKEY* pkey = NULL;
  45645. const unsigned char* client_key =
  45646. (const unsigned char*)ecc_clikey_der_256;
  45647. unsigned char buf[FOURK_BUF];
  45648. unsigned char* pt = NULL;
  45649. int bufSz = 0;
  45650. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  45651. (long)sizeof_ecc_clikey_der_256)));
  45652. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  45653. pt = buf;
  45654. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  45655. ExpectIntNE((pt - buf), 0);
  45656. ExpectIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  45657. EVP_PKEY_free(pkey);
  45658. }
  45659. #endif
  45660. #endif
  45661. return EXPECT_RESULT();
  45662. }
  45663. static int test_wolfSSL_OCSP_id_get0_info(void)
  45664. {
  45665. EXPECT_DECLS;
  45666. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && \
  45667. defined(HAVE_OCSP) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45668. X509* cert = NULL;
  45669. X509* issuer = NULL;
  45670. OCSP_CERTID* id = NULL;
  45671. OCSP_CERTID* id2 = NULL;
  45672. ASN1_STRING* name = NULL;
  45673. ASN1_OBJECT* pmd = NULL;
  45674. ASN1_STRING* keyHash = NULL;
  45675. ASN1_INTEGER* serial = NULL;
  45676. ASN1_INTEGER* x509Int = NULL;
  45677. ExpectNotNull(cert = wolfSSL_X509_load_certificate_file(svrCertFile,
  45678. SSL_FILETYPE_PEM));
  45679. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caCertFile,
  45680. SSL_FILETYPE_PEM));
  45681. ExpectNotNull(id = OCSP_cert_to_id(NULL, cert, issuer));
  45682. ExpectNotNull(id2 = OCSP_cert_to_id(NULL, cert, issuer));
  45683. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  45684. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  45685. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  45686. ExpectIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  45687. ExpectIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  45688. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  45689. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  45690. ExpectNotNull(serial);
  45691. /* compare serial number to one in cert, should be equal */
  45692. ExpectNotNull(x509Int = X509_get_serialNumber(cert));
  45693. ExpectIntEQ(x509Int->length, serial->length);
  45694. ExpectIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  45695. /* test OCSP_id_cmp */
  45696. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  45697. ExpectIntNE(OCSP_id_cmp(id, NULL), 0);
  45698. ExpectIntNE(OCSP_id_cmp(NULL, id2), 0);
  45699. ExpectIntEQ(OCSP_id_cmp(id, id2), 0);
  45700. if (id != NULL) {
  45701. id->issuerHash[0] = ~id->issuerHash[0];
  45702. }
  45703. ExpectIntNE(OCSP_id_cmp(id, id2), 0);
  45704. OCSP_CERTID_free(id);
  45705. OCSP_CERTID_free(id2);
  45706. X509_free(cert); /* free's x509Int */
  45707. X509_free(issuer);
  45708. #endif
  45709. return EXPECT_RESULT();
  45710. }
  45711. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  45712. {
  45713. EXPECT_DECLS;
  45714. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  45715. WOLFSSL_OCSP_CERTID certId;
  45716. byte* targetBuffer = NULL;
  45717. byte* p;
  45718. /* OCSP CertID bytes taken from PCAP */
  45719. byte rawCertId[] = {
  45720. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  45721. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  45722. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  45723. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  45724. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  45725. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  45726. 0xcf, 0xbc, 0x91
  45727. };
  45728. int ret = 0;
  45729. int i;
  45730. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  45731. certId.rawCertId = rawCertId;
  45732. certId.rawCertIdSize = sizeof(rawCertId);
  45733. ExpectNotNull(targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL,
  45734. DYNAMIC_TYPE_TMP_BUFFER));
  45735. p = targetBuffer;
  45736. /* Function returns the size of the encoded data. */
  45737. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &p), sizeof(rawCertId));
  45738. /* If target buffer is not null, function increments targetBuffer to point
  45739. * just past the end of the encoded data. */
  45740. ExpectPtrEq(p, (targetBuffer + sizeof(rawCertId)));
  45741. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  45742. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  45743. }
  45744. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45745. targetBuffer = NULL;
  45746. /* If target buffer is null, function allocates memory for a buffer and
  45747. * copies the encoded data into it. targetBuffer then points to the start of
  45748. * this newly allocate buffer. */
  45749. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer),
  45750. sizeof(rawCertId));
  45751. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  45752. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  45753. }
  45754. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  45755. #endif
  45756. return EXPECT_RESULT();
  45757. }
  45758. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  45759. {
  45760. EXPECT_DECLS;
  45761. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  45762. WOLFSSL_OCSP_CERTID* certId;
  45763. WOLFSSL_OCSP_CERTID* certIdGood;
  45764. WOLFSSL_OCSP_CERTID* certIdBad;
  45765. const unsigned char* rawCertIdPtr;
  45766. const unsigned char rawCertId[] = {
  45767. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  45768. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  45769. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  45770. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  45771. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  45772. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  45773. 0xcf, 0xbc, 0x91
  45774. };
  45775. rawCertIdPtr = &rawCertId[0];
  45776. /* If the cert ID is NULL the function should allocate it and copy the
  45777. * data to it. */
  45778. certId = NULL;
  45779. ExpectNotNull(certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  45780. sizeof(rawCertId)));
  45781. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  45782. if (certId != NULL) {
  45783. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  45784. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  45785. }
  45786. /* If the cert ID is not NULL the function will just copy the data to it. */
  45787. ExpectNotNull(certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  45788. DYNAMIC_TYPE_TMP_BUFFER));
  45789. ExpectNotNull(certId);
  45790. ExpectNotNull(XMEMSET(certId, 0, sizeof(*certId)));
  45791. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  45792. rawCertIdPtr = &rawCertId[0];
  45793. ExpectNotNull(certIdGood = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  45794. sizeof(rawCertId)));
  45795. ExpectPtrEq(certIdGood, certId);
  45796. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  45797. if (certId != NULL) {
  45798. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  45799. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45800. certId = NULL;
  45801. }
  45802. /* The below tests should fail when passed bad parameters. NULL should
  45803. * always be returned. */
  45804. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr,
  45805. sizeof(rawCertId)));
  45806. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL,
  45807. sizeof(rawCertId)));
  45808. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0));
  45809. #endif
  45810. return EXPECT_RESULT();
  45811. }
  45812. static int test_wolfSSL_OCSP_id_cmp(void)
  45813. {
  45814. EXPECT_DECLS;
  45815. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45816. OCSP_CERTID id1;
  45817. OCSP_CERTID id2;
  45818. XMEMSET(&id1, 0, sizeof(id1));
  45819. XMEMSET(&id2, 0, sizeof(id2));
  45820. ExpectIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  45821. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  45822. ExpectIntNE(OCSP_id_cmp(&id1, NULL), 0);
  45823. ExpectIntNE(OCSP_id_cmp(NULL, &id2), 0);
  45824. #endif
  45825. return EXPECT_RESULT();
  45826. }
  45827. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  45828. {
  45829. EXPECT_DECLS;
  45830. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45831. WOLFSSL_OCSP_SINGLERESP single;
  45832. const WOLFSSL_OCSP_CERTID* certId;
  45833. XMEMSET(&single, 0, sizeof(single));
  45834. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  45835. ExpectPtrEq(&single, certId);
  45836. ExpectNull(wolfSSL_OCSP_SINGLERESP_get0_id(NULL));
  45837. #endif
  45838. return EXPECT_RESULT();
  45839. }
  45840. static int test_wolfSSL_OCSP_single_get0_status(void)
  45841. {
  45842. EXPECT_DECLS;
  45843. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45844. WOLFSSL_OCSP_SINGLERESP single;
  45845. CertStatus certStatus;
  45846. WOLFSSL_ASN1_TIME* thisDate;
  45847. WOLFSSL_ASN1_TIME* nextDate;
  45848. int ret, i;
  45849. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45850. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  45851. /* Fill the date fields with some dummy data. */
  45852. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  45853. certStatus.thisDateParsed.data[i] = i;
  45854. certStatus.nextDateParsed.data[i] = i;
  45855. }
  45856. certStatus.status = CERT_GOOD;
  45857. single.status = &certStatus;
  45858. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  45859. &nextDate);
  45860. ExpectIntEQ(ret, CERT_GOOD);
  45861. ExpectPtrEq(thisDate, &certStatus.thisDateParsed);
  45862. ExpectPtrEq(nextDate, &certStatus.nextDateParsed);
  45863. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(NULL, NULL, NULL, NULL, NULL),
  45864. CERT_GOOD);
  45865. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, NULL,
  45866. NULL), CERT_GOOD);
  45867. #endif
  45868. return EXPECT_RESULT();
  45869. }
  45870. static int test_wolfSSL_OCSP_resp_count(void)
  45871. {
  45872. EXPECT_DECLS;
  45873. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45874. WOLFSSL_OCSP_BASICRESP basicResp;
  45875. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  45876. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  45877. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  45878. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45879. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45880. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 0);
  45881. basicResp.single = &singleRespOne;
  45882. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 1);
  45883. singleRespOne.next = &singleRespTwo;
  45884. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 2);
  45885. #endif
  45886. return EXPECT_RESULT();
  45887. }
  45888. static int test_wolfSSL_OCSP_resp_get0(void)
  45889. {
  45890. EXPECT_DECLS;
  45891. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45892. WOLFSSL_OCSP_BASICRESP basicResp;
  45893. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  45894. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  45895. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  45896. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45897. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45898. basicResp.single = &singleRespOne;
  45899. singleRespOne.next = &singleRespTwo;
  45900. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 0), &singleRespOne);
  45901. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 1), &singleRespTwo);
  45902. #endif
  45903. return EXPECT_RESULT();
  45904. }
  45905. static int test_wolfSSL_EVP_PKEY_derive(void)
  45906. {
  45907. EXPECT_DECLS;
  45908. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  45909. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  45910. EVP_PKEY_CTX *ctx = NULL;
  45911. unsigned char *skey = NULL;
  45912. size_t skeylen;
  45913. EVP_PKEY *pkey = NULL;
  45914. EVP_PKEY *peerkey = NULL;
  45915. const unsigned char* key;
  45916. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  45917. /* DH */
  45918. key = dh_key_der_2048;
  45919. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  45920. sizeof_dh_key_der_2048)));
  45921. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  45922. key = dh_key_der_2048;
  45923. ExpectNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  45924. sizeof_dh_key_der_2048)));
  45925. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  45926. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45927. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  45928. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  45929. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  45930. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  45931. DYNAMIC_TYPE_OPENSSL));
  45932. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  45933. EVP_PKEY_CTX_free(ctx);
  45934. ctx = NULL;
  45935. EVP_PKEY_free(peerkey);
  45936. peerkey = NULL;
  45937. EVP_PKEY_free(pkey);
  45938. pkey = NULL;
  45939. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  45940. skey = NULL;
  45941. #endif
  45942. #ifdef HAVE_ECC
  45943. /* ECDH */
  45944. key = ecc_clikey_der_256;
  45945. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  45946. sizeof_ecc_clikey_der_256)));
  45947. key = ecc_clikeypub_der_256;
  45948. ExpectNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  45949. sizeof_ecc_clikeypub_der_256)));
  45950. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45951. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  45952. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  45953. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  45954. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  45955. DYNAMIC_TYPE_OPENSSL));
  45956. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  45957. EVP_PKEY_CTX_free(ctx);
  45958. EVP_PKEY_free(peerkey);
  45959. EVP_PKEY_free(pkey);
  45960. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  45961. #endif /* HAVE_ECC */
  45962. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  45963. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  45964. return EXPECT_RESULT();
  45965. }
  45966. static int test_wolfSSL_EVP_PBE_scrypt(void)
  45967. {
  45968. EXPECT_DECLS;
  45969. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  45970. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  45971. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  45972. int ret;
  45973. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  45974. int pwdlen = sizeof(pwd);
  45975. const byte salt[] = {'N','a','C','l'};
  45976. int saltlen = sizeof(salt);
  45977. byte key[80];
  45978. word64 numOvr32 = (word64)INT32_MAX + 1;
  45979. /* expected derived key for N:16, r:1, p:1 */
  45980. const byte expectedKey[] = {
  45981. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  45982. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  45983. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  45984. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  45985. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  45986. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  45987. 0xE7, 0xE9, 0xC0, 0x9A};
  45988. /* N r p mx key keylen */
  45989. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  45990. ExpectIntEQ(ret, 0); /* N must be greater than 1 */
  45991. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  45992. ExpectIntEQ(ret, 0); /* N must be power of 2 */
  45993. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  45994. ExpectIntEQ(ret, 0); /* r must be greater than 0 */
  45995. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  45996. ExpectIntEQ(ret, 0); /* p must be greater than 0 */
  45997. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  45998. ExpectIntEQ(ret, 0); /* keylen must be greater than 0 */
  45999. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  46000. ExpectIntEQ(ret, 0); /* r must be smaller than 9 */
  46001. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  46002. ExpectIntEQ(ret, 1); /* should succeed if key is NULL */
  46003. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  46004. ExpectIntEQ(ret, 1); /* should succeed */
  46005. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  46006. key, 64);
  46007. ExpectIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  46008. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  46009. key, 64);
  46010. ExpectIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  46011. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  46012. ExpectIntEQ(ret, 1); /* should succeed even if salt is NULL */
  46013. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  46014. ExpectIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  46015. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  46016. ExpectIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  46017. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  46018. ExpectIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  46019. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  46020. ExpectIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  46021. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  46022. ExpectIntEQ(ret, 1);
  46023. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  46024. ExpectIntEQ(ret, 0); /* derived key must be the same as expected-key */
  46025. #endif /* !NO_PWDBASED && !NO_SHA256 */
  46026. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  46027. return EXPECT_RESULT();
  46028. }
  46029. static int test_no_op_functions(void)
  46030. {
  46031. EXPECT_DECLS;
  46032. #if defined(OPENSSL_EXTRA)
  46033. /* this makes sure wolfSSL can compile and run these no-op functions */
  46034. SSL_load_error_strings();
  46035. ENGINE_load_builtin_engines();
  46036. OpenSSL_add_all_ciphers();
  46037. ExpectIntEQ(CRYPTO_malloc_init(), 0);
  46038. #endif
  46039. return EXPECT_RESULT();
  46040. }
  46041. static int test_wolfSSL_CRYPTO_memcmp(void)
  46042. {
  46043. EXPECT_DECLS;
  46044. #ifdef OPENSSL_EXTRA
  46045. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  46046. "implementation of TLS/SSL for embedded devices to the cloud.";
  46047. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  46048. "implementation of TLS/SSL for embedded devices to the cloud.";
  46049. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  46050. "implementation of TLS/SSL for embedded devices to the cloud!";
  46051. ExpectIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  46052. ExpectIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  46053. #endif
  46054. return EXPECT_RESULT();
  46055. }
  46056. /*----------------------------------------------------------------------------*
  46057. | wolfCrypt ASN
  46058. *----------------------------------------------------------------------------*/
  46059. static int test_wc_CreateEncryptedPKCS8Key(void)
  46060. {
  46061. EXPECT_DECLS;
  46062. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  46063. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  46064. WC_RNG rng;
  46065. byte* encKey = NULL;
  46066. word32 encKeySz = 0;
  46067. word32 decKeySz = 0;
  46068. const char password[] = "Lorem ipsum dolor sit amet";
  46069. word32 passwordSz = (word32)XSTRLEN(password);
  46070. word32 tradIdx = 0;
  46071. XMEMSET(&rng, 0, sizeof(WC_RNG));
  46072. ExpectIntEQ(wc_InitRng(&rng), 0);
  46073. PRIVATE_KEY_UNLOCK();
  46074. /* Call with NULL for out buffer to get necessary length. */
  46075. ExpectIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  46076. sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
  46077. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  46078. LENGTH_ONLY_E);
  46079. ExpectNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  46080. DYNAMIC_TYPE_TMP_BUFFER));
  46081. /* Call with the allocated out buffer. */
  46082. ExpectIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  46083. sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
  46084. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  46085. 0);
  46086. /* Decrypt the encrypted PKCS8 key we just made. */
  46087. ExpectIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
  46088. passwordSz)), 0);
  46089. /* encKey now holds the decrypted key (decrypted in place). */
  46090. ExpectIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  46091. /* Check that the decrypted key matches the key prior to encryption. */
  46092. ExpectIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  46093. sizeof_server_key_der_2048), 0);
  46094. PRIVATE_KEY_LOCK();
  46095. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46096. wc_FreeRng(&rng);
  46097. #endif
  46098. return EXPECT_RESULT();
  46099. }
  46100. static int test_wc_GetPkcs8TraditionalOffset(void)
  46101. {
  46102. EXPECT_DECLS;
  46103. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  46104. int length;
  46105. int derSz = 0;
  46106. word32 inOutIdx;
  46107. const char* path = "./certs/server-keyPkcs8.der";
  46108. XFILE file = XBADFILE;
  46109. byte der[2048];
  46110. ExpectTrue((file = XFOPEN(path, "rb")) != XBADFILE);
  46111. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), file), 0);
  46112. if (file != XBADFILE)
  46113. XFCLOSE(file);
  46114. /* valid case */
  46115. inOutIdx = 0;
  46116. ExpectIntGT(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  46117. 0);
  46118. /* inOutIdx > sz */
  46119. inOutIdx = 4000;
  46120. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  46121. BAD_FUNC_ARG);
  46122. /* null input */
  46123. inOutIdx = 0;
  46124. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0),
  46125. BAD_FUNC_ARG);
  46126. /* invalid input, fill buffer with 1's */
  46127. XMEMSET(der, 1, sizeof(der));
  46128. inOutIdx = 0;
  46129. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  46130. ASN_PARSE_E);
  46131. #endif /* NO_ASN */
  46132. return EXPECT_RESULT();
  46133. }
  46134. static int test_wc_SetSubjectRaw(void)
  46135. {
  46136. EXPECT_DECLS;
  46137. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46138. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  46139. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  46140. WOLFSSL_X509* x509 = NULL;
  46141. int peerCertSz;
  46142. const byte* peerCertBuf = NULL;
  46143. Cert forgedCert;
  46144. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  46145. WOLFSSL_FILETYPE_ASN1));
  46146. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  46147. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  46148. ExpectIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  46149. wolfSSL_FreeX509(x509);
  46150. #endif
  46151. return EXPECT_RESULT();
  46152. }
  46153. static int test_wc_GetSubjectRaw(void)
  46154. {
  46155. EXPECT_DECLS;
  46156. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46157. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  46158. Cert cert;
  46159. byte *subjectRaw;
  46160. ExpectIntEQ(0, wc_InitCert(&cert));
  46161. ExpectIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  46162. #endif
  46163. return EXPECT_RESULT();
  46164. }
  46165. static int test_wc_SetIssuerRaw(void)
  46166. {
  46167. EXPECT_DECLS;
  46168. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46169. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  46170. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  46171. WOLFSSL_X509* x509 = NULL;
  46172. int peerCertSz;
  46173. const byte* peerCertBuf;
  46174. Cert forgedCert;
  46175. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  46176. WOLFSSL_FILETYPE_ASN1));
  46177. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  46178. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  46179. ExpectIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  46180. wolfSSL_FreeX509(x509);
  46181. #endif
  46182. return EXPECT_RESULT();
  46183. }
  46184. static int test_wc_SetIssueBuffer(void)
  46185. {
  46186. EXPECT_DECLS;
  46187. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46188. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  46189. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  46190. WOLFSSL_X509* x509 = NULL;
  46191. int peerCertSz;
  46192. const byte* peerCertBuf;
  46193. Cert forgedCert;
  46194. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  46195. WOLFSSL_FILETYPE_ASN1));
  46196. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  46197. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  46198. ExpectIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  46199. wolfSSL_FreeX509(x509);
  46200. #endif
  46201. return EXPECT_RESULT();
  46202. }
  46203. /*
  46204. * Testing wc_SetSubjectKeyId
  46205. */
  46206. static int test_wc_SetSubjectKeyId(void)
  46207. {
  46208. EXPECT_DECLS;
  46209. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46210. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  46211. Cert cert;
  46212. const char* file = "certs/ecc-client-keyPub.pem";
  46213. ExpectIntEQ(0, wc_InitCert(&cert));
  46214. ExpectIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  46215. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  46216. ExpectIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  46217. #endif
  46218. return EXPECT_RESULT();
  46219. } /* END test_wc_SetSubjectKeyId */
  46220. /*
  46221. * Testing wc_SetSubject
  46222. */
  46223. static int test_wc_SetSubject(void)
  46224. {
  46225. EXPECT_DECLS;
  46226. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46227. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  46228. Cert cert;
  46229. const char* file = "./certs/ca-ecc-cert.pem";
  46230. ExpectIntEQ(0, wc_InitCert(&cert));
  46231. ExpectIntEQ(0, wc_SetSubject(&cert, file));
  46232. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  46233. ExpectIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  46234. #endif
  46235. return EXPECT_RESULT();
  46236. } /* END test_wc_SetSubject */
  46237. static int test_CheckCertSignature(void)
  46238. {
  46239. EXPECT_DECLS;
  46240. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  46241. WOLFSSL_CERT_MANAGER* cm = NULL;
  46242. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  46243. XFILE fp = XBADFILE;
  46244. byte cert[4096];
  46245. int certSz;
  46246. #endif
  46247. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  46248. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  46249. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  46250. #ifndef NO_RSA
  46251. #ifdef USE_CERT_BUFFERS_1024
  46252. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  46253. sizeof_server_cert_der_1024, NULL, cm));
  46254. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  46255. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  46256. WOLFSSL_FILETYPE_ASN1));
  46257. ExpectIntEQ(0, CheckCertSignature(server_cert_der_1024,
  46258. sizeof_server_cert_der_1024, NULL, cm));
  46259. #elif defined(USE_CERT_BUFFERS_2048)
  46260. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  46261. sizeof_server_cert_der_2048, NULL, cm));
  46262. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  46263. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  46264. WOLFSSL_FILETYPE_ASN1));
  46265. ExpectIntEQ(0, CheckCertSignature(server_cert_der_2048,
  46266. sizeof_server_cert_der_2048, NULL, cm));
  46267. #endif
  46268. #endif
  46269. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  46270. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  46271. sizeof_serv_ecc_der_256, NULL, cm));
  46272. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  46273. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  46274. WOLFSSL_FILETYPE_ASN1));
  46275. ExpectIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  46276. NULL, cm));
  46277. #endif
  46278. #if !defined(NO_FILESYSTEM)
  46279. wolfSSL_CertManagerFree(cm);
  46280. cm = NULL;
  46281. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  46282. #ifndef NO_RSA
  46283. ExpectTrue((fp = XFOPEN("./certs/server-cert.der", "rb")) != XBADFILE);
  46284. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  46285. if (fp != XBADFILE) {
  46286. XFCLOSE(fp);
  46287. fp = XBADFILE;
  46288. }
  46289. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  46290. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  46291. "./certs/ca-cert.pem", NULL));
  46292. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  46293. #endif
  46294. #ifdef HAVE_ECC
  46295. ExpectTrue((fp = XFOPEN("./certs/server-ecc.der", "rb")) != XBADFILE);
  46296. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  46297. if (fp != XBADFILE) {
  46298. XFCLOSE(fp);
  46299. fp = XBADFILE;
  46300. }
  46301. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  46302. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  46303. "./certs/ca-ecc-cert.pem", NULL));
  46304. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  46305. #endif
  46306. #endif
  46307. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  46308. (void)fp;
  46309. (void)cert;
  46310. (void)certSz;
  46311. #endif
  46312. wolfSSL_CertManagerFree(cm);
  46313. #endif
  46314. return EXPECT_RESULT();
  46315. }
  46316. static int test_wc_ParseCert(void)
  46317. {
  46318. EXPECT_DECLS;
  46319. #if !defined(NO_CERTS) && !defined(NO_RSA)
  46320. DecodedCert decodedCert;
  46321. const byte* rawCert = client_cert_der_2048;
  46322. const int rawCertSize = sizeof_client_cert_der_2048;
  46323. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  46324. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  46325. #ifndef IGNORE_NAME_CONSTRAINTS
  46326. /* check that the subjects emailAddress was not put in the alt name list */
  46327. ExpectNotNull(decodedCert.subjectEmail);
  46328. ExpectNull(decodedCert.altEmailNames);
  46329. #endif
  46330. wc_FreeDecodedCert(&decodedCert);
  46331. #endif
  46332. return EXPECT_RESULT();
  46333. }
  46334. /* Test wc_ParseCert decoding of various encodings and scenarios ensuring that
  46335. * the API safely errors out on badly-formed ASN input.
  46336. * NOTE: Test not compatible with released FIPS implementations!
  46337. */
  46338. static int test_wc_ParseCert_Error(void)
  46339. {
  46340. EXPECT_DECLS;
  46341. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(HAVE_SELFTEST) && \
  46342. (!defined(HAVE_FIPS) || \
  46343. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  46344. DecodedCert decodedCert;
  46345. int i;
  46346. /* Certificate data */
  46347. const byte c0[] = { 0x30, 0x04, 0x30, 0x02, 0x02, 0x80, 0x00, 0x00};
  46348. const byte c1[] = { 0x30, 0x04, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  46349. const byte c2[] = { 0x30, 0x06, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  46350. const byte c3[] = { 0x30, 0x07, 0x30, 0x05, 0x02, 0x80, 0x10, 0x00, 0x00};
  46351. const byte c4[] = { 0x02, 0x80, 0x10, 0x00, 0x00};
  46352. /* Test data */
  46353. const struct testStruct {
  46354. const byte* c;
  46355. const int cSz;
  46356. const int expRet;
  46357. } t[] = {
  46358. {c0, sizeof(c0), ASN_PARSE_E}, /* Invalid bit-string length */
  46359. {c1, sizeof(c1), ASN_PARSE_E}, /* Invalid bit-string length */
  46360. {c2, sizeof(c2), ASN_PARSE_E}, /* Invalid integer length (zero) */
  46361. {c3, sizeof(c3), ASN_PARSE_E}, /* Valid INTEGER, but buffer too short */
  46362. {c4, sizeof(c4), ASN_PARSE_E}, /* Valid INTEGER, but not in bit-string */
  46363. };
  46364. const int tSz = (int)(sizeof(t) / sizeof(struct testStruct));
  46365. for (i = 0; i < tSz; i++) {
  46366. WOLFSSL_MSG_EX("i == %d", i);
  46367. wc_InitDecodedCert(&decodedCert, t[i].c, t[i].cSz, NULL);
  46368. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), t[i].expRet);
  46369. wc_FreeDecodedCert(&decodedCert);
  46370. }
  46371. #endif
  46372. return EXPECT_RESULT();
  46373. }
  46374. static int test_MakeCertWithPathLen(void)
  46375. {
  46376. EXPECT_DECLS;
  46377. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  46378. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  46379. const byte expectedPathLen = 7;
  46380. Cert cert;
  46381. DecodedCert decodedCert;
  46382. byte der[FOURK_BUF];
  46383. int derSize = 0;
  46384. WC_RNG rng;
  46385. ecc_key key;
  46386. int ret;
  46387. XMEMSET(&rng, 0, sizeof(WC_RNG));
  46388. XMEMSET(&key, 0, sizeof(ecc_key));
  46389. XMEMSET(&cert, 0, sizeof(Cert));
  46390. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  46391. ExpectIntEQ(wc_InitRng(&rng), 0);
  46392. ExpectIntEQ(wc_ecc_init(&key), 0);
  46393. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  46394. ExpectIntEQ(wc_InitCert(&cert), 0);
  46395. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  46396. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  46397. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  46398. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  46399. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  46400. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  46401. CTC_NAME_SIZE);
  46402. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  46403. CTC_NAME_SIZE);
  46404. cert.selfSigned = 1;
  46405. cert.isCA = 1;
  46406. cert.pathLen = expectedPathLen;
  46407. cert.pathLenSet = 1;
  46408. cert.sigType = CTC_SHA256wECDSA;
  46409. #ifdef WOLFSSL_CERT_EXT
  46410. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  46411. #endif
  46412. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  46413. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  46414. FOURK_BUF, NULL, &key, &rng), 0);
  46415. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  46416. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  46417. ExpectIntEQ(decodedCert.pathLength, expectedPathLen);
  46418. wc_FreeDecodedCert(&decodedCert);
  46419. ret = wc_ecc_free(&key);
  46420. ExpectIntEQ(ret, 0);
  46421. ret = wc_FreeRng(&rng);
  46422. ExpectIntEQ(ret, 0);
  46423. #endif
  46424. return EXPECT_RESULT();
  46425. }
  46426. static int test_MakeCertWithCaFalse(void)
  46427. {
  46428. EXPECT_DECLS;
  46429. #if defined(WOLFSSL_ALLOW_ENCODING_CA_FALSE) && defined(WOLFSSL_CERT_REQ) && \
  46430. !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  46431. const byte expectedIsCa = 0;
  46432. Cert cert;
  46433. DecodedCert decodedCert;
  46434. byte der[FOURK_BUF];
  46435. int derSize = 0;
  46436. WC_RNG rng;
  46437. ecc_key key;
  46438. int ret;
  46439. XMEMSET(&rng, 0, sizeof(WC_RNG));
  46440. XMEMSET(&key, 0, sizeof(ecc_key));
  46441. XMEMSET(&cert, 0, sizeof(Cert));
  46442. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  46443. ExpectIntEQ(wc_InitRng(&rng), 0);
  46444. ExpectIntEQ(wc_ecc_init(&key), 0);
  46445. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  46446. ExpectIntEQ(wc_InitCert(&cert), 0);
  46447. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  46448. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  46449. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  46450. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  46451. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  46452. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  46453. CTC_NAME_SIZE);
  46454. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  46455. CTC_NAME_SIZE);
  46456. cert.selfSigned = 1;
  46457. cert.isCA = expectedIsCa;
  46458. cert.isCaSet = 1;
  46459. cert.sigType = CTC_SHA256wECDSA;
  46460. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  46461. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  46462. FOURK_BUF, NULL, &key, &rng), 0);
  46463. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  46464. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  46465. ExpectIntEQ(decodedCert.isCA, expectedIsCa);
  46466. wc_FreeDecodedCert(&decodedCert);
  46467. ret = wc_ecc_free(&key);
  46468. ExpectIntEQ(ret, 0);
  46469. ret = wc_FreeRng(&rng);
  46470. ExpectIntEQ(ret, 0);
  46471. #endif
  46472. return EXPECT_RESULT();
  46473. }
  46474. /*----------------------------------------------------------------------------*
  46475. | wolfCrypt ECC
  46476. *----------------------------------------------------------------------------*/
  46477. static int test_wc_ecc_get_curve_size_from_name(void)
  46478. {
  46479. EXPECT_DECLS;
  46480. #ifdef HAVE_ECC
  46481. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46482. ExpectIntEQ(wc_ecc_get_curve_size_from_name("SECP256R1"), 32);
  46483. #endif
  46484. /* invalid case */
  46485. ExpectIntEQ(wc_ecc_get_curve_size_from_name("BADCURVE"), -1);
  46486. /* NULL input */
  46487. ExpectIntEQ(wc_ecc_get_curve_size_from_name(NULL), BAD_FUNC_ARG);
  46488. #endif /* HAVE_ECC */
  46489. return EXPECT_RESULT();
  46490. }
  46491. static int test_wc_ecc_get_curve_id_from_name(void)
  46492. {
  46493. EXPECT_DECLS;
  46494. #ifdef HAVE_ECC
  46495. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46496. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"),
  46497. ECC_SECP256R1);
  46498. #endif
  46499. /* invalid case */
  46500. ExpectIntEQ(wc_ecc_get_curve_id_from_name("BADCURVE"), -1);
  46501. /* NULL input */
  46502. ExpectIntEQ(wc_ecc_get_curve_id_from_name(NULL), BAD_FUNC_ARG);
  46503. #endif /* HAVE_ECC */
  46504. return EXPECT_RESULT();
  46505. }
  46506. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  46507. !defined(HAVE_SELFTEST) && \
  46508. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  46509. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  46510. {
  46511. EXPECT_DECLS;
  46512. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46513. ecc_key* key;
  46514. const ecc_set_type* params = NULL;
  46515. int ret;
  46516. #endif
  46517. WOLFSSL_EC_KEY *ecKey = NULL;
  46518. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46519. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"), ECC_SECP256R1);
  46520. ExpectNotNull(ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  46521. if (EXPECT_SUCCESS()) {
  46522. ret = EC_KEY_generate_key(ecKey);
  46523. } else
  46524. ret = 0;
  46525. if (ret == 1) {
  46526. /* normal test */
  46527. key = (ecc_key*)ecKey->internal;
  46528. if (key != NULL) {
  46529. params = key->dp;
  46530. }
  46531. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(params),
  46532. ECC_SECP256R1);
  46533. }
  46534. #endif
  46535. /* invalid case, NULL input*/
  46536. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(NULL), BAD_FUNC_ARG);
  46537. wolfSSL_EC_KEY_free(ecKey);
  46538. return EXPECT_RESULT();
  46539. }
  46540. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  46541. static int test_wc_ecc_get_curve_id_from_params(void)
  46542. {
  46543. EXPECT_DECLS;
  46544. #ifdef HAVE_ECC
  46545. const byte prime[] =
  46546. {
  46547. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  46548. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46549. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  46550. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  46551. };
  46552. const byte primeInvalid[] =
  46553. {
  46554. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  46555. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46556. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  46557. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  46558. };
  46559. const byte Af[] =
  46560. {
  46561. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  46562. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46563. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  46564. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  46565. };
  46566. const byte Bf[] =
  46567. {
  46568. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  46569. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  46570. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  46571. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  46572. };
  46573. const byte order[] =
  46574. {
  46575. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  46576. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  46577. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  46578. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  46579. };
  46580. const byte Gx[] =
  46581. {
  46582. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  46583. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  46584. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  46585. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  46586. };
  46587. const byte Gy[] =
  46588. {
  46589. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  46590. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  46591. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  46592. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  46593. };
  46594. int cofactor = 1;
  46595. int fieldSize = 256;
  46596. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46597. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  46598. prime, sizeof(prime), Af, sizeof(Af), Bf, sizeof(Bf),
  46599. order, sizeof(order), Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor),
  46600. ECC_SECP256R1);
  46601. #endif
  46602. /* invalid case, fieldSize = 0 */
  46603. ExpectIntEQ(wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  46604. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  46605. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  46606. /* invalid case, NULL prime */
  46607. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  46608. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  46609. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), BAD_FUNC_ARG);
  46610. /* invalid case, invalid prime */
  46611. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  46612. primeInvalid, sizeof(primeInvalid),
  46613. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  46614. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  46615. #endif
  46616. return EXPECT_RESULT();
  46617. }
  46618. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  46619. {
  46620. EXPECT_DECLS;
  46621. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  46622. WOLFSSL_RSA* rsa = NULL;
  46623. WOLFSSL_EVP_PKEY* pkey = NULL;
  46624. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  46625. const char* in = "What is easy to do is easy not to do.";
  46626. size_t inlen = XSTRLEN(in);
  46627. size_t outEncLen = 0;
  46628. byte* outEnc = NULL;
  46629. byte* outDec = NULL;
  46630. size_t outDecLen = 0;
  46631. size_t rsaKeySz = 2048/8; /* Bytes */
  46632. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  46633. byte* inTmp = NULL;
  46634. byte* outEncTmp = NULL;
  46635. byte* outDecTmp = NULL;
  46636. #endif
  46637. ExpectNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  46638. DYNAMIC_TYPE_TMP_BUFFER));
  46639. if (outEnc != NULL) {
  46640. XMEMSET(outEnc, 0, rsaKeySz);
  46641. }
  46642. ExpectNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  46643. DYNAMIC_TYPE_TMP_BUFFER));
  46644. if (outDec != NULL) {
  46645. XMEMSET(outDec, 0, rsaKeySz);
  46646. }
  46647. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46648. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  46649. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  46650. if (EXPECT_FAIL()) {
  46651. RSA_free(rsa);
  46652. }
  46653. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  46654. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  46655. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  46656. WOLFSSL_SUCCESS);
  46657. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  46658. since ctx uses it.*/
  46659. ExpectIntEQ(pkey->ref.count, 2);
  46660. EVP_PKEY_free(pkey);
  46661. ExpectIntEQ(pkey->ref.count, 1);
  46662. /* Encrypt data */
  46663. /* Check that we can get the required output buffer length by passing in a
  46664. * NULL output buffer. */
  46665. ExpectIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  46666. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  46667. ExpectIntEQ(rsaKeySz, outEncLen);
  46668. /* Now do the actual encryption. */
  46669. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  46670. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  46671. /* Decrypt data */
  46672. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  46673. /* Check that we can get the required output buffer length by passing in a
  46674. * NULL output buffer. */
  46675. ExpectIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  46676. WOLFSSL_SUCCESS);
  46677. ExpectIntEQ(rsaKeySz, outDecLen);
  46678. /* Now do the actual decryption. */
  46679. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  46680. WOLFSSL_SUCCESS);
  46681. ExpectIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  46682. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  46683. /* The input length must be the same size as the RSA key.*/
  46684. ExpectNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  46685. DYNAMIC_TYPE_TMP_BUFFER));
  46686. if (inTmp != NULL) {
  46687. XMEMSET(inTmp, 9, rsaKeySz);
  46688. }
  46689. ExpectNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  46690. DYNAMIC_TYPE_TMP_BUFFER));
  46691. if (outEncTmp != NULL) {
  46692. XMEMSET(outEncTmp, 0, rsaKeySz);
  46693. }
  46694. ExpectNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  46695. DYNAMIC_TYPE_TMP_BUFFER));
  46696. if (outDecTmp != NULL) {
  46697. XMEMSET(outDecTmp, 0, rsaKeySz);
  46698. }
  46699. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  46700. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  46701. WOLFSSL_SUCCESS);
  46702. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  46703. WOLFSSL_SUCCESS);
  46704. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  46705. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp,
  46706. outEncLen), WOLFSSL_SUCCESS);
  46707. ExpectIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  46708. #endif
  46709. EVP_PKEY_CTX_free(ctx);
  46710. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46711. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46712. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  46713. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46714. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46715. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46716. #endif
  46717. #endif
  46718. return EXPECT_RESULT();
  46719. }
  46720. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46721. !defined(HAVE_SELFTEST)
  46722. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46723. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46724. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46725. #endif
  46726. #endif
  46727. #endif
  46728. #if defined(OPENSSL_EXTRA)
  46729. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46730. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46731. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46732. #endif
  46733. #endif
  46734. #endif
  46735. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46736. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46737. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46738. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46739. #endif
  46740. #endif
  46741. #endif
  46742. #ifdef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46743. static int test_wolfSSL_EVP_PKEY_sign_verify(int keyType)
  46744. {
  46745. EXPECT_DECLS;
  46746. #if defined(OPENSSL_EXTRA)
  46747. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46748. !defined(HAVE_SELFTEST)
  46749. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46750. WOLFSSL_RSA* rsa = NULL;
  46751. #endif
  46752. #endif
  46753. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46754. WOLFSSL_DSA* dsa = NULL;
  46755. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  46756. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46757. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46758. WOLFSSL_EC_KEY* ecKey = NULL;
  46759. #endif
  46760. #endif
  46761. WOLFSSL_EVP_PKEY* pkey = NULL;
  46762. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  46763. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  46764. const char* in = "What is easy to do is easy not to do.";
  46765. size_t inlen = XSTRLEN(in);
  46766. byte hash[SHA256_DIGEST_LENGTH] = {0};
  46767. byte zero[SHA256_DIGEST_LENGTH] = {0};
  46768. SHA256_CTX c;
  46769. byte* sig = NULL;
  46770. byte* sigVerify = NULL;
  46771. size_t siglen;
  46772. size_t siglenOnlyLen;
  46773. size_t keySz = 2048/8; /* Bytes */
  46774. ExpectNotNull(sig =
  46775. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  46776. ExpectNotNull(sigVerify =
  46777. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  46778. siglen = keySz;
  46779. ExpectNotNull(XMEMSET(sig, 0, keySz));
  46780. ExpectNotNull(XMEMSET(sigVerify, 0, keySz));
  46781. /* Generate hash */
  46782. SHA256_Init(&c);
  46783. SHA256_Update(&c, in, inlen);
  46784. SHA256_Final(hash, &c);
  46785. #ifdef WOLFSSL_SMALL_STACK_CACHE
  46786. /* workaround for small stack cache case */
  46787. wc_Sha256Free((wc_Sha256*)&c);
  46788. #endif
  46789. /* Generate key */
  46790. ExpectNotNull(pkey = EVP_PKEY_new());
  46791. switch (keyType) {
  46792. case EVP_PKEY_RSA:
  46793. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46794. !defined(HAVE_SELFTEST)
  46795. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46796. {
  46797. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46798. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  46799. }
  46800. #endif
  46801. #endif
  46802. break;
  46803. case EVP_PKEY_DSA:
  46804. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46805. ExpectNotNull(dsa = DSA_new());
  46806. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  46807. NULL, 0, NULL, NULL, NULL), 1);
  46808. ExpectIntEQ(DSA_generate_key(dsa), 1);
  46809. ExpectIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  46810. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  46811. break;
  46812. case EVP_PKEY_EC:
  46813. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46814. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46815. {
  46816. ExpectNotNull(ecKey = EC_KEY_new());
  46817. ExpectIntEQ(EC_KEY_generate_key(ecKey), 1);
  46818. ExpectIntEQ(
  46819. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  46820. if (EXPECT_FAIL()) {
  46821. EC_KEY_free(ecKey);
  46822. }
  46823. }
  46824. #endif
  46825. #endif
  46826. break;
  46827. }
  46828. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  46829. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46830. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46831. !defined(HAVE_SELFTEST)
  46832. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46833. if (keyType == EVP_PKEY_RSA)
  46834. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  46835. WOLFSSL_SUCCESS);
  46836. #endif
  46837. #endif
  46838. /* Check returning only length */
  46839. ExpectIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  46840. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  46841. ExpectIntGT(siglenOnlyLen, 0);
  46842. /* Sign data */
  46843. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  46844. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  46845. ExpectIntGE(siglenOnlyLen, siglen);
  46846. /* Verify signature */
  46847. ExpectNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  46848. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  46849. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46850. !defined(HAVE_SELFTEST)
  46851. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46852. if (keyType == EVP_PKEY_RSA)
  46853. ExpectIntEQ(
  46854. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  46855. WOLFSSL_SUCCESS);
  46856. #endif
  46857. #endif
  46858. ExpectIntEQ(EVP_PKEY_verify(
  46859. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  46860. WOLFSSL_SUCCESS);
  46861. ExpectIntEQ(EVP_PKEY_verify(
  46862. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  46863. WOLFSSL_FAILURE);
  46864. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46865. !defined(HAVE_SELFTEST)
  46866. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46867. if (keyType == EVP_PKEY_RSA) {
  46868. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  46869. /* Try RSA sign/verify with no padding. */
  46870. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46871. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  46872. WOLFSSL_SUCCESS);
  46873. ExpectIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  46874. siglen), WOLFSSL_SUCCESS);
  46875. ExpectIntGE(siglenOnlyLen, siglen);
  46876. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  46877. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  46878. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  46879. ExpectIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  46880. siglen), WOLFSSL_SUCCESS);
  46881. #endif
  46882. /* Wrong padding schemes. */
  46883. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46884. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  46885. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  46886. ExpectIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  46887. siglen), WOLFSSL_SUCCESS);
  46888. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  46889. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  46890. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  46891. ExpectIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  46892. siglen), WOLFSSL_SUCCESS);
  46893. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  46894. WOLFSSL_SUCCESS);
  46895. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  46896. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  46897. }
  46898. #endif
  46899. #endif
  46900. /* error cases */
  46901. siglen = keySz; /* Reset because sig size may vary slightly */
  46902. ExpectIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  46903. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46904. ExpectIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  46905. WOLFSSL_SUCCESS);
  46906. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  46907. WOLFSSL_SUCCESS);
  46908. EVP_PKEY_free(pkey);
  46909. pkey = NULL;
  46910. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46911. DSA_free(dsa);
  46912. dsa = NULL;
  46913. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  46914. EVP_PKEY_CTX_free(ctx_verify);
  46915. ctx_verify = NULL;
  46916. EVP_PKEY_CTX_free(ctx);
  46917. ctx = NULL;
  46918. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46919. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46920. #endif /* OPENSSL_EXTRA */
  46921. return EXPECT_RESULT();
  46922. }
  46923. #endif
  46924. static int test_wolfSSL_EVP_PKEY_sign_verify_rsa(void)
  46925. {
  46926. EXPECT_DECLS;
  46927. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46928. !defined(HAVE_SELFTEST)
  46929. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46930. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_RSA), TEST_SUCCESS);
  46931. #endif
  46932. #endif
  46933. return EXPECT_RESULT();
  46934. }
  46935. static int test_wolfSSL_EVP_PKEY_sign_verify_dsa(void)
  46936. {
  46937. EXPECT_DECLS;
  46938. #if defined(OPENSSL_EXTRA)
  46939. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46940. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_DSA), TEST_SUCCESS);
  46941. #endif
  46942. #endif
  46943. return EXPECT_RESULT();
  46944. }
  46945. static int test_wolfSSL_EVP_PKEY_sign_verify_ec(void)
  46946. {
  46947. EXPECT_DECLS;
  46948. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46949. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46950. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_EC), TEST_SUCCESS);
  46951. #endif
  46952. #endif
  46953. return EXPECT_RESULT();
  46954. }
  46955. static int test_EVP_PKEY_rsa(void)
  46956. {
  46957. EXPECT_DECLS;
  46958. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  46959. WOLFSSL_RSA* rsa = NULL;
  46960. WOLFSSL_EVP_PKEY* pkey = NULL;
  46961. ExpectNotNull(rsa = wolfSSL_RSA_new());
  46962. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  46963. ExpectIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  46964. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  46965. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  46966. if (EXPECT_FAIL()) {
  46967. wolfSSL_RSA_free(rsa);
  46968. }
  46969. ExpectPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  46970. wolfSSL_EVP_PKEY_free(pkey);
  46971. #endif
  46972. return EXPECT_RESULT();
  46973. }
  46974. static int test_EVP_PKEY_ec(void)
  46975. {
  46976. EXPECT_DECLS;
  46977. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46978. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46979. WOLFSSL_EC_KEY* ecKey = NULL;
  46980. WOLFSSL_EVP_PKEY* pkey = NULL;
  46981. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  46982. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  46983. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  46984. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  46985. /* Should fail since ecKey is empty */
  46986. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  46987. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  46988. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  46989. if (EXPECT_FAIL()) {
  46990. wolfSSL_EC_KEY_free(ecKey);
  46991. }
  46992. wolfSSL_EVP_PKEY_free(pkey);
  46993. #endif
  46994. #endif
  46995. return EXPECT_RESULT();
  46996. }
  46997. static int test_EVP_PKEY_cmp(void)
  46998. {
  46999. EXPECT_DECLS;
  47000. #if defined(OPENSSL_EXTRA)
  47001. EVP_PKEY *a = NULL;
  47002. EVP_PKEY *b = NULL;
  47003. const unsigned char *in;
  47004. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  47005. in = client_key_der_2048;
  47006. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  47007. &in, (long)sizeof_client_key_der_2048));
  47008. in = client_key_der_2048;
  47009. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  47010. &in, (long)sizeof_client_key_der_2048));
  47011. /* Test success case RSA */
  47012. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  47013. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  47014. #else
  47015. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  47016. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  47017. EVP_PKEY_free(b);
  47018. b = NULL;
  47019. EVP_PKEY_free(a);
  47020. a = NULL;
  47021. #endif
  47022. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  47023. in = ecc_clikey_der_256;
  47024. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  47025. &in, (long)sizeof_ecc_clikey_der_256));
  47026. in = ecc_clikey_der_256;
  47027. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  47028. &in, (long)sizeof_ecc_clikey_der_256));
  47029. /* Test success case ECC */
  47030. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  47031. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  47032. #else
  47033. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  47034. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  47035. EVP_PKEY_free(b);
  47036. b = NULL;
  47037. EVP_PKEY_free(a);
  47038. a = NULL;
  47039. #endif
  47040. /* Test failure cases */
  47041. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  47042. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  47043. in = client_key_der_2048;
  47044. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  47045. &in, (long)sizeof_client_key_der_2048));
  47046. in = ecc_clikey_der_256;
  47047. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  47048. &in, (long)sizeof_ecc_clikey_der_256));
  47049. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  47050. ExpectIntEQ(EVP_PKEY_cmp(a, b), -1);
  47051. #else
  47052. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  47053. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  47054. EVP_PKEY_free(b);
  47055. b = NULL;
  47056. EVP_PKEY_free(a);
  47057. a = NULL;
  47058. #endif
  47059. /* invalid or empty failure cases */
  47060. a = EVP_PKEY_new();
  47061. b = EVP_PKEY_new();
  47062. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  47063. ExpectIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  47064. ExpectIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  47065. ExpectIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  47066. #ifdef NO_RSA
  47067. /* Type check will fail since RSA is the default EVP key type */
  47068. ExpectIntEQ(EVP_PKEY_cmp(a, b), -2);
  47069. #else
  47070. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  47071. #endif
  47072. #else
  47073. ExpectIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  47074. ExpectIntNE(EVP_PKEY_cmp(a, NULL), 0);
  47075. ExpectIntNE(EVP_PKEY_cmp(NULL, b), 0);
  47076. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  47077. #endif
  47078. EVP_PKEY_free(b);
  47079. EVP_PKEY_free(a);
  47080. (void)in;
  47081. #endif
  47082. return EXPECT_RESULT();
  47083. }
  47084. static int test_ERR_load_crypto_strings(void)
  47085. {
  47086. #if defined(OPENSSL_ALL)
  47087. ERR_load_crypto_strings();
  47088. return TEST_SUCCESS;
  47089. #else
  47090. return TEST_SKIPPED;
  47091. #endif
  47092. }
  47093. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  47094. static void free_x509(X509* x)
  47095. {
  47096. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  47097. }
  47098. #endif
  47099. static int test_sk_X509(void)
  47100. {
  47101. EXPECT_DECLS;
  47102. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  47103. {
  47104. STACK_OF(X509)* s = NULL;
  47105. ExpectNotNull(s = sk_X509_new_null());
  47106. ExpectIntEQ(sk_X509_num(s), 0);
  47107. sk_X509_pop_free(s, NULL);
  47108. ExpectNotNull(s = sk_X509_new_null());
  47109. ExpectIntEQ(sk_X509_num(s), 0);
  47110. sk_X509_pop_free(s, NULL);
  47111. ExpectNotNull(s = sk_X509_new_null());
  47112. sk_X509_push(s, (X509*)1);
  47113. ExpectIntEQ(sk_X509_num(s), 1);
  47114. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  47115. sk_X509_push(s, (X509*)2);
  47116. ExpectIntEQ(sk_X509_num(s), 2);
  47117. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  47118. ExpectIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  47119. sk_X509_push(s, (X509*)2);
  47120. sk_X509_pop_free(s, free_x509);
  47121. }
  47122. {
  47123. /* Push a list of 10 X509s onto stack, then verify that
  47124. * value(), push(), shift(), and pop() behave as expected. */
  47125. STACK_OF(X509)* s = NULL;
  47126. X509* xList[10];
  47127. int i = 0;
  47128. const int len = (sizeof(xList) / sizeof(xList[0]));
  47129. for (i = 0; i < len; ++i) {
  47130. xList[i] = NULL;
  47131. ExpectNotNull(xList[i] = X509_new());
  47132. }
  47133. /* test push, pop, and free */
  47134. ExpectNotNull(s = sk_X509_new_null());
  47135. for (i = 0; i < len; ++i) {
  47136. sk_X509_push(s, xList[i]);
  47137. ExpectIntEQ(sk_X509_num(s), i + 1);
  47138. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  47139. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  47140. }
  47141. /* pop returns and removes last pushed on stack, which is index 0
  47142. * in sk_x509_value */
  47143. for (i = 0; i < len; ++i) {
  47144. X509 * x = sk_X509_value(s, 0);
  47145. X509 * y = sk_X509_pop(s);
  47146. X509 * z = xList[len - 1 - i];
  47147. ExpectIntEQ((x == y), 1);
  47148. ExpectIntEQ((x == z), 1);
  47149. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  47150. }
  47151. sk_free(s);
  47152. s = NULL;
  47153. /* test push, shift, and free */
  47154. ExpectNotNull(s = sk_X509_new_null());
  47155. for (i = 0; i < len; ++i) {
  47156. sk_X509_push(s, xList[i]);
  47157. ExpectIntEQ(sk_X509_num(s), i + 1);
  47158. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  47159. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  47160. }
  47161. /* shift returns and removes first pushed on stack, which is index i
  47162. * in sk_x509_value() */
  47163. for (i = 0; i < len; ++i) {
  47164. X509 * x = sk_X509_value(s, len - 1 - i);
  47165. X509 * y = sk_X509_shift(s);
  47166. X509 * z = xList[i];
  47167. ExpectIntEQ((x == y), 1);
  47168. ExpectIntEQ((x == z), 1);
  47169. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  47170. }
  47171. sk_free(s);
  47172. for (i = 0; i < len; ++i)
  47173. X509_free(xList[i]);
  47174. }
  47175. #endif
  47176. return EXPECT_RESULT();
  47177. }
  47178. static int test_sk_X509_CRL(void)
  47179. {
  47180. EXPECT_DECLS;
  47181. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  47182. X509_CRL* crl = NULL;
  47183. XFILE fp = XBADFILE;
  47184. STACK_OF(X509_CRL)* s = NULL;
  47185. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  47186. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  47187. NULL, NULL));
  47188. if (fp != XBADFILE)
  47189. XFCLOSE(fp);
  47190. ExpectNotNull(s = sk_X509_CRL_new());
  47191. ExpectIntEQ(sk_X509_CRL_num(s), 0);
  47192. ExpectIntEQ(sk_X509_CRL_push(s, crl), 1);
  47193. if (EXPECT_FAIL()) {
  47194. X509_CRL_free(crl);
  47195. }
  47196. ExpectIntEQ(sk_X509_CRL_num(s), 1);
  47197. ExpectPtrEq(sk_X509_CRL_value(s, 0), crl);
  47198. sk_X509_CRL_free(s);
  47199. #endif
  47200. return EXPECT_RESULT();
  47201. }
  47202. static int test_X509_get_signature_nid(void)
  47203. {
  47204. EXPECT_DECLS;
  47205. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  47206. X509* x509 = NULL;
  47207. ExpectIntEQ(X509_get_signature_nid(NULL), 0);
  47208. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  47209. SSL_FILETYPE_PEM));
  47210. ExpectIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  47211. X509_free(x509);
  47212. #endif
  47213. return EXPECT_RESULT();
  47214. }
  47215. static int test_X509_REQ(void)
  47216. {
  47217. EXPECT_DECLS;
  47218. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  47219. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  47220. X509_NAME* name = NULL;
  47221. #ifndef NO_RSA
  47222. X509_NAME* subject = NULL;
  47223. #endif
  47224. #if !defined(NO_RSA) || defined(HAVE_ECC)
  47225. X509_REQ* req = NULL;
  47226. EVP_PKEY* priv = NULL;
  47227. EVP_PKEY* pub = NULL;
  47228. unsigned char* der = NULL;
  47229. int len;
  47230. #endif
  47231. #ifndef NO_RSA
  47232. EVP_MD_CTX *mctx = NULL;
  47233. EVP_PKEY_CTX *pkctx = NULL;
  47234. #ifdef USE_CERT_BUFFERS_1024
  47235. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  47236. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  47237. #elif defined(USE_CERT_BUFFERS_2048)
  47238. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  47239. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  47240. #endif
  47241. #endif
  47242. #ifdef HAVE_ECC
  47243. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  47244. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  47245. #endif
  47246. ExpectNotNull(name = X509_NAME_new());
  47247. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  47248. (byte*)"wolfssl.com", 11, 0, 1), WOLFSSL_SUCCESS);
  47249. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  47250. (byte*)"support@wolfssl.com", 19, -1, 1), WOLFSSL_SUCCESS);
  47251. #ifndef NO_RSA
  47252. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  47253. (long)sizeof_client_key_der_2048));
  47254. ExpectNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  47255. (long)sizeof_client_keypub_der_2048));
  47256. ExpectNotNull(req = X509_REQ_new());
  47257. ExpectIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  47258. ExpectIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  47259. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  47260. ExpectIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  47261. ExpectIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  47262. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  47263. ExpectIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  47264. ExpectIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  47265. ExpectIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  47266. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  47267. len = i2d_X509_REQ(req, &der);
  47268. DEBUG_WRITE_DER(der, len, "req.der");
  47269. #ifdef USE_CERT_BUFFERS_1024
  47270. ExpectIntEQ(len, 381);
  47271. #else
  47272. ExpectIntEQ(len, 643);
  47273. #endif
  47274. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  47275. der = NULL;
  47276. mctx = EVP_MD_CTX_new();
  47277. ExpectIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv),
  47278. WOLFSSL_SUCCESS);
  47279. ExpectIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  47280. EVP_MD_CTX_free(mctx);
  47281. mctx = NULL;
  47282. X509_REQ_free(NULL);
  47283. X509_REQ_free(req);
  47284. req = NULL;
  47285. /* Test getting the subject from a newly created X509_REQ */
  47286. ExpectNotNull(req = X509_REQ_new());
  47287. ExpectNotNull(subject = X509_REQ_get_subject_name(req));
  47288. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  47289. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  47290. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  47291. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  47292. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  47293. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  47294. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  47295. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  47296. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  47297. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  47298. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  47299. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  47300. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  47301. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  47302. len = i2d_X509_REQ(req, &der);
  47303. DEBUG_WRITE_DER(der, len, "req2.der");
  47304. #ifdef USE_CERT_BUFFERS_1024
  47305. ExpectIntEQ(len, 435);
  47306. #else
  47307. ExpectIntEQ(len, 696);
  47308. #endif
  47309. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  47310. der = NULL;
  47311. EVP_PKEY_free(pub);
  47312. pub = NULL;
  47313. EVP_PKEY_free(priv);
  47314. priv = NULL;
  47315. X509_REQ_free(req);
  47316. req = NULL;
  47317. #endif
  47318. #ifdef HAVE_ECC
  47319. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  47320. sizeof_ecc_clikey_der_256));
  47321. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  47322. sizeof_ecc_clikeypub_der_256));
  47323. ExpectNotNull(req = X509_REQ_new());
  47324. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  47325. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  47326. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  47327. /* Signature is random and may be shorter or longer. */
  47328. ExpectIntGE((len = i2d_X509_REQ(req, &der)), 245);
  47329. ExpectIntLE(len, 253);
  47330. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  47331. X509_REQ_free(req);
  47332. EVP_PKEY_free(pub);
  47333. EVP_PKEY_free(priv);
  47334. #ifdef FP_ECC
  47335. wc_ecc_fp_free();
  47336. #endif
  47337. #endif /* HAVE_ECC */
  47338. X509_NAME_free(name);
  47339. #endif
  47340. return EXPECT_RESULT();
  47341. }
  47342. static int test_wolfssl_PKCS7(void)
  47343. {
  47344. EXPECT_DECLS;
  47345. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  47346. !defined(NO_RSA)
  47347. PKCS7* pkcs7 = NULL;
  47348. byte data[FOURK_BUF];
  47349. word32 len = sizeof(data);
  47350. const byte* p = data;
  47351. byte content[] = "Test data to encode.";
  47352. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  47353. BIO* bio = NULL;
  47354. byte key[sizeof(client_key_der_2048)];
  47355. word32 keySz = (word32)sizeof(key);
  47356. byte* out = NULL;
  47357. #endif
  47358. ExpectIntGT((len = CreatePKCS7SignedData(data, len, content,
  47359. (word32)sizeof(content), 0, 0, 0, RSA_TYPE)), 0);
  47360. ExpectNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  47361. ExpectNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  47362. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  47363. ExpectIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  47364. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  47365. PKCS7_free(pkcs7);
  47366. pkcs7 = NULL;
  47367. /* fail case, without PKCS7_NOVERIFY */
  47368. p = data;
  47369. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  47370. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  47371. 0), WOLFSSL_FAILURE);
  47372. PKCS7_free(pkcs7);
  47373. pkcs7 = NULL;
  47374. /* success case, with PKCS7_NOVERIFY */
  47375. p = data;
  47376. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  47377. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  47378. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  47379. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  47380. /* test i2d */
  47381. XMEMCPY(key, client_key_der_2048, keySz);
  47382. if (pkcs7 != NULL) {
  47383. pkcs7->privateKey = key;
  47384. pkcs7->privateKeySz = (word32)sizeof(key);
  47385. pkcs7->encryptOID = RSAk;
  47386. #ifdef NO_SHA
  47387. pkcs7->hashOID = SHA256h;
  47388. #else
  47389. pkcs7->hashOID = SHAh;
  47390. #endif
  47391. }
  47392. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  47393. ExpectIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  47394. #ifndef NO_ASN_TIME
  47395. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  47396. #else
  47397. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 625);
  47398. #endif
  47399. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47400. BIO_free(bio);
  47401. #endif
  47402. PKCS7_free(NULL);
  47403. PKCS7_free(pkcs7);
  47404. #endif
  47405. return EXPECT_RESULT();
  47406. }
  47407. static int test_wolfSSL_PKCS7_sign(void)
  47408. {
  47409. EXPECT_DECLS;
  47410. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  47411. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  47412. PKCS7* p7 = NULL;
  47413. PKCS7* p7Ver = NULL;
  47414. byte* out = NULL;
  47415. byte* tmpPtr = NULL;
  47416. int outLen = 0;
  47417. int flags = 0;
  47418. byte data[] = "Test data to encode.";
  47419. const char* cert = "./certs/server-cert.pem";
  47420. const char* key = "./certs/server-key.pem";
  47421. const char* ca = "./certs/ca-cert.pem";
  47422. WOLFSSL_BIO* certBio = NULL;
  47423. WOLFSSL_BIO* keyBio = NULL;
  47424. WOLFSSL_BIO* caBio = NULL;
  47425. WOLFSSL_BIO* inBio = NULL;
  47426. X509* signCert = NULL;
  47427. EVP_PKEY* signKey = NULL;
  47428. X509* caCert = NULL;
  47429. X509_STORE* store = NULL;
  47430. #ifndef NO_PKCS7_STREAM
  47431. int z;
  47432. int ret;
  47433. #endif /* !NO_PKCS7_STREAM */
  47434. /* read signer cert/key into BIO */
  47435. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  47436. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  47437. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  47438. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  47439. /* read CA cert into store (for verify) */
  47440. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  47441. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  47442. ExpectNotNull(store = X509_STORE_new());
  47443. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  47444. /* data to be signed into BIO */
  47445. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47446. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47447. /* PKCS7_sign, bad args: signer NULL */
  47448. ExpectNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  47449. /* PKCS7_sign, bad args: signer key NULL */
  47450. ExpectNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  47451. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  47452. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  47453. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  47454. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  47455. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  47456. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  47457. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  47458. {
  47459. flags = PKCS7_BINARY;
  47460. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47461. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  47462. /* verify with d2i_PKCS7 */
  47463. tmpPtr = out;
  47464. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  47465. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  47466. PKCS7_free(p7Ver);
  47467. p7Ver = NULL;
  47468. /* verify with wc_PKCS7_VerifySignedData */
  47469. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47470. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  47471. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  47472. #ifndef NO_PKCS7_STREAM
  47473. /* verify with wc_PKCS7_VerifySignedData streaming */
  47474. wc_PKCS7_Free(p7Ver);
  47475. p7Ver = NULL;
  47476. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47477. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  47478. /* test for streaming */
  47479. ret = -1;
  47480. for (z = 0; z < outLen && ret != 0; z++) {
  47481. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  47482. if (ret < 0){
  47483. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  47484. }
  47485. }
  47486. ExpectIntEQ(ret, 0);
  47487. #endif /* !NO_PKCS7_STREAM */
  47488. /* compare the signer found to expected signer */
  47489. ExpectIntNE(p7Ver->verifyCertSz, 0);
  47490. tmpPtr = NULL;
  47491. ExpectIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  47492. ExpectIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  47493. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  47494. tmpPtr = NULL;
  47495. wc_PKCS7_Free(p7Ver);
  47496. p7Ver = NULL;
  47497. ExpectNotNull(out);
  47498. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47499. out = NULL;
  47500. PKCS7_free(p7);
  47501. p7 = NULL;
  47502. }
  47503. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  47504. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  47505. {
  47506. /* re-populate input BIO, may have been consumed */
  47507. BIO_free(inBio);
  47508. inBio = NULL;
  47509. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47510. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47511. flags = PKCS7_BINARY | PKCS7_STREAM;
  47512. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47513. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  47514. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  47515. /* PKCS7_final, bad args: PKCS7 null */
  47516. ExpectIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  47517. /* PKCS7_final, bad args: PKCS7 null */
  47518. ExpectIntEQ(PKCS7_final(p7, NULL, 0), 0);
  47519. tmpPtr = out;
  47520. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  47521. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  47522. PKCS7_free(p7Ver);
  47523. p7Ver = NULL;
  47524. ExpectNotNull(out);
  47525. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47526. out = NULL;
  47527. PKCS7_free(p7);
  47528. p7 = NULL;
  47529. }
  47530. /* TEST SUCCESS: Detached, not streaming, not MIME */
  47531. {
  47532. /* re-populate input BIO, may have been consumed */
  47533. BIO_free(inBio);
  47534. inBio = NULL;
  47535. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47536. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47537. flags = PKCS7_BINARY | PKCS7_DETACHED;
  47538. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47539. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  47540. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  47541. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47542. if (p7Ver != NULL) {
  47543. p7Ver->content = data;
  47544. p7Ver->contentSz = sizeof(data);
  47545. }
  47546. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  47547. wc_PKCS7_Free(p7Ver);
  47548. p7Ver = NULL;
  47549. #ifndef NO_PKCS7_STREAM
  47550. /* verify with wc_PKCS7_VerifySignedData streaming */
  47551. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47552. if (p7Ver != NULL) {
  47553. p7Ver->content = data;
  47554. p7Ver->contentSz = sizeof(data);
  47555. }
  47556. /* test for streaming */
  47557. ret = -1;
  47558. for (z = 0; z < outLen && ret != 0; z++) {
  47559. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  47560. if (ret < 0){
  47561. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  47562. }
  47563. }
  47564. ExpectIntEQ(ret, 0);
  47565. wc_PKCS7_Free(p7Ver);
  47566. p7Ver = NULL;
  47567. #endif /* !NO_PKCS7_STREAM */
  47568. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  47569. * yet support detached content */
  47570. tmpPtr = out;
  47571. ExpectNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  47572. PKCS7_free(p7Ver);
  47573. p7Ver = NULL;
  47574. ExpectNotNull(out);
  47575. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47576. out = NULL;
  47577. PKCS7_free(p7);
  47578. p7 = NULL;
  47579. }
  47580. /* TEST SUCCESS: Detached, streaming, not MIME */
  47581. {
  47582. /* re-populate input BIO, may have been consumed */
  47583. BIO_free(inBio);
  47584. inBio = NULL;
  47585. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47586. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47587. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  47588. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47589. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  47590. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  47591. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  47592. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47593. if (p7Ver != NULL) {
  47594. p7Ver->content = data;
  47595. p7Ver->contentSz = sizeof(data);
  47596. }
  47597. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  47598. wc_PKCS7_Free(p7Ver);
  47599. p7Ver = NULL;
  47600. ExpectNotNull(out);
  47601. #ifndef NO_PKCS7_STREAM
  47602. /* verify with wc_PKCS7_VerifySignedData streaming */
  47603. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47604. if (p7Ver != NULL) {
  47605. p7Ver->content = data;
  47606. p7Ver->contentSz = sizeof(data);
  47607. }
  47608. /* test for streaming */
  47609. ret = -1;
  47610. for (z = 0; z < outLen && ret != 0; z++) {
  47611. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  47612. if (ret < 0){
  47613. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  47614. }
  47615. }
  47616. ExpectIntEQ(ret, 0);
  47617. ExpectNotNull(out);
  47618. wc_PKCS7_Free(p7Ver);
  47619. p7Ver = NULL;
  47620. #endif /* !NO_PKCS7_STREAM */
  47621. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47622. PKCS7_free(p7);
  47623. p7 = NULL;
  47624. }
  47625. X509_STORE_free(store);
  47626. X509_free(caCert);
  47627. X509_free(signCert);
  47628. EVP_PKEY_free(signKey);
  47629. BIO_free(inBio);
  47630. BIO_free(keyBio);
  47631. BIO_free(certBio);
  47632. BIO_free(caBio);
  47633. #endif
  47634. return EXPECT_RESULT();
  47635. }
  47636. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  47637. {
  47638. EXPECT_DECLS;
  47639. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  47640. PKCS7_SIGNED* pkcs7 = NULL;
  47641. ExpectNotNull(pkcs7 = PKCS7_SIGNED_new());
  47642. ExpectIntEQ(pkcs7->contentOID, SIGNED_DATA);
  47643. PKCS7_SIGNED_free(pkcs7);
  47644. #endif
  47645. return EXPECT_RESULT();
  47646. }
  47647. #ifndef NO_BIO
  47648. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  47649. {
  47650. EXPECT_DECLS;
  47651. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  47652. PKCS7* pkcs7 = NULL;
  47653. BIO* bio = NULL;
  47654. const byte* cert_buf = NULL;
  47655. int ret = 0;
  47656. WC_RNG rng;
  47657. const byte data[] = { /* Hello World */
  47658. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  47659. 0x72,0x6c,0x64
  47660. };
  47661. #ifndef NO_RSA
  47662. #if defined(USE_CERT_BUFFERS_2048)
  47663. byte key[sizeof(client_key_der_2048)];
  47664. byte cert[sizeof(client_cert_der_2048)];
  47665. word32 keySz = (word32)sizeof(key);
  47666. word32 certSz = (word32)sizeof(cert);
  47667. XMEMSET(key, 0, keySz);
  47668. XMEMSET(cert, 0, certSz);
  47669. XMEMCPY(key, client_key_der_2048, keySz);
  47670. XMEMCPY(cert, client_cert_der_2048, certSz);
  47671. #elif defined(USE_CERT_BUFFERS_1024)
  47672. byte key[sizeof_client_key_der_1024];
  47673. byte cert[sizeof(sizeof_client_cert_der_1024)];
  47674. word32 keySz = (word32)sizeof(key);
  47675. word32 certSz = (word32)sizeof(cert);
  47676. XMEMSET(key, 0, keySz);
  47677. XMEMSET(cert, 0, certSz);
  47678. XMEMCPY(key, client_key_der_1024, keySz);
  47679. XMEMCPY(cert, client_cert_der_1024, certSz);
  47680. #else
  47681. unsigned char cert[ONEK_BUF];
  47682. unsigned char key[ONEK_BUF];
  47683. XFILE fp = XBADFILE;
  47684. int certSz;
  47685. int keySz;
  47686. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  47687. XBADFILE);
  47688. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  47689. fp), 0);
  47690. if (fp != XBADFILE) {
  47691. XFCLOSE(fp);
  47692. fp = XBADFILE;
  47693. }
  47694. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  47695. XBADFILE);
  47696. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  47697. 0);
  47698. if (fp != XBADFILE) {
  47699. XFCLOSE(fp);
  47700. fp = XBADFILE;
  47701. }
  47702. #endif
  47703. #elif defined(HAVE_ECC)
  47704. #if defined(USE_CERT_BUFFERS_256)
  47705. unsigned char cert[sizeof(cliecc_cert_der_256)];
  47706. unsigned char key[sizeof(ecc_clikey_der_256)];
  47707. int certSz = (int)sizeof(cert);
  47708. int keySz = (int)sizeof(key);
  47709. XMEMSET(cert, 0, certSz);
  47710. XMEMSET(key, 0, keySz);
  47711. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  47712. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  47713. #else
  47714. unsigned char cert[ONEK_BUF];
  47715. unsigned char key[ONEK_BUF];
  47716. XFILE fp = XBADFILE;
  47717. int certSz, keySz;
  47718. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  47719. XBADFILE);
  47720. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  47721. fp), 0);
  47722. if (fp != XBADFILE) {
  47723. XFCLOSE(fp);
  47724. fp = XBADFILE;
  47725. }
  47726. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  47727. XBADFILE);
  47728. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  47729. 0);
  47730. if (fp != XBADFILE) {
  47731. XFCLOSE(fp);
  47732. fp = XBADFILE;
  47733. }
  47734. #endif
  47735. #else
  47736. #error PKCS7 requires ECC or RSA
  47737. #endif
  47738. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  47739. /* initialize with DER encoded cert */
  47740. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  47741. /* init rng */
  47742. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47743. ExpectIntEQ(wc_InitRng(&rng), 0);
  47744. if (pkcs7 != NULL) {
  47745. pkcs7->rng = &rng;
  47746. pkcs7->content = (byte*)data; /* not used for ex */
  47747. pkcs7->contentSz = (word32)sizeof(data);
  47748. pkcs7->contentOID = SIGNED_DATA;
  47749. pkcs7->privateKey = key;
  47750. pkcs7->privateKeySz = (word32)sizeof(key);
  47751. pkcs7->encryptOID = RSAk;
  47752. #ifdef NO_SHA
  47753. pkcs7->hashOID = SHA256h;
  47754. #else
  47755. pkcs7->hashOID = SHAh;
  47756. #endif
  47757. pkcs7->signedAttribs = NULL;
  47758. pkcs7->signedAttribsSz = 0;
  47759. }
  47760. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  47761. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  47762. ExpectIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  47763. /* Read PKCS#7 PEM from BIO */
  47764. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  47765. ExpectIntGE(ret, 0);
  47766. BIO_free(bio);
  47767. wc_PKCS7_Free(pkcs7);
  47768. wc_FreeRng(&rng);
  47769. #endif
  47770. return EXPECT_RESULT();
  47771. }
  47772. #ifdef HAVE_SMIME
  47773. static int test_wolfSSL_SMIME_read_PKCS7(void)
  47774. {
  47775. EXPECT_DECLS;
  47776. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  47777. !defined(NO_RSA)
  47778. PKCS7* pkcs7 = NULL;
  47779. BIO* bio = NULL;
  47780. BIO* bcont = NULL;
  47781. BIO* out = NULL;
  47782. const byte* outBuf = NULL;
  47783. int outBufLen = 0;
  47784. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  47785. XFILE smimeTestFile = XBADFILE;
  47786. ExpectTrue((smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r")) !=
  47787. XBADFILE);
  47788. /* smime-test.p7s */
  47789. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  47790. ExpectNotNull(bio);
  47791. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47792. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47793. ExpectNotNull(pkcs7);
  47794. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47795. PKCS7_NOVERIFY), SSL_SUCCESS);
  47796. XFCLOSE(smimeTestFile);
  47797. if (bcont) BIO_free(bcont);
  47798. bcont = NULL;
  47799. wolfSSL_PKCS7_free(pkcs7);
  47800. pkcs7 = NULL;
  47801. /* smime-test-multipart.p7s */
  47802. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  47803. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47804. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47805. ExpectNotNull(pkcs7);
  47806. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47807. PKCS7_NOVERIFY), SSL_SUCCESS);
  47808. XFCLOSE(smimeTestFile);
  47809. if (bcont) BIO_free(bcont);
  47810. bcont = NULL;
  47811. wolfSSL_PKCS7_free(pkcs7);
  47812. pkcs7 = NULL;
  47813. /* smime-test-multipart-badsig.p7s */
  47814. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  47815. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47816. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47817. ExpectNotNull(pkcs7); /* can read in the unverified smime bundle */
  47818. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47819. PKCS7_NOVERIFY), SSL_FAILURE);
  47820. XFCLOSE(smimeTestFile);
  47821. if (bcont) BIO_free(bcont);
  47822. bcont = NULL;
  47823. wolfSSL_PKCS7_free(pkcs7);
  47824. pkcs7 = NULL;
  47825. /* smime-test-canon.p7s */
  47826. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  47827. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47828. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47829. ExpectNotNull(pkcs7);
  47830. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47831. PKCS7_NOVERIFY), SSL_SUCCESS);
  47832. XFCLOSE(smimeTestFile);
  47833. if (bcont) BIO_free(bcont);
  47834. bcont = NULL;
  47835. wolfSSL_PKCS7_free(pkcs7);
  47836. pkcs7 = NULL;
  47837. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  47838. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  47839. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47840. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47841. ExpectNotNull(pkcs7);
  47842. out = wolfSSL_BIO_new(BIO_s_mem());
  47843. ExpectNotNull(out);
  47844. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  47845. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  47846. ExpectIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  47847. /* Content-Type should not show up at beginning of output buffer */
  47848. ExpectIntGT(outBufLen, XSTRLEN(contTypeText));
  47849. ExpectIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  47850. BIO_free(out);
  47851. BIO_free(bio);
  47852. if (bcont) BIO_free(bcont);
  47853. wolfSSL_PKCS7_free(pkcs7);
  47854. #endif
  47855. return EXPECT_RESULT();
  47856. }
  47857. static int test_wolfSSL_SMIME_write_PKCS7(void)
  47858. {
  47859. EXPECT_DECLS;
  47860. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  47861. PKCS7* p7 = NULL;
  47862. PKCS7* p7Ver = NULL;
  47863. int flags = 0;
  47864. byte data[] = "Test data to encode.";
  47865. const char* cert = "./certs/server-cert.pem";
  47866. const char* key = "./certs/server-key.pem";
  47867. const char* ca = "./certs/ca-cert.pem";
  47868. WOLFSSL_BIO* certBio = NULL;
  47869. WOLFSSL_BIO* keyBio = NULL;
  47870. WOLFSSL_BIO* caBio = NULL;
  47871. WOLFSSL_BIO* inBio = NULL;
  47872. WOLFSSL_BIO* outBio = NULL;
  47873. WOLFSSL_BIO* content = NULL;
  47874. X509* signCert = NULL;
  47875. EVP_PKEY* signKey = NULL;
  47876. X509* caCert = NULL;
  47877. X509_STORE* store = NULL;
  47878. /* read signer cert/key into BIO */
  47879. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  47880. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  47881. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  47882. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  47883. /* read CA cert into store (for verify) */
  47884. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  47885. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  47886. ExpectNotNull(store = X509_STORE_new());
  47887. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  47888. /* generate and verify SMIME: not detached */
  47889. {
  47890. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47891. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47892. flags = PKCS7_STREAM;
  47893. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47894. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  47895. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47896. /* bad arg: out NULL */
  47897. ExpectIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  47898. /* bad arg: pkcs7 NULL */
  47899. ExpectIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  47900. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47901. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  47902. BIO_free(content);
  47903. content = NULL;
  47904. BIO_free(inBio);
  47905. inBio = NULL;
  47906. BIO_free(outBio);
  47907. outBio = NULL;
  47908. PKCS7_free(p7Ver);
  47909. p7Ver = NULL;
  47910. PKCS7_free(p7);
  47911. p7 = NULL;
  47912. }
  47913. /* generate and verify SMIME: not detached, add Content-Type */
  47914. {
  47915. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47916. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47917. flags = PKCS7_STREAM | PKCS7_TEXT;
  47918. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47919. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  47920. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47921. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47922. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  47923. BIO_free(content);
  47924. content = NULL;
  47925. BIO_free(inBio);
  47926. inBio = NULL;
  47927. BIO_free(outBio);
  47928. outBio = NULL;
  47929. PKCS7_free(p7Ver);
  47930. p7Ver = NULL;
  47931. PKCS7_free(p7);
  47932. p7 = NULL;
  47933. }
  47934. /* generate and verify SMIME: detached */
  47935. {
  47936. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47937. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47938. flags = PKCS7_DETACHED | PKCS7_STREAM;
  47939. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47940. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  47941. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47942. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47943. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  47944. BIO_free(content);
  47945. content = NULL;
  47946. BIO_free(inBio);
  47947. inBio = NULL;
  47948. BIO_free(outBio);
  47949. outBio = NULL;
  47950. PKCS7_free(p7Ver);
  47951. p7Ver = NULL;
  47952. PKCS7_free(p7);
  47953. p7 = NULL;
  47954. }
  47955. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  47956. {
  47957. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47958. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47959. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  47960. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47961. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  47962. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47963. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47964. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  47965. BIO_free(content);
  47966. content = NULL;
  47967. BIO_free(inBio);
  47968. inBio = NULL;
  47969. BIO_free(outBio);
  47970. outBio = NULL;
  47971. PKCS7_free(p7Ver);
  47972. p7Ver = NULL;
  47973. PKCS7_free(p7);
  47974. p7 = NULL;
  47975. }
  47976. X509_STORE_free(store);
  47977. X509_free(caCert);
  47978. X509_free(signCert);
  47979. EVP_PKEY_free(signKey);
  47980. BIO_free(keyBio);
  47981. BIO_free(certBio);
  47982. BIO_free(caBio);
  47983. #endif
  47984. return EXPECT_RESULT();
  47985. }
  47986. #endif /* HAVE_SMIME */
  47987. #endif /* !NO_BIO */
  47988. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  47989. * returns 0) */
  47990. static int test_X509_STORE_No_SSL_CTX(void)
  47991. {
  47992. EXPECT_DECLS;
  47993. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  47994. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  47995. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  47996. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  47997. defined(HAVE_CRL) && !defined(NO_RSA)
  47998. X509_STORE * store = NULL;
  47999. X509_STORE_CTX * storeCtx = NULL;
  48000. X509_CRL * crl = NULL;
  48001. X509 * ca = NULL;
  48002. X509 * cert = NULL;
  48003. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  48004. const char srvCert[] = "./certs/server-cert.pem";
  48005. const char caCert[] = "./certs/ca-cert.pem";
  48006. const char caDir[] = "./certs/crl/hash_pem";
  48007. XFILE fp = XBADFILE;
  48008. X509_LOOKUP * lookup = NULL;
  48009. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  48010. /* Set up store with CA */
  48011. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  48012. SSL_FILETYPE_PEM)));
  48013. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  48014. /* Add CRL lookup directory to store
  48015. * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  48016. * of crl.pem */
  48017. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  48018. X509_LOOKUP_hash_dir())));
  48019. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  48020. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  48021. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  48022. SSL_SUCCESS);
  48023. /* Add CRL to store NOT containing the verified certificate, which
  48024. * forces use of the CRL lookup directory */
  48025. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  48026. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  48027. NULL, NULL));
  48028. if (fp != XBADFILE)
  48029. XFCLOSE(fp);
  48030. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  48031. /* Create verification context outside of an SSL session */
  48032. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  48033. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  48034. SSL_FILETYPE_PEM)));
  48035. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  48036. /* Perform verification, which should NOT indicate CRL missing due to the
  48037. * store CM's X509 store pointer being NULL */
  48038. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  48039. X509_CRL_free(crl);
  48040. X509_STORE_free(store);
  48041. X509_STORE_CTX_free(storeCtx);
  48042. X509_free(cert);
  48043. X509_free(ca);
  48044. #endif
  48045. return EXPECT_RESULT();
  48046. }
  48047. /* Test of X509 store use outside of SSL context w/ CRL lookup, but
  48048. * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  48049. static int test_X509_LOOKUP_add_dir(void)
  48050. {
  48051. EXPECT_DECLS;
  48052. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  48053. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  48054. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  48055. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  48056. defined(HAVE_CRL) && !defined(NO_RSA)
  48057. X509_STORE * store = NULL;
  48058. X509_STORE_CTX * storeCtx = NULL;
  48059. X509_CRL * crl = NULL;
  48060. X509 * ca = NULL;
  48061. X509 * cert = NULL;
  48062. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  48063. const char srvCert[] = "./certs/server-cert.pem";
  48064. const char caCert[] = "./certs/ca-cert.pem";
  48065. const char caDir[] = "./certs/crl/hash_der";
  48066. XFILE fp = XBADFILE;
  48067. X509_LOOKUP * lookup = NULL;
  48068. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  48069. /* Set up store with CA */
  48070. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  48071. SSL_FILETYPE_PEM)));
  48072. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  48073. /* Add CRL lookup directory to store.
  48074. * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  48075. * of crl.der */
  48076. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  48077. X509_LOOKUP_hash_dir())));
  48078. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  48079. SSL_SUCCESS);
  48080. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  48081. SSL_SUCCESS);
  48082. /* Add CRL to store NOT containing the verified certificate, which
  48083. * forces use of the CRL lookup directory */
  48084. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  48085. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  48086. NULL, NULL));
  48087. if (fp != XBADFILE) {
  48088. XFCLOSE(fp);
  48089. fp = XBADFILE;
  48090. }
  48091. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  48092. /* Create verification context outside of an SSL session */
  48093. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  48094. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  48095. SSL_FILETYPE_PEM)));
  48096. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  48097. /* Perform verification, which should NOT return CRL missing */
  48098. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  48099. X509_CRL_free(crl);
  48100. crl = NULL;
  48101. X509_STORE_free(store);
  48102. store = NULL;
  48103. X509_STORE_CTX_free(storeCtx);
  48104. storeCtx = NULL;
  48105. X509_free(cert);
  48106. cert = NULL;
  48107. X509_free(ca);
  48108. ca = NULL;
  48109. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  48110. * We should get CRL_MISSING at the end, because the lookup
  48111. * dir has only ASN1 CRLs. */
  48112. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  48113. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  48114. SSL_FILETYPE_PEM)));
  48115. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  48116. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  48117. X509_LOOKUP_hash_dir())));
  48118. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  48119. SSL_SUCCESS);
  48120. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  48121. SSL_SUCCESS);
  48122. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  48123. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  48124. NULL, NULL));
  48125. if (fp != XBADFILE) {
  48126. XFCLOSE(fp);
  48127. fp = XBADFILE;
  48128. }
  48129. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  48130. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  48131. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  48132. SSL_FILETYPE_PEM)));
  48133. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  48134. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  48135. * in dir containing only ASN1/DER. */
  48136. ExpectIntEQ(X509_verify_cert(storeCtx), WOLFSSL_FAILURE);
  48137. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_MISSING);
  48138. X509_CRL_free(crl);
  48139. X509_STORE_free(store);
  48140. X509_STORE_CTX_free(storeCtx);
  48141. X509_free(cert);
  48142. X509_free(ca);
  48143. #endif
  48144. return EXPECT_RESULT();
  48145. }
  48146. /*----------------------------------------------------------------------------*
  48147. | Certificate Failure Checks
  48148. *----------------------------------------------------------------------------*/
  48149. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  48150. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  48151. #if !defined(NO_RSA) || defined(HAVE_ECC)
  48152. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  48153. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  48154. int type)
  48155. {
  48156. int ret;
  48157. WOLFSSL_CERT_MANAGER* cm = NULL;
  48158. switch (type) {
  48159. case TESTING_RSA:
  48160. #ifdef NO_RSA
  48161. fprintf(stderr, "RSA disabled, skipping test\n");
  48162. return ASN_SIG_CONFIRM_E;
  48163. #else
  48164. break;
  48165. #endif
  48166. case TESTING_ECC:
  48167. #ifndef HAVE_ECC
  48168. fprintf(stderr, "ECC disabled, skipping test\n");
  48169. return ASN_SIG_CONFIRM_E;
  48170. #else
  48171. break;
  48172. #endif
  48173. default:
  48174. fprintf(stderr, "Bad function argument\n");
  48175. return BAD_FUNC_ARG;
  48176. }
  48177. cm = wolfSSL_CertManagerNew();
  48178. if (cm == NULL) {
  48179. fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
  48180. return -1;
  48181. }
  48182. #ifndef NO_FILESYSTEM
  48183. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  48184. if (ret != WOLFSSL_SUCCESS) {
  48185. fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
  48186. wolfSSL_CertManagerFree(cm);
  48187. return ret;
  48188. }
  48189. #else
  48190. (void)ca;
  48191. #endif
  48192. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz,
  48193. WOLFSSL_FILETYPE_ASN1);
  48194. /* Let ExpectIntEQ handle return code */
  48195. wolfSSL_CertManagerFree(cm);
  48196. return ret;
  48197. }
  48198. #endif
  48199. #if !defined(NO_FILESYSTEM)
  48200. static int test_RsaSigFailure_cm(void)
  48201. {
  48202. EXPECT_DECLS;
  48203. #ifndef NO_RSA
  48204. const char* ca_cert = "./certs/ca-cert.pem";
  48205. const char* server_cert = "./certs/server-cert.der";
  48206. byte* cert_buf = NULL;
  48207. size_t cert_sz = 0;
  48208. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  48209. if (cert_buf != NULL) {
  48210. /* corrupt DER - invert last byte, which is signature */
  48211. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  48212. /* test bad cert */
  48213. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  48214. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  48215. WOLFSSL_FATAL_ERROR);
  48216. #else
  48217. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  48218. ASN_SIG_CONFIRM_E);
  48219. #endif
  48220. }
  48221. /* load_file() uses malloc. */
  48222. if (cert_buf != NULL) {
  48223. free(cert_buf);
  48224. }
  48225. #endif /* !NO_RSA */
  48226. return EXPECT_RESULT();
  48227. }
  48228. static int test_EccSigFailure_cm(void)
  48229. {
  48230. EXPECT_DECLS;
  48231. #ifdef HAVE_ECC
  48232. /* self-signed ECC cert, so use server cert as CA */
  48233. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  48234. const char* server_cert = "./certs/server-ecc.der";
  48235. byte* cert_buf = NULL;
  48236. size_t cert_sz = 0;
  48237. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  48238. if (cert_buf != NULL) {
  48239. /* corrupt DER - invert last byte, which is signature */
  48240. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  48241. /* test bad cert */
  48242. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  48243. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  48244. WOLFSSL_FATAL_ERROR);
  48245. #else
  48246. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  48247. ASN_SIG_CONFIRM_E);
  48248. #endif
  48249. }
  48250. /* load_file() uses malloc. */
  48251. if (cert_buf != NULL) {
  48252. free(cert_buf);
  48253. }
  48254. #ifdef FP_ECC
  48255. wc_ecc_fp_free();
  48256. #endif
  48257. #endif /* HAVE_ECC */
  48258. return EXPECT_RESULT();
  48259. }
  48260. #endif /* !NO_FILESYSTEM */
  48261. #endif /* NO_CERTS */
  48262. #ifdef WOLFSSL_TLS13
  48263. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  48264. #ifdef WC_SHA384_DIGEST_SIZE
  48265. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  48266. #else
  48267. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  48268. #endif
  48269. #endif
  48270. #ifdef WOLFSSL_EARLY_DATA
  48271. static const char earlyData[] = "Early Data";
  48272. static char earlyDataBuffer[1];
  48273. #endif
  48274. static int test_tls13_apis(void)
  48275. {
  48276. EXPECT_DECLS;
  48277. int ret;
  48278. #ifndef WOLFSSL_NO_TLS12
  48279. #ifndef NO_WOLFSSL_CLIENT
  48280. WOLFSSL_CTX* clientTls12Ctx = NULL;
  48281. WOLFSSL* clientTls12Ssl = NULL;
  48282. #endif
  48283. #ifndef NO_WOLFSSL_SERVER
  48284. WOLFSSL_CTX* serverTls12Ctx = NULL;
  48285. WOLFSSL* serverTls12Ssl = NULL;
  48286. #endif
  48287. #endif
  48288. #ifndef NO_WOLFSSL_CLIENT
  48289. WOLFSSL_CTX* clientCtx = NULL;
  48290. WOLFSSL* clientSsl = NULL;
  48291. #endif
  48292. #ifndef NO_WOLFSSL_SERVER
  48293. WOLFSSL_CTX* serverCtx = NULL;
  48294. WOLFSSL* serverSsl = NULL;
  48295. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  48296. const char* ourCert = svrCertFile;
  48297. const char* ourKey = svrKeyFile;
  48298. #endif
  48299. #endif
  48300. int required;
  48301. #ifdef WOLFSSL_EARLY_DATA
  48302. int outSz;
  48303. #endif
  48304. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  48305. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  48306. #ifdef HAVE_PQC
  48307. WOLFSSL_KYBER_LEVEL1
  48308. #else
  48309. WOLFSSL_ECC_SECP256R1
  48310. #endif
  48311. };
  48312. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  48313. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  48314. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  48315. int numGroups = 2;
  48316. #endif
  48317. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  48318. char groupList[] =
  48319. #ifndef NO_ECC_SECP
  48320. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  48321. "P-521:"
  48322. #endif
  48323. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  48324. "P-384:"
  48325. #endif
  48326. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  48327. "P-256"
  48328. #if defined(HAVE_PQC) && defined(HAVE_LIBOQS)
  48329. ":P256_KYBER_LEVEL1"
  48330. #endif
  48331. #endif
  48332. #endif /* !defined(NO_ECC_SECP) */
  48333. #ifdef HAVE_PQC
  48334. ":KYBER_LEVEL1"
  48335. #endif
  48336. "";
  48337. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  48338. (void)ret;
  48339. #ifndef WOLFSSL_NO_TLS12
  48340. #ifndef NO_WOLFSSL_CLIENT
  48341. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  48342. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  48343. #endif
  48344. #ifndef NO_WOLFSSL_SERVER
  48345. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  48346. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  48347. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  48348. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey,
  48349. WOLFSSL_FILETYPE_PEM);
  48350. #endif
  48351. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  48352. #endif
  48353. #endif
  48354. #ifndef NO_WOLFSSL_CLIENT
  48355. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  48356. clientSsl = wolfSSL_new(clientCtx);
  48357. #endif
  48358. #ifndef NO_WOLFSSL_SERVER
  48359. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  48360. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  48361. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  48362. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  48363. #endif
  48364. serverSsl = wolfSSL_new(serverCtx);
  48365. ExpectNotNull(serverSsl);
  48366. #endif
  48367. #ifdef WOLFSSL_SEND_HRR_COOKIE
  48368. ExpectIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  48369. #ifndef NO_WOLFSSL_CLIENT
  48370. ExpectIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  48371. #endif
  48372. #ifndef NO_WOLFSSL_SERVER
  48373. #ifndef WOLFSSL_NO_TLS12
  48374. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0),
  48375. BAD_FUNC_ARG);
  48376. #endif
  48377. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  48378. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  48379. WOLFSSL_SUCCESS);
  48380. #endif
  48381. #endif
  48382. #ifdef HAVE_SUPPORTED_CURVES
  48383. #ifdef HAVE_ECC
  48384. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  48385. BAD_FUNC_ARG);
  48386. #ifndef NO_WOLFSSL_SERVER
  48387. do {
  48388. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  48389. #ifdef WOLFSSL_ASYNC_CRYPT
  48390. if (ret == WC_PENDING_E)
  48391. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  48392. #endif
  48393. }
  48394. while (ret == WC_PENDING_E);
  48395. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  48396. #endif
  48397. #ifndef NO_WOLFSSL_CLIENT
  48398. #ifndef WOLFSSL_NO_TLS12
  48399. do {
  48400. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  48401. #ifdef WOLFSSL_ASYNC_CRYPT
  48402. if (ret == WC_PENDING_E)
  48403. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  48404. #endif
  48405. }
  48406. while (ret == WC_PENDING_E);
  48407. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  48408. #endif
  48409. do {
  48410. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  48411. #ifdef WOLFSSL_ASYNC_CRYPT
  48412. if (ret == WC_PENDING_E)
  48413. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  48414. #endif
  48415. }
  48416. while (ret == WC_PENDING_E);
  48417. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  48418. #endif
  48419. #elif defined(HAVE_CURVE25519)
  48420. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  48421. #ifndef NO_WOLFSSL_SERVER
  48422. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  48423. WOLFSSL_SUCCESS);
  48424. #endif
  48425. #ifndef NO_WOLFSSL_CLIENT
  48426. #ifndef WOLFSSL_NO_TLS12
  48427. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  48428. WOLFSSL_SUCCESS);
  48429. #endif
  48430. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  48431. WOLFSSL_SUCCESS);
  48432. #endif
  48433. #elif defined(HAVE_CURVE448)
  48434. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  48435. #ifndef NO_WOLFSSL_SERVER
  48436. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  48437. WOLFSSL_SUCCESS);
  48438. #endif
  48439. #ifndef NO_WOLFSSL_CLIENT
  48440. #ifndef WOLFSSL_NO_TLS12
  48441. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  48442. WOLFSSL_SUCCESS);
  48443. #endif
  48444. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  48445. WOLFSSL_SUCCESS);
  48446. #endif
  48447. #else
  48448. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  48449. BAD_FUNC_ARG);
  48450. #ifndef NO_WOLFSSL_CLIENT
  48451. #ifndef WOLFSSL_NO_TLS12
  48452. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  48453. NOT_COMPILED_IN);
  48454. #endif
  48455. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  48456. NOT_COMPILED_IN);
  48457. #endif
  48458. #endif
  48459. #if defined(HAVE_PQC)
  48460. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  48461. #ifndef NO_WOLFSSL_SERVER
  48462. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  48463. WOLFSSL_SUCCESS);
  48464. #endif
  48465. #ifndef NO_WOLFSSL_CLIENT
  48466. #ifndef WOLFSSL_NO_TLS12
  48467. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  48468. BAD_FUNC_ARG);
  48469. #endif
  48470. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  48471. WOLFSSL_SUCCESS);
  48472. #endif
  48473. #endif
  48474. ExpectIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  48475. #ifndef NO_WOLFSSL_SERVER
  48476. ExpectIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  48477. #endif
  48478. #ifndef NO_WOLFSSL_CLIENT
  48479. #ifndef WOLFSSL_NO_TLS12
  48480. ExpectIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  48481. #endif
  48482. ExpectIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  48483. #endif
  48484. #endif /* HAVE_SUPPORTED_CURVES */
  48485. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  48486. #ifndef NO_WOLFSSL_CLIENT
  48487. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  48488. #endif
  48489. #ifndef NO_WOLFSSL_SERVER
  48490. #ifndef WOLFSSL_NO_TLS12
  48491. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  48492. #endif
  48493. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  48494. #endif
  48495. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  48496. #ifndef NO_WOLFSSL_CLIENT
  48497. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  48498. #endif
  48499. #ifndef NO_WOLFSSL_SERVER
  48500. #ifndef WOLFSSL_NO_TLS12
  48501. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  48502. #endif
  48503. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  48504. #endif
  48505. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  48506. #ifndef NO_WOLFSSL_CLIENT
  48507. #ifndef WOLFSSL_NO_TLS12
  48508. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  48509. #endif
  48510. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  48511. #endif
  48512. #ifndef NO_WOLFSSL_SERVER
  48513. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  48514. #endif
  48515. ExpectIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  48516. #ifndef NO_WOLFSSL_CLIENT
  48517. #ifndef WOLFSSL_NO_TLS12
  48518. ExpectIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  48519. #endif
  48520. ExpectIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  48521. #endif
  48522. #ifndef NO_WOLFSSL_SERVER
  48523. ExpectIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  48524. #endif
  48525. ExpectIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  48526. #ifndef NO_WOLFSSL_CLIENT
  48527. #ifndef WOLFSSL_NO_TLS12
  48528. ExpectIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  48529. #endif
  48530. ExpectIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  48531. #endif
  48532. #ifndef NO_WOLFSSL_SERVER
  48533. ExpectIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  48534. #endif
  48535. ExpectIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  48536. ExpectIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  48537. #ifndef NO_WOLFSSL_CLIENT
  48538. #ifndef WOLFSSL_NO_TLS12
  48539. ExpectIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  48540. BAD_FUNC_ARG);
  48541. #endif
  48542. ExpectIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  48543. #endif
  48544. #ifndef NO_WOLFSSL_SERVER
  48545. ExpectIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  48546. #endif
  48547. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  48548. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  48549. #ifndef NO_WOLFSSL_SERVER
  48550. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  48551. #endif
  48552. #ifndef NO_WOLFSSL_CLIENT
  48553. #ifndef WOLFSSL_NO_TLS12
  48554. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  48555. BAD_FUNC_ARG);
  48556. #endif
  48557. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  48558. #endif
  48559. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  48560. #ifndef NO_WOLFSSL_SERVER
  48561. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  48562. #endif
  48563. #ifndef NO_WOLFSSL_CLIENT
  48564. #ifndef WOLFSSL_NO_TLS12
  48565. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  48566. BAD_FUNC_ARG);
  48567. #endif
  48568. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  48569. #endif
  48570. ExpectIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  48571. #ifndef NO_WOLFSSL_CLIENT
  48572. ExpectIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  48573. #endif
  48574. #ifndef NO_WOLFSSL_SERVER
  48575. #ifndef WOLFSSL_NO_TLS12
  48576. ExpectIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  48577. BAD_FUNC_ARG);
  48578. #endif
  48579. ExpectIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  48580. #endif
  48581. #endif
  48582. #ifdef HAVE_ECC
  48583. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  48584. ExpectIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  48585. #ifndef NO_WOLFSSL_SERVER
  48586. ExpectIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  48587. #endif
  48588. #ifndef NO_WOLFSSL_CLIENT
  48589. #ifndef WOLFSSL_NO_TLS12
  48590. ExpectIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  48591. #endif
  48592. ExpectIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  48593. #endif
  48594. #endif
  48595. #ifdef HAVE_SUPPORTED_CURVES
  48596. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  48597. #ifndef NO_WOLFSSL_CLIENT
  48598. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  48599. #endif
  48600. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  48601. #ifndef NO_WOLFSSL_CLIENT
  48602. #ifndef WOLFSSL_NO_TLS12
  48603. ExpectIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  48604. BAD_FUNC_ARG);
  48605. #endif
  48606. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  48607. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  48608. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  48609. WOLFSSL_SUCCESS);
  48610. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  48611. BAD_FUNC_ARG);
  48612. #endif
  48613. #ifndef NO_WOLFSSL_SERVER
  48614. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  48615. WOLFSSL_SUCCESS);
  48616. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  48617. BAD_FUNC_ARG);
  48618. #endif
  48619. ExpectIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  48620. #ifndef NO_WOLFSSL_CLIENT
  48621. ExpectIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  48622. #endif
  48623. ExpectIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  48624. #ifndef NO_WOLFSSL_CLIENT
  48625. #ifndef WOLFSSL_NO_TLS12
  48626. ExpectIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  48627. BAD_FUNC_ARG);
  48628. #endif
  48629. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups,
  48630. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  48631. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  48632. WOLFSSL_SUCCESS);
  48633. ExpectIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  48634. BAD_FUNC_ARG);
  48635. #endif
  48636. #ifndef NO_WOLFSSL_SERVER
  48637. ExpectIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  48638. WOLFSSL_SUCCESS);
  48639. ExpectIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  48640. BAD_FUNC_ARG);
  48641. #endif
  48642. #ifdef OPENSSL_EXTRA
  48643. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  48644. #ifndef NO_WOLFSSL_CLIENT
  48645. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL),
  48646. WOLFSSL_FAILURE);
  48647. #endif
  48648. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList),
  48649. WOLFSSL_FAILURE);
  48650. #ifndef NO_WOLFSSL_CLIENT
  48651. #ifndef WOLFSSL_NO_TLS12
  48652. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  48653. WOLFSSL_FAILURE);
  48654. #endif
  48655. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  48656. WOLFSSL_SUCCESS);
  48657. #endif
  48658. #ifndef NO_WOLFSSL_SERVER
  48659. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  48660. WOLFSSL_SUCCESS);
  48661. #endif
  48662. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  48663. #ifndef NO_WOLFSSL_CLIENT
  48664. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  48665. #endif
  48666. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  48667. #ifndef NO_WOLFSSL_CLIENT
  48668. #ifndef WOLFSSL_NO_TLS12
  48669. ExpectIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  48670. WOLFSSL_FAILURE);
  48671. #endif
  48672. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  48673. WOLFSSL_SUCCESS);
  48674. #endif
  48675. #ifndef NO_WOLFSSL_SERVER
  48676. ExpectIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  48677. WOLFSSL_SUCCESS);
  48678. #endif
  48679. #endif /* OPENSSL_EXTRA */
  48680. #endif /* HAVE_SUPPORTED_CURVES */
  48681. #endif /* HAVE_ECC */
  48682. #ifdef WOLFSSL_EARLY_DATA
  48683. #ifndef OPENSSL_EXTRA
  48684. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  48685. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  48686. #else
  48687. ExpectIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  48688. ExpectIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  48689. #endif
  48690. #ifndef NO_WOLFSSL_CLIENT
  48691. #ifndef OPENSSL_EXTRA
  48692. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  48693. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  48694. #else
  48695. ExpectIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  48696. ExpectIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  48697. #endif
  48698. #endif
  48699. #ifndef NO_WOLFSSL_SERVER
  48700. #ifndef WOLFSSL_NO_TLS12
  48701. #ifndef OPENSSL_EXTRA
  48702. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  48703. BAD_FUNC_ARG);
  48704. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  48705. #else
  48706. ExpectIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  48707. BAD_FUNC_ARG);
  48708. ExpectIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  48709. #endif
  48710. #endif
  48711. #ifndef OPENSSL_EXTRA
  48712. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  48713. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32),
  48714. WOLFSSL_SUCCESS);
  48715. #else
  48716. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  48717. #endif
  48718. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  48719. #else
  48720. ExpectIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  48721. ExpectIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  48722. #endif
  48723. #endif
  48724. #ifndef OPENSSL_EXTRA
  48725. ExpectIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  48726. ExpectIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  48727. #else
  48728. ExpectIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  48729. ExpectIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  48730. #endif
  48731. #ifndef NO_WOLFSSL_CLIENT
  48732. #ifndef OPENSSL_EXTRA
  48733. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  48734. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  48735. #else
  48736. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  48737. #endif
  48738. ExpectIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  48739. #else
  48740. ExpectIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  48741. ExpectIntEQ(SSL_get_max_early_data(clientSsl), 17);
  48742. #endif
  48743. #endif
  48744. #ifndef NO_WOLFSSL_SERVER
  48745. #ifndef WOLFSSL_NO_TLS12
  48746. #ifndef OPENSSL_EXTRA
  48747. ExpectIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  48748. ExpectIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  48749. #else
  48750. ExpectIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  48751. ExpectIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  48752. #endif
  48753. #endif
  48754. #ifndef OPENSSL_EXTRA
  48755. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  48756. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), WOLFSSL_SUCCESS);
  48757. #else
  48758. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  48759. #endif
  48760. ExpectIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  48761. #else
  48762. ExpectIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  48763. ExpectIntEQ(SSL_get_max_early_data(serverSsl), 16);
  48764. #endif
  48765. #endif
  48766. ExpectIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  48767. &outSz), BAD_FUNC_ARG);
  48768. #ifndef NO_WOLFSSL_CLIENT
  48769. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  48770. &outSz), BAD_FUNC_ARG);
  48771. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  48772. BAD_FUNC_ARG);
  48773. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  48774. sizeof(earlyData), NULL), BAD_FUNC_ARG);
  48775. #endif
  48776. #ifndef NO_WOLFSSL_SERVER
  48777. ExpectIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  48778. sizeof(earlyData), &outSz), SIDE_ERROR);
  48779. #endif
  48780. #ifndef NO_WOLFSSL_CLIENT
  48781. #ifndef WOLFSSL_NO_TLS12
  48782. ExpectIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  48783. sizeof(earlyData), &outSz), BAD_FUNC_ARG);
  48784. #endif
  48785. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  48786. sizeof(earlyData), &outSz), WOLFSSL_FATAL_ERROR);
  48787. #endif
  48788. ExpectIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  48789. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  48790. #ifndef NO_WOLFSSL_SERVER
  48791. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  48792. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  48793. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1,
  48794. &outSz), BAD_FUNC_ARG);
  48795. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  48796. sizeof(earlyDataBuffer), NULL), BAD_FUNC_ARG);
  48797. #endif
  48798. #ifndef NO_WOLFSSL_CLIENT
  48799. ExpectIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  48800. sizeof(earlyDataBuffer), &outSz), SIDE_ERROR);
  48801. #endif
  48802. #ifndef NO_WOLFSSL_SERVER
  48803. #ifndef WOLFSSL_NO_TLS12
  48804. ExpectIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  48805. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  48806. #endif
  48807. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  48808. sizeof(earlyDataBuffer), &outSz), WOLFSSL_FATAL_ERROR);
  48809. #endif
  48810. #endif
  48811. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  48812. ExpectIntLT(SSL_get_early_data_status(NULL), 0);
  48813. #endif
  48814. #ifndef NO_WOLFSSL_SERVER
  48815. wolfSSL_free(serverSsl);
  48816. wolfSSL_CTX_free(serverCtx);
  48817. #endif
  48818. #ifndef NO_WOLFSSL_CLIENT
  48819. wolfSSL_free(clientSsl);
  48820. wolfSSL_CTX_free(clientCtx);
  48821. #endif
  48822. #ifndef WOLFSSL_NO_TLS12
  48823. #ifndef NO_WOLFSSL_SERVER
  48824. wolfSSL_free(serverTls12Ssl);
  48825. wolfSSL_CTX_free(serverTls12Ctx);
  48826. #endif
  48827. #ifndef NO_WOLFSSL_CLIENT
  48828. wolfSSL_free(clientTls12Ssl);
  48829. wolfSSL_CTX_free(clientTls12Ctx);
  48830. #endif
  48831. #endif
  48832. return EXPECT_RESULT();
  48833. }
  48834. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  48835. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  48836. defined(BUILD_TLS_AES_256_GCM_SHA384)
  48837. /* Called when writing. */
  48838. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  48839. {
  48840. (void)ssl;
  48841. (void)buf;
  48842. (void)sz;
  48843. (void)ctx;
  48844. /* Force error return from wolfSSL_accept_TLSv13(). */
  48845. return WANT_WRITE;
  48846. }
  48847. /* Called when reading. */
  48848. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  48849. {
  48850. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  48851. int len = (int)msg->length;
  48852. (void)ssl;
  48853. (void)sz;
  48854. /* Pass back as much of message as will fit in buffer. */
  48855. if (len > sz)
  48856. len = sz;
  48857. XMEMCPY(buf, msg->buffer, len);
  48858. /* Move over returned data. */
  48859. msg->buffer += len;
  48860. msg->length -= len;
  48861. /* Amount actually copied. */
  48862. return len;
  48863. }
  48864. #endif
  48865. static int test_tls13_cipher_suites(void)
  48866. {
  48867. EXPECT_DECLS;
  48868. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  48869. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  48870. defined(BUILD_TLS_AES_256_GCM_SHA384)
  48871. WOLFSSL_CTX* ctx = NULL;
  48872. WOLFSSL *ssl = NULL;
  48873. int i;
  48874. byte clientHello[] = {
  48875. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  48876. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  48877. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  48878. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  48879. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  48880. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  48881. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  48882. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  48883. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48884. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  48885. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  48886. 0x13, 0x01,
  48887. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  48888. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  48889. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  48890. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  48891. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  48892. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  48893. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  48894. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  48895. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  48896. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  48897. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  48898. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  48899. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  48900. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  48901. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  48902. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  48903. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  48904. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  48905. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  48906. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  48907. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  48908. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  48909. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  48910. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  48911. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  48912. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  48913. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  48914. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  48915. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  48916. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  48917. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  48918. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  48919. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  48920. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  48921. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  48922. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48923. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48924. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  48925. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  48926. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  48927. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  48928. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  48929. };
  48930. WOLFSSL_BUFFER_INFO msg;
  48931. /* Offset into ClientHello message data of first cipher suite. */
  48932. const int csOff = 78;
  48933. /* Server cipher list. */
  48934. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  48935. /* Suite list with duplicates. */
  48936. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  48937. "TLS13-AES128-GCM-SHA256:"
  48938. "TLS13-AES256-GCM-SHA384:"
  48939. "TLS13-AES256-GCM-SHA384:"
  48940. "TLS13-AES128-GCM-SHA256";
  48941. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  48942. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  48943. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  48944. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  48945. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  48946. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  48947. #endif
  48948. /* Set up wolfSSL context. */
  48949. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  48950. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  48951. WOLFSSL_FILETYPE_PEM));
  48952. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  48953. WOLFSSL_FILETYPE_PEM));
  48954. /* Read from 'msg'. */
  48955. wolfSSL_SetIORecv(ctx, CsRecv);
  48956. /* No where to send to - dummy sender. */
  48957. wolfSSL_SetIOSend(ctx, CsSend);
  48958. /* Test cipher suite list with many copies of a cipher suite. */
  48959. ExpectNotNull(ssl = wolfSSL_new(ctx));
  48960. msg.buffer = clientHello;
  48961. msg.length = (unsigned int)sizeof(clientHello);
  48962. wolfSSL_SetIOReadCtx(ssl, &msg);
  48963. /* Force server to have as many occurrences of same cipher suite as
  48964. * possible. */
  48965. if (ssl != NULL) {
  48966. Suites* suites = (Suites*)WOLFSSL_SUITES(ssl);
  48967. suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  48968. for (i = 0; i < suites->suiteSz; i += 2) {
  48969. suites->suites[i + 0] = TLS13_BYTE;
  48970. suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  48971. }
  48972. }
  48973. /* Test multiple occurrences of same cipher suite. */
  48974. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  48975. wolfSSL_free(ssl);
  48976. ssl = NULL;
  48977. /* Set client order opposite to server order:
  48978. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  48979. clientHello[csOff + 0] = TLS13_BYTE;
  48980. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  48981. clientHello[csOff + 2] = TLS13_BYTE;
  48982. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  48983. /* Test server order negotiation. */
  48984. ExpectNotNull(ssl = wolfSSL_new(ctx));
  48985. msg.buffer = clientHello;
  48986. msg.length = (unsigned int)sizeof(clientHello);
  48987. wolfSSL_SetIOReadCtx(ssl, &msg);
  48988. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  48989. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  48990. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  48991. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  48992. /* Check refined order - server order. */
  48993. ExpectIntEQ(ssl->suites->suiteSz, 4);
  48994. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  48995. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  48996. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  48997. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  48998. wolfSSL_free(ssl);
  48999. ssl = NULL;
  49000. /* Test client order negotiation. */
  49001. ExpectNotNull(ssl = wolfSSL_new(ctx));
  49002. msg.buffer = clientHello;
  49003. msg.length = (unsigned int)sizeof(clientHello);
  49004. wolfSSL_SetIOReadCtx(ssl, &msg);
  49005. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  49006. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  49007. ExpectIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  49008. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  49009. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  49010. /* Check refined order - client order. */
  49011. ExpectIntEQ(ssl->suites->suiteSz, 4);
  49012. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  49013. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  49014. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  49015. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  49016. wolfSSL_free(ssl);
  49017. ssl = NULL;
  49018. /* Check duplicate detection is working. */
  49019. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  49020. ExpectIntEQ(ctx->suites->suiteSz, 4);
  49021. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  49022. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  49023. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  49024. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  49025. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  49026. ExpectIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  49027. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  49028. ExpectIntEQ(ctx->suites->suiteSz, 4);
  49029. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  49030. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  49031. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  49032. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  49033. #endif
  49034. wolfSSL_CTX_free(ctx);
  49035. #endif
  49036. return EXPECT_RESULT();
  49037. }
  49038. #endif
  49039. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  49040. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  49041. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  49042. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  49043. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  49044. const unsigned char* priv, unsigned int privSz,
  49045. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  49046. unsigned char* out, unsigned int* outlen,
  49047. void* ctx)
  49048. {
  49049. int result;
  49050. /* Test fail when context associated with WOLFSSL is NULL */
  49051. if (ctx == NULL) {
  49052. return -1;
  49053. }
  49054. (void)ssl;
  49055. /* return 0 on success */
  49056. PRIVATE_KEY_UNLOCK();
  49057. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  49058. PRIVATE_KEY_LOCK();
  49059. return result;
  49060. }
  49061. static int test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  49062. EXPECT_DECLS;
  49063. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  49064. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  49065. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  49066. WOLFSSL_SUCCESS);
  49067. #endif
  49068. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  49069. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  49070. WOLFSSL_SUCCESS);
  49071. #endif
  49072. return EXPECT_RESULT();
  49073. }
  49074. static int test_dh_ssl_setup(WOLFSSL* ssl)
  49075. {
  49076. EXPECT_DECLS;
  49077. static int dh_test_ctx = 1;
  49078. int ret;
  49079. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  49080. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  49081. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  49082. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  49083. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  49084. }
  49085. return EXPECT_RESULT();
  49086. }
  49087. static int test_dh_ssl_setup_fail(WOLFSSL* ssl)
  49088. {
  49089. EXPECT_DECLS;
  49090. int ret;
  49091. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  49092. ExpectNull(wolfSSL_GetDhAgreeCtx(ssl));
  49093. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  49094. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  49095. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  49096. }
  49097. return EXPECT_RESULT();
  49098. }
  49099. #endif
  49100. static int test_DhCallbacks(void)
  49101. {
  49102. EXPECT_DECLS;
  49103. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  49104. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  49105. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  49106. WOLFSSL_CTX *ctx = NULL;
  49107. WOLFSSL *ssl = NULL;
  49108. int test;
  49109. test_ssl_cbf func_cb_client;
  49110. test_ssl_cbf func_cb_server;
  49111. /* Test that DH callback APIs work. */
  49112. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  49113. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  49114. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  49115. /* load client ca cert */
  49116. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  49117. WOLFSSL_SUCCESS);
  49118. /* test with NULL arguments */
  49119. wolfSSL_SetDhAgreeCtx(NULL, &test);
  49120. ExpectNull(wolfSSL_GetDhAgreeCtx(NULL));
  49121. /* test success case */
  49122. test = 1;
  49123. ExpectNotNull(ssl = wolfSSL_new(ctx));
  49124. wolfSSL_SetDhAgreeCtx(ssl, &test);
  49125. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  49126. wolfSSL_free(ssl);
  49127. wolfSSL_CTX_free(ctx);
  49128. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  49129. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  49130. /* set callbacks to use DH functions */
  49131. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  49132. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  49133. func_cb_client.method = wolfTLSv1_2_client_method;
  49134. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  49135. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  49136. func_cb_server.method = wolfTLSv1_2_server_method;
  49137. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  49138. &func_cb_server, NULL), TEST_SUCCESS);
  49139. /* Test fail */
  49140. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  49141. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  49142. /* set callbacks to use DH functions */
  49143. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  49144. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  49145. func_cb_client.method = wolfTLSv1_2_client_method;
  49146. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  49147. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  49148. func_cb_server.method = wolfTLSv1_2_server_method;
  49149. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  49150. &func_cb_server, NULL), TEST_FAIL);
  49151. #endif
  49152. return EXPECT_RESULT();
  49153. }
  49154. #endif /* HAVE_PK_CALLBACKS */
  49155. #ifdef HAVE_HASHDRBG
  49156. #ifdef TEST_RESEED_INTERVAL
  49157. static int test_wc_RNG_GenerateBlock_Reseed(void)
  49158. {
  49159. EXPECT_DECLS;
  49160. int i;
  49161. WC_RNG rng;
  49162. byte key[32];
  49163. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49164. ExpectIntEQ(wc_InitRng(&rng), 0);
  49165. for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  49166. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  49167. }
  49168. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  49169. return EXPECT_RESULT();
  49170. }
  49171. #endif /* TEST_RESEED_INTERVAL */
  49172. static int test_wc_RNG_GenerateBlock(void)
  49173. {
  49174. EXPECT_DECLS;
  49175. int i;
  49176. WC_RNG rng;
  49177. byte key[32];
  49178. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49179. ExpectIntEQ(wc_InitRng(&rng), 0);
  49180. for (i = 0; i < 10; i++) {
  49181. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  49182. }
  49183. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  49184. return EXPECT_RESULT();
  49185. }
  49186. #endif /* HAVE_HASHDRBG */
  49187. /*
  49188. * Testing get_rand_digit
  49189. */
  49190. static int test_get_rand_digit(void)
  49191. {
  49192. EXPECT_DECLS;
  49193. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  49194. WC_RNG rng;
  49195. mp_digit d;
  49196. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49197. ExpectIntEQ(wc_InitRng(&rng), 0);
  49198. ExpectIntEQ(get_rand_digit(&rng, &d), 0);
  49199. ExpectIntEQ(get_rand_digit(NULL, NULL), BAD_FUNC_ARG);
  49200. ExpectIntEQ(get_rand_digit(NULL, &d), BAD_FUNC_ARG);
  49201. ExpectIntEQ(get_rand_digit(&rng, NULL), BAD_FUNC_ARG);
  49202. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  49203. #endif
  49204. return EXPECT_RESULT();
  49205. } /* End test_get_rand_digit*/
  49206. /*
  49207. * Testing get_digit_count
  49208. */
  49209. static int test_get_digit_count(void)
  49210. {
  49211. EXPECT_DECLS;
  49212. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  49213. mp_int a;
  49214. XMEMSET(&a, 0, sizeof(mp_int));
  49215. ExpectIntEQ(mp_init(&a), 0);
  49216. ExpectIntEQ(get_digit_count(NULL), 0);
  49217. ExpectIntEQ(get_digit_count(&a), 0);
  49218. mp_clear(&a);
  49219. #endif
  49220. return EXPECT_RESULT();
  49221. } /* End test_get_digit_count*/
  49222. /*
  49223. * Testing mp_cond_copy
  49224. */
  49225. static int test_mp_cond_copy(void)
  49226. {
  49227. EXPECT_DECLS;
  49228. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  49229. defined(WOLFSSL_PUBLIC_MP)
  49230. mp_int a;
  49231. mp_int b;
  49232. int copy = 0;
  49233. XMEMSET(&a, 0, sizeof(mp_int));
  49234. XMEMSET(&b, 0, sizeof(mp_int));
  49235. ExpectIntEQ(mp_init(&a), MP_OKAY);
  49236. ExpectIntEQ(mp_init(&b), MP_OKAY);
  49237. ExpectIntEQ(mp_cond_copy(NULL, copy, NULL), BAD_FUNC_ARG);
  49238. ExpectIntEQ(mp_cond_copy(NULL, copy, &b), BAD_FUNC_ARG);
  49239. ExpectIntEQ(mp_cond_copy(&a, copy, NULL), BAD_FUNC_ARG);
  49240. ExpectIntEQ(mp_cond_copy(&a, copy, &b), 0);
  49241. mp_clear(&a);
  49242. mp_clear(&b);
  49243. #endif
  49244. return EXPECT_RESULT();
  49245. } /* End test_mp_cond_copy*/
  49246. /*
  49247. * Testing mp_rand
  49248. */
  49249. static int test_mp_rand(void)
  49250. {
  49251. EXPECT_DECLS;
  49252. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  49253. mp_int a;
  49254. WC_RNG rng;
  49255. int digits = 1;
  49256. XMEMSET(&a, 0, sizeof(mp_int));
  49257. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49258. ExpectIntEQ(mp_init(&a), MP_OKAY);
  49259. ExpectIntEQ(wc_InitRng(&rng), 0);
  49260. ExpectIntEQ(mp_rand(&a, digits, NULL), MISSING_RNG_E);
  49261. ExpectIntEQ(mp_rand(NULL, digits, &rng), BAD_FUNC_ARG);
  49262. ExpectIntEQ(mp_rand(&a, 0, &rng), BAD_FUNC_ARG);
  49263. ExpectIntEQ(mp_rand(&a, digits, &rng), 0);
  49264. mp_clear(&a);
  49265. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  49266. #endif
  49267. return EXPECT_RESULT();
  49268. } /* End test_mp_rand*/
  49269. /*
  49270. * Testing get_digit
  49271. */
  49272. static int test_get_digit(void)
  49273. {
  49274. EXPECT_DECLS;
  49275. #if defined(WOLFSSL_PUBLIC_MP)
  49276. mp_int a;
  49277. int n = 0;
  49278. XMEMSET(&a, 0, sizeof(mp_int));
  49279. ExpectIntEQ(mp_init(&a), MP_OKAY);
  49280. ExpectIntEQ(get_digit(NULL, n), 0);
  49281. ExpectIntEQ(get_digit(&a, n), 0);
  49282. mp_clear(&a);
  49283. #endif
  49284. return EXPECT_RESULT();
  49285. } /* End test_get_digit*/
  49286. /*
  49287. * Testing wc_export_int
  49288. */
  49289. static int test_wc_export_int(void)
  49290. {
  49291. EXPECT_DECLS;
  49292. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  49293. defined(WOLFSSL_PUBLIC_MP)
  49294. mp_int mp;
  49295. byte buf[32];
  49296. word32 keySz = (word32)sizeof(buf);
  49297. word32 len = (word32)sizeof(buf);
  49298. XMEMSET(&mp, 0, sizeof(mp_int));
  49299. ExpectIntEQ(mp_init(&mp), MP_OKAY);
  49300. ExpectIntEQ(mp_set(&mp, 1234), 0);
  49301. ExpectIntEQ(wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  49302. BAD_FUNC_ARG);
  49303. len = sizeof(buf)-1;
  49304. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  49305. BUFFER_E);
  49306. len = sizeof(buf);
  49307. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN), 0);
  49308. len = 4; /* test input too small */
  49309. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), BUFFER_E);
  49310. len = sizeof(buf);
  49311. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), 0);
  49312. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  49313. ExpectIntEQ(len, 5);
  49314. mp_clear(&mp);
  49315. #endif
  49316. return EXPECT_RESULT();
  49317. } /* End test_wc_export_int*/
  49318. static int test_wc_InitRngNonce(void)
  49319. {
  49320. EXPECT_DECLS;
  49321. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  49322. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  49323. HAVE_FIPS_VERSION >= 2))
  49324. WC_RNG rng;
  49325. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  49326. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  49327. word32 nonceSz = sizeof(nonce);
  49328. ExpectIntEQ(wc_InitRngNonce(&rng, nonce, nonceSz), 0);
  49329. ExpectIntEQ(wc_FreeRng(&rng), 0);
  49330. #endif
  49331. return EXPECT_RESULT();
  49332. } /* End test_wc_InitRngNonce*/
  49333. /*
  49334. * Testing wc_InitRngNonce_ex
  49335. */
  49336. static int test_wc_InitRngNonce_ex(void)
  49337. {
  49338. EXPECT_DECLS;
  49339. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  49340. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  49341. HAVE_FIPS_VERSION >= 2))
  49342. WC_RNG rng;
  49343. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  49344. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  49345. word32 nonceSz = sizeof(nonce);
  49346. ExpectIntEQ(wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId),
  49347. 0);
  49348. ExpectIntEQ(wc_FreeRng(&rng), 0);
  49349. #endif
  49350. return EXPECT_RESULT();
  49351. } /* End test_wc_InitRngNonce_ex */
  49352. static int test_wolfSSL_X509_CRL(void)
  49353. {
  49354. EXPECT_DECLS;
  49355. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  49356. X509_CRL *crl = NULL;
  49357. char pem[][100] = {
  49358. "./certs/crl/crl.pem",
  49359. "./certs/crl/crl2.pem",
  49360. "./certs/crl/caEccCrl.pem",
  49361. "./certs/crl/eccCliCRL.pem",
  49362. "./certs/crl/eccSrvCRL.pem",
  49363. ""
  49364. };
  49365. #ifndef NO_BIO
  49366. BIO *bio = NULL;
  49367. #endif
  49368. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  49369. char der[][100] = {
  49370. "./certs/crl/crl.der",
  49371. "./certs/crl/crl2.der",
  49372. ""};
  49373. #endif
  49374. XFILE fp = XBADFILE;
  49375. int i;
  49376. for (i = 0; pem[i][0] != '\0'; i++)
  49377. {
  49378. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  49379. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  49380. NULL, NULL));
  49381. ExpectNotNull(crl);
  49382. X509_CRL_free(crl);
  49383. if (fp != XBADFILE) {
  49384. XFCLOSE(fp);
  49385. fp = XBADFILE;
  49386. }
  49387. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  49388. ExpectNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL,
  49389. NULL));
  49390. if (EXPECT_FAIL()) {
  49391. crl = NULL;
  49392. }
  49393. ExpectNotNull(crl);
  49394. X509_CRL_free(crl);
  49395. crl = NULL;
  49396. if (fp != XBADFILE) {
  49397. XFCLOSE(fp);
  49398. fp = XBADFILE;
  49399. }
  49400. }
  49401. #ifndef NO_BIO
  49402. for (i = 0; pem[i][0] != '\0'; i++)
  49403. {
  49404. ExpectNotNull(bio = BIO_new_file(pem[i], "rb"));
  49405. ExpectNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  49406. X509_CRL_free(crl);
  49407. crl = NULL;
  49408. BIO_free(bio);
  49409. bio = NULL;
  49410. }
  49411. #endif
  49412. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  49413. for (i = 0; der[i][0] != '\0'; i++) {
  49414. ExpectTrue((fp = XFOPEN(der[i], "rb")) != XBADFILE);
  49415. ExpectTrue((fp != XBADFILE));
  49416. ExpectNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  49417. ExpectNotNull(crl);
  49418. X509_CRL_free(crl);
  49419. if (fp != XBADFILE) {
  49420. XFCLOSE(fp);
  49421. fp = XBADFILE;
  49422. }
  49423. fp = XFOPEN(der[i], "rb");
  49424. ExpectTrue((fp != XBADFILE));
  49425. ExpectNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  49426. if (EXPECT_FAIL()) {
  49427. crl = NULL;
  49428. }
  49429. ExpectNotNull(crl);
  49430. X509_CRL_free(crl);
  49431. crl = NULL;
  49432. if (fp != XBADFILE) {
  49433. XFCLOSE(fp);
  49434. fp = XBADFILE;
  49435. }
  49436. }
  49437. #endif
  49438. #endif
  49439. return EXPECT_RESULT();
  49440. }
  49441. static int test_wolfSSL_X509_load_crl_file(void)
  49442. {
  49443. EXPECT_DECLS;
  49444. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  49445. !defined(NO_STDIO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  49446. int i;
  49447. char pem[][100] = {
  49448. "./certs/crl/crl.pem",
  49449. "./certs/crl/crl2.pem",
  49450. "./certs/crl/caEccCrl.pem",
  49451. "./certs/crl/eccCliCRL.pem",
  49452. "./certs/crl/eccSrvCRL.pem",
  49453. #ifdef WC_RSA_PSS
  49454. "./certs/crl/crl_rsapss.pem",
  49455. #endif
  49456. ""
  49457. };
  49458. char der[][100] = {
  49459. "./certs/crl/crl.der",
  49460. "./certs/crl/crl2.der",
  49461. ""
  49462. };
  49463. WOLFSSL_X509_STORE* store = NULL;
  49464. WOLFSSL_X509_LOOKUP* lookup = NULL;
  49465. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  49466. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  49467. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  49468. X509_FILETYPE_PEM), 1);
  49469. #ifdef WC_RSA_PSS
  49470. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/rsapss/ca-rsapss.pem",
  49471. X509_FILETYPE_PEM), 1);
  49472. #endif
  49473. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  49474. X509_FILETYPE_PEM), 1);
  49475. if (store) {
  49476. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  49477. WOLFSSL_FILETYPE_PEM), 1);
  49478. /* since store hasn't yet known the revoked cert*/
  49479. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  49480. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  49481. }
  49482. for (i = 0; pem[i][0] != '\0'; i++) {
  49483. ExpectIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM),
  49484. 1);
  49485. }
  49486. if (store) {
  49487. /* since store knows crl list */
  49488. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  49489. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  49490. CRL_CERT_REVOKED);
  49491. #ifdef WC_RSA_PSS
  49492. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  49493. "certs/rsapss/server-rsapss-cert.pem", WOLFSSL_FILETYPE_PEM),
  49494. CRL_CERT_REVOKED);
  49495. #endif
  49496. }
  49497. /* once feeing store */
  49498. X509_STORE_free(store);
  49499. store = NULL;
  49500. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  49501. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  49502. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  49503. X509_FILETYPE_PEM), 1);
  49504. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  49505. X509_FILETYPE_PEM), 1);
  49506. if (store) {
  49507. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  49508. WOLFSSL_FILETYPE_PEM), 1);
  49509. /* since store hasn't yet known the revoked cert*/
  49510. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  49511. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  49512. }
  49513. for (i = 0; der[i][0] != '\0'; i++) {
  49514. ExpectIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1),
  49515. 1);
  49516. }
  49517. if (store) {
  49518. /* since store knows crl list */
  49519. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  49520. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  49521. CRL_CERT_REVOKED);
  49522. }
  49523. /* test for incorrect parameter */
  49524. ExpectIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  49525. ExpectIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  49526. ExpectIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  49527. X509_STORE_free(store);
  49528. store = NULL;
  49529. #endif
  49530. return EXPECT_RESULT();
  49531. }
  49532. static int test_wolfSSL_i2d_X509(void)
  49533. {
  49534. EXPECT_DECLS;
  49535. #if defined(OPENSSL_EXTRA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  49536. const unsigned char* cert_buf = server_cert_der_2048;
  49537. unsigned char* out = NULL;
  49538. unsigned char* tmp = NULL;
  49539. X509* cert = NULL;
  49540. ExpectNotNull(d2i_X509(&cert, &cert_buf, sizeof_server_cert_der_2048));
  49541. /* Pointer should be advanced */
  49542. ExpectPtrGT(cert_buf, server_cert_der_2048);
  49543. ExpectIntGT(i2d_X509(cert, &out), 0);
  49544. ExpectNotNull(out);
  49545. tmp = out;
  49546. ExpectIntGT(i2d_X509(cert, &tmp), 0);
  49547. ExpectPtrGT(tmp, out);
  49548. if (out != NULL)
  49549. XFREE(out, NULL, DYNAMIC_TYPE_OPENSSL);
  49550. X509_free(cert);
  49551. #endif
  49552. return EXPECT_RESULT();
  49553. }
  49554. static int test_wolfSSL_d2i_X509_REQ(void)
  49555. {
  49556. EXPECT_DECLS;
  49557. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  49558. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  49559. !defined(WOLFSSL_SP_MATH)
  49560. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der
  49561. * were generated by libest
  49562. * ./certs/csr.attr.der contains sample attributes
  49563. * ./certs/csr.ext.der contains sample extensions */
  49564. const char* csrFile = "./certs/csr.signed.der";
  49565. const char* csrPopFile = "./certs/csr.attr.der";
  49566. const char* csrExtFile = "./certs/csr.ext.der";
  49567. /* ./certs/csr.dsa.pem is generated using
  49568. * openssl req -newkey dsa:certs/dsaparams.pem \
  49569. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  49570. * -outform PEM
  49571. * with the passphrase "wolfSSL"
  49572. */
  49573. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  49574. const char* csrDsaFile = "./certs/csr.dsa.pem";
  49575. XFILE f = XBADFILE;
  49576. #endif
  49577. BIO* bio = NULL;
  49578. X509* req = NULL;
  49579. EVP_PKEY *pub_key = NULL;
  49580. {
  49581. ExpectNotNull(bio = BIO_new_file(csrFile, "rb"));
  49582. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  49583. /*
  49584. * Extract the public key from the CSR
  49585. */
  49586. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  49587. /*
  49588. * Verify the signature in the CSR
  49589. */
  49590. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  49591. X509_free(req);
  49592. req = NULL;
  49593. BIO_free(bio);
  49594. bio = NULL;
  49595. EVP_PKEY_free(pub_key);
  49596. pub_key = NULL;
  49597. }
  49598. {
  49599. #ifdef OPENSSL_ALL
  49600. X509_ATTRIBUTE* attr = NULL;
  49601. ASN1_TYPE *at = NULL;
  49602. #endif
  49603. ExpectNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  49604. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  49605. /*
  49606. * Extract the public key from the CSR
  49607. */
  49608. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  49609. /*
  49610. * Verify the signature in the CSR
  49611. */
  49612. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  49613. #ifdef OPENSSL_ALL
  49614. /*
  49615. * Obtain the challenge password from the CSR
  49616. */
  49617. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  49618. -1), 1);
  49619. ExpectNotNull(attr = X509_REQ_get_attr(req, 1));
  49620. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  49621. ExpectNotNull(at->value.asn1_string);
  49622. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string),
  49623. "2xIE+qqp/rhyTXP+");
  49624. ExpectIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  49625. #endif
  49626. X509_free(req);
  49627. req = NULL;
  49628. BIO_free(bio);
  49629. bio = NULL;
  49630. EVP_PKEY_free(pub_key);
  49631. pub_key = NULL;
  49632. }
  49633. {
  49634. #ifdef OPENSSL_ALL
  49635. X509_ATTRIBUTE* attr = NULL;
  49636. ASN1_TYPE *at = NULL;
  49637. STACK_OF(X509_EXTENSION) *exts = NULL;
  49638. #endif
  49639. ExpectNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  49640. /* This CSR contains an Extension Request attribute so
  49641. * we test extension parsing in a CSR attribute here. */
  49642. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  49643. /*
  49644. * Extract the public key from the CSR
  49645. */
  49646. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  49647. /*
  49648. * Verify the signature in the CSR
  49649. */
  49650. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  49651. #ifdef OPENSSL_ALL
  49652. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  49653. req));
  49654. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  49655. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  49656. /*
  49657. * Obtain the challenge password from the CSR
  49658. */
  49659. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  49660. -1), 0);
  49661. ExpectNotNull(attr = X509_REQ_get_attr(req, 0));
  49662. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  49663. ExpectNotNull(at->value.asn1_string);
  49664. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  49665. ExpectIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  49666. ExpectIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  49667. #endif
  49668. X509_free(req);
  49669. req = NULL;
  49670. BIO_free(bio);
  49671. bio = NULL;
  49672. EVP_PKEY_free(pub_key);
  49673. pub_key = NULL;
  49674. }
  49675. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  49676. {
  49677. ExpectNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  49678. ExpectNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  49679. /*
  49680. * Extract the public key from the CSR
  49681. */
  49682. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  49683. /*
  49684. * Verify the signature in the CSR
  49685. */
  49686. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  49687. X509_free(req);
  49688. req = NULL;
  49689. BIO_free(bio);
  49690. /* Run the same test, but with a file pointer instead of a BIO.
  49691. * (PEM_read_X509_REQ)*/
  49692. ExpectTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  49693. ExpectNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  49694. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  49695. X509_free(req);
  49696. EVP_PKEY_free(pub_key);
  49697. }
  49698. #endif /* !NO_DSA && !HAVE_SELFTEST */
  49699. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  49700. return EXPECT_RESULT();
  49701. }
  49702. static int test_wolfSSL_PEM_read_X509(void)
  49703. {
  49704. EXPECT_DECLS;
  49705. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  49706. !defined(NO_RSA)
  49707. X509 *x509 = NULL;
  49708. XFILE fp = XBADFILE;
  49709. ExpectTrue((fp = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  49710. ExpectNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  49711. X509_free(x509);
  49712. if (fp != XBADFILE)
  49713. XFCLOSE(fp);
  49714. #endif
  49715. return EXPECT_RESULT();
  49716. }
  49717. static int test_wolfSSL_PEM_read(void)
  49718. {
  49719. EXPECT_DECLS;
  49720. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  49721. const char* filename = "./certs/server-keyEnc.pem";
  49722. XFILE fp = XBADFILE;
  49723. char* name = NULL;
  49724. char* header = NULL;
  49725. byte* data = NULL;
  49726. long len;
  49727. EVP_CIPHER_INFO cipher;
  49728. WOLFSSL_BIO* bio = NULL;
  49729. byte* fileData = NULL;
  49730. size_t fileDataSz = 0;
  49731. byte* out;
  49732. ExpectNotNull(bio = BIO_new_file(filename, "rb"));
  49733. ExpectIntEQ(PEM_read_bio(bio, NULL, &header, &data, &len), 0);
  49734. ExpectIntEQ(PEM_read_bio(bio, &name, NULL, &data, &len), 0);
  49735. ExpectIntEQ(PEM_read_bio(bio, &name, &header, NULL, &len), 0);
  49736. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, NULL), 0);
  49737. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, &len), 1);
  49738. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  49739. ExpectIntGT(XSTRLEN(header), 0);
  49740. ExpectIntGT(len, 0);
  49741. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49742. name = NULL;
  49743. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49744. header = NULL;
  49745. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49746. data = NULL;
  49747. BIO_free(bio);
  49748. bio = NULL;
  49749. ExpectTrue((fp = XFOPEN(filename, "rb")) != XBADFILE);
  49750. /* Fail cases. */
  49751. ExpectIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  49752. ExpectIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  49753. ExpectIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  49754. ExpectIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  49755. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  49756. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  49757. ExpectIntGT(XSTRLEN(header), 0);
  49758. ExpectIntGT(len, 0);
  49759. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  49760. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  49761. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  49762. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  49763. DYNAMIC_TYPE_TMP_BUFFER));
  49764. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  49765. if (fp != XBADFILE) {
  49766. XFCLOSE(fp);
  49767. fp = XBADFILE;
  49768. }
  49769. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  49770. /* Fail cases. */
  49771. ExpectIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  49772. ExpectIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  49773. ExpectIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  49774. ExpectIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  49775. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  49776. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  49777. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  49778. /* Fail cases. */
  49779. ExpectIntEQ(PEM_write(XBADFILE, name, header, data, len), 0);
  49780. ExpectIntEQ(PEM_write(stderr, NULL, header, data, len), 0);
  49781. ExpectIntEQ(PEM_write(stderr, name, NULL, data, len), 0);
  49782. ExpectIntEQ(PEM_write(stderr, name, header, NULL, len), 0);
  49783. /* Pass case */
  49784. ExpectIntEQ(PEM_write(stderr, name, header, data, len), fileDataSz);
  49785. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49786. name = NULL;
  49787. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49788. header = NULL;
  49789. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49790. data = NULL;
  49791. /* Read out of a fixed buffer BIO - forces malloc in PEM_read_bio. */
  49792. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, &len), 1);
  49793. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  49794. ExpectIntGT(XSTRLEN(header), 0);
  49795. ExpectIntGT(len, 0);
  49796. /* Fail cases. */
  49797. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  49798. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  49799. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  49800. #ifndef NO_DES3
  49801. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  49802. #endif
  49803. /* Fail cases. */
  49804. ExpectIntEQ(PEM_do_header(NULL, data, &len, PasswordCallBack,
  49805. (void*)"yassl123"), WOLFSSL_FAILURE);
  49806. ExpectIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  49807. (void*)"yassl123"), WOLFSSL_FAILURE);
  49808. ExpectIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  49809. (void*)"yassl123"), WOLFSSL_FAILURE);
  49810. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  49811. (void*)"yassl123"), WOLFSSL_FAILURE);
  49812. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NoPasswordCallBack,
  49813. (void*)"yassl123"), WOLFSSL_FAILURE);
  49814. #if !defined(NO_DES3) && !defined(NO_MD5)
  49815. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  49816. (void*)"yassl123"), WOLFSSL_SUCCESS);
  49817. #else
  49818. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  49819. (void*)"yassl123"), WOLFSSL_FAILURE);
  49820. #endif
  49821. BIO_free(bio);
  49822. bio = NULL;
  49823. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49824. fileData = NULL;
  49825. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49826. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49827. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49828. name = NULL;
  49829. header = NULL;
  49830. data = NULL;
  49831. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  49832. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  49833. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  49834. ExpectIntEQ(XSTRLEN(header), 0);
  49835. ExpectIntGT(len, 0);
  49836. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  49837. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  49838. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  49839. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  49840. DYNAMIC_TYPE_TMP_BUFFER));
  49841. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  49842. if (fp != XBADFILE)
  49843. XFCLOSE(fp);
  49844. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  49845. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  49846. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  49847. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  49848. BIO_free(bio);
  49849. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49850. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49851. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49852. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49853. #endif
  49854. return EXPECT_RESULT();
  49855. }
  49856. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  49857. {
  49858. EXPECT_DECLS;
  49859. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  49860. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49861. const byte iv[12] = { 0 };
  49862. const byte key[16] = { 0 };
  49863. const byte cleartext[16] = { 0 };
  49864. const byte aad[] = {
  49865. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  49866. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  49867. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  49868. 0x4f
  49869. };
  49870. byte out1Part[16];
  49871. byte outTag1Part[16];
  49872. byte out2Part[16];
  49873. byte outTag2Part[16];
  49874. byte decryptBuf[16];
  49875. int len = 0;
  49876. int tlen;
  49877. EVP_CIPHER_CTX* ctx = NULL;
  49878. /* ENCRYPT */
  49879. /* Send AAD and data in 1 part */
  49880. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  49881. tlen = 0;
  49882. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49883. 1);
  49884. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49885. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  49886. ExpectIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  49887. sizeof(cleartext)), 1);
  49888. tlen += len;
  49889. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  49890. tlen += len;
  49891. ExpectIntEQ(tlen, sizeof(cleartext));
  49892. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  49893. outTag1Part), 1);
  49894. EVP_CIPHER_CTX_free(ctx);
  49895. ctx = NULL;
  49896. /* DECRYPT */
  49897. /* Send AAD and data in 1 part */
  49898. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  49899. tlen = 0;
  49900. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49901. 1);
  49902. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49903. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  49904. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  49905. sizeof(cleartext)), 1);
  49906. tlen += len;
  49907. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  49908. outTag1Part), 1);
  49909. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  49910. tlen += len;
  49911. ExpectIntEQ(tlen, sizeof(cleartext));
  49912. EVP_CIPHER_CTX_free(ctx);
  49913. ctx = NULL;
  49914. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  49915. /* ENCRYPT */
  49916. /* Send AAD and data in 2 parts */
  49917. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  49918. tlen = 0;
  49919. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49920. 1);
  49921. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49922. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  49923. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  49924. 1);
  49925. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  49926. tlen += len;
  49927. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  49928. sizeof(cleartext) - 1), 1);
  49929. tlen += len;
  49930. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  49931. tlen += len;
  49932. ExpectIntEQ(tlen, sizeof(cleartext));
  49933. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  49934. outTag2Part), 1);
  49935. ExpectIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  49936. ExpectIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  49937. EVP_CIPHER_CTX_free(ctx);
  49938. ctx = NULL;
  49939. /* DECRYPT */
  49940. /* Send AAD and data in 2 parts */
  49941. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  49942. tlen = 0;
  49943. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49944. 1);
  49945. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49946. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  49947. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  49948. 1);
  49949. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  49950. tlen += len;
  49951. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  49952. sizeof(cleartext) - 1), 1);
  49953. tlen += len;
  49954. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  49955. outTag1Part), 1);
  49956. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  49957. tlen += len;
  49958. ExpectIntEQ(tlen, sizeof(cleartext));
  49959. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  49960. /* Test AAD reuse */
  49961. EVP_CIPHER_CTX_free(ctx);
  49962. #endif
  49963. return EXPECT_RESULT();
  49964. }
  49965. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  49966. {
  49967. EXPECT_DECLS;
  49968. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  49969. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49970. /* Zero length plain text */
  49971. byte key[] = {
  49972. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49973. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49974. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49975. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49976. }; /* align */
  49977. byte iv[] = {
  49978. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49979. }; /* align */
  49980. byte plaintxt[1];
  49981. int ivSz = 12;
  49982. int plaintxtSz = 0;
  49983. unsigned char tag[16];
  49984. unsigned char tag_kat[] = {
  49985. 0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  49986. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b
  49987. };
  49988. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  49989. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  49990. int ciphertxtSz = 0;
  49991. int decryptedtxtSz = 0;
  49992. int len = 0;
  49993. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  49994. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  49995. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  49996. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49997. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  49998. plaintxtSz));
  49999. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  50000. ciphertxtSz += len;
  50001. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  50002. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  50003. ExpectIntEQ(0, ciphertxtSz);
  50004. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  50005. EVP_CIPHER_CTX_init(de);
  50006. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  50007. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50008. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  50009. decryptedtxtSz = len;
  50010. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  50011. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  50012. decryptedtxtSz += len;
  50013. ExpectIntEQ(0, decryptedtxtSz);
  50014. EVP_CIPHER_CTX_free(en);
  50015. EVP_CIPHER_CTX_free(de);
  50016. #endif
  50017. return EXPECT_RESULT();
  50018. }
  50019. static int test_wolfssl_EVP_aes_gcm(void)
  50020. {
  50021. EXPECT_DECLS;
  50022. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  50023. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50024. /* A 256 bit key, AES_128 will use the first 128 bit*/
  50025. byte *key = (byte*)"01234567890123456789012345678901";
  50026. /* A 128 bit IV */
  50027. byte *iv = (byte*)"0123456789012345";
  50028. int ivSz = AES_BLOCK_SIZE;
  50029. /* Message to be encrypted */
  50030. byte *plaintxt = (byte*)"for things to change you have to change";
  50031. /* Additional non-confidential data */
  50032. byte *aad = (byte*)"Don't spend major time on minor things.";
  50033. unsigned char tag[AES_BLOCK_SIZE] = {0};
  50034. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  50035. int aadSz = (int)XSTRLEN((char*)aad);
  50036. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  50037. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  50038. int ciphertxtSz = 0;
  50039. int decryptedtxtSz = 0;
  50040. int len = 0;
  50041. int i = 0;
  50042. EVP_CIPHER_CTX en[2];
  50043. EVP_CIPHER_CTX de[2];
  50044. for (i = 0; i < 2; i++) {
  50045. EVP_CIPHER_CTX_init(&en[i]);
  50046. if (i == 0) {
  50047. /* Default uses 96-bits IV length */
  50048. #ifdef WOLFSSL_AES_128
  50049. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  50050. key, iv));
  50051. #elif defined(WOLFSSL_AES_192)
  50052. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  50053. key, iv));
  50054. #elif defined(WOLFSSL_AES_256)
  50055. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  50056. key, iv));
  50057. #endif
  50058. }
  50059. else {
  50060. #ifdef WOLFSSL_AES_128
  50061. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  50062. NULL, NULL));
  50063. #elif defined(WOLFSSL_AES_192)
  50064. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  50065. NULL, NULL));
  50066. #elif defined(WOLFSSL_AES_256)
  50067. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  50068. NULL, NULL));
  50069. #endif
  50070. /* non-default must to set the IV length first */
  50071. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  50072. ivSz, NULL));
  50073. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50074. }
  50075. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  50076. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  50077. plaintxtSz));
  50078. ciphertxtSz = len;
  50079. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50080. ciphertxtSz += len;
  50081. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  50082. AES_BLOCK_SIZE, tag));
  50083. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  50084. EVP_CIPHER_CTX_init(&de[i]);
  50085. if (i == 0) {
  50086. /* Default uses 96-bits IV length */
  50087. #ifdef WOLFSSL_AES_128
  50088. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50089. key, iv));
  50090. #elif defined(WOLFSSL_AES_192)
  50091. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50092. key, iv));
  50093. #elif defined(WOLFSSL_AES_256)
  50094. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50095. key, iv));
  50096. #endif
  50097. }
  50098. else {
  50099. #ifdef WOLFSSL_AES_128
  50100. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50101. NULL, NULL));
  50102. #elif defined(WOLFSSL_AES_192)
  50103. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50104. NULL, NULL));
  50105. #elif defined(WOLFSSL_AES_256)
  50106. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50107. NULL, NULL));
  50108. #endif
  50109. /* non-default must to set the IV length first */
  50110. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  50111. ivSz, NULL));
  50112. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50113. }
  50114. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50115. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50116. ciphertxtSz));
  50117. decryptedtxtSz = len;
  50118. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50119. AES_BLOCK_SIZE, tag));
  50120. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50121. decryptedtxtSz += len;
  50122. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  50123. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50124. /* modify tag*/
  50125. if (i == 0) {
  50126. /* Default uses 96-bits IV length */
  50127. #ifdef WOLFSSL_AES_128
  50128. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50129. key, iv));
  50130. #elif defined(WOLFSSL_AES_192)
  50131. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50132. key, iv));
  50133. #elif defined(WOLFSSL_AES_256)
  50134. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50135. key, iv));
  50136. #endif
  50137. }
  50138. else {
  50139. #ifdef WOLFSSL_AES_128
  50140. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50141. NULL, NULL));
  50142. #elif defined(WOLFSSL_AES_192)
  50143. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50144. NULL, NULL));
  50145. #elif defined(WOLFSSL_AES_256)
  50146. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50147. NULL, NULL));
  50148. #endif
  50149. /* non-default must to set the IV length first */
  50150. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  50151. ivSz, NULL));
  50152. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50153. }
  50154. tag[AES_BLOCK_SIZE-1]+=0xBB;
  50155. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50156. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50157. AES_BLOCK_SIZE, tag));
  50158. /* fail due to wrong tag */
  50159. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50160. ciphertxtSz));
  50161. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50162. ExpectIntEQ(0, len);
  50163. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  50164. }
  50165. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  50166. return EXPECT_RESULT();
  50167. }
  50168. static int test_wolfssl_EVP_aria_gcm(void)
  50169. {
  50170. int res = TEST_SKIPPED;
  50171. #if defined(OPENSSL_EXTRA) && defined(HAVE_ARIA) && \
  50172. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50173. /* A 256 bit key, AES_128 will use the first 128 bit*/
  50174. byte *key = (byte*)"01234567890123456789012345678901";
  50175. /* A 128 bit IV */
  50176. byte *iv = (byte*)"0123456789012345";
  50177. int ivSz = ARIA_BLOCK_SIZE;
  50178. /* Message to be encrypted */
  50179. const int plaintxtSz = 40;
  50180. byte plaintxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  50181. XMEMCPY(plaintxt,"for things to change you have to change",plaintxtSz);
  50182. /* Additional non-confidential data */
  50183. byte *aad = (byte*)"Don't spend major time on minor things.";
  50184. unsigned char tag[ARIA_BLOCK_SIZE] = {0};
  50185. int aadSz = (int)XSTRLEN((char*)aad);
  50186. byte ciphertxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  50187. byte decryptedtxt[plaintxtSz];
  50188. int ciphertxtSz = 0;
  50189. int decryptedtxtSz = 0;
  50190. int len = 0;
  50191. int i = 0;
  50192. #define TEST_ARIA_GCM_COUNT 6
  50193. EVP_CIPHER_CTX en[TEST_ARIA_GCM_COUNT];
  50194. EVP_CIPHER_CTX de[TEST_ARIA_GCM_COUNT];
  50195. for (i = 0; i < TEST_ARIA_GCM_COUNT; i++) {
  50196. EVP_CIPHER_CTX_init(&en[i]);
  50197. switch (i) {
  50198. case 0:
  50199. /* Default uses 96-bits IV length */
  50200. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, key, iv));
  50201. break;
  50202. case 1:
  50203. /* Default uses 96-bits IV length */
  50204. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, key, iv));
  50205. break;
  50206. case 2:
  50207. /* Default uses 96-bits IV length */
  50208. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, key, iv));
  50209. break;
  50210. case 3:
  50211. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  50212. /* non-default must to set the IV length first */
  50213. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50214. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50215. break;
  50216. case 4:
  50217. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  50218. /* non-default must to set the IV length first */
  50219. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50220. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50221. break;
  50222. case 5:
  50223. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  50224. /* non-default must to set the IV length first */
  50225. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50226. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50227. break;
  50228. }
  50229. XMEMSET(ciphertxt,0,sizeof(ciphertxt));
  50230. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  50231. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  50232. ciphertxtSz = len;
  50233. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50234. AssertIntNE(0, XMEMCMP(plaintxt, ciphertxt, plaintxtSz));
  50235. ciphertxtSz += len;
  50236. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, ARIA_BLOCK_SIZE, tag));
  50237. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  50238. EVP_CIPHER_CTX_init(&de[i]);
  50239. switch (i) {
  50240. case 0:
  50241. /* Default uses 96-bits IV length */
  50242. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, key, iv));
  50243. break;
  50244. case 1:
  50245. /* Default uses 96-bits IV length */
  50246. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, key, iv));
  50247. break;
  50248. case 2:
  50249. /* Default uses 96-bits IV length */
  50250. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, key, iv));
  50251. break;
  50252. case 3:
  50253. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  50254. /* non-default must to set the IV length first */
  50255. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50256. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50257. break;
  50258. case 4:
  50259. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  50260. /* non-default must to set the IV length first */
  50261. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50262. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50263. break;
  50264. case 5:
  50265. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  50266. /* non-default must to set the IV length first */
  50267. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50268. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50269. break;
  50270. }
  50271. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  50272. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50273. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  50274. decryptedtxtSz = len;
  50275. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  50276. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50277. decryptedtxtSz += len;
  50278. AssertIntEQ(plaintxtSz, decryptedtxtSz);
  50279. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50280. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  50281. /* modify tag*/
  50282. tag[AES_BLOCK_SIZE-1]+=0xBB;
  50283. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50284. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  50285. /* fail due to wrong tag */
  50286. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  50287. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50288. AssertIntEQ(0, len);
  50289. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  50290. }
  50291. res = TEST_RES_CHECK(1);
  50292. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  50293. return res;
  50294. }
  50295. static int test_wolfssl_EVP_aes_ccm_zeroLen(void)
  50296. {
  50297. EXPECT_DECLS;
  50298. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  50299. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50300. /* Zero length plain text */
  50301. byte key[] = {
  50302. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50303. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50304. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50305. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50306. }; /* align */
  50307. byte iv[] = {
  50308. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50309. }; /* align */
  50310. byte plaintxt[1];
  50311. int ivSz = 12;
  50312. int plaintxtSz = 0;
  50313. unsigned char tag[16];
  50314. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  50315. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  50316. int ciphertxtSz = 0;
  50317. int decryptedtxtSz = 0;
  50318. int len = 0;
  50319. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  50320. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  50321. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_ccm(), NULL, key, iv));
  50322. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  50323. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  50324. plaintxtSz));
  50325. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  50326. ciphertxtSz += len;
  50327. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  50328. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  50329. ExpectIntEQ(0, ciphertxtSz);
  50330. EVP_CIPHER_CTX_init(de);
  50331. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_ccm(), NULL, key, iv));
  50332. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  50333. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  50334. decryptedtxtSz = len;
  50335. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  50336. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  50337. decryptedtxtSz += len;
  50338. ExpectIntEQ(0, decryptedtxtSz);
  50339. EVP_CIPHER_CTX_free(en);
  50340. EVP_CIPHER_CTX_free(de);
  50341. #endif
  50342. return EXPECT_RESULT();
  50343. }
  50344. static int test_wolfssl_EVP_aes_ccm(void)
  50345. {
  50346. EXPECT_DECLS;
  50347. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  50348. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50349. /* A 256 bit key, AES_128 will use the first 128 bit*/
  50350. byte *key = (byte*)"01234567890123456789012345678901";
  50351. /* A 128 bit IV */
  50352. byte *iv = (byte*)"0123456789012";
  50353. int ivSz = (int)XSTRLEN((char*)iv);
  50354. /* Message to be encrypted */
  50355. byte *plaintxt = (byte*)"for things to change you have to change";
  50356. /* Additional non-confidential data */
  50357. byte *aad = (byte*)"Don't spend major time on minor things.";
  50358. unsigned char tag[AES_BLOCK_SIZE] = {0};
  50359. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  50360. int aadSz = (int)XSTRLEN((char*)aad);
  50361. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  50362. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  50363. int ciphertxtSz = 0;
  50364. int decryptedtxtSz = 0;
  50365. int len = 0;
  50366. int i = 0;
  50367. int ret;
  50368. EVP_CIPHER_CTX en[2];
  50369. EVP_CIPHER_CTX de[2];
  50370. for (i = 0; i < 2; i++) {
  50371. EVP_CIPHER_CTX_init(&en[i]);
  50372. if (i == 0) {
  50373. /* Default uses 96-bits IV length */
  50374. #ifdef WOLFSSL_AES_128
  50375. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  50376. key, iv));
  50377. #elif defined(WOLFSSL_AES_192)
  50378. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  50379. key, iv));
  50380. #elif defined(WOLFSSL_AES_256)
  50381. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  50382. key, iv));
  50383. #endif
  50384. }
  50385. else {
  50386. #ifdef WOLFSSL_AES_128
  50387. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  50388. NULL, NULL));
  50389. #elif defined(WOLFSSL_AES_192)
  50390. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  50391. NULL, NULL));
  50392. #elif defined(WOLFSSL_AES_256)
  50393. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  50394. NULL, NULL));
  50395. #endif
  50396. /* non-default must to set the IV length first */
  50397. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  50398. ivSz, NULL));
  50399. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50400. }
  50401. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  50402. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  50403. plaintxtSz));
  50404. ciphertxtSz = len;
  50405. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50406. ciphertxtSz += len;
  50407. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  50408. AES_BLOCK_SIZE, tag));
  50409. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]);
  50410. ExpectIntEQ(ret, 1);
  50411. EVP_CIPHER_CTX_init(&de[i]);
  50412. if (i == 0) {
  50413. /* Default uses 96-bits IV length */
  50414. #ifdef WOLFSSL_AES_128
  50415. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  50416. key, iv));
  50417. #elif defined(WOLFSSL_AES_192)
  50418. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  50419. key, iv));
  50420. #elif defined(WOLFSSL_AES_256)
  50421. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  50422. key, iv));
  50423. #endif
  50424. }
  50425. else {
  50426. #ifdef WOLFSSL_AES_128
  50427. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  50428. NULL, NULL));
  50429. #elif defined(WOLFSSL_AES_192)
  50430. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  50431. NULL, NULL));
  50432. #elif defined(WOLFSSL_AES_256)
  50433. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  50434. NULL, NULL));
  50435. #endif
  50436. /* non-default must to set the IV length first */
  50437. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  50438. ivSz, NULL));
  50439. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50440. }
  50441. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50442. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50443. ciphertxtSz));
  50444. decryptedtxtSz = len;
  50445. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  50446. AES_BLOCK_SIZE, tag));
  50447. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50448. decryptedtxtSz += len;
  50449. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  50450. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50451. /* modify tag*/
  50452. tag[AES_BLOCK_SIZE-1]+=0xBB;
  50453. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50454. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  50455. AES_BLOCK_SIZE, tag));
  50456. /* fail due to wrong tag */
  50457. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50458. ciphertxtSz));
  50459. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50460. ExpectIntEQ(0, len);
  50461. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]);
  50462. ExpectIntEQ(ret, 1);
  50463. }
  50464. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESCCM */
  50465. return EXPECT_RESULT();
  50466. }
  50467. static int test_wolfssl_EVP_chacha20_poly1305(void)
  50468. {
  50469. EXPECT_DECLS;
  50470. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  50471. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  50472. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  50473. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  50474. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  50475. byte cipherText[sizeof(plainText)];
  50476. byte decryptedText[sizeof(plainText)];
  50477. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  50478. EVP_CIPHER_CTX* ctx = NULL;
  50479. int outSz;
  50480. /* Encrypt. */
  50481. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50482. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  50483. NULL), WOLFSSL_SUCCESS);
  50484. /* Invalid IV length. */
  50485. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  50486. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  50487. /* Valid IV length. */
  50488. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  50489. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  50490. /* Invalid tag length. */
  50491. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  50492. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  50493. /* Valid tag length. */
  50494. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  50495. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  50496. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50497. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  50498. WOLFSSL_SUCCESS);
  50499. ExpectIntEQ(outSz, sizeof(aad));
  50500. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  50501. sizeof(plainText)), WOLFSSL_SUCCESS);
  50502. ExpectIntEQ(outSz, sizeof(plainText));
  50503. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  50504. ExpectIntEQ(outSz, 0);
  50505. /* Invalid tag length. */
  50506. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  50507. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  50508. /* Valid tag length. */
  50509. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  50510. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  50511. EVP_CIPHER_CTX_free(ctx);
  50512. ctx = NULL;
  50513. /* Decrypt. */
  50514. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50515. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  50516. NULL), WOLFSSL_SUCCESS);
  50517. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  50518. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  50519. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  50520. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  50521. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50522. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  50523. WOLFSSL_SUCCESS);
  50524. ExpectIntEQ(outSz, sizeof(aad));
  50525. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50526. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50527. ExpectIntEQ(outSz, sizeof(cipherText));
  50528. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50529. WOLFSSL_SUCCESS);
  50530. ExpectIntEQ(outSz, 0);
  50531. EVP_CIPHER_CTX_free(ctx);
  50532. ctx = NULL;
  50533. /* Test partial Inits. CipherInit() allow setting of key and iv
  50534. * in separate calls. */
  50535. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50536. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  50537. key, NULL, 1), WOLFSSL_SUCCESS);
  50538. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  50539. WOLFSSL_SUCCESS);
  50540. ExpectIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  50541. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  50542. ExpectIntEQ(outSz, sizeof(aad));
  50543. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50544. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50545. ExpectIntEQ(outSz, sizeof(cipherText));
  50546. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50547. WOLFSSL_SUCCESS);
  50548. ExpectIntEQ(outSz, 0);
  50549. EVP_CIPHER_CTX_free(ctx);
  50550. #endif
  50551. return EXPECT_RESULT();
  50552. }
  50553. static int test_wolfssl_EVP_chacha20(void)
  50554. {
  50555. EXPECT_DECLS;
  50556. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  50557. byte key[CHACHA_MAX_KEY_SZ];
  50558. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  50559. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  50560. byte cipherText[sizeof(plainText)];
  50561. byte decryptedText[sizeof(plainText)];
  50562. EVP_CIPHER_CTX* ctx = NULL;
  50563. int outSz;
  50564. XMEMSET(key, 0, sizeof(key));
  50565. XMEMSET(iv, 0, sizeof(iv));
  50566. /* Encrypt. */
  50567. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50568. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  50569. NULL), WOLFSSL_SUCCESS);
  50570. /* Any tag length must fail - not an AEAD cipher. */
  50571. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  50572. 16, NULL), WOLFSSL_FAILURE);
  50573. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50574. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  50575. sizeof(plainText)), WOLFSSL_SUCCESS);
  50576. ExpectIntEQ(outSz, sizeof(plainText));
  50577. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  50578. ExpectIntEQ(outSz, 0);
  50579. EVP_CIPHER_CTX_free(ctx);
  50580. ctx = NULL;
  50581. /* Decrypt. */
  50582. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50583. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  50584. NULL), WOLFSSL_SUCCESS);
  50585. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50586. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50587. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50588. ExpectIntEQ(outSz, sizeof(cipherText));
  50589. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50590. WOLFSSL_SUCCESS);
  50591. ExpectIntEQ(outSz, 0);
  50592. EVP_CIPHER_CTX_free(ctx);
  50593. ctx = NULL;
  50594. /* Test partial Inits. CipherInit() allow setting of key and iv
  50595. * in separate calls. */
  50596. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50597. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  50598. key, NULL, 1), WOLFSSL_SUCCESS);
  50599. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  50600. WOLFSSL_SUCCESS);
  50601. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50602. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50603. ExpectIntEQ(outSz, sizeof(cipherText));
  50604. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50605. WOLFSSL_SUCCESS);
  50606. ExpectIntEQ(outSz, 0);
  50607. EVP_CIPHER_CTX_free(ctx);
  50608. #endif
  50609. return EXPECT_RESULT();
  50610. }
  50611. static int test_wolfssl_EVP_sm4_ecb(void)
  50612. {
  50613. int res = TEST_SKIPPED;
  50614. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_ECB)
  50615. EXPECT_DECLS;
  50616. byte key[SM4_KEY_SIZE];
  50617. byte plainText[SM4_BLOCK_SIZE] = {
  50618. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  50619. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  50620. };
  50621. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  50622. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  50623. EVP_CIPHER_CTX* ctx;
  50624. int outSz;
  50625. XMEMSET(key, 0, sizeof(key));
  50626. /* Encrypt. */
  50627. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50628. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  50629. WOLFSSL_SUCCESS);
  50630. /* Any tag length must fail - not an AEAD cipher. */
  50631. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  50632. WOLFSSL_FAILURE);
  50633. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL),
  50634. WOLFSSL_SUCCESS);
  50635. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  50636. sizeof(plainText)), WOLFSSL_SUCCESS);
  50637. ExpectIntEQ(outSz, sizeof(plainText));
  50638. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  50639. WOLFSSL_SUCCESS);
  50640. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  50641. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  50642. EVP_CIPHER_CTX_free(ctx);
  50643. /* Decrypt. */
  50644. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50645. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  50646. WOLFSSL_SUCCESS);
  50647. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, NULL),
  50648. WOLFSSL_SUCCESS);
  50649. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50650. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50651. ExpectIntEQ(outSz, sizeof(plainText));
  50652. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  50653. WOLFSSL_SUCCESS);
  50654. ExpectIntEQ(outSz, 0);
  50655. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50656. EVP_CIPHER_CTX_free(ctx);
  50657. res = EXPECT_RESULT();
  50658. #endif
  50659. return res;
  50660. }
  50661. static int test_wolfssl_EVP_sm4_cbc(void)
  50662. {
  50663. int res = TEST_SKIPPED;
  50664. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CBC)
  50665. EXPECT_DECLS;
  50666. byte key[SM4_KEY_SIZE];
  50667. byte iv[SM4_BLOCK_SIZE];
  50668. byte plainText[SM4_BLOCK_SIZE] = {
  50669. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  50670. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  50671. };
  50672. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  50673. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  50674. EVP_CIPHER_CTX* ctx;
  50675. int outSz;
  50676. XMEMSET(key, 0, sizeof(key));
  50677. XMEMSET(iv, 0, sizeof(iv));
  50678. /* Encrypt. */
  50679. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50680. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  50681. WOLFSSL_SUCCESS);
  50682. /* Any tag length must fail - not an AEAD cipher. */
  50683. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  50684. WOLFSSL_FAILURE);
  50685. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50686. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  50687. sizeof(plainText)), WOLFSSL_SUCCESS);
  50688. ExpectIntEQ(outSz, sizeof(plainText));
  50689. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  50690. WOLFSSL_SUCCESS);
  50691. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  50692. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  50693. EVP_CIPHER_CTX_free(ctx);
  50694. /* Decrypt. */
  50695. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50696. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  50697. WOLFSSL_SUCCESS);
  50698. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50699. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50700. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50701. ExpectIntEQ(outSz, sizeof(plainText));
  50702. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  50703. WOLFSSL_SUCCESS);
  50704. ExpectIntEQ(outSz, 0);
  50705. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50706. EVP_CIPHER_CTX_free(ctx);
  50707. /* Test partial Inits. CipherInit() allow setting of key and iv
  50708. * in separate calls. */
  50709. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50710. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_cbc(), key, NULL, 0),
  50711. WOLFSSL_SUCCESS);
  50712. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 0),
  50713. WOLFSSL_SUCCESS);
  50714. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50715. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50716. ExpectIntEQ(outSz, sizeof(plainText));
  50717. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  50718. WOLFSSL_SUCCESS);
  50719. ExpectIntEQ(outSz, 0);
  50720. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50721. EVP_CIPHER_CTX_free(ctx);
  50722. res = EXPECT_RESULT();
  50723. #endif
  50724. return res;
  50725. }
  50726. static int test_wolfssl_EVP_sm4_ctr(void)
  50727. {
  50728. int res = TEST_SKIPPED;
  50729. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CTR)
  50730. EXPECT_DECLS;
  50731. byte key[SM4_KEY_SIZE];
  50732. byte iv[SM4_BLOCK_SIZE];
  50733. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  50734. byte cipherText[sizeof(plainText)];
  50735. byte decryptedText[sizeof(plainText)];
  50736. EVP_CIPHER_CTX* ctx;
  50737. int outSz;
  50738. XMEMSET(key, 0, sizeof(key));
  50739. XMEMSET(iv, 0, sizeof(iv));
  50740. /* Encrypt. */
  50741. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50742. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  50743. WOLFSSL_SUCCESS);
  50744. /* Any tag length must fail - not an AEAD cipher. */
  50745. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  50746. WOLFSSL_FAILURE);
  50747. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50748. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  50749. sizeof(plainText)), WOLFSSL_SUCCESS);
  50750. ExpectIntEQ(outSz, sizeof(plainText));
  50751. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  50752. ExpectIntEQ(outSz, 0);
  50753. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  50754. EVP_CIPHER_CTX_free(ctx);
  50755. /* Decrypt. */
  50756. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50757. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  50758. WOLFSSL_SUCCESS);
  50759. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50760. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50761. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50762. ExpectIntEQ(outSz, sizeof(cipherText));
  50763. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50764. WOLFSSL_SUCCESS);
  50765. ExpectIntEQ(outSz, 0);
  50766. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50767. EVP_CIPHER_CTX_free(ctx);
  50768. /* Test partial Inits. CipherInit() allow setting of key and iv
  50769. * in separate calls. */
  50770. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50771. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_ctr(), key, NULL, 1),
  50772. WOLFSSL_SUCCESS);
  50773. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  50774. WOLFSSL_SUCCESS);
  50775. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50776. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50777. ExpectIntEQ(outSz, sizeof(cipherText));
  50778. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50779. WOLFSSL_SUCCESS);
  50780. ExpectIntEQ(outSz, 0);
  50781. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50782. EVP_CIPHER_CTX_free(ctx);
  50783. res = EXPECT_RESULT();
  50784. #endif
  50785. return res;
  50786. }
  50787. static int test_wolfssl_EVP_sm4_gcm_zeroLen(void)
  50788. {
  50789. int res = TEST_SKIPPED;
  50790. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  50791. /* Zero length plain text */
  50792. EXPECT_DECLS;
  50793. byte key[] = {
  50794. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50795. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50796. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50797. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50798. }; /* align */
  50799. byte iv[] = {
  50800. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50801. }; /* align */
  50802. byte plaintxt[1];
  50803. int ivSz = 12;
  50804. int plaintxtSz = 0;
  50805. unsigned char tag[16];
  50806. unsigned char tag_kat[16] = {
  50807. 0x23,0x2f,0x0c,0xfe,0x30,0x8b,0x49,0xea,
  50808. 0x6f,0xc8,0x82,0x29,0xb5,0xdc,0x85,0x8d
  50809. };
  50810. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  50811. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  50812. int ciphertxtSz = 0;
  50813. int decryptedtxtSz = 0;
  50814. int len = 0;
  50815. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  50816. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  50817. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_gcm(), NULL, key, iv));
  50818. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50819. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  50820. plaintxtSz));
  50821. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  50822. ciphertxtSz += len;
  50823. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  50824. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  50825. ExpectIntEQ(0, ciphertxtSz);
  50826. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  50827. EVP_CIPHER_CTX_init(de);
  50828. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_gcm(), NULL, key, iv));
  50829. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50830. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  50831. decryptedtxtSz = len;
  50832. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  50833. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  50834. decryptedtxtSz += len;
  50835. ExpectIntEQ(0, decryptedtxtSz);
  50836. EVP_CIPHER_CTX_free(en);
  50837. EVP_CIPHER_CTX_free(de);
  50838. res = EXPECT_RESULT();
  50839. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  50840. return res;
  50841. }
  50842. static int test_wolfssl_EVP_sm4_gcm(void)
  50843. {
  50844. int res = TEST_SKIPPED;
  50845. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  50846. EXPECT_DECLS;
  50847. byte *key = (byte*)"0123456789012345";
  50848. /* A 128 bit IV */
  50849. byte *iv = (byte*)"0123456789012345";
  50850. int ivSz = SM4_BLOCK_SIZE;
  50851. /* Message to be encrypted */
  50852. byte *plaintxt = (byte*)"for things to change you have to change";
  50853. /* Additional non-confidential data */
  50854. byte *aad = (byte*)"Don't spend major time on minor things.";
  50855. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  50856. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  50857. int aadSz = (int)XSTRLEN((char*)aad);
  50858. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  50859. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  50860. int ciphertxtSz = 0;
  50861. int decryptedtxtSz = 0;
  50862. int len = 0;
  50863. int i = 0;
  50864. EVP_CIPHER_CTX en[2];
  50865. EVP_CIPHER_CTX de[2];
  50866. for (i = 0; i < 2; i++) {
  50867. EVP_CIPHER_CTX_init(&en[i]);
  50868. if (i == 0) {
  50869. /* Default uses 96-bits IV length */
  50870. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, key,
  50871. iv));
  50872. }
  50873. else {
  50874. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, NULL,
  50875. NULL));
  50876. /* non-default must to set the IV length first */
  50877. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  50878. ivSz, NULL));
  50879. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50880. }
  50881. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  50882. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  50883. plaintxtSz));
  50884. ciphertxtSz = len;
  50885. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50886. ciphertxtSz += len;
  50887. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  50888. SM4_BLOCK_SIZE, tag));
  50889. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  50890. EVP_CIPHER_CTX_init(&de[i]);
  50891. if (i == 0) {
  50892. /* Default uses 96-bits IV length */
  50893. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, key,
  50894. iv));
  50895. }
  50896. else {
  50897. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, NULL,
  50898. NULL));
  50899. /* non-default must to set the IV length first */
  50900. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  50901. ivSz, NULL));
  50902. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50903. }
  50904. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50905. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50906. ciphertxtSz));
  50907. decryptedtxtSz = len;
  50908. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50909. SM4_BLOCK_SIZE, tag));
  50910. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50911. decryptedtxtSz += len;
  50912. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  50913. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50914. /* modify tag*/
  50915. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  50916. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50917. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50918. SM4_BLOCK_SIZE, tag));
  50919. /* fail due to wrong tag */
  50920. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50921. ciphertxtSz));
  50922. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50923. ExpectIntEQ(0, len);
  50924. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  50925. }
  50926. res = EXPECT_RESULT();
  50927. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  50928. return res;
  50929. }
  50930. static int test_wolfssl_EVP_sm4_ccm_zeroLen(void)
  50931. {
  50932. int res = TEST_SKIPPED;
  50933. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  50934. /* Zero length plain text */
  50935. EXPECT_DECLS;
  50936. byte key[] = {
  50937. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50938. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50939. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50940. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50941. }; /* align */
  50942. byte iv[] = {
  50943. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50944. }; /* align */
  50945. byte plaintxt[1];
  50946. int ivSz = 12;
  50947. int plaintxtSz = 0;
  50948. unsigned char tag[16];
  50949. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  50950. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  50951. int ciphertxtSz = 0;
  50952. int decryptedtxtSz = 0;
  50953. int len = 0;
  50954. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  50955. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  50956. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_ccm(), NULL, key, iv));
  50957. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  50958. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  50959. plaintxtSz));
  50960. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  50961. ciphertxtSz += len;
  50962. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  50963. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  50964. ExpectIntEQ(0, ciphertxtSz);
  50965. EVP_CIPHER_CTX_init(de);
  50966. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_ccm(), NULL, key, iv));
  50967. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  50968. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  50969. decryptedtxtSz = len;
  50970. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  50971. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  50972. decryptedtxtSz += len;
  50973. ExpectIntEQ(0, decryptedtxtSz);
  50974. EVP_CIPHER_CTX_free(en);
  50975. EVP_CIPHER_CTX_free(de);
  50976. res = EXPECT_RESULT();
  50977. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  50978. return res;
  50979. }
  50980. static int test_wolfssl_EVP_sm4_ccm(void)
  50981. {
  50982. int res = TEST_SKIPPED;
  50983. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  50984. EXPECT_DECLS;
  50985. byte *key = (byte*)"0123456789012345";
  50986. byte *iv = (byte*)"0123456789012";
  50987. int ivSz = (int)XSTRLEN((char*)iv);
  50988. /* Message to be encrypted */
  50989. byte *plaintxt = (byte*)"for things to change you have to change";
  50990. /* Additional non-confidential data */
  50991. byte *aad = (byte*)"Don't spend major time on minor things.";
  50992. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  50993. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  50994. int aadSz = (int)XSTRLEN((char*)aad);
  50995. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  50996. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  50997. int ciphertxtSz = 0;
  50998. int decryptedtxtSz = 0;
  50999. int len = 0;
  51000. int i = 0;
  51001. EVP_CIPHER_CTX en[2];
  51002. EVP_CIPHER_CTX de[2];
  51003. for (i = 0; i < 2; i++) {
  51004. EVP_CIPHER_CTX_init(&en[i]);
  51005. if (i == 0) {
  51006. /* Default uses 96-bits IV length */
  51007. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, key,
  51008. iv));
  51009. }
  51010. else {
  51011. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, NULL,
  51012. NULL));
  51013. /* non-default must to set the IV length first */
  51014. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  51015. ivSz, NULL));
  51016. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  51017. }
  51018. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  51019. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  51020. plaintxtSz));
  51021. ciphertxtSz = len;
  51022. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  51023. ciphertxtSz += len;
  51024. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  51025. SM4_BLOCK_SIZE, tag));
  51026. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  51027. EVP_CIPHER_CTX_init(&de[i]);
  51028. if (i == 0) {
  51029. /* Default uses 96-bits IV length */
  51030. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, key,
  51031. iv));
  51032. }
  51033. else {
  51034. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, NULL,
  51035. NULL));
  51036. /* non-default must to set the IV length first */
  51037. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  51038. ivSz, NULL));
  51039. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  51040. }
  51041. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  51042. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  51043. ciphertxtSz));
  51044. decryptedtxtSz = len;
  51045. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  51046. SM4_BLOCK_SIZE, tag));
  51047. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  51048. decryptedtxtSz += len;
  51049. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  51050. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  51051. /* modify tag*/
  51052. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  51053. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  51054. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  51055. SM4_BLOCK_SIZE, tag));
  51056. /* fail due to wrong tag */
  51057. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  51058. ciphertxtSz));
  51059. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  51060. ExpectIntEQ(0, len);
  51061. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  51062. }
  51063. res = EXPECT_RESULT();
  51064. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  51065. return res;
  51066. }
  51067. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  51068. {
  51069. EXPECT_DECLS;
  51070. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  51071. EVP_PKEY_CTX* ctx = NULL;
  51072. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  51073. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  51074. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  51075. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  51076. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  51077. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  51078. byte outKey[34];
  51079. size_t outKeySz = sizeof(outKey);
  51080. /* These expected outputs were gathered by running the same test below using
  51081. * OpenSSL. */
  51082. const byte extractAndExpand[] = {
  51083. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  51084. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  51085. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  51086. };
  51087. const byte extractOnly[] = {
  51088. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  51089. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  51090. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  51091. };
  51092. const byte expandOnly[] = {
  51093. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  51094. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  51095. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  51096. };
  51097. const byte extractAndExpandAddInfo[] = {
  51098. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  51099. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  51100. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  51101. };
  51102. ExpectNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  51103. ExpectIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  51104. /* NULL ctx. */
  51105. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  51106. /* NULL md. */
  51107. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  51108. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  51109. /* NULL ctx. */
  51110. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  51111. WOLFSSL_FAILURE);
  51112. /* NULL salt is ok. */
  51113. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  51114. WOLFSSL_SUCCESS);
  51115. /* Salt length <= 0. */
  51116. /* Length 0 salt is ok. */
  51117. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  51118. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  51119. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  51120. WOLFSSL_SUCCESS);
  51121. /* NULL ctx. */
  51122. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  51123. WOLFSSL_FAILURE);
  51124. /* NULL key. */
  51125. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  51126. WOLFSSL_FAILURE);
  51127. /* Key length <= 0 */
  51128. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  51129. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  51130. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  51131. WOLFSSL_SUCCESS);
  51132. /* NULL ctx. */
  51133. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  51134. WOLFSSL_FAILURE);
  51135. /* NULL info is ok. */
  51136. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  51137. WOLFSSL_SUCCESS);
  51138. /* Info length <= 0 */
  51139. /* Length 0 info is ok. */
  51140. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  51141. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  51142. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  51143. WOLFSSL_SUCCESS);
  51144. /* NULL ctx. */
  51145. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  51146. WOLFSSL_FAILURE);
  51147. /* Extract and expand (default). */
  51148. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51149. ExpectIntEQ(outKeySz, sizeof(extractAndExpand));
  51150. ExpectIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  51151. /* Extract only. */
  51152. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  51153. WOLFSSL_SUCCESS);
  51154. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51155. ExpectIntEQ(outKeySz, sizeof(extractOnly));
  51156. ExpectIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  51157. outKeySz = sizeof(outKey);
  51158. /* Expand only. */
  51159. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  51160. WOLFSSL_SUCCESS);
  51161. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51162. ExpectIntEQ(outKeySz, sizeof(expandOnly));
  51163. ExpectIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  51164. outKeySz = sizeof(outKey);
  51165. /* Extract and expand with appended additional info. */
  51166. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  51167. WOLFSSL_SUCCESS);
  51168. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  51169. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  51170. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51171. ExpectIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  51172. ExpectIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  51173. EVP_PKEY_CTX_free(ctx);
  51174. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  51175. return EXPECT_RESULT();
  51176. }
  51177. #ifndef NO_BIO
  51178. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  51179. {
  51180. EXPECT_DECLS;
  51181. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  51182. BIO* bio = NULL;
  51183. X509_INFO* info = NULL;
  51184. STACK_OF(X509_INFO)* sk = NULL;
  51185. char* subject = NULL;
  51186. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/"
  51187. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  51188. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/"
  51189. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  51190. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  51191. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  51192. ExpectNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  51193. ExpectIntEQ(sk_X509_INFO_num(sk), 2);
  51194. /* using dereference to maintain testing for Apache port*/
  51195. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  51196. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  51197. 0, 0));
  51198. ExpectIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  51199. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  51200. X509_INFO_free(info);
  51201. info = NULL;
  51202. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  51203. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  51204. 0, 0));
  51205. ExpectIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  51206. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  51207. X509_INFO_free(info);
  51208. ExpectNull(info = sk_X509_INFO_pop(sk));
  51209. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  51210. BIO_free(bio);
  51211. #endif
  51212. return EXPECT_RESULT();
  51213. }
  51214. #endif /* !NO_BIO */
  51215. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  51216. {
  51217. EXPECT_DECLS;
  51218. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  51219. X509 *x509 = NULL;
  51220. X509_NAME* name = NULL;
  51221. int idx = 0;
  51222. X509_NAME_ENTRY *ne = NULL;
  51223. ASN1_OBJECT *object = NULL;
  51224. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  51225. WOLFSSL_FILETYPE_PEM));
  51226. ExpectNotNull(name = X509_get_subject_name(x509));
  51227. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  51228. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  51229. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  51230. X509_free(x509);
  51231. #endif
  51232. return EXPECT_RESULT();
  51233. }
  51234. static int test_wolfSSL_X509_STORE_get1_certs(void)
  51235. {
  51236. EXPECT_DECLS;
  51237. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  51238. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  51239. X509_STORE_CTX *storeCtx = NULL;
  51240. X509_STORE *store = NULL;
  51241. X509 *caX509 = NULL;
  51242. X509 *svrX509 = NULL;
  51243. X509_NAME *subject = NULL;
  51244. WOLF_STACK_OF(WOLFSSL_X509) *certs = NULL;
  51245. ExpectNotNull(caX509 = X509_load_certificate_file(caCertFile,
  51246. SSL_FILETYPE_PEM));
  51247. ExpectNotNull((svrX509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  51248. SSL_FILETYPE_PEM)));
  51249. ExpectNotNull(storeCtx = X509_STORE_CTX_new());
  51250. ExpectNotNull(store = X509_STORE_new());
  51251. ExpectNotNull(subject = X509_get_subject_name(caX509));
  51252. /* Errors */
  51253. ExpectNull(X509_STORE_get1_certs(storeCtx, subject));
  51254. ExpectNull(X509_STORE_get1_certs(NULL, subject));
  51255. ExpectNull(X509_STORE_get1_certs(storeCtx, NULL));
  51256. ExpectIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  51257. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL),
  51258. SSL_SUCCESS);
  51259. /* Should find the cert */
  51260. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  51261. ExpectIntEQ(1, wolfSSL_sk_X509_num(certs));
  51262. sk_X509_pop_free(certs, NULL);
  51263. certs = NULL;
  51264. /* Should not find the cert */
  51265. ExpectNotNull(subject = X509_get_subject_name(svrX509));
  51266. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  51267. ExpectIntEQ(0, wolfSSL_sk_X509_num(certs));
  51268. sk_X509_pop_free(certs, NULL);
  51269. certs = NULL;
  51270. X509_STORE_free(store);
  51271. X509_STORE_CTX_free(storeCtx);
  51272. X509_free(svrX509);
  51273. X509_free(caX509);
  51274. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  51275. return EXPECT_RESULT();
  51276. }
  51277. static int test_wolfSSL_dup_CA_list(void)
  51278. {
  51279. int res = TEST_SKIPPED;
  51280. #if defined(OPENSSL_ALL)
  51281. EXPECT_DECLS;
  51282. STACK_OF(X509_NAME) *originalStack = NULL;
  51283. STACK_OF(X509_NAME) *copyStack = NULL;
  51284. int originalCount = 0;
  51285. int copyCount = 0;
  51286. X509_NAME *name = NULL;
  51287. int i;
  51288. originalStack = sk_X509_NAME_new_null();
  51289. ExpectNotNull(originalStack);
  51290. for (i = 0; i < 3; i++) {
  51291. name = X509_NAME_new();
  51292. ExpectNotNull(name);
  51293. AssertIntEQ(sk_X509_NAME_push(originalStack, name), WOLFSSL_SUCCESS);
  51294. }
  51295. copyStack = SSL_dup_CA_list(originalStack);
  51296. ExpectNotNull(copyStack);
  51297. originalCount = sk_X509_NAME_num(originalStack);
  51298. copyCount = sk_X509_NAME_num(copyStack);
  51299. AssertIntEQ(originalCount, copyCount);
  51300. sk_X509_NAME_pop_free(originalStack, X509_NAME_free);
  51301. sk_X509_NAME_pop_free(copyStack, X509_NAME_free);
  51302. originalStack = NULL;
  51303. copyStack = NULL;
  51304. res = EXPECT_RESULT();
  51305. #endif /* OPENSSL_ALL */
  51306. return res;
  51307. }
  51308. static int test_ForceZero(void)
  51309. {
  51310. EXPECT_DECLS;
  51311. unsigned char data[32];
  51312. unsigned int i, j, len;
  51313. /* Test case with 0 length */
  51314. ForceZero(data, 0);
  51315. /* Test ForceZero */
  51316. for (i = 0; i < sizeof(data); i++) {
  51317. for (len = 1; len < sizeof(data) - i; len++) {
  51318. for (j = 0; j < sizeof(data); j++)
  51319. data[j] = j + 1;
  51320. ForceZero(data + i, len);
  51321. for (j = 0; j < sizeof(data); j++) {
  51322. if (j < i || j >= i + len) {
  51323. ExpectIntNE(data[j], 0x00);
  51324. }
  51325. else {
  51326. ExpectIntEQ(data[j], 0x00);
  51327. }
  51328. }
  51329. }
  51330. }
  51331. return EXPECT_RESULT();
  51332. }
  51333. #ifndef NO_BIO
  51334. static int test_wolfSSL_X509_print(void)
  51335. {
  51336. EXPECT_DECLS;
  51337. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  51338. !defined(NO_RSA) && defined(XSNPRINTF)
  51339. X509 *x509 = NULL;
  51340. BIO *bio = NULL;
  51341. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  51342. const X509_ALGOR *cert_sig_alg = NULL;
  51343. #endif
  51344. ExpectNotNull(x509 = X509_load_certificate_file(svrCertFile,
  51345. WOLFSSL_FILETYPE_PEM));
  51346. /* print to memory */
  51347. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  51348. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  51349. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  51350. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  51351. /* Will print IP address subject alt name. */
  51352. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  51353. #elif defined(NO_ASN_TIME)
  51354. /* Will print IP address subject alt name but not Validity. */
  51355. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3235);
  51356. #else
  51357. /* Will print IP address subject alt name. */
  51358. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  51359. #endif
  51360. #elif defined(NO_ASN_TIME)
  51361. /* With NO_ASN_TIME defined, X509_print skips printing Validity. */
  51362. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3213);
  51363. #else
  51364. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  51365. #endif
  51366. BIO_free(bio);
  51367. bio = NULL;
  51368. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  51369. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  51370. /* Print signature */
  51371. ExpectNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  51372. ExpectIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  51373. #endif
  51374. /* print to stderr */
  51375. #if !defined(NO_WOLFSSL_DIR)
  51376. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  51377. #endif
  51378. /* print again */
  51379. ExpectIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
  51380. X509_free(x509);
  51381. BIO_free(bio);
  51382. #endif
  51383. return EXPECT_RESULT();
  51384. }
  51385. static int test_wolfSSL_X509_CRL_print(void)
  51386. {
  51387. EXPECT_DECLS;
  51388. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  51389. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  51390. X509_CRL* crl = NULL;
  51391. BIO *bio = NULL;
  51392. XFILE fp = XBADFILE;
  51393. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  51394. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  51395. NULL, NULL));
  51396. if (fp != XBADFILE)
  51397. XFCLOSE(fp);
  51398. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  51399. ExpectIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  51400. X509_CRL_free(crl);
  51401. BIO_free(bio);
  51402. #endif
  51403. return EXPECT_RESULT();
  51404. }
  51405. static int test_wolfSSL_BIO_get_len(void)
  51406. {
  51407. EXPECT_DECLS;
  51408. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  51409. BIO *bio = NULL;
  51410. const char txt[] = "Some example text to push to the BIO.";
  51411. ExpectIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  51412. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  51413. ExpectIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  51414. ExpectIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  51415. BIO_free(bio);
  51416. bio = NULL;
  51417. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  51418. ExpectIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  51419. BIO_free(bio);
  51420. #endif
  51421. return EXPECT_RESULT();
  51422. }
  51423. #endif /* !NO_BIO */
  51424. static int test_wolfSSL_RSA(void)
  51425. {
  51426. EXPECT_DECLS;
  51427. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  51428. RSA* rsa = NULL;
  51429. const BIGNUM *n = NULL;
  51430. const BIGNUM *e = NULL;
  51431. const BIGNUM *d = NULL;
  51432. const BIGNUM *p = NULL;
  51433. const BIGNUM *q = NULL;
  51434. const BIGNUM *dmp1 = NULL;
  51435. const BIGNUM *dmq1 = NULL;
  51436. const BIGNUM *iqmp = NULL;
  51437. ExpectNotNull(rsa = RSA_new());
  51438. ExpectIntEQ(RSA_size(NULL), 0);
  51439. ExpectIntEQ(RSA_size(rsa), 0);
  51440. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  51441. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  51442. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  51443. #ifdef WOLFSSL_RSA_KEY_CHECK
  51444. ExpectIntEQ(RSA_check_key(rsa), 0);
  51445. #endif
  51446. RSA_free(rsa);
  51447. rsa = NULL;
  51448. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  51449. ExpectIntEQ(RSA_size(rsa), 256);
  51450. #ifdef WOLFSSL_RSA_KEY_CHECK
  51451. ExpectIntEQ(RSA_check_key(NULL), 0);
  51452. ExpectIntEQ(RSA_check_key(rsa), 1);
  51453. #endif
  51454. /* sanity check */
  51455. ExpectIntEQ(RSA_bits(NULL), 0);
  51456. /* key */
  51457. ExpectIntEQ(RSA_bits(rsa), 2048);
  51458. RSA_get0_key(rsa, &n, &e, &d);
  51459. ExpectPtrEq(rsa->n, n);
  51460. ExpectPtrEq(rsa->e, e);
  51461. ExpectPtrEq(rsa->d, d);
  51462. n = NULL;
  51463. e = NULL;
  51464. d = NULL;
  51465. ExpectNotNull(n = BN_new());
  51466. ExpectNotNull(e = BN_new());
  51467. ExpectNotNull(d = BN_new());
  51468. ExpectIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  51469. if (EXPECT_FAIL()) {
  51470. BN_free((BIGNUM*)n);
  51471. BN_free((BIGNUM*)e);
  51472. BN_free((BIGNUM*)d);
  51473. }
  51474. ExpectPtrEq(rsa->n, n);
  51475. ExpectPtrEq(rsa->e, e);
  51476. ExpectPtrEq(rsa->d, d);
  51477. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  51478. ExpectIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  51479. /* crt_params */
  51480. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  51481. ExpectPtrEq(rsa->dmp1, dmp1);
  51482. ExpectPtrEq(rsa->dmq1, dmq1);
  51483. ExpectPtrEq(rsa->iqmp, iqmp);
  51484. dmp1 = NULL;
  51485. dmq1 = NULL;
  51486. iqmp = NULL;
  51487. ExpectNotNull(dmp1 = BN_new());
  51488. ExpectNotNull(dmq1 = BN_new());
  51489. ExpectNotNull(iqmp = BN_new());
  51490. ExpectIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  51491. (BIGNUM*)iqmp), 1);
  51492. if (EXPECT_FAIL()) {
  51493. BN_free((BIGNUM*)dmp1);
  51494. BN_free((BIGNUM*)dmq1);
  51495. BN_free((BIGNUM*)iqmp);
  51496. }
  51497. ExpectPtrEq(rsa->dmp1, dmp1);
  51498. ExpectPtrEq(rsa->dmq1, dmq1);
  51499. ExpectPtrEq(rsa->iqmp, iqmp);
  51500. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  51501. ExpectIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  51502. (BIGNUM*)iqmp), 0);
  51503. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  51504. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  51505. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  51506. ExpectNull(dmp1);
  51507. ExpectNull(dmq1);
  51508. ExpectNull(iqmp);
  51509. /* factors */
  51510. RSA_get0_factors(rsa, NULL, NULL);
  51511. RSA_get0_factors(rsa, &p, &q);
  51512. ExpectPtrEq(rsa->p, p);
  51513. ExpectPtrEq(rsa->q, q);
  51514. p = NULL;
  51515. q = NULL;
  51516. ExpectNotNull(p = BN_new());
  51517. ExpectNotNull(q = BN_new());
  51518. ExpectIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  51519. if (EXPECT_FAIL()) {
  51520. BN_free((BIGNUM*)p);
  51521. BN_free((BIGNUM*)q);
  51522. }
  51523. ExpectPtrEq(rsa->p, p);
  51524. ExpectPtrEq(rsa->q, q);
  51525. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  51526. ExpectIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  51527. RSA_get0_factors(NULL, NULL, NULL);
  51528. RSA_get0_factors(NULL, &p, &q);
  51529. ExpectNull(p);
  51530. ExpectNull(q);
  51531. ExpectIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  51532. ExpectIntEQ(RSA_bits(rsa), 21);
  51533. RSA_free(rsa);
  51534. rsa = NULL;
  51535. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  51536. ExpectNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  51537. ExpectIntEQ(RSA_size(rsa), 384);
  51538. ExpectIntEQ(RSA_bits(rsa), 3072);
  51539. RSA_free(rsa);
  51540. rsa = NULL;
  51541. #endif
  51542. /* remove for now with odd key size until adjusting rsa key size check with
  51543. wc_MakeRsaKey()
  51544. ExpectNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  51545. RSA_free(rsa);
  51546. rsa = NULL;
  51547. */
  51548. ExpectNull(RSA_generate_key(-1, 3, NULL, NULL));
  51549. ExpectNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  51550. ExpectNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  51551. ExpectNull(RSA_generate_key(2048, 0, NULL, NULL));
  51552. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  51553. {
  51554. byte buff[FOURK_BUF];
  51555. byte der[FOURK_BUF];
  51556. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  51557. XFILE f = XBADFILE;
  51558. int bytes = 0;
  51559. /* test loading encrypted RSA private pem w/o password */
  51560. ExpectTrue((f = XFOPEN(PrivKeyPemFile, "rb")) != XBADFILE);
  51561. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  51562. if (f != XBADFILE)
  51563. XFCLOSE(f);
  51564. XMEMSET(der, 0, sizeof(der));
  51565. /* test that error value is returned with no password */
  51566. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""),
  51567. 0);
  51568. }
  51569. #endif
  51570. #endif
  51571. return EXPECT_RESULT();
  51572. }
  51573. static int test_wolfSSL_RSA_DER(void)
  51574. {
  51575. EXPECT_DECLS;
  51576. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  51577. RSA *rsa = NULL;
  51578. int i;
  51579. const unsigned char *buff = NULL;
  51580. unsigned char *newBuff = NULL;
  51581. struct tbl_s
  51582. {
  51583. const unsigned char *der;
  51584. int sz;
  51585. } tbl[] = {
  51586. #ifdef USE_CERT_BUFFERS_1024
  51587. {client_key_der_1024, sizeof_client_key_der_1024},
  51588. {server_key_der_1024, sizeof_server_key_der_1024},
  51589. #endif
  51590. #ifdef USE_CERT_BUFFERS_2048
  51591. {client_key_der_2048, sizeof_client_key_der_2048},
  51592. {server_key_der_2048, sizeof_server_key_der_2048},
  51593. #endif
  51594. {NULL, 0}
  51595. };
  51596. /* Public Key DER */
  51597. struct tbl_s pub[] = {
  51598. #ifdef USE_CERT_BUFFERS_1024
  51599. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  51600. #endif
  51601. #ifdef USE_CERT_BUFFERS_2048
  51602. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  51603. #endif
  51604. {NULL, 0}
  51605. };
  51606. ExpectNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  51607. buff = pub[0].der;
  51608. ExpectNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  51609. ExpectNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  51610. buff = tbl[0].der;
  51611. ExpectNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  51612. ExpectIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  51613. rsa = RSA_new();
  51614. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  51615. RSA_free(rsa);
  51616. rsa = NULL;
  51617. for (i = 0; tbl[i].der != NULL; i++)
  51618. {
  51619. /* Passing in pointer results in pointer moving. */
  51620. buff = tbl[i].der;
  51621. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  51622. ExpectNotNull(rsa);
  51623. RSA_free(rsa);
  51624. rsa = NULL;
  51625. }
  51626. for (i = 0; tbl[i].der != NULL; i++)
  51627. {
  51628. /* Passing in pointer results in pointer moving. */
  51629. buff = tbl[i].der;
  51630. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  51631. ExpectNotNull(rsa);
  51632. RSA_free(rsa);
  51633. rsa = NULL;
  51634. }
  51635. for (i = 0; pub[i].der != NULL; i++)
  51636. {
  51637. buff = pub[i].der;
  51638. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  51639. ExpectNotNull(rsa);
  51640. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  51641. newBuff = NULL;
  51642. ExpectIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  51643. ExpectNotNull(newBuff);
  51644. ExpectIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  51645. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51646. RSA_free(rsa);
  51647. rsa = NULL;
  51648. }
  51649. #endif
  51650. return EXPECT_RESULT();
  51651. }
  51652. static int test_wolfSSL_RSA_print(void)
  51653. {
  51654. EXPECT_DECLS;
  51655. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  51656. !defined(NO_STDIO_FILESYSTEM) && \
  51657. !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  51658. !defined(NO_BIO) && defined(XFPRINTF)
  51659. BIO *bio = NULL;
  51660. WOLFSSL_RSA* rsa = NULL;
  51661. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  51662. ExpectNotNull(rsa = RSA_new());
  51663. ExpectIntEQ(RSA_print(NULL, rsa, 0), -1);
  51664. ExpectIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  51665. ExpectIntEQ(RSA_print(bio, NULL, 0), -1);
  51666. ExpectIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
  51667. /* Some very large number of indent spaces. */
  51668. ExpectIntEQ(RSA_print(bio, rsa, 128), -1);
  51669. /* RSA is empty. */
  51670. ExpectIntEQ(RSA_print(bio, rsa, 0), 0);
  51671. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
  51672. RSA_free(rsa);
  51673. rsa = NULL;
  51674. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  51675. ExpectIntEQ(RSA_print(bio, rsa, 0), 1);
  51676. ExpectIntEQ(RSA_print(bio, rsa, 4), 1);
  51677. ExpectIntEQ(RSA_print(bio, rsa, -1), 1);
  51678. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
  51679. ExpectIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
  51680. ExpectIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
  51681. BIO_free(bio);
  51682. RSA_free(rsa);
  51683. #endif
  51684. return EXPECT_RESULT();
  51685. }
  51686. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  51687. {
  51688. EXPECT_DECLS;
  51689. #ifndef NO_RSA
  51690. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  51691. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  51692. RSA *rsa = NULL;
  51693. const unsigned char *derBuf = client_key_der_2048;
  51694. unsigned char em[256] = {0}; /* len = 2048/8 */
  51695. /* Random data simulating a hash */
  51696. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  51697. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  51698. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  51699. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  51700. };
  51701. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  51702. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  51703. RSA_PSS_SALTLEN_DIGEST), 0);
  51704. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  51705. RSA_PSS_SALTLEN_DIGEST), 0);
  51706. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  51707. RSA_PSS_SALTLEN_DIGEST), 0);
  51708. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  51709. RSA_PSS_SALTLEN_DIGEST), 0);
  51710. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  51711. ExpectIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  51712. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  51713. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  51714. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  51715. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  51716. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  51717. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  51718. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  51719. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  51720. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  51721. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  51722. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  51723. RSA_PSS_SALTLEN_DIGEST), 1);
  51724. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  51725. RSA_PSS_SALTLEN_DIGEST), 1);
  51726. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  51727. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  51728. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  51729. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  51730. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  51731. RSA_PSS_SALTLEN_MAX), 1);
  51732. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  51733. RSA_PSS_SALTLEN_MAX), 1);
  51734. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  51735. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  51736. RSA_free(rsa);
  51737. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  51738. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  51739. #endif
  51740. return EXPECT_RESULT();
  51741. }
  51742. static int test_wolfSSL_RSA_sign_sha3(void)
  51743. {
  51744. EXPECT_DECLS;
  51745. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  51746. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  51747. RSA* rsa = NULL;
  51748. const unsigned char *derBuf = client_key_der_2048;
  51749. unsigned char sigRet[256] = {0};
  51750. unsigned int sigLen = sizeof(sigRet);
  51751. /* Random data simulating a hash */
  51752. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  51753. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  51754. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  51755. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  51756. };
  51757. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  51758. ExpectIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet, &sigLen,
  51759. rsa), 1);
  51760. RSA_free(rsa);
  51761. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  51762. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  51763. return EXPECT_RESULT();
  51764. }
  51765. static int test_wolfSSL_RSA_get0_key(void)
  51766. {
  51767. EXPECT_DECLS;
  51768. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51769. RSA *rsa = NULL;
  51770. const BIGNUM* n = NULL;
  51771. const BIGNUM* e = NULL;
  51772. const BIGNUM* d = NULL;
  51773. const unsigned char* der;
  51774. int derSz;
  51775. #ifdef USE_CERT_BUFFERS_1024
  51776. der = client_key_der_1024;
  51777. derSz = sizeof_client_key_der_1024;
  51778. #elif defined(USE_CERT_BUFFERS_2048)
  51779. der = client_key_der_2048;
  51780. derSz = sizeof_client_key_der_2048;
  51781. #else
  51782. der = NULL;
  51783. derSz = 0;
  51784. #endif
  51785. if (der != NULL) {
  51786. RSA_get0_key(NULL, NULL, NULL, NULL);
  51787. RSA_get0_key(rsa, NULL, NULL, NULL);
  51788. RSA_get0_key(NULL, &n, &e, &d);
  51789. ExpectNull(n);
  51790. ExpectNull(e);
  51791. ExpectNull(d);
  51792. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  51793. ExpectNotNull(rsa);
  51794. RSA_get0_key(rsa, NULL, NULL, NULL);
  51795. RSA_get0_key(rsa, &n, NULL, NULL);
  51796. ExpectNotNull(n);
  51797. RSA_get0_key(rsa, NULL, &e, NULL);
  51798. ExpectNotNull(e);
  51799. RSA_get0_key(rsa, NULL, NULL, &d);
  51800. ExpectNotNull(d);
  51801. RSA_get0_key(rsa, &n, &e, &d);
  51802. ExpectNotNull(n);
  51803. ExpectNotNull(e);
  51804. ExpectNotNull(d);
  51805. RSA_free(rsa);
  51806. }
  51807. #endif
  51808. return EXPECT_RESULT();
  51809. }
  51810. static int test_wolfSSL_RSA_meth(void)
  51811. {
  51812. EXPECT_DECLS;
  51813. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51814. RSA *rsa = NULL;
  51815. RSA_METHOD *rsa_meth = NULL;
  51816. #ifdef WOLFSSL_KEY_GEN
  51817. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  51818. RSA_free(rsa);
  51819. rsa = NULL;
  51820. #else
  51821. ExpectNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  51822. #endif
  51823. ExpectNotNull(RSA_get_default_method());
  51824. wolfSSL_RSA_meth_free(NULL);
  51825. ExpectNull(wolfSSL_RSA_meth_new(NULL, 0));
  51826. ExpectNotNull(rsa_meth = RSA_meth_new("placeholder RSA method",
  51827. RSA_METHOD_FLAG_NO_CHECK));
  51828. #ifndef NO_WOLFSSL_STUB
  51829. ExpectIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  51830. ExpectIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  51831. ExpectIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  51832. ExpectIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  51833. ExpectIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  51834. ExpectIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  51835. ExpectIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  51836. #endif
  51837. ExpectIntEQ(RSA_flags(NULL), 0);
  51838. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  51839. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  51840. ExpectIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  51841. ExpectNotNull(rsa = RSA_new());
  51842. /* No method set. */
  51843. ExpectIntEQ(RSA_flags(rsa), 0);
  51844. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  51845. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  51846. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  51847. ExpectIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  51848. ExpectIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  51849. if (EXPECT_FAIL()) {
  51850. wolfSSL_RSA_meth_free(rsa_meth);
  51851. }
  51852. ExpectNull(RSA_get_method(NULL));
  51853. ExpectPtrEq(RSA_get_method(rsa), rsa_meth);
  51854. ExpectIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  51855. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  51856. ExpectIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  51857. ExpectIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  51858. RSA_METHOD_FLAG_NO_CHECK);
  51859. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  51860. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  51861. ExpectIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  51862. /* rsa_meth is freed here */
  51863. RSA_free(rsa);
  51864. #endif
  51865. return EXPECT_RESULT();
  51866. }
  51867. static int test_wolfSSL_RSA_verify(void)
  51868. {
  51869. EXPECT_DECLS;
  51870. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  51871. #ifndef NO_BIO
  51872. XFILE fp = XBADFILE;
  51873. RSA *pKey = NULL;
  51874. RSA *pubKey = NULL;
  51875. X509 *cert = NULL;
  51876. const char *text = "Hello wolfSSL !";
  51877. unsigned char hash[SHA256_DIGEST_LENGTH];
  51878. unsigned char signature[2048/8];
  51879. unsigned int signatureLength;
  51880. byte *buf = NULL;
  51881. BIO *bio = NULL;
  51882. SHA256_CTX c;
  51883. EVP_PKEY *evpPkey = NULL;
  51884. EVP_PKEY *evpPubkey = NULL;
  51885. size_t sz;
  51886. /* generate hash */
  51887. SHA256_Init(&c);
  51888. SHA256_Update(&c, text, strlen(text));
  51889. SHA256_Final(hash, &c);
  51890. #ifdef WOLFSSL_SMALL_STACK_CACHE
  51891. /* workaround for small stack cache case */
  51892. wc_Sha256Free((wc_Sha256*)&c);
  51893. #endif
  51894. /* read privete key file */
  51895. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  51896. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_END), 0);
  51897. ExpectTrue((sz = XFTELL(fp)) > 0);
  51898. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_SET), 0);
  51899. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  51900. ExpectIntEQ(XFREAD(buf, 1, sz, fp), sz);
  51901. if (fp != XBADFILE) {
  51902. XFCLOSE(fp);
  51903. fp = XBADFILE;
  51904. }
  51905. /* read private key and sign hash data */
  51906. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  51907. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  51908. ExpectNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  51909. ExpectIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  51910. signature, &signatureLength, pKey), SSL_SUCCESS);
  51911. /* read public key and verify signed data */
  51912. ExpectTrue((fp = XFOPEN(svrCertFile,"rb")) != XBADFILE);
  51913. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  51914. if (fp != XBADFILE)
  51915. XFCLOSE(fp);
  51916. ExpectNotNull(evpPubkey = X509_get_pubkey(cert));
  51917. ExpectNotNull(pubKey = EVP_PKEY_get1_RSA(evpPubkey));
  51918. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  51919. signatureLength, pubKey), SSL_SUCCESS);
  51920. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  51921. signatureLength, NULL), SSL_FAILURE);
  51922. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  51923. signatureLength, pubKey), SSL_FAILURE);
  51924. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  51925. signatureLength, pubKey), SSL_FAILURE);
  51926. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  51927. signatureLength, NULL), SSL_FAILURE);
  51928. RSA_free(pKey);
  51929. EVP_PKEY_free(evpPkey);
  51930. RSA_free(pubKey);
  51931. EVP_PKEY_free(evpPubkey);
  51932. X509_free(cert);
  51933. BIO_free(bio);
  51934. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  51935. #endif
  51936. #endif
  51937. return EXPECT_RESULT();
  51938. }
  51939. static int test_wolfSSL_RSA_sign(void)
  51940. {
  51941. EXPECT_DECLS;
  51942. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51943. RSA *rsa;
  51944. unsigned char hash[SHA256_DIGEST_LENGTH];
  51945. #ifdef USE_CERT_BUFFERS_1024
  51946. const unsigned char* privDer = client_key_der_1024;
  51947. size_t privDerSz = sizeof_client_key_der_1024;
  51948. const unsigned char* pubDer = client_keypub_der_1024;
  51949. size_t pubDerSz = sizeof_client_keypub_der_1024;
  51950. unsigned char signature[1024/8];
  51951. #else
  51952. const unsigned char* privDer = client_key_der_2048;
  51953. size_t privDerSz = sizeof_client_key_der_2048;
  51954. const unsigned char* pubDer = client_keypub_der_2048;
  51955. size_t pubDerSz = sizeof_client_keypub_der_2048;
  51956. unsigned char signature[2048/8];
  51957. #endif
  51958. unsigned int signatureLen;
  51959. const unsigned char* der;
  51960. XMEMSET(hash, 0, sizeof(hash));
  51961. der = privDer;
  51962. rsa = NULL;
  51963. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51964. /* Invalid parameters. */
  51965. ExpectIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  51966. ExpectIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  51967. &signatureLen, rsa), 0);
  51968. ExpectIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  51969. &signatureLen, rsa), 0);
  51970. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  51971. &signatureLen, rsa), 0);
  51972. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  51973. NULL, rsa), 0);
  51974. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  51975. &signatureLen, NULL), 0);
  51976. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  51977. &signatureLen, rsa), 1);
  51978. RSA_free(rsa);
  51979. der = pubDer;
  51980. rsa = NULL;
  51981. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  51982. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  51983. signatureLen, rsa), 1);
  51984. RSA_free(rsa);
  51985. #endif
  51986. return EXPECT_RESULT();
  51987. }
  51988. static int test_wolfSSL_RSA_sign_ex(void)
  51989. {
  51990. EXPECT_DECLS;
  51991. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51992. RSA *rsa = NULL;
  51993. unsigned char hash[SHA256_DIGEST_LENGTH];
  51994. #ifdef USE_CERT_BUFFERS_1024
  51995. const unsigned char* privDer = client_key_der_1024;
  51996. size_t privDerSz = sizeof_client_key_der_1024;
  51997. const unsigned char* pubDer = client_keypub_der_1024;
  51998. size_t pubDerSz = sizeof_client_keypub_der_1024;
  51999. unsigned char signature[1024/8];
  52000. #else
  52001. const unsigned char* privDer = client_key_der_2048;
  52002. size_t privDerSz = sizeof_client_key_der_2048;
  52003. const unsigned char* pubDer = client_keypub_der_2048;
  52004. size_t pubDerSz = sizeof_client_keypub_der_2048;
  52005. unsigned char signature[2048/8];
  52006. #endif
  52007. unsigned int signatureLen;
  52008. const unsigned char* der;
  52009. unsigned char encodedHash[51];
  52010. unsigned int encodedHashLen;
  52011. const unsigned char expEncHash[] = {
  52012. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  52013. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  52014. 0x00, 0x04, 0x20,
  52015. /* Hash data */
  52016. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52017. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52018. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52019. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52020. };
  52021. XMEMSET(hash, 0, sizeof(hash));
  52022. ExpectNotNull(rsa = wolfSSL_RSA_new());
  52023. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52024. &signatureLen, rsa, 1), 0);
  52025. wolfSSL_RSA_free(rsa);
  52026. der = privDer;
  52027. rsa = NULL;
  52028. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52029. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  52030. -1), 0);
  52031. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  52032. signature, &signatureLen, rsa, 1), 0);
  52033. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  52034. &signatureLen, rsa, 1), 0);
  52035. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  52036. &signatureLen, rsa, 1), 0);
  52037. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52038. NULL, rsa, 1), 0);
  52039. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52040. &signatureLen, NULL, 1), 0);
  52041. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52042. &signatureLen, rsa, -1), 0);
  52043. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  52044. &signatureLen, rsa, 0), 0);
  52045. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  52046. &signatureLen, rsa, 0), 0);
  52047. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52048. NULL, rsa, 0), 0);
  52049. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52050. &signatureLen, rsa, 1), 1);
  52051. /* Test returning encoded hash. */
  52052. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  52053. &encodedHashLen, rsa, 0), 1);
  52054. ExpectIntEQ(encodedHashLen, sizeof(expEncHash));
  52055. ExpectIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  52056. RSA_free(rsa);
  52057. der = pubDer;
  52058. rsa = NULL;
  52059. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  52060. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  52061. signatureLen, rsa), 1);
  52062. RSA_free(rsa);
  52063. #endif
  52064. return EXPECT_RESULT();
  52065. }
  52066. static int test_wolfSSL_RSA_public_decrypt(void)
  52067. {
  52068. EXPECT_DECLS;
  52069. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52070. RSA *rsa;
  52071. unsigned char msg[SHA256_DIGEST_LENGTH];
  52072. #ifdef USE_CERT_BUFFERS_1024
  52073. const unsigned char* pubDer = client_keypub_der_1024;
  52074. size_t pubDerSz = sizeof_client_keypub_der_1024;
  52075. unsigned char decMsg[1024/8];
  52076. const unsigned char encMsg[] = {
  52077. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  52078. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  52079. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  52080. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  52081. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  52082. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  52083. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  52084. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  52085. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  52086. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  52087. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  52088. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  52089. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  52090. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  52091. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  52092. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  52093. };
  52094. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  52095. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  52096. defined(WC_RSA_NO_PADDING)
  52097. const unsigned char encMsgNoPad[] = {
  52098. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  52099. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  52100. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  52101. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  52102. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  52103. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  52104. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  52105. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  52106. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  52107. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  52108. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  52109. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  52110. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  52111. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  52112. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  52113. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  52114. };
  52115. #endif
  52116. #else
  52117. const unsigned char* pubDer = client_keypub_der_2048;
  52118. size_t pubDerSz = sizeof_client_keypub_der_2048;
  52119. unsigned char decMsg[2048/8];
  52120. const unsigned char encMsg[] = {
  52121. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  52122. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  52123. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  52124. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  52125. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  52126. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  52127. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  52128. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  52129. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  52130. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  52131. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  52132. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  52133. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  52134. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  52135. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  52136. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  52137. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  52138. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  52139. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  52140. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  52141. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  52142. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  52143. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  52144. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  52145. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  52146. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  52147. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  52148. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  52149. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  52150. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  52151. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  52152. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  52153. };
  52154. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  52155. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  52156. defined(WC_RSA_NO_PADDING)
  52157. const unsigned char encMsgNoPad[] = {
  52158. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  52159. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  52160. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  52161. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  52162. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  52163. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  52164. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  52165. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  52166. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  52167. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  52168. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  52169. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  52170. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  52171. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  52172. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  52173. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  52174. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  52175. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  52176. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  52177. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  52178. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  52179. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  52180. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  52181. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  52182. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  52183. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  52184. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  52185. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  52186. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  52187. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  52188. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  52189. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  52190. };
  52191. #endif
  52192. #endif
  52193. const unsigned char* der;
  52194. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  52195. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  52196. defined(WC_RSA_NO_PADDING)
  52197. int i;
  52198. #endif
  52199. XMEMSET(msg, 0, sizeof(msg));
  52200. der = pubDer;
  52201. rsa = NULL;
  52202. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  52203. ExpectIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  52204. ExpectIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  52205. RSA_PKCS1_PADDING), -1);
  52206. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  52207. RSA_PKCS1_PADDING), -1);
  52208. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  52209. RSA_PKCS1_PADDING), -1);
  52210. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  52211. RSA_PKCS1_PADDING), -1);
  52212. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  52213. RSA_PKCS1_PSS_PADDING), -1);
  52214. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  52215. RSA_PKCS1_PADDING), 32);
  52216. ExpectIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  52217. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  52218. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  52219. defined(WC_RSA_NO_PADDING)
  52220. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  52221. rsa, RSA_NO_PADDING), sizeof(decMsg));
  52222. /* Zeros before actual data. */
  52223. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  52224. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  52225. }
  52226. /* Check actual data. */
  52227. XMEMSET(msg, 0x01, sizeof(msg));
  52228. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  52229. #endif
  52230. RSA_free(rsa);
  52231. #endif
  52232. return EXPECT_RESULT();
  52233. }
  52234. static int test_wolfSSL_RSA_private_encrypt(void)
  52235. {
  52236. EXPECT_DECLS;
  52237. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52238. RSA *rsa;
  52239. unsigned char msg[SHA256_DIGEST_LENGTH];
  52240. #ifdef USE_CERT_BUFFERS_1024
  52241. const unsigned char* privDer = client_key_der_1024;
  52242. size_t privDerSz = sizeof_client_key_der_1024;
  52243. unsigned char encMsg[1024/8];
  52244. const unsigned char expEncMsg[] = {
  52245. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  52246. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  52247. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  52248. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  52249. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  52250. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  52251. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  52252. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  52253. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  52254. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  52255. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  52256. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  52257. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  52258. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  52259. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  52260. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  52261. };
  52262. #ifdef WC_RSA_NO_PADDING
  52263. const unsigned char expEncMsgNoPad[] = {
  52264. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  52265. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  52266. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  52267. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  52268. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  52269. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  52270. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  52271. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  52272. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  52273. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  52274. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  52275. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  52276. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  52277. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  52278. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  52279. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  52280. };
  52281. #endif
  52282. #else
  52283. const unsigned char* privDer = client_key_der_2048;
  52284. size_t privDerSz = sizeof_client_key_der_2048;
  52285. unsigned char encMsg[2048/8];
  52286. const unsigned char expEncMsg[] = {
  52287. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  52288. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  52289. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  52290. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  52291. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  52292. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  52293. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  52294. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  52295. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  52296. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  52297. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  52298. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  52299. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  52300. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  52301. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  52302. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  52303. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  52304. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  52305. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  52306. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  52307. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  52308. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  52309. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  52310. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  52311. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  52312. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  52313. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  52314. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  52315. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  52316. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  52317. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  52318. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  52319. };
  52320. #ifdef WC_RSA_NO_PADDING
  52321. const unsigned char expEncMsgNoPad[] = {
  52322. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  52323. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  52324. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  52325. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  52326. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  52327. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  52328. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  52329. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  52330. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  52331. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  52332. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  52333. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  52334. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  52335. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  52336. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  52337. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  52338. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  52339. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  52340. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  52341. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  52342. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  52343. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  52344. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  52345. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  52346. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  52347. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  52348. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  52349. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  52350. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  52351. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  52352. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  52353. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  52354. };
  52355. #endif
  52356. #endif
  52357. const unsigned char* der;
  52358. XMEMSET(msg, 0x00, sizeof(msg));
  52359. der = privDer;
  52360. rsa = NULL;
  52361. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52362. ExpectIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  52363. ExpectIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  52364. -1);
  52365. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  52366. RSA_PKCS1_PADDING), -1);
  52367. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  52368. RSA_PKCS1_PADDING), -1);
  52369. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  52370. RSA_PKCS1_PADDING), -1);
  52371. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  52372. RSA_PKCS1_PSS_PADDING), -1);
  52373. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  52374. RSA_PKCS1_PADDING), sizeof(encMsg));
  52375. ExpectIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  52376. #ifdef WC_RSA_NO_PADDING
  52377. /* Non-zero message. */
  52378. XMEMSET(msg, 0x01, sizeof(msg));
  52379. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  52380. RSA_NO_PADDING), sizeof(encMsg));
  52381. ExpectIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  52382. #endif
  52383. RSA_free(rsa);
  52384. #endif
  52385. return EXPECT_RESULT();
  52386. }
  52387. static int test_wolfSSL_RSA_public_encrypt(void)
  52388. {
  52389. EXPECT_DECLS;
  52390. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52391. RSA* rsa = NULL;
  52392. const unsigned char msg[2048/8] = { 0 };
  52393. unsigned char encMsg[2048/8];
  52394. ExpectNotNull(rsa = RSA_new());
  52395. ExpectIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  52396. RSA_PKCS1_PADDING), -1);
  52397. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  52398. RSA_PKCS1_PADDING), -1);
  52399. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  52400. RSA_PKCS1_PADDING), -1);
  52401. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  52402. RSA_PKCS1_PADDING), -1);
  52403. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  52404. RSA_PKCS1_PSS_PADDING), -1);
  52405. /* Empty RSA key. */
  52406. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  52407. RSA_PKCS1_PADDING), -1);
  52408. RSA_free(rsa);
  52409. #endif
  52410. return EXPECT_RESULT();
  52411. }
  52412. static int test_wolfSSL_RSA_private_decrypt(void)
  52413. {
  52414. EXPECT_DECLS;
  52415. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52416. RSA* rsa = NULL;
  52417. unsigned char msg[2048/8];
  52418. const unsigned char encMsg[2048/8] = { 0 };
  52419. ExpectNotNull(rsa = RSA_new());
  52420. ExpectIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  52421. RSA_PKCS1_PADDING), -1);
  52422. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  52423. RSA_PKCS1_PADDING), -1);
  52424. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  52425. RSA_PKCS1_PADDING), -1);
  52426. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  52427. RSA_PKCS1_PADDING), -1);
  52428. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  52429. RSA_PKCS1_PSS_PADDING), -1);
  52430. /* Empty RSA key. */
  52431. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  52432. RSA_PKCS1_PADDING), -1);
  52433. RSA_free(rsa);
  52434. #endif
  52435. return EXPECT_RESULT();
  52436. }
  52437. static int test_wolfSSL_RSA_GenAdd(void)
  52438. {
  52439. EXPECT_DECLS;
  52440. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52441. RSA *rsa;
  52442. #ifdef USE_CERT_BUFFERS_1024
  52443. const unsigned char* privDer = client_key_der_1024;
  52444. size_t privDerSz = sizeof_client_key_der_1024;
  52445. const unsigned char* pubDer = client_keypub_der_1024;
  52446. size_t pubDerSz = sizeof_client_keypub_der_1024;
  52447. #else
  52448. const unsigned char* privDer = client_key_der_2048;
  52449. size_t privDerSz = sizeof_client_key_der_2048;
  52450. const unsigned char* pubDer = client_keypub_der_2048;
  52451. size_t pubDerSz = sizeof_client_keypub_der_2048;
  52452. #endif
  52453. const unsigned char* der;
  52454. der = privDer;
  52455. rsa = NULL;
  52456. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52457. ExpectIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  52458. #ifndef RSA_LOW_MEM
  52459. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  52460. #else
  52461. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  52462. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  52463. #endif
  52464. RSA_free(rsa);
  52465. der = pubDer;
  52466. rsa = NULL;
  52467. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  52468. /* Need private values. */
  52469. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  52470. RSA_free(rsa);
  52471. #endif
  52472. return EXPECT_RESULT();
  52473. }
  52474. static int test_wolfSSL_RSA_blinding_on(void)
  52475. {
  52476. EXPECT_DECLS;
  52477. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  52478. RSA *rsa;
  52479. WOLFSSL_BN_CTX *bnCtx = NULL;
  52480. #ifdef USE_CERT_BUFFERS_1024
  52481. const unsigned char* privDer = client_key_der_1024;
  52482. size_t privDerSz = sizeof_client_key_der_1024;
  52483. #else
  52484. const unsigned char* privDer = client_key_der_2048;
  52485. size_t privDerSz = sizeof_client_key_der_2048;
  52486. #endif
  52487. const unsigned char* der;
  52488. der = privDer;
  52489. rsa = NULL;
  52490. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52491. ExpectNotNull(bnCtx = wolfSSL_BN_CTX_new());
  52492. /* Does nothing so all parameters are valid. */
  52493. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  52494. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  52495. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  52496. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  52497. wolfSSL_BN_CTX_free(bnCtx);
  52498. RSA_free(rsa);
  52499. #endif
  52500. return EXPECT_RESULT();
  52501. }
  52502. static int test_wolfSSL_RSA_ex_data(void)
  52503. {
  52504. EXPECT_DECLS;
  52505. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  52506. RSA* rsa = NULL;
  52507. unsigned char data[1];
  52508. ExpectNotNull(rsa = RSA_new());
  52509. ExpectNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  52510. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  52511. #ifdef MAX_EX_DATA
  52512. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  52513. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  52514. #endif
  52515. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  52516. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  52517. #ifdef HAVE_EX_DATA
  52518. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  52519. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  52520. ExpectPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  52521. #else
  52522. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  52523. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  52524. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  52525. #endif
  52526. RSA_free(rsa);
  52527. #endif /* !NO_RSA && OPENSSL_EXTRA */
  52528. return EXPECT_RESULT();
  52529. }
  52530. static int test_wolfSSL_RSA_LoadDer(void)
  52531. {
  52532. EXPECT_DECLS;
  52533. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  52534. defined(OPENSSL_EXTRA_X509_SMALL))
  52535. RSA *rsa = NULL;
  52536. #ifdef USE_CERT_BUFFERS_1024
  52537. const unsigned char* privDer = client_key_der_1024;
  52538. size_t privDerSz = sizeof_client_key_der_1024;
  52539. #else
  52540. const unsigned char* privDer = client_key_der_2048;
  52541. size_t privDerSz = sizeof_client_key_der_2048;
  52542. #endif
  52543. ExpectNotNull(rsa = RSA_new());
  52544. ExpectIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  52545. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  52546. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  52547. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  52548. RSA_free(rsa);
  52549. #endif /* !NO_RSA && OPENSSL_EXTRA */
  52550. return EXPECT_RESULT();
  52551. }
  52552. /* Local API. */
  52553. static int test_wolfSSL_RSA_To_Der(void)
  52554. {
  52555. EXPECT_DECLS;
  52556. #ifdef WOLFSSL_TEST_STATIC_BUILD
  52557. #if defined(WOLFSSL_KEY_GEN) && defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52558. RSA* rsa;
  52559. #ifdef USE_CERT_BUFFERS_1024
  52560. const unsigned char* privDer = client_key_der_1024;
  52561. size_t privDerSz = sizeof_client_key_der_1024;
  52562. const unsigned char* pubDer = client_keypub_der_1024;
  52563. size_t pubDerSz = sizeof_client_keypub_der_1024;
  52564. unsigned char out[sizeof(client_key_der_1024)];
  52565. #else
  52566. const unsigned char* privDer = client_key_der_2048;
  52567. size_t privDerSz = sizeof_client_key_der_2048;
  52568. const unsigned char* pubDer = client_keypub_der_2048;
  52569. size_t pubDerSz = sizeof_client_keypub_der_2048;
  52570. unsigned char out[sizeof(client_key_der_2048)];
  52571. #endif
  52572. const unsigned char* der;
  52573. unsigned char* outDer = NULL;
  52574. der = privDer;
  52575. rsa = NULL;
  52576. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52577. ExpectIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  52578. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  52579. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  52580. outDer = out;
  52581. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  52582. ExpectIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  52583. outDer = NULL;
  52584. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  52585. ExpectNotNull(outDer);
  52586. ExpectIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  52587. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  52588. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  52589. outDer = out;
  52590. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  52591. ExpectIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  52592. RSA_free(rsa);
  52593. ExpectNotNull(rsa = RSA_new());
  52594. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  52595. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  52596. RSA_free(rsa);
  52597. der = pubDer;
  52598. rsa = NULL;
  52599. ExpectNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  52600. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  52601. RSA_free(rsa);
  52602. #endif
  52603. #endif
  52604. return EXPECT_RESULT();
  52605. }
  52606. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  52607. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  52608. {
  52609. EXPECT_DECLS;
  52610. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  52611. XFILE file = XBADFILE;
  52612. const char* fname = "./certs/server-keyPub.pem";
  52613. RSA *rsa = NULL;
  52614. ExpectNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  52615. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  52616. ExpectNotNull(rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL));
  52617. ExpectIntEQ(RSA_size(rsa), 256);
  52618. RSA_free(rsa);
  52619. if (file != XBADFILE)
  52620. XFCLOSE(file);
  52621. #endif
  52622. return EXPECT_RESULT();
  52623. }
  52624. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  52625. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  52626. {
  52627. EXPECT_DECLS;
  52628. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  52629. defined(WOLFSSL_KEY_GEN)
  52630. RSA* rsa = NULL;
  52631. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  52632. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
  52633. /* Valid but stub so returns 0. */
  52634. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
  52635. #endif
  52636. return EXPECT_RESULT();
  52637. }
  52638. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  52639. {
  52640. EXPECT_DECLS;
  52641. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  52642. (defined(WOLFSSL_PEM_TO_DER) || \
  52643. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  52644. RSA* rsa = NULL;
  52645. #ifdef USE_CERT_BUFFERS_1024
  52646. const unsigned char* privDer = client_key_der_1024;
  52647. size_t privDerSz = sizeof_client_key_der_1024;
  52648. #else
  52649. const unsigned char* privDer = client_key_der_2048;
  52650. size_t privDerSz = sizeof_client_key_der_2048;
  52651. #endif
  52652. const unsigned char* der;
  52653. #ifndef NO_AES
  52654. unsigned char passwd[] = "password";
  52655. #endif
  52656. ExpectNotNull(rsa = RSA_new());
  52657. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  52658. NULL, NULL), 0);
  52659. RSA_free(rsa);
  52660. der = privDer;
  52661. rsa = NULL;
  52662. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52663. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  52664. NULL, NULL), 0);
  52665. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
  52666. NULL, NULL), 0);
  52667. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  52668. NULL, NULL), 1);
  52669. #ifndef NO_AES
  52670. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  52671. NULL, 0, NULL, NULL), 1);
  52672. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  52673. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  52674. #endif
  52675. RSA_free(rsa);
  52676. #endif
  52677. return EXPECT_RESULT();
  52678. }
  52679. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  52680. {
  52681. EXPECT_DECLS;
  52682. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  52683. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  52684. RSA* rsa = NULL;
  52685. #ifdef USE_CERT_BUFFERS_1024
  52686. const unsigned char* privDer = client_key_der_1024;
  52687. size_t privDerSz = sizeof_client_key_der_1024;
  52688. #else
  52689. const unsigned char* privDer = client_key_der_2048;
  52690. size_t privDerSz = sizeof_client_key_der_2048;
  52691. #endif
  52692. const unsigned char* der;
  52693. #ifndef NO_AES
  52694. unsigned char passwd[] = "password";
  52695. #endif
  52696. unsigned char* pem = NULL;
  52697. int plen;
  52698. ExpectNotNull(rsa = RSA_new());
  52699. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  52700. &plen), 0);
  52701. RSA_free(rsa);
  52702. der = privDer;
  52703. rsa = NULL;
  52704. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52705. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  52706. &plen), 0);
  52707. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  52708. &plen), 0);
  52709. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  52710. NULL), 0);
  52711. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  52712. &plen), 1);
  52713. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  52714. pem = NULL;
  52715. #ifndef NO_AES
  52716. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  52717. NULL, 0, &pem, &plen), 1);
  52718. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  52719. pem = NULL;
  52720. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  52721. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  52722. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  52723. #endif
  52724. RSA_free(rsa);
  52725. #endif
  52726. return EXPECT_RESULT();
  52727. }
  52728. static int test_wolfSSL_DH(void)
  52729. {
  52730. EXPECT_DECLS;
  52731. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  52732. DH *dh = NULL;
  52733. BIGNUM* p;
  52734. BIGNUM* q;
  52735. BIGNUM* g;
  52736. BIGNUM* pub = NULL;
  52737. BIGNUM* priv = NULL;
  52738. #if defined(OPENSSL_ALL)
  52739. #if !defined(HAVE_FIPS) || \
  52740. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  52741. FILE* f = NULL;
  52742. unsigned char buf[268];
  52743. const unsigned char* pt = buf;
  52744. long len = 0;
  52745. dh = NULL;
  52746. XMEMSET(buf, 0, sizeof(buf));
  52747. /* Test 2048 bit parameters */
  52748. ExpectTrue((f = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  52749. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  52750. if (f != XBADFILE)
  52751. XFCLOSE(f);
  52752. ExpectNotNull(dh = d2i_DHparams(NULL, &pt, len));
  52753. ExpectNotNull(dh->p);
  52754. ExpectNotNull(dh->g);
  52755. ExpectTrue(pt == buf);
  52756. ExpectIntEQ(DH_generate_key(dh), 1);
  52757. ExpectIntEQ(DH_generate_key(dh), 1);
  52758. ExpectIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  52759. ExpectNotNull(pub = BN_new());
  52760. ExpectIntEQ(BN_set_word(pub, 1), 1);
  52761. ExpectIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  52762. ExpectIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  52763. ExpectIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  52764. ExpectIntEQ(DH_compute_key(buf, pub, NULL), -1);
  52765. ExpectIntEQ(DH_compute_key(buf, NULL, dh), -1);
  52766. ExpectIntEQ(DH_compute_key(NULL, pub, dh), -1);
  52767. ExpectIntEQ(DH_compute_key(buf, pub, dh), -1);
  52768. BN_free(pub);
  52769. pub = NULL;
  52770. DH_get0_pqg(dh, (const BIGNUM**)&p,
  52771. (const BIGNUM**)&q,
  52772. (const BIGNUM**)&g);
  52773. ExpectPtrEq(p, dh->p);
  52774. ExpectPtrEq(q, dh->q);
  52775. ExpectPtrEq(g, dh->g);
  52776. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  52777. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  52778. ExpectPtrEq(pub, dh->pub_key);
  52779. ExpectPtrEq(priv, dh->priv_key);
  52780. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  52781. ExpectPtrEq(pub, dh->pub_key);
  52782. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  52783. ExpectPtrEq(priv, dh->priv_key);
  52784. pub = NULL;
  52785. priv = NULL;
  52786. ExpectNotNull(pub = BN_new());
  52787. ExpectNotNull(priv = BN_new());
  52788. ExpectIntEQ(DH_set0_key(NULL, pub, priv), 0);
  52789. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  52790. if (EXPECT_FAIL()) {
  52791. BN_free(pub);
  52792. BN_free(priv);
  52793. }
  52794. pub = NULL;
  52795. priv = NULL;
  52796. ExpectNotNull(pub = BN_new());
  52797. ExpectIntEQ(DH_set0_key(dh, pub, NULL), 1);
  52798. if (EXPECT_FAIL()) {
  52799. BN_free(pub);
  52800. }
  52801. ExpectNotNull(priv = BN_new());
  52802. ExpectIntEQ(DH_set0_key(dh, NULL, priv), 1);
  52803. if (EXPECT_FAIL()) {
  52804. BN_free(priv);
  52805. }
  52806. ExpectPtrEq(pub, dh->pub_key);
  52807. ExpectPtrEq(priv, dh->priv_key);
  52808. pub = NULL;
  52809. priv = NULL;
  52810. DH_free(dh);
  52811. dh = NULL;
  52812. ExpectNotNull(dh = DH_new());
  52813. p = NULL;
  52814. ExpectNotNull(p = BN_new());
  52815. ExpectIntEQ(BN_set_word(p, 1), 1);
  52816. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  52817. ExpectNotNull(pub = BN_new());
  52818. ExpectNotNull(priv = BN_new());
  52819. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  52820. if (EXPECT_FAIL()) {
  52821. BN_free(pub);
  52822. BN_free(priv);
  52823. }
  52824. pub = NULL;
  52825. priv = NULL;
  52826. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  52827. BN_free(p);
  52828. p = NULL;
  52829. DH_free(dh);
  52830. dh = NULL;
  52831. #ifdef WOLFSSL_KEY_GEN
  52832. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  52833. ExpectIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  52834. DH_free(dh);
  52835. dh = NULL;
  52836. #endif
  52837. #endif /* !HAVE_FIPS || (HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2) */
  52838. #endif /* OPENSSL_ALL */
  52839. (void)dh;
  52840. (void)p;
  52841. (void)q;
  52842. (void)g;
  52843. (void)pub;
  52844. (void)priv;
  52845. ExpectNotNull(dh = wolfSSL_DH_new());
  52846. /* invalid parameters test */
  52847. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  52848. (const BIGNUM**)&q,
  52849. (const BIGNUM**)&g);
  52850. DH_get0_pqg(dh, NULL,
  52851. (const BIGNUM**)&q,
  52852. (const BIGNUM**)&g);
  52853. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  52854. DH_get0_pqg(dh, NULL, NULL, NULL);
  52855. DH_get0_pqg(dh, (const BIGNUM**)&p,
  52856. (const BIGNUM**)&q,
  52857. (const BIGNUM**)&g);
  52858. ExpectPtrEq(p, NULL);
  52859. ExpectPtrEq(q, NULL);
  52860. ExpectPtrEq(g, NULL);
  52861. DH_free(dh);
  52862. dh = NULL;
  52863. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  52864. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  52865. #if defined(OPENSSL_ALL) || \
  52866. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  52867. dh = wolfSSL_DH_new();
  52868. ExpectNotNull(dh);
  52869. p = wolfSSL_BN_new();
  52870. ExpectNotNull(p);
  52871. ExpectIntEQ(BN_set_word(p, 11), 1);
  52872. g = wolfSSL_BN_new();
  52873. ExpectNotNull(g);
  52874. ExpectIntEQ(BN_set_word(g, 2), 1);
  52875. q = wolfSSL_BN_new();
  52876. ExpectNotNull(q);
  52877. ExpectIntEQ(BN_set_word(q, 5), 1);
  52878. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  52879. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  52880. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  52881. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  52882. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  52883. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  52884. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  52885. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  52886. /* Don't need q. */
  52887. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  52888. if (EXPECT_FAIL()) {
  52889. BN_free(p);
  52890. BN_free(g);
  52891. }
  52892. p = NULL;
  52893. g = NULL;
  52894. /* Setting again will free the p and g. */
  52895. wolfSSL_BN_free(q);
  52896. q = NULL;
  52897. DH_free(dh);
  52898. dh = NULL;
  52899. dh = wolfSSL_DH_new();
  52900. ExpectNotNull(dh);
  52901. p = wolfSSL_BN_new();
  52902. ExpectNotNull(p);
  52903. ExpectIntEQ(BN_set_word(p, 11), 1);
  52904. g = wolfSSL_BN_new();
  52905. ExpectNotNull(g);
  52906. ExpectIntEQ(BN_set_word(g, 2), 1);
  52907. q = wolfSSL_BN_new();
  52908. ExpectNotNull(q);
  52909. ExpectIntEQ(BN_set_word(q, 5), 1);
  52910. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  52911. /* p, q and g are now owned by dh - don't free. */
  52912. if (EXPECT_FAIL()) {
  52913. BN_free(p);
  52914. BN_free(q);
  52915. BN_free(g);
  52916. }
  52917. p = NULL;
  52918. q = NULL;
  52919. g = NULL;
  52920. p = wolfSSL_BN_new();
  52921. ExpectNotNull(p);
  52922. ExpectIntEQ(BN_set_word(p, 11), 1);
  52923. g = wolfSSL_BN_new();
  52924. ExpectNotNull(g);
  52925. ExpectIntEQ(BN_set_word(g, 2), 1);
  52926. q = wolfSSL_BN_new();
  52927. ExpectNotNull(q);
  52928. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  52929. if (EXPECT_FAIL()) {
  52930. BN_free(p);
  52931. }
  52932. p = NULL;
  52933. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  52934. if (EXPECT_FAIL()) {
  52935. BN_free(q);
  52936. }
  52937. q = NULL;
  52938. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  52939. if (EXPECT_FAIL()) {
  52940. BN_free(g);
  52941. }
  52942. g = NULL;
  52943. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  52944. /* p, q and g are now owned by dh - don't free. */
  52945. DH_free(dh);
  52946. dh = NULL;
  52947. ExpectIntEQ(DH_generate_key(NULL), 0);
  52948. ExpectNotNull(dh = DH_new());
  52949. ExpectIntEQ(DH_generate_key(dh), 0);
  52950. p = wolfSSL_BN_new();
  52951. ExpectNotNull(p);
  52952. ExpectIntEQ(BN_set_word(p, 0), 1);
  52953. g = wolfSSL_BN_new();
  52954. ExpectNotNull(g);
  52955. ExpectIntEQ(BN_set_word(g, 2), 1);
  52956. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  52957. if (EXPECT_FAIL()) {
  52958. BN_free(p);
  52959. BN_free(g);
  52960. }
  52961. p = NULL;
  52962. g = NULL;
  52963. ExpectIntEQ(DH_generate_key(dh), 0);
  52964. DH_free(dh);
  52965. dh = NULL;
  52966. #endif
  52967. #endif
  52968. /* Test DH_up_ref() */
  52969. dh = wolfSSL_DH_new();
  52970. ExpectNotNull(dh);
  52971. ExpectIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  52972. ExpectIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  52973. DH_free(dh); /* decrease ref count */
  52974. DH_free(dh); /* free WOLFSSL_DH */
  52975. dh = NULL;
  52976. q = NULL;
  52977. ExpectNull((dh = DH_new_by_nid(NID_sha1)));
  52978. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  52979. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  52980. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  52981. #ifdef HAVE_FFDHE_2048
  52982. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  52983. DH_free(dh);
  52984. dh = NULL;
  52985. q = NULL;
  52986. #endif
  52987. #ifdef HAVE_FFDHE_3072
  52988. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  52989. DH_free(dh);
  52990. dh = NULL;
  52991. q = NULL;
  52992. #endif
  52993. #ifdef HAVE_FFDHE_4096
  52994. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  52995. DH_free(dh);
  52996. dh = NULL;
  52997. q = NULL;
  52998. #endif
  52999. #else
  53000. ExpectNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  53001. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  53002. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  53003. ExpectIntEQ(wolfSSL_DH_size(NULL), -1);
  53004. #endif /* OPENSSL_EXTRA && !NO_DH */
  53005. return EXPECT_RESULT();
  53006. }
  53007. static int test_wolfSSL_DH_dup(void)
  53008. {
  53009. EXPECT_DECLS;
  53010. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  53011. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  53012. defined(OPENSSL_EXTRA)
  53013. DH *dh = NULL;
  53014. DH *dhDup = NULL;
  53015. ExpectNotNull(dh = wolfSSL_DH_new());
  53016. ExpectNull(dhDup = wolfSSL_DH_dup(NULL));
  53017. ExpectNull(dhDup = wolfSSL_DH_dup(dh));
  53018. #if defined(OPENSSL_ALL) || \
  53019. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  53020. {
  53021. WOLFSSL_BIGNUM* p = NULL;
  53022. WOLFSSL_BIGNUM* g = NULL;
  53023. ExpectNotNull(p = wolfSSL_BN_new());
  53024. ExpectNotNull(g = wolfSSL_BN_new());
  53025. ExpectIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  53026. ExpectIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  53027. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  53028. if (EXPECT_FAIL()) {
  53029. wolfSSL_BN_free(p);
  53030. wolfSSL_BN_free(g);
  53031. }
  53032. ExpectNotNull(dhDup = wolfSSL_DH_dup(dh));
  53033. wolfSSL_DH_free(dhDup);
  53034. }
  53035. #endif
  53036. wolfSSL_DH_free(dh);
  53037. #endif
  53038. #endif
  53039. return EXPECT_RESULT();
  53040. }
  53041. static int test_wolfSSL_DH_check(void)
  53042. {
  53043. EXPECT_DECLS;
  53044. #ifdef OPENSSL_ALL
  53045. #ifndef NO_DH
  53046. #ifndef NO_BIO
  53047. #ifndef NO_DSA
  53048. byte buf[6000];
  53049. char file[] = "./certs/dsaparams.pem";
  53050. XFILE f = XBADFILE;
  53051. int bytes;
  53052. BIO* bio = NULL;
  53053. DSA* dsa = NULL;
  53054. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  53055. static const byte dh2048[] = {
  53056. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  53057. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  53058. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  53059. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  53060. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  53061. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  53062. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  53063. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  53064. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  53065. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  53066. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  53067. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  53068. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  53069. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  53070. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  53071. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  53072. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  53073. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  53074. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  53075. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  53076. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  53077. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  53078. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  53079. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  53080. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  53081. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  53082. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  53083. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  53084. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  53085. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  53086. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  53087. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  53088. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  53089. 0x93, 0x02, 0x01, 0x02
  53090. };
  53091. const byte* params;
  53092. #endif
  53093. DH* dh = NULL;
  53094. WOLFSSL_BIGNUM* p = NULL;
  53095. WOLFSSL_BIGNUM* g = NULL;
  53096. WOLFSSL_BIGNUM* pTmp = NULL;
  53097. WOLFSSL_BIGNUM* gTmp = NULL;
  53098. int codes = -1;
  53099. #ifndef NO_DSA
  53100. /* Initialize DH */
  53101. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  53102. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  53103. if (f != XBADFILE)
  53104. XFCLOSE(f);
  53105. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  53106. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  53107. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  53108. ExpectNotNull(dh);
  53109. BIO_free(bio);
  53110. DSA_free(dsa);
  53111. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  53112. params = dh2048;
  53113. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &params,
  53114. (long)sizeof(dh2048)));
  53115. #else
  53116. ExpectNotNull(dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048));
  53117. #endif
  53118. /* Test assumed to be valid dh.
  53119. * Should return WOLFSSL_SUCCESS
  53120. * codes should be 0
  53121. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  53122. */
  53123. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  53124. ExpectIntEQ(codes, 0);
  53125. /* Test NULL dh: expected BAD_FUNC_ARG */
  53126. ExpectIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  53127. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  53128. if (dh != NULL) {
  53129. pTmp = dh->p;
  53130. dh->p = NULL;
  53131. }
  53132. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  53133. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  53134. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  53135. /* set dh->p back to normal so it won't fail on next tests */
  53136. if (dh != NULL) {
  53137. dh->p = pTmp;
  53138. pTmp = NULL;
  53139. }
  53140. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  53141. if (dh != NULL) {
  53142. gTmp = dh->g;
  53143. dh->g = NULL;
  53144. }
  53145. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  53146. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  53147. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  53148. if (dh != NULL) {
  53149. dh->g = gTmp;
  53150. gTmp = NULL;
  53151. }
  53152. /* Cleanup */
  53153. DH_free(dh);
  53154. dh = NULL;
  53155. dh = DH_new();
  53156. ExpectNotNull(dh);
  53157. /* Check empty DH. */
  53158. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  53159. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  53160. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  53161. /* Check non-prime valued p. */
  53162. ExpectNotNull(p = BN_new());
  53163. ExpectIntEQ(BN_set_word(p, 4), 1);
  53164. ExpectNotNull(g = BN_new());
  53165. ExpectIntEQ(BN_set_word(g, 2), 1);
  53166. ExpectIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  53167. if (EXPECT_FAIL()) {
  53168. wolfSSL_BN_free(p);
  53169. wolfSSL_BN_free(g);
  53170. }
  53171. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  53172. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  53173. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  53174. DH_free(dh);
  53175. dh = NULL;
  53176. #endif
  53177. #endif /* !NO_DH && !NO_DSA */
  53178. #endif
  53179. return EXPECT_RESULT();
  53180. }
  53181. static int test_wolfSSL_DH_prime(void)
  53182. {
  53183. EXPECT_DECLS;
  53184. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  53185. WOLFSSL_BIGNUM* bn = NULL;
  53186. #if WOLFSSL_MAX_BN_BITS >= 768
  53187. WOLFSSL_BIGNUM* bn2 = NULL;
  53188. #endif
  53189. bn = wolfSSL_DH_768_prime(NULL);
  53190. #if WOLFSSL_MAX_BN_BITS >= 768
  53191. ExpectNotNull(bn);
  53192. bn2 = wolfSSL_DH_768_prime(bn);
  53193. ExpectNotNull(bn2);
  53194. ExpectTrue(bn == bn2);
  53195. wolfSSL_BN_free(bn);
  53196. bn = NULL;
  53197. #else
  53198. ExpectNull(bn);
  53199. #endif
  53200. bn = wolfSSL_DH_1024_prime(NULL);
  53201. #if WOLFSSL_MAX_BN_BITS >= 1024
  53202. ExpectNotNull(bn);
  53203. wolfSSL_BN_free(bn);
  53204. bn = NULL;
  53205. #else
  53206. ExpectNull(bn);
  53207. #endif
  53208. bn = wolfSSL_DH_2048_prime(NULL);
  53209. #if WOLFSSL_MAX_BN_BITS >= 2048
  53210. ExpectNotNull(bn);
  53211. wolfSSL_BN_free(bn);
  53212. bn = NULL;
  53213. #else
  53214. ExpectNull(bn);
  53215. #endif
  53216. bn = wolfSSL_DH_3072_prime(NULL);
  53217. #if WOLFSSL_MAX_BN_BITS >= 3072
  53218. ExpectNotNull(bn);
  53219. wolfSSL_BN_free(bn);
  53220. bn = NULL;
  53221. #else
  53222. ExpectNull(bn);
  53223. #endif
  53224. bn = wolfSSL_DH_4096_prime(NULL);
  53225. #if WOLFSSL_MAX_BN_BITS >= 4096
  53226. ExpectNotNull(bn);
  53227. wolfSSL_BN_free(bn);
  53228. bn = NULL;
  53229. #else
  53230. ExpectNull(bn);
  53231. #endif
  53232. bn = wolfSSL_DH_6144_prime(NULL);
  53233. #if WOLFSSL_MAX_BN_BITS >= 6144
  53234. ExpectNotNull(bn);
  53235. wolfSSL_BN_free(bn);
  53236. bn = NULL;
  53237. #else
  53238. ExpectNull(bn);
  53239. #endif
  53240. bn = wolfSSL_DH_8192_prime(NULL);
  53241. #if WOLFSSL_MAX_BN_BITS >= 8192
  53242. ExpectNotNull(bn);
  53243. wolfSSL_BN_free(bn);
  53244. bn = NULL;
  53245. #else
  53246. ExpectNull(bn);
  53247. #endif
  53248. #endif
  53249. return EXPECT_RESULT();
  53250. }
  53251. static int test_wolfSSL_DH_1536_prime(void)
  53252. {
  53253. EXPECT_DECLS;
  53254. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  53255. BIGNUM* bn = NULL;
  53256. unsigned char bits[200];
  53257. int sz = 192; /* known binary size */
  53258. const byte expected[] = {
  53259. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  53260. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  53261. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  53262. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  53263. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  53264. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  53265. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  53266. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  53267. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  53268. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  53269. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  53270. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  53271. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  53272. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  53273. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  53274. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  53275. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  53276. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  53277. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  53278. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  53279. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  53280. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  53281. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  53282. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  53283. };
  53284. ExpectNotNull(bn = get_rfc3526_prime_1536(NULL));
  53285. ExpectIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  53286. ExpectIntEQ(0, XMEMCMP(expected, bits, sz));
  53287. BN_free(bn);
  53288. #endif
  53289. return EXPECT_RESULT();
  53290. }
  53291. static int test_wolfSSL_DH_get_2048_256(void)
  53292. {
  53293. EXPECT_DECLS;
  53294. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  53295. WOLFSSL_DH* dh = NULL;
  53296. const WOLFSSL_BIGNUM* pBn;
  53297. const WOLFSSL_BIGNUM* gBn;
  53298. const WOLFSSL_BIGNUM* qBn;
  53299. const byte pExpected[] = {
  53300. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  53301. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  53302. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  53303. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  53304. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  53305. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  53306. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  53307. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  53308. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  53309. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  53310. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  53311. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  53312. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  53313. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  53314. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  53315. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  53316. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  53317. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  53318. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  53319. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  53320. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  53321. 0x1E, 0x1A, 0x15, 0x97
  53322. };
  53323. const byte gExpected[] = {
  53324. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  53325. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  53326. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  53327. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  53328. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  53329. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  53330. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  53331. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  53332. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  53333. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  53334. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  53335. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  53336. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  53337. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  53338. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  53339. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  53340. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  53341. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  53342. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  53343. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  53344. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  53345. 0x6C, 0xC4, 0x16, 0x59
  53346. };
  53347. const byte qExpected[] = {
  53348. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  53349. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  53350. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  53351. };
  53352. int pSz = 0;
  53353. int qSz = 0;
  53354. int gSz = 0;
  53355. byte* pReturned = NULL;
  53356. byte* qReturned = NULL;
  53357. byte* gReturned = NULL;
  53358. ExpectNotNull((dh = wolfSSL_DH_get_2048_256()));
  53359. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  53360. ExpectIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  53361. ExpectNotNull(pReturned = (byte*)XMALLOC(pSz, NULL,
  53362. DYNAMIC_TYPE_TMP_BUFFER));
  53363. ExpectIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  53364. ExpectIntEQ(pSz, sizeof(pExpected));
  53365. ExpectIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  53366. ExpectIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  53367. ExpectNotNull(qReturned = (byte*)XMALLOC(qSz, NULL,
  53368. DYNAMIC_TYPE_TMP_BUFFER));
  53369. ExpectIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  53370. ExpectIntEQ(qSz, sizeof(qExpected));
  53371. ExpectIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  53372. ExpectIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  53373. ExpectNotNull(gReturned = (byte*)XMALLOC(gSz, NULL,
  53374. DYNAMIC_TYPE_TMP_BUFFER));
  53375. ExpectIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  53376. ExpectIntEQ(gSz, sizeof(gExpected));
  53377. ExpectIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  53378. wolfSSL_DH_free(dh);
  53379. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53380. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53381. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53382. #endif
  53383. return EXPECT_RESULT();
  53384. }
  53385. static int test_wolfSSL_PEM_write_DHparams(void)
  53386. {
  53387. EXPECT_DECLS;
  53388. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  53389. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  53390. DH* dh = NULL;
  53391. BIO* bio = NULL;
  53392. XFILE fp = XBADFILE;
  53393. byte pem[2048];
  53394. int pemSz = 0;
  53395. const char expected[] =
  53396. "-----BEGIN DH PARAMETERS-----\n"
  53397. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  53398. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  53399. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  53400. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  53401. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  53402. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  53403. "-----END DH PARAMETERS-----\n";
  53404. const char badPem[] =
  53405. "-----BEGIN DH PARAMETERS-----\n"
  53406. "-----END DH PARAMETERS-----\n";
  53407. const char emptySeqPem[] =
  53408. "-----BEGIN DH PARAMETERS-----\n"
  53409. "MAA=\n"
  53410. "-----END DH PARAMETERS-----\n";
  53411. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  53412. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  53413. if (fp != XBADFILE) {
  53414. XFCLOSE(fp);
  53415. fp = XBADFILE;
  53416. }
  53417. ExpectNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  53418. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  53419. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  53420. ExpectIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  53421. (int)sizeof(badPem));
  53422. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  53423. BIO_free(bio);
  53424. bio = NULL;
  53425. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  53426. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  53427. ExpectIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  53428. (int)sizeof(emptySeqPem));
  53429. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  53430. BIO_free(bio);
  53431. bio = NULL;
  53432. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  53433. ExpectIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  53434. ExpectNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  53435. BIO_free(bio);
  53436. bio = NULL;
  53437. ExpectNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  53438. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  53439. ExpectIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  53440. DH_free(dh);
  53441. dh = NULL;
  53442. dh = wolfSSL_DH_new();
  53443. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  53444. if (fp != XBADFILE) {
  53445. XFCLOSE(fp);
  53446. fp = XBADFILE;
  53447. }
  53448. wolfSSL_DH_free(dh);
  53449. dh = NULL;
  53450. /* check results */
  53451. XMEMSET(pem, 0, sizeof(pem));
  53452. ExpectTrue((fp = XFOPEN("./test-write-dhparams.pem", "rb")) != XBADFILE);
  53453. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  53454. ExpectIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  53455. if (fp != XBADFILE)
  53456. XFCLOSE(fp);
  53457. #endif
  53458. return EXPECT_RESULT();
  53459. }
  53460. static int test_wolfSSL_d2i_DHparams(void)
  53461. {
  53462. EXPECT_DECLS;
  53463. #ifdef OPENSSL_ALL
  53464. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  53465. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  53466. XFILE f = XBADFILE;
  53467. unsigned char buf[4096];
  53468. const unsigned char* pt = buf;
  53469. #ifdef HAVE_FFDHE_2048
  53470. const char* params1 = "./certs/dh2048.der";
  53471. #endif
  53472. #ifdef HAVE_FFDHE_3072
  53473. const char* params2 = "./certs/dh3072.der";
  53474. #endif
  53475. long len = 0;
  53476. WOLFSSL_DH* dh = NULL;
  53477. XMEMSET(buf, 0, sizeof(buf));
  53478. /* Test 2048 bit parameters */
  53479. #ifdef HAVE_FFDHE_2048
  53480. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  53481. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  53482. if (f != XBADFILE) {
  53483. XFCLOSE(f);
  53484. f = XBADFILE;
  53485. }
  53486. /* Valid case */
  53487. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  53488. ExpectNotNull(dh->p);
  53489. ExpectNotNull(dh->g);
  53490. ExpectTrue(pt == buf);
  53491. ExpectIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  53492. ExpectIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  53493. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  53494. /* Invalid cases */
  53495. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  53496. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  53497. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  53498. DH_free(dh);
  53499. dh = NULL;
  53500. *buf = 0;
  53501. pt = buf;
  53502. #endif /* HAVE_FFDHE_2048 */
  53503. /* Test 3072 bit parameters */
  53504. #ifdef HAVE_FFDHE_3072
  53505. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  53506. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  53507. if (f != XBADFILE) {
  53508. XFCLOSE(f);
  53509. f = XBADFILE;
  53510. }
  53511. /* Valid case */
  53512. ExpectNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
  53513. ExpectNotNull(dh->p);
  53514. ExpectNotNull(dh->g);
  53515. ExpectTrue(pt != buf);
  53516. ExpectIntEQ(DH_generate_key(dh), 1);
  53517. /* Invalid cases */
  53518. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  53519. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  53520. DH_free(dh);
  53521. dh = NULL;
  53522. #endif /* HAVE_FFDHE_3072 */
  53523. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  53524. #endif /* !NO_DH */
  53525. #endif
  53526. return EXPECT_RESULT();
  53527. }
  53528. static int test_wolfSSL_DH_LoadDer(void)
  53529. {
  53530. EXPECT_DECLS;
  53531. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  53532. defined(OPENSSL_EXTRA)
  53533. static const byte dh2048[] = {
  53534. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  53535. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  53536. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  53537. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  53538. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  53539. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  53540. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  53541. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  53542. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  53543. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  53544. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  53545. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  53546. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  53547. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  53548. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  53549. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  53550. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  53551. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  53552. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  53553. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  53554. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  53555. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  53556. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  53557. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  53558. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  53559. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  53560. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  53561. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  53562. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  53563. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  53564. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  53565. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  53566. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  53567. 0x93, 0x02, 0x01, 0x02
  53568. };
  53569. WOLFSSL_DH* dh = NULL;
  53570. ExpectNotNull(dh = wolfSSL_DH_new());
  53571. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  53572. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  53573. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  53574. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  53575. wolfSSL_DH_free(dh);
  53576. #endif
  53577. return EXPECT_RESULT();
  53578. }
  53579. static int test_wolfSSL_i2d_DHparams(void)
  53580. {
  53581. EXPECT_DECLS;
  53582. #ifdef OPENSSL_ALL
  53583. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  53584. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  53585. XFILE f = XBADFILE;
  53586. unsigned char buf[4096];
  53587. const unsigned char* pt;
  53588. unsigned char* pt2;
  53589. #ifdef HAVE_FFDHE_2048
  53590. const char* params1 = "./certs/dh2048.der";
  53591. #endif
  53592. #ifdef HAVE_FFDHE_3072
  53593. const char* params2 = "./certs/dh3072.der";
  53594. #endif
  53595. long len = 0;
  53596. WOLFSSL_DH* dh = NULL;
  53597. /* Test 2048 bit parameters */
  53598. #ifdef HAVE_FFDHE_2048
  53599. pt = buf;
  53600. pt2 = buf;
  53601. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  53602. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  53603. if (f != XBADFILE) {
  53604. XFCLOSE(f);
  53605. f = XBADFILE;
  53606. }
  53607. /* Valid case */
  53608. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  53609. ExpectTrue(pt == buf);
  53610. ExpectIntEQ(DH_generate_key(dh), 1);
  53611. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  53612. /* Invalid case */
  53613. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  53614. /* Return length only */
  53615. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  53616. DH_free(dh);
  53617. dh = NULL;
  53618. *buf = 0;
  53619. #endif
  53620. /* Test 3072 bit parameters */
  53621. #ifdef HAVE_FFDHE_3072
  53622. pt = buf;
  53623. pt2 = buf;
  53624. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  53625. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  53626. if (f != XBADFILE) {
  53627. XFCLOSE(f);
  53628. f = XBADFILE;
  53629. }
  53630. /* Valid case */
  53631. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  53632. ExpectTrue(pt == buf);
  53633. ExpectIntEQ(DH_generate_key(dh), 1);
  53634. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  53635. /* Invalid case */
  53636. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  53637. /* Return length only */
  53638. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  53639. DH_free(dh);
  53640. dh = NULL;
  53641. #endif
  53642. dh = DH_new();
  53643. ExpectNotNull(dh);
  53644. pt2 = buf;
  53645. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  53646. DH_free(dh);
  53647. dh = NULL;
  53648. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  53649. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  53650. #endif
  53651. return EXPECT_RESULT();
  53652. }
  53653. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  53654. /*----------------------------------------------------------------------------*
  53655. | EC
  53656. *----------------------------------------------------------------------------*/
  53657. static int test_wolfSSL_EC_GROUP(void)
  53658. {
  53659. EXPECT_DECLS;
  53660. #ifdef OPENSSL_EXTRA
  53661. EC_GROUP *group = NULL;
  53662. EC_GROUP *group2 = NULL;
  53663. EC_GROUP *group3 = NULL;
  53664. #ifndef HAVE_ECC_BRAINPOOL
  53665. EC_GROUP *group4 = NULL;
  53666. #endif
  53667. WOLFSSL_BIGNUM* order = NULL;
  53668. int group_bits;
  53669. int i;
  53670. static const int knownEccNids[] = {
  53671. NID_X9_62_prime192v1,
  53672. NID_X9_62_prime192v2,
  53673. NID_X9_62_prime192v3,
  53674. NID_X9_62_prime239v1,
  53675. NID_X9_62_prime239v2,
  53676. NID_X9_62_prime239v3,
  53677. NID_X9_62_prime256v1,
  53678. NID_secp112r1,
  53679. NID_secp112r2,
  53680. NID_secp128r1,
  53681. NID_secp128r2,
  53682. NID_secp160r1,
  53683. NID_secp160r2,
  53684. NID_secp224r1,
  53685. NID_secp384r1,
  53686. NID_secp521r1,
  53687. NID_secp160k1,
  53688. NID_secp192k1,
  53689. NID_secp224k1,
  53690. NID_secp256k1,
  53691. NID_brainpoolP160r1,
  53692. NID_brainpoolP192r1,
  53693. NID_brainpoolP224r1,
  53694. NID_brainpoolP256r1,
  53695. NID_brainpoolP320r1,
  53696. NID_brainpoolP384r1,
  53697. NID_brainpoolP512r1,
  53698. };
  53699. int knowEccNidsLen = (int)(sizeof(knownEccNids) / sizeof(*knownEccNids));
  53700. static const int knownEccEnums[] = {
  53701. ECC_SECP192R1,
  53702. ECC_PRIME192V2,
  53703. ECC_PRIME192V3,
  53704. ECC_PRIME239V1,
  53705. ECC_PRIME239V2,
  53706. ECC_PRIME239V3,
  53707. ECC_SECP256R1,
  53708. ECC_SECP112R1,
  53709. ECC_SECP112R2,
  53710. ECC_SECP128R1,
  53711. ECC_SECP128R2,
  53712. ECC_SECP160R1,
  53713. ECC_SECP160R2,
  53714. ECC_SECP224R1,
  53715. ECC_SECP384R1,
  53716. ECC_SECP521R1,
  53717. ECC_SECP160K1,
  53718. ECC_SECP192K1,
  53719. ECC_SECP224K1,
  53720. ECC_SECP256K1,
  53721. ECC_BRAINPOOLP160R1,
  53722. ECC_BRAINPOOLP192R1,
  53723. ECC_BRAINPOOLP224R1,
  53724. ECC_BRAINPOOLP256R1,
  53725. ECC_BRAINPOOLP320R1,
  53726. ECC_BRAINPOOLP384R1,
  53727. ECC_BRAINPOOLP512R1,
  53728. };
  53729. int knowEccEnumsLen = (int)(sizeof(knownEccEnums) / sizeof(*knownEccEnums));
  53730. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  53731. ExpectNotNull(group2 = EC_GROUP_dup(group));
  53732. ExpectNotNull(group3 = wolfSSL_EC_GROUP_new_by_curve_name(NID_secp384r1));
  53733. #ifndef HAVE_ECC_BRAINPOOL
  53734. ExpectNotNull(group4 = wolfSSL_EC_GROUP_new_by_curve_name(
  53735. NID_brainpoolP256r1));
  53736. #endif
  53737. ExpectNull(EC_GROUP_dup(NULL));
  53738. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(NULL), 0);
  53739. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  53740. ExpectIntEQ((group_bits = EC_GROUP_order_bits(NULL)), 0);
  53741. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  53742. #ifndef HAVE_ECC_BRAINPOOL
  53743. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group4)), 0);
  53744. #endif
  53745. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(NULL), 0);
  53746. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(group), 256);
  53747. ExpectNotNull(order = BN_new());
  53748. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, NULL, NULL), 0);
  53749. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, NULL, NULL), 0);
  53750. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, order, NULL), 0);
  53751. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, order, NULL), 1);
  53752. wolfSSL_BN_free(order);
  53753. ExpectNotNull(EC_GROUP_method_of(group));
  53754. ExpectIntEQ(EC_METHOD_get_field_type(NULL), 0);
  53755. ExpectIntEQ(EC_METHOD_get_field_type(EC_GROUP_method_of(group)),
  53756. NID_X9_62_prime_field);
  53757. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, NULL, NULL), -1);
  53758. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, NULL, NULL), -1);
  53759. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, group, NULL), -1);
  53760. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, group3, NULL), 1);
  53761. #ifndef NO_WOLFSSL_STUB
  53762. wolfSSL_EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  53763. #endif
  53764. #ifndef HAVE_ECC_BRAINPOOL
  53765. EC_GROUP_free(group4);
  53766. #endif
  53767. EC_GROUP_free(group3);
  53768. EC_GROUP_free(group2);
  53769. EC_GROUP_free(group);
  53770. for (i = 0; i < knowEccNidsLen; i++) {
  53771. group = NULL;
  53772. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccNids[i]));
  53773. ExpectIntGT(wolfSSL_EC_GROUP_get_degree(group), 0);
  53774. EC_GROUP_free(group);
  53775. }
  53776. for (i = 0; i < knowEccEnumsLen; i++) {
  53777. group = NULL;
  53778. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccEnums[i]));
  53779. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), knownEccNids[i]);
  53780. EC_GROUP_free(group);
  53781. }
  53782. #endif
  53783. return EXPECT_RESULT();
  53784. }
  53785. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  53786. {
  53787. EXPECT_DECLS;
  53788. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  53789. EC_GROUP *group = NULL;
  53790. BIO* bio = NULL;
  53791. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  53792. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  53793. EC_GROUP *ret = NULL;
  53794. static char ec_nc_p384[] = "-----BEGIN EC PARAMETERS-----\n"
  53795. "BgUrgQQAIg==\n"
  53796. "-----END EC PARAMETERS-----";
  53797. #endif
  53798. static char ec_nc_bad_1[] = "-----BEGIN EC PARAMETERS-----\n"
  53799. "MAA=\n"
  53800. "-----END EC PARAMETERS-----";
  53801. static char ec_nc_bad_2[] = "-----BEGIN EC PARAMETERS-----\n"
  53802. "BgA=\n"
  53803. "-----END EC PARAMETERS-----";
  53804. static char ec_nc_bad_3[] = "-----BEGIN EC PARAMETERS-----\n"
  53805. "BgE=\n"
  53806. "-----END EC PARAMETERS-----";
  53807. static char ec_nc_bad_4[] = "-----BEGIN EC PARAMETERS-----\n"
  53808. "BgE*\n"
  53809. "-----END EC PARAMETERS-----";
  53810. /* Test that first parameter, bio, being NULL fails. */
  53811. ExpectNull(PEM_read_bio_ECPKParameters(NULL, NULL, NULL, NULL));
  53812. /* Test that reading named parameters works. */
  53813. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  53814. ExpectIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  53815. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53816. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  53817. BIO_free(bio);
  53818. bio = NULL;
  53819. EC_GROUP_free(group);
  53820. group = NULL;
  53821. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  53822. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  53823. /* Test that reusing group works. */
  53824. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  53825. sizeof(ec_nc_p384)));
  53826. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  53827. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  53828. BIO_free(bio);
  53829. bio = NULL;
  53830. EC_GROUP_free(group);
  53831. group = NULL;
  53832. /* Test that returning through group works. */
  53833. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  53834. sizeof(ec_nc_p384)));
  53835. ExpectNotNull(ret = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  53836. ExpectIntEQ(group == ret, 1);
  53837. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  53838. BIO_free(bio);
  53839. bio = NULL;
  53840. EC_GROUP_free(group);
  53841. group = NULL;
  53842. #endif
  53843. /* Test 0x30, 0x00 (not and object id) fails. */
  53844. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_1,
  53845. sizeof(ec_nc_bad_1)));
  53846. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53847. BIO_free(bio);
  53848. bio = NULL;
  53849. /* Test 0x06, 0x00 (empty object id) fails. */
  53850. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_2,
  53851. sizeof(ec_nc_bad_2)));
  53852. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53853. BIO_free(bio);
  53854. bio = NULL;
  53855. /* Test 0x06, 0x01 (badly formed object id) fails. */
  53856. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_3,
  53857. sizeof(ec_nc_bad_3)));
  53858. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53859. BIO_free(bio);
  53860. bio = NULL;
  53861. /* Test invalid PEM encoding - invalid character. */
  53862. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_4,
  53863. sizeof(ec_nc_bad_4)));
  53864. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53865. BIO_free(bio);
  53866. #endif
  53867. return EXPECT_RESULT();
  53868. }
  53869. static int test_wolfSSL_EC_POINT(void)
  53870. {
  53871. EXPECT_DECLS;
  53872. #if !defined(WOLFSSL_SP_MATH) && \
  53873. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  53874. #ifdef OPENSSL_EXTRA
  53875. BN_CTX* ctx = NULL;
  53876. EC_GROUP* group = NULL;
  53877. #ifndef HAVE_ECC_BRAINPOOL
  53878. EC_GROUP* group2 = NULL;
  53879. #endif
  53880. EC_POINT* Gxy = NULL;
  53881. EC_POINT* new_point = NULL;
  53882. EC_POINT* set_point = NULL;
  53883. EC_POINT* infinity = NULL;
  53884. BIGNUM* k = NULL;
  53885. BIGNUM* Gx = NULL;
  53886. BIGNUM* Gy = NULL;
  53887. BIGNUM* Gz = NULL;
  53888. BIGNUM* X = NULL;
  53889. BIGNUM* Y = NULL;
  53890. BIGNUM* set_point_bn = NULL;
  53891. char* hexStr = NULL;
  53892. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD"
  53893. "17AF381913FF7A96314EA47055EA0FD0";
  53894. /* NISTP256R1 Gx/Gy */
  53895. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F2"
  53896. "77037D812DEB33A0F4A13945D898C296";
  53897. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  53898. "2BCE33576B315ECECBB6406837BF51F5";
  53899. #ifndef HAVE_SELFTEST
  53900. EC_POINT *tmp = NULL;
  53901. size_t bin_len;
  53902. unsigned int blen = 0;
  53903. unsigned char* buf = NULL;
  53904. unsigned char bufInf[1] = { 0x00 };
  53905. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F2"
  53906. "77037D812DEB33A0F4A13945D898C296"
  53907. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  53908. "2BCE33576B315ECECBB6406837BF51F5";
  53909. const unsigned char binUncompG[] = {
  53910. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  53911. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  53912. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  53913. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  53914. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  53915. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  53916. };
  53917. const unsigned char binUncompGBad[] = {
  53918. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  53919. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  53920. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  53921. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  53922. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  53923. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  53924. };
  53925. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F2"
  53926. "77037D812DEB33A0F4A13945D898C296";
  53927. #ifdef HAVE_COMP_KEY
  53928. const unsigned char binCompG[] = {
  53929. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  53930. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  53931. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  53932. };
  53933. #endif
  53934. #endif
  53935. ExpectNotNull(ctx = BN_CTX_new());
  53936. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  53937. #ifndef HAVE_ECC_BRAINPOOL
  53938. /* Used to make groups curve_idx == -1. */
  53939. ExpectNotNull(group2 = EC_GROUP_new_by_curve_name(NID_brainpoolP256r1));
  53940. #endif
  53941. ExpectNull(EC_POINT_new(NULL));
  53942. ExpectNotNull(Gxy = EC_POINT_new(group));
  53943. ExpectNotNull(new_point = EC_POINT_new(group));
  53944. ExpectNotNull(set_point = EC_POINT_new(group));
  53945. ExpectNotNull(X = BN_new());
  53946. ExpectNotNull(Y = BN_new());
  53947. ExpectNotNull(set_point_bn = BN_new());
  53948. ExpectNotNull(infinity = EC_POINT_new(group));
  53949. /* load test values */
  53950. ExpectIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  53951. ExpectIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  53952. ExpectIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  53953. ExpectIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  53954. /* populate coordinates for input point */
  53955. if (Gxy != NULL) {
  53956. Gxy->X = Gx;
  53957. Gxy->Y = Gy;
  53958. Gxy->Z = Gz;
  53959. }
  53960. /* Test handling of NULL point. */
  53961. EC_POINT_clear_free(NULL);
  53962. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  53963. NULL, NULL, ctx), 0);
  53964. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  53965. NULL, NULL, ctx), 0);
  53966. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  53967. NULL, NULL, ctx), 0);
  53968. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  53969. X, NULL, ctx), 0);
  53970. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  53971. NULL, Y, ctx), 0);
  53972. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  53973. X, Y, ctx), 0);
  53974. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  53975. X, Y, ctx), 0);
  53976. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  53977. NULL, Y, ctx), 0);
  53978. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  53979. X, NULL, ctx), 0);
  53980. /* Getting point at infinity returns an error. */
  53981. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, infinity,
  53982. X, Y, ctx), 0);
  53983. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  53984. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  53985. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  53986. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, NULL, ctx), 0);
  53987. ExpectIntEQ(EC_POINT_add(group, NULL, NULL, NULL, ctx), 0);
  53988. ExpectIntEQ(EC_POINT_add(NULL, new_point, NULL, NULL, ctx), 0);
  53989. ExpectIntEQ(EC_POINT_add(NULL, NULL, new_point, NULL, ctx), 0);
  53990. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, Gxy, ctx), 0);
  53991. ExpectIntEQ(EC_POINT_add(NULL, new_point, new_point, Gxy, ctx), 0);
  53992. ExpectIntEQ(EC_POINT_add(group, NULL, new_point, Gxy, ctx), 0);
  53993. ExpectIntEQ(EC_POINT_add(group, new_point, NULL, Gxy, ctx), 0);
  53994. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, NULL, ctx), 0);
  53995. ExpectIntEQ(EC_POINT_mul(NULL, NULL, Gx, Gxy, k, ctx), 0);
  53996. ExpectIntEQ(EC_POINT_mul(NULL, new_point, Gx, Gxy, k, ctx), 0);
  53997. ExpectIntEQ(EC_POINT_mul(group, NULL, Gx, Gxy, k, ctx), 0);
  53998. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), 1);
  53999. /* perform point multiplication */
  54000. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), 1);
  54001. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  54002. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  54003. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  54004. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), 1);
  54005. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  54006. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  54007. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  54008. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), 1);
  54009. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  54010. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  54011. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  54012. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, NULL, NULL, ctx), 1);
  54013. ExpectIntEQ(BN_is_zero(new_point->X), 1);
  54014. ExpectIntEQ(BN_is_zero(new_point->Y), 1);
  54015. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  54016. /* Set point to something. */
  54017. ExpectIntEQ(EC_POINT_add(group, new_point, Gxy, Gxy, ctx), 1);
  54018. #else
  54019. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy,
  54020. ctx), 1);
  54021. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  54022. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  54023. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  54024. #endif
  54025. /* check if point X coordinate is zero */
  54026. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  54027. #if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
  54028. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  54029. ExpectIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  54030. #endif
  54031. /* extract the coordinates from point */
  54032. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  54033. ctx), WOLFSSL_SUCCESS);
  54034. /* check if point X coordinate is zero */
  54035. ExpectIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  54036. /* set the same X and Y points in another object */
  54037. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y,
  54038. ctx), WOLFSSL_SUCCESS);
  54039. /* compare points as they should be the same */
  54040. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, NULL, ctx), -1);
  54041. ExpectIntEQ(EC_POINT_cmp(group, NULL, NULL, ctx), -1);
  54042. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, NULL, ctx), -1);
  54043. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, set_point, ctx), -1);
  54044. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, set_point, ctx), -1);
  54045. ExpectIntEQ(EC_POINT_cmp(group, NULL, set_point, ctx), -1);
  54046. ExpectIntEQ(EC_POINT_cmp(group, new_point, NULL, ctx), -1);
  54047. ExpectIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  54048. /* Test copying */
  54049. ExpectIntEQ(EC_POINT_copy(NULL, NULL), 0);
  54050. ExpectIntEQ(EC_POINT_copy(NULL, set_point), 0);
  54051. ExpectIntEQ(EC_POINT_copy(new_point, NULL), 0);
  54052. ExpectIntEQ(EC_POINT_copy(new_point, set_point), 1);
  54053. /* Test inverting */
  54054. ExpectIntEQ(EC_POINT_invert(NULL, NULL, ctx), 0);
  54055. ExpectIntEQ(EC_POINT_invert(NULL, new_point, ctx), 0);
  54056. ExpectIntEQ(EC_POINT_invert(group, NULL, ctx), 0);
  54057. ExpectIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  54058. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  54059. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  54060. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  54061. {
  54062. EC_POINT* orig_point = NULL;
  54063. ExpectNotNull(orig_point = EC_POINT_new(group));
  54064. ExpectIntEQ(EC_POINT_add(group, orig_point, set_point, set_point, NULL),
  54065. 1);
  54066. /* new_point should be set_point inverted so adding it will revert
  54067. * the point back to set_point */
  54068. ExpectIntEQ(EC_POINT_add(group, orig_point, orig_point, new_point,
  54069. NULL), 1);
  54070. ExpectIntEQ(EC_POINT_cmp(group, orig_point, set_point, NULL), 0);
  54071. EC_POINT_free(orig_point);
  54072. }
  54073. #endif
  54074. /* Test getting affine converts from projective. */
  54075. ExpectIntEQ(EC_POINT_copy(set_point, new_point), 1);
  54076. /* Force non-affine coordinates */
  54077. ExpectIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  54078. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  54079. if (new_point != NULL) {
  54080. new_point->inSet = 0;
  54081. }
  54082. /* extract the coordinates from point */
  54083. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  54084. ctx), WOLFSSL_SUCCESS);
  54085. /* check if point ordinates have changed. */
  54086. ExpectIntNE(BN_cmp(X, set_point->X), 0);
  54087. ExpectIntNE(BN_cmp(Y, set_point->Y), 0);
  54088. /* Test check for infinity */
  54089. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  54090. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, NULL), 0);
  54091. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, infinity), 0);
  54092. ExpectIntEQ(EC_POINT_is_at_infinity(group, NULL), 0);
  54093. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 1);
  54094. ExpectIntEQ(EC_POINT_is_at_infinity(group, Gxy), 0);
  54095. #else
  54096. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 0);
  54097. #endif
  54098. ExpectPtrEq(EC_POINT_point2bn(group, set_point,
  54099. POINT_CONVERSION_UNCOMPRESSED, set_point_bn, ctx), set_point_bn);
  54100. /* check bn2hex */
  54101. hexStr = BN_bn2hex(k);
  54102. ExpectStrEQ(hexStr, kTest);
  54103. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  54104. defined(XFPRINTF)
  54105. BN_print_fp(stderr, k);
  54106. fprintf(stderr, "\n");
  54107. #endif
  54108. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  54109. hexStr = BN_bn2hex(Gx);
  54110. ExpectStrEQ(hexStr, kGx);
  54111. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  54112. defined(XFPRINTF)
  54113. BN_print_fp(stderr, Gx);
  54114. fprintf(stderr, "\n");
  54115. #endif
  54116. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  54117. hexStr = BN_bn2hex(Gy);
  54118. ExpectStrEQ(hexStr, kGy);
  54119. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  54120. defined(XFPRINTF)
  54121. BN_print_fp(stderr, Gy);
  54122. fprintf(stderr, "\n");
  54123. #endif
  54124. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  54125. #ifndef HAVE_SELFTEST
  54126. /* Test point to hex */
  54127. ExpectNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  54128. ctx));
  54129. ExpectNull(EC_POINT_point2hex(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  54130. ctx));
  54131. ExpectNull(EC_POINT_point2hex(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  54132. ctx));
  54133. #ifndef HAVE_ECC_BRAINPOOL
  54134. /* Group not supported in wolfCrypt. */
  54135. ExpectNull(EC_POINT_point2hex(group2, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  54136. ctx));
  54137. #endif
  54138. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  54139. ExpectNotNull(hexStr);
  54140. ExpectStrEQ(hexStr, uncompG);
  54141. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  54142. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  54143. ExpectNotNull(hexStr);
  54144. ExpectStrEQ(hexStr, compG);
  54145. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  54146. /* Test point to oct */
  54147. ExpectIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  54148. NULL, 0, ctx), 0);
  54149. ExpectIntEQ(EC_POINT_point2oct(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  54150. NULL, 0, ctx), 0);
  54151. ExpectIntEQ(EC_POINT_point2oct(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  54152. NULL, 0, ctx), 0);
  54153. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  54154. NULL, 0, ctx);
  54155. ExpectIntEQ(bin_len, sizeof(binUncompG));
  54156. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  54157. DYNAMIC_TYPE_ECC));
  54158. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  54159. buf, bin_len, ctx), bin_len);
  54160. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  54161. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  54162. /* Infinity (x=0, y=0) encodes as '0x00'. */
  54163. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  54164. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), 1);
  54165. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  54166. POINT_CONVERSION_UNCOMPRESSED, bufInf, 0, ctx), 0);
  54167. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  54168. POINT_CONVERSION_UNCOMPRESSED, bufInf, 1, ctx), 1);
  54169. ExpectIntEQ(bufInf[0], 0);
  54170. wolfSSL_EC_POINT_dump(NULL, NULL);
  54171. /* Test point i2d */
  54172. ExpectIntEQ(ECPoint_i2d(NULL, NULL, NULL, &blen), 0);
  54173. ExpectIntEQ(ECPoint_i2d(NULL, Gxy, NULL, &blen), 0);
  54174. ExpectIntEQ(ECPoint_i2d(group, NULL, NULL, &blen), 0);
  54175. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, NULL), 0);
  54176. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, &blen), 1);
  54177. ExpectIntEQ(blen, sizeof(binUncompG));
  54178. ExpectNotNull(buf = (unsigned char*)XMALLOC(blen, NULL, DYNAMIC_TYPE_ECC));
  54179. blen -= 1;
  54180. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 0);
  54181. blen += 1;
  54182. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 1);
  54183. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  54184. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  54185. #ifdef HAVE_COMP_KEY
  54186. /* Test point to oct compressed */
  54187. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL,
  54188. 0, ctx);
  54189. ExpectIntEQ(bin_len, sizeof(binCompG));
  54190. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  54191. DYNAMIC_TYPE_ECC));
  54192. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  54193. bin_len, ctx), bin_len);
  54194. ExpectIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  54195. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  54196. #endif
  54197. /* Test point BN */
  54198. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, NULL,
  54199. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  54200. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, Gxy,
  54201. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  54202. ExpectNull(wolfSSL_EC_POINT_point2bn(group, NULL,
  54203. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  54204. ExpectNull(wolfSSL_EC_POINT_point2bn(group, Gxy, 0, NULL, ctx));
  54205. /* Test oct to point */
  54206. ExpectNotNull(tmp = EC_POINT_new(group));
  54207. ExpectIntEQ(EC_POINT_oct2point(NULL, NULL, binUncompG, sizeof(binUncompG),
  54208. ctx), 0);
  54209. ExpectIntEQ(EC_POINT_oct2point(NULL, tmp, binUncompG, sizeof(binUncompG),
  54210. ctx), 0);
  54211. ExpectIntEQ(EC_POINT_oct2point(group, NULL, binUncompG, sizeof(binUncompG),
  54212. ctx), 0);
  54213. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompGBad,
  54214. sizeof(binUncompGBad), ctx), 0);
  54215. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG),
  54216. ctx), 1);
  54217. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  54218. EC_POINT_free(tmp);
  54219. tmp = NULL;
  54220. /* Test setting BN ordinates. */
  54221. ExpectNotNull(tmp = EC_POINT_new(group));
  54222. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  54223. NULL, ctx), 0);
  54224. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, NULL,
  54225. NULL, ctx), 0);
  54226. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, NULL,
  54227. NULL, ctx), 0);
  54228. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, Gx,
  54229. NULL, ctx), 0);
  54230. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  54231. Gy, ctx), 0);
  54232. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, Gx, Gy,
  54233. ctx), 0);
  54234. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, Gx, Gy,
  54235. ctx), 0);
  54236. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, NULL,
  54237. Gy, ctx), 0);
  54238. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx,
  54239. NULL, ctx), 0);
  54240. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx, Gy,
  54241. ctx), 1);
  54242. EC_POINT_free(tmp);
  54243. tmp = NULL;
  54244. /* Test point d2i */
  54245. ExpectNotNull(tmp = EC_POINT_new(group));
  54246. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, NULL), 0);
  54247. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, NULL), 0);
  54248. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, NULL), 0);
  54249. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, tmp), 0);
  54250. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, tmp), 0);
  54251. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, tmp), 0);
  54252. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, NULL), 0);
  54253. ExpectIntEQ(ECPoint_d2i(binUncompGBad, sizeof(binUncompG), group, tmp), 0);
  54254. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, tmp), 1);
  54255. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  54256. EC_POINT_free(tmp);
  54257. tmp = NULL;
  54258. #ifdef HAVE_COMP_KEY
  54259. /* Test oct compressed to point */
  54260. ExpectNotNull(tmp = EC_POINT_new(group));
  54261. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx),
  54262. 1);
  54263. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  54264. EC_POINT_free(tmp);
  54265. tmp = NULL;
  54266. /* Test point d2i - compressed */
  54267. ExpectNotNull(tmp = EC_POINT_new(group));
  54268. ExpectIntEQ(ECPoint_d2i(binCompG, sizeof(binCompG), group, tmp), 1);
  54269. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  54270. EC_POINT_free(tmp);
  54271. tmp = NULL;
  54272. #endif
  54273. #endif
  54274. /* test BN_mod_add */
  54275. ExpectIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  54276. (WOLFSSL_BIGNUM*)BN_value_one(), (WOLFSSL_BIGNUM*)BN_value_one(), NULL),
  54277. 1);
  54278. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  54279. /* cleanup */
  54280. BN_free(X);
  54281. BN_free(Y);
  54282. BN_free(k);
  54283. BN_free(set_point_bn);
  54284. EC_POINT_free(infinity);
  54285. EC_POINT_free(new_point);
  54286. EC_POINT_free(set_point);
  54287. EC_POINT_clear_free(Gxy);
  54288. #ifndef HAVE_ECC_BRAINPOOL
  54289. EC_GROUP_free(group2);
  54290. #endif
  54291. EC_GROUP_free(group);
  54292. BN_CTX_free(ctx);
  54293. #endif
  54294. #endif /* !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  54295. return EXPECT_RESULT();
  54296. }
  54297. static int test_wolfSSL_SPAKE(void)
  54298. {
  54299. EXPECT_DECLS;
  54300. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(WOLFSSL_ATECC508A) \
  54301. && !defined(WOLFSSL_ATECC608A) && !defined(HAVE_SELFTEST) && \
  54302. !defined(WOLFSSL_SP_MATH) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  54303. BIGNUM* x = NULL; /* kdc priv */
  54304. BIGNUM* y = NULL; /* client priv */
  54305. BIGNUM* w = NULL; /* shared value */
  54306. byte M_bytes[] = {
  54307. /* uncompressed */
  54308. 0x04,
  54309. /* x */
  54310. 0x88, 0x6e, 0x2f, 0x97, 0xac, 0xe4, 0x6e, 0x55, 0xba, 0x9d, 0xd7, 0x24,
  54311. 0x25, 0x79, 0xf2, 0x99, 0x3b, 0x64, 0xe1, 0x6e, 0xf3, 0xdc, 0xab, 0x95,
  54312. 0xaf, 0xd4, 0x97, 0x33, 0x3d, 0x8f, 0xa1, 0x2f,
  54313. /* y */
  54314. 0x5f, 0xf3, 0x55, 0x16, 0x3e, 0x43, 0xce, 0x22, 0x4e, 0x0b, 0x0e, 0x65,
  54315. 0xff, 0x02, 0xac, 0x8e, 0x5c, 0x7b, 0xe0, 0x94, 0x19, 0xc7, 0x85, 0xe0,
  54316. 0xca, 0x54, 0x7d, 0x55, 0xa1, 0x2e, 0x2d, 0x20
  54317. };
  54318. EC_POINT* M = NULL; /* shared value */
  54319. byte N_bytes[] = {
  54320. /* uncompressed */
  54321. 0x04,
  54322. /* x */
  54323. 0xd8, 0xbb, 0xd6, 0xc6, 0x39, 0xc6, 0x29, 0x37, 0xb0, 0x4d, 0x99, 0x7f,
  54324. 0x38, 0xc3, 0x77, 0x07, 0x19, 0xc6, 0x29, 0xd7, 0x01, 0x4d, 0x49, 0xa2,
  54325. 0x4b, 0x4f, 0x98, 0xba, 0xa1, 0x29, 0x2b, 0x49,
  54326. /* y */
  54327. 0x07, 0xd6, 0x0a, 0xa6, 0xbf, 0xad, 0xe4, 0x50, 0x08, 0xa6, 0x36, 0x33,
  54328. 0x7f, 0x51, 0x68, 0xc6, 0x4d, 0x9b, 0xd3, 0x60, 0x34, 0x80, 0x8c, 0xd5,
  54329. 0x64, 0x49, 0x0b, 0x1e, 0x65, 0x6e, 0xdb, 0xe7
  54330. };
  54331. EC_POINT* N = NULL; /* shared value */
  54332. EC_POINT* T = NULL; /* kdc pub */
  54333. EC_POINT* tmp1 = NULL; /* kdc pub */
  54334. EC_POINT* tmp2 = NULL; /* kdc pub */
  54335. EC_POINT* S = NULL; /* client pub */
  54336. EC_POINT* client_secret = NULL;
  54337. EC_POINT* kdc_secret = NULL;
  54338. EC_GROUP* group = NULL;
  54339. BN_CTX* bn_ctx = NULL;
  54340. /* Values taken from a test run of Kerberos 5 */
  54341. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  54342. ExpectNotNull(bn_ctx = BN_CTX_new());
  54343. ExpectNotNull(M = EC_POINT_new(group));
  54344. ExpectNotNull(N = EC_POINT_new(group));
  54345. ExpectNotNull(T = EC_POINT_new(group));
  54346. ExpectNotNull(tmp1 = EC_POINT_new(group));
  54347. ExpectNotNull(tmp2 = EC_POINT_new(group));
  54348. ExpectNotNull(S = EC_POINT_new(group));
  54349. ExpectNotNull(client_secret = EC_POINT_new(group));
  54350. ExpectNotNull(kdc_secret = EC_POINT_new(group));
  54351. ExpectIntEQ(BN_hex2bn(&x, "DAC3027CD692B4BDF0EDFE9B7D0E4E7"
  54352. "E5D8768A725EAEEA6FC68EC239A17C0"), 1);
  54353. ExpectIntEQ(BN_hex2bn(&y, "6F6A1D394E26B1655A54B26DCE30D49"
  54354. "90CC47EBE08F809EF3FF7F6AEAABBB5"), 1);
  54355. ExpectIntEQ(BN_hex2bn(&w, "1D992AB8BA851B9BA05353453D81EE9"
  54356. "506AB395478F0AAB647752CF117B36250"), 1);
  54357. ExpectIntEQ(EC_POINT_oct2point(group, M, M_bytes, sizeof(M_bytes), bn_ctx),
  54358. 1);
  54359. ExpectIntEQ(EC_POINT_oct2point(group, N, N_bytes, sizeof(N_bytes), bn_ctx),
  54360. 1);
  54361. /* Function pattern similar to ossl_keygen and ossl_result in krb5 */
  54362. /* kdc */
  54363. /* T=x*P+w*M */
  54364. /* All in one function call */
  54365. ExpectIntEQ(EC_POINT_mul(group, T, x, M, w, bn_ctx), 1);
  54366. /* Spread into separate calls */
  54367. ExpectIntEQ(EC_POINT_mul(group, tmp1, x, NULL, NULL, bn_ctx), 1);
  54368. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, M, w, bn_ctx), 1);
  54369. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  54370. 1);
  54371. ExpectIntEQ(EC_POINT_cmp(group, T, tmp1, bn_ctx), 0);
  54372. /* client */
  54373. /* S=y*P+w*N */
  54374. /* All in one function call */
  54375. ExpectIntEQ(EC_POINT_mul(group, S, y, N, w, bn_ctx), 1);
  54376. /* Spread into separate calls */
  54377. ExpectIntEQ(EC_POINT_mul(group, tmp1, y, NULL, NULL, bn_ctx), 1);
  54378. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, N, w, bn_ctx), 1);
  54379. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  54380. 1);
  54381. ExpectIntEQ(EC_POINT_cmp(group, S, tmp1, bn_ctx), 0);
  54382. /* K=y*(T-w*M) */
  54383. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, M, w, bn_ctx), 1);
  54384. ExpectIntEQ(EC_POINT_invert(group, client_secret, bn_ctx), 1);
  54385. ExpectIntEQ(EC_POINT_add(group, client_secret, T, client_secret, bn_ctx),
  54386. 1);
  54387. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, client_secret, y,
  54388. bn_ctx), 1);
  54389. /* kdc */
  54390. /* K=x*(S-w*N) */
  54391. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, N, w, bn_ctx), 1);
  54392. ExpectIntEQ(EC_POINT_invert(group, kdc_secret, bn_ctx), 1);
  54393. ExpectIntEQ(EC_POINT_add(group, kdc_secret, S, kdc_secret, bn_ctx),
  54394. 1);
  54395. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, kdc_secret, x, bn_ctx),
  54396. 1);
  54397. /* kdc_secret == client_secret */
  54398. ExpectIntEQ(EC_POINT_cmp(group, client_secret, kdc_secret, bn_ctx), 0);
  54399. BN_free(x);
  54400. BN_free(y);
  54401. BN_free(w);
  54402. EC_POINT_free(M);
  54403. EC_POINT_free(N);
  54404. EC_POINT_free(T);
  54405. EC_POINT_free(tmp1);
  54406. EC_POINT_free(tmp2);
  54407. EC_POINT_free(S);
  54408. EC_POINT_free(client_secret);
  54409. EC_POINT_free(kdc_secret);
  54410. EC_GROUP_free(group);
  54411. BN_CTX_free(bn_ctx);
  54412. #endif
  54413. return EXPECT_RESULT();
  54414. }
  54415. static int test_wolfSSL_EC_KEY_generate(void)
  54416. {
  54417. EXPECT_DECLS;
  54418. #ifdef OPENSSL_EXTRA
  54419. WOLFSSL_EC_KEY* key = NULL;
  54420. #ifndef HAVE_ECC_BRAINPOOL
  54421. WOLFSSL_EC_GROUP* group = NULL;
  54422. #endif
  54423. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54424. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(NULL), 0);
  54425. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  54426. wolfSSL_EC_KEY_free(key);
  54427. key = NULL;
  54428. #ifndef HAVE_ECC_BRAINPOOL
  54429. ExpectNotNull(group = wolfSSL_EC_GROUP_new_by_curve_name(
  54430. NID_brainpoolP256r1));
  54431. ExpectNotNull(key = wolfSSL_EC_KEY_new());
  54432. ExpectIntEQ(wolfSSL_EC_KEY_set_group(key, group), 1);
  54433. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 0);
  54434. wolfSSL_EC_KEY_free(key);
  54435. wolfSSL_EC_GROUP_free(group);
  54436. #endif
  54437. #endif
  54438. return EXPECT_RESULT();
  54439. }
  54440. static int test_EC_i2d(void)
  54441. {
  54442. EXPECT_DECLS;
  54443. #if defined(OPENSSL_EXTRA) && !defined(HAVE_FIPS)
  54444. EC_KEY *key = NULL;
  54445. EC_KEY *copy = NULL;
  54446. int len = 0;
  54447. unsigned char *buf = NULL;
  54448. unsigned char *p = NULL;
  54449. const unsigned char *tmp = NULL;
  54450. const unsigned char octBad[] = {
  54451. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  54452. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  54453. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  54454. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  54455. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  54456. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  54457. };
  54458. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54459. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  54460. ExpectIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  54461. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  54462. DYNAMIC_TYPE_TMP_BUFFER));
  54463. p = buf;
  54464. ExpectIntEQ(i2d_EC_PUBKEY(key, &p), len);
  54465. ExpectNull(o2i_ECPublicKey(NULL, NULL, -1));
  54466. ExpectNull(o2i_ECPublicKey(&copy, NULL, -1));
  54467. ExpectNull(o2i_ECPublicKey(&key, NULL, -1));
  54468. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  54469. ExpectNull(o2i_ECPublicKey(NULL, NULL, 0));
  54470. ExpectNull(o2i_ECPublicKey(&key, NULL, 0));
  54471. ExpectNull(o2i_ECPublicKey(&key, &tmp, 0));
  54472. tmp = buf;
  54473. ExpectNull(o2i_ECPublicKey(NULL, &tmp, 0));
  54474. ExpectNull(o2i_ECPublicKey(&copy, &tmp, 0));
  54475. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  54476. ExpectNull(o2i_ECPublicKey(&key, &tmp, -1));
  54477. ExpectIntEQ(i2o_ECPublicKey(NULL, NULL), 0);
  54478. ExpectIntEQ(i2o_ECPublicKey(NULL, &buf), 0);
  54479. tmp = buf;
  54480. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 0));
  54481. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 1));
  54482. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 0));
  54483. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 1));
  54484. ExpectNull(d2i_ECPrivateKey(&key, &tmp, 0));
  54485. ExpectIntEQ(i2d_ECPrivateKey(NULL, &p), 0);
  54486. ExpectIntEQ(i2d_ECPrivateKey(NULL, NULL), 0);
  54487. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer(NULL, NULL, -1), -1);
  54488. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1, 0), -1);
  54489. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, -1, 0), -1);
  54490. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, -1, 0), -1);
  54491. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, 0, 0), -1);
  54492. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1,
  54493. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  54494. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, len,
  54495. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  54496. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, len,
  54497. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  54498. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, -1,
  54499. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  54500. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len, 0), -1);
  54501. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len,
  54502. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  54503. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  54504. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  54505. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  54506. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  54507. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54508. buf = NULL;
  54509. buf = NULL;
  54510. ExpectIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  54511. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  54512. DYNAMIC_TYPE_TMP_BUFFER));
  54513. p = buf;
  54514. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  54515. p = NULL;
  54516. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  54517. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54518. p = NULL;
  54519. /* Bad point is also an invalid private key. */
  54520. tmp = octBad;
  54521. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, sizeof(octBad)));
  54522. tmp = buf;
  54523. ExpectNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  54524. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54525. buf = NULL;
  54526. buf = NULL;
  54527. ExpectIntGT((len = i2o_ECPublicKey(key, NULL)), 0);
  54528. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  54529. DYNAMIC_TYPE_TMP_BUFFER));
  54530. p = buf;
  54531. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  54532. p = NULL;
  54533. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  54534. tmp = buf;
  54535. ExpectNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  54536. tmp = octBad;
  54537. ExpectNull(o2i_ECPublicKey(&key, &tmp, sizeof(octBad)));
  54538. ExpectIntEQ(EC_KEY_check_key(NULL), 0);
  54539. ExpectIntEQ(EC_KEY_check_key(key), 1);
  54540. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  54541. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  54542. EC_KEY_free(key);
  54543. EC_KEY_free(copy);
  54544. #endif
  54545. return EXPECT_RESULT();
  54546. }
  54547. static int test_wolfSSL_EC_curve(void)
  54548. {
  54549. EXPECT_DECLS;
  54550. #if defined(OPENSSL_EXTRA)
  54551. int nid = NID_secp160k1;
  54552. const char* nid_name = NULL;
  54553. ExpectNull(EC_curve_nid2nist(NID_sha256));
  54554. ExpectNotNull(nid_name = EC_curve_nid2nist(nid));
  54555. ExpectIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  54556. ExpectIntEQ(EC_curve_nist2nid("INVALID"), 0);
  54557. ExpectIntEQ(EC_curve_nist2nid(nid_name), nid);
  54558. #endif
  54559. return EXPECT_RESULT();
  54560. }
  54561. static int test_wolfSSL_EC_KEY_dup(void)
  54562. {
  54563. EXPECT_DECLS;
  54564. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  54565. WOLFSSL_EC_KEY* ecKey = NULL;
  54566. WOLFSSL_EC_KEY* dupKey = NULL;
  54567. ecc_key* srcKey = NULL;
  54568. ecc_key* destKey = NULL;
  54569. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  54570. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  54571. /* Valid cases */
  54572. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54573. ExpectIntEQ(EC_KEY_check_key(dupKey), 1);
  54574. /* Compare pubkey */
  54575. if (ecKey != NULL) {
  54576. srcKey = (ecc_key*)ecKey->internal;
  54577. }
  54578. if (dupKey != NULL) {
  54579. destKey = (ecc_key*)dupKey->internal;
  54580. }
  54581. ExpectIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  54582. /* compare EC_GROUP */
  54583. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  54584. /* compare EC_POINT */
  54585. ExpectIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  54586. dupKey->pub_key, NULL), MP_EQ);
  54587. /* compare BIGNUM */
  54588. ExpectIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  54589. wolfSSL_EC_KEY_free(dupKey);
  54590. dupKey = NULL;
  54591. /* Invalid cases */
  54592. /* NULL key */
  54593. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  54594. /* NULL ecc_key */
  54595. if (ecKey != NULL) {
  54596. wc_ecc_free((ecc_key*)ecKey->internal);
  54597. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  54598. ecKey->internal = NULL; /* Set ecc_key to NULL */
  54599. }
  54600. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54601. wolfSSL_EC_KEY_free(ecKey);
  54602. ecKey = NULL;
  54603. wolfSSL_EC_KEY_free(dupKey);
  54604. dupKey = NULL;
  54605. /* NULL Group */
  54606. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  54607. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  54608. if (ecKey != NULL) {
  54609. wolfSSL_EC_GROUP_free(ecKey->group);
  54610. ecKey->group = NULL; /* Set group to NULL */
  54611. }
  54612. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54613. wolfSSL_EC_KEY_free(ecKey);
  54614. ecKey = NULL;
  54615. wolfSSL_EC_KEY_free(dupKey);
  54616. dupKey = NULL;
  54617. /* NULL public key */
  54618. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  54619. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  54620. if (ecKey != NULL) {
  54621. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  54622. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  54623. }
  54624. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54625. if (ecKey != NULL) {
  54626. wolfSSL_EC_POINT_free(ecKey->pub_key);
  54627. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  54628. }
  54629. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54630. wolfSSL_EC_KEY_free(ecKey);
  54631. ecKey = NULL;
  54632. wolfSSL_EC_KEY_free(dupKey);
  54633. dupKey = NULL;
  54634. /* NULL private key */
  54635. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  54636. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  54637. if (ecKey != NULL) {
  54638. wolfSSL_BN_free(ecKey->priv_key);
  54639. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  54640. }
  54641. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54642. wolfSSL_EC_KEY_free(ecKey);
  54643. ecKey = NULL;
  54644. wolfSSL_EC_KEY_free(dupKey);
  54645. dupKey = NULL;
  54646. /* Test EC_KEY_up_ref */
  54647. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  54648. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  54649. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  54650. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  54651. /* reference count doesn't follow duplicate */
  54652. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54653. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  54654. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  54655. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  54656. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  54657. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  54658. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  54659. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  54660. #endif
  54661. return EXPECT_RESULT();
  54662. }
  54663. static int test_wolfSSL_EC_KEY_set_group(void)
  54664. {
  54665. EXPECT_DECLS;
  54666. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  54667. defined(OPENSSL_EXTRA)
  54668. EC_KEY *key = NULL;
  54669. EC_GROUP *group = NULL;
  54670. const EC_GROUP *group2 = NULL;
  54671. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  54672. ExpectNotNull(key = EC_KEY_new());
  54673. ExpectNull(EC_KEY_get0_group(NULL));
  54674. ExpectIntEQ(EC_KEY_set_group(NULL, NULL), 0);
  54675. ExpectIntEQ(EC_KEY_set_group(key, NULL), 0);
  54676. ExpectIntEQ(EC_KEY_set_group(NULL, group), 0);
  54677. ExpectIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  54678. ExpectNotNull(group2 = EC_KEY_get0_group(key));
  54679. ExpectIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  54680. EC_GROUP_free(group);
  54681. EC_KEY_free(key);
  54682. #endif
  54683. return EXPECT_RESULT();
  54684. }
  54685. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  54686. {
  54687. EXPECT_DECLS;
  54688. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  54689. BIO* bio = NULL;
  54690. EC_KEY* key = NULL;
  54691. /* Error condition: NULL key. */
  54692. ExpectIntLT(EC_KEY_get_conv_form(NULL), 0);
  54693. ExpectNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  54694. ExpectNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  54695. /* Conversion form defaults to uncompressed. */
  54696. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  54697. #ifdef HAVE_COMP_KEY
  54698. /* Explicitly set to compressed. */
  54699. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  54700. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  54701. #else
  54702. /* Will still work just won't change anything. */
  54703. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  54704. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  54705. EC_KEY_set_conv_form(key, POINT_CONVERSION_UNCOMPRESSED);
  54706. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  54707. #endif
  54708. EC_KEY_set_conv_form(NULL, POINT_CONVERSION_UNCOMPRESSED);
  54709. BIO_free(bio);
  54710. EC_KEY_free(key);
  54711. #endif
  54712. return EXPECT_RESULT();
  54713. }
  54714. static int test_wolfSSL_EC_KEY_private_key(void)
  54715. {
  54716. EXPECT_DECLS;
  54717. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  54718. WOLFSSL_EC_KEY* key = NULL;
  54719. WOLFSSL_BIGNUM* priv = NULL;
  54720. WOLFSSL_BIGNUM* priv2 = NULL;
  54721. WOLFSSL_BIGNUM* bn;
  54722. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54723. ExpectNotNull(priv = wolfSSL_BN_new());
  54724. ExpectNotNull(priv2 = wolfSSL_BN_new());
  54725. ExpectIntNE(BN_set_word(priv, 2), 0);
  54726. ExpectIntNE(BN_set_word(priv2, 2), 0);
  54727. ExpectNull(wolfSSL_EC_KEY_get0_private_key(NULL));
  54728. /* No private key set. */
  54729. ExpectNull(wolfSSL_EC_KEY_get0_private_key(key));
  54730. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, NULL), 0);
  54731. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, NULL), 0);
  54732. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, priv), 0);
  54733. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv), 1);
  54734. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  54735. ExpectPtrNE(bn, priv);
  54736. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv2), 1);
  54737. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  54738. ExpectPtrNE(bn, priv2);
  54739. wolfSSL_BN_free(priv2);
  54740. wolfSSL_BN_free(priv);
  54741. wolfSSL_EC_KEY_free(key);
  54742. #endif
  54743. return EXPECT_RESULT();
  54744. }
  54745. static int test_wolfSSL_EC_KEY_public_key(void)
  54746. {
  54747. EXPECT_DECLS;
  54748. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  54749. WOLFSSL_EC_KEY* key = NULL;
  54750. WOLFSSL_EC_POINT* pub = NULL;
  54751. WOLFSSL_EC_POINT* point = NULL;
  54752. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54753. ExpectNull(wolfSSL_EC_KEY_get0_public_key(NULL));
  54754. ExpectNotNull(wolfSSL_EC_KEY_get0_public_key(key));
  54755. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  54756. ExpectNotNull(pub = wolfSSL_EC_KEY_get0_public_key(key));
  54757. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, NULL), 0);
  54758. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, NULL), 0);
  54759. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, pub), 0);
  54760. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, pub), 1);
  54761. ExpectNotNull(point = wolfSSL_EC_KEY_get0_public_key(key));
  54762. ExpectPtrEq(point, pub);
  54763. wolfSSL_EC_KEY_free(key);
  54764. #endif
  54765. return EXPECT_RESULT();
  54766. }
  54767. static int test_wolfSSL_EC_KEY_print_fp(void)
  54768. {
  54769. EXPECT_DECLS;
  54770. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  54771. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
  54772. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  54773. !defined(NO_STDIO_FILESYSTEM)
  54774. EC_KEY* key = NULL;
  54775. /* Bad file pointer. */
  54776. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  54777. /* NULL key. */
  54778. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
  54779. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  54780. /* Negative indent. */
  54781. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
  54782. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  54783. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  54784. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  54785. wolfSSL_EC_KEY_free(key);
  54786. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  54787. NID_X9_62_prime256v1)));
  54788. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  54789. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  54790. wolfSSL_EC_KEY_free(key);
  54791. #endif
  54792. return EXPECT_RESULT();
  54793. }
  54794. static int test_wolfSSL_EC_get_builtin_curves(void)
  54795. {
  54796. EXPECT_DECLS;
  54797. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  54798. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  54799. EC_builtin_curve* curves = NULL;
  54800. size_t crv_len = 0;
  54801. size_t i = 0;
  54802. ExpectIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  54803. ExpectNotNull(curves = (EC_builtin_curve*)XMALLOC(
  54804. sizeof(EC_builtin_curve) * crv_len, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  54805. ExpectIntEQ((EC_get_builtin_curves(curves, 0)), crv_len);
  54806. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  54807. for (i = 0; EXPECT_SUCCESS() && (i < crv_len); i++) {
  54808. if (curves[i].comment != NULL) {
  54809. ExpectStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  54810. }
  54811. }
  54812. if (crv_len > 1) {
  54813. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len - 1), crv_len - 1);
  54814. }
  54815. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54816. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  54817. #endif /* OPENSSL_EXTRA || OPENSSL_ALL */
  54818. return EXPECT_RESULT();
  54819. }
  54820. static int test_wolfSSL_ECDSA_SIG(void)
  54821. {
  54822. EXPECT_DECLS;
  54823. #ifdef OPENSSL_EXTRA
  54824. WOLFSSL_ECDSA_SIG* sig = NULL;
  54825. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  54826. WOLFSSL_BIGNUM* r = NULL;
  54827. WOLFSSL_BIGNUM* s = NULL;
  54828. const WOLFSSL_BIGNUM* r2 = NULL;
  54829. const WOLFSSL_BIGNUM* s2 = NULL;
  54830. const unsigned char* cp = NULL;
  54831. unsigned char* p = NULL;
  54832. unsigned char outSig[8];
  54833. unsigned char sigData[8] =
  54834. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  54835. unsigned char sigDataBad[8] =
  54836. { 0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  54837. wolfSSL_ECDSA_SIG_free(NULL);
  54838. ExpectNotNull(sig = wolfSSL_ECDSA_SIG_new());
  54839. ExpectNotNull(r = wolfSSL_BN_new());
  54840. ExpectNotNull(s = wolfSSL_BN_new());
  54841. ExpectIntEQ(wolfSSL_BN_set_word(r, 1), 1);
  54842. ExpectIntEQ(wolfSSL_BN_set_word(s, 1), 1);
  54843. wolfSSL_ECDSA_SIG_get0(NULL, NULL, NULL);
  54844. wolfSSL_ECDSA_SIG_get0(NULL, &r2, NULL);
  54845. wolfSSL_ECDSA_SIG_get0(NULL, NULL, &s2);
  54846. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  54847. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, NULL), 0);
  54848. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, NULL), 0);
  54849. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, NULL), 0);
  54850. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, s), 0);
  54851. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, s), 0);
  54852. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, s), 0);
  54853. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, NULL), 0);
  54854. r2 = NULL;
  54855. s2 = NULL;
  54856. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  54857. ExpectNull(r2);
  54858. ExpectNull(s2);
  54859. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, s), 1);
  54860. if (EXPECT_FAIL()) {
  54861. wolfSSL_BN_free(r);
  54862. wolfSSL_BN_free(s);
  54863. }
  54864. wolfSSL_ECDSA_SIG_get0(sig, &r2, &s2);
  54865. ExpectPtrEq(r2, r);
  54866. ExpectPtrEq(s2, s);
  54867. r2 = NULL;
  54868. wolfSSL_ECDSA_SIG_get0(sig, &r2, NULL);
  54869. ExpectPtrEq(r2, r);
  54870. s2 = NULL;
  54871. wolfSSL_ECDSA_SIG_get0(sig, NULL, &s2);
  54872. ExpectPtrEq(s2, s);
  54873. /* r and s are freed when sig is freed. */
  54874. wolfSSL_ECDSA_SIG_free(sig);
  54875. sig = NULL;
  54876. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData)));
  54877. cp = sigDataBad;
  54878. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigDataBad)));
  54879. cp = sigData;
  54880. ExpectNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  54881. ExpectIntEQ((cp == sigData + 8), 1);
  54882. cp = sigData;
  54883. ExpectNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  54884. ExpectNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  54885. ExpectIntEQ((sig == sig2), 1);
  54886. cp = outSig;
  54887. p = outSig;
  54888. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  54889. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  54890. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  54891. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  54892. ExpectIntEQ((p == outSig + 8), 1);
  54893. ExpectIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  54894. wolfSSL_ECDSA_SIG_free(sig);
  54895. #endif
  54896. return EXPECT_RESULT();
  54897. }
  54898. static int test_ECDSA_size_sign(void)
  54899. {
  54900. EXPECT_DECLS;
  54901. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  54902. EC_KEY* key = NULL;
  54903. ECDSA_SIG* ecdsaSig = NULL;
  54904. int id;
  54905. byte hash[WC_MAX_DIGEST_SIZE];
  54906. byte hash2[WC_MAX_DIGEST_SIZE];
  54907. byte sig[ECC_MAX_SIG_SIZE];
  54908. unsigned int sigSz = sizeof(sig);
  54909. XMEMSET(hash, 123, sizeof(hash));
  54910. XMEMSET(hash2, 234, sizeof(hash2));
  54911. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  54912. ExpectIntEQ(id, ECC_SECP256R1);
  54913. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54914. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  54915. ExpectIntGE(ECDSA_size(NULL), 0);
  54916. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, NULL), 0);
  54917. ExpectIntEQ(ECDSA_sign(0, NULL, sizeof(hash), sig, &sigSz, key), 0);
  54918. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), NULL, &sigSz, key), 0);
  54919. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, NULL), 0);
  54920. ExpectIntEQ(ECDSA_verify(0, NULL, sizeof(hash), sig, sigSz, key), 0);
  54921. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), NULL, sigSz, key), 0);
  54922. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  54923. ExpectIntGE(ECDSA_size(key), sigSz);
  54924. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
  54925. ExpectIntEQ(ECDSA_verify(0, hash2, sizeof(hash2), sig, sigSz, key), 0);
  54926. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), NULL));
  54927. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), key));
  54928. ExpectNull(ECDSA_do_sign(hash, sizeof(hash), NULL));
  54929. ExpectNotNull(ecdsaSig = ECDSA_do_sign(hash, sizeof(hash), key));
  54930. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, NULL), -1);
  54931. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, NULL), -1);
  54932. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, NULL), -1);
  54933. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, key), -1);
  54934. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, key), -1);
  54935. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, key), -1);
  54936. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, NULL), -1);
  54937. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, key), 1);
  54938. ExpectIntEQ(ECDSA_do_verify(hash2, sizeof(hash2), ecdsaSig, key), 0);
  54939. ECDSA_SIG_free(ecdsaSig);
  54940. EC_KEY_free(key);
  54941. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP */
  54942. return EXPECT_RESULT();
  54943. }
  54944. static int test_ECDH_compute_key(void)
  54945. {
  54946. EXPECT_DECLS;
  54947. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  54948. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  54949. EC_KEY* key1 = NULL;
  54950. EC_KEY* key2 = NULL;
  54951. EC_POINT* pub1 = NULL;
  54952. EC_POINT* pub2 = NULL;
  54953. byte secret1[32];
  54954. byte secret2[32];
  54955. int i;
  54956. ExpectNotNull(key1 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54957. ExpectIntEQ(EC_KEY_generate_key(key1), 1);
  54958. ExpectNotNull(pub1 = wolfSSL_EC_KEY_get0_public_key(key1));
  54959. ExpectNotNull(key2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54960. ExpectIntEQ(EC_KEY_generate_key(key2), 1);
  54961. ExpectNotNull(pub2 = wolfSSL_EC_KEY_get0_public_key(key2));
  54962. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, NULL, NULL), 0);
  54963. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, NULL, NULL),
  54964. 0);
  54965. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, NULL, NULL), 0);
  54966. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, key1, NULL), 0);
  54967. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, key1, NULL), 0);
  54968. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, key1, NULL),
  54969. 0);
  54970. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, NULL, NULL),
  54971. 0);
  54972. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1) - 16, pub2, key1,
  54973. NULL), 0);
  54974. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, key1, NULL),
  54975. sizeof(secret1));
  54976. ExpectIntEQ(ECDH_compute_key(secret2, sizeof(secret2), pub1, key2, NULL),
  54977. sizeof(secret2));
  54978. for (i = 0; i < (int)sizeof(secret1); i++) {
  54979. ExpectIntEQ(secret1[i], secret2[i]);
  54980. }
  54981. EC_KEY_free(key2);
  54982. EC_KEY_free(key1);
  54983. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP &&
  54984. * !WOLF_CRYPTO_CB_ONLY_ECC */
  54985. return EXPECT_RESULT();
  54986. }
  54987. #endif /* HAVE_ECC && !OPENSSL_NO_PK */
  54988. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  54989. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  54990. !defined(NO_ASN_TIME)
  54991. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey,
  54992. int expectedDerSz)
  54993. {
  54994. EXPECT_DECLS;
  54995. X509* x509 = NULL;
  54996. BIGNUM* serial_number = NULL;
  54997. X509_NAME* name = NULL;
  54998. time_t epoch_off = 0;
  54999. ASN1_INTEGER* asn1_serial_number;
  55000. long not_before, not_after;
  55001. int derSz;
  55002. ExpectNotNull(x509 = X509_new());
  55003. ExpectIntNE(X509_set_pubkey(x509, pkey), 0);
  55004. ExpectNotNull(serial_number = BN_new());
  55005. ExpectIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  55006. ExpectNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  55007. ExpectNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  55008. /* version 3 */
  55009. ExpectIntNE(X509_set_version(x509, 2L), 0);
  55010. ExpectNotNull(name = X509_NAME_new());
  55011. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  55012. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  55013. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_pkcs9_contentType,
  55014. MBSTRING_UTF8,(unsigned char*)"Server", -1, -1, 0), 0);
  55015. ExpectIntNE(X509_set_subject_name(x509, name), 0);
  55016. ExpectIntNE(X509_set_issuer_name(x509, name), 0);
  55017. not_before = (long)wc_Time(NULL);
  55018. not_after = not_before + (365 * 24 * 60 * 60);
  55019. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before,
  55020. &epoch_off));
  55021. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after,
  55022. &epoch_off));
  55023. ExpectIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  55024. ExpectNotNull(wolfSSL_X509_get_der(x509, &derSz));
  55025. ExpectIntGE(derSz, expectedDerSz);
  55026. BN_free(serial_number);
  55027. X509_NAME_free(name);
  55028. X509_free(x509);
  55029. return EXPECT_RESULT();
  55030. }
  55031. #endif
  55032. static int test_openssl_generate_key_and_cert(void)
  55033. {
  55034. EXPECT_DECLS;
  55035. #if defined(OPENSSL_EXTRA)
  55036. int expectedDerSz;
  55037. EVP_PKEY* pkey = NULL;
  55038. #ifdef HAVE_ECC
  55039. EC_KEY* ec_key = NULL;
  55040. #endif
  55041. #if !defined(NO_RSA)
  55042. int key_length = 2048;
  55043. BIGNUM* exponent = NULL;
  55044. RSA* rsa = NULL;
  55045. ExpectNotNull(pkey = EVP_PKEY_new());
  55046. ExpectNotNull(exponent = BN_new());
  55047. ExpectNotNull(rsa = RSA_new());
  55048. ExpectIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  55049. #ifndef WOLFSSL_KEY_GEN
  55050. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  55051. #if defined(USE_CERT_BUFFERS_1024)
  55052. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  55053. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  55054. key_length = 1024;
  55055. #elif defined(USE_CERT_BUFFERS_2048)
  55056. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  55057. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  55058. #else
  55059. RSA_free(rsa);
  55060. rsa = NULL;
  55061. #endif
  55062. #else
  55063. ExpectIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  55064. ExpectIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  55065. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  55066. ExpectIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  55067. #endif
  55068. if (rsa) {
  55069. ExpectIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  55070. if (EXPECT_FAIL()) {
  55071. RSA_free(rsa);
  55072. }
  55073. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  55074. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  55075. expectedDerSz = 743;
  55076. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey,
  55077. expectedDerSz), TEST_SUCCESS);
  55078. #endif
  55079. }
  55080. EVP_PKEY_free(pkey);
  55081. pkey = NULL;
  55082. BN_free(exponent);
  55083. #endif /* !NO_RSA */
  55084. #ifdef HAVE_ECC
  55085. ExpectNotNull(pkey = EVP_PKEY_new());
  55086. ExpectNotNull(ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  55087. #ifndef NO_WOLFSSL_STUB
  55088. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  55089. #endif
  55090. ExpectIntNE(EC_KEY_generate_key(ec_key), 0);
  55091. ExpectIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  55092. if (EXPECT_FAIL()) {
  55093. EC_KEY_free(ec_key);
  55094. }
  55095. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  55096. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  55097. expectedDerSz = 344;
  55098. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey, expectedDerSz),
  55099. TEST_SUCCESS);
  55100. #endif
  55101. EVP_PKEY_free(pkey);
  55102. #endif /* HAVE_ECC */
  55103. (void)pkey;
  55104. (void)expectedDerSz;
  55105. #endif /* OPENSSL_EXTRA */
  55106. return EXPECT_RESULT();
  55107. }
  55108. static int test_stubs_are_stubs(void)
  55109. {
  55110. EXPECT_DECLS;
  55111. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB) && \
  55112. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  55113. WOLFSSL_CTX* ctx = NULL;
  55114. WOLFSSL_CTX* ctxN = NULL;
  55115. #ifndef NO_WOLFSSL_CLIENT
  55116. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  55117. #elif !defined(NO_WOLFSSL_SERVER)
  55118. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  55119. #endif
  55120. #define CHECKZERO_RET(x, y, z) ExpectIntEQ((int) x(y), 0); \
  55121. ExpectIntEQ((int) x(z), 0)
  55122. /* test logic, all stubs return same result regardless of ctx being NULL
  55123. * as there are no sanity checks, it's just a stub! If at some
  55124. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  55125. * if invalid inputs are supplied. Test calling both
  55126. * with and without valid inputs, if a stub functionality remains unchanged.
  55127. */
  55128. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  55129. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  55130. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  55131. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  55132. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  55133. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  55134. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  55135. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  55136. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  55137. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  55138. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  55139. wolfSSL_CTX_free(ctx);
  55140. ctx = NULL;
  55141. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB && (!NO_WOLFSSL_CLIENT ||
  55142. * !NO_WOLFSSL_SERVER) */
  55143. return EXPECT_RESULT();
  55144. }
  55145. static int test_CONF_modules_xxx(void)
  55146. {
  55147. int res = TEST_SKIPPED;
  55148. #if defined(OPENSSL_EXTRA)
  55149. CONF_modules_free();
  55150. CONF_modules_unload(0);
  55151. CONF_modules_unload(1);
  55152. CONF_modules_unload(-1);
  55153. res = TEST_SUCCESS;
  55154. #endif /* OPENSSL_EXTRA */
  55155. return res;
  55156. }
  55157. static int test_CRYPTO_set_dynlock_xxx(void)
  55158. {
  55159. int res = TEST_SKIPPED;
  55160. #if defined(OPENSSL_EXTRA)
  55161. CRYPTO_set_dynlock_create_callback(
  55162. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  55163. CRYPTO_set_dynlock_create_callback(
  55164. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  55165. CRYPTO_set_dynlock_destroy_callback(
  55166. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  55167. CRYPTO_set_dynlock_destroy_callback(
  55168. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  55169. CRYPTO_set_dynlock_lock_callback(
  55170. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  55171. CRYPTO_set_dynlock_lock_callback(
  55172. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  55173. res = TEST_SUCCESS;
  55174. #endif /* OPENSSL_EXTRA */
  55175. return res;
  55176. }
  55177. static int test_CRYPTO_THREADID_xxx(void)
  55178. {
  55179. EXPECT_DECLS;
  55180. #if defined(OPENSSL_EXTRA)
  55181. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  55182. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  55183. ExpectIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  55184. #endif /* OPENSSL_EXTRA */
  55185. return EXPECT_RESULT();
  55186. }
  55187. static int test_ENGINE_cleanup(void)
  55188. {
  55189. int res = TEST_SKIPPED;
  55190. #if defined(OPENSSL_EXTRA)
  55191. ENGINE_cleanup();
  55192. res = TEST_SUCCESS;
  55193. #endif /* OPENSSL_EXTRA */
  55194. return res;
  55195. }
  55196. static int test_wolfSSL_CTX_LoadCRL(void)
  55197. {
  55198. EXPECT_DECLS;
  55199. #if defined(HAVE_CRL) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  55200. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  55201. WOLFSSL_CTX* ctx = NULL;
  55202. WOLFSSL* ssl = NULL;
  55203. const char* badPath = "dummypath";
  55204. const char* validPath = "./certs/crl";
  55205. const char* validFilePath = "./certs/crl/cliCrl.pem";
  55206. const char* issuerCert = "./certs/client-cert.pem";
  55207. int derType = WOLFSSL_FILETYPE_ASN1;
  55208. int pemType = WOLFSSL_FILETYPE_PEM;
  55209. #ifdef HAVE_CRL_MONITOR
  55210. int monitor = WOLFSSL_CRL_MONITOR;
  55211. #else
  55212. int monitor = 0;
  55213. #endif
  55214. WOLFSSL_CERT_MANAGER* cm = NULL;
  55215. #define FAIL_T1(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  55216. BAD_FUNC_ARG)
  55217. #define FAIL_T2(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  55218. NOT_COMPILED_IN)
  55219. #define SUCC_T(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  55220. WOLFSSL_SUCCESS)
  55221. #ifndef NO_WOLFSSL_CLIENT
  55222. #define NEW_CTX(ctx) ExpectNotNull( \
  55223. (ctx) = wolfSSL_CTX_new(wolfSSLv23_client_method()))
  55224. #elif !defined(NO_WOLFSSL_SERVER)
  55225. #define NEW_CTX(ctx) ExpectNotNull( \
  55226. (ctx) = wolfSSL_CTX_new(wolfSSLv23_server_method()))
  55227. #else
  55228. #define NEW_CTX(ctx) return
  55229. #endif
  55230. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  55231. NEW_CTX(ctx);
  55232. #ifndef HAVE_CRL_MONITOR
  55233. FAIL_T2(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, WOLFSSL_CRL_MONITOR);
  55234. wolfSSL_CTX_free(ctx);
  55235. NEW_CTX(ctx);
  55236. #endif
  55237. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  55238. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  55239. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  55240. wolfSSL_CTX_free(ctx);
  55241. ctx = NULL;
  55242. NEW_CTX(ctx);
  55243. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  55244. WOLFSSL_SUCCESS);
  55245. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  55246. wolfSSL_CTX_free(ctx);
  55247. ctx = NULL;
  55248. NEW_CTX(ctx);
  55249. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  55250. WOLFSSL_SUCCESS);
  55251. ExpectNotNull(ssl = wolfSSL_new(ctx));
  55252. ExpectIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  55253. wolfSSL_free(ssl);
  55254. ssl = NULL;
  55255. wolfSSL_CTX_free(ctx);
  55256. ctx = NULL;
  55257. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55258. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  55259. WOLFSSL_SUCCESS);
  55260. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType),
  55261. WOLFSSL_SUCCESS);
  55262. wolfSSL_CertManagerFree(cm);
  55263. #endif
  55264. return EXPECT_RESULT();
  55265. }
  55266. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  55267. static int test_multiple_crls_same_issuer_ctx_ready(WOLFSSL_CTX* ctx)
  55268. {
  55269. EXPECT_DECLS;
  55270. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  55271. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, "./certs/crl/crl.pem",
  55272. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  55273. return EXPECT_RESULT();
  55274. }
  55275. #endif
  55276. static int test_multiple_crls_same_issuer(void)
  55277. {
  55278. EXPECT_DECLS;
  55279. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  55280. test_ssl_cbf client_cbs, server_cbs;
  55281. struct {
  55282. const char* server_cert;
  55283. const char* server_key;
  55284. } test_params[] = {
  55285. { "./certs/server-cert.pem", "./certs/server-key.pem" },
  55286. { "./certs/server-revoked-cert.pem", "./certs/server-revoked-key.pem" }
  55287. };
  55288. size_t i;
  55289. for (i = 0; i < (sizeof(test_params)/sizeof(*test_params)); i++) {
  55290. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  55291. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  55292. server_cbs.certPemFile = test_params[i].server_cert;
  55293. server_cbs.keyPemFile = test_params[i].server_key;
  55294. client_cbs.crlPemFile = "./certs/crl/extra-crls/general-server-crl.pem";
  55295. client_cbs.ctx_ready = test_multiple_crls_same_issuer_ctx_ready;
  55296. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  55297. &server_cbs, NULL), TEST_FAIL);
  55298. }
  55299. #endif
  55300. return EXPECT_RESULT();
  55301. }
  55302. static int test_SetTmpEC_DHE_Sz(void)
  55303. {
  55304. EXPECT_DECLS;
  55305. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  55306. WOLFSSL_CTX *ctx = NULL;
  55307. WOLFSSL *ssl = NULL;
  55308. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  55309. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  55310. ExpectNotNull(ssl = wolfSSL_new(ctx));
  55311. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  55312. wolfSSL_free(ssl);
  55313. wolfSSL_CTX_free(ctx);
  55314. #endif
  55315. return EXPECT_RESULT();
  55316. }
  55317. static int test_wolfSSL_CTX_get0_privatekey(void)
  55318. {
  55319. EXPECT_DECLS;
  55320. #ifdef OPENSSL_ALL
  55321. WOLFSSL_CTX* ctx = NULL;
  55322. (void)ctx;
  55323. #ifndef NO_RSA
  55324. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  55325. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  55326. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  55327. WOLFSSL_FILETYPE_PEM));
  55328. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  55329. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  55330. WOLFSSL_FILETYPE_PEM));
  55331. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  55332. wolfSSL_CTX_free(ctx);
  55333. ctx = NULL;
  55334. #endif
  55335. #ifdef HAVE_ECC
  55336. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  55337. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  55338. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  55339. WOLFSSL_FILETYPE_PEM));
  55340. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  55341. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  55342. WOLFSSL_FILETYPE_PEM));
  55343. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  55344. wolfSSL_CTX_free(ctx);
  55345. #endif
  55346. #endif
  55347. return EXPECT_RESULT();
  55348. }
  55349. static int test_wolfSSL_dtls_set_mtu(void)
  55350. {
  55351. EXPECT_DECLS;
  55352. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  55353. !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS) && \
  55354. !defined(WOLFSSL_NO_TLS12)
  55355. WOLFSSL_CTX* ctx = NULL;
  55356. WOLFSSL* ssl = NULL;
  55357. const char* testCertFile;
  55358. const char* testKeyFile;
  55359. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  55360. #ifndef NO_RSA
  55361. testCertFile = svrCertFile;
  55362. testKeyFile = svrKeyFile;
  55363. #elif defined(HAVE_ECC)
  55364. testCertFile = eccCertFile;
  55365. testKeyFile = eccKeyFile;
  55366. #endif
  55367. if (testCertFile != NULL && testKeyFile != NULL) {
  55368. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  55369. WOLFSSL_FILETYPE_PEM));
  55370. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  55371. WOLFSSL_FILETYPE_PEM));
  55372. }
  55373. ExpectNotNull(ssl = wolfSSL_new(ctx));
  55374. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  55375. ExpectIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  55376. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  55377. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  55378. ExpectIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  55379. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  55380. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  55381. wolfSSL_free(ssl);
  55382. wolfSSL_CTX_free(ctx);
  55383. #endif
  55384. return EXPECT_RESULT();
  55385. }
  55386. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  55387. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  55388. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  55389. enum HandShakeType hsType, word16 length)
  55390. {
  55391. size_t idx = 0;
  55392. byte* l;
  55393. /* record layer */
  55394. /* handshake type */
  55395. out[idx++] = handshake;
  55396. /* protocol version */
  55397. out[idx++] = 0xfe;
  55398. out[idx++] = 0xfd; /* DTLS 1.2 */
  55399. /* epoch 0 */
  55400. XMEMSET(out + idx, 0, 2);
  55401. idx += 2;
  55402. /* sequence number */
  55403. XMEMSET(out + idx, 0, 6);
  55404. c32toa(seq, out + idx + 2);
  55405. idx += 6;
  55406. /* length in BE */
  55407. if (length)
  55408. c16toa(length, out + idx);
  55409. else
  55410. c16toa(outSz - idx - 2, out + idx);
  55411. idx += 2;
  55412. /* handshake layer */
  55413. /* handshake type */
  55414. out[idx++] = (byte)hsType;
  55415. /* length */
  55416. l = out + idx;
  55417. idx += 3;
  55418. /* message seq */
  55419. c16toa(0, out + idx);
  55420. idx += 2;
  55421. /* frag offset */
  55422. c32to24(0, out + idx);
  55423. idx += 3;
  55424. /* frag length */
  55425. c32to24((word32)outSz - (word32)idx - 3, l);
  55426. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  55427. idx += 3;
  55428. XMEMSET(out + idx, 0, outSz - idx);
  55429. }
  55430. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  55431. {
  55432. byte msg[] = "This is a msg for the client";
  55433. byte reply[40];
  55434. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  55435. reply[sizeof(reply) - 1] = '\0';
  55436. fprintf(stderr, "Client message: %s\n", reply);
  55437. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  55438. }
  55439. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  55440. {
  55441. byte ch[50];
  55442. int fd = wolfSSL_get_fd(ssl);
  55443. byte msg[] = "This is a msg for the server";
  55444. byte reply[40];
  55445. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  55446. /* Server should ignore this datagram */
  55447. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  55448. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  55449. /* Server should ignore this datagram */
  55450. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  55451. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  55452. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  55453. reply[sizeof(reply) - 1] = '\0';
  55454. fprintf(stderr, "Server response: %s\n", reply);
  55455. }
  55456. static int test_wolfSSL_dtls_plaintext(void)
  55457. {
  55458. callback_functions func_cb_client;
  55459. callback_functions func_cb_server;
  55460. size_t i;
  55461. struct test_params {
  55462. method_provider client_meth;
  55463. method_provider server_meth;
  55464. ssl_callback on_result_server;
  55465. ssl_callback on_result_client;
  55466. } params[] = {
  55467. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  55468. test_wolfSSL_dtls_plaintext_server,
  55469. test_wolfSSL_dtls_plaintext_client},
  55470. };
  55471. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  55472. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  55473. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  55474. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  55475. func_cb_server.method = params[i].server_meth;
  55476. func_cb_client.method = params[i].client_meth;
  55477. func_cb_client.on_result = params[i].on_result_client;
  55478. func_cb_server.on_result = params[i].on_result_server;
  55479. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  55480. if (!func_cb_client.return_code)
  55481. return TEST_FAIL;
  55482. if (!func_cb_server.return_code)
  55483. return TEST_FAIL;
  55484. }
  55485. return TEST_RES_CHECK(1);
  55486. }
  55487. #else
  55488. static int test_wolfSSL_dtls_plaintext(void) {
  55489. return TEST_SKIPPED;
  55490. }
  55491. #endif
  55492. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  55493. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  55494. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  55495. {
  55496. byte b[1100]; /* buffer for the messages to send */
  55497. size_t idx = 0;
  55498. size_t seq_offset = 0;
  55499. size_t msg_offset = 0;
  55500. int i;
  55501. int fd = wolfSSL_get_fd(ssl);
  55502. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  55503. word32 seq_number = 100; /* start high so server definitely reads this */
  55504. word16 msg_number = 50; /* start high so server has to buffer this */
  55505. AssertIntEQ(ret, 1);
  55506. /* Now let's start spamming the peer with fragments it needs to store */
  55507. XMEMSET(b, -1, sizeof(b));
  55508. /* record layer */
  55509. /* handshake type */
  55510. b[idx++] = 22;
  55511. /* protocol version */
  55512. b[idx++] = 0xfe;
  55513. b[idx++] = 0xfd; /* DTLS 1.2 */
  55514. /* epoch 0 */
  55515. XMEMSET(b + idx, 0, 2);
  55516. idx += 2;
  55517. /* sequence number */
  55518. XMEMSET(b + idx, 0, 6);
  55519. seq_offset = idx + 2; /* increment only the low 32 bits */
  55520. idx += 6;
  55521. /* static length in BE */
  55522. c16toa(42, b + idx);
  55523. idx += 2;
  55524. /* handshake layer */
  55525. /* cert type */
  55526. b[idx++] = 11;
  55527. /* length */
  55528. c32to24(1000, b + idx);
  55529. idx += 3;
  55530. /* message seq */
  55531. c16toa(0, b + idx);
  55532. msg_offset = idx;
  55533. idx += 2;
  55534. /* frag offset */
  55535. c32to24(500, b + idx);
  55536. idx += 3;
  55537. /* frag length */
  55538. c32to24(30, b + idx);
  55539. idx += 3;
  55540. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  55541. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  55542. seq_number++, msg_number++, i++) {
  55543. struct timespec delay;
  55544. XMEMSET(&delay, 0, sizeof(delay));
  55545. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  55546. c32toa(seq_number, b + seq_offset);
  55547. c16toa(msg_number, b + msg_offset);
  55548. ret = (int)send(fd, b, 55, 0);
  55549. nanosleep(&delay, NULL);
  55550. }
  55551. }
  55552. #ifdef WOLFSSL_DTLS13
  55553. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  55554. {
  55555. const word16 sendCountMax = 100;
  55556. byte b[150]; /* buffer for the messages to send */
  55557. size_t idx = 0;
  55558. size_t msg_offset = 0;
  55559. int fd = wolfSSL_get_fd(ssl);
  55560. word16 msg_number = 10; /* start high so server has to buffer this */
  55561. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  55562. AssertIntEQ(ret, 1);
  55563. /* Now let's start spamming the peer with fragments it needs to store */
  55564. XMEMSET(b, -1, sizeof(b));
  55565. /* handshake type */
  55566. b[idx++] = 11;
  55567. /* length */
  55568. c32to24(10000, b + idx);
  55569. idx += 3;
  55570. /* message_seq */
  55571. msg_offset = idx;
  55572. idx += 2;
  55573. /* fragment_offset */
  55574. c32to24(5000, b + idx);
  55575. idx += 3;
  55576. /* fragment_length */
  55577. c32to24(100, b + idx);
  55578. idx += 3;
  55579. /* fragment contents */
  55580. idx += 100;
  55581. for (; ret > 0 && msg_number < sendCountMax; msg_number++) {
  55582. byte sendBuf[150];
  55583. int sendSz = sizeof(sendBuf);
  55584. struct timespec delay;
  55585. XMEMSET(&delay, 0, sizeof(delay));
  55586. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  55587. c16toa(msg_number, b + msg_offset);
  55588. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  55589. (int)idx, handshake, 0, 0, 0);
  55590. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  55591. nanosleep(&delay, NULL);
  55592. }
  55593. }
  55594. #endif
  55595. static int test_wolfSSL_dtls_fragments(void)
  55596. {
  55597. EXPECT_DECLS;
  55598. callback_functions func_cb_client;
  55599. callback_functions func_cb_server;
  55600. size_t i;
  55601. struct test_params {
  55602. method_provider client_meth;
  55603. method_provider server_meth;
  55604. ssl_callback spammer;
  55605. } params[] = {
  55606. #if !defined(WOLFSSL_NO_TLS12)
  55607. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  55608. test_wolfSSL_dtls12_fragments_spammer},
  55609. #endif
  55610. #ifdef WOLFSSL_DTLS13
  55611. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  55612. test_wolfSSL_dtls13_fragments_spammer},
  55613. #endif
  55614. };
  55615. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  55616. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  55617. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  55618. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  55619. func_cb_server.method = params[i].server_meth;
  55620. func_cb_client.method = params[i].client_meth;
  55621. func_cb_client.ssl_ready = params[i].spammer;
  55622. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  55623. ExpectFalse(func_cb_client.return_code);
  55624. ExpectFalse(func_cb_server.return_code);
  55625. /* The socket should be closed by the server resulting in a
  55626. * socket error, fatal error or reading a close notify alert */
  55627. if (func_cb_client.last_err != SOCKET_ERROR_E &&
  55628. func_cb_client.last_err != WOLFSSL_ERROR_ZERO_RETURN &&
  55629. func_cb_client.last_err != FATAL_ERROR) {
  55630. ExpectIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  55631. }
  55632. /* Check the server returned an error indicating the msg buffer
  55633. * was full */
  55634. ExpectIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  55635. if (EXPECT_FAIL())
  55636. break;
  55637. }
  55638. return EXPECT_RESULT();
  55639. }
  55640. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  55641. {
  55642. int fd, ret;
  55643. byte alert_msg[] = {
  55644. 0x15, /* alert type */
  55645. 0xfe, 0xfd, /* version */
  55646. 0x00, 0x00, /* epoch */
  55647. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  55648. 0x00, 0x02, /* length */
  55649. 0x02, /* level: fatal */
  55650. 0x46 /* protocol version */
  55651. };
  55652. fd = wolfSSL_get_fd(ssl);
  55653. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  55654. AssertIntGT(ret, 0);
  55655. }
  55656. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  55657. {
  55658. callback_functions client_cbs, server_cbs;
  55659. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  55660. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  55661. client_cbs.doUdp = server_cbs.doUdp = 1;
  55662. if (version12) {
  55663. #if !defined(WOLFSSL_NO_TLS12)
  55664. client_cbs.method = wolfDTLSv1_2_client_method;
  55665. server_cbs.method = wolfDTLSv1_2_server_method;
  55666. #else
  55667. return TEST_SKIPPED;
  55668. #endif
  55669. }
  55670. else
  55671. {
  55672. #ifdef WOLFSSL_DTLS13
  55673. client_cbs.method = wolfDTLSv1_3_client_method;
  55674. server_cbs.method = wolfDTLSv1_3_server_method;
  55675. #else
  55676. return TEST_SKIPPED;
  55677. #endif /* WOLFSSL_DTLS13 */
  55678. }
  55679. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  55680. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  55681. if (!client_cbs.return_code)
  55682. return TEST_FAIL;
  55683. if (!server_cbs.return_code)
  55684. return TEST_FAIL;
  55685. return TEST_SUCCESS;
  55686. }
  55687. static int test_wolfSSL_ignore_alert_before_cookie(void)
  55688. {
  55689. int ret;
  55690. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  55691. if (ret != 0)
  55692. return ret;
  55693. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  55694. if (ret != 0)
  55695. return ret;
  55696. return 0;
  55697. }
  55698. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  55699. {
  55700. int ret;
  55701. int fd;
  55702. byte bad_msg[] = {
  55703. 0x17, /* app data */
  55704. 0xaa, 0xfd, /* bad version */
  55705. 0x00, 0x01, /* epoch 1 */
  55706. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  55707. 0x00, 0x26, /* length: 38 bytes */
  55708. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  55709. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  55710. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  55711. 0x03, 0x18, 0x72
  55712. };
  55713. fd = wolfSSL_get_fd(ssl);
  55714. AssertIntGE(fd, 0);
  55715. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  55716. AssertIntEQ(ret, sizeof(bad_msg));
  55717. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  55718. AssertIntEQ(ret, sizeof("badrecordtest"));
  55719. }
  55720. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  55721. {
  55722. byte buf[100];
  55723. int ret;
  55724. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  55725. AssertIntGT(ret, 0);
  55726. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  55727. }
  55728. static int _test_wolfSSL_dtls_bad_record(
  55729. method_provider client_method, method_provider server_method)
  55730. {
  55731. callback_functions client_cbs, server_cbs;
  55732. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  55733. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  55734. client_cbs.doUdp = server_cbs.doUdp = 1;
  55735. client_cbs.method = client_method;
  55736. server_cbs.method = server_method;
  55737. client_cbs.on_result = test_wolfSSL_send_bad_record;
  55738. server_cbs.on_result = test_wolfSSL_read_string;
  55739. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  55740. if (!client_cbs.return_code)
  55741. return TEST_FAIL;
  55742. if (!server_cbs.return_code)
  55743. return TEST_FAIL;
  55744. return TEST_SUCCESS;
  55745. }
  55746. static int test_wolfSSL_dtls_bad_record(void)
  55747. {
  55748. int ret = TEST_SUCCESS;
  55749. #if !defined(WOLFSSL_NO_TLS12)
  55750. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  55751. wolfDTLSv1_2_server_method);
  55752. #endif
  55753. #ifdef WOLFSSL_DTLS13
  55754. if (ret == TEST_SUCCESS) {
  55755. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  55756. wolfDTLSv1_3_server_method);
  55757. }
  55758. #endif /* WOLFSSL_DTLS13 */
  55759. return ret;
  55760. }
  55761. #else
  55762. static int test_wolfSSL_dtls_fragments(void) {
  55763. return TEST_SKIPPED;
  55764. }
  55765. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  55766. return TEST_SKIPPED;
  55767. }
  55768. static int test_wolfSSL_dtls_bad_record(void) {
  55769. return TEST_SKIPPED;
  55770. }
  55771. #endif
  55772. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
  55773. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  55774. defined(HAVE_IO_TESTS_DEPENDENCIES)
  55775. static byte test_AEAD_fail_decryption = 0;
  55776. static byte test_AEAD_seq_num = 0;
  55777. static byte test_AEAD_done = 0;
  55778. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  55779. {
  55780. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  55781. if (ret > 0) {
  55782. if (test_AEAD_fail_decryption) {
  55783. /* Modify the packet to trigger a decryption failure */
  55784. buf[ret/2] ^= 0xFF;
  55785. if (test_AEAD_fail_decryption == 1)
  55786. test_AEAD_fail_decryption = 0;
  55787. }
  55788. }
  55789. (void)ctx;
  55790. return ret;
  55791. }
  55792. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  55793. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  55794. {
  55795. if (sendLimit)
  55796. w64Zero(sendLimit);
  55797. switch (ssl->specs.bulk_cipher_algorithm) {
  55798. case wolfssl_aes_gcm:
  55799. if (sendLimit)
  55800. *sendLimit = AEAD_AES_LIMIT;
  55801. FALL_THROUGH;
  55802. case wolfssl_chacha:
  55803. if (hardLimit)
  55804. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  55805. if (keyUpdateLimit)
  55806. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  55807. break;
  55808. case wolfssl_aes_ccm:
  55809. if (sendLimit)
  55810. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  55811. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  55812. if (hardLimit)
  55813. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  55814. if (keyUpdateLimit)
  55815. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  55816. }
  55817. else {
  55818. if (hardLimit)
  55819. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  55820. if (keyUpdateLimit)
  55821. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  55822. }
  55823. break;
  55824. default:
  55825. fprintf(stderr, "Unrecognized bulk cipher");
  55826. AssertFalse(1);
  55827. break;
  55828. }
  55829. }
  55830. static void test_AEAD_limit_client(WOLFSSL* ssl)
  55831. {
  55832. int ret;
  55833. int i;
  55834. int didReKey = 0;
  55835. char msgBuf[20];
  55836. w64wrapper hardLimit;
  55837. w64wrapper keyUpdateLimit;
  55838. w64wrapper counter;
  55839. w64wrapper sendLimit;
  55840. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  55841. w64Zero(&counter);
  55842. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  55843. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  55844. for (i = 0; i < 10; i++) {
  55845. /* Test some failed decryptions */
  55846. test_AEAD_fail_decryption = 1;
  55847. w64Increment(&counter);
  55848. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55849. /* Should succeed since decryption failures are dropped */
  55850. AssertIntGT(ret, 0);
  55851. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  55852. }
  55853. test_AEAD_fail_decryption = 1;
  55854. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  55855. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  55856. /* 100 read calls should be enough to complete the key update */
  55857. w64Zero(&counter);
  55858. for (i = 0; i < 100; i++) {
  55859. /* Key update should be sent and negotiated */
  55860. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55861. AssertIntGT(ret, 0);
  55862. /* Epoch after one key update is 4 */
  55863. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  55864. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  55865. didReKey = 1;
  55866. break;
  55867. }
  55868. }
  55869. AssertTrue(didReKey);
  55870. if (!w64IsZero(sendLimit)) {
  55871. /* Test the sending limit for AEAD ciphers */
  55872. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  55873. test_AEAD_seq_num = 1;
  55874. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  55875. AssertIntGT(ret, 0);
  55876. didReKey = 0;
  55877. w64Zero(&counter);
  55878. /* 100 read calls should be enough to complete the key update */
  55879. for (i = 0; i < 100; i++) {
  55880. /* Key update should be sent and negotiated */
  55881. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55882. AssertIntGT(ret, 0);
  55883. /* Epoch after another key update is 5 */
  55884. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  55885. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  55886. didReKey = 1;
  55887. break;
  55888. }
  55889. }
  55890. AssertTrue(didReKey);
  55891. }
  55892. test_AEAD_fail_decryption = 2;
  55893. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  55894. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  55895. /* Connection should fail with a DECRYPT_ERROR */
  55896. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55897. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  55898. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  55899. test_AEAD_done = 1;
  55900. }
  55901. int counter = 0;
  55902. static void test_AEAD_limit_server(WOLFSSL* ssl)
  55903. {
  55904. char msgBuf[] = "Sending data";
  55905. int ret = WOLFSSL_SUCCESS;
  55906. w64wrapper sendLimit;
  55907. SOCKET_T fd = wolfSSL_get_fd(ssl);
  55908. struct timespec delay;
  55909. XMEMSET(&delay, 0, sizeof(delay));
  55910. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  55911. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  55912. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  55913. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  55914. while (!test_AEAD_done && ret > 0) {
  55915. counter++;
  55916. if (test_AEAD_seq_num) {
  55917. /* We need to update the seq number so that we can understand the
  55918. * peer. Otherwise we will incorrectly interpret the seq number. */
  55919. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  55920. AssertNotNull(e);
  55921. e->nextPeerSeqNumber = sendLimit;
  55922. test_AEAD_seq_num = 0;
  55923. }
  55924. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55925. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  55926. nanosleep(&delay, NULL);
  55927. }
  55928. }
  55929. static int test_wolfSSL_dtls_AEAD_limit(void)
  55930. {
  55931. callback_functions func_cb_client;
  55932. callback_functions func_cb_server;
  55933. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  55934. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  55935. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  55936. func_cb_server.method = wolfDTLSv1_3_server_method;
  55937. func_cb_client.method = wolfDTLSv1_3_client_method;
  55938. func_cb_server.on_result = test_AEAD_limit_server;
  55939. func_cb_client.on_result = test_AEAD_limit_client;
  55940. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  55941. if (!func_cb_client.return_code)
  55942. return TEST_FAIL;
  55943. if (!func_cb_server.return_code)
  55944. return TEST_FAIL;
  55945. return TEST_SUCCESS;
  55946. }
  55947. #else
  55948. static int test_wolfSSL_dtls_AEAD_limit(void)
  55949. {
  55950. return TEST_SKIPPED;
  55951. }
  55952. #endif
  55953. #if defined(WOLFSSL_DTLS) && \
  55954. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  55955. !defined(DEBUG_VECTOR_REGISTER_ACCESS_FUZZING)
  55956. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  55957. {
  55958. int fd, ret;
  55959. byte ch_msg[] = {
  55960. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  55961. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  55962. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  55963. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  55964. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  55965. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  55966. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  55967. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  55968. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  55969. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  55970. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  55971. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  55972. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  55973. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  55974. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  55975. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  55976. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  55977. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  55978. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  55979. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  55980. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  55981. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  55982. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  55983. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  55984. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  55985. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  55986. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  55987. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  55988. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  55989. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  55990. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  55991. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  55992. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  55993. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  55994. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  55995. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  55996. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  55997. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  55998. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  55999. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  56000. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  56001. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  56002. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  56003. 0x31, 0x68, 0x4c
  56004. };
  56005. fd = wolfSSL_get_fd(ssl);
  56006. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  56007. AssertIntGT(ret, 0);
  56008. /* consume the HRR otherwise handshake will fail */
  56009. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  56010. AssertIntGT(ret, 0);
  56011. }
  56012. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  56013. static void test_wolfSSL_dtls_send_ch_with_invalid_cookie(WOLFSSL* ssl)
  56014. {
  56015. int fd, ret;
  56016. byte ch_msh_invalid_cookie[] = {
  56017. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
  56018. 0x4e, 0x01, 0x00, 0x02, 0x42, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02,
  56019. 0x42, 0xfe, 0xfd, 0x69, 0xca, 0x77, 0x60, 0x6f, 0xfc, 0xd1, 0x5b, 0x60,
  56020. 0x5d, 0xf1, 0xa6, 0x5c, 0x44, 0x71, 0xae, 0xca, 0x62, 0x19, 0x0c, 0xb6,
  56021. 0xf7, 0x2c, 0xa6, 0xd5, 0xd2, 0x99, 0x9d, 0x18, 0xae, 0xac, 0x11, 0x00,
  56022. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  56023. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  56024. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  56025. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  56026. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  56027. 0xe2, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  56028. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  56029. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  56030. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x2c, 0x00, 0x45,
  56031. 0x00, 0x43, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56032. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56033. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56034. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56035. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56036. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x2d, 0x00,
  56037. 0x03, 0x02, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x0a, 0x00, 0x19,
  56038. 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00, 0x00, 0x16, 0x00, 0x00,
  56039. 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17, 0x00, 0x41, 0x04, 0x7c,
  56040. 0x5a, 0xc2, 0x5a, 0xfd, 0xcd, 0x2b, 0x08, 0xb2, 0xeb, 0x8e, 0xc0, 0x02,
  56041. 0x03, 0x9d, 0xb1, 0xc1, 0x0d, 0x7b, 0x7f, 0x46, 0x43, 0xdf, 0xf3, 0xee,
  56042. 0x2b, 0x78, 0x0e, 0x29, 0x8c, 0x42, 0x11, 0x2c, 0xde, 0xd7, 0x41, 0x0f,
  56043. 0x28, 0x94, 0x80, 0x41, 0x70, 0xc4, 0x17, 0xfd, 0x6d, 0xfa, 0xee, 0x9a,
  56044. 0xf2, 0xc4, 0x15, 0x4c, 0x5f, 0x54, 0xb6, 0x78, 0x6e, 0xf9, 0x63, 0x27,
  56045. 0x33, 0xb8, 0x7b, 0x01, 0x00, 0x01, 0x00, 0xd4, 0x46, 0x62, 0x9c, 0xbf,
  56046. 0x8f, 0x1b, 0x65, 0x9b, 0xf0, 0x29, 0x64, 0xd8, 0x50, 0x0e, 0x74, 0xf1,
  56047. 0x58, 0x10, 0xc9, 0xd9, 0x82, 0x5b, 0xd9, 0xbe, 0x14, 0xdf, 0xde, 0x86,
  56048. 0xb4, 0x2e, 0x15, 0xee, 0x4f, 0xf6, 0x74, 0x9e, 0x59, 0x11, 0x36, 0x2d,
  56049. 0xb9, 0x67, 0xaa, 0x5a, 0x09, 0x9b, 0x45, 0xf1, 0x01, 0x4c, 0x4e, 0xf6,
  56050. 0xda, 0x6a, 0xae, 0xa7, 0x73, 0x7b, 0x2e, 0xb6, 0x24, 0x89, 0x99, 0xb7,
  56051. 0x52, 0x16, 0x62, 0x0a, 0xab, 0x58, 0xf8, 0x3f, 0x10, 0x5b, 0x83, 0xfd,
  56052. 0x7b, 0x81, 0x77, 0x81, 0x8d, 0xef, 0x24, 0x56, 0x6d, 0xba, 0x49, 0xd4,
  56053. 0x8b, 0xb5, 0xa0, 0xb1, 0xc9, 0x8c, 0x32, 0x95, 0x1c, 0x5e, 0x0a, 0x4b,
  56054. 0xf6, 0x00, 0x50, 0x0a, 0x87, 0x99, 0x59, 0xcf, 0x6f, 0x9d, 0x02, 0xd0,
  56055. 0x1b, 0xa1, 0x96, 0x45, 0x28, 0x76, 0x40, 0x33, 0x28, 0xc9, 0xa1, 0xfd,
  56056. 0x46, 0xab, 0x2c, 0x9e, 0x5e, 0xc6, 0x74, 0x19, 0x9a, 0xf5, 0x9b, 0x51,
  56057. 0x11, 0x4f, 0xc8, 0xb9, 0x99, 0x6b, 0x4e, 0x3e, 0x31, 0x64, 0xb4, 0x92,
  56058. 0xf4, 0x0d, 0x41, 0x4b, 0x2c, 0x65, 0x23, 0xf7, 0x47, 0xe3, 0xa5, 0x2e,
  56059. 0xe4, 0x9c, 0x2b, 0xc9, 0x41, 0x22, 0x83, 0x8a, 0x23, 0xef, 0x29, 0x7e,
  56060. 0x4f, 0x3f, 0xa3, 0xbf, 0x73, 0x2b, 0xd7, 0xcc, 0xc8, 0xc6, 0xe9, 0xbc,
  56061. 0x01, 0xb7, 0x32, 0x63, 0xd4, 0x7e, 0x7f, 0x9a, 0xaf, 0x5f, 0x05, 0x31,
  56062. 0x53, 0xd6, 0x1f, 0xa2, 0xd0, 0xdf, 0x67, 0x56, 0xf1, 0x9c, 0x4a, 0x9d,
  56063. 0x83, 0xb4, 0xef, 0xb3, 0xf2, 0xcc, 0xf1, 0x91, 0x6c, 0x47, 0xc3, 0x8b,
  56064. 0xd0, 0x92, 0x79, 0x3d, 0xa0, 0xc0, 0x3a, 0x57, 0x26, 0x6d, 0x0a, 0xad,
  56065. 0x5f, 0xad, 0xb4, 0x74, 0x48, 0x4a, 0x51, 0xe1, 0xb5, 0x82, 0x0a, 0x4c,
  56066. 0x4f, 0x9d, 0xaf, 0xee, 0x5a, 0xa2, 0x4d, 0x4d, 0x5f, 0xe0, 0x17, 0x00,
  56067. 0x23, 0x00, 0x00
  56068. };
  56069. byte alert_reply[50];
  56070. byte expected_alert_reply[] = {
  56071. 0x15, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  56072. 0x02, 0x02, 0x2f
  56073. };
  56074. fd = wolfSSL_get_fd(ssl);
  56075. ret = (int)send(fd, ch_msh_invalid_cookie, sizeof(ch_msh_invalid_cookie), 0);
  56076. AssertIntGT(ret, 0);
  56077. /* should reply with an illegal_parameter reply */
  56078. ret = (int)recv(fd, alert_reply, sizeof(alert_reply), 0);
  56079. AssertIntEQ(ret, sizeof(expected_alert_reply));
  56080. AssertIntEQ(XMEMCMP(alert_reply, expected_alert_reply, sizeof(expected_alert_reply)), 0);
  56081. }
  56082. #endif
  56083. static word32 test_wolfSSL_dtls_stateless_HashWOLFSSL(const WOLFSSL* ssl)
  56084. {
  56085. #ifndef NO_MD5
  56086. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  56087. #elif !defined(NO_SHA)
  56088. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  56089. #elif !defined(NO_SHA256)
  56090. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  56091. #else
  56092. #error "We need a digest to hash the WOLFSSL object"
  56093. #endif
  56094. byte hashBuf[WC_MAX_DIGEST_SIZE];
  56095. wc_HashAlg hash;
  56096. const TLSX* exts = ssl->extensions;
  56097. WOLFSSL sslCopy; /* Use a copy to omit certain fields */
  56098. HS_Hashes* hsHashes = ssl->hsHashes; /* Is re-allocated in
  56099. * InitHandshakeHashes */
  56100. XMEMCPY(&sslCopy, ssl, sizeof(*ssl));
  56101. XMEMSET(hashBuf, 0, sizeof(hashBuf));
  56102. /* Following fields are not important to compare */
  56103. XMEMSET(sslCopy.buffers.inputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  56104. sslCopy.buffers.inputBuffer.buffer = NULL;
  56105. sslCopy.buffers.inputBuffer.bufferSize = 0;
  56106. sslCopy.buffers.inputBuffer.dynamicFlag = 0;
  56107. sslCopy.buffers.inputBuffer.offset = 0;
  56108. XMEMSET(sslCopy.buffers.outputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  56109. sslCopy.buffers.outputBuffer.buffer = NULL;
  56110. sslCopy.buffers.outputBuffer.bufferSize = 0;
  56111. sslCopy.buffers.outputBuffer.dynamicFlag = 0;
  56112. sslCopy.buffers.outputBuffer.offset = 0;
  56113. sslCopy.error = 0;
  56114. sslCopy.curSize = 0;
  56115. sslCopy.curStartIdx = 0;
  56116. sslCopy.keys.curSeq_lo = 0;
  56117. XMEMSET(&sslCopy.curRL, 0, sizeof(sslCopy.curRL));
  56118. #ifdef WOLFSSL_DTLS13
  56119. XMEMSET(&sslCopy.keys.curSeq, 0, sizeof(sslCopy.keys.curSeq));
  56120. sslCopy.dtls13FastTimeout = 0;
  56121. #endif
  56122. sslCopy.keys.dtls_peer_handshake_number = 0;
  56123. XMEMSET(&sslCopy.alert_history, 0, sizeof(sslCopy.alert_history));
  56124. sslCopy.hsHashes = NULL;
  56125. #ifdef WOLFSSL_ASYNC_IO
  56126. #ifdef WOLFSSL_ASYNC_CRYPT
  56127. sslCopy.asyncDev = NULL;
  56128. #endif
  56129. sslCopy.async = NULL;
  56130. #endif
  56131. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  56132. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)&sslCopy, sizeof(sslCopy)), 0);
  56133. /* hash extension list */
  56134. while (exts != NULL) {
  56135. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)exts, sizeof(*exts)), 0);
  56136. exts = exts->next;
  56137. }
  56138. /* Hash suites */
  56139. if (sslCopy.suites != NULL) {
  56140. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)sslCopy.suites,
  56141. sizeof(struct Suites)), 0);
  56142. }
  56143. /* Hash hsHashes */
  56144. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)hsHashes,
  56145. sizeof(*hsHashes)), 0);
  56146. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  56147. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  56148. return MakeWordFromHash(hashBuf);
  56149. }
  56150. static CallbackIORecv test_wolfSSL_dtls_compare_stateless_cb;
  56151. static int test_wolfSSL_dtls_compare_stateless_cb_call_once;
  56152. static int test_wolfSSL_dtls_compare_stateless_read_cb_once(WOLFSSL *ssl,
  56153. char *buf, int sz, void *ctx)
  56154. {
  56155. if (test_wolfSSL_dtls_compare_stateless_cb_call_once) {
  56156. test_wolfSSL_dtls_compare_stateless_cb_call_once = 0;
  56157. return test_wolfSSL_dtls_compare_stateless_cb(ssl, buf, sz, ctx);
  56158. }
  56159. else {
  56160. return WOLFSSL_CBIO_ERR_WANT_READ;
  56161. }
  56162. }
  56163. static void test_wolfSSL_dtls_compare_stateless(WOLFSSL* ssl)
  56164. {
  56165. /* Compare the ssl object before and after one ClientHello msg */
  56166. SOCKET_T fd = wolfSSL_get_fd(ssl);
  56167. int res;
  56168. int err;
  56169. word32 initHash;
  56170. test_wolfSSL_dtls_compare_stateless_cb = ssl->CBIORecv;
  56171. test_wolfSSL_dtls_compare_stateless_cb_call_once = 1;
  56172. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  56173. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_read_cb_once;
  56174. initHash = test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl);
  56175. (void)initHash;
  56176. res = tcp_select(fd, 5);
  56177. /* We are expecting a msg. A timeout indicates failure. */
  56178. AssertIntEQ(res, TEST_RECV_READY);
  56179. res = wolfSSL_accept(ssl);
  56180. err = wolfSSL_get_error(ssl, res);
  56181. AssertIntEQ(res, WOLFSSL_FATAL_ERROR);
  56182. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  56183. AssertIntEQ(initHash, test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl));
  56184. wolfSSL_dtls_set_using_nonblock(ssl, 0);
  56185. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_cb;
  56186. }
  56187. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  56188. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  56189. {
  56190. int ret;
  56191. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  56192. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  56193. test_wolfSSL_dtls_compare_stateless(ssl);
  56194. }
  56195. #endif
  56196. static int test_wolfSSL_dtls_stateless(void)
  56197. {
  56198. callback_functions client_cbs, server_cbs;
  56199. size_t i;
  56200. struct {
  56201. method_provider client_meth;
  56202. method_provider server_meth;
  56203. ssl_callback client_ssl_ready;
  56204. ssl_callback server_ssl_ready;
  56205. } test_params[] = {
  56206. #if !defined(WOLFSSL_NO_TLS12)
  56207. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  56208. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_compare_stateless},
  56209. #endif
  56210. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  56211. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  56212. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  56213. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  56214. test_wolfSSL_dtls_send_ch_with_invalid_cookie, test_wolfSSL_dtls_enable_hrrcookie},
  56215. #endif
  56216. };
  56217. if (0 == sizeof(test_params)){
  56218. return TEST_SKIPPED;
  56219. }
  56220. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  56221. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56222. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56223. client_cbs.doUdp = server_cbs.doUdp = 1;
  56224. client_cbs.method = test_params[i].client_meth;
  56225. server_cbs.method = test_params[i].server_meth;
  56226. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  56227. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  56228. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56229. if (!client_cbs.return_code)
  56230. return TEST_FAIL;
  56231. if (!server_cbs.return_code)
  56232. return TEST_FAIL;
  56233. }
  56234. return TEST_SUCCESS;
  56235. }
  56236. #else
  56237. static int test_wolfSSL_dtls_stateless(void)
  56238. {
  56239. return TEST_SKIPPED;
  56240. }
  56241. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  56242. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  56243. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  56244. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  56245. !defined(WOLFSSL_NO_CLIENT_AUTH))
  56246. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  56247. {
  56248. int ret;
  56249. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  56250. fprintf(stderr, "loading cert %s failed\n", certA);
  56251. fprintf(stderr, "Error: (%d): %s\n", ret,
  56252. wolfSSL_ERR_reason_error_string(ret));
  56253. return -1;
  56254. }
  56255. return 0;
  56256. }
  56257. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  56258. {
  56259. int ret;
  56260. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  56261. != WOLFSSL_SUCCESS) {
  56262. fprintf(stderr, "could not verify the cert: %s\n", certA);
  56263. fprintf(stderr, "Error: (%d): %s\n", ret,
  56264. wolfSSL_ERR_reason_error_string(ret));
  56265. return -1;
  56266. }
  56267. else {
  56268. fprintf(stderr, "successfully verified: %s\n", certA);
  56269. }
  56270. return 0;
  56271. }
  56272. #define LOAD_ONE_CA(a, b, c, d) \
  56273. do { \
  56274. (a) = load_ca_into_cm(c, d); \
  56275. if ((a) != 0) \
  56276. return (b); \
  56277. else \
  56278. (b)--; \
  56279. } while(0)
  56280. #define VERIFY_ONE_CERT(a, b, c, d) \
  56281. do { \
  56282. (a) = verify_cert_with_cm(c, d);\
  56283. if ((a) != 0) \
  56284. return (b); \
  56285. else \
  56286. (b)--; \
  56287. } while(0)
  56288. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  56289. {
  56290. int ret;
  56291. int i = -1;
  56292. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  56293. char chainGArr[9][50] = {"certs/ca-cert.pem",
  56294. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  56295. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  56296. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  56297. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  56298. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  56299. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  56300. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  56301. "certs/test-pathlen/chainG-entity.pem"};
  56302. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  56303. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  56304. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  56305. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  56306. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  56307. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  56308. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  56309. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  56310. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  56311. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  56312. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  56313. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  56314. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  56315. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  56316. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  56317. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  56318. /* test validating the entity twice, should have no effect on pathLen since
  56319. * entity/leaf cert */
  56320. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  56321. return ret;
  56322. }
  56323. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  56324. {
  56325. int ret;
  56326. int i = -1;
  56327. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  56328. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  56329. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  56330. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  56331. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  56332. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  56333. */
  56334. char chainHArr[6][50] = {"certs/ca-cert.pem",
  56335. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  56336. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  56337. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  56338. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  56339. "certs/test-pathlen/chainH-entity.pem"};
  56340. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  56341. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  56342. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  56343. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  56344. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  56345. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  56346. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  56347. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  56348. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  56349. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  56350. return ret;
  56351. }
  56352. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  56353. {
  56354. int ret;
  56355. int i = -1;
  56356. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  56357. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  56358. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  56359. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  56360. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  56361. */
  56362. char chainIArr[5][50] = {"certs/ca-cert.pem",
  56363. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  56364. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  56365. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  56366. "certs/test-pathlen/chainI-entity.pem"};
  56367. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  56368. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  56369. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  56370. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  56371. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  56372. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  56373. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  56374. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  56375. return ret;
  56376. }
  56377. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  56378. {
  56379. int ret;
  56380. int i = -1;
  56381. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  56382. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  56383. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  56384. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  56385. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  56386. */
  56387. char chainJArr[6][50] = {"certs/ca-cert.pem",
  56388. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  56389. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  56390. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  56391. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  56392. "certs/test-pathlen/chainJ-entity.pem"};
  56393. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  56394. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  56395. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  56396. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  56397. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  56398. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  56399. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  56400. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  56401. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  56402. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  56403. return ret;
  56404. }
  56405. static int test_various_pathlen_chains(void)
  56406. {
  56407. EXPECT_DECLS;
  56408. WOLFSSL_CERT_MANAGER* cm = NULL;
  56409. /* Test chain G (large chain with varying pathLens) */
  56410. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56411. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  56412. ExpectIntEQ(test_chainG(cm), -1);
  56413. #else
  56414. ExpectIntEQ(test_chainG(cm), 0);
  56415. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  56416. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56417. wolfSSL_CertManagerFree(cm);
  56418. /* end test chain G */
  56419. /* Test chain H (5 chain with same pathLens) */
  56420. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56421. ExpectIntLT(test_chainH(cm), 0);
  56422. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56423. wolfSSL_CertManagerFree(cm);
  56424. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56425. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56426. wolfSSL_CertManagerFree(cm);
  56427. /* end test chain H */
  56428. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  56429. * followed by 2 ICA's, should pass) */
  56430. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56431. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  56432. ExpectIntEQ(test_chainI(cm), -1);
  56433. #else
  56434. ExpectIntEQ(test_chainI(cm), 0);
  56435. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  56436. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56437. wolfSSL_CertManagerFree(cm);
  56438. cm = NULL;
  56439. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56440. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56441. wolfSSL_CertManagerFree(cm);
  56442. cm = NULL;
  56443. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  56444. * this time followed by 3 ICA's, should fail */
  56445. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56446. ExpectIntLT(test_chainJ(cm), 0);
  56447. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56448. wolfSSL_CertManagerFree(cm);
  56449. cm = NULL;
  56450. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56451. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56452. wolfSSL_CertManagerFree(cm);
  56453. return EXPECT_RESULT();
  56454. }
  56455. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  56456. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  56457. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  56458. {
  56459. EXPECT_DECLS;
  56460. byte ekm[100] = {0};
  56461. (void)ctx;
  56462. /* Success Cases */
  56463. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56464. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  56465. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56466. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  56467. /* Use some random context */
  56468. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56469. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  56470. /* Failure cases */
  56471. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56472. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  56473. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56474. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  56475. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56476. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  56477. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56478. "extended master secret", XSTR_SIZEOF("extended master secret"),
  56479. NULL, 0, 0), 0);
  56480. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56481. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  56482. return EXPECT_RESULT();
  56483. }
  56484. static int test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  56485. {
  56486. wolfSSL_KeepArrays(ssl);
  56487. return TEST_SUCCESS;
  56488. }
  56489. static int test_export_keying_material(void)
  56490. {
  56491. EXPECT_DECLS;
  56492. test_ssl_cbf serverCb;
  56493. test_ssl_cbf clientCb;
  56494. XMEMSET(&serverCb, 0, sizeof(serverCb));
  56495. XMEMSET(&clientCb, 0, sizeof(clientCb));
  56496. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  56497. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&clientCb,
  56498. &serverCb, test_export_keying_material_cb), TEST_SUCCESS);
  56499. return EXPECT_RESULT();
  56500. }
  56501. #endif /* HAVE_KEYING_MATERIAL */
  56502. static int test_wolfSSL_THREADID_hash(void)
  56503. {
  56504. EXPECT_DECLS;
  56505. #if defined(OPENSSL_EXTRA)
  56506. CRYPTO_THREADID id;
  56507. CRYPTO_THREADID_current(NULL);
  56508. /* Hash result is unsigned long. */
  56509. ExpectTrue(CRYPTO_THREADID_hash(NULL) == 0UL);
  56510. XMEMSET(&id, 0, sizeof(id));
  56511. ExpectTrue(CRYPTO_THREADID_hash(&id) == 0UL);
  56512. #endif /* OPENSSL_EXTRA */
  56513. return EXPECT_RESULT();
  56514. }
  56515. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  56516. {
  56517. EXPECT_DECLS;
  56518. #if defined(OPENSSL_EXTRA)
  56519. WOLFSSL_CTX* ctx = NULL;
  56520. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,0), 1);
  56521. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,1), 1);
  56522. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,0), 1);
  56523. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,1), 1);
  56524. #endif /* OPENSSL_EXTRA */
  56525. return EXPECT_RESULT();
  56526. }
  56527. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  56528. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  56529. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  56530. {
  56531. EXPECT_DECLS;
  56532. callback_functions* callbacks = NULL;
  56533. WOLFSSL_CTX* ctx = NULL;
  56534. WOLFSSL* ssl = NULL;
  56535. SOCKET_T sfd = 0;
  56536. SOCKET_T cfd = 0;
  56537. word16 port;
  56538. char msg[] = "I hear you fa shizzle!";
  56539. int len = (int) XSTRLEN(msg);
  56540. char input[1024];
  56541. int ret = 0;
  56542. int err = 0;
  56543. if (!args)
  56544. WOLFSSL_RETURN_FROM_THREAD(0);
  56545. ((func_args*)args)->return_code = TEST_FAIL;
  56546. callbacks = ((func_args*)args)->callbacks;
  56547. ctx = wolfSSL_CTX_new(callbacks->method());
  56548. #if defined(USE_WINDOWS_API)
  56549. port = ((func_args*)args)->signal->port;
  56550. #else
  56551. /* Let tcp_listen assign port */
  56552. port = 0;
  56553. #endif
  56554. #ifdef WOLFSSL_TIRTOS
  56555. fdOpenSession(Task_self());
  56556. #endif
  56557. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  56558. caCertFile, 0));
  56559. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  56560. svrCertFile, WOLFSSL_FILETYPE_PEM));
  56561. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  56562. svrKeyFile, WOLFSSL_FILETYPE_PEM));
  56563. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  56564. ExpectIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  56565. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  56566. #elif !defined(NO_DH)
  56567. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  56568. #endif
  56569. if (callbacks->ctx_ready)
  56570. callbacks->ctx_ready(ctx);
  56571. ssl = wolfSSL_new(ctx);
  56572. ExpectNotNull(ssl);
  56573. /* listen and accept */
  56574. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  56575. CloseSocket(sfd);
  56576. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  56577. if (callbacks->ssl_ready)
  56578. callbacks->ssl_ready(ssl);
  56579. if (EXPECT_SUCCESS()) {
  56580. do {
  56581. err = 0; /* Reset error */
  56582. ret = wolfSSL_accept(ssl);
  56583. if (ret != WOLFSSL_SUCCESS) {
  56584. err = wolfSSL_get_error(ssl, 0);
  56585. }
  56586. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  56587. }
  56588. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  56589. /* read and write data */
  56590. XMEMSET(input, 0, sizeof(input));
  56591. while (EXPECT_SUCCESS()) {
  56592. ret = wolfSSL_read(ssl, input, sizeof(input));
  56593. if (ret > 0) {
  56594. break;
  56595. }
  56596. else {
  56597. err = wolfSSL_get_error(ssl,ret);
  56598. if (err == WOLFSSL_ERROR_WANT_READ) {
  56599. continue;
  56600. }
  56601. break;
  56602. }
  56603. }
  56604. if (EXPECT_SUCCESS() && (err == WOLFSSL_ERROR_ZERO_RETURN)) {
  56605. do {
  56606. ret = wolfSSL_write(ssl, msg, len);
  56607. if (ret > 0) {
  56608. break;
  56609. }
  56610. } while (ret < 0);
  56611. }
  56612. /* bidirectional shutdown */
  56613. while (EXPECT_SUCCESS()) {
  56614. ret = wolfSSL_shutdown(ssl);
  56615. ExpectIntNE(ret, WOLFSSL_FATAL_ERROR);
  56616. if (ret == WOLFSSL_SUCCESS) {
  56617. break;
  56618. }
  56619. }
  56620. if (EXPECT_SUCCESS()) {
  56621. /* wait for the peer to disconnect the tcp connection */
  56622. do {
  56623. ret = wolfSSL_read(ssl, input, sizeof(input));
  56624. err = wolfSSL_get_error(ssl, ret);
  56625. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  56626. }
  56627. /* detect TCP disconnect */
  56628. ExpectIntLE(ret,WOLFSSL_FAILURE);
  56629. ExpectIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  56630. ((func_args*)args)->return_code = EXPECT_RESULT();
  56631. wolfSSL_free(ssl);
  56632. wolfSSL_CTX_free(ctx);
  56633. CloseSocket(cfd);
  56634. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  56635. wc_ecc_fp_free(); /* free per thread cache */
  56636. #endif
  56637. WOLFSSL_RETURN_FROM_THREAD(0);
  56638. }
  56639. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  56640. {
  56641. EXPECT_DECLS;
  56642. callback_functions* callbacks = NULL;
  56643. WOLFSSL_CTX* ctx = NULL;
  56644. WOLFSSL* ssl = NULL;
  56645. SOCKET_T sfd = 0;
  56646. char msg[] = "hello wolfssl server!";
  56647. int len = (int) XSTRLEN(msg);
  56648. char input[1024];
  56649. int idx;
  56650. int ret, err;
  56651. if (!args)
  56652. WOLFSSL_RETURN_FROM_THREAD(0);
  56653. ((func_args*)args)->return_code = TEST_FAIL;
  56654. callbacks = ((func_args*)args)->callbacks;
  56655. ctx = wolfSSL_CTX_new(callbacks->method());
  56656. #ifdef WOLFSSL_TIRTOS
  56657. fdOpenSession(Task_self());
  56658. #endif
  56659. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  56660. caCertFile, 0));
  56661. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  56662. cliCertFile, WOLFSSL_FILETYPE_PEM));
  56663. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  56664. cliKeyFile, WOLFSSL_FILETYPE_PEM));
  56665. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  56666. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  56667. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  56668. if (EXPECT_SUCCESS()) {
  56669. do {
  56670. err = 0; /* Reset error */
  56671. ret = wolfSSL_connect(ssl);
  56672. if (ret != WOLFSSL_SUCCESS) {
  56673. err = wolfSSL_get_error(ssl, 0);
  56674. }
  56675. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  56676. }
  56677. ExpectIntGE(wolfSSL_write(ssl, msg, len), 0);
  56678. if (EXPECT_SUCCESS()) {
  56679. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  56680. input[idx] = 0;
  56681. }
  56682. }
  56683. if (EXPECT_SUCCESS()) {
  56684. ret = wolfSSL_shutdown(ssl);
  56685. if (ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  56686. ret = wolfSSL_shutdown(ssl);
  56687. }
  56688. }
  56689. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  56690. ((func_args*)args)->return_code = EXPECT_RESULT();
  56691. wolfSSL_free(ssl);
  56692. wolfSSL_CTX_free(ctx);
  56693. CloseSocket(sfd);
  56694. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  56695. wc_ecc_fp_free(); /* free per thread cache */
  56696. #endif
  56697. WOLFSSL_RETURN_FROM_THREAD(0);
  56698. }
  56699. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  56700. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  56701. /* This test is to check wolfSSL_read behaves as same as
  56702. * openSSL when it is called after SSL_shutdown completes.
  56703. */
  56704. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  56705. {
  56706. EXPECT_DECLS;
  56707. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  56708. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  56709. tcp_ready ready;
  56710. func_args client_args;
  56711. func_args server_args;
  56712. THREAD_TYPE serverThread;
  56713. THREAD_TYPE clientThread;
  56714. callback_functions server_cbf;
  56715. callback_functions client_cbf;
  56716. #ifdef WOLFSSL_TIRTOS
  56717. fdOpenSession(Task_self());
  56718. #endif
  56719. StartTCP();
  56720. InitTcpReady(&ready);
  56721. #if defined(USE_WINDOWS_API)
  56722. /* use RNG to get random port if using windows */
  56723. ready.port = GetRandomPort();
  56724. #endif
  56725. XMEMSET(&client_args, 0, sizeof(func_args));
  56726. XMEMSET(&server_args, 0, sizeof(func_args));
  56727. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  56728. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  56729. server_cbf.method = wolfTLSv1_2_server_method;
  56730. client_cbf.method = wolfTLSv1_2_client_method;
  56731. server_args.callbacks = &server_cbf;
  56732. client_args.callbacks = &client_cbf;
  56733. server_args.signal = &ready;
  56734. client_args.signal = &ready;
  56735. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  56736. wait_tcp_ready(&server_args);
  56737. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  56738. join_thread(clientThread);
  56739. join_thread(serverThread);
  56740. ExpectTrue(client_args.return_code);
  56741. ExpectTrue(server_args.return_code);
  56742. FreeTcpReady(&ready);
  56743. #endif
  56744. return EXPECT_RESULT();
  56745. }
  56746. static int test_wolfSSL_CTX_get_min_proto_version(void)
  56747. {
  56748. EXPECT_DECLS;
  56749. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  56750. WOLFSSL_CTX *ctx = NULL;
  56751. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  56752. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  56753. WOLFSSL_SUCCESS);
  56754. #ifdef WOLFSSL_ALLOW_SSLV3
  56755. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  56756. #else
  56757. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  56758. #endif
  56759. wolfSSL_CTX_free(ctx);
  56760. ctx = NULL;
  56761. #ifndef NO_OLD_TLS
  56762. #ifdef WOLFSSL_ALLOW_TLSV10
  56763. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  56764. #else
  56765. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  56766. #endif
  56767. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION),
  56768. WOLFSSL_SUCCESS);
  56769. #ifdef WOLFSSL_ALLOW_TLSV10
  56770. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  56771. #else
  56772. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  56773. #endif
  56774. wolfSSL_CTX_free(ctx);
  56775. ctx = NULL;
  56776. #endif
  56777. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  56778. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION),
  56779. WOLFSSL_SUCCESS);
  56780. #ifndef NO_OLD_TLS
  56781. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  56782. #else
  56783. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  56784. #endif
  56785. wolfSSL_CTX_free(ctx);
  56786. ctx = NULL;
  56787. #ifndef WOLFSSL_NO_TLS12
  56788. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  56789. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION),
  56790. WOLFSSL_SUCCESS);
  56791. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  56792. wolfSSL_CTX_free(ctx);
  56793. ctx = NULL;
  56794. #endif
  56795. #ifdef WOLFSSL_TLS13
  56796. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  56797. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION),
  56798. WOLFSSL_SUCCESS);
  56799. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  56800. wolfSSL_CTX_free(ctx);
  56801. ctx = NULL;
  56802. #endif
  56803. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  56804. return EXPECT_RESULT();
  56805. }
  56806. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  56807. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  56808. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  56809. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  56810. static int test_wolfSSL_set_SSL_CTX(void)
  56811. {
  56812. EXPECT_DECLS;
  56813. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
  56814. && !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13) && \
  56815. !defined(NO_RSA)
  56816. WOLFSSL_CTX *ctx1 = NULL;
  56817. WOLFSSL_CTX *ctx2 = NULL;
  56818. WOLFSSL *ssl = NULL;
  56819. const byte *session_id1 = (const byte *)"CTX1";
  56820. const byte *session_id2 = (const byte *)"CTX2";
  56821. ExpectNotNull(ctx1 = wolfSSL_CTX_new(wolfTLS_server_method()));
  56822. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx1, svrCertFile,
  56823. WOLFSSL_FILETYPE_PEM));
  56824. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx1, svrKeyFile,
  56825. WOLFSSL_FILETYPE_PEM));
  56826. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx1, TLS1_2_VERSION),
  56827. WOLFSSL_SUCCESS);
  56828. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx1), TLS1_2_VERSION);
  56829. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx1), TLS1_3_VERSION);
  56830. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx1, session_id1, 4),
  56831. WOLFSSL_SUCCESS);
  56832. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfTLS_server_method()));
  56833. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx2, svrCertFile,
  56834. WOLFSSL_FILETYPE_PEM));
  56835. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx2, svrKeyFile,
  56836. WOLFSSL_FILETYPE_PEM));
  56837. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx2, TLS1_2_VERSION),
  56838. WOLFSSL_SUCCESS);
  56839. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx2, TLS1_2_VERSION),
  56840. WOLFSSL_SUCCESS);
  56841. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx2), TLS1_2_VERSION);
  56842. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx2), TLS1_2_VERSION);
  56843. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx2, session_id2, 4),
  56844. WOLFSSL_SUCCESS);
  56845. #ifdef HAVE_SESSION_TICKET
  56846. ExpectIntEQ((wolfSSL_CTX_get_options(ctx1) & SSL_OP_NO_TICKET), 0);
  56847. wolfSSL_CTX_set_options(ctx2, SSL_OP_NO_TICKET);
  56848. ExpectIntNE((wolfSSL_CTX_get_options(ctx2) & SSL_OP_NO_TICKET), 0);
  56849. #endif
  56850. ExpectNotNull(ssl = wolfSSL_new(ctx2));
  56851. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  56852. #ifdef WOLFSSL_INT_H
  56853. #ifdef WOLFSSL_SESSION_ID_CTX
  56854. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id2, 4), 0);
  56855. #endif
  56856. ExpectTrue(ssl->buffers.certificate == ctx2->certificate);
  56857. ExpectTrue(ssl->buffers.certChain == ctx2->certChain);
  56858. #endif
  56859. #ifdef HAVE_SESSION_TICKET
  56860. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  56861. #endif
  56862. /* Set the ctx1 that has TLSv1.3 as max proto version */
  56863. ExpectNotNull(wolfSSL_set_SSL_CTX(ssl, ctx1));
  56864. /* MUST not change proto versions of ssl */
  56865. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  56866. #ifdef HAVE_SESSION_TICKET
  56867. /* MUST not change */
  56868. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  56869. #endif
  56870. /* MUST change */
  56871. #ifdef WOLFSSL_INT_H
  56872. ExpectTrue(ssl->buffers.certificate == ctx1->certificate);
  56873. ExpectTrue(ssl->buffers.certChain == ctx1->certChain);
  56874. #ifdef WOLFSSL_SESSION_ID_CTX
  56875. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id1, 4), 0);
  56876. #endif
  56877. #endif
  56878. wolfSSL_free(ssl);
  56879. wolfSSL_CTX_free(ctx1);
  56880. wolfSSL_CTX_free(ctx2);
  56881. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  56882. return EXPECT_RESULT();
  56883. }
  56884. #endif /* defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  56885. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  56886. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  56887. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))) */
  56888. static int test_wolfSSL_security_level(void)
  56889. {
  56890. EXPECT_DECLS;
  56891. #if defined(OPENSSL_EXTRA)
  56892. SSL_CTX *ctx = NULL;
  56893. #ifdef WOLFSSL_TLS13
  56894. #ifdef NO_WOLFSSL_SERVER
  56895. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  56896. #else
  56897. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  56898. #endif
  56899. SSL_CTX_set_security_level(NULL, 1);
  56900. SSL_CTX_set_security_level(ctx, 1);
  56901. ExpectIntEQ(SSL_CTX_get_security_level(NULL), 0);
  56902. /* Stub so nothing happens. */
  56903. ExpectIntEQ(SSL_CTX_get_security_level(ctx), 0);
  56904. SSL_CTX_free(ctx);
  56905. #else
  56906. (void)ctx;
  56907. #endif
  56908. #endif
  56909. return EXPECT_RESULT();
  56910. }
  56911. static int test_wolfSSL_SSL_in_init(void)
  56912. {
  56913. EXPECT_DECLS;
  56914. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  56915. SSL_CTX* ctx = NULL;
  56916. SSL* ssl = NULL;
  56917. const char* testCertFile;
  56918. const char* testKeyFile;
  56919. #ifdef WOLFSSL_TLS13
  56920. #ifdef NO_WOLFSSL_SERVER
  56921. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  56922. #else
  56923. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  56924. #endif
  56925. #else
  56926. #ifdef NO_WOLFSSL_SERVER
  56927. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  56928. #else
  56929. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56930. #endif
  56931. #endif
  56932. #ifndef NO_RSA
  56933. testCertFile = svrCertFile;
  56934. testKeyFile = svrKeyFile;
  56935. #elif defined(HAVE_ECC)
  56936. testCertFile = eccCertFile;
  56937. testKeyFile = eccKeyFile;
  56938. #else
  56939. testCertFile = NULL;
  56940. testKeyFile = NULL;
  56941. #endif
  56942. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  56943. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  56944. SSL_FILETYPE_PEM));
  56945. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  56946. SSL_FILETYPE_PEM));
  56947. }
  56948. ExpectNotNull(ssl = SSL_new(ctx));
  56949. ExpectIntEQ(SSL_in_init(ssl), 1);
  56950. SSL_CTX_free(ctx);
  56951. SSL_free(ssl);
  56952. #endif
  56953. return EXPECT_RESULT();
  56954. }
  56955. static int test_wolfSSL_CTX_set_timeout(void)
  56956. {
  56957. EXPECT_DECLS;
  56958. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  56959. int timeout;
  56960. WOLFSSL_CTX* ctx = NULL;
  56961. (void)timeout;
  56962. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56963. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  56964. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  56965. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  56966. */
  56967. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  56968. /* giving 0 as timeout value sets default timeout */
  56969. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  56970. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  56971. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  56972. #else
  56973. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  56974. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  56975. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  56976. #endif
  56977. wolfSSL_CTX_free(ctx);
  56978. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  56979. return EXPECT_RESULT();
  56980. }
  56981. static int test_wolfSSL_OpenSSL_version(void)
  56982. {
  56983. EXPECT_DECLS;
  56984. #if defined(OPENSSL_EXTRA)
  56985. const char* ver;
  56986. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  56987. ExpectNotNull(ver = OpenSSL_version(0));
  56988. #else
  56989. ExpectNotNull(ver = OpenSSL_version());
  56990. #endif
  56991. ExpectIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  56992. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  56993. #endif
  56994. return EXPECT_RESULT();
  56995. }
  56996. static int test_CONF_CTX_CMDLINE(void)
  56997. {
  56998. EXPECT_DECLS;
  56999. #if defined(OPENSSL_ALL)
  57000. SSL_CTX* ctx = NULL;
  57001. SSL_CONF_CTX* cctx = NULL;
  57002. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  57003. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57004. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  57005. /* set flags */
  57006. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  57007. WOLFSSL_CONF_FLAG_CMDLINE);
  57008. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  57009. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  57010. /* cmd invalid command */
  57011. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  57012. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  57013. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  57014. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  57015. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  57016. /* cmd Certificate and Private Key*/
  57017. {
  57018. #if !defined(NO_CERTS) && !defined(NO_RSA)
  57019. const char* ourCert = svrCertFile;
  57020. const char* ourKey = svrKeyFile;
  57021. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  57022. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert), WOLFSSL_SUCCESS);
  57023. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  57024. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  57025. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57026. #endif
  57027. }
  57028. /* cmd curves */
  57029. {
  57030. #if defined(HAVE_ECC)
  57031. const char* curve = "secp256r1";
  57032. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  57033. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  57034. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57035. #endif
  57036. }
  57037. /* cmd CipherString */
  57038. {
  57039. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  57040. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  57041. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  57042. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57043. }
  57044. /* cmd DH parameter */
  57045. {
  57046. #if !defined(NO_DH) && !defined(NO_BIO)
  57047. const char* ourdhcert = "./certs/dh2048.pem";
  57048. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL), -3);
  57049. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert), WOLFSSL_SUCCESS);
  57050. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57051. #endif
  57052. }
  57053. SSL_CTX_free(ctx);
  57054. SSL_CONF_CTX_free(cctx);
  57055. #endif /* OPENSSL_EXTRA */
  57056. return EXPECT_RESULT();
  57057. }
  57058. static int test_CONF_CTX_FILE(void)
  57059. {
  57060. EXPECT_DECLS;
  57061. #if defined(OPENSSL_ALL)
  57062. SSL_CTX* ctx = NULL;
  57063. SSL_CONF_CTX* cctx = NULL;
  57064. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  57065. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57066. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  57067. /* set flags */
  57068. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  57069. WOLFSSL_CONF_FLAG_FILE);
  57070. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  57071. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  57072. /* sanity check */
  57073. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  57074. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  57075. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  57076. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  57077. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  57078. /* cmd Certificate and Private Key*/
  57079. {
  57080. #if !defined(NO_CERTS) && !defined(NO_RSA)
  57081. const char* ourCert = svrCertFile;
  57082. const char* ourKey = svrKeyFile;
  57083. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  57084. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  57085. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  57086. WOLFSSL_SUCCESS);
  57087. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  57088. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57089. #endif
  57090. }
  57091. /* cmd curves */
  57092. {
  57093. #if defined(HAVE_ECC)
  57094. const char* curve = "secp256r1";
  57095. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  57096. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  57097. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57098. #endif
  57099. }
  57100. /* cmd CipherString */
  57101. {
  57102. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  57103. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  57104. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher),
  57105. WOLFSSL_SUCCESS);
  57106. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57107. }
  57108. /* cmd DH parameter */
  57109. {
  57110. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  57111. const char* ourdhcert = "./certs/dh3072.pem";
  57112. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  57113. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  57114. WOLFSSL_SUCCESS);
  57115. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57116. #endif
  57117. }
  57118. SSL_CTX_free(ctx);
  57119. SSL_CONF_CTX_free(cctx);
  57120. #endif /* OPENSSL_EXTRA */
  57121. return EXPECT_RESULT();
  57122. }
  57123. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  57124. {
  57125. EXPECT_DECLS;
  57126. #ifdef HAVE_EX_DATA
  57127. int idx1, idx2;
  57128. /* test for unsupported class index */
  57129. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  57130. 0,NULL, NULL, NULL, NULL ), -1);
  57131. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(
  57132. WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  57133. 0,NULL, NULL, NULL, NULL ), -1);
  57134. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  57135. 0,NULL, NULL, NULL, NULL ), -1);
  57136. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  57137. 0,NULL, NULL, NULL, NULL ), -1);
  57138. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  57139. 0,NULL, NULL, NULL, NULL ), -1);
  57140. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  57141. 0,NULL, NULL, NULL, NULL ), -1);
  57142. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  57143. 0,NULL, NULL, NULL, NULL ), -1);
  57144. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  57145. 0,NULL, NULL, NULL, NULL ), -1);
  57146. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  57147. 0,NULL, NULL, NULL, NULL ), -1);
  57148. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  57149. 0,NULL, NULL, NULL, NULL ), -1);
  57150. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  57151. 0,NULL, NULL, NULL, NULL ), -1);
  57152. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  57153. 0,NULL, NULL, NULL, NULL ), -1);
  57154. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20,
  57155. 0,NULL, NULL, NULL, NULL ), -1);
  57156. /* test for supported class index */
  57157. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  57158. 0,NULL, NULL, NULL, NULL );
  57159. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  57160. 0,NULL, NULL, NULL, NULL );
  57161. ExpectIntNE(idx1, -1);
  57162. ExpectIntNE(idx2, -1);
  57163. ExpectIntNE(idx1, idx2);
  57164. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  57165. 0,NULL, NULL, NULL, NULL );
  57166. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  57167. 0,NULL, NULL, NULL, NULL );
  57168. ExpectIntNE(idx1, -1);
  57169. ExpectIntNE(idx2, -1);
  57170. ExpectIntNE(idx1, idx2);
  57171. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  57172. 0,NULL, NULL, NULL, NULL );
  57173. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  57174. 0,NULL, NULL, NULL, NULL );
  57175. ExpectIntNE(idx1, -1);
  57176. ExpectIntNE(idx2, -1);
  57177. ExpectIntNE(idx1, idx2);
  57178. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  57179. 0,NULL, NULL, NULL, NULL );
  57180. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  57181. 0,NULL, NULL, NULL, NULL );
  57182. ExpectIntNE(idx1, -1);
  57183. ExpectIntNE(idx2, -1);
  57184. ExpectIntNE(idx1, idx2);
  57185. #endif /* HAVE_EX_DATA */
  57186. return EXPECT_RESULT();
  57187. }
  57188. #if defined(HAVE_EX_DATA) && defined(HAVE_EXT_CACHE) && \
  57189. (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  57190. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  57191. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  57192. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))))
  57193. #define SESSION_NEW_IDX_LONG 0xDEADBEEF
  57194. #define SESSION_NEW_IDX_VAL ((void*)0xAEADAEAD)
  57195. #define SESSION_DUP_IDX_VAL ((void*)0xDEDEDEDE)
  57196. #define SESSION_NEW_IDX_PTR "Testing"
  57197. static void test_wolfSSL_SESSION_get_ex_new_index_new_cb(void* p, void* ptr,
  57198. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  57199. {
  57200. AssertNotNull(p);
  57201. AssertNull(ptr);
  57202. AssertIntEQ(CRYPTO_set_ex_data(a, idx, SESSION_NEW_IDX_VAL), SSL_SUCCESS);
  57203. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  57204. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  57205. }
  57206. static int test_wolfSSL_SESSION_get_ex_new_index_dup_cb(CRYPTO_EX_DATA* out,
  57207. const CRYPTO_EX_DATA* in, void* inPtr, int idx, long argV,
  57208. void* arg)
  57209. {
  57210. EXPECT_DECLS;
  57211. ExpectNotNull(out);
  57212. ExpectNotNull(in);
  57213. ExpectPtrEq(*(void**)inPtr, SESSION_NEW_IDX_VAL);
  57214. ExpectPtrEq(CRYPTO_get_ex_data(in, idx), SESSION_NEW_IDX_VAL);
  57215. ExpectPtrEq(CRYPTO_get_ex_data(out, idx), SESSION_NEW_IDX_VAL);
  57216. ExpectIntEQ(argV, SESSION_NEW_IDX_LONG);
  57217. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  57218. *(void**)inPtr = SESSION_DUP_IDX_VAL;
  57219. if (EXPECT_SUCCESS()) {
  57220. return SSL_SUCCESS;
  57221. }
  57222. else {
  57223. return SSL_FAILURE;
  57224. }
  57225. }
  57226. static int test_wolfSSL_SESSION_get_ex_new_index_free_cb_called = 0;
  57227. static void test_wolfSSL_SESSION_get_ex_new_index_free_cb(void* p, void* ptr,
  57228. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  57229. {
  57230. EXPECT_DECLS;
  57231. ExpectNotNull(p);
  57232. ExpectNull(ptr);
  57233. ExpectPtrNE(CRYPTO_get_ex_data(a, idx), 0);
  57234. ExpectIntEQ(argValue, SESSION_NEW_IDX_LONG);
  57235. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  57236. if (EXPECT_SUCCESS()) {
  57237. test_wolfSSL_SESSION_get_ex_new_index_free_cb_called++;
  57238. }
  57239. }
  57240. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  57241. {
  57242. EXPECT_DECLS;
  57243. int idx = SSL_SESSION_get_ex_new_index(SESSION_NEW_IDX_LONG,
  57244. (void*)SESSION_NEW_IDX_PTR,
  57245. test_wolfSSL_SESSION_get_ex_new_index_new_cb,
  57246. test_wolfSSL_SESSION_get_ex_new_index_dup_cb,
  57247. test_wolfSSL_SESSION_get_ex_new_index_free_cb);
  57248. SSL_SESSION* s = SSL_SESSION_new();
  57249. SSL_SESSION* d = NULL;
  57250. ExpectNotNull(s);
  57251. ExpectPtrEq(SSL_SESSION_get_ex_data(s, idx), SESSION_NEW_IDX_VAL);
  57252. ExpectNotNull(d = SSL_SESSION_dup(s));
  57253. ExpectPtrEq(SSL_SESSION_get_ex_data(d, idx), SESSION_DUP_IDX_VAL);
  57254. SSL_SESSION_free(s);
  57255. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 1);
  57256. SSL_SESSION_free(d);
  57257. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 2);
  57258. crypto_ex_cb_free(crypto_ex_cb_ctx_session);
  57259. crypto_ex_cb_ctx_session = NULL;
  57260. return EXPECT_RESULT();
  57261. }
  57262. #else
  57263. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  57264. {
  57265. return TEST_SKIPPED;
  57266. }
  57267. #endif
  57268. static int test_wolfSSL_set_psk_use_session_callback(void)
  57269. {
  57270. EXPECT_DECLS;
  57271. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  57272. SSL_CTX* ctx = NULL;
  57273. SSL* ssl = NULL;
  57274. const char* testCertFile;
  57275. const char* testKeyFile;
  57276. #ifdef WOLFSSL_TLS13
  57277. #ifdef NO_WOLFSSL_SERVER
  57278. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  57279. #else
  57280. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  57281. #endif
  57282. #else
  57283. #ifdef NO_WOLFSSL_SERVER
  57284. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  57285. #else
  57286. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57287. #endif
  57288. #endif
  57289. #ifndef NO_RSA
  57290. testCertFile = svrCertFile;
  57291. testKeyFile = svrKeyFile;
  57292. #elif defined(HAVE_ECC)
  57293. testCertFile = eccCertFile;
  57294. testKeyFile = eccKeyFile;
  57295. #else
  57296. testCertFile = NULL;
  57297. testKeyFile = NULL;
  57298. #endif
  57299. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  57300. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  57301. SSL_FILETYPE_PEM));
  57302. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  57303. SSL_FILETYPE_PEM));
  57304. }
  57305. ExpectNotNull(ssl = SSL_new(ctx));
  57306. SSL_set_psk_use_session_callback(ssl, my_psk_use_session_cb);
  57307. SSL_CTX_free(ctx);
  57308. SSL_free(ssl);
  57309. #endif
  57310. return EXPECT_RESULT();
  57311. }
  57312. static int test_wolfSSL_ERR_strings(void)
  57313. {
  57314. EXPECT_DECLS;
  57315. #if !defined(NO_ERROR_STRINGS)
  57316. const char* err1 = "unsupported cipher suite";
  57317. const char* err2 = "wolfSSL PEM routines";
  57318. const char* err = NULL;
  57319. (void)err;
  57320. (void)err1;
  57321. (void)err2;
  57322. #if defined(OPENSSL_EXTRA)
  57323. ExpectNotNull(err = ERR_reason_error_string(UNSUPPORTED_SUITE));
  57324. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  57325. ExpectNotNull(err = ERR_func_error_string(UNSUPPORTED_SUITE));
  57326. ExpectIntEQ((*err == '\0'), 1);
  57327. ExpectNotNull(err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD));
  57328. ExpectIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  57329. #else
  57330. ExpectNotNull(err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE));
  57331. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  57332. ExpectNotNull(err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE));
  57333. ExpectIntEQ((*err == '\0'), 1);
  57334. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  57335. ExpectNotNull(err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2));
  57336. ExpectIntEQ((*err == '\0'), 1);
  57337. #endif
  57338. #endif
  57339. return EXPECT_RESULT();
  57340. }
  57341. static int test_wolfSSL_EVP_shake128(void)
  57342. {
  57343. EXPECT_DECLS;
  57344. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  57345. defined(WOLFSSL_SHAKE128)
  57346. const EVP_MD* md = NULL;
  57347. ExpectNotNull(md = EVP_shake128());
  57348. ExpectIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  57349. #endif
  57350. return EXPECT_RESULT();
  57351. }
  57352. static int test_wolfSSL_EVP_shake256(void)
  57353. {
  57354. EXPECT_DECLS;
  57355. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  57356. defined(WOLFSSL_SHAKE256)
  57357. const EVP_MD* md = NULL;
  57358. ExpectNotNull(md = EVP_shake256());
  57359. ExpectIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  57360. #endif
  57361. return EXPECT_RESULT();
  57362. }
  57363. /*
  57364. * Testing EVP digest API with SM3
  57365. */
  57366. static int test_wolfSSL_EVP_sm3(void)
  57367. {
  57368. int res = TEST_SKIPPED;
  57369. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM3)
  57370. EXPECT_DECLS;
  57371. const EVP_MD* md = NULL;
  57372. EVP_MD_CTX* mdCtx = NULL;
  57373. byte data[WC_SM3_BLOCK_SIZE * 4];
  57374. byte hash[WC_SM3_DIGEST_SIZE];
  57375. byte calcHash[WC_SM3_DIGEST_SIZE];
  57376. byte expHash[WC_SM3_DIGEST_SIZE] = {
  57377. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  57378. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  57379. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  57380. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  57381. };
  57382. word32 chunk;
  57383. word32 i;
  57384. unsigned int sz;
  57385. int ret;
  57386. XMEMSET(data, 0, sizeof(data));
  57387. md = EVP_sm3();
  57388. ExpectTrue(md != NULL);
  57389. ExpectIntEQ(XSTRNCMP(md, "SM3", XSTRLEN("SM3")), 0);
  57390. mdCtx = EVP_MD_CTX_new();
  57391. ExpectTrue(mdCtx != NULL);
  57392. /* Invalid Parameters */
  57393. ExpectIntEQ(EVP_DigestInit(NULL, md), BAD_FUNC_ARG);
  57394. /* Valid Parameters */
  57395. ExpectIntEQ(EVP_DigestInit(mdCtx, md), WOLFSSL_SUCCESS);
  57396. ExpectIntEQ(EVP_DigestUpdate(NULL, NULL, 1), WOLFSSL_FAILURE);
  57397. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 1), WOLFSSL_FAILURE);
  57398. ExpectIntEQ(EVP_DigestUpdate(NULL, data, 1), WOLFSSL_FAILURE);
  57399. /* Valid Parameters */
  57400. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 0), WOLFSSL_SUCCESS);
  57401. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  57402. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  57403. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE),
  57404. WOLFSSL_SUCCESS);
  57405. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE - 2),
  57406. WOLFSSL_SUCCESS);
  57407. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE * 2),
  57408. WOLFSSL_SUCCESS);
  57409. /* Ensure too many bytes for lengths. */
  57410. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_PAD_SIZE),
  57411. WOLFSSL_SUCCESS);
  57412. /* Invalid Parameters */
  57413. ExpectIntEQ(EVP_DigestFinal(NULL, NULL, NULL), WOLFSSL_FAILURE);
  57414. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  57415. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  57416. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  57417. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  57418. /* Valid Parameters */
  57419. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  57420. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  57421. /* Chunk tests. */
  57422. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, sizeof(data)), WOLFSSL_SUCCESS);
  57423. ExpectIntEQ(EVP_DigestFinal(mdCtx, calcHash, &sz), WOLFSSL_SUCCESS);
  57424. ExpectIntEQ(sz, WC_SM3_DIGEST_SIZE);
  57425. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  57426. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  57427. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i, chunk),
  57428. WOLFSSL_SUCCESS);
  57429. }
  57430. if (i < (word32)sizeof(data)) {
  57431. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i,
  57432. (word32)sizeof(data) - i), WOLFSSL_SUCCESS);
  57433. }
  57434. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  57435. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  57436. }
  57437. /* Not testing when the low 32-bit length overflows. */
  57438. ret = EVP_MD_CTX_cleanup(mdCtx);
  57439. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  57440. wolfSSL_EVP_MD_CTX_free(mdCtx);
  57441. res = EXPECT_RESULT();
  57442. #endif
  57443. return res;
  57444. } /* END test_EVP_sm3 */
  57445. static int test_EVP_blake2(void)
  57446. {
  57447. EXPECT_DECLS;
  57448. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  57449. const EVP_MD* md = NULL;
  57450. (void)md;
  57451. #if defined(HAVE_BLAKE2)
  57452. ExpectNotNull(md = EVP_blake2b512());
  57453. ExpectIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  57454. #endif
  57455. #if defined(HAVE_BLAKE2S)
  57456. ExpectNotNull(md = EVP_blake2s256());
  57457. ExpectIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  57458. #endif
  57459. #endif
  57460. return EXPECT_RESULT();
  57461. }
  57462. #if defined(OPENSSL_EXTRA)
  57463. static void list_md_fn(const EVP_MD* m, const char* from,
  57464. const char* to, void* arg)
  57465. {
  57466. const char* mn;
  57467. BIO *bio;
  57468. (void) from;
  57469. (void) to;
  57470. (void) arg;
  57471. (void) mn;
  57472. (void) bio;
  57473. if (!m) {
  57474. /* alias */
  57475. AssertNull(m);
  57476. AssertNotNull(to);
  57477. }
  57478. else {
  57479. AssertNotNull(m);
  57480. AssertNull(to);
  57481. }
  57482. AssertNotNull(from);
  57483. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  57484. mn = EVP_get_digestbyname(from);
  57485. /* print to stderr */
  57486. AssertNotNull(arg);
  57487. bio = BIO_new(BIO_s_file());
  57488. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  57489. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  57490. BIO_free(bio);
  57491. #endif
  57492. }
  57493. #endif
  57494. static int test_EVP_MD_do_all(void)
  57495. {
  57496. int res = TEST_SKIPPED;
  57497. #if defined(OPENSSL_EXTRA)
  57498. EVP_MD_do_all(NULL, stderr);
  57499. EVP_MD_do_all(list_md_fn, stderr);
  57500. res = TEST_SUCCESS;
  57501. #endif
  57502. return res;
  57503. }
  57504. #if defined(OPENSSL_EXTRA)
  57505. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  57506. {
  57507. (void)arg;
  57508. (void)nm;
  57509. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  57510. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  57511. /* print to stderr */
  57512. AssertNotNull(arg);
  57513. BIO *bio = BIO_new(BIO_s_file());
  57514. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  57515. BIO_printf(bio, "%s\n", nm);
  57516. BIO_free(bio);
  57517. #endif
  57518. }
  57519. #endif
  57520. static int test_OBJ_NAME_do_all(void)
  57521. {
  57522. int res = TEST_SKIPPED;
  57523. #if defined(OPENSSL_EXTRA)
  57524. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  57525. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
  57526. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
  57527. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
  57528. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
  57529. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
  57530. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
  57531. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
  57532. OBJ_NAME_do_all(-1, obj_name_t, stderr);
  57533. res = TEST_SUCCESS;
  57534. #endif
  57535. return res;
  57536. }
  57537. static int test_SSL_CIPHER_get_xxx(void)
  57538. {
  57539. EXPECT_DECLS;
  57540. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  57541. !defined(NO_FILESYSTEM)
  57542. const SSL_CIPHER* cipher = NULL;
  57543. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  57544. int i, numCiphers = 0;
  57545. SSL_CTX* ctx = NULL;
  57546. SSL* ssl = NULL;
  57547. const char* testCertFile;
  57548. const char* testKeyFile;
  57549. char buf[256] = {0};
  57550. const char* cipher_id = NULL;
  57551. int expect_nid1 = NID_undef;
  57552. int expect_nid2 = NID_undef;
  57553. int expect_nid3 = NID_undef;
  57554. int expect_nid4 = NID_undef;
  57555. int expect_nid5 = 0;
  57556. const char* cipher_id2 = NULL;
  57557. int expect_nid21 = NID_undef;
  57558. int expect_nid22 = NID_undef;
  57559. int expect_nid23 = NID_undef;
  57560. int expect_nid24 = NID_undef;
  57561. int expect_nid25 = 0;
  57562. (void)cipher;
  57563. (void)supportedCiphers;
  57564. (void)i;
  57565. (void)numCiphers;
  57566. (void)ctx;
  57567. (void)ssl;
  57568. (void)testCertFile;
  57569. (void)testKeyFile;
  57570. #if defined(WOLFSSL_TLS13)
  57571. cipher_id = "TLS13-AES128-GCM-SHA256";
  57572. expect_nid1 = NID_auth_rsa;
  57573. expect_nid2 = NID_aes_128_gcm;
  57574. expect_nid3 = NID_sha256;
  57575. expect_nid4 = NID_kx_any;
  57576. expect_nid5 = 1;
  57577. #if !defined(WOLFSSL_NO_TLS12)
  57578. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  57579. expect_nid21 = NID_auth_rsa;
  57580. expect_nid22 = NID_aes_256_gcm;
  57581. expect_nid23 = NID_sha384;
  57582. expect_nid24 = NID_kx_ecdhe;
  57583. expect_nid25 = 1;
  57584. #endif
  57585. #endif
  57586. #ifdef NO_WOLFSSL_SERVER
  57587. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  57588. #else
  57589. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57590. #endif
  57591. if (cipher_id) {
  57592. #ifndef NO_RSA
  57593. testCertFile = svrCertFile;
  57594. testKeyFile = svrKeyFile;
  57595. #elif defined(HAVE_ECC)
  57596. testCertFile = eccCertFile;
  57597. testKeyFile = eccKeyFile;
  57598. #else
  57599. testCertFile = NULL;
  57600. testKeyFile = NULL;
  57601. #endif
  57602. if (testCertFile != NULL && testKeyFile != NULL) {
  57603. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  57604. SSL_FILETYPE_PEM));
  57605. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  57606. SSL_FILETYPE_PEM));
  57607. }
  57608. ExpectNotNull(ssl = SSL_new(ctx));
  57609. ExpectIntEQ(SSL_in_init(ssl), 1);
  57610. supportedCiphers = SSL_get_ciphers(ssl);
  57611. numCiphers = sk_num(supportedCiphers);
  57612. for (i = 0; i < numCiphers; ++i) {
  57613. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  57614. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  57615. }
  57616. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  57617. break;
  57618. }
  57619. }
  57620. /* test case for */
  57621. if (i != numCiphers) {
  57622. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  57623. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  57624. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  57625. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  57626. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  57627. }
  57628. if (cipher_id2) {
  57629. for (i = 0; i < numCiphers; ++i) {
  57630. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  57631. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  57632. }
  57633. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  57634. break;
  57635. }
  57636. }
  57637. /* test case for */
  57638. if (i != numCiphers) {
  57639. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  57640. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  57641. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  57642. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  57643. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  57644. }
  57645. }
  57646. }
  57647. SSL_CTX_free(ctx);
  57648. SSL_free(ssl);
  57649. #endif
  57650. return EXPECT_RESULT();
  57651. }
  57652. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  57653. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  57654. int* keyFormat)
  57655. {
  57656. int ret;
  57657. byte* key_buf = NULL;
  57658. size_t key_sz = 0;
  57659. EncryptedInfo encInfo;
  57660. XMEMSET(&encInfo, 0, sizeof(encInfo));
  57661. ret = load_file(privKeyFile, &key_buf, &key_sz);
  57662. if (ret == 0) {
  57663. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  57664. NULL, &encInfo, keyFormat);
  57665. }
  57666. if (key_buf != NULL) {
  57667. free(key_buf); key_buf = NULL;
  57668. }
  57669. (void)encInfo; /* not used in this test */
  57670. #ifdef DEBUG_WOLFSSL
  57671. fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  57672. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  57673. (*pDer)->length);
  57674. #endif
  57675. return ret;
  57676. }
  57677. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  57678. {
  57679. int ret = CRYPTOCB_UNAVAILABLE;
  57680. const char* privKeyFile = (const char*)ctx;
  57681. DerBuffer* pDer = NULL;
  57682. int keyFormat = 0;
  57683. if (info->algo_type == WC_ALGO_TYPE_PK) {
  57684. #ifdef DEBUG_WOLFSSL
  57685. fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  57686. #endif
  57687. #ifndef NO_RSA
  57688. if (info->pk.type == WC_PK_TYPE_RSA) {
  57689. switch (info->pk.rsa.type) {
  57690. case RSA_PUBLIC_ENCRYPT:
  57691. case RSA_PUBLIC_DECRYPT:
  57692. /* perform software based RSA public op */
  57693. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  57694. break;
  57695. case RSA_PRIVATE_ENCRYPT:
  57696. case RSA_PRIVATE_DECRYPT:
  57697. {
  57698. RsaKey key;
  57699. /* perform software based RSA private op */
  57700. #ifdef DEBUG_WOLFSSL
  57701. fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
  57702. #endif
  57703. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  57704. &keyFormat);
  57705. if (ret != 0) {
  57706. return ret;
  57707. }
  57708. ret = wc_InitRsaKey(&key, HEAP_HINT);
  57709. if (ret == 0) {
  57710. word32 keyIdx = 0;
  57711. /* load RSA private key and perform private transform */
  57712. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  57713. &key, pDer->length);
  57714. if (ret == 0) {
  57715. ret = wc_RsaFunction(
  57716. info->pk.rsa.in, info->pk.rsa.inLen,
  57717. info->pk.rsa.out, info->pk.rsa.outLen,
  57718. info->pk.rsa.type, &key, info->pk.rsa.rng);
  57719. }
  57720. else {
  57721. /* if decode fails, then fall-back to software based crypto */
  57722. fprintf(stderr, "test_CryptoCb_Func: RSA private "
  57723. "key decode failed %d, falling back to "
  57724. "software\n", ret);
  57725. ret = CRYPTOCB_UNAVAILABLE;
  57726. }
  57727. wc_FreeRsaKey(&key);
  57728. }
  57729. wc_FreeDer(&pDer); pDer = NULL;
  57730. break;
  57731. }
  57732. }
  57733. #ifdef DEBUG_WOLFSSL
  57734. fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  57735. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  57736. #endif
  57737. }
  57738. #endif /* !NO_RSA */
  57739. #ifdef HAVE_ECC
  57740. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  57741. /* mark this key as ephemeral */
  57742. if (info->pk.eckg.key != NULL) {
  57743. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  57744. sizeof(info->pk.eckg.key->label));
  57745. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  57746. }
  57747. }
  57748. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  57749. ecc_key key;
  57750. /* perform software based ECC sign */
  57751. #ifdef DEBUG_WOLFSSL
  57752. fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
  57753. #endif
  57754. if (info->pk.eccsign.key != NULL &&
  57755. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  57756. /* this is an empheral key */
  57757. #ifdef DEBUG_WOLFSSL
  57758. fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
  57759. "ephemeral key\n");
  57760. #endif
  57761. return CRYPTOCB_UNAVAILABLE;
  57762. }
  57763. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  57764. if (ret != 0) {
  57765. return ret;
  57766. }
  57767. ret = wc_ecc_init(&key);
  57768. if (ret == 0) {
  57769. word32 keyIdx = 0;
  57770. /* load ECC private key and perform private transform */
  57771. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  57772. &key, pDer->length);
  57773. if (ret == 0) {
  57774. ret = wc_ecc_sign_hash(
  57775. info->pk.eccsign.in, info->pk.eccsign.inlen,
  57776. info->pk.eccsign.out, info->pk.eccsign.outlen,
  57777. info->pk.eccsign.rng, &key);
  57778. }
  57779. else {
  57780. /* if decode fails, then fall-back to software based crypto */
  57781. fprintf(stderr, "test_CryptoCb_Func: ECC private key "
  57782. "decode failed %d, falling back to software\n", ret);
  57783. ret = CRYPTOCB_UNAVAILABLE;
  57784. }
  57785. wc_ecc_free(&key);
  57786. }
  57787. wc_FreeDer(&pDer); pDer = NULL;
  57788. #ifdef DEBUG_WOLFSSL
  57789. fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  57790. ret, *info->pk.eccsign.outlen);
  57791. #endif
  57792. }
  57793. #endif /* HAVE_ECC */
  57794. #ifdef HAVE_ED25519
  57795. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  57796. ed25519_key key;
  57797. /* perform software based ED25519 sign */
  57798. #ifdef DEBUG_WOLFSSL
  57799. fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
  57800. #endif
  57801. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  57802. if (ret != 0) {
  57803. return ret;
  57804. }
  57805. ret = wc_ed25519_init(&key);
  57806. if (ret == 0) {
  57807. word32 keyIdx = 0;
  57808. /* load ED25519 private key and perform private transform */
  57809. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  57810. &key, pDer->length);
  57811. if (ret == 0) {
  57812. /* calculate public key */
  57813. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  57814. if (ret == 0) {
  57815. key.pubKeySet = 1;
  57816. ret = wc_ed25519_sign_msg_ex(
  57817. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  57818. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  57819. &key, info->pk.ed25519sign.type,
  57820. info->pk.ed25519sign.context,
  57821. info->pk.ed25519sign.contextLen);
  57822. }
  57823. }
  57824. else {
  57825. /* if decode fails, then fall-back to software based crypto */
  57826. fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
  57827. "decode failed %d, falling back to software\n", ret);
  57828. ret = CRYPTOCB_UNAVAILABLE;
  57829. }
  57830. wc_ed25519_free(&key);
  57831. }
  57832. wc_FreeDer(&pDer); pDer = NULL;
  57833. #ifdef DEBUG_WOLFSSL
  57834. fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  57835. ret, *info->pk.ed25519sign.outLen);
  57836. #endif
  57837. }
  57838. #endif /* HAVE_ED25519 */
  57839. }
  57840. (void)thisDevId;
  57841. (void)keyFormat;
  57842. return ret;
  57843. }
  57844. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  57845. static int test_wc_CryptoCb_TLS(int tlsVer,
  57846. const char* cliCaPemFile, const char* cliCertPemFile,
  57847. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  57848. const char* svrCaPemFile, const char* svrCertPemFile,
  57849. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  57850. {
  57851. EXPECT_DECLS;
  57852. callback_functions client_cbf;
  57853. callback_functions server_cbf;
  57854. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  57855. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  57856. if (tlsVer == WOLFSSL_TLSV1_3) {
  57857. #ifdef WOLFSSL_TLS13
  57858. server_cbf.method = wolfTLSv1_3_server_method;
  57859. client_cbf.method = wolfTLSv1_3_client_method;
  57860. #endif
  57861. }
  57862. else if (tlsVer == WOLFSSL_TLSV1_2) {
  57863. #ifndef WOLFSSL_NO_TLS12
  57864. server_cbf.method = wolfTLSv1_2_server_method;
  57865. client_cbf.method = wolfTLSv1_2_client_method;
  57866. #endif
  57867. }
  57868. else if (tlsVer == WOLFSSL_TLSV1_1) {
  57869. #ifndef NO_OLD_TLS
  57870. server_cbf.method = wolfTLSv1_1_server_method;
  57871. client_cbf.method = wolfTLSv1_1_client_method;
  57872. #endif
  57873. }
  57874. else if (tlsVer == WOLFSSL_TLSV1) {
  57875. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  57876. server_cbf.method = wolfTLSv1_server_method;
  57877. client_cbf.method = wolfTLSv1_client_method;
  57878. #endif
  57879. }
  57880. else if (tlsVer == WOLFSSL_SSLV3) {
  57881. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  57882. defined(WOLFSSL_STATIC_RSA)
  57883. server_cbf.method = wolfSSLv3_server_method;
  57884. client_cbf.method = wolfSSLv3_client_method;
  57885. #endif
  57886. }
  57887. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  57888. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  57889. server_cbf.method = wolfDTLSv1_2_server_method;
  57890. client_cbf.method = wolfDTLSv1_2_client_method;
  57891. #endif
  57892. }
  57893. else if (tlsVer == WOLFSSL_DTLSV1) {
  57894. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  57895. server_cbf.method = wolfDTLSv1_server_method;
  57896. client_cbf.method = wolfDTLSv1_client_method;
  57897. #endif
  57898. }
  57899. if (server_cbf.method == NULL) {
  57900. /* not enabled */
  57901. return TEST_SUCCESS;
  57902. }
  57903. /* Setup the keys for the TLS test */
  57904. client_cbf.certPemFile = cliCertPemFile;
  57905. client_cbf.keyPemFile = cliPubKeyPemFile;
  57906. client_cbf.caPemFile = cliCaPemFile;
  57907. server_cbf.certPemFile = svrCertPemFile;
  57908. server_cbf.keyPemFile = svrPubKeyPemFile;
  57909. server_cbf.caPemFile = svrCaPemFile;
  57910. /* Setup a crypto callback with pointer to private key file for testing */
  57911. client_cbf.devId = 1;
  57912. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  57913. (void*)cliPrivKeyPemFile);
  57914. server_cbf.devId = 2;
  57915. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  57916. (void*)svrPrivKeyPemFile);
  57917. /* Perform TLS server and client test */
  57918. /* First test is at WOLFSSL_CTX level */
  57919. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  57920. /* Check for success */
  57921. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  57922. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  57923. if (EXPECT_SUCCESS()) {
  57924. /* Second test is a WOLFSSL object level */
  57925. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  57926. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  57927. }
  57928. /* Check for success */
  57929. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  57930. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  57931. /* Un register the devId's */
  57932. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  57933. client_cbf.devId = INVALID_DEVID;
  57934. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  57935. server_cbf.devId = INVALID_DEVID;
  57936. return EXPECT_RESULT();
  57937. }
  57938. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  57939. static int test_wc_CryptoCb(void)
  57940. {
  57941. EXPECT_DECLS;
  57942. #ifdef WOLF_CRYPTO_CB
  57943. /* TODO: Add crypto callback API tests */
  57944. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  57945. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  57946. int tlsVer;
  57947. #endif
  57948. #ifndef NO_RSA
  57949. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  57950. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  57951. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  57952. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile),
  57953. TEST_SUCCESS);
  57954. }
  57955. #endif
  57956. #ifdef HAVE_ECC
  57957. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  57958. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  57959. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  57960. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile),
  57961. TEST_SUCCESS);
  57962. }
  57963. #endif
  57964. #ifdef HAVE_ED25519
  57965. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  57966. if (tlsVer == WOLFSSL_DTLSV1) continue;
  57967. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  57968. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  57969. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile),
  57970. TEST_SUCCESS);
  57971. }
  57972. #endif
  57973. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  57974. #endif /* WOLF_CRYPTO_CB */
  57975. return EXPECT_RESULT();
  57976. }
  57977. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  57978. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  57979. static int test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  57980. const char* cliCaPemFile, const char* cliCertPemFile,
  57981. const char* cliPrivKeyPemFile,
  57982. const char* svrCaPemFile, const char* svrCertPemFile,
  57983. const char* svrPrivKeyPemFile,
  57984. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  57985. {
  57986. EXPECT_DECLS;
  57987. callback_functions client_cbf;
  57988. callback_functions server_cbf;
  57989. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  57990. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  57991. if (tlsVer == WOLFSSL_TLSV1_3) {
  57992. #ifdef WOLFSSL_TLS13
  57993. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  57994. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  57995. #endif
  57996. }
  57997. else if (tlsVer == WOLFSSL_TLSV1_2) {
  57998. #ifndef WOLFSSL_NO_TLS12
  57999. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  58000. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  58001. #endif
  58002. }
  58003. else if (tlsVer == WOLFSSL_TLSV1_1) {
  58004. #ifndef NO_OLD_TLS
  58005. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  58006. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  58007. #endif
  58008. }
  58009. else if (tlsVer == WOLFSSL_TLSV1) {
  58010. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  58011. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  58012. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  58013. #endif
  58014. }
  58015. else if (tlsVer == WOLFSSL_SSLV3) {
  58016. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  58017. defined(WOLFSSL_STATIC_RSA)
  58018. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  58019. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  58020. #endif
  58021. }
  58022. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  58023. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  58024. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  58025. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  58026. #endif
  58027. }
  58028. else if (tlsVer == WOLFSSL_DTLSV1) {
  58029. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  58030. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  58031. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  58032. #endif
  58033. }
  58034. if (server_cbf.method_ex == NULL) {
  58035. /* not enabled */
  58036. return TEST_SUCCESS;
  58037. }
  58038. /* Setup the keys for the TLS test */
  58039. client_cbf.certPemFile = cliCertPemFile;
  58040. client_cbf.keyPemFile = cliPrivKeyPemFile;
  58041. client_cbf.caPemFile = cliCaPemFile;
  58042. server_cbf.certPemFile = svrCertPemFile;
  58043. server_cbf.keyPemFile = svrPrivKeyPemFile;
  58044. server_cbf.caPemFile = svrCaPemFile;
  58045. client_cbf.mem = cliMem;
  58046. client_cbf.memSz = cliMemSz;
  58047. server_cbf.mem = svrMem;
  58048. server_cbf.memSz = svrMemSz;
  58049. client_cbf.devId = INVALID_DEVID;
  58050. server_cbf.devId = INVALID_DEVID;
  58051. /* Perform TLS server and client test */
  58052. /* First test is at WOLFSSL_CTX level */
  58053. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  58054. /* Check for success */
  58055. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  58056. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  58057. if (EXPECT_SUCCESS()) {
  58058. /* Second test is a WOLFSSL object level */
  58059. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  58060. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  58061. }
  58062. /* Check for success */
  58063. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  58064. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  58065. return EXPECT_RESULT();
  58066. }
  58067. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  58068. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  58069. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  58070. {
  58071. EXPECT_DECLS;
  58072. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  58073. WOLFSSL_MEM_STATS mem_stats;
  58074. WOLFSSL_MEM_CONN_STATS ssl_stats;
  58075. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  58076. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  58077. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  58078. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  58079. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  58080. #endif
  58081. ExpectNotNull((ssl1 = wolfSSL_new(ctx)));
  58082. ExpectNotNull((ssl2 = wolfSSL_new(ctx)));
  58083. /* this should fail because kMaxCtxClients == 2 */
  58084. ExpectNull((ssl3 = wolfSSL_new(ctx)));
  58085. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  58086. #ifdef DEBUG_WOLFSSL
  58087. wolfSSL_PrintStatsConn(&ssl_stats);
  58088. #endif
  58089. (void)ssl_stats;
  58090. }
  58091. /* display collected statistics */
  58092. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  58093. #ifdef DEBUG_WOLFSSL
  58094. wolfSSL_PrintStats(&mem_stats);
  58095. #endif
  58096. (void)mem_stats;
  58097. }
  58098. wolfSSL_free(ssl1);
  58099. wolfSSL_free(ssl2);
  58100. return EXPECT_RESULT();
  58101. }
  58102. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  58103. static int test_wolfSSL_CTX_StaticMemory(void)
  58104. {
  58105. EXPECT_DECLS;
  58106. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  58107. wolfSSL_method_func method_func;
  58108. WOLFSSL_CTX* ctx;
  58109. const int kMaxCtxClients = 2;
  58110. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  58111. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  58112. int tlsVer;
  58113. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  58114. #endif
  58115. #endif
  58116. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  58117. #ifndef NO_WOLFSSL_SERVER
  58118. #ifndef WOLFSSL_NO_TLS12
  58119. method_func = wolfTLSv1_2_server_method_ex;
  58120. #else
  58121. method_func = wolfTLSv1_3_server_method_ex;
  58122. #endif
  58123. #else
  58124. #ifndef WOLFSSL_NO_TLS12
  58125. method_func = wolfTLSv1_2_client_method_ex;
  58126. #else
  58127. method_func = wolfTLSv1_3_client_method_ex;
  58128. #endif
  58129. #endif
  58130. /* Test creating CTX directly from static memory pool */
  58131. ctx = NULL;
  58132. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, method_func, svrMem,
  58133. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  58134. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  58135. wolfSSL_CTX_free(ctx);
  58136. ctx = NULL;
  58137. /* Test for heap allocated CTX, then assigning static pool to it */
  58138. ExpectNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  58139. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, NULL, svrMem,
  58140. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  58141. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  58142. wolfSSL_CTX_free(ctx);
  58143. /* TLS Level Tests using static memory */
  58144. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  58145. #ifndef NO_RSA
  58146. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  58147. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  58148. svrCertFile, cliCertFile, cliKeyFile,
  58149. cliCertFile, svrCertFile, svrKeyFile,
  58150. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  58151. TEST_SUCCESS);
  58152. }
  58153. #endif
  58154. #ifdef HAVE_ECC
  58155. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  58156. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  58157. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  58158. cliEccCertFile, eccCertFile, eccKeyFile,
  58159. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  58160. TEST_SUCCESS);
  58161. }
  58162. #endif
  58163. #ifdef HAVE_ED25519
  58164. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  58165. if (tlsVer == WOLFSSL_DTLSV1) continue;
  58166. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  58167. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  58168. cliEdCertFile, edCertFile, edKeyFile,
  58169. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  58170. TEST_SUCCESS);
  58171. }
  58172. #endif
  58173. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  58174. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  58175. return EXPECT_RESULT();
  58176. }
  58177. static int test_openssl_FIPS_drbg(void)
  58178. {
  58179. EXPECT_DECLS;
  58180. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  58181. DRBG_CTX* dctx = NULL;
  58182. byte data1[32], data2[32], zeroData[32];
  58183. byte testSeed[16];
  58184. size_t dlen = sizeof(data1);
  58185. int i;
  58186. XMEMSET(data1, 0, dlen);
  58187. XMEMSET(data2, 0, dlen);
  58188. XMEMSET(zeroData, 0, sizeof(zeroData));
  58189. for (i = 0; i < (int)sizeof(testSeed); i++) {
  58190. testSeed[i] = (byte)i;
  58191. }
  58192. ExpectNotNull(dctx = FIPS_get_default_drbg());
  58193. ExpectIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  58194. ExpectIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  58195. WOLFSSL_SUCCESS);
  58196. ExpectIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  58197. ExpectIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  58198. WOLFSSL_SUCCESS);
  58199. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  58200. ExpectIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  58201. WOLFSSL_SUCCESS);
  58202. ExpectIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  58203. WOLFSSL_SUCCESS);
  58204. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  58205. ExpectIntNE(XMEMCMP(data1, data2, dlen), 0);
  58206. ExpectIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  58207. #ifndef HAVE_GLOBAL_RNG
  58208. /* gets freed by wolfSSL_Cleanup() when HAVE_GLOBAL_RNG defined */
  58209. wolfSSL_FIPS_drbg_free(dctx);
  58210. #endif
  58211. #endif
  58212. return EXPECT_RESULT();
  58213. }
  58214. static int test_wolfSSL_FIPS_mode(void)
  58215. {
  58216. EXPECT_DECLS;
  58217. #if defined(OPENSSL_ALL)
  58218. #ifdef HAVE_FIPS
  58219. ExpectIntEQ(wolfSSL_FIPS_mode(), 1);
  58220. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  58221. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  58222. #else
  58223. ExpectIntEQ(wolfSSL_FIPS_mode(), 0);
  58224. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  58225. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  58226. #endif
  58227. #endif
  58228. return EXPECT_RESULT();
  58229. }
  58230. #ifdef WOLFSSL_DTLS
  58231. /* Prints out the current window */
  58232. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  58233. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  58234. int i;
  58235. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  58236. word32 b = w[i];
  58237. int j;
  58238. /* Prints out a 32 bit binary number in big endian order */
  58239. for (j = 0; j < 32; j++, b <<= 1) {
  58240. if (b & (((word32)1) << 31))
  58241. fprintf(stderr, "1");
  58242. else
  58243. fprintf(stderr, "0");
  58244. }
  58245. fprintf(stderr, " ");
  58246. }
  58247. fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
  58248. #else
  58249. (void)h;
  58250. (void)l;
  58251. (void)w;
  58252. #endif
  58253. }
  58254. /* a - cur_hi
  58255. * b - cur_lo
  58256. * c - next_hi
  58257. * d - next_lo
  58258. * e - window
  58259. * f - expected next_hi
  58260. * g - expected next_lo
  58261. * h - expected window[1]
  58262. * i - expected window[0]
  58263. */
  58264. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  58265. ExpectIntEQ(wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)), 1); \
  58266. DUW_TEST_print_window_binary((a), (b), (e)); \
  58267. ExpectIntEQ((c), (f)); \
  58268. ExpectIntEQ((d), (g)); \
  58269. ExpectIntEQ((e)[1], (h)); \
  58270. ExpectIntEQ((e)[0], (i)); \
  58271. } while (0)
  58272. static int test_wolfSSL_DtlsUpdateWindow(void)
  58273. {
  58274. EXPECT_DECLS;
  58275. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  58276. word32 next_lo = 0;
  58277. word16 next_hi = 0;
  58278. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  58279. fprintf(stderr, "\n");
  58280. #endif
  58281. XMEMSET(window, 0, sizeof window);
  58282. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  58283. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  58284. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  58285. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  58286. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  58287. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  58288. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  58289. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  58290. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  58291. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  58292. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  58293. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  58294. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  58295. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  58296. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  58297. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  58298. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  58299. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  58300. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  58301. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  58302. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  58303. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  58304. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  58305. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  58306. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  58307. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  58308. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  58309. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  58310. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  58311. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  58312. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  58313. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  58314. return EXPECT_RESULT();
  58315. }
  58316. #endif /* WOLFSSL_DTLS */
  58317. #ifdef WOLFSSL_DTLS
  58318. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  58319. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  58320. {
  58321. DtlsMsg* cur;
  58322. static byte msg[100];
  58323. static byte msgInit = 0;
  58324. if (!msgInit) {
  58325. int i;
  58326. for (i = 0; i < 100; i++)
  58327. msg[i] = i + 1;
  58328. msgInit = 1;
  58329. }
  58330. /* Sanitize test parameters */
  58331. if (len > sizeof(msg))
  58332. return -1;
  58333. if (f_offset + f_len > sizeof(msg))
  58334. return -1;
  58335. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  58336. if (ssl->dtls_rx_msg_list == NULL)
  58337. return -100;
  58338. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  58339. return -200;
  58340. if (cur->fragBucketListCount != f_count)
  58341. return -300;
  58342. if (cur->ready != ready)
  58343. return -400;
  58344. if (cur->bytesReceived != bytesReceived)
  58345. return -500;
  58346. if (ready) {
  58347. if (cur->fragBucketList != NULL)
  58348. return -600;
  58349. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  58350. return -700;
  58351. }
  58352. else {
  58353. DtlsFragBucket* fb;
  58354. if (cur->fragBucketList == NULL)
  58355. return -800;
  58356. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  58357. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  58358. return -900;
  58359. }
  58360. }
  58361. return 0;
  58362. }
  58363. static int test_wolfSSL_DTLS_fragment_buckets(void)
  58364. {
  58365. EXPECT_DECLS;
  58366. WOLFSSL ssl[1];
  58367. XMEMSET(ssl, 0, sizeof(*ssl));
  58368. ExpectIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  58369. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  58370. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  58371. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  58372. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  58373. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  58374. /* Test all permutations of 3 regions */
  58375. /* 1 2 3 */
  58376. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  58377. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  58378. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  58379. /* 1 3 2 */
  58380. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  58381. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  58382. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  58383. /* 2 1 3 */
  58384. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  58385. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  58386. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  58387. /* 2 3 1 */
  58388. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  58389. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  58390. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  58391. /* 3 1 2 */
  58392. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  58393. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  58394. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  58395. /* 3 2 1 */
  58396. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  58397. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  58398. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  58399. /* Test overlapping regions */
  58400. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  58401. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  58402. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  58403. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  58404. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  58405. /* Test overlapping multiple regions */
  58406. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  58407. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  58408. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  58409. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  58410. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  58411. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  58412. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  58413. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  58414. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  58415. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  58416. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  58417. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  58418. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  58419. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  58420. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  58421. ssl->dtls_rx_msg_list = NULL;
  58422. ssl->dtls_rx_msg_list_sz = 0;
  58423. return EXPECT_RESULT();
  58424. }
  58425. #endif
  58426. #if !defined(NO_FILESYSTEM) && \
  58427. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58428. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  58429. static int test_wolfSSL_dtls_stateless2(void)
  58430. {
  58431. EXPECT_DECLS;
  58432. WOLFSSL *ssl_c = NULL;
  58433. WOLFSSL *ssl_c2 = NULL;
  58434. WOLFSSL *ssl_s = NULL;
  58435. struct test_memio_ctx test_ctx;
  58436. WOLFSSL_CTX *ctx_c = NULL;
  58437. WOLFSSL_CTX *ctx_s = NULL;
  58438. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58439. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58440. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  58441. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  58442. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  58443. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  58444. /* send CH */
  58445. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  58446. (ssl_c2->error == WANT_READ));
  58447. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58448. (ssl_s->error == WANT_READ));
  58449. ExpectIntNE(test_ctx.c_len, 0);
  58450. /* consume HRR */
  58451. test_ctx.c_len = 0;
  58452. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58453. wolfSSL_free(ssl_c2);
  58454. wolfSSL_free(ssl_c);
  58455. wolfSSL_free(ssl_s);
  58456. wolfSSL_CTX_free(ctx_c);
  58457. wolfSSL_CTX_free(ctx_s);
  58458. return EXPECT_RESULT();
  58459. }
  58460. #ifdef HAVE_MAX_FRAGMENT
  58461. static int test_wolfSSL_dtls_stateless_maxfrag(void)
  58462. {
  58463. EXPECT_DECLS;
  58464. WOLFSSL *ssl_c = NULL;
  58465. WOLFSSL *ssl_c2 = NULL;
  58466. WOLFSSL *ssl_s = NULL;
  58467. struct test_memio_ctx test_ctx;
  58468. WOLFSSL_CTX *ctx_c = NULL;
  58469. WOLFSSL_CTX *ctx_s = NULL;
  58470. word16 max_fragment = 0;
  58471. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58472. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58473. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  58474. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  58475. ExpectIntEQ(wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8),
  58476. WOLFSSL_SUCCESS);
  58477. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  58478. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  58479. if (ssl_s != NULL) {
  58480. max_fragment = ssl_s->max_fragment;
  58481. }
  58482. /* send CH */
  58483. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  58484. (ssl_c2->error == WANT_READ));
  58485. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58486. (ssl_s->error == WANT_READ));
  58487. /* CH without cookie shouldn't change state */
  58488. ExpectIntEQ(ssl_s->max_fragment, max_fragment);
  58489. ExpectIntNE(test_ctx.c_len, 0);
  58490. /* consume HRR from buffer */
  58491. test_ctx.c_len = 0;
  58492. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58493. wolfSSL_free(ssl_c2);
  58494. wolfSSL_free(ssl_c);
  58495. wolfSSL_free(ssl_s);
  58496. wolfSSL_CTX_free(ctx_c);
  58497. wolfSSL_CTX_free(ctx_s);
  58498. return EXPECT_RESULT();
  58499. }
  58500. #endif /* HAVE_MAX_FRAGMENT */
  58501. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  58502. #define ROUNDS_WITH_HVR 4
  58503. #define ROUNDS_WITHOUT_HVR 2
  58504. #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
  58505. static int buf_is_hvr(const byte *data, int len)
  58506. {
  58507. if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
  58508. return 0;
  58509. return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
  58510. }
  58511. static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
  58512. {
  58513. EXPECT_DECLS;
  58514. struct test_memio_ctx test_ctx;
  58515. WOLFSSL_CTX *ctx_c = NULL;
  58516. WOLFSSL_CTX *ctx_s = NULL;
  58517. WOLFSSL *ssl_c = NULL;
  58518. WOLFSSL *ssl_s = NULL;
  58519. WOLFSSL_SESSION *sess = NULL;
  58520. int round_trips;
  58521. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58522. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  58523. &ssl_s, wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  58524. #ifdef HAVE_SESSION_TICKET
  58525. if (useticket) {
  58526. ExpectIntEQ(wolfSSL_UseSessionTicket(ssl_c), WOLFSSL_SUCCESS);
  58527. }
  58528. #endif
  58529. round_trips = ROUNDS_WITH_HVR;
  58530. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, round_trips,
  58531. &round_trips), 0);
  58532. ExpectIntEQ(round_trips, ROUNDS_WITH_HVR);
  58533. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  58534. wolfSSL_shutdown(ssl_c);
  58535. wolfSSL_shutdown(ssl_s);
  58536. wolfSSL_free(ssl_c);
  58537. ssl_c = NULL;
  58538. wolfSSL_free(ssl_s);
  58539. ssl_s = NULL;
  58540. test_ctx.c_len = test_ctx.s_len = 0;
  58541. /* make resumption invalid */
  58542. if (bad && (sess != NULL)) {
  58543. if (useticket) {
  58544. #ifdef HAVE_SESSION_TICKET
  58545. if (sess->ticket != NULL) {
  58546. sess->ticket[0] = !sess->ticket[0];
  58547. }
  58548. #endif /* HAVE_SESSION_TICKET */
  58549. }
  58550. else {
  58551. sess->sessionID[0] = !sess->sessionID[0];
  58552. }
  58553. }
  58554. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  58555. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  58556. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  58557. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  58558. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  58559. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  58560. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  58561. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  58562. (ssl_c->error == WANT_READ));
  58563. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58564. (ssl_s->error == WANT_READ));
  58565. ExpectFalse(bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  58566. ExpectFalse(!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  58567. if (!useticket) {
  58568. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips), 0);
  58569. ExpectFalse(bad && round_trips != ROUNDS_WITH_HVR - 1);
  58570. ExpectFalse(!bad && round_trips != ROUNDS_WITHOUT_HVR - 1);
  58571. }
  58572. wolfSSL_SESSION_free(sess);
  58573. wolfSSL_free(ssl_c);
  58574. wolfSSL_free(ssl_s);
  58575. wolfSSL_CTX_free(ctx_c);
  58576. wolfSSL_CTX_free(ctx_s);
  58577. return EXPECT_RESULT();
  58578. }
  58579. static int test_wolfSSL_dtls_stateless_resume(void)
  58580. {
  58581. EXPECT_DECLS;
  58582. #ifdef HAVE_SESSION_TICKET
  58583. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 0), TEST_SUCCESS);
  58584. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 1), TEST_SUCCESS);
  58585. #endif /* HAVE_SESION_TICKET */
  58586. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 0), TEST_SUCCESS);
  58587. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 1), TEST_SUCCESS);
  58588. return EXPECT_RESULT();
  58589. }
  58590. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  58591. #if !defined(NO_OLD_TLS)
  58592. static int test_wolfSSL_dtls_stateless_downgrade(void)
  58593. {
  58594. EXPECT_DECLS;
  58595. WOLFSSL_CTX *ctx_c = NULL;
  58596. WOLFSSL_CTX *ctx_c2 = NULL;
  58597. WOLFSSL_CTX *ctx_s = NULL;
  58598. WOLFSSL *ssl_c = NULL;
  58599. WOLFSSL *ssl_c2 = NULL;
  58600. WOLFSSL *ssl_s = NULL;
  58601. struct test_memio_ctx test_ctx;
  58602. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58603. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58604. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  58605. ExpectIntEQ(wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1),
  58606. WOLFSSL_SUCCESS);
  58607. ExpectNotNull(ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method()));
  58608. wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
  58609. wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
  58610. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c2));
  58611. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  58612. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  58613. /* send CH */
  58614. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  58615. (ssl_c2->error == WANT_READ));
  58616. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58617. (ssl_s->error == WANT_READ));
  58618. ExpectIntNE(test_ctx.c_len, 0);
  58619. /* consume HRR */
  58620. test_ctx.c_len = 0;
  58621. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58622. wolfSSL_free(ssl_c2);
  58623. wolfSSL_free(ssl_c);
  58624. wolfSSL_free(ssl_s);
  58625. wolfSSL_CTX_free(ctx_c);
  58626. wolfSSL_CTX_free(ctx_c2);
  58627. wolfSSL_CTX_free(ctx_s);
  58628. return EXPECT_RESULT();
  58629. }
  58630. #endif /* !defined(NO_OLD_TLS) */
  58631. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58632. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
  58633. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58634. !defined(NO_OLD_TLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  58635. static int test_WOLFSSL_dtls_version_alert(void)
  58636. {
  58637. EXPECT_DECLS;
  58638. struct test_memio_ctx test_ctx;
  58639. WOLFSSL_CTX *ctx_c = NULL;
  58640. WOLFSSL_CTX *ctx_s = NULL;
  58641. WOLFSSL *ssl_c = NULL;
  58642. WOLFSSL *ssl_s = NULL;
  58643. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58644. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58645. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method), 0);
  58646. /* client hello */
  58647. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  58648. (ssl_c->error == WANT_READ));
  58649. /* hrr */
  58650. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58651. (ssl_s->error == WANT_READ));
  58652. /* client hello 1 */
  58653. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  58654. (ssl_c->error == WANT_READ));
  58655. /* server hello */
  58656. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58657. (ssl_s->error == WANT_READ));
  58658. /* should fail */
  58659. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  58660. (ssl_c->error == VERSION_ERROR));
  58661. /* shuould fail */
  58662. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58663. (ssl_s->error == VERSION_ERROR || ssl_s->error == FATAL_ERROR));
  58664. wolfSSL_free(ssl_c);
  58665. wolfSSL_free(ssl_s);
  58666. wolfSSL_CTX_free(ctx_c);
  58667. wolfSSL_CTX_free(ctx_s);
  58668. return EXPECT_RESULT();
  58669. }
  58670. #else
  58671. static int test_WOLFSSL_dtls_version_alert(void)
  58672. {
  58673. return TEST_SKIPPED;
  58674. }
  58675. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  58676. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  58677. * !defined(NO_OLD_TLS) && !defined(NO_RSA)
  58678. */
  58679. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  58680. && defined(WOLFSSL_TLS13) && \
  58681. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))\
  58682. && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  58683. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  58684. {
  58685. struct test_memio_ctx *test_ctx;
  58686. byte buf[2048];
  58687. int idx, sz;
  58688. word32 tmp;
  58689. int ret;
  58690. idx = 5; /* space for record header */
  58691. buf[idx] = session_ticket; /* type */
  58692. idx++;
  58693. tmp = OPAQUE32_LEN +
  58694. OPAQUE32_LEN +
  58695. OPAQUE8_LEN + nonceLength +
  58696. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  58697. c32to24(tmp, buf + idx);
  58698. idx += OPAQUE24_LEN;
  58699. c32toa((word32)12345, buf+idx); /* lifetime */
  58700. idx += OPAQUE32_LEN;
  58701. c32toa((word32)12345, buf+idx); /* add */
  58702. idx += OPAQUE32_LEN;
  58703. buf[idx] = nonceLength; /* nonce length */
  58704. idx++;
  58705. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  58706. idx += nonceLength;
  58707. tmp = 1; /* ticket len */
  58708. c16toa((word16)tmp, buf+idx);
  58709. idx += 2;
  58710. buf[idx] = 0xFF; /* ticket */
  58711. idx++;
  58712. tmp = 0; /* ext len */
  58713. c16toa((word16)tmp, buf+idx);
  58714. idx += 2;
  58715. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  58716. handshake, 0, 0, 0);
  58717. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  58718. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  58719. return !(ret == sz);
  58720. }
  58721. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  58722. {
  58723. int ret = 0;
  58724. if ((sess == NULL) || (sess->ticketNonce.len != len)) {
  58725. ret = -1;
  58726. }
  58727. else {
  58728. int i;
  58729. for (i = 0; i < len; i++) {
  58730. if (sess->ticketNonce.data[i] != len) {
  58731. ret = -1;
  58732. break;
  58733. }
  58734. }
  58735. }
  58736. return ret;
  58737. }
  58738. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  58739. {
  58740. EXPECT_DECLS;
  58741. char *buf[1024];
  58742. ExpectIntEQ(send_new_session_ticket(ssl_s, len, len), 0);
  58743. ExpectTrue((wolfSSL_recv(ssl_c, buf, 1024, 0) == WOLFSSL_FATAL_ERROR) &&
  58744. (ssl_c->error == WANT_READ));
  58745. ExpectIntEQ(test_ticket_nonce_check(ssl_c->session, len), 0);
  58746. return EXPECT_RESULT();
  58747. }
  58748. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  58749. {
  58750. EXPECT_DECLS;
  58751. WOLFSSL_SESSION *sess = NULL;
  58752. WOLFSSL_SESSION *cached = NULL;
  58753. WOLFSSL_CTX *ctx = ssl_c->ctx;
  58754. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, len), TEST_SUCCESS);
  58755. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  58756. ExpectIntEQ(AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  58757. NULL, ssl_c->options.side, 1,NULL), 0);
  58758. ExpectNotNull(cached = wolfSSL_SESSION_new());
  58759. ExpectIntEQ(wolfSSL_GetSessionFromCache(ssl_c, cached), WOLFSSL_SUCCESS);
  58760. ExpectIntEQ(test_ticket_nonce_check(cached, len), 0);
  58761. wolfSSL_SESSION_free(cached);
  58762. wolfSSL_SESSION_free(sess);
  58763. return EXPECT_RESULT();
  58764. }
  58765. static int test_ticket_nonce_malloc(void)
  58766. {
  58767. EXPECT_DECLS;
  58768. struct test_memio_ctx test_ctx;
  58769. WOLFSSL_CTX *ctx_c = NULL;
  58770. WOLFSSL_CTX *ctx_s = NULL;
  58771. WOLFSSL *ssl_c = NULL;
  58772. WOLFSSL *ssl_s = NULL;
  58773. byte small;
  58774. byte medium;
  58775. byte big;
  58776. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58777. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58778. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  58779. /* will send ticket manually */
  58780. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(ssl_s), 0);
  58781. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  58782. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  58783. while (EXPECT_SUCCESS() && (ssl_c->options.handShakeDone == 0) &&
  58784. (ssl_s->options.handShakeDone == 0)) {
  58785. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_SUCCESS) ||
  58786. (ssl_c->error == WANT_READ));
  58787. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_SUCCESS) ||
  58788. (ssl_s->error == WANT_READ));
  58789. }
  58790. small = TLS13_TICKET_NONCE_STATIC_SZ;
  58791. medium = small + 20 <= 255 ? small + 20 : 255;
  58792. big = medium + 20 <= 255 ? small + 20 : 255;
  58793. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  58794. ExpectPtrEq(ssl_c->session->ticketNonce.data,
  58795. ssl_c->session->ticketNonce.dataStatic);
  58796. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  58797. TEST_SUCCESS);
  58798. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, big), TEST_SUCCESS);
  58799. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  58800. TEST_SUCCESS);
  58801. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  58802. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  58803. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  58804. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, big), TEST_SUCCESS);
  58805. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  58806. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  58807. wolfSSL_free(ssl_c);
  58808. wolfSSL_free(ssl_s);
  58809. wolfSSL_CTX_free(ctx_c);
  58810. wolfSSL_CTX_free(ctx_s);
  58811. return EXPECT_RESULT();
  58812. }
  58813. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  58814. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
  58815. !defined(WOLFSSL_TICKET_DECRYPT_NO_CREATE) && \
  58816. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  58817. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_RSA) && \
  58818. defined(HAVE_ECC) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  58819. static int test_ticket_ret_create(void)
  58820. {
  58821. EXPECT_DECLS;
  58822. WOLFSSL_CTX *ctx_c = NULL;
  58823. WOLFSSL_CTX *ctx_s = NULL;
  58824. WOLFSSL *ssl_c = NULL;
  58825. WOLFSSL *ssl_s = NULL;
  58826. byte ticket[SESSION_TICKET_LEN];
  58827. struct test_memio_ctx test_ctx;
  58828. WOLFSSL_SESSION *sess = NULL;
  58829. word16 ticketLen = 0;
  58830. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58831. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58832. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  58833. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  58834. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  58835. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx_c), WOLFSSL_SUCCESS);
  58836. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58837. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  58838. ExpectIntLE(sess->ticketLen, SESSION_TICKET_LEN);
  58839. if (sess != NULL) {
  58840. ticketLen = sess->ticketLen;
  58841. XMEMCPY(ticket, sess->ticket, sess->ticketLen);
  58842. }
  58843. wolfSSL_free(ssl_c);
  58844. ssl_c = NULL;
  58845. wolfSSL_free(ssl_s);
  58846. ssl_s = NULL;
  58847. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  58848. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  58849. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  58850. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  58851. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  58852. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  58853. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  58854. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58855. ExpectIntLE(ssl_c->session->ticketLen, SESSION_TICKET_LEN);
  58856. ExpectIntEQ(ssl_c->session->ticketLen, ticketLen);
  58857. ExpectTrue(XMEMCMP(ssl_c->session->ticket, ticket, ticketLen) != 0);
  58858. wolfSSL_SESSION_free(sess);
  58859. wolfSSL_free(ssl_c);
  58860. wolfSSL_free(ssl_s);
  58861. wolfSSL_CTX_free(ctx_c);
  58862. wolfSSL_CTX_free(ctx_s);
  58863. return EXPECT_RESULT();
  58864. }
  58865. #else
  58866. static int test_ticket_ret_create(void)
  58867. {
  58868. return TEST_SKIPPED;
  58869. }
  58870. #endif
  58871. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && \
  58872. defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  58873. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  58874. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  58875. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  58876. static void test_ticket_and_psk_mixing_on_result(WOLFSSL* ssl)
  58877. {
  58878. int ret;
  58879. WOLFSSL_SESSION* session = NULL;
  58880. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  58881. if (!wolfSSL_is_server(ssl)) {
  58882. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  58883. AssertNotNull(session);
  58884. }
  58885. do {
  58886. ret = wolfSSL_shutdown(ssl);
  58887. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  58888. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  58889. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  58890. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  58891. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  58892. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  58893. #endif
  58894. if (!wolfSSL_is_server(ssl)) {
  58895. /* client */
  58896. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  58897. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  58898. wolfSSL_set_session(ssl, session);
  58899. wolfSSL_SESSION_free(session);
  58900. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  58901. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  58902. }
  58903. else {
  58904. /* server */
  58905. /* Different ciphersuite so that the ticket will be invalidated based on
  58906. * the ciphersuite */
  58907. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"),
  58908. WOLFSSL_SUCCESS);
  58909. wolfSSL_set_psk_server_tls13_callback(ssl, my_psk_server_tls13_cb);
  58910. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  58911. }
  58912. }
  58913. static void test_ticket_and_psk_mixing_ssl_ready(WOLFSSL* ssl)
  58914. {
  58915. AssertIntEQ(wolfSSL_UseSessionTicket(ssl), WOLFSSL_SUCCESS);
  58916. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  58917. WOLFSSL_SUCCESS);
  58918. }
  58919. static int test_ticket_and_psk_mixing(void)
  58920. {
  58921. EXPECT_DECLS;
  58922. /* Test mixing tickets and regular PSK */
  58923. callback_functions client_cbs, server_cbs;
  58924. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58925. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58926. client_cbs.method = wolfTLSv1_3_client_method;
  58927. server_cbs.method = wolfTLSv1_3_server_method;
  58928. client_cbs.ssl_ready = test_ticket_and_psk_mixing_ssl_ready;
  58929. client_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  58930. server_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  58931. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  58932. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  58933. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  58934. return EXPECT_RESULT();
  58935. }
  58936. #else
  58937. static int test_ticket_and_psk_mixing(void)
  58938. {
  58939. return TEST_SKIPPED;
  58940. }
  58941. #endif
  58942. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  58943. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  58944. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  58945. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  58946. static int test_prioritize_psk_cb_called = FALSE;
  58947. static unsigned int test_prioritize_psk_cb(WOLFSSL* ssl,
  58948. const char* identity, unsigned char* key, unsigned int key_max_len,
  58949. const char** ciphersuite)
  58950. {
  58951. test_prioritize_psk_cb_called = TRUE;
  58952. return my_psk_server_tls13_cb(ssl, identity, key, key_max_len, ciphersuite);
  58953. }
  58954. static void test_prioritize_psk_on_result(WOLFSSL* ssl)
  58955. {
  58956. int ret;
  58957. WOLFSSL_SESSION* session = NULL;
  58958. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  58959. if (!wolfSSL_is_server(ssl)) {
  58960. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  58961. AssertNotNull(session);
  58962. }
  58963. do {
  58964. ret = wolfSSL_shutdown(ssl);
  58965. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  58966. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  58967. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  58968. /* Previous connection was made with TLS13-AES128-GCM-SHA256. Order is
  58969. * important. */
  58970. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  58971. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  58972. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  58973. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  58974. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  58975. #endif
  58976. if (!wolfSSL_is_server(ssl)) {
  58977. /* client */
  58978. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  58979. wolfSSL_set_session(ssl, session);
  58980. wolfSSL_SESSION_free(session);
  58981. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  58982. }
  58983. else {
  58984. /* server */
  58985. wolfSSL_set_psk_server_tls13_callback(ssl, test_prioritize_psk_cb);
  58986. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  58987. #ifdef WOLFSSL_PRIORITIZE_PSK
  58988. /* The ticket should be first tried with all ciphersuites and chosen */
  58989. AssertFalse(test_prioritize_psk_cb_called);
  58990. #else
  58991. /* Ciphersuites should be tried with each PSK. This triggers the PSK
  58992. * callback that sets this var. */
  58993. AssertTrue(test_prioritize_psk_cb_called);
  58994. #endif
  58995. }
  58996. }
  58997. static void test_prioritize_psk_ssl_ready(WOLFSSL* ssl)
  58998. {
  58999. if (!wolfSSL_is_server(ssl))
  59000. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  59001. WOLFSSL_SUCCESS);
  59002. else
  59003. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  59004. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  59005. }
  59006. static int test_prioritize_psk(void)
  59007. {
  59008. EXPECT_DECLS;
  59009. /* We always send the ticket first. With WOLFSSL_PRIORITIZE_PSK the order
  59010. * of the PSK's will be followed instead of the ciphersuite. */
  59011. callback_functions client_cbs, server_cbs;
  59012. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59013. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59014. client_cbs.method = wolfTLSv1_3_client_method;
  59015. server_cbs.method = wolfTLSv1_3_server_method;
  59016. client_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  59017. server_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  59018. client_cbs.on_result = test_prioritize_psk_on_result;
  59019. server_cbs.on_result = test_prioritize_psk_on_result;
  59020. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  59021. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  59022. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  59023. return EXPECT_RESULT();
  59024. }
  59025. #else
  59026. static int test_prioritize_psk(void)
  59027. {
  59028. return TEST_SKIPPED;
  59029. }
  59030. #endif
  59031. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  59032. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  59033. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  59034. !defined(WOLFSSL_NO_TLS12)
  59035. static int test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server(WOLFSSL_CTX* ctx)
  59036. {
  59037. EXPECT_DECLS;
  59038. ExpectTrue(SSL_CTX_set_cipher_list(ctx, "DEFAULT"));
  59039. /* Set TLS 1.3 specific suite */
  59040. ExpectTrue(SSL_CTX_set_ciphersuites(ctx, "TLS13-AES128-GCM-SHA256"));
  59041. return EXPECT_RESULT();
  59042. }
  59043. static int test_wolfSSL_CTX_set_ciphersuites(void)
  59044. {
  59045. EXPECT_DECLS;
  59046. /* Test using SSL_CTX_set_cipher_list and SSL_CTX_set_ciphersuites and then
  59047. * do a 1.2 connection. */
  59048. test_ssl_cbf client_cbs, server_cbs;
  59049. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59050. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59051. client_cbs.method = wolfTLSv1_2_client_method;
  59052. server_cbs.method = wolfTLS_server_method; /* Allow downgrade */
  59053. server_cbs.ctx_ready = test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server;
  59054. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  59055. &server_cbs, NULL), TEST_SUCCESS);
  59056. return EXPECT_RESULT();
  59057. }
  59058. #else
  59059. static int test_wolfSSL_CTX_set_ciphersuites(void)
  59060. {
  59061. return TEST_SKIPPED;
  59062. }
  59063. #endif
  59064. #if defined(HAVE_CRL) && defined(WOLFSSL_CHECK_ALERT_ON_ERR) && \
  59065. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  59066. static int test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready(WOLFSSL_CTX* ctx)
  59067. {
  59068. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  59069. return TEST_SUCCESS;
  59070. }
  59071. static int test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup(WOLFSSL* ssl)
  59072. {
  59073. EXPECT_DECLS;
  59074. WOLFSSL_ALERT_HISTORY h;
  59075. ExpectIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  59076. ExpectIntEQ(h.last_rx.level, alert_fatal);
  59077. ExpectIntEQ(h.last_rx.code, certificate_revoked);
  59078. return EXPECT_RESULT();
  59079. }
  59080. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  59081. {
  59082. EXPECT_DECLS;
  59083. test_ssl_cbf client_cbs, server_cbs;
  59084. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59085. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59086. server_cbs.certPemFile = "./certs/server-revoked-cert.pem";
  59087. server_cbs.keyPemFile = "./certs/server-revoked-key.pem";
  59088. client_cbs.crlPemFile = "./certs/crl/crl.revoked";
  59089. client_cbs.ctx_ready = test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready;
  59090. server_cbs.on_cleanup = test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup;
  59091. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  59092. &server_cbs, NULL), TEST_FAIL);
  59093. return EXPECT_RESULT();
  59094. }
  59095. #else
  59096. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  59097. {
  59098. return TEST_SKIPPED;
  59099. }
  59100. #endif
  59101. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET) \
  59102. && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  59103. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  59104. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  59105. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  59106. static WOLFSSL_CTX* test_TLS_13_ticket_different_ciphers_ctx = NULL;
  59107. static WOLFSSL_SESSION* test_TLS_13_ticket_different_ciphers_session = NULL;
  59108. static int test_TLS_13_ticket_different_ciphers_run = 0;
  59109. static int test_TLS_13_ticket_different_ciphers_ssl_ready(WOLFSSL* ssl)
  59110. {
  59111. EXPECT_DECLS;
  59112. switch (test_TLS_13_ticket_different_ciphers_run) {
  59113. case 0:
  59114. /* First run */
  59115. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  59116. WOLFSSL_SUCCESS);
  59117. if (wolfSSL_is_server(ssl)) {
  59118. ExpectNotNull(test_TLS_13_ticket_different_ciphers_ctx =
  59119. wolfSSL_get_SSL_CTX(ssl));
  59120. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_up_ref(
  59121. test_TLS_13_ticket_different_ciphers_ctx));
  59122. }
  59123. break;
  59124. case 1:
  59125. /* Second run */
  59126. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  59127. "TLS13-AES128-GCM-SHA256"),
  59128. WOLFSSL_SUCCESS);
  59129. if (!wolfSSL_is_server(ssl)) {
  59130. ExpectIntEQ(wolfSSL_set_session(ssl,
  59131. test_TLS_13_ticket_different_ciphers_session),
  59132. WOLFSSL_SUCCESS);
  59133. }
  59134. break;
  59135. default:
  59136. /* Bad state? */
  59137. Fail(("Should not enter here"), ("Should not enter here"));
  59138. }
  59139. return EXPECT_RESULT();
  59140. }
  59141. static int test_TLS_13_ticket_different_ciphers_on_result(WOLFSSL* ssl)
  59142. {
  59143. EXPECT_DECLS;
  59144. switch (test_TLS_13_ticket_different_ciphers_run) {
  59145. case 0:
  59146. /* First run */
  59147. ExpectNotNull(test_TLS_13_ticket_different_ciphers_session =
  59148. wolfSSL_get1_session(ssl));
  59149. break;
  59150. case 1:
  59151. /* Second run */
  59152. ExpectTrue(wolfSSL_session_reused(ssl));
  59153. break;
  59154. default:
  59155. /* Bad state? */
  59156. Fail(("Should not enter here"), ("Should not enter here"));
  59157. }
  59158. return EXPECT_RESULT();
  59159. }
  59160. static int test_TLS_13_ticket_different_ciphers(void)
  59161. {
  59162. EXPECT_DECLS;
  59163. /* Check that we handle the connection when the ticket doesn't match
  59164. * the first ciphersuite. */
  59165. test_ssl_cbf client_cbs, server_cbs;
  59166. struct test_params {
  59167. method_provider client_meth;
  59168. method_provider server_meth;
  59169. int doUdp;
  59170. } params[] = {
  59171. #ifdef WOLFSSL_DTLS13
  59172. /* Test that the stateless code handles sessions correctly */
  59173. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, 1},
  59174. #endif
  59175. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, 0},
  59176. };
  59177. size_t i;
  59178. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  59179. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59180. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59181. test_TLS_13_ticket_different_ciphers_run = 0;
  59182. client_cbs.doUdp = server_cbs.doUdp = params[i].doUdp;
  59183. client_cbs.method = params[i].client_meth;
  59184. server_cbs.method = params[i].server_meth;
  59185. client_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  59186. server_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  59187. client_cbs.on_result = test_TLS_13_ticket_different_ciphers_on_result;
  59188. server_cbs.ticNoInit = 1;
  59189. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  59190. &server_cbs, NULL), TEST_SUCCESS);
  59191. test_TLS_13_ticket_different_ciphers_run++;
  59192. server_cbs.ctx = test_TLS_13_ticket_different_ciphers_ctx;
  59193. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  59194. &server_cbs, NULL), TEST_SUCCESS);
  59195. wolfSSL_SESSION_free(test_TLS_13_ticket_different_ciphers_session);
  59196. test_TLS_13_ticket_different_ciphers_session = NULL;
  59197. wolfSSL_CTX_free(test_TLS_13_ticket_different_ciphers_ctx);
  59198. test_TLS_13_ticket_different_ciphers_ctx = NULL;
  59199. }
  59200. return EXPECT_RESULT();
  59201. }
  59202. #else
  59203. static int test_TLS_13_ticket_different_ciphers(void)
  59204. {
  59205. return TEST_SKIPPED;
  59206. }
  59207. #endif
  59208. #if defined(WOLFSSL_EXTRA_ALERTS) && !defined(WOLFSSL_NO_TLS12) && \
  59209. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  59210. #define TEST_WRONG_CS_CLIENT "DHE-RSA-AES128-SHA"
  59211. /* AKA TLS_DHE_RSA_WITH_AES_128_CBC_SHA */
  59212. byte test_extra_alerts_wrong_cs_sh[] = {
  59213. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0xef,
  59214. 0x0c, 0x30, 0x98, 0xa2, 0xac, 0xfa, 0x68, 0xe9, 0x3e, 0xaa, 0x5c, 0xcf,
  59215. 0xa7, 0x42, 0x72, 0xaf, 0xa0, 0xe8, 0x39, 0x2b, 0x3e, 0x81, 0xa7, 0x7a,
  59216. 0xa5, 0x62, 0x8a, 0x0e, 0x41, 0xba, 0xda, 0x20, 0x18, 0x9f, 0xe1, 0x8c,
  59217. 0x1d, 0xc0, 0x37, 0x9c, 0xf4, 0x90, 0x5d, 0x8d, 0xa0, 0x79, 0xa7, 0x4b,
  59218. 0xa8, 0x79, 0xdf, 0xcd, 0x8d, 0xf5, 0xb5, 0x50, 0x5f, 0xf1, 0xdb, 0x4d,
  59219. 0xbb, 0x07, 0x54, 0x1c,
  59220. 0x00, 0x02, /* TLS_RSA_WITH_NULL_SHA */
  59221. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  59222. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  59223. };
  59224. static int test_extra_alerts_wrong_cs(void)
  59225. {
  59226. EXPECT_DECLS;
  59227. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  59228. struct test_memio_ctx test_ctx;
  59229. WOLFSSL_CTX *ctx_c = NULL;
  59230. WOLFSSL_ALERT_HISTORY h;
  59231. WOLFSSL *ssl_c = NULL;
  59232. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59233. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  59234. wolfTLSv1_2_client_method, NULL), 0);
  59235. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, TEST_WRONG_CS_CLIENT),
  59236. WOLFSSL_SUCCESS);
  59237. /* CH */
  59238. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59239. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59240. WOLFSSL_ERROR_WANT_READ);
  59241. /* consume CH */
  59242. test_ctx.s_len = 0;
  59243. /* inject SH */
  59244. XMEMCPY(test_ctx.c_buff, test_extra_alerts_wrong_cs_sh,
  59245. sizeof(test_extra_alerts_wrong_cs_sh));
  59246. test_ctx.c_len = sizeof(test_extra_alerts_wrong_cs_sh);
  59247. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59248. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59249. WOLFSSL_ERROR_WANT_READ);
  59250. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  59251. ExpectIntEQ(h.last_tx.code, handshake_failure);
  59252. ExpectIntEQ(h.last_tx.level, alert_fatal);
  59253. wolfSSL_free(ssl_c);
  59254. wolfSSL_CTX_free(ctx_c);
  59255. #endif
  59256. return EXPECT_RESULT();
  59257. }
  59258. #else
  59259. static int test_extra_alerts_wrong_cs(void)
  59260. {
  59261. return TEST_SKIPPED;
  59262. }
  59263. #endif
  59264. #if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_EXTRA_ALERTS) && \
  59265. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_SP_MATH)
  59266. static void test_remove_msg(byte *msg, int tail_len, int *len, int msg_length)
  59267. {
  59268. tail_len -= msg_length;
  59269. XMEMMOVE(msg, msg + msg_length, tail_len);
  59270. *len = *len - msg_length;
  59271. }
  59272. static int test_remove_hs_msg_from_buffer(byte *buf, int *len, byte type,
  59273. byte *found)
  59274. {
  59275. const unsigned int _HANDSHAKE_HEADER_SZ = 4;
  59276. const unsigned int _RECORD_HEADER_SZ = 5;
  59277. const int _change_cipher_hs = 55;
  59278. const int _change_cipher = 20;
  59279. const int _handshake = 22;
  59280. unsigned int tail_len;
  59281. byte *idx, *curr;
  59282. word8 currType;
  59283. word16 rLength;
  59284. word32 hLength;
  59285. idx = buf;
  59286. tail_len = *len;
  59287. *found = 0;
  59288. while (tail_len > _RECORD_HEADER_SZ) {
  59289. curr = idx;
  59290. currType = *idx;
  59291. ato16(idx + 3, &rLength);
  59292. idx += _RECORD_HEADER_SZ;
  59293. tail_len -= _RECORD_HEADER_SZ;
  59294. if (tail_len < rLength)
  59295. return -1;
  59296. if (type == _change_cipher_hs && currType == _change_cipher) {
  59297. if (rLength != 1)
  59298. return -1;
  59299. /* match */
  59300. test_remove_msg(curr, *len - (int)(curr - buf),
  59301. len, _RECORD_HEADER_SZ + 1);
  59302. *found = 1;
  59303. return 0;
  59304. }
  59305. if (currType != _handshake) {
  59306. idx += rLength;
  59307. tail_len -= rLength;
  59308. continue;
  59309. }
  59310. if (rLength < _HANDSHAKE_HEADER_SZ)
  59311. return -1;
  59312. currType = *idx;
  59313. ato24(idx+1, &hLength);
  59314. hLength += _HANDSHAKE_HEADER_SZ;
  59315. if (tail_len < hLength)
  59316. return -1;
  59317. if (currType != type) {
  59318. idx += hLength;
  59319. tail_len -= hLength;
  59320. continue;
  59321. }
  59322. /* match */
  59323. test_remove_msg(curr, *len - (int)(curr - buf), len,
  59324. hLength + _RECORD_HEADER_SZ);
  59325. *found = 1;
  59326. return 0;
  59327. }
  59328. /* not found */
  59329. return 0;
  59330. }
  59331. static int test_remove_hs_message(byte hs_message_type,
  59332. int extra_round, byte alert_type)
  59333. {
  59334. EXPECT_DECLS;
  59335. WOLFSSL_CTX *ctx_c = NULL;
  59336. WOLFSSL_CTX *ctx_s = NULL;
  59337. WOLFSSL *ssl_c = NULL;
  59338. WOLFSSL *ssl_s = NULL;
  59339. struct test_memio_ctx test_ctx;
  59340. WOLFSSL_ALERT_HISTORY h;
  59341. byte found = 0;
  59342. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59343. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59344. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  59345. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59346. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59347. WOLFSSL_ERROR_WANT_READ);
  59348. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  59349. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59350. WOLFSSL_ERROR_WANT_READ);
  59351. if (extra_round) {
  59352. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59353. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59354. WOLFSSL_ERROR_WANT_READ);
  59355. /* this will complete handshake from server side */
  59356. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  59357. }
  59358. ExpectIntEQ(test_remove_hs_msg_from_buffer(test_ctx.c_buff,
  59359. &test_ctx.c_len, hs_message_type, &found), 0);
  59360. if (!found) {
  59361. wolfSSL_free(ssl_c);
  59362. wolfSSL_CTX_free(ctx_c);
  59363. wolfSSL_free(ssl_s);
  59364. wolfSSL_CTX_free(ctx_s);
  59365. return TEST_SKIPPED;
  59366. }
  59367. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59368. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59369. WOLFSSL_ERROR_WANT_READ);
  59370. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  59371. ExpectTrue(alert_type == 0xff || h.last_tx.code == alert_type);
  59372. ExpectIntEQ(h.last_tx.level, alert_fatal);
  59373. wolfSSL_free(ssl_c);
  59374. wolfSSL_CTX_free(ctx_c);
  59375. wolfSSL_free(ssl_s);
  59376. wolfSSL_CTX_free(ctx_s);
  59377. return EXPECT_RESULT();
  59378. }
  59379. static int test_extra_alerts_skip_hs(void)
  59380. {
  59381. EXPECT_DECLS;
  59382. const byte _server_key_exchange = 12;
  59383. const byte _server_hello = 2;
  59384. const byte _certificate = 11;
  59385. /* server_hello */
  59386. ExpectIntNE(test_remove_hs_message(_server_hello, 0,
  59387. unexpected_message), TEST_FAIL);
  59388. ExpectIntNE(test_remove_hs_message(_certificate, 0,
  59389. 0xff), TEST_FAIL);
  59390. ExpectIntNE(test_remove_hs_message(_server_key_exchange, 0,
  59391. unexpected_message), TEST_FAIL);
  59392. return EXPECT_RESULT();
  59393. }
  59394. #else
  59395. static int test_extra_alerts_skip_hs(void)
  59396. {
  59397. return TEST_SKIPPED;
  59398. }
  59399. #endif
  59400. #if !defined(WOLFSSL_NO_TLS12) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  59401. && defined(WOLFSSL_EXTRA_ALERTS) && !defined(NO_PSK) && !defined(NO_DH)
  59402. static unsigned int test_server_psk_cb(WOLFSSL* ssl, const char* id,
  59403. unsigned char* key, unsigned int key_max_len)
  59404. {
  59405. (void)ssl;
  59406. (void)id;
  59407. (void)key_max_len;
  59408. /* zero means error */
  59409. key[0] = 0x10;
  59410. return 1;
  59411. }
  59412. static int test_extra_alerts_bad_psk(void)
  59413. {
  59414. EXPECT_DECLS;
  59415. WOLFSSL_CTX *ctx_c = NULL;
  59416. WOLFSSL_CTX *ctx_s = NULL;
  59417. WOLFSSL *ssl_c = NULL;
  59418. WOLFSSL *ssl_s = NULL;
  59419. struct test_memio_ctx test_ctx;
  59420. WOLFSSL_ALERT_HISTORY h;
  59421. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59422. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59423. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  59424. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, "DHE-PSK-AES128-GCM-SHA256"),
  59425. WOLFSSL_SUCCESS);
  59426. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_s, "DHE-PSK-AES128-GCM-SHA256"),
  59427. WOLFSSL_SUCCESS);
  59428. wolfSSL_set_psk_server_callback(ssl_s, test_server_psk_cb);
  59429. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59430. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59431. WOLFSSL_ERROR_WANT_READ);
  59432. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  59433. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  59434. WOLFSSL_ERROR_WANT_READ);
  59435. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59436. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59437. WOLFSSL_ERROR_WANT_READ);
  59438. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  59439. ExpectIntEQ(h.last_tx.code, handshake_failure);
  59440. ExpectIntEQ(h.last_tx.level, alert_fatal);
  59441. wolfSSL_free(ssl_c);
  59442. wolfSSL_CTX_free(ctx_c);
  59443. wolfSSL_free(ssl_s);
  59444. wolfSSL_CTX_free(ctx_s);
  59445. return EXPECT_RESULT();
  59446. }
  59447. #else
  59448. static int test_extra_alerts_bad_psk(void)
  59449. {
  59450. return TEST_SKIPPED;
  59451. }
  59452. #endif
  59453. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  59454. && !defined(NO_PSK)
  59455. static unsigned int test_tls13_bad_psk_binder_client_cb(WOLFSSL* ssl,
  59456. const char* hint, char* identity, unsigned int id_max_len,
  59457. unsigned char* key, unsigned int key_max_len)
  59458. {
  59459. (void)ssl;
  59460. (void)hint;
  59461. (void)key_max_len;
  59462. /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */
  59463. XSTRNCPY(identity, "Client_identity", id_max_len);
  59464. key[0] = 0x20;
  59465. return 1;
  59466. }
  59467. static unsigned int test_tls13_bad_psk_binder_server_cb(WOLFSSL* ssl,
  59468. const char* id, unsigned char* key, unsigned int key_max_len)
  59469. {
  59470. (void)ssl;
  59471. (void)id;
  59472. (void)key_max_len;
  59473. /* zero means error */
  59474. key[0] = 0x10;
  59475. return 1;
  59476. }
  59477. #endif
  59478. static int test_tls13_bad_psk_binder(void)
  59479. {
  59480. EXPECT_DECLS;
  59481. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  59482. && !defined(NO_PSK)
  59483. WOLFSSL_CTX *ctx_c = NULL;
  59484. WOLFSSL_CTX *ctx_s = NULL;
  59485. WOLFSSL *ssl_c = NULL;
  59486. WOLFSSL *ssl_s = NULL;
  59487. struct test_memio_ctx test_ctx;
  59488. WOLFSSL_ALERT_HISTORY h;
  59489. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59490. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59491. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  59492. wolfSSL_set_psk_client_callback(ssl_c, test_tls13_bad_psk_binder_client_cb);
  59493. wolfSSL_set_psk_server_callback(ssl_s, test_tls13_bad_psk_binder_server_cb);
  59494. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59495. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59496. WOLFSSL_ERROR_WANT_READ);
  59497. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  59498. ExpectIntEQ( wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  59499. BAD_BINDER);
  59500. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59501. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59502. FATAL_ERROR);
  59503. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  59504. ExpectIntEQ(h.last_rx.code, illegal_parameter);
  59505. ExpectIntEQ(h.last_rx.level, alert_fatal);
  59506. wolfSSL_free(ssl_c);
  59507. wolfSSL_CTX_free(ctx_c);
  59508. wolfSSL_free(ssl_s);
  59509. wolfSSL_CTX_free(ctx_s);
  59510. #endif
  59511. return EXPECT_RESULT();
  59512. }
  59513. #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  59514. defined(HAVE_IO_TESTS_DEPENDENCIES)
  59515. static int test_harden_no_secure_renegotiation_io_cb(WOLFSSL *ssl, char *buf,
  59516. int sz, void *ctx)
  59517. {
  59518. static int sentServerHello = FALSE;
  59519. if (!sentServerHello) {
  59520. byte renegExt[] = { 0xFF, 0x01, 0x00, 0x01, 0x00 };
  59521. size_t i;
  59522. if (sz < (int)sizeof(renegExt))
  59523. return WOLFSSL_CBIO_ERR_GENERAL;
  59524. /* Remove SCR from ServerHello */
  59525. for (i = 0; i < sz - sizeof(renegExt); i++) {
  59526. if (XMEMCMP(buf + i, renegExt, sizeof(renegExt)) == 0) {
  59527. /* Found the extension. Change it to something unrecognized. */
  59528. buf[i+1] = 0x11;
  59529. break;
  59530. }
  59531. }
  59532. sentServerHello = TRUE;
  59533. }
  59534. return EmbedSend(ssl, buf, sz, ctx);
  59535. }
  59536. static void test_harden_no_secure_renegotiation_ssl_ready(WOLFSSL* ssl)
  59537. {
  59538. wolfSSL_SSLSetIOSend(ssl, test_harden_no_secure_renegotiation_io_cb);
  59539. }
  59540. static void test_harden_no_secure_renegotiation_on_cleanup(WOLFSSL* ssl)
  59541. {
  59542. WOLFSSL_ALERT_HISTORY h;
  59543. AssertIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  59544. AssertIntEQ(h.last_rx.code, handshake_failure);
  59545. AssertIntEQ(h.last_rx.level, alert_fatal);
  59546. }
  59547. static int test_harden_no_secure_renegotiation(void)
  59548. {
  59549. EXPECT_DECLS;
  59550. callback_functions client_cbs, server_cbs;
  59551. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59552. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59553. client_cbs.method = wolfTLSv1_2_client_method;
  59554. server_cbs.method = wolfTLSv1_2_server_method;
  59555. server_cbs.ssl_ready = test_harden_no_secure_renegotiation_ssl_ready;
  59556. server_cbs.on_cleanup = test_harden_no_secure_renegotiation_on_cleanup;
  59557. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  59558. ExpectIntEQ(client_cbs.return_code, TEST_FAIL);
  59559. ExpectIntEQ(client_cbs.last_err, SECURE_RENEGOTIATION_E);
  59560. ExpectIntEQ(server_cbs.return_code, TEST_FAIL);
  59561. ExpectTrue(server_cbs.last_err == SOCKET_ERROR_E ||
  59562. server_cbs.last_err == FATAL_ERROR);
  59563. return EXPECT_RESULT();
  59564. }
  59565. #else
  59566. static int test_harden_no_secure_renegotiation(void)
  59567. {
  59568. return TEST_SKIPPED;
  59569. }
  59570. #endif
  59571. #if defined(HAVE_OCSP) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  59572. static int test_override_alt_cert_chain_cert_cb(int preverify,
  59573. WOLFSSL_X509_STORE_CTX* store)
  59574. {
  59575. fprintf(stderr, "preverify: %d\n", preverify);
  59576. fprintf(stderr, "store->error: %d\n", store->error);
  59577. fprintf(stderr, "error reason: %s\n", wolfSSL_ERR_reason_error_string(store->error));
  59578. if (store->error == OCSP_INVALID_STATUS) {
  59579. fprintf(stderr, "Overriding OCSP error\n");
  59580. return 1;
  59581. }
  59582. #ifndef WOLFSSL_ALT_CERT_CHAINS
  59583. else if ((store->error == ASN_NO_SIGNER_E ||
  59584. store->error == ASN_SELF_SIGNED_E
  59585. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  59586. defined(HAVE_WEBSERVER)
  59587. || store->error == WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
  59588. #endif
  59589. ) && store->error_depth == store->totalCerts - 1) {
  59590. fprintf(stderr, "Overriding no signer error only for root cert\n");
  59591. return 1;
  59592. }
  59593. #endif
  59594. else
  59595. return preverify;
  59596. }
  59597. static int test_override_alt_cert_chain_ocsp_cb(void* ioCtx, const char* url,
  59598. int urlSz, unsigned char* request, int requestSz,
  59599. unsigned char** response)
  59600. {
  59601. (void)ioCtx;
  59602. (void)url;
  59603. (void)urlSz;
  59604. (void)request;
  59605. (void)requestSz;
  59606. (void)response;
  59607. return -1;
  59608. }
  59609. static int test_override_alt_cert_chain_client_ctx_ready(WOLFSSL_CTX* ctx)
  59610. {
  59611. EXPECT_DECLS;
  59612. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER,
  59613. test_override_alt_cert_chain_cert_cb);
  59614. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  59615. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  59616. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  59617. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  59618. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  59619. WOLFSSL_SUCCESS);
  59620. return EXPECT_RESULT();
  59621. }
  59622. static int test_override_alt_cert_chain_client_ctx_ready2(WOLFSSL_CTX* ctx)
  59623. {
  59624. EXPECT_DECLS;
  59625. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  59626. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  59627. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  59628. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  59629. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  59630. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  59631. WOLFSSL_SUCCESS);
  59632. return EXPECT_RESULT();
  59633. }
  59634. static int test_override_alt_cert_chain_server_ctx_ready(WOLFSSL_CTX* ctx)
  59635. {
  59636. EXPECT_DECLS;
  59637. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  59638. "./certs/intermediate/server-chain-alt.pem"), WOLFSSL_SUCCESS);
  59639. return EXPECT_RESULT();
  59640. }
  59641. static int test_override_alt_cert_chain(void)
  59642. {
  59643. EXPECT_DECLS;
  59644. size_t i;
  59645. struct test_params {
  59646. ctx_cb client_ctx_cb;
  59647. ctx_cb server_ctx_cb;
  59648. int result;
  59649. } params[] = {
  59650. {test_override_alt_cert_chain_client_ctx_ready,
  59651. test_override_alt_cert_chain_server_ctx_ready, TEST_SUCCESS},
  59652. {test_override_alt_cert_chain_client_ctx_ready2,
  59653. test_override_alt_cert_chain_server_ctx_ready, TEST_FAIL},
  59654. };
  59655. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  59656. test_ssl_cbf client_cbs, server_cbs;
  59657. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59658. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59659. fprintf(stderr, "test config: %d\n", (int)i);
  59660. client_cbs.ctx_ready = params[i].client_ctx_cb;
  59661. server_cbs.ctx_ready = params[i].server_ctx_cb;
  59662. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  59663. &server_cbs, NULL), params[i].result);
  59664. ExpectIntEQ(client_cbs.return_code, params[i].result);
  59665. ExpectIntEQ(server_cbs.return_code, params[i].result);
  59666. }
  59667. return EXPECT_RESULT();
  59668. }
  59669. #else
  59670. static int test_override_alt_cert_chain(void)
  59671. {
  59672. return TEST_SKIPPED;
  59673. }
  59674. #endif
  59675. #if defined(HAVE_RPK)
  59676. #define svrRpkCertFile "./certs/rpk/server-cert-rpk.der"
  59677. #define clntRpkCertFile "./certs/rpk/client-cert-rpk.der"
  59678. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  59679. static int MyRpkVerifyCb(int mode, WOLFSSL_X509_STORE_CTX* strctx)
  59680. {
  59681. int ret = WOLFSSL_SUCCESS;
  59682. (void)mode;
  59683. (void)strctx;
  59684. WOLFSSL_ENTER("MyRpkVerifyCb");
  59685. return ret;
  59686. }
  59687. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  59688. static WC_INLINE int test_rpk_memio_setup(
  59689. struct test_memio_ctx *ctx,
  59690. WOLFSSL_CTX **ctx_c,
  59691. WOLFSSL_CTX **ctx_s,
  59692. WOLFSSL **ssl_c,
  59693. WOLFSSL **ssl_s,
  59694. method_provider method_c,
  59695. method_provider method_s,
  59696. const char* certfile_c, int fmt_cc, /* client cert file path and format */
  59697. const char* certfile_s, int fmt_cs, /* server cert file path and format */
  59698. const char* pkey_c, int fmt_kc, /* client private key and format */
  59699. const char* pkey_s, int fmt_ks /* server private key and format */
  59700. )
  59701. {
  59702. int ret;
  59703. if (ctx_c != NULL && *ctx_c == NULL) {
  59704. *ctx_c = wolfSSL_CTX_new(method_c());
  59705. if (*ctx_c == NULL) {
  59706. return -1;
  59707. }
  59708. wolfSSL_CTX_set_verify(*ctx_c, WOLFSSL_VERIFY_PEER, NULL);
  59709. ret = wolfSSL_CTX_load_verify_locations(*ctx_c, caCertFile, 0);
  59710. if (ret != WOLFSSL_SUCCESS) {
  59711. return -1;
  59712. }
  59713. wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
  59714. wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
  59715. ret = wolfSSL_CTX_use_certificate_file(*ctx_c, certfile_c, fmt_cc);
  59716. if (ret != WOLFSSL_SUCCESS) {
  59717. return -1;
  59718. }
  59719. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_c, pkey_c, fmt_kc);
  59720. if (ret != WOLFSSL_SUCCESS) {
  59721. return -1;
  59722. }
  59723. }
  59724. if (ctx_s != NULL && *ctx_s == NULL) {
  59725. *ctx_s = wolfSSL_CTX_new(method_s());
  59726. if (*ctx_s == NULL) {
  59727. return -1;
  59728. }
  59729. wolfSSL_CTX_set_verify(*ctx_s, WOLFSSL_VERIFY_PEER, NULL);
  59730. ret = wolfSSL_CTX_load_verify_locations(*ctx_s, cliCertFile, 0);
  59731. if (ret != WOLFSSL_SUCCESS) {
  59732. return -1;
  59733. }
  59734. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, pkey_s, fmt_ks);
  59735. if (ret != WOLFSSL_SUCCESS) {
  59736. return -1;
  59737. }
  59738. ret = wolfSSL_CTX_use_certificate_file(*ctx_s, certfile_s, fmt_cs);
  59739. if (ret != WOLFSSL_SUCCESS) {
  59740. return -1;
  59741. }
  59742. wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
  59743. wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
  59744. if (ctx->s_ciphers != NULL) {
  59745. ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
  59746. if (ret != WOLFSSL_SUCCESS) {
  59747. return -1;
  59748. }
  59749. }
  59750. }
  59751. if (ctx_c != NULL && ssl_c != NULL) {
  59752. *ssl_c = wolfSSL_new(*ctx_c);
  59753. if (*ssl_c == NULL) {
  59754. return -1;
  59755. }
  59756. wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
  59757. wolfSSL_SetIOReadCtx(*ssl_c, ctx);
  59758. }
  59759. if (ctx_s != NULL && ssl_s != NULL) {
  59760. *ssl_s = wolfSSL_new(*ctx_s);
  59761. if (*ssl_s == NULL) {
  59762. return -1;
  59763. }
  59764. wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
  59765. wolfSSL_SetIOReadCtx(*ssl_s, ctx);
  59766. #if !defined(NO_DH)
  59767. SetDH(*ssl_s);
  59768. #endif
  59769. }
  59770. return 0;
  59771. }
  59772. #endif /* HAVE_RPK */
  59773. static int test_rpk_set_xxx_cert_type(void)
  59774. {
  59775. EXPECT_DECLS;
  59776. #if defined(HAVE_RPK)
  59777. char ctype[MAX_CLIENT_CERT_TYPE_CNT + 1]; /* prepare bigger buffer */
  59778. WOLFSSL_CTX* ctx = NULL;
  59779. WOLFSSL* ssl = NULL;
  59780. int tp;
  59781. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  59782. ExpectNotNull(ctx);
  59783. ssl = wolfSSL_new(ctx);
  59784. ExpectNotNull(ssl);
  59785. /*--------------------------------------------*/
  59786. /* tests for wolfSSL_CTX_set_client_cert_type */
  59787. /*--------------------------------------------*/
  59788. /* illegal parameter test caces */
  59789. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(NULL, ctype,
  59790. MAX_CLIENT_CERT_TYPE_CNT),
  59791. BAD_FUNC_ARG);
  59792. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59793. sizeof(ctype)),
  59794. BAD_FUNC_ARG);
  59795. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  59796. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  59797. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59798. MAX_CLIENT_CERT_TYPE_CNT),
  59799. BAD_FUNC_ARG);
  59800. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  59801. ctype[1] = 10; /* set unknown cert type */
  59802. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59803. MAX_CLIENT_CERT_TYPE_CNT),
  59804. BAD_FUNC_ARG);
  59805. /* pass larger type count */
  59806. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  59807. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  59808. ctype[2] = 1; /* pass unacceptable type count */
  59809. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59810. MAX_CLIENT_CERT_TYPE_CNT + 1),
  59811. BAD_FUNC_ARG);
  59812. /* should accept NULL for type buffer */
  59813. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, NULL,
  59814. MAX_CLIENT_CERT_TYPE_CNT),
  59815. WOLFSSL_SUCCESS);
  59816. /* should accept zero for type count */
  59817. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59818. 0),
  59819. WOLFSSL_SUCCESS);
  59820. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59821. MAX_CLIENT_CERT_TYPE_CNT),
  59822. WOLFSSL_SUCCESS);
  59823. /*--------------------------------------------*/
  59824. /* tests for wolfSSL_CTX_set_server_cert_type */
  59825. /*--------------------------------------------*/
  59826. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(NULL, ctype,
  59827. MAX_SERVER_CERT_TYPE_CNT),
  59828. BAD_FUNC_ARG);
  59829. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59830. sizeof(ctype)),
  59831. BAD_FUNC_ARG);
  59832. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  59833. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  59834. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59835. MAX_SERVER_CERT_TYPE_CNT),
  59836. BAD_FUNC_ARG);
  59837. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  59838. ctype[1] = 10; /* set unknown cert type */
  59839. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59840. MAX_SERVER_CERT_TYPE_CNT),
  59841. BAD_FUNC_ARG);
  59842. /* pass larger type count */
  59843. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  59844. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  59845. ctype[2] = 1; /* pass unacceptable type count */
  59846. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59847. MAX_SERVER_CERT_TYPE_CNT + 1),
  59848. BAD_FUNC_ARG);
  59849. /* should accept NULL for type buffer */
  59850. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, NULL,
  59851. MAX_SERVER_CERT_TYPE_CNT),
  59852. WOLFSSL_SUCCESS);
  59853. /* should accept zero for type count */
  59854. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59855. 0),
  59856. WOLFSSL_SUCCESS);
  59857. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59858. MAX_CLIENT_CERT_TYPE_CNT),
  59859. WOLFSSL_SUCCESS);
  59860. /*--------------------------------------------*/
  59861. /* tests for wolfSSL_set_client_cert_type */
  59862. /*--------------------------------------------*/
  59863. ExpectIntEQ(wolfSSL_set_client_cert_type(NULL, ctype,
  59864. MAX_CLIENT_CERT_TYPE_CNT),
  59865. BAD_FUNC_ARG);
  59866. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59867. sizeof(ctype)),
  59868. BAD_FUNC_ARG);
  59869. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  59870. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  59871. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59872. MAX_CLIENT_CERT_TYPE_CNT),
  59873. BAD_FUNC_ARG);
  59874. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  59875. ctype[1] = 10; /* set unknown cert type */
  59876. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59877. MAX_CLIENT_CERT_TYPE_CNT),
  59878. BAD_FUNC_ARG);
  59879. /* pass larger type count */
  59880. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  59881. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  59882. ctype[2] = 1; /* pass unacceptable type count */
  59883. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59884. MAX_CLIENT_CERT_TYPE_CNT + 1),
  59885. BAD_FUNC_ARG);
  59886. /* should accept NULL for type buffer */
  59887. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, NULL,
  59888. MAX_CLIENT_CERT_TYPE_CNT),
  59889. WOLFSSL_SUCCESS);
  59890. /* should accept zero for type count */
  59891. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59892. 0),
  59893. WOLFSSL_SUCCESS);
  59894. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59895. MAX_CLIENT_CERT_TYPE_CNT),
  59896. WOLFSSL_SUCCESS);
  59897. /*--------------------------------------------*/
  59898. /* tests for wolfSSL_CTX_set_server_cert_type */
  59899. /*--------------------------------------------*/
  59900. ExpectIntEQ(wolfSSL_set_server_cert_type(NULL, ctype,
  59901. MAX_SERVER_CERT_TYPE_CNT),
  59902. BAD_FUNC_ARG);
  59903. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59904. sizeof(ctype)),
  59905. BAD_FUNC_ARG);
  59906. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  59907. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  59908. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59909. MAX_SERVER_CERT_TYPE_CNT),
  59910. BAD_FUNC_ARG);
  59911. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  59912. ctype[1] = 10; /* set unknown cert type */
  59913. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59914. MAX_SERVER_CERT_TYPE_CNT),
  59915. BAD_FUNC_ARG);
  59916. /* pass larger type count */
  59917. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  59918. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  59919. ctype[2] = 1; /* pass unacceptable type count */
  59920. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59921. MAX_SERVER_CERT_TYPE_CNT + 1),
  59922. BAD_FUNC_ARG);
  59923. /* should accept NULL for type buffer */
  59924. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, NULL,
  59925. MAX_SERVER_CERT_TYPE_CNT),
  59926. WOLFSSL_SUCCESS);
  59927. /* should accept zero for type count */
  59928. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59929. 0),
  59930. WOLFSSL_SUCCESS);
  59931. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59932. MAX_SERVER_CERT_TYPE_CNT),
  59933. WOLFSSL_SUCCESS);
  59934. /*------------------------------------------------*/
  59935. /* tests for wolfSSL_get_negotiated_xxx_cert_type */
  59936. /*------------------------------------------------*/
  59937. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(NULL, &tp),
  59938. BAD_FUNC_ARG);
  59939. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl, NULL),
  59940. BAD_FUNC_ARG);
  59941. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(NULL, &tp),
  59942. BAD_FUNC_ARG);
  59943. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl, NULL),
  59944. BAD_FUNC_ARG);
  59945. /* clean up */
  59946. wolfSSL_free(ssl);
  59947. wolfSSL_CTX_free(ctx);
  59948. #endif
  59949. return EXPECT_RESULT();
  59950. }
  59951. static int test_tls13_rpk_handshake(void)
  59952. {
  59953. EXPECT_DECLS;
  59954. #if defined(HAVE_RPK)
  59955. int ret = 0;
  59956. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  59957. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  59958. struct test_memio_ctx test_ctx;
  59959. int err;
  59960. char certType_c[MAX_CLIENT_CERT_TYPE_CNT];
  59961. char certType_s[MAX_CLIENT_CERT_TYPE_CNT];
  59962. int typeCnt_c;
  59963. int typeCnt_s;
  59964. int tp;
  59965. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  59966. int isServer;
  59967. #endif
  59968. (void)err;
  59969. (void)typeCnt_c;
  59970. (void)typeCnt_s;
  59971. (void)certType_c;
  59972. (void)certType_s;
  59973. /* TLS1.2
  59974. * Both client and server load x509 cert and start handshaking.
  59975. * Check no negotiation occurred.
  59976. */
  59977. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59978. ExpectIntEQ(
  59979. test_rpk_memio_setup(
  59980. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59981. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  59982. cliCertFile, WOLFSSL_FILETYPE_PEM,
  59983. svrCertFile, WOLFSSL_FILETYPE_PEM,
  59984. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59985. svrKeyFile, WOLFSSL_FILETYPE_PEM)
  59986. , 0);
  59987. /* set client certificate type in client end */
  59988. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59989. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  59990. typeCnt_c = 2;
  59991. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  59992. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  59993. typeCnt_s = 2;
  59994. /* both client and server do not call client/server_cert_type APIs,
  59995. * expecting default settings works and no negotiation performed.
  59996. */
  59997. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  59998. return TEST_FAIL;
  59999. /* confirm no negotiation occurred */
  60000. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60001. WOLFSSL_SUCCESS);
  60002. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60003. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60004. WOLFSSL_SUCCESS);
  60005. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60006. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60007. WOLFSSL_SUCCESS);
  60008. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60009. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60010. WOLFSSL_SUCCESS);
  60011. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60012. (void)typeCnt_c;
  60013. (void)typeCnt_s;
  60014. wolfSSL_free(ssl_c);
  60015. wolfSSL_CTX_free(ctx_c);
  60016. wolfSSL_free(ssl_s);
  60017. wolfSSL_CTX_free(ctx_s);
  60018. ssl_c = ssl_s = NULL;
  60019. ctx_c = ctx_s = NULL;
  60020. /* Both client and server load x509 cert and start handshaking.
  60021. * Check no negotiation occurred.
  60022. */
  60023. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60024. ExpectIntEQ(
  60025. test_rpk_memio_setup(
  60026. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60027. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60028. cliCertFile, WOLFSSL_FILETYPE_PEM,
  60029. svrCertFile, WOLFSSL_FILETYPE_PEM,
  60030. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60031. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60032. , 0);
  60033. /* set client certificate type in client end */
  60034. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60035. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60036. typeCnt_c = 2;
  60037. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  60038. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  60039. typeCnt_s = 2;
  60040. /* both client and server do not call client/server_cert_type APIs,
  60041. * expecting default settings works and no negotiation performed.
  60042. */
  60043. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  60044. return TEST_FAIL;
  60045. /* confirm no negotiation occurred */
  60046. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60047. WOLFSSL_SUCCESS);
  60048. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60049. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60050. WOLFSSL_SUCCESS);
  60051. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60052. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60053. WOLFSSL_SUCCESS);
  60054. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60055. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60056. WOLFSSL_SUCCESS);
  60057. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60058. (void)typeCnt_c;
  60059. (void)typeCnt_s;
  60060. wolfSSL_free(ssl_c);
  60061. wolfSSL_CTX_free(ctx_c);
  60062. wolfSSL_free(ssl_s);
  60063. wolfSSL_CTX_free(ctx_s);
  60064. ssl_c = ssl_s = NULL;
  60065. ctx_c = ctx_s = NULL;
  60066. /* Both client and server load RPK cert and start handshaking.
  60067. * Confirm negotiated cert types match as expected.
  60068. */
  60069. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60070. ExpectIntEQ(
  60071. test_rpk_memio_setup(
  60072. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60073. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60074. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60075. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60076. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60077. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60078. , 0);
  60079. /* set client certificate type in client end */
  60080. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60081. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60082. typeCnt_c = 2;
  60083. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  60084. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  60085. typeCnt_s = 2;
  60086. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60087. WOLFSSL_SUCCESS);
  60088. /* set server certificate type in client end */
  60089. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60090. WOLFSSL_SUCCESS);
  60091. /* set client certificate type in server end */
  60092. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60093. WOLFSSL_SUCCESS);
  60094. /* set server certificate type in server end */
  60095. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60096. WOLFSSL_SUCCESS);
  60097. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  60098. return TEST_FAIL;
  60099. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60100. WOLFSSL_SUCCESS);
  60101. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60102. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60103. WOLFSSL_SUCCESS);
  60104. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60105. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60106. WOLFSSL_SUCCESS);
  60107. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60108. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60109. WOLFSSL_SUCCESS);
  60110. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60111. wolfSSL_free(ssl_c);
  60112. wolfSSL_CTX_free(ctx_c);
  60113. wolfSSL_free(ssl_s);
  60114. wolfSSL_CTX_free(ctx_s);
  60115. ssl_c = ssl_s = NULL;
  60116. ctx_c = ctx_s = NULL;
  60117. /* TLS1.2
  60118. * Both client and server load RPK cert and start handshaking.
  60119. * Confirm negotiated cert types match as expected.
  60120. */
  60121. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60122. ExpectIntEQ(
  60123. test_rpk_memio_setup(
  60124. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60125. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  60126. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60127. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60128. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60129. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60130. , 0);
  60131. /* set client certificate type in client end */
  60132. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60133. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60134. typeCnt_c = 2;
  60135. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  60136. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  60137. typeCnt_s = 2;
  60138. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60139. WOLFSSL_SUCCESS);
  60140. /* set server certificate type in client end */
  60141. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60142. WOLFSSL_SUCCESS);
  60143. /* set client certificate type in server end */
  60144. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60145. WOLFSSL_SUCCESS);
  60146. /* set server certificate type in server end */
  60147. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60148. WOLFSSL_SUCCESS);
  60149. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  60150. return TEST_FAIL;
  60151. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60152. WOLFSSL_SUCCESS);
  60153. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60154. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60155. WOLFSSL_SUCCESS);
  60156. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60157. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60158. WOLFSSL_SUCCESS);
  60159. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60160. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60161. WOLFSSL_SUCCESS);
  60162. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60163. wolfSSL_free(ssl_c);
  60164. wolfSSL_CTX_free(ctx_c);
  60165. wolfSSL_free(ssl_s);
  60166. wolfSSL_CTX_free(ctx_s);
  60167. ssl_c = ssl_s = NULL;
  60168. ctx_c = ctx_s = NULL;
  60169. /* Both client and server load x509 cert.
  60170. * Have client call set_client_cert_type with both RPK and x509.
  60171. * This doesn't makes client add client cert type extension to ClientHello,
  60172. * since it does not load RPK cert actually.
  60173. */
  60174. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60175. ExpectIntEQ(
  60176. test_rpk_memio_setup(
  60177. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60178. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60179. cliCertFile, WOLFSSL_FILETYPE_PEM,
  60180. svrCertFile, WOLFSSL_FILETYPE_PEM,
  60181. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60182. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60183. , 0);
  60184. /* set client certificate type in client end
  60185. *
  60186. * client indicates both RPK and x509 certs are available but loaded RPK
  60187. * cert only. It does not have client add client-cert-type extension in CH.
  60188. */
  60189. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60190. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60191. typeCnt_c = 2;
  60192. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60193. WOLFSSL_SUCCESS);
  60194. /* client indicates both RPK and x509 certs are acceptable */
  60195. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  60196. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  60197. typeCnt_s = 2;
  60198. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60199. WOLFSSL_SUCCESS);
  60200. /* server indicates both RPK and x509 certs are acceptable */
  60201. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60202. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60203. typeCnt_c = 2;
  60204. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60205. WOLFSSL_SUCCESS);
  60206. /* server should indicate only RPK cert is available */
  60207. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60208. certType_s[1] = -1;
  60209. typeCnt_s = 1;
  60210. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60211. WOLFSSL_SUCCESS);
  60212. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  60213. return TEST_FAIL;
  60214. /* Negotiation for client-cert-type should NOT happen. Therefore -1 should
  60215. * be returned as cert type.
  60216. */
  60217. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60218. WOLFSSL_SUCCESS);
  60219. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60220. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60221. WOLFSSL_SUCCESS);
  60222. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60223. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60224. WOLFSSL_SUCCESS);
  60225. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60226. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60227. WOLFSSL_SUCCESS);
  60228. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60229. wolfSSL_free(ssl_c);
  60230. wolfSSL_CTX_free(ctx_c);
  60231. wolfSSL_free(ssl_s);
  60232. wolfSSL_CTX_free(ctx_s);
  60233. ssl_c = ssl_s = NULL;
  60234. ctx_c = ctx_s = NULL;
  60235. /* Have client load RPK cert and have server load x509 cert.
  60236. * Check the negotiation result from both ends.
  60237. */
  60238. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60239. ExpectIntEQ(
  60240. test_rpk_memio_setup(
  60241. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60242. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60243. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60244. svrCertFile, WOLFSSL_FILETYPE_PEM,
  60245. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60246. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60247. , 0);
  60248. /* have client tell to use RPK cert */
  60249. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60250. certType_c[1] = -1;
  60251. typeCnt_c = 1;
  60252. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60253. WOLFSSL_SUCCESS);
  60254. /* have client tell to accept both RPK and x509 cert */
  60255. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60256. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  60257. typeCnt_s = 2;
  60258. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60259. WOLFSSL_SUCCESS);
  60260. /* have server accept to both RPK and x509 cert */
  60261. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  60262. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  60263. typeCnt_c = 2;
  60264. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60265. WOLFSSL_SUCCESS);
  60266. /* does not call wolfSSL_set_server_cert_type intentionally in sesrver
  60267. * end, expecting the default setting works.
  60268. */
  60269. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  60270. return TEST_FAIL;
  60271. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60272. WOLFSSL_SUCCESS);
  60273. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60274. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60275. WOLFSSL_SUCCESS);
  60276. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60277. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60278. WOLFSSL_SUCCESS);
  60279. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60280. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60281. WOLFSSL_SUCCESS);
  60282. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60283. wolfSSL_free(ssl_c);
  60284. wolfSSL_CTX_free(ctx_c);
  60285. wolfSSL_free(ssl_s);
  60286. wolfSSL_CTX_free(ctx_s);
  60287. ssl_c = ssl_s = NULL;
  60288. ctx_c = ctx_s = NULL;
  60289. /* Have both client and server load RPK cert, however, have server
  60290. * indicate its cert type x509.
  60291. * Client is expected to detect the cert type mismatch then to send alert
  60292. * with "unsupported_certificate".
  60293. */
  60294. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60295. ExpectIntEQ(
  60296. test_rpk_memio_setup(
  60297. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60298. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60299. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60300. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1, /* server sends RPK cert */
  60301. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60302. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60303. , 0);
  60304. /* have client tell to use RPK cert */
  60305. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60306. certType_c[1] = -1;
  60307. typeCnt_c = 1;
  60308. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60309. WOLFSSL_SUCCESS);
  60310. /* have client tell to accept both RPK and x509 cert */
  60311. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60312. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  60313. typeCnt_s = 2;
  60314. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60315. WOLFSSL_SUCCESS);
  60316. /* have server accept to both RPK and x509 cert */
  60317. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  60318. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  60319. typeCnt_c = 2;
  60320. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60321. WOLFSSL_SUCCESS);
  60322. /* have server tell to use x509 cert intentionally. This will bring
  60323. * certificate type mismatch in client side.
  60324. */
  60325. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60326. certType_s[1] = -1;
  60327. typeCnt_s = 1;
  60328. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60329. WOLFSSL_SUCCESS);
  60330. /* expect client detect cert type mismatch then send Alert */
  60331. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  60332. if (ret != -1)
  60333. return TEST_FAIL;
  60334. ExpectIntEQ(wolfSSL_get_error(ssl_c, ret), UNSUPPORTED_CERTIFICATE);
  60335. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60336. WOLFSSL_SUCCESS);
  60337. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60338. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60339. WOLFSSL_SUCCESS);
  60340. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60341. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60342. WOLFSSL_SUCCESS);
  60343. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60344. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60345. WOLFSSL_SUCCESS);
  60346. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60347. wolfSSL_free(ssl_c);
  60348. wolfSSL_CTX_free(ctx_c);
  60349. wolfSSL_free(ssl_s);
  60350. wolfSSL_CTX_free(ctx_s);
  60351. ssl_c = ssl_s = NULL;
  60352. ctx_c = ctx_s = NULL;
  60353. /* Have client load x509 cert and server load RPK cert,
  60354. * however, have client indicate its cert type RPK.
  60355. * Server is expected to detect the cert type mismatch then to send alert
  60356. * with "unsupported_certificate".
  60357. */
  60358. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60359. ExpectIntEQ(
  60360. test_rpk_memio_setup(
  60361. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60362. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60363. cliCertFile, WOLFSSL_FILETYPE_PEM,
  60364. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60365. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60366. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60367. , 0);
  60368. /* have client tell to use RPK cert intentionally */
  60369. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60370. certType_c[1] = -1;
  60371. typeCnt_c = 1;
  60372. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60373. WOLFSSL_SUCCESS);
  60374. /* have client tell to accept both RPK and x509 cert */
  60375. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60376. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  60377. typeCnt_s = 2;
  60378. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60379. WOLFSSL_SUCCESS);
  60380. /* have server accept to both RPK and x509 cert */
  60381. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  60382. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  60383. typeCnt_c = 2;
  60384. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60385. WOLFSSL_SUCCESS);
  60386. /* have server tell to use x509 cert intentionally. This will bring
  60387. * certificate type mismatch in client side.
  60388. */
  60389. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60390. certType_s[1] = -1;
  60391. typeCnt_s = 1;
  60392. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60393. WOLFSSL_SUCCESS);
  60394. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  60395. /* expect server detect cert type mismatch then send Alert */
  60396. ExpectIntNE(ret, 0);
  60397. err = wolfSSL_get_error(ssl_c, ret);
  60398. ExpectIntEQ(err, UNSUPPORTED_CERTIFICATE);
  60399. /* client did not load RPK cert actually, so negotiation did not happen */
  60400. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60401. WOLFSSL_SUCCESS);
  60402. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60403. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60404. WOLFSSL_SUCCESS);
  60405. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60406. /* client did not load RPK cert actually, so negotiation did not happen */
  60407. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60408. WOLFSSL_SUCCESS);
  60409. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60410. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60411. WOLFSSL_SUCCESS);
  60412. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60413. wolfSSL_free(ssl_c);
  60414. wolfSSL_CTX_free(ctx_c);
  60415. wolfSSL_free(ssl_s);
  60416. wolfSSL_CTX_free(ctx_s);
  60417. ssl_c = ssl_s = NULL;
  60418. ctx_c = ctx_s = NULL;
  60419. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  60420. /* Both client and server load RPK cert and set certificate verify
  60421. * callbacks then start handshaking.
  60422. * Confirm both side can refer the peer's cert.
  60423. */
  60424. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60425. ExpectIntEQ(
  60426. test_rpk_memio_setup(
  60427. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60428. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60429. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60430. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60431. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60432. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60433. , 0);
  60434. /* set client certificate type in client end */
  60435. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60436. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60437. typeCnt_c = 2;
  60438. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  60439. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  60440. typeCnt_s = 2;
  60441. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60442. WOLFSSL_SUCCESS);
  60443. /* set server certificate type in client end */
  60444. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60445. WOLFSSL_SUCCESS);
  60446. /* set client certificate type in server end */
  60447. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60448. WOLFSSL_SUCCESS);
  60449. /* set server certificate type in server end */
  60450. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60451. WOLFSSL_SUCCESS);
  60452. /* set certificate verify callback to both client and server */
  60453. isServer = 0;
  60454. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  60455. wolfSSL_set_verify(ssl_c, SSL_VERIFY_PEER, MyRpkVerifyCb);
  60456. isServer = 1;
  60457. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  60458. wolfSSL_set_verify(ssl_s, SSL_VERIFY_PEER, MyRpkVerifyCb);
  60459. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  60460. if (ret != 0)
  60461. return TEST_FAIL;
  60462. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60463. WOLFSSL_SUCCESS);
  60464. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60465. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60466. WOLFSSL_SUCCESS);
  60467. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60468. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60469. WOLFSSL_SUCCESS);
  60470. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60471. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60472. WOLFSSL_SUCCESS);
  60473. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60474. wolfSSL_free(ssl_c);
  60475. wolfSSL_CTX_free(ctx_c);
  60476. wolfSSL_free(ssl_s);
  60477. wolfSSL_CTX_free(ctx_s);
  60478. ssl_c = ssl_s = NULL;
  60479. ctx_c = ctx_s = NULL;
  60480. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  60481. #endif /* HAVE_RPK */
  60482. return EXPECT_RESULT();
  60483. }
  60484. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  60485. static int test_dtls13_bad_epoch_ch(void)
  60486. {
  60487. EXPECT_DECLS;
  60488. WOLFSSL_CTX *ctx_c = NULL;
  60489. WOLFSSL_CTX *ctx_s = NULL;
  60490. WOLFSSL *ssl_c = NULL;
  60491. WOLFSSL *ssl_s = NULL;
  60492. struct test_memio_ctx test_ctx;
  60493. const int EPOCH_OFF = 3;
  60494. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60495. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60496. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  60497. /* disable hrr cookie so we can later check msgsReceived.got_client_hello
  60498. * with just one message */
  60499. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  60500. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60501. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60502. WOLFSSL_ERROR_WANT_READ);
  60503. ExpectIntGE(test_ctx.s_len, EPOCH_OFF + 2);
  60504. /* first CH should use epoch 0x0 */
  60505. ExpectTrue((test_ctx.s_buff[EPOCH_OFF] == 0x0) &&
  60506. (test_ctx.s_buff[EPOCH_OFF + 1] == 0x0));
  60507. /* change epoch to 2 */
  60508. test_ctx.s_buff[EPOCH_OFF + 1] = 0x2;
  60509. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  60510. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  60511. WOLFSSL_ERROR_WANT_READ);
  60512. ExpectIntNE(ssl_s->msgsReceived.got_client_hello, 1);
  60513. /* resend the CH */
  60514. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  60515. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60516. wolfSSL_free(ssl_c);
  60517. wolfSSL_CTX_free(ctx_c);
  60518. wolfSSL_free(ssl_s);
  60519. wolfSSL_CTX_free(ctx_s);
  60520. return EXPECT_RESULT();
  60521. }
  60522. #else
  60523. static int test_dtls13_bad_epoch_ch(void)
  60524. {
  60525. return TEST_SKIPPED;
  60526. }
  60527. #endif
  60528. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_SESSION_CACHE)
  60529. static int test_short_session_id_ssl_ready(WOLFSSL* ssl)
  60530. {
  60531. EXPECT_DECLS;
  60532. WOLFSSL_SESSION *sess = NULL;
  60533. /* Setup the session to avoid errors */
  60534. ssl->session->timeout = -1;
  60535. ssl->session->side = WOLFSSL_CLIENT_END;
  60536. #if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
  60537. defined(HAVE_SESSION_TICKET))
  60538. ssl->session->version = ssl->version;
  60539. #endif
  60540. /* Force a short session ID to be sent */
  60541. ssl->session->sessionIDSz = 4;
  60542. #ifndef NO_SESSION_CACHE_REF
  60543. /* Allow the client cache to be used */
  60544. ssl->session->idLen = 4;
  60545. #endif
  60546. ssl->session->isSetup = 1;
  60547. ExpectNotNull(sess = wolfSSL_get_session(ssl));
  60548. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  60549. return EXPECT_RESULT();
  60550. }
  60551. static int test_short_session_id(void)
  60552. {
  60553. EXPECT_DECLS;
  60554. test_ssl_cbf client_cbf;
  60555. test_ssl_cbf server_cbf;
  60556. size_t i;
  60557. struct {
  60558. method_provider client_meth;
  60559. method_provider server_meth;
  60560. const char* tls_version;
  60561. } params[] = {
  60562. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  60563. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID) && \
  60564. !defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
  60565. /* With WOLFSSL_TLS13_MIDDLEBOX_COMPAT a short ID will result in an error */
  60566. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  60567. #ifdef WOLFSSL_DTLS13
  60568. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  60569. #endif
  60570. #endif
  60571. #ifndef WOLFSSL_NO_TLS12
  60572. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  60573. #ifdef WOLFSSL_DTLS
  60574. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  60575. #endif
  60576. #endif
  60577. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  60578. !defined(NO_DES3))
  60579. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  60580. #ifdef WOLFSSL_DTLS
  60581. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  60582. #endif
  60583. #endif
  60584. };
  60585. fprintf(stderr, "\n");
  60586. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  60587. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  60588. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  60589. fprintf(stderr, "\tTesting short ID with %s\n", params[i].tls_version);
  60590. client_cbf.ssl_ready = test_short_session_id_ssl_ready;
  60591. client_cbf.method = params[i].client_meth;
  60592. server_cbf.method = params[i].server_meth;
  60593. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  60594. &server_cbf, NULL), TEST_SUCCESS);
  60595. }
  60596. return EXPECT_RESULT();
  60597. }
  60598. #else
  60599. static int test_short_session_id(void)
  60600. {
  60601. return TEST_SKIPPED;
  60602. }
  60603. #endif
  60604. #if defined(HAVE_NULL_CIPHER) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) \
  60605. && defined(WOLFSSL_DTLS13)
  60606. static byte* test_find_string(const char *string,
  60607. byte *buf, int buf_size)
  60608. {
  60609. int string_size, i;
  60610. string_size = (int)XSTRLEN(string);
  60611. for (i = 0; i < buf_size - string_size - 1; i++) {
  60612. if (XSTRCMP((char*)&buf[i], string) == 0)
  60613. return &buf[i];
  60614. }
  60615. return NULL;
  60616. }
  60617. static int test_wolfSSL_dtls13_null_cipher(void)
  60618. {
  60619. EXPECT_DECLS;
  60620. WOLFSSL_CTX *ctx_c = NULL;
  60621. WOLFSSL_CTX *ctx_s = NULL;
  60622. WOLFSSL *ssl_c = NULL;
  60623. WOLFSSL *ssl_s = NULL;
  60624. struct test_memio_ctx test_ctx;
  60625. const char *test_str = "test";
  60626. int test_str_size;
  60627. byte buf[255], *ptr = NULL;
  60628. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60629. test_ctx.c_ciphers = test_ctx.s_ciphers = "TLS13-SHA256-SHA256";
  60630. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60631. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  60632. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60633. test_str_size = XSTRLEN("test") + 1;
  60634. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  60635. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  60636. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  60637. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  60638. /* check that the packet was sent cleartext */
  60639. ExpectNotNull(ptr = test_find_string(test_str, test_ctx.s_buff,
  60640. test_ctx.s_len));
  60641. if (ptr != NULL) {
  60642. /* modify the message */
  60643. *ptr = 'H';
  60644. /* bad messages should be ignored in DTLS */
  60645. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), -1);
  60646. ExpectIntEQ(ssl_s->error, WANT_READ);
  60647. }
  60648. wolfSSL_free(ssl_c);
  60649. wolfSSL_free(ssl_s);
  60650. wolfSSL_CTX_free(ctx_c);
  60651. wolfSSL_CTX_free(ctx_s);
  60652. return TEST_SUCCESS;
  60653. }
  60654. #else
  60655. static int test_wolfSSL_dtls13_null_cipher(void)
  60656. {
  60657. return TEST_SKIPPED;
  60658. }
  60659. #endif
  60660. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  60661. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  60662. !defined(SINGLE_THREADED) && !defined(NO_RSA)
  60663. static int test_dtls_msg_get_connected_port(int fd, word16 *port)
  60664. {
  60665. SOCKADDR_S peer;
  60666. XSOCKLENT len;
  60667. int ret;
  60668. XMEMSET((byte*)&peer, 0, sizeof(peer));
  60669. len = sizeof(peer);
  60670. ret = getpeername(fd, (SOCKADDR*)&peer, &len);
  60671. if (ret != 0 || len > (XSOCKLENT)sizeof(peer))
  60672. return -1;
  60673. switch (peer.ss_family) {
  60674. #ifdef WOLFSSL_IPV6
  60675. case WOLFSSL_IP6: {
  60676. *port = ntohs(((SOCKADDR_IN6*)&peer)->sin6_port);
  60677. break;
  60678. }
  60679. #endif /* WOLFSSL_IPV6 */
  60680. case WOLFSSL_IP4:
  60681. *port = ntohs(((SOCKADDR_IN*)&peer)->sin_port);
  60682. break;
  60683. default:
  60684. return -1;
  60685. }
  60686. return 0;
  60687. }
  60688. static int test_dtls_msg_from_other_peer_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  60689. {
  60690. char buf[1] = {'t'};
  60691. SOCKADDR_IN_T addr;
  60692. int sock_fd;
  60693. word16 port;
  60694. int err;
  60695. (void)ssl;
  60696. (void)ctx;
  60697. if (ssl == NULL)
  60698. return -1;
  60699. err = test_dtls_msg_get_connected_port(wolfSSL_get_fd(ssl), &port);
  60700. if (err != 0)
  60701. return -1;
  60702. sock_fd = socket(AF_INET_V, SOCK_DGRAM, 0);
  60703. if (sock_fd == -1)
  60704. return -1;
  60705. build_addr(&addr, wolfSSLIP, port, 1, 0);
  60706. /* send a packet to the server. Being another socket, the kernel will ensure
  60707. * the source port will be different. */
  60708. err = (int)sendto(sock_fd, buf, sizeof(buf), 0, (SOCKADDR*)&addr,
  60709. sizeof(addr));
  60710. close(sock_fd);
  60711. if (err == -1)
  60712. return -1;
  60713. return 0;
  60714. }
  60715. /* setup a SSL session but just after the handshake send a packet to the server
  60716. * with a source address different than the one of the connected client. The I/O
  60717. * callback EmbedRecvFrom should just ignore the packet. Sending of the packet
  60718. * is done in test_dtls_msg_from_other_peer_cb */
  60719. static int test_dtls_msg_from_other_peer(void)
  60720. {
  60721. EXPECT_DECLS;
  60722. callback_functions client_cbs;
  60723. callback_functions server_cbs;
  60724. XMEMSET((byte*)&client_cbs, 0, sizeof(client_cbs));
  60725. XMEMSET((byte*)&server_cbs, 0, sizeof(server_cbs));
  60726. client_cbs.method = wolfDTLSv1_2_client_method;
  60727. server_cbs.method = wolfDTLSv1_2_server_method;
  60728. client_cbs.doUdp = 1;
  60729. server_cbs.doUdp = 1;
  60730. test_wolfSSL_client_server_nofail_ex(&client_cbs, &server_cbs,
  60731. test_dtls_msg_from_other_peer_cb);
  60732. ExpectIntEQ(client_cbs.return_code, WOLFSSL_SUCCESS);
  60733. ExpectIntEQ(server_cbs.return_code, WOLFSSL_SUCCESS);
  60734. return EXPECT_RESULT();
  60735. }
  60736. #else
  60737. static int test_dtls_msg_from_other_peer(void)
  60738. {
  60739. return TEST_SKIPPED;
  60740. }
  60741. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  60742. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  60743. * !defined(SINGLE_THREADED) && !defined(NO_RSA) */
  60744. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_IPV6) && \
  60745. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  60746. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  60747. static int test_dtls_ipv6_check(void)
  60748. {
  60749. EXPECT_DECLS;
  60750. WOLFSSL_CTX *ctx_c = NULL;
  60751. WOLFSSL_CTX *ctx_s = NULL;
  60752. WOLFSSL *ssl_c = NULL;
  60753. WOLFSSL *ssl_s = NULL;
  60754. SOCKADDR_IN fake_addr6;
  60755. int sockfd = -1;
  60756. ExpectNotNull(ctx_c = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  60757. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  60758. ExpectNotNull(ctx_s = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  60759. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile,
  60760. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60761. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile,
  60762. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60763. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  60764. XMEMSET((byte*)&fake_addr6, 0, sizeof(fake_addr6));
  60765. /* mimic a sockaddr_in6 struct, this way we can't test without
  60766. * WOLFSSL_IPV6 */
  60767. fake_addr6.sin_family = WOLFSSL_IP6;
  60768. ExpectIntNE(sockfd = socket(AF_INET, SOCK_DGRAM, 0), -1);
  60769. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  60770. /* can't return error here, as the peer is opaque for wolfssl library at
  60771. * this point */
  60772. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_c, &fake_addr6, sizeof(fake_addr6)),
  60773. WOLFSSL_SUCCESS);
  60774. ExpectIntNE(fcntl(sockfd, F_SETFL, O_NONBLOCK), -1);
  60775. wolfSSL_dtls_set_using_nonblock(ssl_c, 1);
  60776. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60777. ExpectIntEQ(ssl_c->error, SOCKET_ERROR_E);
  60778. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_s, &fake_addr6, sizeof(fake_addr6)),
  60779. WOLFSSL_SUCCESS);
  60780. /* reuse the socket */
  60781. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  60782. wolfSSL_dtls_set_using_nonblock(ssl_s, 1);
  60783. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  60784. ExpectIntEQ(ssl_s->error, SOCKET_ERROR_E);
  60785. if (sockfd != -1)
  60786. close(sockfd);
  60787. wolfSSL_free(ssl_c);
  60788. wolfSSL_CTX_free(ctx_c);
  60789. wolfSSL_free(ssl_s);
  60790. wolfSSL_CTX_free(ctx_s);
  60791. return EXPECT_RESULT();
  60792. }
  60793. #else
  60794. static int test_dtls_ipv6_check(void)
  60795. {
  60796. return TEST_SKIPPED;
  60797. }
  60798. #endif
  60799. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  60800. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  60801. static WOLFSSL_SESSION* test_wolfSSL_SCR_after_resumption_session = NULL;
  60802. static void test_wolfSSL_SCR_after_resumption_ctx_ready(WOLFSSL_CTX* ctx)
  60803. {
  60804. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  60805. }
  60806. static void test_wolfSSL_SCR_after_resumption_on_result(WOLFSSL* ssl)
  60807. {
  60808. if (test_wolfSSL_SCR_after_resumption_session == NULL) {
  60809. test_wolfSSL_SCR_after_resumption_session = wolfSSL_get1_session(ssl);
  60810. AssertNotNull(test_wolfSSL_SCR_after_resumption_session);
  60811. }
  60812. else {
  60813. char testMsg[] = "Message after SCR";
  60814. char msgBuf[sizeof(testMsg)];
  60815. int ret;
  60816. if (!wolfSSL_is_server(ssl)) {
  60817. AssertIntEQ(WOLFSSL_SUCCESS,
  60818. wolfSSL_set_session(ssl,
  60819. test_wolfSSL_SCR_after_resumption_session));
  60820. }
  60821. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  60822. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  60823. sizeof(testMsg));
  60824. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  60825. if (ret != sizeof(msgBuf)) /* Possibly APP_DATA_READY error. Retry. */
  60826. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  60827. AssertIntEQ(ret, sizeof(msgBuf));
  60828. }
  60829. }
  60830. static void test_wolfSSL_SCR_after_resumption_ssl_ready(WOLFSSL* ssl)
  60831. {
  60832. AssertIntEQ(WOLFSSL_SUCCESS,
  60833. wolfSSL_set_session(ssl, test_wolfSSL_SCR_after_resumption_session));
  60834. }
  60835. static int test_wolfSSL_SCR_after_resumption(void)
  60836. {
  60837. EXPECT_DECLS;
  60838. callback_functions func_cb_client;
  60839. callback_functions func_cb_server;
  60840. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  60841. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  60842. func_cb_client.method = wolfTLSv1_2_client_method;
  60843. func_cb_client.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  60844. func_cb_client.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  60845. func_cb_server.method = wolfTLSv1_2_server_method;
  60846. func_cb_server.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  60847. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60848. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60849. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60850. func_cb_client.ssl_ready = test_wolfSSL_SCR_after_resumption_ssl_ready;
  60851. func_cb_server.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  60852. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60853. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60854. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60855. wolfSSL_SESSION_free(test_wolfSSL_SCR_after_resumption_session);
  60856. return EXPECT_RESULT();
  60857. }
  60858. #else
  60859. static int test_wolfSSL_SCR_after_resumption(void)
  60860. {
  60861. return TEST_SKIPPED;
  60862. }
  60863. #endif
  60864. static int test_wolfSSL_configure_args(void)
  60865. {
  60866. EXPECT_DECLS;
  60867. #if defined(LIBWOLFSSL_CONFIGURE_ARGS) && defined(HAVE_WC_INTROSPECTION)
  60868. ExpectNotNull(wolfSSL_configure_args());
  60869. #endif
  60870. return EXPECT_RESULT();
  60871. }
  60872. static int test_dtls_no_extensions(void)
  60873. {
  60874. EXPECT_DECLS;
  60875. #if defined(WOLFSSL_DTLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  60876. !defined(WOLFSSL_NO_TLS12)
  60877. WOLFSSL *ssl_s = NULL;
  60878. WOLFSSL_CTX *ctx_s = NULL;
  60879. struct test_memio_ctx test_ctx;
  60880. const byte chNoExtensions[] = {
  60881. /* Handshake type */
  60882. 0x16,
  60883. /* Version */
  60884. 0xfe, 0xff,
  60885. /* Epoch */
  60886. 0x00, 0x00,
  60887. /* Seq number */
  60888. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60889. /* Length */
  60890. 0x00, 0x40,
  60891. /* CH type */
  60892. 0x01,
  60893. /* Length */
  60894. 0x00, 0x00, 0x34,
  60895. /* Msg Seq */
  60896. 0x00, 0x00,
  60897. /* Frag offset */
  60898. 0x00, 0x00, 0x00,
  60899. /* Frag length */
  60900. 0x00, 0x00, 0x34,
  60901. /* Version */
  60902. 0xfe, 0xff,
  60903. /* Random */
  60904. 0x62, 0xfe, 0xbc, 0xfe, 0x2b, 0xfe, 0x3f, 0xeb, 0x03, 0xc4, 0xea, 0x37,
  60905. 0xe7, 0x47, 0x7e, 0x8a, 0xd9, 0xbf, 0x77, 0x0f, 0x6c, 0xb6, 0x77, 0x0b,
  60906. 0x03, 0x3f, 0x82, 0x2b, 0x21, 0x64, 0x57, 0x1d,
  60907. /* Session Length */
  60908. 0x00,
  60909. /* Cookie Length */
  60910. 0x00,
  60911. /* CS Length */
  60912. 0x00, 0x0c,
  60913. /* CS */
  60914. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x39, 0x00, 0x33,
  60915. /* Comp Meths Length */
  60916. 0x01,
  60917. /* Comp Meths */
  60918. 0x00
  60919. /* And finally... no extensions */
  60920. };
  60921. int i;
  60922. #ifdef OPENSSL_EXTRA
  60923. int repeats = 2;
  60924. #else
  60925. int repeats = 1;
  60926. #endif
  60927. for (i = 0; i < repeats; i++) {
  60928. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60929. ssl_s = NULL;
  60930. ctx_s = NULL;
  60931. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  60932. NULL, wolfDTLS_server_method), 0);
  60933. XMEMCPY(test_ctx.s_buff, chNoExtensions, sizeof(chNoExtensions));
  60934. test_ctx.s_len = sizeof(chNoExtensions);
  60935. #ifdef OPENSSL_EXTRA
  60936. if (i > 0) {
  60937. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl_s, DTLS1_2_VERSION),
  60938. WOLFSSL_SUCCESS);
  60939. }
  60940. #endif
  60941. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  60942. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60943. /* Expecting a handshake msg. Either HVR or SH. */
  60944. ExpectIntGT(test_ctx.c_len, 0);
  60945. ExpectIntEQ(test_ctx.c_buff[0], 0x16);
  60946. wolfSSL_free(ssl_s);
  60947. wolfSSL_CTX_free(ctx_s);
  60948. }
  60949. #endif
  60950. return EXPECT_RESULT();
  60951. }
  60952. static int test_TLSX_CA_NAMES_bad_extension(void)
  60953. {
  60954. EXPECT_DECLS;
  60955. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  60956. !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES) && \
  60957. defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA384) && \
  60958. defined(HAVE_NULL_CIPHER)
  60959. /* This test should only fail (with BUFFER_ERROR) when we actually try to
  60960. * parse the CA Names extension. Otherwise it will return other non-related
  60961. * errors. If CA Names will be parsed in more configurations, that should
  60962. * be reflected in the macro guard above. */
  60963. WOLFSSL *ssl_c = NULL;
  60964. WOLFSSL_CTX *ctx_c = NULL;
  60965. struct test_memio_ctx test_ctx;
  60966. /* HRR + SH using TLS_DHE_PSK_WITH_NULL_SHA384 */
  60967. const byte shBadCaNamesExt[] = {
  60968. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  60969. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  60970. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  60971. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  60972. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  60973. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  60974. 0x5c, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x03, 0xcf, 0x21, 0xad, 0x74,
  60975. 0x00, 0x00, 0x83, 0x3f, 0x3b, 0x80, 0x01, 0xac, 0x65, 0x8c, 0x19, 0x2a,
  60976. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x02, 0x00, 0x9e, 0x09, 0x1c, 0xe8,
  60977. 0xa8, 0x09, 0x9c, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  60978. 0x03, 0x3f, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x13, 0x05,
  60979. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x00, 0x09, 0x00, 0x00,
  60980. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x01, 0xff,
  60981. 0xff, 0xff, 0xff, 0xfa, 0x0d, 0x00, 0x00, 0x00, 0xad, 0x02
  60982. };
  60983. const byte shBadCaNamesExt2[] = {
  60984. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  60985. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  60986. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  60987. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  60988. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  60989. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  60990. 0x5e, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x7f, 0xd0, 0x2d, 0xea, 0x6e,
  60991. 0x53, 0xa1, 0x6a, 0xc9, 0xc8, 0x54, 0xef, 0x75, 0xe4, 0xd9, 0xc6, 0x3e,
  60992. 0x74, 0xcb, 0x30, 0x80, 0xcc, 0x83, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x00,
  60993. 0x00, 0xc0, 0x5a, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  60994. 0x03, 0x03, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x53, 0x25,
  60995. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x02, 0x05, 0x00, 0x00,
  60996. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x06, 0x00,
  60997. 0x04, 0x00, 0x03, 0x30, 0x00, 0x13, 0x94, 0x00, 0x06, 0x00, 0x04, 0x02
  60998. };
  60999. int i = 0;
  61000. for (i = 0; i < 2; i++) {
  61001. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61002. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  61003. wolfTLSv1_3_client_method, NULL), 0);
  61004. switch (i) {
  61005. case 0:
  61006. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt,
  61007. sizeof(shBadCaNamesExt));
  61008. test_ctx.c_len = sizeof(shBadCaNamesExt);
  61009. break;
  61010. case 1:
  61011. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt2,
  61012. sizeof(shBadCaNamesExt2));
  61013. test_ctx.c_len = sizeof(shBadCaNamesExt2);
  61014. break;
  61015. }
  61016. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  61017. #ifndef WOLFSSL_DISABLE_EARLY_SANITY_CHECKS
  61018. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), OUT_OF_ORDER_E);
  61019. #else
  61020. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), BUFFER_ERROR);
  61021. #endif
  61022. wolfSSL_free(ssl_c);
  61023. ssl_c = NULL;
  61024. wolfSSL_CTX_free(ctx_c);
  61025. ctx_c = NULL;
  61026. }
  61027. #endif
  61028. return EXPECT_RESULT();
  61029. }
  61030. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61031. defined(HAVE_IO_TESTS_DEPENDENCIES)
  61032. static void test_dtls_1_0_hvr_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  61033. {
  61034. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  61035. WOLFSSL_SUCCESS);
  61036. }
  61037. static int test_dtls_1_0_hvr_downgrade(void)
  61038. {
  61039. EXPECT_DECLS;
  61040. callback_functions func_cb_client;
  61041. callback_functions func_cb_server;
  61042. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  61043. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  61044. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  61045. func_cb_client.method = wolfDTLS_client_method;
  61046. func_cb_server.method = wolfDTLSv1_2_server_method;
  61047. func_cb_client.ctx_ready = test_dtls_1_0_hvr_downgrade_ctx_ready;
  61048. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61049. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61050. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61051. return EXPECT_RESULT();
  61052. }
  61053. #else
  61054. static int test_dtls_1_0_hvr_downgrade(void)
  61055. {
  61056. EXPECT_DECLS;
  61057. return EXPECT_RESULT();
  61058. }
  61059. #endif
  61060. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12) && \
  61061. defined(HAVE_SESSION_TICKET)
  61062. static WOLFSSL_SESSION* test_session_ticket_no_id_session = NULL;
  61063. static void test_session_ticket_no_id_on_result(WOLFSSL* ssl)
  61064. {
  61065. test_session_ticket_no_id_session = wolfSSL_get1_session(ssl);
  61066. AssertNotNull(test_session_ticket_no_id_session);
  61067. }
  61068. static void test_session_ticket_no_id_ctx_ready(WOLFSSL_CTX* ctx)
  61069. {
  61070. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  61071. }
  61072. static void test_session_ticket_no_id_ssl_ready(WOLFSSL* ssl)
  61073. {
  61074. test_session_ticket_no_id_session->sessionIDSz = 0;
  61075. AssertIntEQ(WOLFSSL_SUCCESS,
  61076. wolfSSL_set_session(ssl, test_session_ticket_no_id_session));
  61077. }
  61078. static int test_session_ticket_no_id(void)
  61079. {
  61080. /* We are testing an expired (invalid crypto context in out case since the
  61081. * ctx changes) session ticket being sent with the session ID being 0
  61082. * length. */
  61083. EXPECT_DECLS;
  61084. callback_functions func_cb_client;
  61085. callback_functions func_cb_server;
  61086. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  61087. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  61088. func_cb_client.method = wolfTLSv1_2_client_method;
  61089. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  61090. func_cb_client.on_result = test_session_ticket_no_id_on_result;
  61091. func_cb_server.method = wolfTLSv1_2_server_method;
  61092. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  61093. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61094. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61095. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61096. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  61097. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  61098. func_cb_client.method = wolfTLSv1_2_client_method;
  61099. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  61100. func_cb_client.ssl_ready = test_session_ticket_no_id_ssl_ready;
  61101. func_cb_server.method = wolfTLSv1_2_server_method;
  61102. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  61103. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61104. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61105. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61106. wolfSSL_SESSION_free(test_session_ticket_no_id_session);
  61107. return EXPECT_RESULT();
  61108. }
  61109. #else
  61110. static int test_session_ticket_no_id(void)
  61111. {
  61112. EXPECT_DECLS;
  61113. return EXPECT_RESULT();
  61114. }
  61115. #endif
  61116. static int test_session_ticket_hs_update(void)
  61117. {
  61118. EXPECT_DECLS;
  61119. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  61120. defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  61121. struct test_memio_ctx test_ctx;
  61122. struct test_memio_ctx test_ctx2;
  61123. struct test_memio_ctx test_ctx3;
  61124. WOLFSSL_CTX *ctx_c = NULL;
  61125. WOLFSSL_CTX *ctx_s = NULL;
  61126. WOLFSSL *ssl_c = NULL;
  61127. WOLFSSL *ssl_c2 = NULL;
  61128. WOLFSSL *ssl_c3 = NULL;
  61129. WOLFSSL *ssl_s = NULL;
  61130. WOLFSSL *ssl_s2 = NULL;
  61131. WOLFSSL *ssl_s3 = NULL;
  61132. WOLFSSL_SESSION *sess = NULL;
  61133. byte read_data[1];
  61134. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61135. XMEMSET(&test_ctx2, 0, sizeof(test_ctx2));
  61136. XMEMSET(&test_ctx3, 0, sizeof(test_ctx3));
  61137. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61138. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  61139. /* Generate tickets */
  61140. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61141. wolfSSL_SetLoggingPrefix("client");
  61142. /* Read the ticket msg */
  61143. ExpectIntEQ(wolfSSL_read(ssl_c, read_data, sizeof(read_data)),
  61144. WOLFSSL_FATAL_ERROR);
  61145. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61146. WOLFSSL_ERROR_WANT_READ);
  61147. wolfSSL_SetLoggingPrefix(NULL);
  61148. ExpectIntEQ(test_memio_setup(&test_ctx2, &ctx_c, &ctx_s, &ssl_c2, &ssl_s2,
  61149. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  61150. ExpectIntEQ(test_memio_setup(&test_ctx3, &ctx_c, &ctx_s, &ssl_c3, &ssl_s3,
  61151. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  61152. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  61153. ExpectIntEQ(wolfSSL_set_session(ssl_c2, sess), WOLFSSL_SUCCESS);
  61154. ExpectIntEQ(wolfSSL_set_session(ssl_c3, sess), WOLFSSL_SUCCESS);
  61155. wolfSSL_SetLoggingPrefix("client");
  61156. /* Exchange initial flights for the second connection */
  61157. ExpectIntEQ(wolfSSL_connect(ssl_c2), WOLFSSL_FATAL_ERROR);
  61158. ExpectIntEQ(wolfSSL_get_error(ssl_c2, WOLFSSL_FATAL_ERROR),
  61159. WOLFSSL_ERROR_WANT_READ);
  61160. wolfSSL_SetLoggingPrefix(NULL);
  61161. wolfSSL_SetLoggingPrefix("server");
  61162. ExpectIntEQ(wolfSSL_accept(ssl_s2), WOLFSSL_FATAL_ERROR);
  61163. ExpectIntEQ(wolfSSL_get_error(ssl_s2, WOLFSSL_FATAL_ERROR),
  61164. WOLFSSL_ERROR_WANT_READ);
  61165. wolfSSL_SetLoggingPrefix(NULL);
  61166. /* Complete third connection so that new tickets are exchanged */
  61167. ExpectIntEQ(test_memio_do_handshake(ssl_c3, ssl_s3, 10, NULL), 0);
  61168. /* Read the ticket msg */
  61169. wolfSSL_SetLoggingPrefix("client");
  61170. ExpectIntEQ(wolfSSL_read(ssl_c3, read_data, sizeof(read_data)),
  61171. WOLFSSL_FATAL_ERROR);
  61172. ExpectIntEQ(wolfSSL_get_error(ssl_c3, WOLFSSL_FATAL_ERROR),
  61173. WOLFSSL_ERROR_WANT_READ);
  61174. wolfSSL_SetLoggingPrefix(NULL);
  61175. /* Complete second connection */
  61176. ExpectIntEQ(test_memio_do_handshake(ssl_c2, ssl_s2, 10, NULL), 0);
  61177. ExpectIntEQ(wolfSSL_session_reused(ssl_c2), 1);
  61178. ExpectIntEQ(wolfSSL_session_reused(ssl_c3), 1);
  61179. wolfSSL_free(ssl_c);
  61180. wolfSSL_free(ssl_c2);
  61181. wolfSSL_free(ssl_c3);
  61182. wolfSSL_free(ssl_s);
  61183. wolfSSL_free(ssl_s2);
  61184. wolfSSL_free(ssl_s3);
  61185. wolfSSL_CTX_free(ctx_c);
  61186. wolfSSL_CTX_free(ctx_s);
  61187. wolfSSL_SESSION_free(sess);
  61188. #endif
  61189. return EXPECT_RESULT();
  61190. }
  61191. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61192. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  61193. static void test_dtls_downgrade_scr_server_ctx_ready_server(WOLFSSL_CTX* ctx)
  61194. {
  61195. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  61196. WOLFSSL_SUCCESS);
  61197. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  61198. }
  61199. static void test_dtls_downgrade_scr_server_ctx_ready(WOLFSSL_CTX* ctx)
  61200. {
  61201. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  61202. }
  61203. static void test_dtls_downgrade_scr_server_on_result(WOLFSSL* ssl)
  61204. {
  61205. char testMsg[] = "Message after SCR";
  61206. char msgBuf[sizeof(testMsg)];
  61207. if (wolfSSL_is_server(ssl)) {
  61208. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  61209. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  61210. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  61211. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  61212. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  61213. sizeof(testMsg));
  61214. }
  61215. else {
  61216. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  61217. sizeof(testMsg));
  61218. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  61219. }
  61220. }
  61221. static int test_dtls_downgrade_scr_server(void)
  61222. {
  61223. EXPECT_DECLS;
  61224. callback_functions func_cb_client;
  61225. callback_functions func_cb_server;
  61226. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  61227. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  61228. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  61229. func_cb_client.method = wolfDTLSv1_2_client_method;
  61230. func_cb_server.method = wolfDTLS_server_method;
  61231. func_cb_client.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready;
  61232. func_cb_server.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready_server;
  61233. func_cb_client.on_result = test_dtls_downgrade_scr_server_on_result;
  61234. func_cb_server.on_result = test_dtls_downgrade_scr_server_on_result;
  61235. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61236. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61237. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61238. return EXPECT_RESULT();
  61239. }
  61240. #else
  61241. static int test_dtls_downgrade_scr_server(void)
  61242. {
  61243. EXPECT_DECLS;
  61244. return EXPECT_RESULT();
  61245. }
  61246. #endif
  61247. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61248. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  61249. static void test_dtls_downgrade_scr_ctx_ready(WOLFSSL_CTX* ctx)
  61250. {
  61251. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  61252. WOLFSSL_SUCCESS);
  61253. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  61254. }
  61255. static void test_dtls_downgrade_scr_on_result(WOLFSSL* ssl)
  61256. {
  61257. char testMsg[] = "Message after SCR";
  61258. char msgBuf[sizeof(testMsg)];
  61259. if (wolfSSL_is_server(ssl)) {
  61260. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  61261. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  61262. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  61263. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  61264. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  61265. sizeof(testMsg));
  61266. }
  61267. else {
  61268. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  61269. sizeof(testMsg));
  61270. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  61271. }
  61272. }
  61273. static int test_dtls_downgrade_scr(void)
  61274. {
  61275. EXPECT_DECLS;
  61276. callback_functions func_cb_client;
  61277. callback_functions func_cb_server;
  61278. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  61279. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  61280. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  61281. func_cb_client.method = wolfDTLS_client_method;
  61282. func_cb_server.method = wolfDTLSv1_2_server_method;
  61283. func_cb_client.ctx_ready = test_dtls_downgrade_scr_ctx_ready;
  61284. func_cb_client.on_result = test_dtls_downgrade_scr_on_result;
  61285. func_cb_server.on_result = test_dtls_downgrade_scr_on_result;
  61286. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61287. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61288. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61289. return EXPECT_RESULT();
  61290. }
  61291. #else
  61292. static int test_dtls_downgrade_scr(void)
  61293. {
  61294. EXPECT_DECLS;
  61295. return EXPECT_RESULT();
  61296. }
  61297. #endif
  61298. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  61299. && !defined(WOLFSSL_NO_TLS12)
  61300. static int test_dtls_client_hello_timeout_downgrade_read_cb(WOLFSSL *ssl,
  61301. char *data, int sz, void *ctx)
  61302. {
  61303. static int call_counter = 0;
  61304. call_counter++;
  61305. (void)ssl;
  61306. (void)data;
  61307. (void)sz;
  61308. (void)ctx;
  61309. switch (call_counter) {
  61310. case 1:
  61311. case 2:
  61312. return WOLFSSL_CBIO_ERR_TIMEOUT;
  61313. case 3:
  61314. return WOLFSSL_CBIO_ERR_WANT_READ;
  61315. default:
  61316. AssertIntLE(call_counter, 3);
  61317. return -1;
  61318. }
  61319. }
  61320. #endif
  61321. /* Make sure we don't send acks before getting a server hello */
  61322. static int test_dtls_client_hello_timeout_downgrade(void)
  61323. {
  61324. EXPECT_DECLS;
  61325. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  61326. && !defined(WOLFSSL_NO_TLS12)
  61327. WOLFSSL_CTX *ctx_c = NULL;
  61328. WOLFSSL_CTX *ctx_s = NULL;
  61329. WOLFSSL *ssl_c = NULL;
  61330. WOLFSSL *ssl_s = NULL;
  61331. struct test_memio_ctx test_ctx;
  61332. DtlsRecordLayerHeader* dtlsRH;
  61333. size_t len;
  61334. byte sequence_number[8];
  61335. int i;
  61336. for (i = 0; i < 2; i++) {
  61337. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61338. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61339. wolfDTLS_client_method, wolfDTLSv1_2_server_method), 0);
  61340. if (i == 0) {
  61341. /* First time simulate timeout in IO layer */
  61342. /* CH1 */
  61343. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61344. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61345. /* HVR */
  61346. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61347. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61348. /* CH2 */
  61349. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61350. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61351. /* SH flight */
  61352. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61353. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61354. /* Drop the SH */
  61355. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  61356. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61357. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  61358. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  61359. (sizeof(DtlsRecordLayerHeader) + len));
  61360. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  61361. /* Read the remainder of the flight */
  61362. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61363. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61364. wolfSSL_SSLSetIORecv(ssl_c,
  61365. test_dtls_client_hello_timeout_downgrade_read_cb);
  61366. /* CH3 */
  61367. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61368. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61369. wolfSSL_SSLSetIORecv(ssl_c, test_memio_read_cb);
  61370. }
  61371. else {
  61372. /* Second time call wolfSSL_dtls_got_timeout */
  61373. /* CH1 */
  61374. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61375. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61376. /* HVR */
  61377. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61378. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61379. /* CH2 */
  61380. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61381. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61382. /* SH flight */
  61383. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61384. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61385. /* Drop the SH */
  61386. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  61387. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61388. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  61389. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  61390. (sizeof(DtlsRecordLayerHeader) + len));
  61391. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  61392. /* Read the remainder of the flight */
  61393. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61394. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61395. /* Quick timeout should be set as we received at least one msg */
  61396. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 1);
  61397. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  61398. /* Quick timeout should be cleared after a quick timeout */
  61399. /* CH3 */
  61400. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 0);
  61401. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  61402. }
  61403. /* Parse out to make sure we got exactly one ClientHello message */
  61404. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  61405. /* Second ClientHello after HVR */
  61406. sequence_number[7] = 2;
  61407. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.s_buff;
  61408. ExpectIntEQ(dtlsRH->type, handshake);
  61409. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  61410. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  61411. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  61412. sizeof(sequence_number)), 0);
  61413. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61414. ExpectIntEQ(sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  61415. /* Connection should be able to continue */
  61416. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61417. wolfSSL_free(ssl_c);
  61418. wolfSSL_free(ssl_s);
  61419. wolfSSL_CTX_free(ctx_c);
  61420. wolfSSL_CTX_free(ctx_s);
  61421. ssl_c = NULL;
  61422. ssl_s = NULL;
  61423. ctx_c = NULL;
  61424. ctx_s = NULL;
  61425. if (!EXPECT_SUCCESS())
  61426. break;
  61427. }
  61428. #endif
  61429. return EXPECT_RESULT();
  61430. }
  61431. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  61432. static int test_dtls_client_hello_timeout_read_cb(WOLFSSL *ssl, char *data,
  61433. int sz, void *ctx)
  61434. {
  61435. static int call_counter = 0;
  61436. call_counter++;
  61437. (void)ssl;
  61438. (void)data;
  61439. (void)sz;
  61440. (void)ctx;
  61441. switch (call_counter) {
  61442. case 1:
  61443. return WOLFSSL_CBIO_ERR_TIMEOUT;
  61444. case 2:
  61445. return WOLFSSL_CBIO_ERR_WANT_READ;
  61446. default:
  61447. AssertIntLE(call_counter, 2);
  61448. return -1;
  61449. }
  61450. }
  61451. #endif
  61452. /* Make sure we don't send acks before getting a server hello */
  61453. static int test_dtls_client_hello_timeout(void)
  61454. {
  61455. EXPECT_DECLS;
  61456. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  61457. WOLFSSL *ssl_c = NULL;
  61458. WOLFSSL_CTX *ctx_c = NULL;
  61459. struct test_memio_ctx test_ctx;
  61460. DtlsRecordLayerHeader* dtlsRH;
  61461. size_t idx;
  61462. size_t len;
  61463. byte sequence_number[8];
  61464. int i;
  61465. for (i = 0; i < 2; i++) {
  61466. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61467. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  61468. wolfDTLSv1_3_client_method, NULL), 0);
  61469. if (i == 0) {
  61470. /* First time simulate timeout in IO layer */
  61471. wolfSSL_SSLSetIORecv(ssl_c, test_dtls_client_hello_timeout_read_cb);
  61472. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  61473. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61474. }
  61475. else {
  61476. /* Second time call wolfSSL_dtls_got_timeout */
  61477. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  61478. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61479. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  61480. }
  61481. /* Parse out to make sure we got exactly two ClientHello messages */
  61482. idx = 0;
  61483. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  61484. /* First ClientHello */
  61485. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  61486. ExpectIntEQ(dtlsRH->type, handshake);
  61487. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  61488. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  61489. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  61490. sizeof(sequence_number)), 0);
  61491. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61492. ExpectIntLT(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  61493. idx += sizeof(DtlsRecordLayerHeader) + len;
  61494. /* Second ClientHello */
  61495. sequence_number[7] = 1;
  61496. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  61497. ExpectIntEQ(dtlsRH->type, handshake);
  61498. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  61499. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  61500. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  61501. sizeof(sequence_number)), 0);
  61502. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61503. ExpectIntEQ(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  61504. wolfSSL_free(ssl_c);
  61505. wolfSSL_CTX_free(ctx_c);
  61506. ssl_c = NULL;
  61507. ctx_c = NULL;
  61508. if (!EXPECT_SUCCESS())
  61509. break;
  61510. }
  61511. #endif
  61512. return EXPECT_RESULT();
  61513. }
  61514. /* DTLS test when dropping the changed cipher spec message */
  61515. static int test_dtls_dropped_ccs(void)
  61516. {
  61517. EXPECT_DECLS;
  61518. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  61519. && !defined(WOLFSSL_NO_TLS12)
  61520. WOLFSSL_CTX *ctx_c = NULL;
  61521. WOLFSSL_CTX *ctx_s = NULL;
  61522. WOLFSSL *ssl_c = NULL;
  61523. WOLFSSL *ssl_s = NULL;
  61524. struct test_memio_ctx test_ctx;
  61525. DtlsRecordLayerHeader* dtlsRH;
  61526. size_t len;
  61527. byte data[1];
  61528. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61529. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61530. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  61531. /* CH1 */
  61532. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61533. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61534. /* HVR */
  61535. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61536. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61537. /* CH2 */
  61538. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61539. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61540. /* Server first flight */
  61541. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61542. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61543. /* Client flight */
  61544. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61545. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61546. /* Server ccs + finished */
  61547. ExpectIntEQ(wolfSSL_negotiate(ssl_s), 1);
  61548. /* Drop the ccs */
  61549. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  61550. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61551. ExpectIntEQ(len, 1);
  61552. ExpectIntEQ(dtlsRH->type, change_cipher_spec);
  61553. if (EXPECT_SUCCESS()) {
  61554. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  61555. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  61556. (sizeof(DtlsRecordLayerHeader) + len));
  61557. }
  61558. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  61559. /* Client rtx flight */
  61560. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61561. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61562. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  61563. /* Server ccs + finished rtx */
  61564. ExpectIntEQ(wolfSSL_read(ssl_s, data, sizeof(data)), -1);
  61565. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61566. /* Client processes finished */
  61567. ExpectIntEQ(wolfSSL_negotiate(ssl_c), 1);
  61568. wolfSSL_free(ssl_c);
  61569. wolfSSL_free(ssl_s);
  61570. wolfSSL_CTX_free(ctx_c);
  61571. wolfSSL_CTX_free(ctx_s);
  61572. #endif
  61573. return EXPECT_RESULT();
  61574. }
  61575. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  61576. && !defined(WOLFSSL_NO_TLS12)
  61577. static int test_dtls_seq_num_downgrade_check_num(byte* ioBuf, int ioBufLen,
  61578. byte seq_num)
  61579. {
  61580. EXPECT_DECLS;
  61581. DtlsRecordLayerHeader* dtlsRH;
  61582. byte sequence_number[8];
  61583. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  61584. ExpectIntGE(ioBufLen, sizeof(*dtlsRH));
  61585. dtlsRH = (DtlsRecordLayerHeader*)ioBuf;
  61586. ExpectIntEQ(dtlsRH->type, handshake);
  61587. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  61588. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  61589. sequence_number[7] = seq_num;
  61590. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  61591. sizeof(sequence_number)), 0);
  61592. return EXPECT_RESULT();
  61593. }
  61594. #endif
  61595. /*
  61596. * Make sure that we send the correct sequence number after a HelloVerifyRequest
  61597. * and after a HelloRetryRequest. This is testing the server side as it is
  61598. * operating statelessly and should copy the sequence number of the ClientHello.
  61599. */
  61600. static int test_dtls_seq_num_downgrade(void)
  61601. {
  61602. EXPECT_DECLS;
  61603. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  61604. && !defined(WOLFSSL_NO_TLS12)
  61605. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  61606. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  61607. struct test_memio_ctx test_ctx;
  61608. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61609. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61610. wolfDTLSv1_2_client_method, wolfDTLS_server_method), 0);
  61611. /* CH1 */
  61612. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61613. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61614. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  61615. test_ctx.s_len, 0), TEST_SUCCESS);
  61616. /* HVR */
  61617. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61618. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61619. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  61620. test_ctx.c_len, 0), TEST_SUCCESS);
  61621. /* CH2 */
  61622. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61623. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61624. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  61625. test_ctx.s_len, 1), TEST_SUCCESS);
  61626. /* Server first flight */
  61627. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61628. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61629. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  61630. test_ctx.c_len, 1), TEST_SUCCESS);
  61631. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61632. wolfSSL_free(ssl_c);
  61633. wolfSSL_CTX_free(ctx_c);
  61634. wolfSSL_free(ssl_s);
  61635. wolfSSL_CTX_free(ctx_s);
  61636. #endif
  61637. return EXPECT_RESULT();
  61638. }
  61639. /**
  61640. * Make sure we don't send RSA Signature Hash Algorithms in the
  61641. * CertificateRequest when we don't have any such ciphers set.
  61642. * @return EXPECT_RESULT()
  61643. */
  61644. static int test_certreq_sighash_algos(void)
  61645. {
  61646. EXPECT_DECLS;
  61647. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  61648. !defined(WOLFSSL_MAX_STRENGTH) && defined(HAVE_ECC) && \
  61649. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  61650. defined(HAVE_AES_CBC) && !defined(WOLFSSL_NO_TLS12)
  61651. WOLFSSL_CTX *ctx_c = NULL;
  61652. WOLFSSL_CTX *ctx_s = NULL;
  61653. WOLFSSL *ssl_c = NULL;
  61654. WOLFSSL *ssl_s = NULL;
  61655. struct test_memio_ctx test_ctx;
  61656. int idx = 0;
  61657. int maxIdx = 0;
  61658. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61659. test_ctx.c_ciphers = test_ctx.s_ciphers =
  61660. "ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA384";
  61661. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61662. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  61663. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx_c,
  61664. "./certs/ca-ecc-cert.pem", NULL), WOLFSSL_SUCCESS);
  61665. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_PEER, NULL);
  61666. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ssl_s, "./certs/ecc-key.pem",
  61667. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61668. ExpectIntEQ(wolfSSL_use_certificate_file(ssl_s, "./certs/server-ecc.pem",
  61669. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61670. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_FATAL_ERROR);
  61671. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61672. WOLFSSL_ERROR_WANT_READ);
  61673. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_FATAL_ERROR);
  61674. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  61675. WOLFSSL_ERROR_WANT_READ);
  61676. /* Find the CertificateRequest message */
  61677. for (idx = 0; idx < test_ctx.c_len && EXPECT_SUCCESS();) {
  61678. word16 len;
  61679. ExpectIntEQ(test_ctx.c_buff[idx++], handshake);
  61680. ExpectIntEQ(test_ctx.c_buff[idx++], SSLv3_MAJOR);
  61681. ExpectIntEQ(test_ctx.c_buff[idx++], TLSv1_2_MINOR);
  61682. ato16(test_ctx.c_buff + idx, &len);
  61683. idx += OPAQUE16_LEN;
  61684. if (test_ctx.c_buff[idx] == certificate_request) {
  61685. idx++;
  61686. /* length */
  61687. idx += OPAQUE24_LEN;
  61688. /* cert types */
  61689. idx += 1 + test_ctx.c_buff[idx];
  61690. /* Sig algos */
  61691. ato16(test_ctx.c_buff + idx, &len);
  61692. idx += OPAQUE16_LEN;
  61693. maxIdx = idx + (int)len;
  61694. for (; idx < maxIdx && EXPECT_SUCCESS(); idx += OPAQUE16_LEN) {
  61695. if (test_ctx.c_buff[idx+1] == ED25519_SA_MINOR ||
  61696. test_ctx.c_buff[idx+1] == ED448_SA_MINOR)
  61697. ExpectIntEQ(test_ctx.c_buff[idx], NEW_SA_MAJOR);
  61698. else
  61699. ExpectIntEQ(test_ctx.c_buff[idx+1], ecc_dsa_sa_algo);
  61700. }
  61701. break;
  61702. }
  61703. else {
  61704. idx += (int)len;
  61705. }
  61706. }
  61707. ExpectIntLT(idx, test_ctx.c_len);
  61708. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61709. wolfSSL_free(ssl_c);
  61710. wolfSSL_free(ssl_s);
  61711. wolfSSL_CTX_free(ctx_c);
  61712. wolfSSL_CTX_free(ctx_s);
  61713. #endif
  61714. return EXPECT_RESULT();
  61715. }
  61716. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  61717. static int test_revoked_loaded_int_cert_ctx_ready1(WOLFSSL_CTX* ctx)
  61718. {
  61719. EXPECT_DECLS;
  61720. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  61721. myVerifyAction = VERIFY_USE_PREVERFIY;
  61722. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  61723. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  61724. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  61725. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  61726. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  61727. WOLFSSL_SUCCESS);
  61728. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  61729. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  61730. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61731. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  61732. "./certs/crl/ca-int.pem",
  61733. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61734. return EXPECT_RESULT();
  61735. }
  61736. static int test_revoked_loaded_int_cert_ctx_ready2(WOLFSSL_CTX* ctx)
  61737. {
  61738. EXPECT_DECLS;
  61739. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  61740. myVerifyAction = VERIFY_USE_PREVERFIY;
  61741. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  61742. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  61743. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  61744. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  61745. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  61746. "./certs/intermediate/ca-int2-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  61747. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  61748. WOLFSSL_SUCCESS);
  61749. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  61750. "./certs/crl/ca-int2.pem",
  61751. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61752. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  61753. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  61754. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61755. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  61756. "./certs/crl/ca-int.pem",
  61757. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61758. return EXPECT_RESULT();
  61759. }
  61760. #endif
  61761. static int test_revoked_loaded_int_cert(void)
  61762. {
  61763. EXPECT_DECLS;
  61764. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  61765. test_ssl_cbf client_cbf;
  61766. test_ssl_cbf server_cbf;
  61767. struct {
  61768. const char* certPemFile;
  61769. const char* keyPemFile;
  61770. ctx_cb client_ctx_ready;
  61771. } test_params[] = {
  61772. {"./certs/intermediate/ca-int2-cert.pem",
  61773. "./certs/intermediate/ca-int2-key.pem",
  61774. test_revoked_loaded_int_cert_ctx_ready1},
  61775. {"./certs/intermediate/server-chain.pem",
  61776. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  61777. {"./certs/intermediate/server-chain-short.pem",
  61778. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  61779. };
  61780. size_t i;
  61781. printf("\n");
  61782. for (i = 0; i < XELEM_CNT(test_params); i++) {
  61783. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  61784. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  61785. printf("\tTesting with %s...\n", test_params[i].certPemFile);
  61786. server_cbf.certPemFile = test_params[i].certPemFile;
  61787. server_cbf.keyPemFile = test_params[i].keyPemFile;
  61788. client_cbf.ctx_ready = test_params[i].client_ctx_ready;
  61789. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  61790. &server_cbf, NULL), TEST_FAIL);
  61791. #ifndef WOLFSSL_HAPROXY
  61792. ExpectIntEQ(client_cbf.last_err, CRL_CERT_REVOKED);
  61793. #else
  61794. ExpectIntEQ(client_cbf.last_err, WOLFSSL_X509_V_ERR_CERT_REVOKED);
  61795. #endif
  61796. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  61797. if (!EXPECT_SUCCESS())
  61798. break;
  61799. printf("\t%s passed\n", test_params[i].certPemFile);
  61800. }
  61801. #endif
  61802. return EXPECT_RESULT();
  61803. }
  61804. static int test_dtls13_frag_ch_pq(void)
  61805. {
  61806. EXPECT_DECLS;
  61807. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  61808. && defined(WOLFSSL_DTLS_CH_FRAG) && defined(HAVE_LIBOQS)
  61809. WOLFSSL_CTX *ctx_c = NULL;
  61810. WOLFSSL_CTX *ctx_s = NULL;
  61811. WOLFSSL *ssl_c = NULL;
  61812. WOLFSSL *ssl_s = NULL;
  61813. struct test_memio_ctx test_ctx;
  61814. const char *test_str = "test";
  61815. int test_str_size;
  61816. byte buf[255];
  61817. int group = WOLFSSL_KYBER_LEVEL5;
  61818. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61819. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61820. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  61821. /* Add in a large post-quantum key share to make the CH long. */
  61822. ExpectIntEQ(wolfSSL_set_groups(ssl_c, &group, 1), WOLFSSL_SUCCESS);
  61823. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, group), WOLFSSL_SUCCESS);
  61824. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  61825. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61826. ExpectStrEQ(wolfSSL_get_curve_name(ssl_c), "KYBER_LEVEL5");
  61827. ExpectStrEQ(wolfSSL_get_curve_name(ssl_s), "KYBER_LEVEL5");
  61828. test_str_size = XSTRLEN("test") + 1;
  61829. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  61830. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  61831. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  61832. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  61833. wolfSSL_free(ssl_c);
  61834. wolfSSL_free(ssl_s);
  61835. wolfSSL_CTX_free(ctx_c);
  61836. wolfSSL_CTX_free(ctx_s);
  61837. #endif
  61838. return EXPECT_RESULT();
  61839. }
  61840. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  61841. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  61842. static int test_dtls_frag_ch_count_records(byte* b, int len)
  61843. {
  61844. DtlsRecordLayerHeader* dtlsRH;
  61845. int records = 0;
  61846. size_t recordLen;
  61847. while (len > 0) {
  61848. records++;
  61849. dtlsRH = (DtlsRecordLayerHeader*)b;
  61850. recordLen = (dtlsRH->length[0] << 8) | dtlsRH->length[1];
  61851. b += sizeof(DtlsRecordLayerHeader) + recordLen;
  61852. len -= sizeof(DtlsRecordLayerHeader) + recordLen;
  61853. }
  61854. return records;
  61855. }
  61856. #endif
  61857. static int test_dtls_frag_ch(void)
  61858. {
  61859. EXPECT_DECLS;
  61860. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  61861. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  61862. WOLFSSL_CTX *ctx_c = NULL;
  61863. WOLFSSL_CTX *ctx_s = NULL;
  61864. WOLFSSL *ssl_c = NULL;
  61865. WOLFSSL *ssl_s = NULL;
  61866. struct test_memio_ctx test_ctx;
  61867. static unsigned int DUMMY_MTU = 256;
  61868. unsigned char four_frag_CH[] = {
  61869. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61870. 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61871. 0xce, 0xfe, 0xfd, 0xf3, 0x94, 0x01, 0x33, 0x2c, 0xcf, 0x2c, 0x47, 0xb1,
  61872. 0xe5, 0xa1, 0x7b, 0x19, 0x3e, 0xac, 0x68, 0xdd, 0xe6, 0x17, 0x6b, 0x85,
  61873. 0xad, 0x5f, 0xfc, 0x7f, 0x6e, 0xf0, 0xb9, 0xe0, 0x2e, 0xca, 0x47, 0x00,
  61874. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  61875. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  61876. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  61877. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  61878. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x02,
  61879. 0x7c, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  61880. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  61881. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  61882. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  61883. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  61884. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x02, 0x39, 0x02, 0x37, 0x00, 0x17,
  61885. 0x00, 0x41, 0x04, 0x94, 0xdf, 0x36, 0xd7, 0xb3, 0x90, 0x6d, 0x01, 0xa1,
  61886. 0xe6, 0xed, 0x67, 0xf4, 0xd9, 0x9d, 0x2c, 0xac, 0x57, 0x74, 0xff, 0x19,
  61887. 0xbe, 0x5a, 0xc9, 0x30, 0x11, 0xb7, 0x2b, 0x59, 0x47, 0x80, 0x7c, 0xa9,
  61888. 0xb7, 0x31, 0x8c, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61889. 0x00, 0x01, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00,
  61890. 0xce, 0x00, 0x00, 0xce, 0x9e, 0x13, 0x74, 0x3b, 0x86, 0xba, 0x69, 0x1f,
  61891. 0x12, 0xf7, 0xcd, 0x78, 0x53, 0xe8, 0x50, 0x4d, 0x71, 0x3f, 0x4b, 0x4e,
  61892. 0xeb, 0x3e, 0xe5, 0x43, 0x54, 0x78, 0x17, 0x6d, 0x00, 0x18, 0x00, 0x61,
  61893. 0x04, 0xd1, 0x99, 0x66, 0x4f, 0xda, 0xc7, 0x12, 0x3b, 0xff, 0xb2, 0xd6,
  61894. 0x2f, 0x35, 0xb6, 0x17, 0x1f, 0xb3, 0xd0, 0xb6, 0x52, 0xff, 0x97, 0x8b,
  61895. 0x01, 0xe8, 0xd9, 0x68, 0x71, 0x40, 0x02, 0xd5, 0x68, 0x3a, 0x58, 0xb2,
  61896. 0x5d, 0xee, 0xa4, 0xe9, 0x5f, 0xf4, 0xaf, 0x3e, 0x30, 0x9c, 0x3e, 0x2b,
  61897. 0xda, 0x61, 0x43, 0x99, 0x02, 0x35, 0x33, 0x9f, 0xcf, 0xb5, 0xd3, 0x28,
  61898. 0x19, 0x9d, 0x1c, 0xbe, 0x69, 0x07, 0x9e, 0xfc, 0xe4, 0x8e, 0xcd, 0x86,
  61899. 0x4a, 0x1b, 0xf0, 0xfc, 0x17, 0x94, 0x66, 0x53, 0xda, 0x24, 0x5e, 0xaf,
  61900. 0xce, 0xec, 0x62, 0x4c, 0x06, 0xb4, 0x52, 0x94, 0xb1, 0x4a, 0x7a, 0x8c,
  61901. 0x4f, 0x00, 0x19, 0x00, 0x85, 0x04, 0x00, 0x27, 0xeb, 0x99, 0x49, 0x7f,
  61902. 0xcb, 0x2c, 0x46, 0x54, 0x2d, 0x93, 0x5d, 0x25, 0x92, 0x58, 0x5e, 0x06,
  61903. 0xc3, 0x7c, 0xfb, 0x9a, 0xa7, 0xec, 0xcd, 0x9f, 0xe1, 0x6b, 0x2d, 0x78,
  61904. 0xf5, 0x16, 0xa9, 0x20, 0x52, 0x48, 0x19, 0x0f, 0x1a, 0xd0, 0xce, 0xd8,
  61905. 0x68, 0xb1, 0x4e, 0x7f, 0x33, 0x03, 0x7d, 0x0c, 0x39, 0xdb, 0x9c, 0x4b,
  61906. 0xf4, 0xe7, 0xc2, 0xf5, 0xdd, 0x51, 0x9b, 0x03, 0xa8, 0x53, 0x2b, 0xe6,
  61907. 0x00, 0x15, 0x4b, 0xff, 0xd2, 0xa0, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00,
  61908. 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00,
  61909. 0x00, 0x00, 0x01, 0x9c, 0x00, 0x00, 0xce, 0x58, 0x30, 0x10, 0x3d, 0x46,
  61910. 0xcc, 0xca, 0x1a, 0x44, 0xc8, 0x58, 0x9b, 0x27, 0x17, 0x67, 0x31, 0x96,
  61911. 0x8a, 0x66, 0x39, 0xf4, 0xcc, 0xc1, 0x9f, 0x12, 0x1f, 0x01, 0x30, 0x50,
  61912. 0x16, 0xd6, 0x89, 0x97, 0xa3, 0x66, 0xd7, 0x99, 0x50, 0x09, 0x6e, 0x80,
  61913. 0x87, 0xe4, 0xa2, 0x88, 0xae, 0xb4, 0x23, 0x57, 0x2f, 0x12, 0x60, 0xe7,
  61914. 0x7d, 0x44, 0x2d, 0xad, 0xbe, 0xe9, 0x0d, 0x01, 0x00, 0x01, 0x00, 0xd5,
  61915. 0xdd, 0x62, 0xee, 0xf3, 0x0e, 0xd9, 0x30, 0x0e, 0x38, 0xf3, 0x48, 0xf4,
  61916. 0xc9, 0x8f, 0x8c, 0x20, 0xf7, 0xd3, 0xa8, 0xb3, 0x87, 0x3c, 0x98, 0x5d,
  61917. 0x70, 0xc5, 0x03, 0x76, 0xb7, 0xd5, 0x0b, 0x7b, 0x23, 0x97, 0x6b, 0xe3,
  61918. 0xb5, 0x18, 0xeb, 0x64, 0x55, 0x18, 0xb2, 0x8a, 0x90, 0x1a, 0x8f, 0x0e,
  61919. 0x15, 0xda, 0xb1, 0x8e, 0x7f, 0xee, 0x1f, 0xe0, 0x3b, 0xb9, 0xed, 0xfc,
  61920. 0x4e, 0x3f, 0x78, 0x16, 0x39, 0x95, 0x5f, 0xb7, 0xcb, 0x65, 0x55, 0x72,
  61921. 0x7b, 0x7d, 0x86, 0x2f, 0x8a, 0xe5, 0xee, 0xf7, 0x57, 0x40, 0xf3, 0xc4,
  61922. 0x96, 0x4f, 0x11, 0x4d, 0x85, 0xf9, 0x56, 0xfa, 0x3d, 0xf0, 0xc9, 0xa4,
  61923. 0xec, 0x1e, 0xaa, 0x47, 0x90, 0x53, 0xdf, 0xe1, 0xb7, 0x78, 0x18, 0xeb,
  61924. 0xdd, 0x0d, 0x89, 0xb7, 0xf6, 0x15, 0x0e, 0x55, 0x12, 0xb3, 0x23, 0x17,
  61925. 0x0b, 0x59, 0x6f, 0x83, 0x05, 0x6b, 0xa6, 0xf8, 0x6c, 0x3a, 0x9b, 0x1b,
  61926. 0x50, 0x93, 0x51, 0xea, 0x95, 0x2d, 0x99, 0x96, 0x38, 0x16, 0xfe, 0xfd,
  61927. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x7e, 0x01, 0x00,
  61928. 0x02, 0xdc, 0x00, 0x00, 0x00, 0x02, 0x6a, 0x00, 0x00, 0x72, 0x2d, 0x66,
  61929. 0x3e, 0xf2, 0x36, 0x5a, 0xf2, 0x23, 0x8f, 0x28, 0x09, 0xa9, 0x55, 0x8c,
  61930. 0x8f, 0xc0, 0x0d, 0x61, 0x98, 0x33, 0x56, 0x87, 0x7a, 0xfd, 0xa7, 0x50,
  61931. 0x71, 0x84, 0x2e, 0x41, 0x58, 0x00, 0x87, 0xd9, 0x27, 0xe5, 0x7b, 0xf4,
  61932. 0x6d, 0x84, 0x4e, 0x2e, 0x0c, 0x80, 0x0c, 0xf3, 0x8a, 0x02, 0x4b, 0x99,
  61933. 0x3a, 0x1f, 0x9f, 0x18, 0x7d, 0x1c, 0xec, 0xad, 0x60, 0x54, 0xa6, 0xa3,
  61934. 0x2c, 0x82, 0x5e, 0xf8, 0x8f, 0xae, 0xe1, 0xc4, 0x82, 0x7e, 0x43, 0x43,
  61935. 0xc5, 0x99, 0x49, 0x05, 0xd3, 0xf6, 0xdf, 0xa1, 0xb5, 0x2d, 0x0c, 0x13,
  61936. 0x2f, 0x1e, 0xb6, 0x28, 0x7c, 0x5c, 0xa1, 0x02, 0x6b, 0x8d, 0xa3, 0xeb,
  61937. 0xd4, 0x58, 0xe6, 0xa0, 0x7e, 0x6b, 0xaa, 0x09, 0x43, 0x67, 0x71, 0x87,
  61938. 0xa5, 0xcb, 0x68, 0xf3
  61939. };
  61940. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61941. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61942. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  61943. /* Fragment msgs */
  61944. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_c, DUMMY_MTU), WOLFSSL_SUCCESS);
  61945. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_s, DUMMY_MTU), WOLFSSL_SUCCESS);
  61946. /* Add in some key shares to make the CH long */
  61947. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP256R1),
  61948. WOLFSSL_SUCCESS);
  61949. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP384R1),
  61950. WOLFSSL_SUCCESS);
  61951. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP521R1),
  61952. WOLFSSL_SUCCESS);
  61953. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_FFDHE_2048),
  61954. WOLFSSL_SUCCESS);
  61955. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  61956. /* Reject fragmented first CH */
  61957. ExpectIntEQ(test_dtls_frag_ch_count_records(four_frag_CH,
  61958. sizeof(four_frag_CH)), 4);
  61959. XMEMCPY(test_ctx.s_buff, four_frag_CH, sizeof(four_frag_CH));
  61960. test_ctx.s_len = sizeof(four_frag_CH);
  61961. while (test_ctx.s_len > 0 && EXPECT_SUCCESS()) {
  61962. int s_len = test_ctx.s_len;
  61963. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61964. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61965. /* Fail if we didn't advance the buffer to avoid infinite loops */
  61966. ExpectIntLT(test_ctx.s_len, s_len);
  61967. }
  61968. /* Expect all fragments read */
  61969. ExpectIntEQ(test_ctx.s_len, 0);
  61970. /* Expect quietly dropping fragmented first CH */
  61971. ExpectIntEQ(test_ctx.c_len, 0);
  61972. /* CH1 */
  61973. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61974. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61975. /* Count records. Expect 1 unfragmented CH */
  61976. ExpectIntEQ(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  61977. test_ctx.s_len), 1);
  61978. /* HRR */
  61979. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61980. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61981. /* CH2 */
  61982. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61983. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61984. /* Count records. Expect fragmented CH */
  61985. ExpectIntGT(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  61986. test_ctx.s_len), 1);
  61987. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61988. wolfSSL_free(ssl_c);
  61989. wolfSSL_free(ssl_s);
  61990. wolfSSL_CTX_free(ctx_c);
  61991. wolfSSL_CTX_free(ctx_s);
  61992. ssl_c = ssl_s = NULL;
  61993. ctx_c = ctx_s = NULL;
  61994. #endif
  61995. return EXPECT_RESULT();
  61996. }
  61997. static int test_dtls_empty_keyshare_with_cookie(void)
  61998. {
  61999. EXPECT_DECLS;
  62000. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  62001. WOLFSSL_CTX *ctx_s = NULL;
  62002. WOLFSSL *ssl_s = NULL;
  62003. struct test_memio_ctx test_ctx;
  62004. unsigned char ch_empty_keyshare_with_cookie[] = {
  62005. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  62006. 0x12, 0x01, 0x00, 0x01, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
  62007. 0x06, 0xfe, 0xfd, 0xfb, 0x8c, 0x9b, 0x28, 0xae, 0x50, 0x1c, 0x4d, 0xf3,
  62008. 0xb8, 0xcf, 0x4d, 0xd8, 0x7e, 0x93, 0x13, 0x7b, 0x9e, 0xd9, 0xeb, 0xe9,
  62009. 0x13, 0x4b, 0x0d, 0x7f, 0x2e, 0x43, 0x62, 0x8c, 0xe4, 0x57, 0x79, 0x00,
  62010. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  62011. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  62012. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  62013. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  62014. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x00,
  62015. 0xa6, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x2c, 0x00, 0x47,
  62016. 0x00, 0x45, 0x20, 0xee, 0x4b, 0x17, 0x70, 0x63, 0xa0, 0x4c, 0x82, 0xbf,
  62017. 0x43, 0x01, 0x7d, 0x8d, 0xc1, 0x1b, 0x4e, 0x9b, 0xa0, 0x3c, 0x53, 0x1f,
  62018. 0xb7, 0xd1, 0x10, 0x81, 0xa8, 0xdf, 0xdf, 0x8c, 0x7f, 0xf3, 0x11, 0x13,
  62019. 0x01, 0x02, 0x3d, 0x3b, 0x7d, 0x14, 0x2c, 0x31, 0xb3, 0x60, 0x72, 0x4d,
  62020. 0xe5, 0x1a, 0xb2, 0xa3, 0x61, 0x77, 0x73, 0x03, 0x40, 0x0e, 0x5f, 0xc5,
  62021. 0x61, 0x38, 0x43, 0x56, 0x21, 0x4a, 0x95, 0xd5, 0x35, 0xa8, 0x0d, 0x00,
  62022. 0x0d, 0x00, 0x2a, 0x00, 0x28, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02,
  62023. 0x03, 0xfe, 0x0b, 0xfe, 0x0e, 0xfe, 0xa0, 0xfe, 0xa3, 0xfe, 0xa5, 0x08,
  62024. 0x06, 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06,
  62025. 0x01, 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00,
  62026. 0x18, 0x00, 0x16, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01,
  62027. 0x00, 0x02, 0x3a, 0x02, 0x3c, 0x02, 0x3d, 0x2f, 0x3a, 0x2f, 0x3c, 0x2f,
  62028. 0x3d, 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x00, 0x02, 0x00, 0x00
  62029. };
  62030. DtlsRecordLayerHeader* dtlsRH;
  62031. byte sequence_number[8];
  62032. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  62033. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62034. XMEMCPY(test_ctx.s_buff, ch_empty_keyshare_with_cookie,
  62035. sizeof(ch_empty_keyshare_with_cookie));
  62036. test_ctx.s_len = sizeof(ch_empty_keyshare_with_cookie);
  62037. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  62038. NULL, wolfDTLSv1_3_server_method), 0);
  62039. /* CH1 */
  62040. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62041. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62042. /* Expect an alert. A plaintext alert should be exactly 15 bytes. */
  62043. ExpectIntEQ(test_ctx.c_len, 15);
  62044. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  62045. ExpectIntEQ(dtlsRH->type, alert);
  62046. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  62047. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  62048. sequence_number[7] = 1;
  62049. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  62050. sizeof(sequence_number)), 0);
  62051. ExpectIntEQ(dtlsRH->length[0], 0);
  62052. ExpectIntEQ(dtlsRH->length[1], 2);
  62053. ExpectIntEQ(test_ctx.c_buff[13], alert_fatal);
  62054. ExpectIntEQ(test_ctx.c_buff[14], illegal_parameter);
  62055. wolfSSL_free(ssl_s);
  62056. wolfSSL_CTX_free(ctx_s);
  62057. #endif
  62058. return EXPECT_RESULT();
  62059. }
  62060. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  62061. defined(HAVE_LIBOQS)
  62062. static void test_tls13_pq_groups_ctx_ready(WOLFSSL_CTX* ctx)
  62063. {
  62064. int group = WOLFSSL_KYBER_LEVEL5;
  62065. AssertIntEQ(wolfSSL_CTX_set_groups(ctx, &group, 1), WOLFSSL_SUCCESS);
  62066. }
  62067. static void test_tls13_pq_groups_on_result(WOLFSSL* ssl)
  62068. {
  62069. AssertStrEQ(wolfSSL_get_curve_name(ssl), "KYBER_LEVEL5");
  62070. }
  62071. #endif
  62072. static int test_tls13_pq_groups(void)
  62073. {
  62074. EXPECT_DECLS;
  62075. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  62076. defined(HAVE_LIBOQS)
  62077. callback_functions func_cb_client;
  62078. callback_functions func_cb_server;
  62079. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  62080. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  62081. func_cb_client.method = wolfTLSv1_3_client_method;
  62082. func_cb_server.method = wolfTLSv1_3_server_method;
  62083. func_cb_client.ctx_ready = test_tls13_pq_groups_ctx_ready;
  62084. func_cb_client.on_result = test_tls13_pq_groups_on_result;
  62085. func_cb_server.on_result = test_tls13_pq_groups_on_result;
  62086. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  62087. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  62088. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  62089. #endif
  62090. return EXPECT_RESULT();
  62091. }
  62092. static int test_tls13_early_data(void)
  62093. {
  62094. EXPECT_DECLS;
  62095. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  62096. defined(WOLFSSL_EARLY_DATA) && defined(HAVE_SESSION_TICKET)
  62097. int written = 0;
  62098. int read = 0;
  62099. size_t i;
  62100. int splitEarlyData;
  62101. char msg[] = "This is early data";
  62102. char msg2[] = "This is client data";
  62103. char msg3[] = "This is server data";
  62104. char msg4[] = "This is server immediate data";
  62105. char msgBuf[50];
  62106. struct {
  62107. method_provider client_meth;
  62108. method_provider server_meth;
  62109. const char* tls_version;
  62110. int isUdp;
  62111. } params[] = {
  62112. #ifdef WOLFSSL_TLS13
  62113. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  62114. "TLS 1.3", 0 },
  62115. #endif
  62116. #ifdef WOLFSSL_DTLS13
  62117. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  62118. "DTLS 1.3", 1 },
  62119. #endif
  62120. };
  62121. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  62122. for (splitEarlyData = 0; splitEarlyData < 2; splitEarlyData++) {
  62123. struct test_memio_ctx test_ctx;
  62124. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  62125. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  62126. WOLFSSL_SESSION *sess = NULL;
  62127. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62128. fprintf(stderr, "\tEarly data with %s\n", params[i].tls_version);
  62129. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  62130. &ssl_s, params[i].client_meth, params[i].server_meth), 0);
  62131. /* Get a ticket so that we can do 0-RTT on the next connection */
  62132. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62133. /* Make sure we read the ticket */
  62134. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), -1);
  62135. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62136. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  62137. wolfSSL_free(ssl_c);
  62138. ssl_c = NULL;
  62139. wolfSSL_free(ssl_s);
  62140. ssl_s = NULL;
  62141. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62142. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62143. params[i].client_meth, params[i].server_meth), 0);
  62144. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  62145. #ifdef WOLFSSL_DTLS13
  62146. if (params[i].isUdp) {
  62147. #ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME
  62148. ExpectIntEQ(wolfSSL_dtls13_no_hrr_on_resume(ssl_s, 1), WOLFSSL_SUCCESS);
  62149. #else
  62150. /* Let's test this but we generally don't recommend turning off the
  62151. * cookie exchange */
  62152. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  62153. #endif
  62154. }
  62155. #endif
  62156. /* Test 0-RTT data */
  62157. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  62158. &written), sizeof(msg));
  62159. ExpectIntEQ(written, sizeof(msg));
  62160. if (splitEarlyData) {
  62161. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  62162. &written), sizeof(msg));
  62163. ExpectIntEQ(written, sizeof(msg));
  62164. }
  62165. /* Read first 0-RTT data (if split otherwise entire data) */
  62166. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  62167. &read), sizeof(msg));
  62168. ExpectIntEQ(read, sizeof(msg));
  62169. ExpectStrEQ(msg, msgBuf);
  62170. /* Test 0.5-RTT data */
  62171. ExpectIntEQ(wolfSSL_write(ssl_s, msg4, sizeof(msg4)), sizeof(msg4));
  62172. if (splitEarlyData) {
  62173. /* Read second 0-RTT data */
  62174. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  62175. &read), sizeof(msg));
  62176. ExpectIntEQ(read, sizeof(msg));
  62177. ExpectStrEQ(msg, msgBuf);
  62178. }
  62179. if (params[i].isUdp) {
  62180. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62181. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), APP_DATA_READY);
  62182. /* Read server 0.5-RTT data */
  62183. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  62184. ExpectStrEQ(msg4, msgBuf);
  62185. /* Complete handshake */
  62186. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62187. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62188. /* Use wolfSSL_is_init_finished to check if handshake is complete. Normally
  62189. * a user would loop until it is true but here we control both sides so we
  62190. * just assert the expected value. wolfSSL_read_early_data does not provide
  62191. * handshake status to us with non-blocking IO and we can't use
  62192. * wolfSSL_accept as TLS layer may return ZERO_RETURN due to early data
  62193. * parsing logic. */
  62194. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  62195. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  62196. &read), 0);
  62197. ExpectIntEQ(read, 0);
  62198. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  62199. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  62200. }
  62201. else {
  62202. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  62203. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  62204. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  62205. &read), 0);
  62206. ExpectIntEQ(read, 0);
  62207. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  62208. /* Read server 0.5-RTT data */
  62209. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  62210. ExpectStrEQ(msg4, msgBuf);
  62211. }
  62212. /* Test bi-directional write */
  62213. ExpectIntEQ(wolfSSL_write(ssl_c, msg2, sizeof(msg2)), sizeof(msg2));
  62214. ExpectIntEQ(wolfSSL_read(ssl_s, msgBuf, sizeof(msgBuf)), sizeof(msg2));
  62215. ExpectStrEQ(msg2, msgBuf);
  62216. ExpectIntEQ(wolfSSL_write(ssl_s, msg3, sizeof(msg3)), sizeof(msg3));
  62217. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg3));
  62218. ExpectStrEQ(msg3, msgBuf);
  62219. ExpectTrue(wolfSSL_session_reused(ssl_c));
  62220. ExpectTrue(wolfSSL_session_reused(ssl_s));
  62221. wolfSSL_SESSION_free(sess);
  62222. wolfSSL_free(ssl_c);
  62223. wolfSSL_free(ssl_s);
  62224. wolfSSL_CTX_free(ctx_c);
  62225. wolfSSL_CTX_free(ctx_s);
  62226. }
  62227. }
  62228. #endif
  62229. return EXPECT_RESULT();
  62230. }
  62231. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  62232. static int test_self_signed_stapling_client_v1_ctx_ready(WOLFSSL_CTX* ctx)
  62233. {
  62234. EXPECT_DECLS;
  62235. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  62236. ExpectIntEQ(wolfSSL_CTX_UseOCSPStapling(ctx, WOLFSSL_CSR_OCSP,
  62237. WOLFSSL_CSR_OCSP_USE_NONCE), 1);
  62238. return EXPECT_RESULT();
  62239. }
  62240. #endif
  62241. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  62242. static int test_self_signed_stapling_client_v2_ctx_ready(WOLFSSL_CTX* ctx)
  62243. {
  62244. EXPECT_DECLS;
  62245. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  62246. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP,
  62247. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  62248. return EXPECT_RESULT();
  62249. }
  62250. static int test_self_signed_stapling_client_v2_multi_ctx_ready(WOLFSSL_CTX* ctx)
  62251. {
  62252. EXPECT_DECLS;
  62253. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  62254. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP_MULTI,
  62255. 0), 1);
  62256. return EXPECT_RESULT();
  62257. }
  62258. #endif
  62259. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  62260. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  62261. static int test_self_signed_stapling_server_ctx_ready(WOLFSSL_CTX* ctx)
  62262. {
  62263. EXPECT_DECLS;
  62264. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  62265. return EXPECT_RESULT();
  62266. }
  62267. #endif
  62268. static int test_self_signed_stapling(void)
  62269. {
  62270. EXPECT_DECLS;
  62271. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  62272. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  62273. test_ssl_cbf client_cbf;
  62274. test_ssl_cbf server_cbf;
  62275. size_t i;
  62276. struct {
  62277. method_provider client_meth;
  62278. method_provider server_meth;
  62279. ctx_cb client_ctx;
  62280. const char* tls_version;
  62281. } params[] = {
  62282. #if defined(WOLFSSL_TLS13) && defined(HAVE_CERTIFICATE_STATUS_REQUEST)
  62283. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  62284. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_3 v1" },
  62285. #endif
  62286. #ifndef WOLFSSL_NO_TLS12
  62287. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  62288. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  62289. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_2 v1" },
  62290. #endif
  62291. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  62292. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  62293. test_self_signed_stapling_client_v2_ctx_ready, "TLSv1_2 v2" },
  62294. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  62295. test_self_signed_stapling_client_v2_multi_ctx_ready,
  62296. "TLSv1_2 v2 multi" },
  62297. #endif
  62298. #endif
  62299. };
  62300. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  62301. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  62302. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  62303. printf("\nTesting self-signed cert with status request: %s\n",
  62304. params[i].tls_version);
  62305. client_cbf.method = params[i].client_meth;
  62306. client_cbf.ctx_ready = params[i].client_ctx;
  62307. server_cbf.method = params[i].server_meth;
  62308. server_cbf.certPemFile = "certs/ca-cert.pem";
  62309. server_cbf.keyPemFile = "certs/ca-key.pem";
  62310. server_cbf.ctx_ready = test_self_signed_stapling_server_ctx_ready;
  62311. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  62312. &server_cbf, NULL), TEST_SUCCESS);
  62313. }
  62314. #endif
  62315. return EXPECT_RESULT();
  62316. }
  62317. static int test_tls_multi_handshakes_one_record(void)
  62318. {
  62319. EXPECT_DECLS;
  62320. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  62321. struct test_memio_ctx test_ctx;
  62322. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  62323. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  62324. RecordLayerHeader* rh = NULL;
  62325. byte *len ;
  62326. int newRecIdx = RECORD_HEADER_SZ;
  62327. int idx = 0;
  62328. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62329. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62330. wolfTLS_client_method, wolfTLSv1_2_server_method), 0);
  62331. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62332. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62333. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  62334. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62335. /* Combine server handshake msgs into one record */
  62336. while (idx < test_ctx.c_len) {
  62337. word16 recLen;
  62338. rh = (RecordLayerHeader*)(test_ctx.c_buff + idx);
  62339. len = &rh->length[0];
  62340. ato16((const byte*)len, &recLen);
  62341. idx += RECORD_HEADER_SZ;
  62342. XMEMMOVE(test_ctx.c_buff + newRecIdx, test_ctx.c_buff + idx,
  62343. (size_t)recLen);
  62344. newRecIdx += recLen;
  62345. idx += recLen;
  62346. }
  62347. rh = (RecordLayerHeader*)(test_ctx.c_buff);
  62348. len = &rh->length[0];
  62349. c16toa(newRecIdx - RECORD_HEADER_SZ, len);
  62350. test_ctx.c_len = newRecIdx;
  62351. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62352. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62353. wolfSSL_free(ssl_c);
  62354. wolfSSL_free(ssl_s);
  62355. wolfSSL_CTX_free(ctx_c);
  62356. wolfSSL_CTX_free(ctx_s);
  62357. #endif
  62358. return EXPECT_RESULT();
  62359. }
  62360. static int test_write_dup(void)
  62361. {
  62362. EXPECT_DECLS;
  62363. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_WRITE_DUP)
  62364. size_t i, j;
  62365. char hiWorld[] = "dup message";
  62366. char readData[sizeof(hiWorld) + 5];
  62367. struct {
  62368. method_provider client_meth;
  62369. method_provider server_meth;
  62370. const char* version_name;
  62371. int version;
  62372. } methods[] = {
  62373. #ifndef WOLFSSL_NO_TLS12
  62374. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLS 1.2", WOLFSSL_TLSV1_2},
  62375. #endif
  62376. #ifdef WOLFSSL_TLS13
  62377. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLS 1.3", WOLFSSL_TLSV1_3},
  62378. #endif
  62379. };
  62380. struct {
  62381. const char* cipher;
  62382. int version;
  62383. } ciphers[] = {
  62384. /* For simplicity the macros are copied from internal.h */
  62385. /* TLS 1.2 */
  62386. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && !defined(NO_SHA256)
  62387. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  62388. #ifndef NO_RSA
  62389. {"ECDHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  62390. #endif
  62391. #endif
  62392. #if !defined(NO_DH) && !defined(NO_RSA) && !defined(NO_TLS_DH)
  62393. {"DHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  62394. #endif
  62395. #endif
  62396. #if !defined(NO_DH) && !defined(NO_AES) && !defined(NO_TLS) && \
  62397. !defined(NO_RSA) && defined(HAVE_AESGCM) && !defined(NO_TLS_DH)
  62398. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  62399. {"DHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  62400. #endif
  62401. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  62402. {"DHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  62403. #endif
  62404. #endif
  62405. #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)) \
  62406. && !defined(NO_TLS) && !defined(NO_AES)
  62407. #ifdef HAVE_AESGCM
  62408. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  62409. #ifndef NO_RSA
  62410. {"ECDHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  62411. #endif
  62412. #endif
  62413. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  62414. #ifndef NO_RSA
  62415. {"ECDHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  62416. #endif
  62417. #endif
  62418. #endif
  62419. #endif
  62420. /* TLS 1.3 */
  62421. #ifdef WOLFSSL_TLS13
  62422. #ifdef HAVE_AESGCM
  62423. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  62424. {"TLS13-AES128-GCM-SHA256", WOLFSSL_TLSV1_3},
  62425. #endif
  62426. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  62427. {"TLS13-AES256-GCM-SHA384", WOLFSSL_TLSV1_3},
  62428. #endif
  62429. #endif
  62430. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  62431. #ifndef NO_SHA256
  62432. {"TLS13-CHACHA20-POLY1305-SHA256", WOLFSSL_TLSV1_3},
  62433. #endif
  62434. #endif
  62435. #ifdef HAVE_AESCCM
  62436. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  62437. {"TLS13-AES128-CCM-SHA256", WOLFSSL_TLSV1_3},
  62438. #endif
  62439. #endif
  62440. #endif
  62441. };
  62442. for (i = 0; i < XELEM_CNT(methods); i++) {
  62443. for (j = 0; j < XELEM_CNT(ciphers) && !EXPECT_FAIL(); j++) {
  62444. struct test_memio_ctx test_ctx;
  62445. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  62446. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  62447. WOLFSSL *ssl_c2 = NULL;
  62448. if (methods[i].version != ciphers[j].version)
  62449. continue;
  62450. if (i == 0 && j == 0)
  62451. printf("\n");
  62452. printf("Testing %s with %s... ", methods[i].version_name,
  62453. ciphers[j].cipher);
  62454. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62455. test_ctx.c_ciphers = test_ctx.s_ciphers = ciphers[j].cipher;
  62456. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62457. methods[i].client_meth, methods[i].server_meth), 0);
  62458. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62459. ExpectNotNull(ssl_c2 = wolfSSL_write_dup(ssl_c));
  62460. ExpectIntEQ(wolfSSL_write(ssl_c, hiWorld, sizeof(hiWorld)),
  62461. WRITE_DUP_WRITE_E);
  62462. ExpectIntEQ(wolfSSL_write(ssl_c2, hiWorld, sizeof(hiWorld)),
  62463. sizeof(hiWorld));
  62464. ExpectIntEQ(wolfSSL_read(ssl_s, readData, sizeof(readData)),
  62465. sizeof(hiWorld));
  62466. ExpectIntEQ(wolfSSL_write(ssl_s, hiWorld, sizeof(hiWorld)),
  62467. sizeof(hiWorld));
  62468. ExpectIntEQ(wolfSSL_read(ssl_c2, readData, sizeof(readData)),
  62469. WRITE_DUP_READ_E);
  62470. ExpectIntEQ(wolfSSL_read(ssl_c, readData, sizeof(readData)),
  62471. sizeof(hiWorld));
  62472. if (EXPECT_SUCCESS())
  62473. printf("ok\n");
  62474. else
  62475. printf("failed\n");
  62476. wolfSSL_free(ssl_c);
  62477. wolfSSL_free(ssl_c2);
  62478. wolfSSL_free(ssl_s);
  62479. wolfSSL_CTX_free(ctx_c);
  62480. wolfSSL_CTX_free(ctx_s);
  62481. }
  62482. }
  62483. #endif
  62484. return EXPECT_RESULT();
  62485. }
  62486. static int test_read_write_hs(void)
  62487. {
  62488. EXPECT_DECLS;
  62489. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  62490. WOLFSSL_CTX *ctx_s = NULL, *ctx_c = NULL;
  62491. WOLFSSL *ssl_s = NULL, *ssl_c = NULL;
  62492. struct test_memio_ctx test_ctx;
  62493. uint8_t test_buffer[16];
  62494. unsigned int test;
  62495. /* test == 0 : client writes, server reads */
  62496. /* test == 1 : server writes, client reads */
  62497. for (test = 0; test < 2; test++) {
  62498. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62499. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62500. wolfTLSv1_2_client_method,
  62501. wolfTLSv1_2_server_method), 0);
  62502. ExpectIntEQ(wolfSSL_set_group_messages(ssl_s), WOLFSSL_SUCCESS);
  62503. /* CH -> */
  62504. if (test == 0) {
  62505. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  62506. } else {
  62507. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  62508. sizeof(test_buffer)), -1);
  62509. }
  62510. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62511. /* <- SH + SKE + SHD */
  62512. if (test == 0) {
  62513. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  62514. sizeof(test_buffer)), -1);
  62515. } else {
  62516. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  62517. }
  62518. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62519. /* -> CKE + CLIENT FINISHED */
  62520. if (test == 0) {
  62521. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  62522. } else {
  62523. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  62524. sizeof(test_buffer)), -1);
  62525. }
  62526. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62527. /* abide clang static analyzer */
  62528. if (ssl_s != NULL) {
  62529. /* disable group message to separate sending of ChangeCipherspec
  62530. * from Finished */
  62531. ssl_s->options.groupMessages = 0;
  62532. }
  62533. /* allow writing of CS, but not FINISHED */
  62534. test_ctx.c_len = TEST_MEMIO_BUF_SZ - 6;
  62535. /* <- CS */
  62536. if (test == 0) {
  62537. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  62538. sizeof(test_buffer)), -1);
  62539. } else {
  62540. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  62541. }
  62542. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_WRITE);
  62543. /* move CS message where the client can read it */
  62544. memmove(test_ctx.c_buff,
  62545. (test_ctx.c_buff + TEST_MEMIO_BUF_SZ - 6), 6);
  62546. test_ctx.c_len = 6;
  62547. /* read CS */
  62548. if (test == 0) {
  62549. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  62550. } else {
  62551. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  62552. sizeof(test_buffer)), -1);
  62553. }
  62554. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62555. ExpectIntEQ(test_ctx.c_len, 0);
  62556. if (test == 0) {
  62557. /* send SERVER FINISHED */
  62558. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  62559. sizeof(test_buffer)), -1);
  62560. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1),
  62561. WOLFSSL_ERROR_WANT_READ);
  62562. } else {
  62563. /* send SERVER FINISHED + App Data */
  62564. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), 5);
  62565. }
  62566. ExpectIntGT(test_ctx.c_len, 0);
  62567. /* Send and receive the data */
  62568. if (test == 0) {
  62569. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), 5);
  62570. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  62571. sizeof(test_buffer)), 5);
  62572. } else {
  62573. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  62574. sizeof(test_buffer)), 5);
  62575. }
  62576. ExpectBufEQ(test_buffer, "hello", 5);
  62577. wolfSSL_free(ssl_c);
  62578. wolfSSL_free(ssl_s);
  62579. wolfSSL_CTX_free(ctx_c);
  62580. wolfSSL_CTX_free(ctx_s);
  62581. ssl_c = ssl_s = NULL;
  62582. ctx_c = ctx_s = NULL;
  62583. }
  62584. #endif
  62585. return EXPECT_RESULT();
  62586. }
  62587. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  62588. static const char* test_get_signature_nid_siglag;
  62589. static int test_get_signature_nid_sig;
  62590. static int test_get_signature_nid_hash;
  62591. static int test_get_signature_nid_ssl_ready(WOLFSSL* ssl)
  62592. {
  62593. EXPECT_DECLS;
  62594. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "ALL"), WOLFSSL_SUCCESS);
  62595. if (!wolfSSL_is_server(ssl)) {
  62596. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  62597. test_get_signature_nid_siglag), WOLFSSL_SUCCESS);
  62598. }
  62599. return EXPECT_RESULT();
  62600. }
  62601. static int test_get_signature_nid_on_hs_client(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  62602. {
  62603. EXPECT_DECLS;
  62604. int nid = 0;
  62605. (void)ctx;
  62606. if (XSTRSTR(wolfSSL_get_cipher(*ssl), "TLS_RSA_") == NULL) {
  62607. ExpectIntEQ(SSL_get_peer_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  62608. ExpectIntEQ(nid, test_get_signature_nid_sig);
  62609. ExpectIntEQ(SSL_get_peer_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  62610. ExpectIntEQ(nid, test_get_signature_nid_hash);
  62611. }
  62612. else /* No sigalg info on static ciphersuite */
  62613. return TEST_SUCCESS;
  62614. return EXPECT_RESULT();
  62615. }
  62616. static int test_get_signature_nid_on_hs_server(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  62617. {
  62618. EXPECT_DECLS;
  62619. int nid = 0;
  62620. (void)ctx;
  62621. ExpectIntEQ(SSL_get_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  62622. ExpectIntEQ(nid, test_get_signature_nid_sig);
  62623. ExpectIntEQ(SSL_get_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  62624. ExpectIntEQ(nid, test_get_signature_nid_hash);
  62625. return EXPECT_RESULT();
  62626. }
  62627. #endif
  62628. static int test_get_signature_nid(void)
  62629. {
  62630. EXPECT_DECLS;
  62631. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  62632. test_ssl_cbf client_cbf;
  62633. test_ssl_cbf server_cbf;
  62634. size_t i;
  62635. #define TGSN_TLS12_RSA(sigalg, sig_nid, hash_nid) \
  62636. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, svrCertFile, svrKeyFile, \
  62637. caCertFile }
  62638. #define TGSN_TLS12_ECDSA(sigalg, sig_nid, hash_nid) \
  62639. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, eccCertFile, eccKeyFile, \
  62640. caEccCertFile }
  62641. #define TGSN_TLS13_RSA(sigalg, sig_nid, hash_nid) \
  62642. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, svrCertFile, svrKeyFile, \
  62643. caCertFile }
  62644. #define TGSN_TLS13_ECDSA(sigalg, sig_nid, hash_nid) \
  62645. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, eccCertFile, eccKeyFile, \
  62646. caEccCertFile }
  62647. #define TGSN_TLS13_ED25519(sigalg, sig_nid, hash_nid) \
  62648. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, edCertFile, edKeyFile, \
  62649. caEdCertFile }
  62650. #define TGSN_TLS13_ED448(sigalg, sig_nid, hash_nid) \
  62651. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, ed448CertFile, ed448KeyFile, \
  62652. caEd448CertFile }
  62653. struct {
  62654. const char* siglag;
  62655. int sig_nid;
  62656. int hash_nid;
  62657. int tls_ver;
  62658. const char* server_cert;
  62659. const char* server_key;
  62660. const char* client_ca;
  62661. } params[] = {
  62662. #ifndef NO_RSA
  62663. #ifndef NO_SHA256
  62664. TGSN_TLS12_RSA("RSA+SHA256", NID_rsaEncryption, NID_sha256),
  62665. #ifdef WC_RSA_PSS
  62666. TGSN_TLS12_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  62667. TGSN_TLS13_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  62668. #endif
  62669. #endif
  62670. #ifdef WOLFSSL_SHA512
  62671. TGSN_TLS12_RSA("RSA+SHA512", NID_rsaEncryption, NID_sha512),
  62672. #ifdef WC_RSA_PSS
  62673. TGSN_TLS12_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  62674. TGSN_TLS13_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  62675. #endif
  62676. #endif
  62677. #endif
  62678. #ifdef HAVE_ECC
  62679. #ifndef NO_SHA256
  62680. TGSN_TLS12_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  62681. TGSN_TLS13_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  62682. #endif
  62683. #endif
  62684. #ifdef HAVE_ED25519
  62685. TGSN_TLS13_ED25519("ED25519", NID_ED25519, NID_sha512),
  62686. #endif
  62687. #ifdef HAVE_ED448
  62688. TGSN_TLS13_ED448("ED448", NID_ED448, NID_sha512),
  62689. #endif
  62690. };
  62691. printf("\n");
  62692. for (i = 0; i < XELEM_CNT(params) && !EXPECT_FAIL(); i++) {
  62693. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  62694. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  62695. printf("Testing %s with %s...", tls_desc[params[i].tls_ver],
  62696. params[i].siglag);
  62697. switch (params[i].tls_ver) {
  62698. #ifndef WOLFSSL_NO_TLS12
  62699. case WOLFSSL_TLSV1_2:
  62700. client_cbf.method = wolfTLSv1_2_client_method;
  62701. server_cbf.method = wolfTLSv1_2_server_method;
  62702. break;
  62703. #endif
  62704. #ifdef WOLFSSL_TLS13
  62705. case WOLFSSL_TLSV1_3:
  62706. client_cbf.method = wolfTLSv1_3_client_method;
  62707. server_cbf.method = wolfTLSv1_3_server_method;
  62708. break;
  62709. #endif
  62710. default:
  62711. printf("skipping\n");
  62712. continue;
  62713. }
  62714. test_get_signature_nid_siglag = params[i].siglag;
  62715. test_get_signature_nid_sig = params[i].sig_nid;
  62716. test_get_signature_nid_hash = params[i].hash_nid;
  62717. client_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  62718. server_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  62719. client_cbf.on_handshake = test_get_signature_nid_on_hs_client;
  62720. server_cbf.on_handshake = test_get_signature_nid_on_hs_server;
  62721. server_cbf.certPemFile = params[i].server_cert;
  62722. server_cbf.keyPemFile = params[i].server_key;
  62723. client_cbf.caPemFile = params[i].client_ca;
  62724. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  62725. &server_cbf, NULL), TEST_SUCCESS);
  62726. if (EXPECT_SUCCESS())
  62727. printf("passed\n");
  62728. }
  62729. #endif
  62730. return EXPECT_RESULT();
  62731. }
  62732. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  62733. static word32 test_tls_cert_store_unchanged_HashCaTable(Signer** caTable)
  62734. {
  62735. #ifndef NO_MD5
  62736. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  62737. #elif !defined(NO_SHA)
  62738. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  62739. #elif !defined(NO_SHA256)
  62740. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  62741. #else
  62742. #error "We need a digest to hash the Signer object"
  62743. #endif
  62744. byte hashBuf[WC_MAX_DIGEST_SIZE];
  62745. wc_HashAlg hash;
  62746. size_t i;
  62747. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  62748. for (i = 0; i < CA_TABLE_SIZE; i++) {
  62749. Signer* cur;
  62750. for (cur = caTable[i]; cur != NULL; cur = cur->next)
  62751. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)cur,
  62752. sizeof(*cur)), 0);
  62753. }
  62754. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  62755. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  62756. return MakeWordFromHash(hashBuf);
  62757. }
  62758. static word32 test_tls_cert_store_unchanged_before_hashes[2];
  62759. static size_t test_tls_cert_store_unchanged_before_hashes_idx;
  62760. static word32 test_tls_cert_store_unchanged_after_hashes[2];
  62761. static size_t test_tls_cert_store_unchanged_after_hashes_idx;
  62762. static int test_tls_cert_store_unchanged_ctx_ready(WOLFSSL_CTX* ctx)
  62763. {
  62764. EXPECT_DECLS;
  62765. ExpectIntNE(test_tls_cert_store_unchanged_before_hashes
  62766. [test_tls_cert_store_unchanged_before_hashes_idx++] =
  62767. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  62768. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER |
  62769. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  62770. return EXPECT_RESULT();
  62771. }
  62772. static int test_tls_cert_store_unchanged_ctx_cleanup(WOLFSSL_CTX* ctx)
  62773. {
  62774. EXPECT_DECLS;
  62775. ExpectIntEQ(wolfSSL_CTX_UnloadIntermediateCerts(ctx), WOLFSSL_SUCCESS);
  62776. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  62777. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  62778. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  62779. return EXPECT_RESULT();
  62780. }
  62781. static int test_tls_cert_store_unchanged_on_hs(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  62782. {
  62783. EXPECT_DECLS;
  62784. WOLFSSL_CERT_MANAGER* cm;
  62785. (void)ssl;
  62786. /* WARNING: this approach bypasses the reference counter check in
  62787. * wolfSSL_CTX_UnloadIntermediateCerts. It is not recommended as it may
  62788. * cause unexpected behaviour when other active connections try accessing
  62789. * the caTable. */
  62790. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(*ctx));
  62791. ExpectIntEQ(wolfSSL_CertManagerUnloadIntermediateCerts(cm),
  62792. WOLFSSL_SUCCESS);
  62793. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  62794. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  62795. test_tls_cert_store_unchanged_HashCaTable((*ctx)->cm->caTable), 0);
  62796. return EXPECT_RESULT();
  62797. }
  62798. static int test_tls_cert_store_unchanged_ssl_ready(WOLFSSL* ssl)
  62799. {
  62800. EXPECT_DECLS;
  62801. WOLFSSL_CTX* ctx;
  62802. ExpectNotNull(ctx = wolfSSL_get_SSL_CTX(ssl));
  62803. return EXPECT_RESULT();
  62804. }
  62805. #endif
  62806. static int test_tls_cert_store_unchanged(void)
  62807. {
  62808. EXPECT_DECLS;
  62809. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  62810. test_ssl_cbf client_cbf;
  62811. test_ssl_cbf server_cbf;
  62812. int i;
  62813. for (i = 0; i < 2; i++) {
  62814. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  62815. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  62816. test_tls_cert_store_unchanged_before_hashes_idx = 0;
  62817. XMEMSET(test_tls_cert_store_unchanged_before_hashes, 0,
  62818. sizeof(test_tls_cert_store_unchanged_before_hashes));
  62819. test_tls_cert_store_unchanged_after_hashes_idx = 0;
  62820. XMEMSET(test_tls_cert_store_unchanged_after_hashes, 0,
  62821. sizeof(test_tls_cert_store_unchanged_after_hashes));
  62822. client_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  62823. server_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  62824. client_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  62825. server_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  62826. switch (i) {
  62827. case 0:
  62828. client_cbf.on_ctx_cleanup =
  62829. test_tls_cert_store_unchanged_ctx_cleanup;
  62830. server_cbf.on_ctx_cleanup =
  62831. test_tls_cert_store_unchanged_ctx_cleanup;
  62832. break;
  62833. case 1:
  62834. client_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  62835. server_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  62836. break;
  62837. default:
  62838. Fail(("Should not enter here"), ("Entered here"));
  62839. }
  62840. client_cbf.certPemFile = "certs/intermediate/client-chain.pem";
  62841. server_cbf.certPemFile = "certs/intermediate/server-chain.pem";
  62842. server_cbf.caPemFile = caCertFile;
  62843. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  62844. &server_cbf, NULL), TEST_SUCCESS);
  62845. ExpectBufEQ(test_tls_cert_store_unchanged_before_hashes,
  62846. test_tls_cert_store_unchanged_after_hashes,
  62847. sizeof(test_tls_cert_store_unchanged_after_hashes));
  62848. }
  62849. #endif
  62850. return EXPECT_RESULT();
  62851. }
  62852. /*----------------------------------------------------------------------------*
  62853. | Main
  62854. *----------------------------------------------------------------------------*/
  62855. typedef int (*TEST_FUNC)(void);
  62856. typedef struct {
  62857. const char *name;
  62858. TEST_FUNC func;
  62859. byte run:1;
  62860. byte fail:1;
  62861. } TEST_CASE;
  62862. #define TEST_DECL(func) { #func, func, 0, 0 }
  62863. int testAll = 1;
  62864. TEST_CASE testCases[] = {
  62865. TEST_DECL(test_fileAccess),
  62866. /*********************************
  62867. * wolfcrypt
  62868. *********************************/
  62869. TEST_DECL(test_ForceZero),
  62870. TEST_DECL(test_wolfCrypt_Init),
  62871. TEST_DECL(test_wc_LoadStaticMemory_ex),
  62872. /* Locking with Compat Mutex */
  62873. TEST_DECL(test_wc_SetMutexCb),
  62874. TEST_DECL(test_wc_LockMutex_ex),
  62875. /* Digests */
  62876. TEST_DECL(test_wc_InitMd5),
  62877. TEST_DECL(test_wc_Md5Update),
  62878. TEST_DECL(test_wc_Md5Final),
  62879. TEST_DECL(test_wc_InitSha),
  62880. TEST_DECL(test_wc_ShaUpdate),
  62881. TEST_DECL(test_wc_ShaFinal),
  62882. TEST_DECL(test_wc_InitSha256),
  62883. TEST_DECL(test_wc_Sha256Update),
  62884. TEST_DECL(test_wc_Sha256Final),
  62885. TEST_DECL(test_wc_Sha256FinalRaw),
  62886. TEST_DECL(test_wc_Sha256GetFlags),
  62887. TEST_DECL(test_wc_Sha256Free),
  62888. TEST_DECL(test_wc_Sha256GetHash),
  62889. TEST_DECL(test_wc_Sha256Copy),
  62890. TEST_DECL(test_wc_InitSha224),
  62891. TEST_DECL(test_wc_Sha224Update),
  62892. TEST_DECL(test_wc_Sha224Final),
  62893. TEST_DECL(test_wc_Sha224SetFlags),
  62894. TEST_DECL(test_wc_Sha224GetFlags),
  62895. TEST_DECL(test_wc_Sha224Free),
  62896. TEST_DECL(test_wc_Sha224GetHash),
  62897. TEST_DECL(test_wc_Sha224Copy),
  62898. TEST_DECL(test_wc_InitSha512),
  62899. TEST_DECL(test_wc_Sha512Update),
  62900. TEST_DECL(test_wc_Sha512Final),
  62901. TEST_DECL(test_wc_Sha512GetFlags),
  62902. TEST_DECL(test_wc_Sha512FinalRaw),
  62903. TEST_DECL(test_wc_Sha512Free),
  62904. TEST_DECL(test_wc_Sha512GetHash),
  62905. TEST_DECL(test_wc_Sha512Copy),
  62906. TEST_DECL(test_wc_InitSha512_224),
  62907. TEST_DECL(test_wc_Sha512_224Update),
  62908. TEST_DECL(test_wc_Sha512_224Final),
  62909. TEST_DECL(test_wc_Sha512_224GetFlags),
  62910. TEST_DECL(test_wc_Sha512_224FinalRaw),
  62911. TEST_DECL(test_wc_Sha512_224Free),
  62912. TEST_DECL(test_wc_Sha512_224GetHash),
  62913. TEST_DECL(test_wc_Sha512_224Copy),
  62914. TEST_DECL(test_wc_InitSha512_256),
  62915. TEST_DECL(test_wc_Sha512_256Update),
  62916. TEST_DECL(test_wc_Sha512_256Final),
  62917. TEST_DECL(test_wc_Sha512_256GetFlags),
  62918. TEST_DECL(test_wc_Sha512_256FinalRaw),
  62919. TEST_DECL(test_wc_Sha512_256Free),
  62920. TEST_DECL(test_wc_Sha512_256GetHash),
  62921. TEST_DECL(test_wc_Sha512_256Copy),
  62922. TEST_DECL(test_wc_InitSha384),
  62923. TEST_DECL(test_wc_Sha384Update),
  62924. TEST_DECL(test_wc_Sha384Final),
  62925. TEST_DECL(test_wc_Sha384GetFlags),
  62926. TEST_DECL(test_wc_Sha384FinalRaw),
  62927. TEST_DECL(test_wc_Sha384Free),
  62928. TEST_DECL(test_wc_Sha384GetHash),
  62929. TEST_DECL(test_wc_Sha384Copy),
  62930. TEST_DECL(test_wc_InitBlake2b),
  62931. TEST_DECL(test_wc_InitBlake2b_WithKey),
  62932. TEST_DECL(test_wc_InitBlake2s_WithKey),
  62933. TEST_DECL(test_wc_InitRipeMd),
  62934. TEST_DECL(test_wc_RipeMdUpdate),
  62935. TEST_DECL(test_wc_RipeMdFinal),
  62936. TEST_DECL(test_wc_InitSha3),
  62937. TEST_DECL(testing_wc_Sha3_Update),
  62938. TEST_DECL(test_wc_Sha3_224_Final),
  62939. TEST_DECL(test_wc_Sha3_256_Final),
  62940. TEST_DECL(test_wc_Sha3_384_Final),
  62941. TEST_DECL(test_wc_Sha3_512_Final),
  62942. TEST_DECL(test_wc_Sha3_224_Copy),
  62943. TEST_DECL(test_wc_Sha3_256_Copy),
  62944. TEST_DECL(test_wc_Sha3_384_Copy),
  62945. TEST_DECL(test_wc_Sha3_512_Copy),
  62946. TEST_DECL(test_wc_Sha3_GetFlags),
  62947. TEST_DECL(test_wc_InitShake256),
  62948. TEST_DECL(testing_wc_Shake256_Update),
  62949. TEST_DECL(test_wc_Shake256_Final),
  62950. TEST_DECL(test_wc_Shake256_Copy),
  62951. TEST_DECL(test_wc_Shake256Hash),
  62952. /* SM3 Digest */
  62953. TEST_DECL(test_wc_InitSm3Free),
  62954. TEST_DECL(test_wc_Sm3UpdateFinal),
  62955. TEST_DECL(test_wc_Sm3GetHash),
  62956. TEST_DECL(test_wc_Sm3Copy),
  62957. TEST_DECL(test_wc_Sm3FinalRaw),
  62958. TEST_DECL(test_wc_Sm3GetSetFlags),
  62959. TEST_DECL(test_wc_Sm3Hash),
  62960. TEST_DECL(test_wc_HashInit),
  62961. TEST_DECL(test_wc_HashSetFlags),
  62962. TEST_DECL(test_wc_HashGetFlags),
  62963. /* HMAC */
  62964. TEST_DECL(test_wc_Md5HmacSetKey),
  62965. TEST_DECL(test_wc_Md5HmacUpdate),
  62966. TEST_DECL(test_wc_Md5HmacFinal),
  62967. TEST_DECL(test_wc_ShaHmacSetKey),
  62968. TEST_DECL(test_wc_ShaHmacUpdate),
  62969. TEST_DECL(test_wc_ShaHmacFinal),
  62970. TEST_DECL(test_wc_Sha224HmacSetKey),
  62971. TEST_DECL(test_wc_Sha224HmacUpdate),
  62972. TEST_DECL(test_wc_Sha224HmacFinal),
  62973. TEST_DECL(test_wc_Sha256HmacSetKey),
  62974. TEST_DECL(test_wc_Sha256HmacUpdate),
  62975. TEST_DECL(test_wc_Sha256HmacFinal),
  62976. TEST_DECL(test_wc_Sha384HmacSetKey),
  62977. TEST_DECL(test_wc_Sha384HmacUpdate),
  62978. TEST_DECL(test_wc_Sha384HmacFinal),
  62979. /* CMAC */
  62980. TEST_DECL(test_wc_InitCmac),
  62981. TEST_DECL(test_wc_CmacUpdate),
  62982. TEST_DECL(test_wc_CmacFinal),
  62983. TEST_DECL(test_wc_AesCmacGenerate),
  62984. /* Cipher */
  62985. TEST_DECL(test_wc_AesGcmStream),
  62986. TEST_DECL(test_wc_Des3_SetIV),
  62987. TEST_DECL(test_wc_Des3_SetKey),
  62988. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  62989. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  62990. TEST_DECL(test_wc_Des3_EcbEncrypt),
  62991. TEST_DECL(test_wc_Chacha_SetKey),
  62992. TEST_DECL(test_wc_Chacha_Process),
  62993. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  62994. TEST_DECL(test_wc_Poly1305SetKey),
  62995. TEST_DECL(test_wc_CamelliaSetKey),
  62996. TEST_DECL(test_wc_CamelliaSetIV),
  62997. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  62998. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  62999. TEST_DECL(test_wc_Arc4SetKey),
  63000. TEST_DECL(test_wc_Arc4Process),
  63001. TEST_DECL(test_wc_Rc2SetKey),
  63002. TEST_DECL(test_wc_Rc2SetIV),
  63003. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  63004. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  63005. /* AES cipher and GMAC. */
  63006. TEST_DECL(test_wc_AesSetKey),
  63007. TEST_DECL(test_wc_AesSetIV),
  63008. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  63009. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  63010. TEST_DECL(test_wc_AesGcmSetKey),
  63011. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  63012. TEST_DECL(test_wc_AesGcmMixedEncDecLongIV),
  63013. TEST_DECL(test_wc_GmacSetKey),
  63014. TEST_DECL(test_wc_GmacUpdate),
  63015. TEST_DECL(test_wc_AesCcmSetKey),
  63016. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  63017. #if defined(WOLFSSL_AES_EAX) && \
  63018. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  63019. TEST_DECL(test_wc_AesEaxVectors),
  63020. TEST_DECL(test_wc_AesEaxEncryptAuth),
  63021. TEST_DECL(test_wc_AesEaxDecryptAuth),
  63022. #endif /* WOLFSSL_AES_EAX */
  63023. /* SM4 cipher */
  63024. TEST_DECL(test_wc_Sm4),
  63025. TEST_DECL(test_wc_Sm4Ecb),
  63026. TEST_DECL(test_wc_Sm4Cbc),
  63027. TEST_DECL(test_wc_Sm4Ctr),
  63028. TEST_DECL(test_wc_Sm4Gcm),
  63029. TEST_DECL(test_wc_Sm4Ccm),
  63030. /* RNG tests */
  63031. #ifdef HAVE_HASHDRBG
  63032. #ifdef TEST_RESEED_INTERVAL
  63033. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  63034. #endif
  63035. TEST_DECL(test_wc_RNG_GenerateBlock),
  63036. #endif
  63037. TEST_DECL(test_get_rand_digit),
  63038. TEST_DECL(test_wc_InitRngNonce),
  63039. TEST_DECL(test_wc_InitRngNonce_ex),
  63040. /* MP API tests */
  63041. TEST_DECL(test_get_digit_count),
  63042. TEST_DECL(test_mp_cond_copy),
  63043. TEST_DECL(test_mp_rand),
  63044. TEST_DECL(test_get_digit),
  63045. TEST_DECL(test_wc_export_int),
  63046. /* RSA */
  63047. TEST_DECL(test_wc_InitRsaKey),
  63048. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  63049. TEST_DECL(test_wc_RsaPublicKeyDecode),
  63050. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  63051. TEST_DECL(test_wc_MakeRsaKey),
  63052. TEST_DECL(test_wc_CheckProbablePrime),
  63053. TEST_DECL(test_wc_RsaPSS_Verify),
  63054. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  63055. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  63056. TEST_DECL(test_wc_RsaKeyToDer),
  63057. TEST_DECL(test_wc_RsaKeyToPublicDer),
  63058. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  63059. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  63060. TEST_DECL(test_wc_RsaEncryptSize),
  63061. TEST_DECL(test_wc_RsaSSL_SignVerify),
  63062. TEST_DECL(test_wc_RsaFlattenPublicKey),
  63063. TEST_DECL(test_RsaDecryptBoundsCheck),
  63064. /* DSA */
  63065. TEST_DECL(test_wc_InitDsaKey),
  63066. TEST_DECL(test_wc_DsaSignVerify),
  63067. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  63068. TEST_DECL(test_wc_MakeDsaKey),
  63069. TEST_DECL(test_wc_DsaKeyToDer),
  63070. TEST_DECL(test_wc_DsaKeyToPublicDer),
  63071. TEST_DECL(test_wc_DsaImportParamsRaw),
  63072. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  63073. TEST_DECL(test_wc_DsaExportParamsRaw),
  63074. TEST_DECL(test_wc_DsaExportKeyRaw),
  63075. /* DH */
  63076. TEST_DECL(test_wc_DhPublicKeyDecode),
  63077. /* wolfCrypt ECC tests */
  63078. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  63079. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  63080. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  63081. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  63082. !defined(HAVE_SELFTEST) && \
  63083. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  63084. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  63085. #endif
  63086. TEST_DECL(test_wc_ecc_make_key),
  63087. TEST_DECL(test_wc_ecc_init),
  63088. TEST_DECL(test_wc_ecc_check_key),
  63089. TEST_DECL(test_wc_ecc_get_generator),
  63090. TEST_DECL(test_wc_ecc_size),
  63091. TEST_DECL(test_wc_ecc_params),
  63092. TEST_DECL(test_wc_ecc_signVerify_hash),
  63093. TEST_DECL(test_wc_ecc_shared_secret),
  63094. TEST_DECL(test_wc_ecc_export_x963),
  63095. TEST_DECL(test_wc_ecc_export_x963_ex),
  63096. TEST_DECL(test_wc_ecc_import_x963),
  63097. TEST_DECL(test_wc_ecc_import_private_key),
  63098. TEST_DECL(test_wc_ecc_export_private_only),
  63099. TEST_DECL(test_wc_ecc_rs_to_sig),
  63100. TEST_DECL(test_wc_ecc_import_raw),
  63101. TEST_DECL(test_wc_ecc_import_unsigned),
  63102. TEST_DECL(test_wc_ecc_sig_size),
  63103. TEST_DECL(test_wc_ecc_ctx_new),
  63104. TEST_DECL(test_wc_ecc_ctx_reset),
  63105. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  63106. TEST_DECL(test_wc_ecc_ctx_set_info),
  63107. TEST_DECL(test_wc_ecc_encryptDecrypt),
  63108. TEST_DECL(test_wc_ecc_del_point),
  63109. TEST_DECL(test_wc_ecc_pointFns),
  63110. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  63111. TEST_DECL(test_wc_ecc_verify_hash_ex),
  63112. TEST_DECL(test_wc_ecc_mulmod),
  63113. TEST_DECL(test_wc_ecc_is_valid_idx),
  63114. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  63115. TEST_DECL(test_wc_ecc_sig_size_calc),
  63116. TEST_DECL(test_wc_EccPrivateKeyToDer),
  63117. /* SM2 elliptic curve */
  63118. TEST_DECL(test_wc_ecc_sm2_make_key),
  63119. TEST_DECL(test_wc_ecc_sm2_shared_secret),
  63120. TEST_DECL(test_wc_ecc_sm2_create_digest),
  63121. TEST_DECL(test_wc_ecc_sm2_verify_hash_ex),
  63122. TEST_DECL(test_wc_ecc_sm2_verify_hash),
  63123. TEST_DECL(test_wc_ecc_sm2_sign_hash_ex),
  63124. TEST_DECL(test_wc_ecc_sm2_sign_hash),
  63125. /* Curve25519 */
  63126. TEST_DECL(test_wc_curve25519_init),
  63127. TEST_DECL(test_wc_curve25519_size),
  63128. TEST_DECL(test_wc_curve25519_export_key_raw),
  63129. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  63130. TEST_DECL(test_wc_curve25519_make_key),
  63131. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  63132. TEST_DECL(test_wc_curve25519_make_pub),
  63133. TEST_DECL(test_wc_curve25519_export_public_ex),
  63134. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  63135. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  63136. TEST_DECL(test_wc_curve25519_import_private),
  63137. /* ED25519 */
  63138. TEST_DECL(test_wc_ed25519_make_key),
  63139. TEST_DECL(test_wc_ed25519_init),
  63140. TEST_DECL(test_wc_ed25519_sign_msg),
  63141. TEST_DECL(test_wc_ed25519_import_public),
  63142. TEST_DECL(test_wc_ed25519_import_private_key),
  63143. TEST_DECL(test_wc_ed25519_export),
  63144. TEST_DECL(test_wc_ed25519_size),
  63145. TEST_DECL(test_wc_ed25519_exportKey),
  63146. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  63147. TEST_DECL(test_wc_Ed25519KeyToDer),
  63148. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  63149. /* Curve448 */
  63150. TEST_DECL(test_wc_curve448_make_key),
  63151. TEST_DECL(test_wc_curve448_shared_secret_ex),
  63152. TEST_DECL(test_wc_curve448_export_public_ex),
  63153. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  63154. TEST_DECL(test_wc_curve448_export_key_raw),
  63155. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  63156. TEST_DECL(test_wc_curve448_import_private),
  63157. TEST_DECL(test_wc_curve448_init),
  63158. TEST_DECL(test_wc_curve448_size),
  63159. /* Ed448 */
  63160. TEST_DECL(test_wc_ed448_make_key),
  63161. TEST_DECL(test_wc_ed448_init),
  63162. TEST_DECL(test_wc_ed448_sign_msg),
  63163. TEST_DECL(test_wc_ed448_import_public),
  63164. TEST_DECL(test_wc_ed448_import_private_key),
  63165. TEST_DECL(test_wc_ed448_export),
  63166. TEST_DECL(test_wc_ed448_size),
  63167. TEST_DECL(test_wc_ed448_exportKey),
  63168. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  63169. TEST_DECL(test_wc_Ed448KeyToDer),
  63170. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  63171. /* Signature API */
  63172. TEST_DECL(test_wc_SignatureGetSize_ecc),
  63173. TEST_DECL(test_wc_SignatureGetSize_rsa),
  63174. /* PEM and DER APIs. */
  63175. TEST_DECL(test_wc_PemToDer),
  63176. TEST_DECL(test_wc_AllocDer),
  63177. TEST_DECL(test_wc_CertPemToDer),
  63178. TEST_DECL(test_wc_KeyPemToDer),
  63179. TEST_DECL(test_wc_PubKeyPemToDer),
  63180. TEST_DECL(test_wc_PemPubKeyToDer),
  63181. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  63182. TEST_DECL(test_wc_CheckCertSigPubKey),
  63183. /* wolfCrypt ASN tests */
  63184. TEST_DECL(test_ToTraditional),
  63185. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  63186. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  63187. /* Certificate */
  63188. TEST_DECL(test_wc_SetSubjectRaw),
  63189. TEST_DECL(test_wc_GetSubjectRaw),
  63190. TEST_DECL(test_wc_SetIssuerRaw),
  63191. TEST_DECL(test_wc_SetIssueBuffer),
  63192. TEST_DECL(test_wc_SetSubjectKeyId),
  63193. TEST_DECL(test_wc_SetSubject),
  63194. TEST_DECL(test_CheckCertSignature),
  63195. TEST_DECL(test_wc_ParseCert),
  63196. TEST_DECL(test_wc_ParseCert_Error),
  63197. TEST_DECL(test_MakeCertWithPathLen),
  63198. TEST_DECL(test_MakeCertWithCaFalse),
  63199. TEST_DECL(test_wc_SetKeyUsage),
  63200. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  63201. TEST_DECL(test_wc_SetSubjectBuffer),
  63202. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  63203. /* wolfcrypt PKCS#7 */
  63204. TEST_DECL(test_wc_PKCS7_New),
  63205. TEST_DECL(test_wc_PKCS7_Init),
  63206. TEST_DECL(test_wc_PKCS7_InitWithCert),
  63207. TEST_DECL(test_wc_PKCS7_EncodeData),
  63208. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  63209. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  63210. TEST_DECL(test_wc_PKCS7_VerifySignedData_RSA),
  63211. TEST_DECL(test_wc_PKCS7_VerifySignedData_ECC),
  63212. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  63213. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  63214. TEST_DECL(test_wc_PKCS7_Degenerate),
  63215. TEST_DECL(test_wc_PKCS7_BER),
  63216. TEST_DECL(test_wc_PKCS7_signed_enveloped),
  63217. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  63218. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  63219. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  63220. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  63221. /* wolfCrypt PKCS#12 */
  63222. TEST_DECL(test_wc_i2d_PKCS12),
  63223. /*
  63224. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  63225. * avoid memory leaks.
  63226. */
  63227. TEST_DECL(test_wolfCrypt_Cleanup),
  63228. TEST_DECL(test_wolfSSL_Init),
  63229. TEST_DECL(test_dual_alg_support),
  63230. /*********************************
  63231. * OpenSSL compatibility API tests
  63232. *********************************/
  63233. /* If at some point a stub get implemented this test should fail indicating
  63234. * a need to implement a new test case
  63235. */
  63236. TEST_DECL(test_stubs_are_stubs),
  63237. /* ASN.1 compatibility API tests */
  63238. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  63239. TEST_DECL(test_wolfSSL_ASN1_INTEGER),
  63240. TEST_DECL(test_wolfSSL_ASN1_INTEGER_cmp),
  63241. TEST_DECL(test_wolfSSL_ASN1_INTEGER_BN),
  63242. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  63243. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  63244. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  63245. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  63246. TEST_DECL(test_wolfSSL_ASN1_OBJECT),
  63247. TEST_DECL(test_wolfSSL_ASN1_get_object),
  63248. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  63249. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  63250. TEST_DECL(test_wolfSSL_sk_ASN1_OBJECT),
  63251. TEST_DECL(test_wolfSSL_ASN1_STRING),
  63252. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  63253. TEST_DECL(test_wolfSSL_i2s_ASN1_STRING),
  63254. TEST_DECL(test_wolfSSL_ASN1_STRING_canon),
  63255. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  63256. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  63257. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  63258. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  63259. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_print),
  63260. TEST_DECL(test_wolfSSL_ASN1_TIME),
  63261. TEST_DECL(test_wolfSSL_ASN1_TIME_to_string),
  63262. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  63263. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  63264. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  63265. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  63266. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  63267. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  63268. TEST_DECL(test_wolfSSL_ASN1_TYPE),
  63269. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  63270. TEST_DECL(test_wolfSSL_lhash),
  63271. TEST_DECL(test_wolfSSL_certs),
  63272. TEST_DECL(test_wolfSSL_private_keys),
  63273. TEST_DECL(test_wolfSSL_PEM_def_callback),
  63274. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  63275. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  63276. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  63277. TEST_DECL(test_wolfSSL_PEM_PrivateKey_rsa),
  63278. TEST_DECL(test_wolfSSL_PEM_PrivateKey_ecc),
  63279. TEST_DECL(test_wolfSSL_PEM_PrivateKey_dsa),
  63280. TEST_DECL(test_wolfSSL_PEM_PrivateKey_dh),
  63281. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  63282. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  63283. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  63284. #ifndef NO_BIO
  63285. TEST_DECL(test_wolfSSL_BIO),
  63286. TEST_DECL(test_wolfSSL_BIO_BIO_ring_read),
  63287. TEST_DECL(test_wolfSSL_PEM_read_bio),
  63288. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  63289. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  63290. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  63291. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  63292. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  63293. #endif
  63294. /* EVP API testing */
  63295. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  63296. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  63297. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  63298. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  63299. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  63300. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  63301. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  63302. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  63303. TEST_DECL(test_wolfSSL_EVP_shake128),
  63304. TEST_DECL(test_wolfSSL_EVP_shake256),
  63305. TEST_DECL(test_wolfSSL_EVP_sm3),
  63306. TEST_DECL(test_EVP_blake2),
  63307. #ifdef OPENSSL_ALL
  63308. TEST_DECL(test_wolfSSL_EVP_md4),
  63309. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  63310. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  63311. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  63312. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  63313. #endif
  63314. TEST_DECL(test_EVP_MD_do_all),
  63315. TEST_DECL(test_wolfSSL_EVP_MD_size),
  63316. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  63317. TEST_DECL(test_wolfSSL_EVP_Digest),
  63318. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  63319. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  63320. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  63321. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  63322. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  63323. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  63324. TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
  63325. TEST_DECL(test_wolfssl_EVP_aes_ccm),
  63326. TEST_DECL(test_wolfssl_EVP_aes_ccm_zeroLen),
  63327. TEST_DECL(test_wolfssl_EVP_chacha20),
  63328. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  63329. TEST_DECL(test_wolfssl_EVP_sm4_ecb),
  63330. TEST_DECL(test_wolfssl_EVP_sm4_cbc),
  63331. TEST_DECL(test_wolfssl_EVP_sm4_ctr),
  63332. TEST_DECL(test_wolfssl_EVP_sm4_gcm_zeroLen),
  63333. TEST_DECL(test_wolfssl_EVP_sm4_gcm),
  63334. TEST_DECL(test_wolfssl_EVP_sm4_ccm_zeroLen),
  63335. TEST_DECL(test_wolfssl_EVP_sm4_ccm),
  63336. #ifdef OPENSSL_ALL
  63337. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  63338. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  63339. TEST_DECL(test_wolfSSL_EVP_aes_256_ccm),
  63340. TEST_DECL(test_wolfSSL_EVP_aes_192_ccm),
  63341. TEST_DECL(test_wolfSSL_EVP_aes_128_ccm),
  63342. TEST_DECL(test_wolfSSL_EVP_rc4),
  63343. TEST_DECL(test_wolfSSL_EVP_enc_null),
  63344. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  63345. TEST_DECL(test_wolfSSL_EVP_mdc2),
  63346. TEST_DECL(test_evp_cipher_aes_gcm),
  63347. #endif
  63348. TEST_DECL(test_wolfssl_EVP_aria_gcm),
  63349. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  63350. #ifdef OPENSSL_EXTRA
  63351. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  63352. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  63353. #endif
  63354. #ifdef OPENSSL_ALL
  63355. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  63356. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  63357. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  63358. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  63359. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  63360. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  63361. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  63362. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  63363. #endif
  63364. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  63365. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  63366. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  63367. TEST_DECL(test_wolfSSL_EVP_PKEY_up_ref),
  63368. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  63369. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  63370. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  63371. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey_ecc),
  63372. #ifndef NO_BIO
  63373. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  63374. #endif
  63375. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  63376. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  63377. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  63378. #ifndef NO_BIO
  63379. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  63380. #endif /* !NO_BIO */
  63381. #endif
  63382. #ifdef OPENSSL_ALL
  63383. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  63384. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  63385. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  63386. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  63387. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  63388. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  63389. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  63390. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  63391. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  63392. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  63393. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  63394. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  63395. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  63396. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  63397. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  63398. #endif
  63399. TEST_DECL(test_EVP_PKEY_rsa),
  63400. TEST_DECL(test_EVP_PKEY_ec),
  63401. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  63402. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_rsa),
  63403. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_dsa),
  63404. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_ec),
  63405. TEST_DECL(test_EVP_PKEY_cmp),
  63406. #ifdef OPENSSL_ALL
  63407. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  63408. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  63409. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  63410. #endif
  63411. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  63412. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  63413. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  63414. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  63415. #endif
  63416. #ifndef NO_BIO
  63417. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  63418. TEST_DECL(test_wolfSSL_GetLoggingCb),
  63419. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  63420. TEST_DECL(test_wc_ERR_remove_state),
  63421. TEST_DECL(test_wc_ERR_print_errors_fp),
  63422. #endif
  63423. TEST_DECL(test_wolfSSL_configure_args),
  63424. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  63425. TEST_DECL(test_wolfSSL_set1_curves_list),
  63426. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  63427. TEST_DECL(test_wolfSSL_OtherName),
  63428. TEST_DECL(test_wolfSSL_FPKI),
  63429. TEST_DECL(test_wolfSSL_URI),
  63430. TEST_DECL(test_wolfSSL_TBS),
  63431. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  63432. TEST_DECL(test_X509_STORE_untrusted),
  63433. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  63434. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  63435. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  63436. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  63437. TEST_DECL(test_wolfSSL_X509_Name_canon),
  63438. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  63439. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  63440. TEST_DECL(test_wolfSSL_X509_NID),
  63441. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  63442. TEST_DECL(test_wolfSSL_get0_param),
  63443. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  63444. TEST_DECL(test_wolfSSL_set1_host),
  63445. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  63446. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  63447. TEST_DECL(test_wolfSSL_X509_STORE),
  63448. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  63449. TEST_DECL(test_X509_STORE_get0_objects),
  63450. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  63451. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  63452. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  63453. TEST_DECL(test_wolfSSL_X509_cmp_time),
  63454. TEST_DECL(test_wolfSSL_X509_time_adj),
  63455. /* X509 tests */
  63456. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  63457. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  63458. TEST_DECL(test_wolfSSL_X509_check_host),
  63459. TEST_DECL(test_wolfSSL_X509_check_email),
  63460. TEST_DECL(test_wolfSSL_X509_check_private_key),
  63461. TEST_DECL(test_wolfSSL_X509),
  63462. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  63463. TEST_DECL(test_wolfSSL_X509_sign),
  63464. TEST_DECL(test_wolfSSL_X509_sign2),
  63465. TEST_DECL(test_wolfSSL_X509_verify),
  63466. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  63467. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  63468. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  63469. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  63470. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  63471. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  63472. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  63473. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  63474. TEST_DECL(test_wolfSSL_X509_NAME),
  63475. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  63476. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  63477. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  63478. TEST_DECL(test_wolfSSL_X509_set_name),
  63479. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  63480. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  63481. TEST_DECL(test_wolfSSL_X509_set_version),
  63482. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  63483. TEST_DECL(test_wolfSSL_X509_CRL),
  63484. TEST_DECL(test_wolfSSL_i2d_X509),
  63485. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  63486. TEST_DECL(test_wolfSSL_PEM_read_X509),
  63487. TEST_DECL(test_wolfSSL_X509_check_ca),
  63488. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  63489. TEST_DECL(test_wolfSSL_make_cert),
  63490. #ifndef NO_BIO
  63491. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  63492. TEST_DECL(test_wolfSSL_X509_INFO),
  63493. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  63494. #endif
  63495. #ifdef OPENSSL_ALL
  63496. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  63497. #endif
  63498. TEST_DECL(test_wolfSSL_X509_CA_num),
  63499. TEST_DECL(test_wolfSSL_X509_get_version),
  63500. #ifndef NO_BIO
  63501. TEST_DECL(test_wolfSSL_X509_print),
  63502. TEST_DECL(test_wolfSSL_X509_CRL_print),
  63503. #endif
  63504. TEST_DECL(test_X509_get_signature_nid),
  63505. /* X509 extension testing. */
  63506. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  63507. TEST_DECL(test_wolfSSL_X509_get_ext),
  63508. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  63509. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  63510. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  63511. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  63512. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  63513. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  63514. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  63515. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  63516. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  63517. TEST_DECL(test_wolfSSL_X509V3_EXT),
  63518. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  63519. TEST_DECL(test_wolfSSL_X509_cmp),
  63520. TEST_DECL(test_GENERAL_NAME_set0_othername),
  63521. TEST_DECL(test_othername_and_SID_ext),
  63522. TEST_DECL(test_wolfSSL_dup_CA_list),
  63523. /* OpenSSL sk_X509 API test */
  63524. TEST_DECL(test_sk_X509),
  63525. /* OpenSSL sk_X509_CRL API test */
  63526. TEST_DECL(test_sk_X509_CRL),
  63527. /* OpenSSL X509 REQ API test */
  63528. TEST_DECL(test_X509_REQ),
  63529. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  63530. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  63531. TEST_DECL(test_X509_LOOKUP_add_dir),
  63532. /* RAND compatibility API */
  63533. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  63534. TEST_DECL(test_wolfSSL_RAND_bytes),
  63535. TEST_DECL(test_wolfSSL_RAND),
  63536. /* BN compatibility API */
  63537. TEST_DECL(test_wolfSSL_BN_CTX),
  63538. TEST_DECL(test_wolfSSL_BN),
  63539. TEST_DECL(test_wolfSSL_BN_init),
  63540. TEST_DECL(test_wolfSSL_BN_enc_dec),
  63541. TEST_DECL(test_wolfSSL_BN_word),
  63542. TEST_DECL(test_wolfSSL_BN_bits),
  63543. TEST_DECL(test_wolfSSL_BN_shift),
  63544. TEST_DECL(test_wolfSSL_BN_math),
  63545. TEST_DECL(test_wolfSSL_BN_math_mod),
  63546. TEST_DECL(test_wolfSSL_BN_math_other),
  63547. TEST_DECL(test_wolfSSL_BN_rand),
  63548. TEST_DECL(test_wolfSSL_BN_prime),
  63549. /* OpenSSL PKCS5 API test */
  63550. TEST_DECL(test_wolfSSL_PKCS5),
  63551. /* OpenSSL PKCS8 API test */
  63552. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  63553. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  63554. /* OpenSSL PKCS7 API test */
  63555. TEST_DECL(test_wolfssl_PKCS7),
  63556. TEST_DECL(test_wolfSSL_PKCS7_certs),
  63557. TEST_DECL(test_wolfSSL_PKCS7_sign),
  63558. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  63559. #ifndef NO_BIO
  63560. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  63561. #ifdef HAVE_SMIME
  63562. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  63563. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  63564. #endif /* HAVE_SMIME */
  63565. #endif /* !NO_BIO */
  63566. /* OpenSSL PKCS12 API test */
  63567. TEST_DECL(test_wolfSSL_PKCS12),
  63568. /* Can't memory test as callbacks use Assert. */
  63569. TEST_DECL(test_error_queue_per_thread),
  63570. TEST_DECL(test_wolfSSL_ERR_put_error),
  63571. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  63572. #ifndef NO_BIO
  63573. TEST_DECL(test_wolfSSL_ERR_print_errors),
  63574. #endif
  63575. TEST_DECL(test_OBJ_NAME_do_all),
  63576. TEST_DECL(test_wolfSSL_OBJ),
  63577. TEST_DECL(test_wolfSSL_OBJ_cmp),
  63578. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  63579. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  63580. #ifdef OPENSSL_ALL
  63581. TEST_DECL(test_wolfSSL_OBJ_ln),
  63582. TEST_DECL(test_wolfSSL_OBJ_sn),
  63583. #endif
  63584. #ifndef NO_BIO
  63585. TEST_DECL(test_wolfSSL_BIO_gets),
  63586. TEST_DECL(test_wolfSSL_BIO_puts),
  63587. TEST_DECL(test_wolfSSL_BIO_dump),
  63588. /* Can't memory test as server hangs. */
  63589. TEST_DECL(test_wolfSSL_BIO_should_retry),
  63590. TEST_DECL(test_wolfSSL_BIO_write),
  63591. TEST_DECL(test_wolfSSL_BIO_printf),
  63592. TEST_DECL(test_wolfSSL_BIO_f_md),
  63593. TEST_DECL(test_wolfSSL_BIO_up_ref),
  63594. TEST_DECL(test_wolfSSL_BIO_reset),
  63595. TEST_DECL(test_wolfSSL_BIO_get_len),
  63596. #endif
  63597. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  63598. TEST_DECL(test_wolfSSL_check_domain),
  63599. #endif
  63600. TEST_DECL(test_wolfSSL_cert_cb),
  63601. TEST_DECL(test_wolfSSL_cert_cb_dyn_ciphers),
  63602. TEST_DECL(test_wolfSSL_ciphersuite_auth),
  63603. TEST_DECL(test_wolfSSL_sigalg_info),
  63604. /* Can't memory test as tcp_connect aborts. */
  63605. TEST_DECL(test_wolfSSL_SESSION),
  63606. TEST_DECL(test_wolfSSL_SESSION_expire_downgrade),
  63607. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  63608. TEST_DECL(test_wolfSSL_ticket_keys),
  63609. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  63610. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  63611. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  63612. TEST_DECL(test_wolfSSL_verify_mode),
  63613. TEST_DECL(test_wolfSSL_verify_depth),
  63614. TEST_DECL(test_wolfSSL_verify_result),
  63615. TEST_DECL(test_wolfSSL_msg_callback),
  63616. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  63617. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  63618. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  63619. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  63620. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  63621. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  63622. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  63623. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  63624. TEST_DECL(test_wolfSSL_PEM_read),
  63625. TEST_DECL(test_wolfSSL_OpenSSL_version),
  63626. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  63627. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  63628. TEST_DECL(test_CONF_modules_xxx),
  63629. #ifdef OPENSSL_ALL
  63630. TEST_DECL(test_wolfSSL_TXT_DB),
  63631. TEST_DECL(test_wolfSSL_NCONF),
  63632. #endif
  63633. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  63634. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  63635. TEST_DECL(test_wolfSSL_SESSION_get_ex_new_index),
  63636. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  63637. TEST_DECL(test_CRYPTO_THREADID_xxx),
  63638. TEST_DECL(test_ENGINE_cleanup),
  63639. /* test the no op functions for compatibility */
  63640. TEST_DECL(test_no_op_functions),
  63641. /* OpenSSL error API tests */
  63642. TEST_DECL(test_ERR_load_crypto_strings),
  63643. #ifdef OPENSSL_ALL
  63644. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  63645. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  63646. TEST_DECL(test_wolfSSL_CTX_ctrl),
  63647. #endif /* OPENSSL_ALL */
  63648. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  63649. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  63650. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  63651. /*********************************
  63652. * Crypto API tests
  63653. *********************************/
  63654. TEST_DECL(test_wolfSSL_MD4),
  63655. TEST_DECL(test_wolfSSL_MD5),
  63656. TEST_DECL(test_wolfSSL_MD5_Transform),
  63657. TEST_DECL(test_wolfSSL_SHA),
  63658. TEST_DECL(test_wolfSSL_SHA_Transform),
  63659. TEST_DECL(test_wolfSSL_SHA224),
  63660. TEST_DECL(test_wolfSSL_SHA256),
  63661. TEST_DECL(test_wolfSSL_SHA256_Transform),
  63662. TEST_DECL(test_wolfSSL_SHA512_Transform),
  63663. TEST_DECL(test_wolfSSL_SHA512_224_Transform),
  63664. TEST_DECL(test_wolfSSL_SHA512_256_Transform),
  63665. TEST_DECL(test_wolfSSL_HMAC_CTX),
  63666. TEST_DECL(test_wolfSSL_HMAC),
  63667. TEST_DECL(test_wolfSSL_CMAC),
  63668. TEST_DECL(test_wolfSSL_DES),
  63669. TEST_DECL(test_wolfSSL_DES_ncbc),
  63670. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  63671. TEST_DECL(test_wolfSSL_DES_ede3_cbc_encrypt),
  63672. TEST_DECL(test_wolfSSL_AES_encrypt),
  63673. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  63674. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  63675. TEST_DECL(test_wolfSSL_AES_cfb128_encrypt),
  63676. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  63677. TEST_DECL(test_wolfSSL_RC4),
  63678. TEST_DECL(test_wolfSSL_RSA),
  63679. TEST_DECL(test_wolfSSL_RSA_DER),
  63680. TEST_DECL(test_wolfSSL_RSA_print),
  63681. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  63682. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  63683. TEST_DECL(test_wolfSSL_RSA_get0_key),
  63684. TEST_DECL(test_wolfSSL_RSA_meth),
  63685. TEST_DECL(test_wolfSSL_RSA_verify),
  63686. TEST_DECL(test_wolfSSL_RSA_sign),
  63687. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  63688. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  63689. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  63690. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  63691. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  63692. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  63693. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  63694. TEST_DECL(test_wolfSSL_RSA_ex_data),
  63695. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  63696. TEST_DECL(test_wolfSSL_RSA_To_Der),
  63697. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  63698. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  63699. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  63700. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  63701. TEST_DECL(test_wolfSSL_DH),
  63702. TEST_DECL(test_wolfSSL_DH_dup),
  63703. TEST_DECL(test_wolfSSL_DH_check),
  63704. TEST_DECL(test_wolfSSL_DH_prime),
  63705. TEST_DECL(test_wolfSSL_DH_1536_prime),
  63706. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  63707. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  63708. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  63709. TEST_DECL(test_wolfSSL_d2i_DHparams),
  63710. TEST_DECL(test_wolfSSL_DH_LoadDer),
  63711. TEST_DECL(test_wolfSSL_i2d_DHparams),
  63712. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  63713. TEST_DECL(test_wolfSSL_EC_GROUP),
  63714. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  63715. TEST_DECL(test_wolfSSL_EC_POINT),
  63716. TEST_DECL(test_wolfSSL_SPAKE),
  63717. TEST_DECL(test_wolfSSL_EC_KEY_generate),
  63718. TEST_DECL(test_EC_i2d),
  63719. TEST_DECL(test_wolfSSL_EC_curve),
  63720. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  63721. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  63722. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  63723. TEST_DECL(test_wolfSSL_EC_KEY_private_key),
  63724. TEST_DECL(test_wolfSSL_EC_KEY_public_key),
  63725. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  63726. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  63727. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  63728. TEST_DECL(test_ECDSA_size_sign),
  63729. TEST_DECL(test_ECDH_compute_key),
  63730. #endif
  63731. #ifdef OPENSSL_EXTRA
  63732. TEST_DECL(test_EC25519),
  63733. TEST_DECL(test_ED25519),
  63734. TEST_DECL(test_EC448),
  63735. TEST_DECL(test_ED448),
  63736. #endif
  63737. TEST_DECL(test_DSA_do_sign_verify),
  63738. #ifdef OPENSSL_ALL
  63739. TEST_DECL(test_wolfSSL_DSA_generate_parameters),
  63740. TEST_DECL(test_wolfSSL_DSA_SIG),
  63741. #endif
  63742. TEST_DECL(test_openssl_generate_key_and_cert),
  63743. TEST_DECL(test_wolfSSL_FIPS_mode),
  63744. TEST_DECL(test_openssl_FIPS_drbg),
  63745. /*********************************
  63746. * CertManager API tests
  63747. *********************************/
  63748. TEST_DECL(test_wolfSSL_CertManagerAPI),
  63749. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  63750. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer_ex),
  63751. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  63752. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  63753. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  63754. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  63755. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  63756. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  63757. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  63758. TEST_DECL(test_wolfSSL_CertManagerCRL),
  63759. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  63760. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  63761. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  63762. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  63763. !defined(WOLFSSL_NO_CLIENT_AUTH))
  63764. TEST_DECL(test_various_pathlen_chains),
  63765. #endif
  63766. /*********************************
  63767. * SSL/TLS API tests
  63768. *********************************/
  63769. TEST_DECL(test_wolfSSL_Method_Allocators),
  63770. #ifndef NO_WOLFSSL_SERVER
  63771. TEST_DECL(test_wolfSSL_CTX_new),
  63772. #endif
  63773. TEST_DECL(test_server_wolfSSL_new),
  63774. TEST_DECL(test_client_wolfSSL_new),
  63775. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  63776. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  63777. TEST_DECL(test_for_double_Free),
  63778. #endif
  63779. TEST_DECL(test_wolfSSL_set_options),
  63780. #ifdef WOLFSSL_TLS13
  63781. /* TLS v1.3 API tests */
  63782. TEST_DECL(test_tls13_apis),
  63783. TEST_DECL(test_tls13_cipher_suites),
  63784. #endif
  63785. TEST_DECL(test_wolfSSL_tmp_dh),
  63786. TEST_DECL(test_wolfSSL_ctrl),
  63787. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  63788. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  63789. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  63790. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  63791. TEST_DECL(test_wolfSSL_set_SSL_CTX),
  63792. #endif
  63793. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  63794. TEST_DECL(test_wolfSSL_security_level),
  63795. TEST_DECL(test_wolfSSL_SSL_in_init),
  63796. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  63797. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  63798. TEST_DECL(test_CONF_CTX_FILE),
  63799. TEST_DECL(test_CONF_CTX_CMDLINE),
  63800. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  63801. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  63802. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  63803. /* Bad certificate signature tests */
  63804. TEST_DECL(test_EccSigFailure_cm),
  63805. TEST_DECL(test_RsaSigFailure_cm),
  63806. #endif /* NO_CERTS */
  63807. /* PKCS8 testing */
  63808. TEST_DECL(test_wolfSSL_no_password_cb),
  63809. TEST_DECL(test_wolfSSL_PKCS8),
  63810. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  63811. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  63812. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  63813. TEST_DECL(test_wolfSSL_get_finished),
  63814. /* Uses Assert in handshake callback. */
  63815. TEST_DECL(test_wolfSSL_CTX_add_session),
  63816. /* Large number of memory allocations. */
  63817. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls13),
  63818. /* Large number of memory allocations. */
  63819. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls13),
  63820. /* Large number of memory allocations. */
  63821. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls12),
  63822. /* Large number of memory allocations. */
  63823. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls12),
  63824. /* Large number of memory allocations. */
  63825. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls11),
  63826. /* Large number of memory allocations. */
  63827. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls1),
  63828. #endif
  63829. TEST_DECL(test_SSL_CIPHER_get_xxx),
  63830. TEST_DECL(test_wolfSSL_ERR_strings),
  63831. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  63832. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  63833. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  63834. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  63835. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  63836. /* Large number of memory allocations. */
  63837. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  63838. TEST_DECL(test_wolfSSL_CertRsaPss),
  63839. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  63840. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  63841. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  63842. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  63843. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  63844. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  63845. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  63846. TEST_DECL(test_multiple_crls_same_issuer),
  63847. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  63848. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  63849. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  63850. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  63851. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  63852. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  63853. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  63854. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  63855. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  63856. TEST_DECL(test_SetTmpEC_DHE_Sz),
  63857. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  63858. #ifdef WOLFSSL_DTLS
  63859. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  63860. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  63861. #endif
  63862. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  63863. /* Uses Assert in handshake callback. */
  63864. TEST_DECL(test_wolfSSL_dtls_plaintext),
  63865. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  63866. defined(HAVE_IO_TESTS_DEPENDENCIES)
  63867. TEST_DECL(test_wolfSSL_read_write),
  63868. /* Can't memory test as server hangs if client fails before second connect.
  63869. */
  63870. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  63871. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_1),
  63872. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_2),
  63873. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_3),
  63874. TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
  63875. /* Can't memory test as server hangs. */
  63876. TEST_DECL(test_wolfSSL_dtls_export),
  63877. /* Uses Assert in handshake callback. */
  63878. TEST_DECL(test_wolfSSL_tls_export),
  63879. #endif
  63880. TEST_DECL(test_wolfSSL_dtls_export_peers),
  63881. TEST_DECL(test_wolfSSL_SetMinVersion),
  63882. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  63883. /* wolfSSL handshake APIs. */
  63884. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  63885. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  63886. TEST_DECL(test_wolfSSL_BUF),
  63887. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  63888. /* Can't memory test as server hangs. */
  63889. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  63890. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  63891. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  63892. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  63893. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  63894. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  63895. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  63896. /* Can't memory test as server hangs. */
  63897. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  63898. TEST_DECL(test_wolfSSL_Tls13_postauth),
  63899. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  63900. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  63901. TEST_DECL(test_wolfSSL_CTX_set_max_proto_version),
  63902. TEST_DECL(test_wolfSSL_THREADID_hash),
  63903. /* TLS extensions tests */
  63904. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  63905. #ifdef HAVE_SNI
  63906. TEST_DECL(test_wolfSSL_UseSNI_params),
  63907. /* Uses Assert in handshake callback. */
  63908. TEST_DECL(test_wolfSSL_UseSNI_connection),
  63909. TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
  63910. #endif /* HAVE_SNI */
  63911. #endif
  63912. TEST_DECL(test_wolfSSL_UseTrustedCA),
  63913. TEST_DECL(test_wolfSSL_UseMaxFragment),
  63914. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  63915. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  63916. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  63917. /* Uses Assert in handshake callback. */
  63918. TEST_DECL(test_wolfSSL_UseALPN_connection),
  63919. TEST_DECL(test_wolfSSL_UseALPN_params),
  63920. #endif
  63921. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  63922. /* Uses Assert in handshake callback. */
  63923. TEST_DECL(test_wolfSSL_set_alpn_protos),
  63924. #endif
  63925. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  63926. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  63927. TEST_DECL(test_wolfSSL_SCR_Reconnect),
  63928. TEST_DECL(test_tls_ext_duplicate),
  63929. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  63930. defined(HAVE_IO_TESTS_DEPENDENCIES)
  63931. TEST_DECL(test_wolfSSL_Tls13_ECH_params),
  63932. /* Uses Assert in handshake callback. */
  63933. TEST_DECL(test_wolfSSL_Tls13_ECH),
  63934. #endif
  63935. TEST_DECL(test_wolfSSL_X509_TLS_version_test_1),
  63936. TEST_DECL(test_wolfSSL_X509_TLS_version_test_2),
  63937. /* OCSP Stapling */
  63938. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  63939. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  63940. TEST_DECL(test_self_signed_stapling),
  63941. /* Multicast */
  63942. TEST_DECL(test_wolfSSL_mcast),
  63943. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  63944. TEST_DECL(test_wolfSSL_msgCb),
  63945. TEST_DECL(test_wolfSSL_either_side),
  63946. TEST_DECL(test_wolfSSL_DTLS_either_side),
  63947. /* Uses Assert in handshake callback. */
  63948. TEST_DECL(test_wolfSSL_dtls_fragments),
  63949. /* Uses Assert in handshake callback. */
  63950. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  63951. /* Uses Assert in handshake callback. */
  63952. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  63953. /* Uses Assert in handshake callback. */
  63954. TEST_DECL(test_wolfSSL_dtls_bad_record),
  63955. /* Uses Assert in handshake callback. */
  63956. TEST_DECL(test_wolfSSL_dtls_stateless),
  63957. TEST_DECL(test_generate_cookie),
  63958. #ifndef NO_BIO
  63959. /* Can't memory test as server hangs. */
  63960. TEST_DECL(test_wolfSSL_BIO_connect),
  63961. /* Can't memory test as server Asserts in thread. */
  63962. TEST_DECL(test_wolfSSL_BIO_accept),
  63963. TEST_DECL(test_wolfSSL_BIO_tls),
  63964. #endif
  63965. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  63966. TEST_DECL(test_DhCallbacks),
  63967. #endif
  63968. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  63969. TEST_DECL(test_export_keying_material),
  63970. #endif
  63971. /* Can't memory test as client/server Asserts in thread. */
  63972. TEST_DECL(test_ticket_and_psk_mixing),
  63973. /* Can't memory test as client/server Asserts in thread. */
  63974. TEST_DECL(test_prioritize_psk),
  63975. /* Can't memory test as client/server hangs. */
  63976. TEST_DECL(test_wc_CryptoCb),
  63977. /* Can't memory test as client/server hangs. */
  63978. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  63979. #if !defined(NO_FILESYSTEM) && \
  63980. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  63981. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  63982. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  63983. TEST_DECL(test_wolfSSL_dtls_stateless_resume),
  63984. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  63985. #ifdef HAVE_MAX_FRAGMENT
  63986. TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
  63987. #endif /* HAVE_MAX_FRAGMENT */
  63988. #ifndef NO_RSA
  63989. TEST_DECL(test_wolfSSL_dtls_stateless2),
  63990. #if !defined(NO_OLD_TLS)
  63991. TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
  63992. #endif /* !defined(NO_OLD_TLS) */
  63993. #endif /* ! NO_RSA */
  63994. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  63995. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
  63996. TEST_DECL(test_wolfSSL_CTX_set_ciphersuites),
  63997. TEST_DECL(test_wolfSSL_CRL_CERT_REVOKED_alert),
  63998. TEST_DECL(test_TLS_13_ticket_different_ciphers),
  63999. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  64000. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  64001. && defined(WOLFSSL_TLS13) && \
  64002. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  64003. TEST_DECL(test_ticket_nonce_malloc),
  64004. #endif
  64005. TEST_DECL(test_ticket_ret_create),
  64006. TEST_DECL(test_extra_alerts_wrong_cs),
  64007. TEST_DECL(test_extra_alerts_skip_hs),
  64008. TEST_DECL(test_extra_alerts_bad_psk),
  64009. TEST_DECL(test_tls13_bad_psk_binder),
  64010. /* Can't memory test as client/server Asserts. */
  64011. TEST_DECL(test_harden_no_secure_renegotiation),
  64012. TEST_DECL(test_override_alt_cert_chain),
  64013. TEST_DECL(test_rpk_set_xxx_cert_type),
  64014. TEST_DECL(test_tls13_rpk_handshake),
  64015. TEST_DECL(test_dtls13_bad_epoch_ch),
  64016. TEST_DECL(test_short_session_id),
  64017. TEST_DECL(test_wolfSSL_dtls13_null_cipher),
  64018. /* Can't memory test as client/server hangs. */
  64019. TEST_DECL(test_dtls_msg_from_other_peer),
  64020. TEST_DECL(test_dtls_ipv6_check),
  64021. TEST_DECL(test_wolfSSL_SCR_after_resumption),
  64022. TEST_DECL(test_dtls_no_extensions),
  64023. TEST_DECL(test_TLSX_CA_NAMES_bad_extension),
  64024. TEST_DECL(test_dtls_1_0_hvr_downgrade),
  64025. TEST_DECL(test_session_ticket_no_id),
  64026. TEST_DECL(test_session_ticket_hs_update),
  64027. TEST_DECL(test_dtls_downgrade_scr_server),
  64028. TEST_DECL(test_dtls_downgrade_scr),
  64029. TEST_DECL(test_dtls_client_hello_timeout_downgrade),
  64030. TEST_DECL(test_dtls_client_hello_timeout),
  64031. TEST_DECL(test_dtls_dropped_ccs),
  64032. TEST_DECL(test_dtls_seq_num_downgrade),
  64033. TEST_DECL(test_certreq_sighash_algos),
  64034. TEST_DECL(test_revoked_loaded_int_cert),
  64035. TEST_DECL(test_dtls_frag_ch),
  64036. TEST_DECL(test_dtls13_frag_ch_pq),
  64037. TEST_DECL(test_dtls_empty_keyshare_with_cookie),
  64038. TEST_DECL(test_tls13_pq_groups),
  64039. TEST_DECL(test_tls13_early_data),
  64040. TEST_DECL(test_tls_multi_handshakes_one_record),
  64041. TEST_DECL(test_write_dup),
  64042. TEST_DECL(test_read_write_hs),
  64043. TEST_DECL(test_get_signature_nid),
  64044. TEST_DECL(test_tls_cert_store_unchanged),
  64045. /* This test needs to stay at the end to clean up any caches allocated. */
  64046. TEST_DECL(test_wolfSSL_Cleanup)
  64047. };
  64048. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  64049. static void TestSetup(void)
  64050. {
  64051. /* Stub, for now. Add common test setup code here. */
  64052. }
  64053. static void TestCleanup(void)
  64054. {
  64055. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  64056. /* Clear any errors added to the error queue during the test run. */
  64057. wolfSSL_ERR_clear_error();
  64058. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  64059. }
  64060. /* Print out all API test cases with numeric identifier.
  64061. */
  64062. void ApiTest_PrintTestCases(void)
  64063. {
  64064. int i;
  64065. printf("All Test Cases:\n");
  64066. for (i = 0; i < TEST_CASE_CNT; i++) {
  64067. printf("%3d: %s\n", i + 1, testCases[i].name);
  64068. }
  64069. }
  64070. /* Add test case with index to the list to run.
  64071. *
  64072. * @param [in] idx Index of test case to run starting at 1.
  64073. * @return 0 on success.
  64074. * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
  64075. */
  64076. int ApiTest_RunIdx(int idx)
  64077. {
  64078. if (idx < 1 || idx > TEST_CASE_CNT) {
  64079. printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
  64080. return BAD_FUNC_ARG;
  64081. }
  64082. testAll = 0;
  64083. testCases[idx-1].run = 1;
  64084. return 0;
  64085. }
  64086. /* Add test case with name to the list to run.
  64087. *
  64088. * @param [in] name Name of test case to run.
  64089. * @return 0 on success.
  64090. * @return BAD_FUNC_ARG when name is not a known test case name.
  64091. */
  64092. int ApiTest_RunName(char* name)
  64093. {
  64094. int i;
  64095. for (i = 0; i < TEST_CASE_CNT; i++) {
  64096. if (XSTRCMP(testCases[i].name, name) == 0) {
  64097. testAll = 0;
  64098. testCases[i].run = 1;
  64099. return 0;
  64100. }
  64101. }
  64102. printf("Test case name not found: %s\n", name);
  64103. printf("Use --list to see all test case names.\n");
  64104. return BAD_FUNC_ARG;
  64105. }
  64106. /* Converts the result code to a string.
  64107. *
  64108. * @param [in] res Test result code.
  64109. * @return String describing test result.
  64110. */
  64111. static const char* apitest_res_string(int res)
  64112. {
  64113. const char* str = "invalid result";
  64114. switch (res) {
  64115. case TEST_SUCCESS:
  64116. str = "passed";
  64117. break;
  64118. case TEST_FAIL:
  64119. str = "failed";
  64120. break;
  64121. case TEST_SKIPPED:
  64122. str = "skipped";
  64123. break;
  64124. }
  64125. return str;
  64126. }
  64127. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  64128. static double gettime_secs(void)
  64129. #if defined(_MSC_VER) && defined(_WIN32)
  64130. {
  64131. /* there's no gettimeofday for Windows, so we'll use system time */
  64132. #define EPOCH_DIFF 11644473600LL
  64133. FILETIME currentFileTime;
  64134. GetSystemTimePreciseAsFileTime(&currentFileTime);
  64135. ULARGE_INTEGER uli = { 0, 0 };
  64136. uli.LowPart = currentFileTime.dwLowDateTime;
  64137. uli.HighPart = currentFileTime.dwHighDateTime;
  64138. /* Convert to seconds since Unix epoch */
  64139. return (double)((uli.QuadPart - (EPOCH_DIFF * 10000000)) / 10000000.0);
  64140. }
  64141. #else
  64142. {
  64143. struct timeval tv;
  64144. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  64145. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
  64146. }
  64147. #endif
  64148. #endif
  64149. int ApiTest(void)
  64150. {
  64151. int i;
  64152. int ret;
  64153. int res = 0;
  64154. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  64155. double timeDiff;
  64156. #endif
  64157. printf(" Begin API Tests\n");
  64158. fflush(stdout);
  64159. /* we must perform init and cleanup if not all tests are running */
  64160. if (!testAll) {
  64161. #ifdef WOLFCRYPT_ONLY
  64162. if (wolfCrypt_Init() != 0) {
  64163. printf("wolfCrypt Initialization failed\n");
  64164. res = 1;
  64165. }
  64166. #else
  64167. if (wolfSSL_Init() != WOLFSSL_SUCCESS) {
  64168. printf("wolfSSL Initialization failed\n");
  64169. res = 1;
  64170. }
  64171. #endif
  64172. }
  64173. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  64174. if (res == 0) {
  64175. if (create_tmp_dir(tmpDirName, sizeof(tmpDirName) - 1) == NULL) {
  64176. printf("failed to create tmp dir\n");
  64177. res = 1;
  64178. }
  64179. else {
  64180. tmpDirNameSet = 1;
  64181. }
  64182. }
  64183. #endif
  64184. if (res == 0) {
  64185. for (i = 0; i < TEST_CASE_CNT; ++i) {
  64186. EXPECT_DECLS;
  64187. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  64188. currentTestName = testCases[i].name;
  64189. #endif
  64190. /* When not testing all cases then skip if not marked for running.
  64191. */
  64192. if (!testAll && !testCases[i].run) {
  64193. continue;
  64194. }
  64195. TestSetup();
  64196. printf(" %3d: %-52s:", i + 1, testCases[i].name);
  64197. fflush(stdout);
  64198. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  64199. timeDiff = gettime_secs();
  64200. #endif
  64201. ret = testCases[i].func();
  64202. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  64203. timeDiff = gettime_secs() - timeDiff;
  64204. #endif
  64205. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  64206. if (ret != TEST_SKIPPED) {
  64207. printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
  64208. }
  64209. else
  64210. #endif
  64211. {
  64212. printf(" %s\n", apitest_res_string(ret));
  64213. }
  64214. fflush(stdout);
  64215. /* if return code is < 0 and not skipped then assert error */
  64216. Expect((ret > 0 || ret == TEST_SKIPPED),
  64217. ("Test failed\n"),
  64218. ("ret %d", ret));
  64219. testCases[i].fail = ((ret <= 0) && (ret != TEST_SKIPPED));
  64220. res |= ((ret <= 0) && (ret != TEST_SKIPPED));
  64221. TestCleanup();
  64222. }
  64223. }
  64224. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  64225. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  64226. wc_ecc_fp_free(); /* free per thread cache */
  64227. #endif
  64228. if (!testAll) {
  64229. #ifdef WOLFCRYPT_ONLY
  64230. wolfCrypt_Cleanup();
  64231. #else
  64232. wolfSSL_Cleanup();
  64233. #endif
  64234. }
  64235. (void)testDevId;
  64236. if (res != 0) {
  64237. printf("\nFAILURES:\n");
  64238. for (i = 0; i < TEST_CASE_CNT; ++i) {
  64239. if (testCases[i].fail) {
  64240. printf(" %3d: %s\n", i + 1, testCases[i].name);
  64241. }
  64242. }
  64243. printf("\n");
  64244. fflush(stdout);
  64245. }
  64246. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  64247. if (tmpDirNameSet) {
  64248. printf("\nBinary dumps of the memio streams can be found in the\n"
  64249. "%s directory. This can be imported into\n"
  64250. "Wireshark by transforming the file with\n"
  64251. "\tod -Ax -tx1 -v stream.dump > stream.dump.hex\n"
  64252. "And then loading test_output.dump.hex into Wireshark using\n"
  64253. "the \"Import from Hex Dump...\" option and selecting the\n"
  64254. "TCP encapsulation option.\n", tmpDirName);
  64255. }
  64256. #endif
  64257. printf(" End API Tests\n");
  64258. fflush(stdout);
  64259. return res;
  64260. }